VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/pixelshader.c@ 22652

Last change on this file since 22652 was 22496, checked in by vboxsync, 15 years ago

crOpenGL: update wine to 1.1.27 and better fix for depthstencil surface refcounting

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette