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, CONST DWORD *pFunction) {
209 IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
210 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *) This->baseShader.device;
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);
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 if (FAILED(hr)) return hr;
244 pshader_set_limits(This);
246 for (i = 0; i < MAX_REG_INPUT; ++i)
248 if (This->input_reg_used[i])
251 highest_reg_used = i;
255 /* Don't do any register mapping magic if it is not needed, or if we can't
256 * achieve anything anyway */
257 if (highest_reg_used < (GL_LIMITS(glsl_varyings) / 4)
258 || num_regs_used > (GL_LIMITS(glsl_varyings) / 4))
260 if (num_regs_used > (GL_LIMITS(glsl_varyings) / 4))
262 /* This happens with relative addressing. The input mapper function
263 * warns about this if the higher registers are declared too, so
264 * don't write a FIXME here */
265 WARN("More varying registers used than supported\n");
268 for (i = 0; i < MAX_REG_INPUT; ++i)
270 This->input_reg_map[i] = i;
273 This->declared_in_count = highest_reg_used + 1;
277 This->declared_in_count = 0;
278 for (i = 0; i < MAX_REG_INPUT; ++i)
280 if (This->input_reg_used[i]) This->input_reg_map[i] = This->declared_in_count++;
281 else This->input_reg_map[i] = ~0U;
285 This->baseShader.load_local_constsF = FALSE;
287 This->baseShader.shader_mode = deviceImpl->ps_selected_mode;
289 TRACE("(%p) : Copying the function\n", This);
291 This->baseShader.function = HeapAlloc(GetProcessHeap(), 0, This->baseShader.functionLength);
292 if (!This->baseShader.function) return E_OUTOFMEMORY;
293 memcpy(This->baseShader.function, pFunction, This->baseShader.functionLength);
298 static void pixelshader_update_samplers(struct shader_reg_maps *reg_maps, IWineD3DBaseTexture * const *textures)
300 WINED3DSAMPLER_TEXTURE_TYPE *sampler_type = reg_maps->sampler_type;
303 if (reg_maps->shader_version.major != 1) return;
305 for (i = 0; i < max(MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS); ++i)
307 /* We don't sample from this sampler */
308 if (!sampler_type[i]) continue;
312 ERR("No texture bound to sampler %u, using 2D\n", i);
313 sampler_type[i] = WINED3DSTT_2D;
317 switch (IWineD3DBaseTexture_GetTextureDimensions(textures[i]))
319 case GL_TEXTURE_RECTANGLE_ARB:
321 /* We have to select between texture rectangles and 2D textures later because 2.0 and
322 * 3.0 shaders only have WINED3DSTT_2D as well */
323 sampler_type[i] = WINED3DSTT_2D;
327 sampler_type[i] = WINED3DSTT_VOLUME;
330 case GL_TEXTURE_CUBE_MAP_ARB:
331 sampler_type[i] = WINED3DSTT_CUBE;
335 FIXME("Unrecognized texture type %#x, using 2D\n",
336 IWineD3DBaseTexture_GetTextureDimensions(textures[i]));
337 sampler_type[i] = WINED3DSTT_2D;
342 static GLuint pixelshader_compile(IWineD3DPixelShaderImpl *This, const struct ps_compile_args *args)
344 CONST DWORD *function = This->baseShader.function;
346 SHADER_BUFFER buffer;
347 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
349 TRACE("(%p) : function %p\n", This, function);
351 pixelshader_update_samplers(&This->baseShader.reg_maps,
352 ((IWineD3DDeviceImpl *)This->baseShader.device)->stateBlock->textures);
354 /* Generate the HW shader */
355 TRACE("(%p) : Generating hardware program\n", This);
356 This->cur_args = args;
357 shader_buffer_init(&buffer);
358 retval = device->shader_backend->shader_generate_pshader((IWineD3DPixelShader *)This, &buffer, args);
359 shader_buffer_free(&buffer);
360 This->cur_args = NULL;
365 const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
367 /*** IUnknown methods ***/
368 IWineD3DPixelShaderImpl_QueryInterface,
369 IWineD3DPixelShaderImpl_AddRef,
370 IWineD3DPixelShaderImpl_Release,
371 /*** IWineD3DBase methods ***/
372 IWineD3DPixelShaderImpl_GetParent,
373 /*** IWineD3DBaseShader methods ***/
374 IWineD3DPixelShaderImpl_SetFunction,
375 /*** IWineD3DPixelShader methods ***/
376 IWineD3DPixelShaderImpl_GetDevice,
377 IWineD3DPixelShaderImpl_GetFunction
380 void find_ps_compile_args(IWineD3DPixelShaderImpl *shader, IWineD3DStateBlockImpl *stateblock, struct ps_compile_args *args) {
382 IWineD3DBaseTextureImpl *tex;
384 memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set */
385 args->srgb_correction = stateblock->renderState[WINED3DRS_SRGBWRITEENABLE] ? 1 : 0;
388 for(i = 0; i < MAX_FRAGMENT_SAMPLERS; i++) {
389 if (!shader->baseShader.reg_maps.sampler_type[i]) continue;
390 tex = (IWineD3DBaseTextureImpl *) stateblock->textures[i];
392 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
395 args->color_fixup[i] = tex->resource.format_desc->color_fixup;
397 /* Flag samplers that need NP2 texcoord fixup. */
398 if(!tex->baseTexture.pow2Matrix_identity) {
399 args->np2_fixup |= (1 << i);
402 if (shader->baseShader.reg_maps.shader_version.major >= 3)
404 if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed)
406 args->vp_mode = pretransformed;
408 else if (use_vs(stateblock))
410 args->vp_mode = vertexshader;
412 args->vp_mode = fixedfunction;
416 args->vp_mode = vertexshader;
417 if(stateblock->renderState[WINED3DRS_FOGENABLE]) {
418 switch(stateblock->renderState[WINED3DRS_FOGTABLEMODE]) {
419 case WINED3DFOG_NONE:
420 if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed
421 || use_vs(stateblock))
423 args->fog = FOG_LINEAR;
426 switch(stateblock->renderState[WINED3DRS_FOGVERTEXMODE]) {
427 case WINED3DFOG_NONE: /* Drop through */
428 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
429 case WINED3DFOG_EXP: args->fog = FOG_EXP; break;
430 case WINED3DFOG_EXP2: args->fog = FOG_EXP2; break;
434 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
435 case WINED3DFOG_EXP: args->fog = FOG_EXP; break;
436 case WINED3DFOG_EXP2: args->fog = FOG_EXP2; break;
444 GLuint find_gl_pshader(IWineD3DPixelShaderImpl *shader, const struct ps_compile_args *args)
448 struct ps_compiled_shader *new_array;
450 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
451 * so a linear search is more performant than a hashmap or a binary search
452 * (cache coherency etc)
454 for(i = 0; i < shader->num_gl_shaders; i++) {
455 if(memcmp(&shader->gl_shaders[i].args, args, sizeof(*args)) == 0) {
456 return shader->gl_shaders[i].prgId;
460 TRACE("No matching GL shader found, compiling a new shader\n");
461 if(shader->shader_array_size == shader->num_gl_shaders) {
462 if (shader->num_gl_shaders)
464 new_size = shader->shader_array_size + max(1, shader->shader_array_size / 2);
465 new_array = HeapReAlloc(GetProcessHeap(), 0, shader->gl_shaders,
466 new_size * sizeof(*shader->gl_shaders));
468 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader->gl_shaders));
473 ERR("Out of memory\n");
476 shader->gl_shaders = new_array;
477 shader->shader_array_size = new_size;
480 shader->gl_shaders[shader->num_gl_shaders].args = *args;
481 shader->gl_shaders[shader->num_gl_shaders].prgId = pixelshader_compile(shader, args);
482 return shader->gl_shaders[shader->num_gl_shaders++].prgId;