2 * shaders implementation
4 * Copyright 2002-2003 Jason Edmeades
5 * Copyright 2002-2003 Raphael Junqueira
6 * Copyright 2004 Christian Costa
7 * Copyright 2005 Oliver Stieber
8 * Copyright 2006 Ivan Gyurdiev
9 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "wined3d_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
35 #define GLINFO_LOCATION ((IWineD3DDeviceImpl *) This->baseShader.device)->adapter->gl_info
37 static HRESULT WINAPI IWineD3DPixelShaderImpl_QueryInterface(IWineD3DPixelShader *iface, REFIID riid, LPVOID *ppobj) {
38 TRACE("iface %p, riid %s, ppobj %p\n", iface, debugstr_guid(riid), ppobj);
40 if (IsEqualGUID(riid, &IID_IWineD3DPixelShader)
41 || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
42 || IsEqualGUID(riid, &IID_IWineD3DBase)
43 || IsEqualGUID(riid, &IID_IUnknown))
45 IUnknown_AddRef(iface);
50 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
56 static ULONG WINAPI IWineD3DPixelShaderImpl_AddRef(IWineD3DPixelShader *iface) {
57 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
58 ULONG refcount = InterlockedIncrement(&This->baseShader.ref);
60 TRACE("%p increasing refcount to %u\n", This, refcount);
65 static ULONG WINAPI IWineD3DPixelShaderImpl_Release(IWineD3DPixelShader *iface) {
66 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
67 ULONG refcount = InterlockedDecrement(&This->baseShader.ref);
69 TRACE("%p decreasing refcount to %u\n", This, refcount);
73 shader_cleanup((IWineD3DBaseShader *)iface);
74 HeapFree(GetProcessHeap(), 0, This);
80 /* *******************************************
81 IWineD3DPixelShader IWineD3DPixelShader parts follow
82 ******************************************* */
84 static HRESULT WINAPI IWineD3DPixelShaderImpl_GetParent(IWineD3DPixelShader *iface, IUnknown** parent){
85 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
87 *parent = This->parent;
88 IUnknown_AddRef(*parent);
89 TRACE("(%p) : returning %p\n", This, *parent);
93 static HRESULT WINAPI IWineD3DPixelShaderImpl_GetDevice(IWineD3DPixelShader* iface, IWineD3DDevice **pDevice){
94 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
95 IWineD3DDevice_AddRef(This->baseShader.device);
96 *pDevice = This->baseShader.device;
97 TRACE("(%p) returning %p\n", This, *pDevice);
102 static HRESULT WINAPI IWineD3DPixelShaderImpl_GetFunction(IWineD3DPixelShader* impl, VOID* pData, UINT* pSizeOfData) {
103 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)impl;
104 TRACE("(%p) : pData(%p), pSizeOfData(%p)\n", This, pData, pSizeOfData);
107 *pSizeOfData = This->baseShader.functionLength;
110 if (*pSizeOfData < This->baseShader.functionLength) {
111 /* MSDN claims (for d3d8 at least) that if *pSizeOfData is smaller
112 * than the required size we should write the required size and
113 * return D3DERR_MOREDATA. That's not actually true. */
114 return WINED3DERR_INVALIDCALL;
117 TRACE("(%p) : GetFunction copying to %p\n", This, pData);
118 memcpy(pData, This->baseShader.function, This->baseShader.functionLength);
123 static void pshader_set_limits(IWineD3DPixelShaderImpl *This)
125 This->baseShader.limits.attributes = 0;
126 This->baseShader.limits.address = 0;
127 This->baseShader.limits.packed_output = 0;
129 switch (This->baseShader.reg_maps.shader_version)
131 case WINED3DPS_VERSION(1,0):
132 case WINED3DPS_VERSION(1,1):
133 case WINED3DPS_VERSION(1,2):
134 case WINED3DPS_VERSION(1,3):
135 This->baseShader.limits.temporary = 2;
136 This->baseShader.limits.constant_float = 8;
137 This->baseShader.limits.constant_int = 0;
138 This->baseShader.limits.constant_bool = 0;
139 This->baseShader.limits.texcoord = 4;
140 This->baseShader.limits.sampler = 4;
141 This->baseShader.limits.packed_input = 0;
142 This->baseShader.limits.label = 0;
145 case WINED3DPS_VERSION(1,4):
146 This->baseShader.limits.temporary = 6;
147 This->baseShader.limits.constant_float = 8;
148 This->baseShader.limits.constant_int = 0;
149 This->baseShader.limits.constant_bool = 0;
150 This->baseShader.limits.texcoord = 6;
151 This->baseShader.limits.sampler = 6;
152 This->baseShader.limits.packed_input = 0;
153 This->baseShader.limits.label = 0;
156 /* FIXME: temporaries must match D3DPSHADERCAPS2_0.NumTemps */
157 case WINED3DPS_VERSION(2,0):
158 This->baseShader.limits.temporary = 32;
159 This->baseShader.limits.constant_float = 32;
160 This->baseShader.limits.constant_int = 16;
161 This->baseShader.limits.constant_bool = 16;
162 This->baseShader.limits.texcoord = 8;
163 This->baseShader.limits.sampler = 16;
164 This->baseShader.limits.packed_input = 0;
167 case WINED3DPS_VERSION(2,1):
168 This->baseShader.limits.temporary = 32;
169 This->baseShader.limits.constant_float = 32;
170 This->baseShader.limits.constant_int = 16;
171 This->baseShader.limits.constant_bool = 16;
172 This->baseShader.limits.texcoord = 8;
173 This->baseShader.limits.sampler = 16;
174 This->baseShader.limits.packed_input = 0;
175 This->baseShader.limits.label = 16;
178 case WINED3DPS_VERSION(3,0):
179 This->baseShader.limits.temporary = 32;
180 This->baseShader.limits.constant_float = 224;
181 This->baseShader.limits.constant_int = 16;
182 This->baseShader.limits.constant_bool = 16;
183 This->baseShader.limits.texcoord = 0;
184 This->baseShader.limits.sampler = 16;
185 This->baseShader.limits.packed_input = 12;
186 This->baseShader.limits.label = 16; /* FIXME: 2048 */
190 This->baseShader.limits.temporary = 32;
191 This->baseShader.limits.constant_float = 32;
192 This->baseShader.limits.constant_int = 16;
193 This->baseShader.limits.constant_bool = 16;
194 This->baseShader.limits.texcoord = 8;
195 This->baseShader.limits.sampler = 16;
196 This->baseShader.limits.packed_input = 0;
197 This->baseShader.limits.label = 0;
198 FIXME("Unrecognized pixel shader version %#x\n",
199 This->baseShader.reg_maps.shader_version);
203 static HRESULT WINAPI IWineD3DPixelShaderImpl_SetFunction(IWineD3DPixelShader *iface, CONST DWORD *pFunction) {
205 IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
206 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *) This->baseShader.device;
207 unsigned int i, highest_reg_used = 0, num_regs_used = 0;
208 shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
209 const struct wined3d_shader_frontend *fe;
212 TRACE("(%p) : pFunction %p\n", iface, pFunction);
214 fe = shader_select_frontend(*pFunction);
217 FIXME("Unable to find frontend for shader.\n");
218 return WINED3DERR_INVALIDCALL;
220 This->baseShader.frontend = fe;
221 This->baseShader.frontend_data = fe->shader_init(pFunction);
222 if (!This->baseShader.frontend_data)
224 FIXME("Failed to initialize frontend.\n");
225 return WINED3DERR_INVALIDCALL;
228 /* First pass: trace shader */
229 if (TRACE_ON(d3d_shader)) shader_trace_init(fe, This->baseShader.frontend_data, pFunction);
231 /* Initialize immediate constant lists */
232 list_init(&This->baseShader.constantsF);
233 list_init(&This->baseShader.constantsB);
234 list_init(&This->baseShader.constantsI);
236 /* Second pass: figure out which registers are used, what the semantics are, etc.. */
237 hr = shader_get_registers_used((IWineD3DBaseShader *)This, fe, reg_maps, This->semantics_in, NULL, pFunction);
238 if (FAILED(hr)) return hr;
240 pshader_set_limits(This);
242 for (i = 0; i < MAX_REG_INPUT; ++i)
244 if (This->input_reg_used[i])
247 highest_reg_used = i;
251 /* Don't do any register mapping magic if it is not needed, or if we can't
252 * achieve anything anyway */
253 if (highest_reg_used < (GL_LIMITS(glsl_varyings) / 4)
254 || num_regs_used > (GL_LIMITS(glsl_varyings) / 4))
256 if (num_regs_used > (GL_LIMITS(glsl_varyings) / 4))
258 /* This happens with relative addressing. The input mapper function
259 * warns about this if the higher registers are declared too, so
260 * don't write a FIXME here */
261 WARN("More varying registers used than supported\n");
264 for (i = 0; i < MAX_REG_INPUT; ++i)
266 This->input_reg_map[i] = i;
269 This->declared_in_count = highest_reg_used + 1;
273 This->declared_in_count = 0;
274 for (i = 0; i < MAX_REG_INPUT; ++i)
276 if (This->input_reg_used[i]) This->input_reg_map[i] = This->declared_in_count++;
277 else This->input_reg_map[i] = ~0U;
281 This->baseShader.load_local_constsF = FALSE;
283 This->baseShader.shader_mode = deviceImpl->ps_selected_mode;
285 TRACE("(%p) : Copying the function\n", This);
287 This->baseShader.function = HeapAlloc(GetProcessHeap(), 0, This->baseShader.functionLength);
288 if (!This->baseShader.function) return E_OUTOFMEMORY;
289 memcpy(This->baseShader.function, pFunction, This->baseShader.functionLength);
294 static void pixelshader_update_samplers(struct shader_reg_maps *reg_maps, IWineD3DBaseTexture * const *textures)
296 DWORD shader_version = reg_maps->shader_version;
297 WINED3DSAMPLER_TEXTURE_TYPE *sampler_type = reg_maps->sampler_type;
300 if (WINED3DSHADER_VERSION_MAJOR(shader_version) != 1) return;
302 for (i = 0; i < max(MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS); ++i)
304 /* We don't sample from this sampler */
305 if (!sampler_type[i]) continue;
309 ERR("No texture bound to sampler %u, using 2D\n", i);
310 sampler_type[i] = WINED3DSTT_2D;
314 switch (IWineD3DBaseTexture_GetTextureDimensions(textures[i]))
316 case GL_TEXTURE_RECTANGLE_ARB:
318 /* We have to select between texture rectangles and 2D textures later because 2.0 and
319 * 3.0 shaders only have WINED3DSTT_2D as well */
320 sampler_type[i] = WINED3DSTT_2D;
324 sampler_type[i] = WINED3DSTT_VOLUME;
327 case GL_TEXTURE_CUBE_MAP_ARB:
328 sampler_type[i] = WINED3DSTT_CUBE;
332 FIXME("Unrecognized texture type %#x, using 2D\n",
333 IWineD3DBaseTexture_GetTextureDimensions(textures[i]));
334 sampler_type[i] = WINED3DSTT_2D;
339 static GLuint pixelshader_compile(IWineD3DPixelShaderImpl *This, const struct ps_compile_args *args)
341 CONST DWORD *function = This->baseShader.function;
343 SHADER_BUFFER buffer;
344 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
346 TRACE("(%p) : function %p\n", This, function);
348 pixelshader_update_samplers(&This->baseShader.reg_maps,
349 ((IWineD3DDeviceImpl *)This->baseShader.device)->stateBlock->textures);
351 /* Generate the HW shader */
352 TRACE("(%p) : Generating hardware program\n", This);
353 This->cur_args = args;
354 shader_buffer_init(&buffer);
355 retval = device->shader_backend->shader_generate_pshader((IWineD3DPixelShader *)This, &buffer, args);
356 shader_buffer_free(&buffer);
357 This->cur_args = NULL;
362 const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
364 /*** IUnknown methods ***/
365 IWineD3DPixelShaderImpl_QueryInterface,
366 IWineD3DPixelShaderImpl_AddRef,
367 IWineD3DPixelShaderImpl_Release,
368 /*** IWineD3DBase methods ***/
369 IWineD3DPixelShaderImpl_GetParent,
370 /*** IWineD3DBaseShader methods ***/
371 IWineD3DPixelShaderImpl_SetFunction,
372 /*** IWineD3DPixelShader methods ***/
373 IWineD3DPixelShaderImpl_GetDevice,
374 IWineD3DPixelShaderImpl_GetFunction
377 void find_ps_compile_args(IWineD3DPixelShaderImpl *shader, IWineD3DStateBlockImpl *stateblock, struct ps_compile_args *args) {
379 IWineD3DBaseTextureImpl *tex;
381 memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set */
382 args->srgb_correction = stateblock->renderState[WINED3DRS_SRGBWRITEENABLE] ? 1 : 0;
385 for(i = 0; i < MAX_FRAGMENT_SAMPLERS; i++) {
386 if (!shader->baseShader.reg_maps.sampler_type[i]) continue;
387 tex = (IWineD3DBaseTextureImpl *) stateblock->textures[i];
389 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
392 args->color_fixup[i] = tex->resource.format_desc->color_fixup;
394 /* Flag samplers that need NP2 texcoord fixup. */
395 if(!tex->baseTexture.pow2Matrix_identity) {
396 args->np2_fixup |= (1 << i);
399 if (shader->baseShader.reg_maps.shader_version >= WINED3DPS_VERSION(3,0))
401 if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed)
403 args->vp_mode = pretransformed;
405 else if (use_vs(stateblock))
407 args->vp_mode = vertexshader;
409 args->vp_mode = fixedfunction;
413 args->vp_mode = vertexshader;
414 if(stateblock->renderState[WINED3DRS_FOGENABLE]) {
415 switch(stateblock->renderState[WINED3DRS_FOGTABLEMODE]) {
416 case WINED3DFOG_NONE:
417 if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed
418 || use_vs(stateblock))
420 args->fog = FOG_LINEAR;
423 switch(stateblock->renderState[WINED3DRS_FOGVERTEXMODE]) {
424 case WINED3DFOG_NONE: /* Drop through */
425 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
426 case WINED3DFOG_EXP: args->fog = FOG_EXP; break;
427 case WINED3DFOG_EXP2: args->fog = FOG_EXP2; break;
431 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
432 case WINED3DFOG_EXP: args->fog = FOG_EXP; break;
433 case WINED3DFOG_EXP2: args->fog = FOG_EXP2; break;
441 GLuint find_gl_pshader(IWineD3DPixelShaderImpl *shader, const struct ps_compile_args *args)
445 struct ps_compiled_shader *new_array;
447 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
448 * so a linear search is more performant than a hashmap or a binary search
449 * (cache coherency etc)
451 for(i = 0; i < shader->num_gl_shaders; i++) {
452 if(memcmp(&shader->gl_shaders[i].args, args, sizeof(*args)) == 0) {
453 return shader->gl_shaders[i].prgId;
457 TRACE("No matching GL shader found, compiling a new shader\n");
458 if(shader->shader_array_size == shader->num_gl_shaders) {
459 if (shader->num_gl_shaders)
461 new_size = shader->shader_array_size + max(1, shader->shader_array_size / 2);
462 new_array = HeapReAlloc(GetProcessHeap(), 0, shader->gl_shaders,
463 new_size * sizeof(*shader->gl_shaders));
465 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader->gl_shaders));
470 ERR("Out of memory\n");
473 shader->gl_shaders = new_array;
474 shader->shader_array_size = new_size;
477 shader->gl_shaders[shader->num_gl_shaders].args = *args;
478 shader->gl_shaders[shader->num_gl_shaders].prgId = pixelshader_compile(shader, args);
479 return shader->gl_shaders[shader->num_gl_shaders++].prgId;