wined3d: Pack hardcoded local constants in ARB.
[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     This->baseShader.limits.attributes = 0;
126     This->baseShader.limits.address = 0;
127     This->baseShader.limits.packed_output = 0;
128
129     switch (This->baseShader.reg_maps.shader_version)
130     {
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;
143             break;
144
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;
154             break;
155
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;
165             break;
166
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;
176             break;
177
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 */
187             break;
188
189         default:
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);
200     }
201 }
202
203 static HRESULT WINAPI IWineD3DPixelShaderImpl_SetFunction(IWineD3DPixelShader *iface, CONST DWORD *pFunction) {
204
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;
210     HRESULT hr;
211
212     TRACE("(%p) : pFunction %p\n", iface, pFunction);
213
214     fe = shader_select_frontend(*pFunction);
215     if (!fe)
216     {
217         FIXME("Unable to find frontend for shader.\n");
218         return WINED3DERR_INVALIDCALL;
219     }
220     This->baseShader.frontend = fe;
221     This->baseShader.frontend_data = fe->shader_init(pFunction);
222     if (!This->baseShader.frontend_data)
223     {
224         FIXME("Failed to initialize frontend.\n");
225         return WINED3DERR_INVALIDCALL;
226     }
227
228     /* First pass: trace shader */
229     if (TRACE_ON(d3d_shader)) shader_trace_init(fe, This->baseShader.frontend_data, pFunction);
230
231     /* Initialize immediate constant lists */
232     list_init(&This->baseShader.constantsF);
233     list_init(&This->baseShader.constantsB);
234     list_init(&This->baseShader.constantsI);
235
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;
239
240     pshader_set_limits(This);
241
242     for (i = 0; i < MAX_REG_INPUT; ++i)
243     {
244         if (This->input_reg_used[i])
245         {
246             ++num_regs_used;
247             highest_reg_used = i;
248         }
249     }
250
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))
255     {
256         if (num_regs_used > (GL_LIMITS(glsl_varyings) / 4))
257         {
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");
262         }
263
264         for (i = 0; i < MAX_REG_INPUT; ++i)
265         {
266             This->input_reg_map[i] = i;
267         }
268
269         This->declared_in_count = highest_reg_used + 1;
270     }
271     else
272     {
273         This->declared_in_count = 0;
274         for (i = 0; i < MAX_REG_INPUT; ++i)
275         {
276             if (This->input_reg_used[i]) This->input_reg_map[i] = This->declared_in_count++;
277             else This->input_reg_map[i] = ~0U;
278         }
279     }
280
281     This->baseShader.load_local_constsF = FALSE;
282
283     This->baseShader.shader_mode = deviceImpl->ps_selected_mode;
284
285     TRACE("(%p) : Copying the function\n", This);
286
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);
290
291     return WINED3D_OK;
292 }
293
294 static void pixelshader_update_samplers(struct shader_reg_maps *reg_maps, IWineD3DBaseTexture * const *textures)
295 {
296     DWORD shader_version = reg_maps->shader_version;
297     WINED3DSAMPLER_TEXTURE_TYPE *sampler_type = reg_maps->sampler_type;
298     unsigned int i;
299
300     if (WINED3DSHADER_VERSION_MAJOR(shader_version) != 1) return;
301
302     for (i = 0; i < max(MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS); ++i)
303     {
304         /* We don't sample from this sampler */
305         if (!sampler_type[i]) continue;
306
307         if (!textures[i])
308         {
309             ERR("No texture bound to sampler %u, using 2D\n", i);
310             sampler_type[i] = WINED3DSTT_2D;
311             continue;
312         }
313
314         switch (IWineD3DBaseTexture_GetTextureDimensions(textures[i]))
315         {
316             case GL_TEXTURE_RECTANGLE_ARB:
317             case GL_TEXTURE_2D:
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;
321                 break;
322
323             case GL_TEXTURE_3D:
324                 sampler_type[i] = WINED3DSTT_VOLUME;
325                 break;
326
327             case GL_TEXTURE_CUBE_MAP_ARB:
328                 sampler_type[i] = WINED3DSTT_CUBE;
329                 break;
330
331             default:
332                 FIXME("Unrecognized texture type %#x, using 2D\n",
333                         IWineD3DBaseTexture_GetTextureDimensions(textures[i]));
334                 sampler_type[i] = WINED3DSTT_2D;
335         }
336     }
337 }
338
339 static GLuint pixelshader_compile(IWineD3DPixelShaderImpl *This, const struct ps_compile_args *args)
340 {
341     CONST DWORD *function = This->baseShader.function;
342     GLuint retval;
343     SHADER_BUFFER buffer;
344     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
345
346     TRACE("(%p) : function %p\n", This, function);
347
348     pixelshader_update_samplers(&This->baseShader.reg_maps,
349             ((IWineD3DDeviceImpl *)This->baseShader.device)->stateBlock->textures);
350
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;
358
359     return retval;
360 }
361
362 const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
363 {
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
375 };
376
377 void find_ps_compile_args(IWineD3DPixelShaderImpl *shader, IWineD3DStateBlockImpl *stateblock, struct ps_compile_args *args) {
378     UINT i;
379     IWineD3DBaseTextureImpl *tex;
380
381     memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set */
382     args->srgb_correction = stateblock->renderState[WINED3DRS_SRGBWRITEENABLE] ? 1 : 0;
383     args->np2_fixup = 0;
384
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];
388         if(!tex) {
389             args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
390             continue;
391         }
392         args->color_fixup[i] = tex->resource.format_desc->color_fixup;
393
394         /* Flag samplers that need NP2 texcoord fixup. */
395         if(!tex->baseTexture.pow2Matrix_identity) {
396             args->np2_fixup |= (1 << i);
397         }
398     }
399     if (shader->baseShader.reg_maps.shader_version >= WINED3DPS_VERSION(3,0))
400     {
401         if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed)
402         {
403             args->vp_mode = pretransformed;
404         }
405         else if (use_vs(stateblock))
406         {
407             args->vp_mode = vertexshader;
408         } else {
409             args->vp_mode = fixedfunction;
410         }
411         args->fog = FOG_OFF;
412     } else {
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))
419                     {
420                         args->fog = FOG_LINEAR;
421                         break;
422                     }
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;
428                     }
429                     break;
430
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;
434             }
435         } else {
436             args->fog = FOG_OFF;
437         }
438     }
439 }
440
441 GLuint find_gl_pshader(IWineD3DPixelShaderImpl *shader, const struct ps_compile_args *args)
442 {
443     UINT i;
444     DWORD new_size;
445     struct ps_compiled_shader *new_array;
446
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)
450      */
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;
454         }
455     }
456
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)
460         {
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));
464         } else {
465             new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader->gl_shaders));
466             new_size = 1;
467         }
468
469         if(!new_array) {
470             ERR("Out of memory\n");
471             return 0;
472         }
473         shader->gl_shaders = new_array;
474         shader->shader_array_size = new_size;
475     }
476
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;
480 }