wined3d: Use stateblock_init_contained_states() for WINED3DSBT_VERTEXSTATE stateblocks.
[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  * Copyright 2009 Henri Verbeet for CodeWeavers
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26
27 #include "config.h"
28
29 #include <math.h>
30 #include <stdio.h>
31
32 #include "wined3d_private.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
35
36 #define GLINFO_LOCATION ((IWineD3DDeviceImpl *) This->baseShader.device)->adapter->gl_info
37
38 static HRESULT  WINAPI IWineD3DPixelShaderImpl_QueryInterface(IWineD3DPixelShader *iface, REFIID riid, LPVOID *ppobj) {
39     TRACE("iface %p, riid %s, ppobj %p\n", iface, debugstr_guid(riid), ppobj);
40
41     if (IsEqualGUID(riid, &IID_IWineD3DPixelShader)
42             || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
43             || IsEqualGUID(riid, &IID_IWineD3DBase)
44             || IsEqualGUID(riid, &IID_IUnknown))
45     {
46         IUnknown_AddRef(iface);
47         *ppobj = iface;
48         return S_OK;
49     }
50
51     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
52
53     *ppobj = NULL;
54     return E_NOINTERFACE;
55 }
56
57 static ULONG  WINAPI IWineD3DPixelShaderImpl_AddRef(IWineD3DPixelShader *iface) {
58     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
59     ULONG refcount = InterlockedIncrement(&This->baseShader.ref);
60
61     TRACE("%p increasing refcount to %u\n", This, refcount);
62
63     return refcount;
64 }
65
66 static ULONG  WINAPI IWineD3DPixelShaderImpl_Release(IWineD3DPixelShader *iface) {
67     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
68     ULONG refcount = InterlockedDecrement(&This->baseShader.ref);
69
70     TRACE("%p decreasing refcount to %u\n", This, refcount);
71
72     if (!refcount)
73     {
74         shader_cleanup((IWineD3DBaseShader *)iface);
75         This->baseShader.parent_ops->wined3d_object_destroyed(This->baseShader.parent);
76         HeapFree(GetProcessHeap(), 0, This);
77     }
78
79     return refcount;
80 }
81
82 /* *******************************************
83    IWineD3DPixelShader IWineD3DPixelShader parts follow
84    ******************************************* */
85
86 static HRESULT  WINAPI IWineD3DPixelShaderImpl_GetParent(IWineD3DPixelShader *iface, IUnknown** parent){
87     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
88
89     *parent = This->baseShader.parent;
90     IUnknown_AddRef(*parent);
91     TRACE("(%p) : returning %p\n", This, *parent);
92     return WINED3D_OK;
93 }
94
95 static HRESULT  WINAPI IWineD3DPixelShaderImpl_GetDevice(IWineD3DPixelShader* iface, IWineD3DDevice **pDevice){
96     IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
97     IWineD3DDevice_AddRef(This->baseShader.device);
98     *pDevice = This->baseShader.device;
99     TRACE("(%p) returning %p\n", This, *pDevice);
100     return WINED3D_OK;
101 }
102
103
104 static HRESULT  WINAPI IWineD3DPixelShaderImpl_GetFunction(IWineD3DPixelShader* impl, VOID* pData, UINT* pSizeOfData) {
105   IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)impl;
106   TRACE("(%p) : pData(%p), pSizeOfData(%p)\n", This, pData, pSizeOfData);
107
108   if (NULL == pData) {
109     *pSizeOfData = This->baseShader.functionLength;
110     return WINED3D_OK;
111   }
112   if (*pSizeOfData < This->baseShader.functionLength) {
113     /* MSDN claims (for d3d8 at least) that if *pSizeOfData is smaller
114      * than the required size we should write the required size and
115      * return D3DERR_MOREDATA. That's not actually true. */
116     return WINED3DERR_INVALIDCALL;
117   }
118
119   TRACE("(%p) : GetFunction copying to %p\n", This, pData);
120   memcpy(pData, This->baseShader.function, This->baseShader.functionLength);
121
122   return WINED3D_OK;
123 }
124
125 static void pshader_set_limits(IWineD3DPixelShaderImpl *This)
126 {
127     DWORD shader_version = WINED3D_SHADER_VERSION(This->baseShader.reg_maps.shader_version.major,
128             This->baseShader.reg_maps.shader_version.minor);
129
130     This->baseShader.limits.attributes = 0;
131     This->baseShader.limits.address = 0;
132     This->baseShader.limits.packed_output = 0;
133
134     switch (shader_version)
135     {
136         case WINED3D_SHADER_VERSION(1,0):
137         case WINED3D_SHADER_VERSION(1,1):
138         case WINED3D_SHADER_VERSION(1,2):
139         case WINED3D_SHADER_VERSION(1,3):
140             This->baseShader.limits.temporary = 2;
141             This->baseShader.limits.constant_float = 8;
142             This->baseShader.limits.constant_int = 0;
143             This->baseShader.limits.constant_bool = 0;
144             This->baseShader.limits.texcoord = 4;
145             This->baseShader.limits.sampler = 4;
146             This->baseShader.limits.packed_input = 0;
147             This->baseShader.limits.label = 0;
148             break;
149
150         case WINED3D_SHADER_VERSION(1,4):
151             This->baseShader.limits.temporary = 6;
152             This->baseShader.limits.constant_float = 8;
153             This->baseShader.limits.constant_int = 0;
154             This->baseShader.limits.constant_bool = 0;
155             This->baseShader.limits.texcoord = 6;
156             This->baseShader.limits.sampler = 6;
157             This->baseShader.limits.packed_input = 0;
158             This->baseShader.limits.label = 0;
159             break;
160
161         /* FIXME: temporaries must match D3DPSHADERCAPS2_0.NumTemps */
162         case WINED3D_SHADER_VERSION(2,0):
163             This->baseShader.limits.temporary = 32;
164             This->baseShader.limits.constant_float = 32;
165             This->baseShader.limits.constant_int = 16;
166             This->baseShader.limits.constant_bool = 16;
167             This->baseShader.limits.texcoord = 8;
168             This->baseShader.limits.sampler = 16;
169             This->baseShader.limits.packed_input = 0;
170             break;
171
172         case WINED3D_SHADER_VERSION(2,1):
173             This->baseShader.limits.temporary = 32;
174             This->baseShader.limits.constant_float = 32;
175             This->baseShader.limits.constant_int = 16;
176             This->baseShader.limits.constant_bool = 16;
177             This->baseShader.limits.texcoord = 8;
178             This->baseShader.limits.sampler = 16;
179             This->baseShader.limits.packed_input = 0;
180             This->baseShader.limits.label = 16;
181             break;
182
183         case WINED3D_SHADER_VERSION(4,0):
184             FIXME("Using 3.0 limits for 4.0 shader\n");
185             /* Fall through */
186
187         case WINED3D_SHADER_VERSION(3,0):
188             This->baseShader.limits.temporary = 32;
189             This->baseShader.limits.constant_float = 224;
190             This->baseShader.limits.constant_int = 16;
191             This->baseShader.limits.constant_bool = 16;
192             This->baseShader.limits.texcoord = 0;
193             This->baseShader.limits.sampler = 16;
194             This->baseShader.limits.packed_input = 12;
195             This->baseShader.limits.label = 16; /* FIXME: 2048 */
196             break;
197
198         default:
199             This->baseShader.limits.temporary = 32;
200             This->baseShader.limits.constant_float = 32;
201             This->baseShader.limits.constant_int = 16;
202             This->baseShader.limits.constant_bool = 16;
203             This->baseShader.limits.texcoord = 8;
204             This->baseShader.limits.sampler = 16;
205             This->baseShader.limits.packed_input = 0;
206             This->baseShader.limits.label = 0;
207             FIXME("Unrecognized pixel shader version %u.%u\n",
208                     This->baseShader.reg_maps.shader_version.major,
209                     This->baseShader.reg_maps.shader_version.minor);
210     }
211 }
212
213 static HRESULT pixelshader_set_function(IWineD3DPixelShaderImpl *shader,
214         const DWORD *byte_code, const struct wined3d_shader_signature *output_signature)
215 {
216     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)shader->baseShader.device;
217     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
218     unsigned int i, highest_reg_used = 0, num_regs_used = 0;
219     shader_reg_maps *reg_maps = &shader->baseShader.reg_maps;
220     const struct wined3d_shader_frontend *fe;
221     HRESULT hr;
222
223     TRACE("shader %p, byte_code %p, output_signature %p.\n", shader, byte_code, output_signature);
224
225     fe = shader_select_frontend(*byte_code);
226     if (!fe)
227     {
228         FIXME("Unable to find frontend for shader.\n");
229         return WINED3DERR_INVALIDCALL;
230     }
231     shader->baseShader.frontend = fe;
232     shader->baseShader.frontend_data = fe->shader_init(byte_code, output_signature);
233     if (!shader->baseShader.frontend_data)
234     {
235         FIXME("Failed to initialize frontend.\n");
236         return WINED3DERR_INVALIDCALL;
237     }
238
239     /* First pass: trace shader */
240     if (TRACE_ON(d3d_shader)) shader_trace_init(fe, shader->baseShader.frontend_data, byte_code);
241
242     /* Initialize immediate constant lists */
243     list_init(&shader->baseShader.constantsF);
244     list_init(&shader->baseShader.constantsB);
245     list_init(&shader->baseShader.constantsI);
246
247     /* Second pass: figure out which registers are used, what the semantics are, etc.. */
248     hr = shader_get_registers_used((IWineD3DBaseShader *)shader, fe,
249             reg_maps, NULL, shader->input_signature, NULL,
250             byte_code, gl_info->max_pshader_constantsF);
251     if (FAILED(hr)) return hr;
252
253     pshader_set_limits(shader);
254
255     for (i = 0; i < MAX_REG_INPUT; ++i)
256     {
257         if (shader->input_reg_used[i])
258         {
259             ++num_regs_used;
260             highest_reg_used = i;
261         }
262     }
263
264     /* Don't do any register mapping magic if it is not needed, or if we can't
265      * achieve anything anyway */
266     if (highest_reg_used < (gl_info->max_glsl_varyings / 4)
267             || num_regs_used > (gl_info->max_glsl_varyings / 4))
268     {
269         if (num_regs_used > (gl_info->max_glsl_varyings / 4))
270         {
271             /* This happens with relative addressing. The input mapper function
272              * warns about this if the higher registers are declared too, so
273              * don't write a FIXME here */
274             WARN("More varying registers used than supported\n");
275         }
276
277         for (i = 0; i < MAX_REG_INPUT; ++i)
278         {
279             shader->input_reg_map[i] = i;
280         }
281
282         shader->declared_in_count = highest_reg_used + 1;
283     }
284     else
285     {
286         shader->declared_in_count = 0;
287         for (i = 0; i < MAX_REG_INPUT; ++i)
288         {
289             if (shader->input_reg_used[i]) shader->input_reg_map[i] = shader->declared_in_count++;
290             else shader->input_reg_map[i] = ~0U;
291         }
292     }
293
294     shader->baseShader.load_local_constsF = FALSE;
295
296     TRACE("(%p) : Copying byte code.\n", shader);
297
298     shader->baseShader.function = HeapAlloc(GetProcessHeap(), 0, shader->baseShader.functionLength);
299     if (!shader->baseShader.function) return E_OUTOFMEMORY;
300     memcpy(shader->baseShader.function, byte_code, shader->baseShader.functionLength);
301
302     return WINED3D_OK;
303 }
304
305 void pixelshader_update_samplers(struct shader_reg_maps *reg_maps, IWineD3DBaseTexture * const *textures)
306 {
307     WINED3DSAMPLER_TEXTURE_TYPE *sampler_type = reg_maps->sampler_type;
308     unsigned int i;
309
310     if (reg_maps->shader_version.major != 1) return;
311
312     for (i = 0; i < max(MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS); ++i)
313     {
314         /* We don't sample from this sampler */
315         if (!sampler_type[i]) continue;
316
317         if (!textures[i])
318         {
319             ERR("No texture bound to sampler %u, using 2D\n", i);
320             sampler_type[i] = WINED3DSTT_2D;
321             continue;
322         }
323
324         switch (IWineD3DBaseTexture_GetTextureDimensions(textures[i]))
325         {
326             case GL_TEXTURE_RECTANGLE_ARB:
327             case GL_TEXTURE_2D:
328                 /* We have to select between texture rectangles and 2D textures later because 2.0 and
329                  * 3.0 shaders only have WINED3DSTT_2D as well */
330                 sampler_type[i] = WINED3DSTT_2D;
331                 break;
332
333             case GL_TEXTURE_3D:
334                 sampler_type[i] = WINED3DSTT_VOLUME;
335                 break;
336
337             case GL_TEXTURE_CUBE_MAP_ARB:
338                 sampler_type[i] = WINED3DSTT_CUBE;
339                 break;
340
341             default:
342                 FIXME("Unrecognized texture type %#x, using 2D\n",
343                         IWineD3DBaseTexture_GetTextureDimensions(textures[i]));
344                 sampler_type[i] = WINED3DSTT_2D;
345         }
346     }
347 }
348
349 static const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
350 {
351     /*** IUnknown methods ***/
352     IWineD3DPixelShaderImpl_QueryInterface,
353     IWineD3DPixelShaderImpl_AddRef,
354     IWineD3DPixelShaderImpl_Release,
355     /*** IWineD3DBase methods ***/
356     IWineD3DPixelShaderImpl_GetParent,
357     /*** IWineD3DBaseShader methods ***/
358     IWineD3DPixelShaderImpl_GetDevice,
359     IWineD3DPixelShaderImpl_GetFunction
360     /*** IWineD3DPixelShader methods ***/
361 };
362
363 void find_ps_compile_args(IWineD3DPixelShaderImpl *shader, IWineD3DStateBlockImpl *stateblock, struct ps_compile_args *args) {
364     UINT i;
365     IWineD3DBaseTextureImpl *tex;
366
367     memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set */
368     args->srgb_correction = stateblock->renderState[WINED3DRS_SRGBWRITEENABLE] ? 1 : 0;
369     args->np2_fixup = 0;
370
371     for(i = 0; i < MAX_FRAGMENT_SAMPLERS; i++) {
372         if (!shader->baseShader.reg_maps.sampler_type[i]) continue;
373         tex = (IWineD3DBaseTextureImpl *) stateblock->textures[i];
374         if(!tex) {
375             args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
376             continue;
377         }
378         args->color_fixup[i] = tex->resource.format_desc->color_fixup;
379
380         /* Flag samplers that need NP2 texcoord fixup. */
381         if(!tex->baseTexture.pow2Matrix_identity) {
382             args->np2_fixup |= (1 << i);
383         }
384     }
385     if (shader->baseShader.reg_maps.shader_version.major >= 3)
386     {
387         if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed)
388         {
389             args->vp_mode = pretransformed;
390         }
391         else if (use_vs(stateblock))
392         {
393             args->vp_mode = vertexshader;
394         } else {
395             args->vp_mode = fixedfunction;
396         }
397         args->fog = FOG_OFF;
398     } else {
399         args->vp_mode = vertexshader;
400         if(stateblock->renderState[WINED3DRS_FOGENABLE]) {
401             switch(stateblock->renderState[WINED3DRS_FOGTABLEMODE]) {
402                 case WINED3DFOG_NONE:
403                     if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed
404                             || use_vs(stateblock))
405                     {
406                         args->fog = FOG_LINEAR;
407                         break;
408                     }
409                     switch(stateblock->renderState[WINED3DRS_FOGVERTEXMODE]) {
410                         case WINED3DFOG_NONE: /* Drop through */
411                         case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
412                         case WINED3DFOG_EXP:    args->fog = FOG_EXP;    break;
413                         case WINED3DFOG_EXP2:   args->fog = FOG_EXP2;   break;
414                     }
415                     break;
416
417                 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
418                 case WINED3DFOG_EXP:    args->fog = FOG_EXP;    break;
419                 case WINED3DFOG_EXP2:   args->fog = FOG_EXP2;   break;
420             }
421         } else {
422             args->fog = FOG_OFF;
423         }
424     }
425 }
426
427 HRESULT pixelshader_init(IWineD3DPixelShaderImpl *shader, IWineD3DDeviceImpl *device,
428         const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
429         IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
430 {
431     HRESULT hr;
432
433     if (!byte_code) return WINED3DERR_INVALIDCALL;
434
435     shader->lpVtbl = &IWineD3DPixelShader_Vtbl;
436     shader_init(&shader->baseShader, device, parent, parent_ops);
437
438     hr = pixelshader_set_function(shader, byte_code, output_signature);
439     if (FAILED(hr))
440     {
441         WARN("Failed to set function, hr %#x.\n", hr);
442         shader_cleanup((IWineD3DBaseShader *)shader);
443         return hr;
444     }
445
446     return WINED3D_OK;
447 }