Sync ddraw, d3d8 and d3d9 + wined3d to wine 1.1.28
[reactos.git] / reactos / dll / directx / wine / 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(4,0):
182 FIXME("Using 3.0 limits for 4.0 shader\n");
183 /* Fall through */
184
185 case WINED3D_SHADER_VERSION(3,0):
186 This->baseShader.limits.temporary = 32;
187 This->baseShader.limits.constant_float = 224;
188 This->baseShader.limits.constant_int = 16;
189 This->baseShader.limits.constant_bool = 16;
190 This->baseShader.limits.texcoord = 0;
191 This->baseShader.limits.sampler = 16;
192 This->baseShader.limits.packed_input = 12;
193 This->baseShader.limits.label = 16; /* FIXME: 2048 */
194 break;
195
196 default:
197 This->baseShader.limits.temporary = 32;
198 This->baseShader.limits.constant_float = 32;
199 This->baseShader.limits.constant_int = 16;
200 This->baseShader.limits.constant_bool = 16;
201 This->baseShader.limits.texcoord = 8;
202 This->baseShader.limits.sampler = 16;
203 This->baseShader.limits.packed_input = 0;
204 This->baseShader.limits.label = 0;
205 FIXME("Unrecognized pixel shader version %u.%u\n",
206 This->baseShader.reg_maps.shader_version.major,
207 This->baseShader.reg_maps.shader_version.minor);
208 }
209 }
210
211 static HRESULT WINAPI IWineD3DPixelShaderImpl_SetFunction(IWineD3DPixelShader *iface,
212 const DWORD *pFunction, const struct wined3d_shader_signature *output_signature)
213 {
214 IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
215 unsigned int i, highest_reg_used = 0, num_regs_used = 0;
216 shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
217 const struct wined3d_shader_frontend *fe;
218 HRESULT hr;
219
220 TRACE("(%p) : pFunction %p\n", iface, pFunction);
221
222 fe = shader_select_frontend(*pFunction);
223 if (!fe)
224 {
225 FIXME("Unable to find frontend for shader.\n");
226 return WINED3DERR_INVALIDCALL;
227 }
228 This->baseShader.frontend = fe;
229 This->baseShader.frontend_data = fe->shader_init(pFunction, output_signature);
230 if (!This->baseShader.frontend_data)
231 {
232 FIXME("Failed to initialize frontend.\n");
233 return WINED3DERR_INVALIDCALL;
234 }
235
236 /* First pass: trace shader */
237 if (TRACE_ON(d3d_shader)) shader_trace_init(fe, This->baseShader.frontend_data, pFunction);
238
239 /* Initialize immediate constant lists */
240 list_init(&This->baseShader.constantsF);
241 list_init(&This->baseShader.constantsB);
242 list_init(&This->baseShader.constantsI);
243
244 /* Second pass: figure out which registers are used, what the semantics are, etc.. */
245 hr = shader_get_registers_used((IWineD3DBaseShader *)This, fe,
246 reg_maps, NULL, This->input_signature, NULL,
247 pFunction, GL_LIMITS(pshader_constantsF));
248 if (FAILED(hr)) return hr;
249
250 pshader_set_limits(This);
251
252 for (i = 0; i < MAX_REG_INPUT; ++i)
253 {
254 if (This->input_reg_used[i])
255 {
256 ++num_regs_used;
257 highest_reg_used = i;
258 }
259 }
260
261 /* Don't do any register mapping magic if it is not needed, or if we can't
262 * achieve anything anyway */
263 if (highest_reg_used < (GL_LIMITS(glsl_varyings) / 4)
264 || num_regs_used > (GL_LIMITS(glsl_varyings) / 4))
265 {
266 if (num_regs_used > (GL_LIMITS(glsl_varyings) / 4))
267 {
268 /* This happens with relative addressing. The input mapper function
269 * warns about this if the higher registers are declared too, so
270 * don't write a FIXME here */
271 WARN("More varying registers used than supported\n");
272 }
273
274 for (i = 0; i < MAX_REG_INPUT; ++i)
275 {
276 This->input_reg_map[i] = i;
277 }
278
279 This->declared_in_count = highest_reg_used + 1;
280 }
281 else
282 {
283 This->declared_in_count = 0;
284 for (i = 0; i < MAX_REG_INPUT; ++i)
285 {
286 if (This->input_reg_used[i]) This->input_reg_map[i] = This->declared_in_count++;
287 else This->input_reg_map[i] = ~0U;
288 }
289 }
290
291 This->baseShader.load_local_constsF = FALSE;
292
293 TRACE("(%p) : Copying the function\n", This);
294
295 This->baseShader.function = HeapAlloc(GetProcessHeap(), 0, This->baseShader.functionLength);
296 if (!This->baseShader.function) return E_OUTOFMEMORY;
297 memcpy(This->baseShader.function, pFunction, This->baseShader.functionLength);
298
299 return WINED3D_OK;
300 }
301
302 void pixelshader_update_samplers(struct shader_reg_maps *reg_maps, IWineD3DBaseTexture * const *textures)
303 {
304 WINED3DSAMPLER_TEXTURE_TYPE *sampler_type = reg_maps->sampler_type;
305 unsigned int i;
306
307 if (reg_maps->shader_version.major != 1) return;
308
309 for (i = 0; i < max(MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS); ++i)
310 {
311 /* We don't sample from this sampler */
312 if (!sampler_type[i]) continue;
313
314 if (!textures[i])
315 {
316 ERR("No texture bound to sampler %u, using 2D\n", i);
317 sampler_type[i] = WINED3DSTT_2D;
318 continue;
319 }
320
321 switch (IWineD3DBaseTexture_GetTextureDimensions(textures[i]))
322 {
323 case GL_TEXTURE_RECTANGLE_ARB:
324 case GL_TEXTURE_2D:
325 /* We have to select between texture rectangles and 2D textures later because 2.0 and
326 * 3.0 shaders only have WINED3DSTT_2D as well */
327 sampler_type[i] = WINED3DSTT_2D;
328 break;
329
330 case GL_TEXTURE_3D:
331 sampler_type[i] = WINED3DSTT_VOLUME;
332 break;
333
334 case GL_TEXTURE_CUBE_MAP_ARB:
335 sampler_type[i] = WINED3DSTT_CUBE;
336 break;
337
338 default:
339 FIXME("Unrecognized texture type %#x, using 2D\n",
340 IWineD3DBaseTexture_GetTextureDimensions(textures[i]));
341 sampler_type[i] = WINED3DSTT_2D;
342 }
343 }
344 }
345
346 const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
347 {
348 /*** IUnknown methods ***/
349 IWineD3DPixelShaderImpl_QueryInterface,
350 IWineD3DPixelShaderImpl_AddRef,
351 IWineD3DPixelShaderImpl_Release,
352 /*** IWineD3DBase methods ***/
353 IWineD3DPixelShaderImpl_GetParent,
354 /*** IWineD3DBaseShader methods ***/
355 IWineD3DPixelShaderImpl_SetFunction,
356 /*** IWineD3DPixelShader methods ***/
357 IWineD3DPixelShaderImpl_GetDevice,
358 IWineD3DPixelShaderImpl_GetFunction
359 };
360
361 void find_ps_compile_args(IWineD3DPixelShaderImpl *shader, IWineD3DStateBlockImpl *stateblock, struct ps_compile_args *args) {
362 UINT i;
363 IWineD3DBaseTextureImpl *tex;
364
365 memset(args, 0, sizeof(*args)); /* FIXME: Make sure all bits are set */
366 args->srgb_correction = stateblock->renderState[WINED3DRS_SRGBWRITEENABLE] ? 1 : 0;
367 args->np2_fixup = 0;
368
369 for(i = 0; i < MAX_FRAGMENT_SAMPLERS; i++) {
370 if (!shader->baseShader.reg_maps.sampler_type[i]) continue;
371 tex = (IWineD3DBaseTextureImpl *) stateblock->textures[i];
372 if(!tex) {
373 args->color_fixup[i] = COLOR_FIXUP_IDENTITY;
374 continue;
375 }
376 args->color_fixup[i] = tex->resource.format_desc->color_fixup;
377
378 /* Flag samplers that need NP2 texcoord fixup. */
379 if(!tex->baseTexture.pow2Matrix_identity) {
380 args->np2_fixup |= (1 << i);
381 }
382 }
383 if (shader->baseShader.reg_maps.shader_version.major >= 3)
384 {
385 if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed)
386 {
387 args->vp_mode = pretransformed;
388 }
389 else if (use_vs(stateblock))
390 {
391 args->vp_mode = vertexshader;
392 } else {
393 args->vp_mode = fixedfunction;
394 }
395 args->fog = FOG_OFF;
396 } else {
397 args->vp_mode = vertexshader;
398 if(stateblock->renderState[WINED3DRS_FOGENABLE]) {
399 switch(stateblock->renderState[WINED3DRS_FOGTABLEMODE]) {
400 case WINED3DFOG_NONE:
401 if (((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.position_transformed
402 || use_vs(stateblock))
403 {
404 args->fog = FOG_LINEAR;
405 break;
406 }
407 switch(stateblock->renderState[WINED3DRS_FOGVERTEXMODE]) {
408 case WINED3DFOG_NONE: /* Drop through */
409 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
410 case WINED3DFOG_EXP: args->fog = FOG_EXP; break;
411 case WINED3DFOG_EXP2: args->fog = FOG_EXP2; break;
412 }
413 break;
414
415 case WINED3DFOG_LINEAR: args->fog = FOG_LINEAR; break;
416 case WINED3DFOG_EXP: args->fog = FOG_EXP; break;
417 case WINED3DFOG_EXP2: args->fog = FOG_EXP2; break;
418 }
419 } else {
420 args->fog = FOG_OFF;
421 }
422 }
423 }