wined3d: Add support for immediate constants to shader_dump_register().
[wine] / dlls / wined3d / pixelshader.c
1 /*
2  * shaders implementation
3  *
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
10  *
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.
15  *
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.
20  *
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
24  */
25
26 #include "config.h"
27
28 #include <math.h>
29 #include <stdio.h>
30
31 #include "wined3d_private.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
34
35 #define GLINFO_LOCATION ((IWineD3DDeviceImpl *) This->baseShader.device)->adapter->gl_info
36
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);
39
40     if (IsEqualGUID(riid, &IID_IWineD3DPixelShader)
41             || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
42             || IsEqualGUID(riid, &IID_IWineD3DBase)
43             || IsEqualGUID(riid, &IID_IUnknown))
44     {
45         IUnknown_AddRef(iface);
46         *ppobj = iface;
47         return S_OK;
48     }
49
50     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
51
52     *ppobj = NULL;
53     return E_NOINTERFACE;
54 }
55
56 static ULONG  WINAPI IWineD3DPixelShaderImpl_AddRef(IWineD3DPixelShader *iface) {
57     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
58     ULONG refcount = InterlockedIncrement(&This->baseShader.ref);
59
60     TRACE("%p increasing refcount to %u\n", This, refcount);
61
62     return refcount;
63 }
64
65 static ULONG  WINAPI IWineD3DPixelShaderImpl_Release(IWineD3DPixelShader *iface) {
66     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
67     ULONG refcount = InterlockedDecrement(&This->baseShader.ref);
68
69     TRACE("%p decreasing refcount to %u\n", This, refcount);
70
71     if (!refcount)
72     {
73         shader_cleanup((IWineD3DBaseShader *)iface);
74         HeapFree(GetProcessHeap(), 0, This);
75     }
76
77     return refcount;
78 }
79
80 /* *******************************************
81    IWineD3DPixelShader IWineD3DPixelShader parts follow
82    ******************************************* */
83
84 static HRESULT  WINAPI IWineD3DPixelShaderImpl_GetParent(IWineD3DPixelShader *iface, IUnknown** parent){
85     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
86
87     *parent = This->parent;
88     IUnknown_AddRef(*parent);
89     TRACE("(%p) : returning %p\n", This, *parent);
90     return WINED3D_OK;
91 }
92
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);
98     return WINED3D_OK;
99 }
100
101
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);
105
106   if (NULL == pData) {
107     *pSizeOfData = This->baseShader.functionLength;
108     return WINED3D_OK;
109   }
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;
115   }
116
117   TRACE("(%p) : GetFunction copying to %p\n", This, pData);
118   memcpy(pData, This->baseShader.function, This->baseShader.functionLength);
119
120   return WINED3D_OK;
121 }
122
123 static void pshader_set_limits(IWineD3DPixelShaderImpl *This)
124 {
125     DWORD shader_version = WINED3D_SHADER_VERSION(This->baseShader.reg_maps.shader_version.major,
126             This->baseShader.reg_maps.shader_version.minor);
127
128     This->baseShader.limits.attributes = 0;
129     This->baseShader.limits.address = 0;
130     This->baseShader.limits.packed_output = 0;
131
132     switch (shader_version)
133     {
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;
146             break;
147
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;
157             break;
158
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;
168             break;
169
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;
179             break;
180
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 */
190             break;
191
192         default:
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);
204     }
205 }
206
207 static HRESULT WINAPI IWineD3DPixelShaderImpl_SetFunction(IWineD3DPixelShader *iface, CONST DWORD *pFunction) {
208
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;
214     HRESULT hr;
215
216     TRACE("(%p) : pFunction %p\n", iface, pFunction);
217
218     fe = shader_select_frontend(*pFunction);
219     if (!fe)
220     {
221         FIXME("Unable to find frontend for shader.\n");
222         return WINED3DERR_INVALIDCALL;
223     }
224     This->baseShader.frontend = fe;
225     This->baseShader.frontend_data = fe->shader_init(pFunction);
226     if (!This->baseShader.frontend_data)
227     {
228         FIXME("Failed to initialize frontend.\n");
229         return WINED3DERR_INVALIDCALL;
230     }
231
232     /* First pass: trace shader */
233     if (TRACE_ON(d3d_shader)) shader_trace_init(fe, This->baseShader.frontend_data, pFunction);
234
235     /* Initialize immediate constant lists */
236     list_init(&This->baseShader.constantsF);
237     list_init(&This->baseShader.constantsB);
238     list_init(&This->baseShader.constantsI);
239
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;
243
244     pshader_set_limits(This);
245
246     for (i = 0; i < MAX_REG_INPUT; ++i)
247     {
248         if (This->input_reg_used[i])
249         {
250             ++num_regs_used;
251             highest_reg_used = i;
252         }
253     }
254
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))
259     {
260         if (num_regs_used > (GL_LIMITS(glsl_varyings) / 4))
261         {
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");
266         }
267
268         for (i = 0; i < MAX_REG_INPUT; ++i)
269         {
270             This->input_reg_map[i] = i;
271         }
272
273         This->declared_in_count = highest_reg_used + 1;
274     }
275     else
276     {
277         This->declared_in_count = 0;
278         for (i = 0; i < MAX_REG_INPUT; ++i)
279         {
280             if (This->input_reg_used[i]) This->input_reg_map[i] = This->declared_in_count++;
281             else This->input_reg_map[i] = ~0U;
282         }
283     }
284
285     This->baseShader.load_local_constsF = FALSE;
286
287     This->baseShader.shader_mode = deviceImpl->ps_selected_mode;
288
289     TRACE("(%p) : Copying the function\n", This);
290
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);
294
295     return WINED3D_OK;
296 }
297
298 static void pixelshader_update_samplers(struct shader_reg_maps *reg_maps, IWineD3DBaseTexture * const *textures)
299 {
300     WINED3DSAMPLER_TEXTURE_TYPE *sampler_type = reg_maps->sampler_type;
301     unsigned int i;
302
303     if (reg_maps->shader_version.major != 1) return;
304
305     for (i = 0; i < max(MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS); ++i)
306     {
307         /* We don't sample from this sampler */
308         if (!sampler_type[i]) continue;
309
310         if (!textures[i])
311         {
312             ERR("No texture bound to sampler %u, using 2D\n", i);
313             sampler_type[i] = WINED3DSTT_2D;
314             continue;
315         }
316
317         switch (IWineD3DBaseTexture_GetTextureDimensions(textures[i]))
318         {
319             case GL_TEXTURE_RECTANGLE_ARB:
320             case GL_TEXTURE_2D:
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;
324                 break;
325
326             case GL_TEXTURE_3D:
327                 sampler_type[i] = WINED3DSTT_VOLUME;
328                 break;
329
330             case GL_TEXTURE_CUBE_MAP_ARB:
331                 sampler_type[i] = WINED3DSTT_CUBE;
332                 break;
333
334             default:
335                 FIXME("Unrecognized texture type %#x, using 2D\n",
336                         IWineD3DBaseTexture_GetTextureDimensions(textures[i]));
337                 sampler_type[i] = WINED3DSTT_2D;
338         }
339     }
340 }
341
342 static GLuint pixelshader_compile(IWineD3DPixelShaderImpl *This, const struct ps_compile_args *args)
343 {
344     CONST DWORD *function = This->baseShader.function;
345     GLuint retval;
346     SHADER_BUFFER buffer;
347     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
348
349     TRACE("(%p) : function %p\n", This, function);
350
351     pixelshader_update_samplers(&This->baseShader.reg_maps,
352             ((IWineD3DDeviceImpl *)This->baseShader.device)->stateBlock->textures);
353
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;
361
362     return retval;
363 }
364
365 const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
366 {
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
378 };
379
380 void find_ps_compile_args(IWineD3DPixelShaderImpl *shader, IWineD3DStateBlockImpl *stateblock, struct ps_compile_args *args) {
381     UINT i;
382     IWineD3DBaseTextureImpl *tex;
383
384     memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set */
385     args->srgb_correction = stateblock->renderState[WINED3DRS_SRGBWRITEENABLE] ? 1 : 0;
386     args->np2_fixup = 0;
387
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];
391         if(!tex) {
392             args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
393             continue;
394         }
395         args->color_fixup[i] = tex->resource.format_desc->color_fixup;
396
397         /* Flag samplers that need NP2 texcoord fixup. */
398         if(!tex->baseTexture.pow2Matrix_identity) {
399             args->np2_fixup |= (1 << i);
400         }
401     }
402     if (shader->baseShader.reg_maps.shader_version.major >= 3)
403     {
404         if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed)
405         {
406             args->vp_mode = pretransformed;
407         }
408         else if (use_vs(stateblock))
409         {
410             args->vp_mode = vertexshader;
411         } else {
412             args->vp_mode = fixedfunction;
413         }
414         args->fog = FOG_OFF;
415     } else {
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))
422                     {
423                         args->fog = FOG_LINEAR;
424                         break;
425                     }
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;
431                     }
432                     break;
433
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;
437             }
438         } else {
439             args->fog = FOG_OFF;
440         }
441     }
442 }
443
444 GLuint find_gl_pshader(IWineD3DPixelShaderImpl *shader, const struct ps_compile_args *args)
445 {
446     UINT i;
447     DWORD new_size;
448     struct ps_compiled_shader *new_array;
449
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)
453      */
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;
457         }
458     }
459
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)
463         {
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));
467         } else {
468             new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader->gl_shaders));
469             new_size = 1;
470         }
471
472         if(!new_array) {
473             ERR("Out of memory\n");
474             return 0;
475         }
476         shader->gl_shaders = new_array;
477         shader->shader_array_size = new_size;
478     }
479
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;
483 }