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 DWORD shader_version = WINED3D_SHADER_VERSION(This->baseShader.reg_maps.shader_version.major,
126 This->baseShader.reg_maps.shader_version.minor);
128 This->baseShader.limits.attributes = 0;
129 This->baseShader.limits.address = 0;
130 This->baseShader.limits.packed_output = 0;
132 switch (shader_version)
134 case WINED3D_SHADER_VERSION(1,0):
135 case WINED3D_SHADER_VERSION(1,1):
136 case WINED3D_SHADER_VERSION(1,2):
137 case WINED3D_SHADER_VERSION(1,3):
138 This->baseShader.limits.temporary = 2;
139 This->baseShader.limits.constant_float = 8;
140 This->baseShader.limits.constant_int = 0;
141 This->baseShader.limits.constant_bool = 0;
142 This->baseShader.limits.texcoord = 4;
143 This->baseShader.limits.sampler = 4;
144 This->baseShader.limits.packed_input = 0;
145 This->baseShader.limits.label = 0;
148 case WINED3D_SHADER_VERSION(1,4):
149 This->baseShader.limits.temporary = 6;
150 This->baseShader.limits.constant_float = 8;
151 This->baseShader.limits.constant_int = 0;
152 This->baseShader.limits.constant_bool = 0;
153 This->baseShader.limits.texcoord = 6;
154 This->baseShader.limits.sampler = 6;
155 This->baseShader.limits.packed_input = 0;
156 This->baseShader.limits.label = 0;
159 /* FIXME: temporaries must match D3DPSHADERCAPS2_0.NumTemps */
160 case WINED3D_SHADER_VERSION(2,0):
161 This->baseShader.limits.temporary = 32;
162 This->baseShader.limits.constant_float = 32;
163 This->baseShader.limits.constant_int = 16;
164 This->baseShader.limits.constant_bool = 16;
165 This->baseShader.limits.texcoord = 8;
166 This->baseShader.limits.sampler = 16;
167 This->baseShader.limits.packed_input = 0;
170 case WINED3D_SHADER_VERSION(2,1):
171 This->baseShader.limits.temporary = 32;
172 This->baseShader.limits.constant_float = 32;
173 This->baseShader.limits.constant_int = 16;
174 This->baseShader.limits.constant_bool = 16;
175 This->baseShader.limits.texcoord = 8;
176 This->baseShader.limits.sampler = 16;
177 This->baseShader.limits.packed_input = 0;
178 This->baseShader.limits.label = 16;
181 case WINED3D_SHADER_VERSION(3,0):
182 This->baseShader.limits.temporary = 32;
183 This->baseShader.limits.constant_float = 224;
184 This->baseShader.limits.constant_int = 16;
185 This->baseShader.limits.constant_bool = 16;
186 This->baseShader.limits.texcoord = 0;
187 This->baseShader.limits.sampler = 16;
188 This->baseShader.limits.packed_input = 12;
189 This->baseShader.limits.label = 16; /* FIXME: 2048 */
193 This->baseShader.limits.temporary = 32;
194 This->baseShader.limits.constant_float = 32;
195 This->baseShader.limits.constant_int = 16;
196 This->baseShader.limits.constant_bool = 16;
197 This->baseShader.limits.texcoord = 8;
198 This->baseShader.limits.sampler = 16;
199 This->baseShader.limits.packed_input = 0;
200 This->baseShader.limits.label = 0;
201 FIXME("Unrecognized pixel shader version %u.%u\n",
202 This->baseShader.reg_maps.shader_version.major,
203 This->baseShader.reg_maps.shader_version.minor);
207 static HRESULT WINAPI IWineD3DPixelShaderImpl_SetFunction(IWineD3DPixelShader *iface,
208 const DWORD *pFunction, const struct wined3d_shader_signature *output_signature)
210 IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
211 unsigned int i, highest_reg_used = 0, num_regs_used = 0;
212 shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
213 const struct wined3d_shader_frontend *fe;
216 TRACE("(%p) : pFunction %p\n", iface, pFunction);
218 fe = shader_select_frontend(*pFunction);
221 FIXME("Unable to find frontend for shader.\n");
222 return WINED3DERR_INVALIDCALL;
224 This->baseShader.frontend = fe;
225 This->baseShader.frontend_data = fe->shader_init(pFunction, output_signature);
226 if (!This->baseShader.frontend_data)
228 FIXME("Failed to initialize frontend.\n");
229 return WINED3DERR_INVALIDCALL;
232 /* First pass: trace shader */
233 if (TRACE_ON(d3d_shader)) shader_trace_init(fe, This->baseShader.frontend_data, pFunction);
235 /* Initialize immediate constant lists */
236 list_init(&This->baseShader.constantsF);
237 list_init(&This->baseShader.constantsB);
238 list_init(&This->baseShader.constantsI);
240 /* Second pass: figure out which registers are used, what the semantics are, etc.. */
241 hr = shader_get_registers_used((IWineD3DBaseShader *)This, fe, reg_maps, This->semantics_in, NULL, pFunction,
242 GL_LIMITS(pshader_constantsF));
243 if (FAILED(hr)) return hr;
245 pshader_set_limits(This);
247 for (i = 0; i < MAX_REG_INPUT; ++i)
249 if (This->input_reg_used[i])
252 highest_reg_used = i;
256 /* Don't do any register mapping magic if it is not needed, or if we can't
257 * achieve anything anyway */
258 if (highest_reg_used < (GL_LIMITS(glsl_varyings) / 4)
259 || num_regs_used > (GL_LIMITS(glsl_varyings) / 4))
261 if (num_regs_used > (GL_LIMITS(glsl_varyings) / 4))
263 /* This happens with relative addressing. The input mapper function
264 * warns about this if the higher registers are declared too, so
265 * don't write a FIXME here */
266 WARN("More varying registers used than supported\n");
269 for (i = 0; i < MAX_REG_INPUT; ++i)
271 This->input_reg_map[i] = i;
274 This->declared_in_count = highest_reg_used + 1;
278 This->declared_in_count = 0;
279 for (i = 0; i < MAX_REG_INPUT; ++i)
281 if (This->input_reg_used[i]) This->input_reg_map[i] = This->declared_in_count++;
282 else This->input_reg_map[i] = ~0U;
286 This->baseShader.load_local_constsF = FALSE;
288 TRACE("(%p) : Copying the function\n", This);
290 This->baseShader.function = HeapAlloc(GetProcessHeap(), 0, This->baseShader.functionLength);
291 if (!This->baseShader.function) return E_OUTOFMEMORY;
292 memcpy(This->baseShader.function, pFunction, This->baseShader.functionLength);
297 static void pixelshader_update_samplers(struct shader_reg_maps *reg_maps, IWineD3DBaseTexture * const *textures)
299 WINED3DSAMPLER_TEXTURE_TYPE *sampler_type = reg_maps->sampler_type;
302 if (reg_maps->shader_version.major != 1) return;
304 for (i = 0; i < max(MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS); ++i)
306 /* We don't sample from this sampler */
307 if (!sampler_type[i]) continue;
311 ERR("No texture bound to sampler %u, using 2D\n", i);
312 sampler_type[i] = WINED3DSTT_2D;
316 switch (IWineD3DBaseTexture_GetTextureDimensions(textures[i]))
318 case GL_TEXTURE_RECTANGLE_ARB:
320 /* We have to select between texture rectangles and 2D textures later because 2.0 and
321 * 3.0 shaders only have WINED3DSTT_2D as well */
322 sampler_type[i] = WINED3DSTT_2D;
326 sampler_type[i] = WINED3DSTT_VOLUME;
329 case GL_TEXTURE_CUBE_MAP_ARB:
330 sampler_type[i] = WINED3DSTT_CUBE;
334 FIXME("Unrecognized texture type %#x, using 2D\n",
335 IWineD3DBaseTexture_GetTextureDimensions(textures[i]));
336 sampler_type[i] = WINED3DSTT_2D;
341 static GLuint pixelshader_compile(IWineD3DPixelShaderImpl *This, const struct ps_compile_args *args)
343 CONST DWORD *function = This->baseShader.function;
345 SHADER_BUFFER buffer;
346 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
348 TRACE("(%p) : function %p\n", This, function);
350 pixelshader_update_samplers(&This->baseShader.reg_maps,
351 ((IWineD3DDeviceImpl *)This->baseShader.device)->stateBlock->textures);
353 /* Generate the HW shader */
354 TRACE("(%p) : Generating hardware program\n", This);
355 This->cur_args = args;
356 shader_buffer_init(&buffer);
357 retval = device->shader_backend->shader_generate_pshader((IWineD3DPixelShader *)This, &buffer, args);
358 shader_buffer_free(&buffer);
359 This->cur_args = NULL;
364 const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
366 /*** IUnknown methods ***/
367 IWineD3DPixelShaderImpl_QueryInterface,
368 IWineD3DPixelShaderImpl_AddRef,
369 IWineD3DPixelShaderImpl_Release,
370 /*** IWineD3DBase methods ***/
371 IWineD3DPixelShaderImpl_GetParent,
372 /*** IWineD3DBaseShader methods ***/
373 IWineD3DPixelShaderImpl_SetFunction,
374 /*** IWineD3DPixelShader methods ***/
375 IWineD3DPixelShaderImpl_GetDevice,
376 IWineD3DPixelShaderImpl_GetFunction
379 void find_ps_compile_args(IWineD3DPixelShaderImpl *shader, IWineD3DStateBlockImpl *stateblock, struct ps_compile_args *args) {
381 IWineD3DBaseTextureImpl *tex;
383 memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set */
384 args->srgb_correction = stateblock->renderState[WINED3DRS_SRGBWRITEENABLE] ? 1 : 0;
387 for(i = 0; i < MAX_FRAGMENT_SAMPLERS; i++) {
388 if (!shader->baseShader.reg_maps.sampler_type[i]) continue;
389 tex = (IWineD3DBaseTextureImpl *) stateblock->textures[i];
391 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
394 args->color_fixup[i] = tex->resource.format_desc->color_fixup;
396 /* Flag samplers that need NP2 texcoord fixup. */
397 if(!tex->baseTexture.pow2Matrix_identity) {
398 args->np2_fixup |= (1 << i);
401 if (shader->baseShader.reg_maps.shader_version.major >= 3)
403 if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed)
405 args->vp_mode = pretransformed;
407 else if (use_vs(stateblock))
409 args->vp_mode = vertexshader;
411 args->vp_mode = fixedfunction;
415 args->vp_mode = vertexshader;
416 if(stateblock->renderState[WINED3DRS_FOGENABLE]) {
417 switch(stateblock->renderState[WINED3DRS_FOGTABLEMODE]) {
418 case WINED3DFOG_NONE:
419 if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed
420 || use_vs(stateblock))
422 args->fog = FOG_LINEAR;
425 switch(stateblock->renderState[WINED3DRS_FOGVERTEXMODE]) {
426 case WINED3DFOG_NONE: /* Drop through */
427 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
428 case WINED3DFOG_EXP: args->fog = FOG_EXP; break;
429 case WINED3DFOG_EXP2: args->fog = FOG_EXP2; break;
433 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
434 case WINED3DFOG_EXP: args->fog = FOG_EXP; break;
435 case WINED3DFOG_EXP2: args->fog = FOG_EXP2; break;
443 GLuint find_gl_pshader(IWineD3DPixelShaderImpl *shader, const struct ps_compile_args *args)
447 struct ps_compiled_shader *new_array;
449 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
450 * so a linear search is more performant than a hashmap or a binary search
451 * (cache coherency etc)
453 for(i = 0; i < shader->num_gl_shaders; i++) {
454 if(memcmp(&shader->gl_shaders[i].args, args, sizeof(*args)) == 0) {
455 return shader->gl_shaders[i].prgId;
459 TRACE("No matching GL shader found, compiling a new shader\n");
460 if(shader->shader_array_size == shader->num_gl_shaders) {
461 if (shader->num_gl_shaders)
463 new_size = shader->shader_array_size + max(1, shader->shader_array_size / 2);
464 new_array = HeapReAlloc(GetProcessHeap(), 0, shader->gl_shaders,
465 new_size * sizeof(*shader->gl_shaders));
467 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader->gl_shaders));
472 ERR("Out of memory\n");
475 shader->gl_shaders = new_array;
476 shader->shader_array_size = new_size;
479 shader->gl_shaders[shader->num_gl_shaders].args = *args;
480 shader->gl_shaders[shader->num_gl_shaders].prgId = pixelshader_compile(shader, args);
481 return shader->gl_shaders[shader->num_gl_shaders++].prgId;