VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vertexshader.c@ 21731

Last change on this file since 21731 was 21731, checked in by vboxsync, 16 years ago

crOpenGL: update to wine 1.1.26

  • Property svn:eol-style set to native
File size: 14.5 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 void vshader_set_limits(IWineD3DVertexShaderImpl *This)
47{
48 DWORD shader_version = WINED3D_SHADER_VERSION(This->baseShader.reg_maps.shader_version.major,
49 This->baseShader.reg_maps.shader_version.minor);
50
51 This->baseShader.limits.texcoord = 0;
52 This->baseShader.limits.attributes = 16;
53 This->baseShader.limits.packed_input = 0;
54
55 switch (shader_version)
56 {
57 case WINED3D_SHADER_VERSION(1,0):
58 case WINED3D_SHADER_VERSION(1,1):
59 This->baseShader.limits.temporary = 12;
60 This->baseShader.limits.constant_bool = 0;
61 This->baseShader.limits.constant_int = 0;
62 This->baseShader.limits.address = 1;
63 This->baseShader.limits.packed_output = 0;
64 This->baseShader.limits.sampler = 0;
65 This->baseShader.limits.label = 0;
66 /* TODO: vs_1_1 has a minimum of 96 constants. What happens if a vs_1_1 shader is used
67 * on a vs_3_0 capable card that has 256 constants? */
68 This->baseShader.limits.constant_float = min(256, GL_LIMITS(vshader_constantsF));
69 break;
70
71 case WINED3D_SHADER_VERSION(2,0):
72 case WINED3D_SHADER_VERSION(2,1):
73 This->baseShader.limits.temporary = 12;
74 This->baseShader.limits.constant_bool = 16;
75 This->baseShader.limits.constant_int = 16;
76 This->baseShader.limits.address = 1;
77 This->baseShader.limits.packed_output = 0;
78 This->baseShader.limits.sampler = 0;
79 This->baseShader.limits.label = 16;
80 This->baseShader.limits.constant_float = min(256, GL_LIMITS(vshader_constantsF));
81 break;
82
83 case WINED3D_SHADER_VERSION(4,0):
84 FIXME("Using 3.0 limits for 4.0 shader\n");
85 /* Fall through */
86
87 case WINED3D_SHADER_VERSION(3,0):
88 This->baseShader.limits.temporary = 32;
89 This->baseShader.limits.constant_bool = 32;
90 This->baseShader.limits.constant_int = 32;
91 This->baseShader.limits.address = 1;
92 This->baseShader.limits.packed_output = 12;
93 This->baseShader.limits.sampler = 4;
94 This->baseShader.limits.label = 16; /* FIXME: 2048 */
95 /* DX10 cards on Windows advertise a d3d9 constant limit of 256 even though they are capable
96 * of supporting much more(GL drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
97 * wined3d-advertised maximum. Clamp the constant limit for <= 3.0 shaders to 256.s
98 * use constant buffers */
99 This->baseShader.limits.constant_float = min(256, GL_LIMITS(vshader_constantsF));
100 break;
101
102 default:
103 This->baseShader.limits.temporary = 12;
104 This->baseShader.limits.constant_bool = 16;
105 This->baseShader.limits.constant_int = 16;
106 This->baseShader.limits.address = 1;
107 This->baseShader.limits.packed_output = 0;
108 This->baseShader.limits.sampler = 0;
109 This->baseShader.limits.label = 16;
110 This->baseShader.limits.constant_float = min(256, GL_LIMITS(vshader_constantsF));
111 FIXME("Unrecognized vertex shader version %u.%u\n",
112 This->baseShader.reg_maps.shader_version.major,
113 This->baseShader.reg_maps.shader_version.minor);
114 }
115}
116
117static BOOL match_usage(BYTE usage1, BYTE usage_idx1, BYTE usage2, BYTE usage_idx2) {
118 if (usage_idx1 != usage_idx2) return FALSE;
119 if (usage1 == usage2) return TRUE;
120 if (usage1 == WINED3DDECLUSAGE_POSITION && usage2 == WINED3DDECLUSAGE_POSITIONT) return TRUE;
121 if (usage2 == WINED3DDECLUSAGE_POSITION && usage1 == WINED3DDECLUSAGE_POSITIONT) return TRUE;
122
123 return FALSE;
124}
125
126BOOL vshader_get_input(IWineD3DVertexShader* iface, BYTE usage_req, BYTE usage_idx_req, unsigned int *regnum)
127{
128 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
129 WORD map = This->baseShader.reg_maps.input_registers;
130 unsigned int i;
131
132 for (i = 0; map; map >>= 1, ++i)
133 {
134 if (!(map & 1)) continue;
135
136 if (match_usage(This->attributes[i].usage,
137 This->attributes[i].usage_idx, usage_req, usage_idx_req))
138 {
139 *regnum = i;
140 return TRUE;
141 }
142 }
143 return FALSE;
144}
145
146/* *******************************************
147 IWineD3DVertexShader IUnknown parts follow
148 ******************************************* */
149static HRESULT WINAPI IWineD3DVertexShaderImpl_QueryInterface(IWineD3DVertexShader *iface, REFIID riid, LPVOID *ppobj) {
150 TRACE("iface %p, riid %s, ppobj %p\n", iface, debugstr_guid(riid), ppobj);
151
152 if (IsEqualGUID(riid, &IID_IWineD3DVertexShader)
153 || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
154 || IsEqualGUID(riid, &IID_IWineD3DBase)
155 || IsEqualGUID(riid, &IID_IUnknown))
156 {
157 IUnknown_AddRef(iface);
158 *ppobj = iface;
159 return S_OK;
160 }
161
162 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
163
164 *ppobj = NULL;
165 return E_NOINTERFACE;
166}
167
168static ULONG WINAPI IWineD3DVertexShaderImpl_AddRef(IWineD3DVertexShader *iface) {
169 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
170 ULONG refcount = InterlockedIncrement(&This->baseShader.ref);
171
172 TRACE("%p increasing refcount to %u\n", This, refcount);
173
174 return refcount;
175}
176
177static ULONG WINAPI IWineD3DVertexShaderImpl_Release(IWineD3DVertexShader *iface) {
178 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
179 ULONG refcount = InterlockedDecrement(&This->baseShader.ref);
180
181 TRACE("%p decreasing refcount to %u\n", This, refcount);
182
183 if (!refcount)
184 {
185 shader_cleanup((IWineD3DBaseShader *)iface);
186 HeapFree(GetProcessHeap(), 0, This);
187 }
188
189 return refcount;
190}
191
192/* *******************************************
193 IWineD3DVertexShader IWineD3DVertexShader parts follow
194 ******************************************* */
195
196static HRESULT WINAPI IWineD3DVertexShaderImpl_GetParent(IWineD3DVertexShader *iface, IUnknown** parent){
197 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
198
199 *parent = This->parent;
200 IUnknown_AddRef(*parent);
201 TRACE("(%p) : returning %p\n", This, *parent);
202 return WINED3D_OK;
203}
204
205static HRESULT WINAPI IWineD3DVertexShaderImpl_GetDevice(IWineD3DVertexShader* iface, IWineD3DDevice **pDevice){
206 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
207 IWineD3DDevice_AddRef(This->baseShader.device);
208 *pDevice = This->baseShader.device;
209 TRACE("(%p) returning %p\n", This, *pDevice);
210 return WINED3D_OK;
211}
212
213static HRESULT WINAPI IWineD3DVertexShaderImpl_GetFunction(IWineD3DVertexShader* impl, VOID* pData, UINT* pSizeOfData) {
214 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)impl;
215 TRACE("(%p) : pData(%p), pSizeOfData(%p)\n", This, pData, pSizeOfData);
216
217 if (NULL == pData) {
218 *pSizeOfData = This->baseShader.functionLength;
219 return WINED3D_OK;
220 }
221 if (*pSizeOfData < This->baseShader.functionLength) {
222 /* MSDN claims (for d3d8 at least) that if *pSizeOfData is smaller
223 * than the required size we should write the required size and
224 * return D3DERR_MOREDATA. That's not actually true. */
225 return WINED3DERR_INVALIDCALL;
226 }
227
228 TRACE("(%p) : GetFunction copying to %p\n", This, pData);
229 memcpy(pData, This->baseShader.function, This->baseShader.functionLength);
230
231 return WINED3D_OK;
232}
233
234/* Note that for vertex shaders CompileShader isn't called until the
235 * shader is first used. The reason for this is that we need the vertex
236 * declaration the shader will be used with in order to determine if
237 * the data in a register is of type D3DCOLOR, and needs swizzling. */
238static HRESULT WINAPI IWineD3DVertexShaderImpl_SetFunction(IWineD3DVertexShader *iface,
239 const DWORD *pFunction, const struct wined3d_shader_signature *output_signature)
240{
241 IWineD3DVertexShaderImpl *This =(IWineD3DVertexShaderImpl *)iface;
242 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *) This->baseShader.device;
243 const struct wined3d_shader_frontend *fe;
244 unsigned int i;
245 HRESULT hr;
246 shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
247
248 TRACE("(%p) : pFunction %p\n", iface, pFunction);
249
250 fe = shader_select_frontend(*pFunction);
251 if (!fe)
252 {
253 FIXME("Unable to find frontend for shader.\n");
254 return WINED3DERR_INVALIDCALL;
255 }
256 This->baseShader.frontend = fe;
257 This->baseShader.frontend_data = fe->shader_init(pFunction, output_signature);
258 if (!This->baseShader.frontend_data)
259 {
260 FIXME("Failed to initialize frontend.\n");
261 return WINED3DERR_INVALIDCALL;
262 }
263
264 /* First pass: trace shader */
265 if (TRACE_ON(d3d_shader)) shader_trace_init(fe, This->baseShader.frontend_data, pFunction);
266
267 /* Initialize immediate constant lists */
268 list_init(&This->baseShader.constantsF);
269 list_init(&This->baseShader.constantsB);
270 list_init(&This->baseShader.constantsI);
271
272 /* Second pass: figure out registers used, semantics, etc.. */
273 This->min_rel_offset = GL_LIMITS(vshader_constantsF);
274 This->max_rel_offset = 0;
275 hr = shader_get_registers_used((IWineD3DBaseShader*) This, fe,
276 reg_maps, This->attributes, NULL, This->output_signature,
277 pFunction, GL_LIMITS(vshader_constantsF));
278 if (hr != WINED3D_OK) return hr;
279
280 if (output_signature)
281 {
282 for (i = 0; i < output_signature->element_count; ++i)
283 {
284 struct wined3d_shader_signature_element *e = &output_signature->elements[i];
285 reg_maps->output_registers |= 1 << e->register_idx;
286 This->output_signature[e->register_idx] = *e;
287 }
288 }
289
290 vshader_set_limits(This);
291
292 if (deviceImpl->vs_selected_mode == SHADER_ARB
293 && ((GLINFO_LOCATION).quirks & WINED3D_QUIRK_ARB_VS_OFFSET_LIMIT)
294 && This->min_rel_offset <= This->max_rel_offset)
295 {
296 if(This->max_rel_offset - This->min_rel_offset > 127) {
297 FIXME("The difference between the minimum and maximum relative offset is > 127\n");
298 FIXME("Which this OpenGL implementation does not support. Try using GLSL\n");
299 FIXME("Min: %d, Max: %d\n", This->min_rel_offset, This->max_rel_offset);
300 } else if(This->max_rel_offset - This->min_rel_offset > 63) {
301 This->rel_offset = This->min_rel_offset + 63;
302 } else if(This->max_rel_offset > 63) {
303 This->rel_offset = This->min_rel_offset;
304 } else {
305 This->rel_offset = 0;
306 }
307 }
308 This->baseShader.load_local_constsF = This->baseShader.reg_maps.usesrelconstF && !list_empty(&This->baseShader.constantsF);
309
310 /* copy the function ... because it will certainly be released by application */
311 This->baseShader.function = HeapAlloc(GetProcessHeap(), 0, This->baseShader.functionLength);
312 if (!This->baseShader.function) return E_OUTOFMEMORY;
313 memcpy(This->baseShader.function, pFunction, This->baseShader.functionLength);
314
315 return WINED3D_OK;
316}
317
318/* Set local constants for d3d8 shaders */
319static HRESULT WINAPI IWIneD3DVertexShaderImpl_SetLocalConstantsF(IWineD3DVertexShader *iface,
320 UINT start_idx, const float *src_data, UINT count) {
321 IWineD3DVertexShaderImpl *This =(IWineD3DVertexShaderImpl *)iface;
322 UINT i, end_idx;
323
324 TRACE("(%p) : start_idx %u, src_data %p, count %u\n", This, start_idx, src_data, count);
325
326 end_idx = start_idx + count;
327 if (end_idx > GL_LIMITS(vshader_constantsF)) {
328 WARN("end_idx %u > float constants limit %u\n", end_idx, GL_LIMITS(vshader_constantsF));
329 end_idx = GL_LIMITS(vshader_constantsF);
330 }
331
332 for (i = start_idx; i < end_idx; ++i) {
333 local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
334 if (!lconst) return E_OUTOFMEMORY;
335
336 lconst->idx = i;
337 memcpy(lconst->value, src_data + (i - start_idx) * 4 /* 4 components */, 4 * sizeof(float));
338 list_add_head(&This->baseShader.constantsF, &lconst->entry);
339 }
340
341 return WINED3D_OK;
342}
343
344const IWineD3DVertexShaderVtbl IWineD3DVertexShader_Vtbl =
345{
346 /*** IUnknown methods ***/
347 IWineD3DVertexShaderImpl_QueryInterface,
348 IWineD3DVertexShaderImpl_AddRef,
349 IWineD3DVertexShaderImpl_Release,
350 /*** IWineD3DBase methods ***/
351 IWineD3DVertexShaderImpl_GetParent,
352 /*** IWineD3DBaseShader methods ***/
353 IWineD3DVertexShaderImpl_SetFunction,
354 /*** IWineD3DVertexShader methods ***/
355 IWineD3DVertexShaderImpl_GetDevice,
356 IWineD3DVertexShaderImpl_GetFunction,
357 IWIneD3DVertexShaderImpl_SetLocalConstantsF
358};
359
360void find_vs_compile_args(IWineD3DVertexShaderImpl *shader, IWineD3DStateBlockImpl *stateblock, struct vs_compile_args *args) {
361 args->fog_src = stateblock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE ? VS_FOG_COORD : VS_FOG_Z;
362 args->swizzle_map = ((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.swizzle_map;
363}
Note: See TracBrowser for help on using the repository browser.

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