wined3d: Add UnregisterClass() on DLL_PROCESS_DETACH.
[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,
208         const DWORD *pFunction, const struct wined3d_shader_signature *output_signature)
209 {
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;
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, output_signature);
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                                     GL_LIMITS(pshader_constantsF));
243     if (FAILED(hr)) return hr;
244
245     pshader_set_limits(This);
246
247     for (i = 0; i < MAX_REG_INPUT; ++i)
248     {
249         if (This->input_reg_used[i])
250         {
251             ++num_regs_used;
252             highest_reg_used = i;
253         }
254     }
255
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))
260     {
261         if (num_regs_used > (GL_LIMITS(glsl_varyings) / 4))
262         {
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");
267         }
268
269         for (i = 0; i < MAX_REG_INPUT; ++i)
270         {
271             This->input_reg_map[i] = i;
272         }
273
274         This->declared_in_count = highest_reg_used + 1;
275     }
276     else
277     {
278         This->declared_in_count = 0;
279         for (i = 0; i < MAX_REG_INPUT; ++i)
280         {
281             if (This->input_reg_used[i]) This->input_reg_map[i] = This->declared_in_count++;
282             else This->input_reg_map[i] = ~0U;
283         }
284     }
285
286     This->baseShader.load_local_constsF = FALSE;
287
288     TRACE("(%p) : Copying the function\n", This);
289
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);
293
294     return WINED3D_OK;
295 }
296
297 static void pixelshader_update_samplers(struct shader_reg_maps *reg_maps, IWineD3DBaseTexture * const *textures)
298 {
299     WINED3DSAMPLER_TEXTURE_TYPE *sampler_type = reg_maps->sampler_type;
300     unsigned int i;
301
302     if (reg_maps->shader_version.major != 1) return;
303
304     for (i = 0; i < max(MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS); ++i)
305     {
306         /* We don't sample from this sampler */
307         if (!sampler_type[i]) continue;
308
309         if (!textures[i])
310         {
311             ERR("No texture bound to sampler %u, using 2D\n", i);
312             sampler_type[i] = WINED3DSTT_2D;
313             continue;
314         }
315
316         switch (IWineD3DBaseTexture_GetTextureDimensions(textures[i]))
317         {
318             case GL_TEXTURE_RECTANGLE_ARB:
319             case GL_TEXTURE_2D:
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;
323                 break;
324
325             case GL_TEXTURE_3D:
326                 sampler_type[i] = WINED3DSTT_VOLUME;
327                 break;
328
329             case GL_TEXTURE_CUBE_MAP_ARB:
330                 sampler_type[i] = WINED3DSTT_CUBE;
331                 break;
332
333             default:
334                 FIXME("Unrecognized texture type %#x, using 2D\n",
335                         IWineD3DBaseTexture_GetTextureDimensions(textures[i]));
336                 sampler_type[i] = WINED3DSTT_2D;
337         }
338     }
339 }
340
341 static GLuint pixelshader_compile(IWineD3DPixelShaderImpl *This, const struct ps_compile_args *args)
342 {
343     CONST DWORD *function = This->baseShader.function;
344     GLuint retval;
345     SHADER_BUFFER buffer;
346     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
347
348     TRACE("(%p) : function %p\n", This, function);
349
350     pixelshader_update_samplers(&This->baseShader.reg_maps,
351             ((IWineD3DDeviceImpl *)This->baseShader.device)->stateBlock->textures);
352
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;
360
361     return retval;
362 }
363
364 const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
365 {
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
377 };
378
379 void find_ps_compile_args(IWineD3DPixelShaderImpl *shader, IWineD3DStateBlockImpl *stateblock, struct ps_compile_args *args) {
380     UINT i;
381     IWineD3DBaseTextureImpl *tex;
382
383     memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set */
384     args->srgb_correction = stateblock->renderState[WINED3DRS_SRGBWRITEENABLE] ? 1 : 0;
385     args->np2_fixup = 0;
386
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];
390         if(!tex) {
391             args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
392             continue;
393         }
394         args->color_fixup[i] = tex->resource.format_desc->color_fixup;
395
396         /* Flag samplers that need NP2 texcoord fixup. */
397         if(!tex->baseTexture.pow2Matrix_identity) {
398             args->np2_fixup |= (1 << i);
399         }
400     }
401     if (shader->baseShader.reg_maps.shader_version.major >= 3)
402     {
403         if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed)
404         {
405             args->vp_mode = pretransformed;
406         }
407         else if (use_vs(stateblock))
408         {
409             args->vp_mode = vertexshader;
410         } else {
411             args->vp_mode = fixedfunction;
412         }
413         args->fog = FOG_OFF;
414     } else {
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))
421                     {
422                         args->fog = FOG_LINEAR;
423                         break;
424                     }
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;
430                     }
431                     break;
432
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;
436             }
437         } else {
438             args->fog = FOG_OFF;
439         }
440     }
441 }
442
443 GLuint find_gl_pshader(IWineD3DPixelShaderImpl *shader, const struct ps_compile_args *args)
444 {
445     UINT i;
446     DWORD new_size;
447     struct ps_compiled_shader *new_array;
448
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)
452      */
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;
456         }
457     }
458
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)
462         {
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));
466         } else {
467             new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader->gl_shaders));
468             new_size = 1;
469         }
470
471         if(!new_array) {
472             ERR("Out of memory\n");
473             return 0;
474         }
475         shader->gl_shaders = new_array;
476         shader->shader_array_size = new_size;
477     }
478
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;
482 }