dsound: Refactor playback.
[wine/multimedia.git] / dlls / d3d8 / vshaderdeclaration.c
blobf41b61375477fe87a3bd50aee6b8f99860bd208e
1 /*
2 * vertex shaders declaration implementation
4 * Copyright 2002 Raphael Junqueira
5 * Copyright 2004 Jason Edmeades
6 * Copyright 2004 Christian Costa
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
25 #include <math.h>
26 #include <stdarg.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "wingdi.h"
32 #include "wine/debug.h"
34 #include "d3d8_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
38 /**
39 * DirectX9 SDK download
40 * http://msdn.microsoft.com/library/default.asp?url=/downloads/list/directx.asp
42 * Exploring D3DX
43 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndrive/html/directx07162002.asp
45 * Using Vertex Shaders
46 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndrive/html/directx02192001.asp
48 * Dx9 New
49 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/whatsnew.asp
51 * Dx9 Shaders
52 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader2_0/VertexShader2_0.asp
53 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader2_0/Instructions/Instructions.asp
54 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/GettingStarted/VertexDeclaration/VertexDeclaration.asp
55 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader3_0/VertexShader3_0.asp
57 * Dx9 D3DX
58 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/advancedtopics/VertexPipe/matrixstack/matrixstack.asp
60 * FVF
61 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/GettingStarted/VertexFormats/vformats.asp
63 * NVIDIA: DX8 Vertex Shader to NV Vertex Program
64 * http://developer.nvidia.com/view.asp?IO=vstovp
66 * NVIDIA: Memory Management with VAR
67 * http://developer.nvidia.com/view.asp?IO=var_memory_management
70 /** Vertex Shader Declaration data types tokens */
71 #define MAX_VSHADER_DECL_TYPES 8
72 static CONST char* VertexShaderDeclDataTypes[] = {
73 "D3DVSDT_FLOAT1",
74 "D3DVSDT_FLOAT2",
75 "D3DVSDT_FLOAT3",
76 "D3DVSDT_FLOAT4",
77 "D3DVSDT_D3DCOLOR",
78 "D3DVSDT_UBYTE4",
79 "D3DVSDT_SHORT2",
80 "D3DVSDT_SHORT4",
81 NULL
84 static CONST char* VertexShaderDeclRegister[] = {
85 "D3DVSDE_POSITION",
86 "D3DVSDE_BLENDWEIGHT",
87 "D3DVSDE_BLENDINDICES",
88 "D3DVSDE_NORMAL",
89 "D3DVSDE_PSIZE",
90 "D3DVSDE_DIFFUSE",
91 "D3DVSDE_SPECULAR",
92 "D3DVSDE_TEXCOORD0",
93 "D3DVSDE_TEXCOORD1",
94 "D3DVSDE_TEXCOORD2",
95 "D3DVSDE_TEXCOORD3",
96 "D3DVSDE_TEXCOORD4",
97 "D3DVSDE_TEXCOORD5",
98 "D3DVSDE_TEXCOORD6",
99 "D3DVSDE_TEXCOORD7",
100 "D3DVSDE_POSITION2",
101 "D3DVSDE_NORMAL2",
102 NULL
105 /** todo check decl validity */
106 /*inline static*/ DWORD Direct3DVextexShaderDeclarationImpl_ParseToken(const DWORD* pToken) {
107 const DWORD token = *pToken;
108 DWORD tokenlen = 1;
110 switch ((token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT) { /* maybe a macro to inverse ... */
111 case D3DVSD_TOKEN_NOP:
112 TRACE(" 0x%08lx NOP()\n", token);
113 break;
114 case D3DVSD_TOKEN_STREAM:
115 if (token & D3DVSD_STREAMTESSMASK) {
116 TRACE(" 0x%08lx STREAM_TESS()\n", token);
117 } else {
118 TRACE(" 0x%08lx STREAM(%lu)\n", token, ((token & D3DVSD_STREAMNUMBERMASK) >> D3DVSD_STREAMNUMBERSHIFT));
120 break;
121 case D3DVSD_TOKEN_STREAMDATA:
122 if (token & 0x10000000) {
123 TRACE(" 0x%08lx SKIP(%lu)\n", token, ((token & D3DVSD_SKIPCOUNTMASK) >> D3DVSD_SKIPCOUNTSHIFT));
124 } else {
125 DWORD type = ((token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
126 DWORD reg = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
127 TRACE(" 0x%08lx REG(%s, %s)\n", token, VertexShaderDeclRegister[reg], VertexShaderDeclDataTypes[type]);
129 break;
130 case D3DVSD_TOKEN_TESSELLATOR:
131 if (token & 0x10000000) {
132 DWORD type = ((token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
133 DWORD reg = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
134 TRACE(" 0x%08lx TESSUV(%s) as %s\n", token, VertexShaderDeclRegister[reg], VertexShaderDeclDataTypes[type]);
135 } else {
136 DWORD type = ((token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
137 DWORD regout = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
138 DWORD regin = ((token & D3DVSD_VERTEXREGINMASK) >> D3DVSD_VERTEXREGINSHIFT);
139 TRACE(" 0x%08lx TESSNORMAL(%s, %s) as %s\n", token, VertexShaderDeclRegister[regin], VertexShaderDeclRegister[regout], VertexShaderDeclDataTypes[type]);
141 break;
142 case D3DVSD_TOKEN_CONSTMEM:
144 DWORD i;
145 DWORD count = ((token & D3DVSD_CONSTCOUNTMASK) >> D3DVSD_CONSTCOUNTSHIFT);
146 DWORD constaddress = ((token & D3DVSD_CONSTADDRESSMASK) >> D3DVSD_CONSTADDRESSSHIFT);
147 TRACE(" 0x%08lx CONST(%lu, %lu)\n", token, constaddress, count);
148 ++pToken;
149 for (i = 0; i < count; ++i) {
150 #if 0
151 TRACE(" c[%lu] = (0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx)\n",
152 constaddress,
153 *pToken,
154 *(pToken + 1),
155 *(pToken + 2),
156 *(pToken + 3));
157 #endif
158 TRACE(" c[%lu] = (%8f, %8f, %8f, %8f)\n",
159 constaddress,
160 *(const float*) pToken,
161 *(const float*) (pToken + 1),
162 *(const float*) (pToken + 2),
163 *(const float*) (pToken + 3));
164 pToken += 4;
165 ++constaddress;
167 tokenlen = (4 * count) + 1;
169 break;
170 case D3DVSD_TOKEN_EXT:
172 DWORD count = ((token & D3DVSD_CONSTCOUNTMASK) >> D3DVSD_CONSTCOUNTSHIFT);
173 DWORD extinfo = ((token & D3DVSD_EXTINFOMASK) >> D3DVSD_EXTINFOSHIFT);
174 TRACE(" 0x%08lx EXT(%lu, %lu)\n", token, count, extinfo);
175 /* todo ... print extension */
176 tokenlen = count + 1;
178 break;
179 case D3DVSD_TOKEN_END:
180 TRACE(" 0x%08lx END()\n", token);
181 break;
182 default:
183 TRACE(" 0x%08lx UNKNOWN\n", token);
184 /* argg error */
186 return tokenlen;
189 HRESULT WINAPI IDirect3DDeviceImpl_CreateVertexShaderDeclaration8(IDirect3DDevice8Impl* This, CONST DWORD* pDeclaration8, IDirect3DVertexShaderDeclarationImpl** ppVertexShaderDecl) {
190 /** parser data */
191 const DWORD* pToken = pDeclaration8;
192 DWORD fvf = 0;
193 DWORD len = 0;
194 DWORD stream = 0;
195 DWORD token;
196 DWORD tokenlen;
197 DWORD tokentype;
198 DWORD tex = D3DFVF_TEX0;
199 /** TRUE if declaration can be matched by a fvf */
200 IDirect3DVertexShaderDeclarationImpl* object;
201 BOOL invalid_fvf = FALSE;
203 TRACE("(%p) : pDeclaration8(%p)\n", This, pDeclaration8);
205 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DVertexShaderDeclarationImpl));
207 /*object->lpVtbl = &Direct3DVextexShaderDeclaration8_Vtbl;*/
208 object->device = This; /* FIXME: AddRef(This) */
209 object->ref = 1;
210 object->allFVF = 0;
212 while (D3DVSD_END() != *pToken) {
213 token = *pToken;
214 tokenlen = Direct3DVextexShaderDeclarationImpl_ParseToken(pToken);
215 tokentype = ((token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT);
217 /** FVF generation block */
218 if (D3DVSD_TOKEN_STREAM == tokentype && 0 == (D3DVSD_STREAMTESSMASK & token)) {
219 /**
220 * how really works streams,
221 * in DolphinVS dx8 dsk sample they seems to decal reg numbers !!!
223 DWORD oldStream = stream;
224 stream = ((token & D3DVSD_STREAMNUMBERMASK) >> D3DVSD_STREAMNUMBERSHIFT);
226 /* copy fvf if valid */
227 if (FALSE == invalid_fvf) {
228 fvf |= tex << D3DFVF_TEXCOUNT_SHIFT;
229 tex = 0;
230 object->fvf[oldStream] = fvf;
231 object->allFVF |= fvf;
232 } else {
233 object->fvf[oldStream] = 0;
234 tex = 0;
237 /* reset valid/invalid fvf */
238 fvf = 0;
239 invalid_fvf = FALSE;
241 } else if (D3DVSD_TOKEN_STREAMDATA == tokentype && 0 == (0x10000000 & tokentype)) {
242 DWORD type = ((token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
243 DWORD reg = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
245 switch (reg) {
246 case D3DVSDE_POSITION:
247 switch (type) {
248 case D3DVSDT_FLOAT3: fvf |= D3DFVF_XYZ; break;
249 case D3DVSDT_FLOAT4: fvf |= D3DFVF_XYZRHW; break;
250 default:
251 /** Mismatched use of a register, invalid for fixed function fvf computing (ok for VS) */
252 invalid_fvf = TRUE;
253 if (type >= MAX_VSHADER_DECL_TYPES) {
254 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_POSITION register: unsupported and unrecognized type %08lx\n", type);
255 } else {
256 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_POSITION register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
259 break;
261 case D3DVSDE_BLENDWEIGHT:
262 switch (type) {
263 case D3DVSDT_FLOAT1: fvf |= D3DFVF_XYZB1; break;
264 case D3DVSDT_FLOAT2: fvf |= D3DFVF_XYZB2; break;
265 case D3DVSDT_FLOAT3: fvf |= D3DFVF_XYZB3; break;
266 case D3DVSDT_FLOAT4: fvf |= D3DFVF_XYZB4; break;
267 default:
268 /** Mismatched use of a register, invalid for fixed function fvf computing (ok for VS) */
269 invalid_fvf = TRUE;
270 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_BLENDWEIGHT register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
272 break;
274 case D3DVSDE_BLENDINDICES: /* seem to be B5 as said in MSDN Dx9SDK ?? */
275 switch (type) {
276 case D3DVSDT_UBYTE4: fvf |= D3DFVF_LASTBETA_UBYTE4; break;
277 default:
278 /** Mismatched use of a register, invalid for fixed function fvf computing (ok for VS) */
279 invalid_fvf = TRUE;
280 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_BLENDINDINCES register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
282 break;
284 case D3DVSDE_NORMAL: /* TODO: only FLOAT3 supported ... another choice possible ? */
285 switch (type) {
286 case D3DVSDT_FLOAT3: fvf |= D3DFVF_NORMAL; break;
287 default:
288 /** Mismatched use of a register, invalid for fixed function fvf computing (ok for VS) */
289 invalid_fvf = TRUE;
290 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_NORMAL register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
292 break;
294 case D3DVSDE_PSIZE: /* TODO: only FLOAT1 supported ... another choice possible ? */
295 switch (type) {
296 case D3DVSDT_FLOAT1: fvf |= D3DFVF_PSIZE; break;
297 default:
298 /** Mismatched use of a register, invalid for fixed function fvf computing (ok for VS) */
299 invalid_fvf = TRUE;
300 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_PSIZE register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
302 break;
304 case D3DVSDE_DIFFUSE: /* TODO: only D3DCOLOR supported */
305 switch (type) {
306 case D3DVSDT_D3DCOLOR: fvf |= D3DFVF_DIFFUSE; break;
307 default:
308 /** Mismatched use of a register, invalid for fixed function fvf computing (ok for VS) */
309 invalid_fvf = TRUE;
310 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_DIFFUSE register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
312 break;
314 case D3DVSDE_SPECULAR: /* TODO: only D3DCOLOR supported */
315 switch (type) {
316 case D3DVSDT_D3DCOLOR: fvf |= D3DFVF_SPECULAR; break;
317 default:
318 /** Mismatched use of a register, invalid for fixed function fvf computing (ok for VS) */
319 invalid_fvf = TRUE;
320 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_SPECULAR register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
322 break;
324 case D3DVSDE_TEXCOORD0:
325 case D3DVSDE_TEXCOORD1:
326 case D3DVSDE_TEXCOORD2:
327 case D3DVSDE_TEXCOORD3:
328 case D3DVSDE_TEXCOORD4:
329 case D3DVSDE_TEXCOORD5:
330 case D3DVSDE_TEXCOORD6:
331 case D3DVSDE_TEXCOORD7:
332 /* Fixme? - assume all tex coords in same stream */
334 int texNo = 1 + (reg - D3DVSDE_TEXCOORD0);
335 tex = max(tex, texNo);
336 switch (type) {
337 case D3DVSDT_FLOAT1: fvf |= D3DFVF_TEXCOORDSIZE1(texNo); break;
338 case D3DVSDT_FLOAT2: fvf |= D3DFVF_TEXCOORDSIZE2(texNo); break;
339 case D3DVSDT_FLOAT3: fvf |= D3DFVF_TEXCOORDSIZE3(texNo); break;
340 case D3DVSDT_FLOAT4: fvf |= D3DFVF_TEXCOORDSIZE4(texNo); break;
341 default:
342 /** Mismatched use of a register, invalid for fixed function fvf computing (ok for VS) */
343 invalid_fvf = TRUE;
344 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_TEXCOORD? register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
347 break;
349 case D3DVSDE_POSITION2: /* maybe D3DFVF_XYZRHW instead D3DFVF_XYZ (of D3DVDE_POSITION) ... to see */
350 case D3DVSDE_NORMAL2: /* FIXME i don't know what to do here ;( */
351 FIXME("[%lu] registers in VertexShader declaration not supported yet (token:0x%08lx)\n", reg, token);
352 break;
354 TRACE("VertexShader declaration define %lx as current FVF\n", fvf);
356 len += tokenlen;
357 pToken += tokenlen;
359 /* here D3DVSD_END() */
360 len += Direct3DVextexShaderDeclarationImpl_ParseToken(pToken);
362 /* copy fvf if valid */
363 if (FALSE == invalid_fvf) {
364 fvf |= tex << D3DFVF_TEXCOUNT_SHIFT;
365 object->fvf[stream] = fvf;
366 object->allFVF |= fvf;
367 } else {
368 object->fvf[stream] = 0;
370 TRACE("Completed, allFVF = %lx\n", object->allFVF);
372 /* compute size */
373 object->declaration8Length = len * sizeof(DWORD);
374 /* copy the declaration */
375 object->pDeclaration8 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, object->declaration8Length);
376 memcpy(object->pDeclaration8, pDeclaration8, object->declaration8Length);
377 /* returns */
378 *ppVertexShaderDecl = object;
379 return D3D_OK;
382 HRESULT WINAPI IDirect3DDeviceImpl_FillVertexShaderInputSW(IDirect3DDevice8Impl* This,
383 IDirect3DVertexShaderImpl* vshader,
384 DWORD SkipnStrides) {
385 /** parser data */
386 const DWORD* pToken = This->UpdateStateBlock->vertexShaderDecl->pDeclaration8;
387 DWORD stream = 0;
388 DWORD token;
389 /*DWORD tokenlen;*/
390 DWORD tokentype;
391 /** for input readers */
392 const BYTE* curPos = NULL;
393 FLOAT x, y, z, w;
394 SHORT u, v, r, t;
395 DWORD dw;
397 TRACE("(%p) - This:%p, skipstrides=%lu\n", vshader, This, SkipnStrides);
399 while (D3DVSD_END() != *pToken) {
400 token = *pToken;
401 tokentype = ((token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT);
403 /** FVF generation block */
404 if (D3DVSD_TOKEN_STREAM == tokentype && 0 == (D3DVSD_STREAMTESSMASK & token)) {
405 IDirect3DVertexBuffer8* pVB;
406 int skip = 0;
408 ++pToken;
409 /**
410 * how really works streams,
411 * in DolphinVS dx8 dsk sample use it !!!
413 stream = ((token & D3DVSD_STREAMNUMBERMASK) >> D3DVSD_STREAMNUMBERSHIFT);
414 skip = This->StateBlock->stream_stride[stream];
415 pVB = This->StateBlock->stream_source[stream];
417 if (NULL == pVB) {
418 ERR("using unitialised stream[%lu]\n", stream);
419 return D3DERR_INVALIDCALL;
420 } else {
421 if (This->StateBlock->streamIsUP) {
422 curPos = (BYTE *)pVB + (SkipnStrides * skip); /* Not really a VB */
423 } else {
424 curPos = ((IDirect3DVertexBuffer8Impl*) pVB)->allocatedMemory + (SkipnStrides * skip);
427 TRACE(" using stream[%lu] with %p (%p + (Stride %d * skip %ld))\n", stream, curPos,
428 ((IDirect3DVertexBuffer8Impl*) pVB)->allocatedMemory, skip, SkipnStrides);
430 } else if (D3DVSD_TOKEN_CONSTMEM == tokentype) {
431 /** Const decl */
432 DWORD i;
433 DWORD count = ((token & D3DVSD_CONSTCOUNTMASK) >> D3DVSD_CONSTCOUNTSHIFT);
434 DWORD constaddress = ((token & D3DVSD_CONSTADDRESSMASK) >> D3DVSD_CONSTADDRESSSHIFT);
435 ++pToken;
436 for (i = 0; i < count; ++i) {
437 vshader->data->C[constaddress + i].x = *(const float*)pToken;
438 vshader->data->C[constaddress + i].y = *(const float*)(pToken + 1);
439 vshader->data->C[constaddress + i].z = *(const float*)(pToken + 2);
440 vshader->data->C[constaddress + i].w = *(const float*)(pToken + 3);
441 pToken += 4;
444 } else if (D3DVSD_TOKEN_STREAMDATA == tokentype && 0 != (0x10000000 & tokentype)) {
445 /** skip datas */
446 DWORD skipCount = ((token & D3DVSD_SKIPCOUNTMASK) >> D3DVSD_SKIPCOUNTSHIFT);
447 TRACE(" skipping %ld dwords\n", skipCount);
448 curPos = curPos + skipCount * sizeof(DWORD);
449 ++pToken;
451 } else if (D3DVSD_TOKEN_STREAMDATA == tokentype && 0 == (0x10000000 & tokentype)) {
452 DWORD type = ((token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
453 DWORD reg = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
454 ++pToken;
456 TRACE(" type : %ld, reg = %ld\n", type, reg);
457 switch (type) {
458 case D3DVSDT_FLOAT1:
459 x = *(const float*) curPos;
460 curPos = curPos + sizeof(float);
461 /**/
462 vshader->input.V[reg].x = x;
463 vshader->input.V[reg].y = 0.0f;
464 vshader->input.V[reg].z = 0.0f;
465 vshader->input.V[reg].w = 1.0f;
466 break;
468 case D3DVSDT_FLOAT2:
469 x = *(const float*) curPos;
470 curPos = curPos + sizeof(float);
471 y = *(const float*) curPos;
472 curPos = curPos + sizeof(float);
473 /**/
474 vshader->input.V[reg].x = x;
475 vshader->input.V[reg].y = y;
476 vshader->input.V[reg].z = 0.0f;
477 vshader->input.V[reg].w = 1.0f;
478 break;
480 case D3DVSDT_FLOAT3:
481 x = *(const float*) curPos;
482 curPos = curPos + sizeof(float);
483 y = *(const float*) curPos;
484 curPos = curPos + sizeof(float);
485 z = *(const float*) curPos;
486 curPos = curPos + sizeof(float);
487 /**/
488 vshader->input.V[reg].x = x;
489 vshader->input.V[reg].y = y;
490 vshader->input.V[reg].z = z;
491 vshader->input.V[reg].w = 1.0f;
492 break;
494 case D3DVSDT_FLOAT4:
495 x = *(const float*) curPos;
496 curPos = curPos + sizeof(float);
497 y = *(const float*) curPos;
498 curPos = curPos + sizeof(float);
499 z = *(const float*) curPos;
500 curPos = curPos + sizeof(float);
501 w = *(const float*) curPos;
502 curPos = curPos + sizeof(float);
503 /**/
504 vshader->input.V[reg].x = x;
505 vshader->input.V[reg].y = y;
506 vshader->input.V[reg].z = z;
507 vshader->input.V[reg].w = w;
508 break;
510 case D3DVSDT_D3DCOLOR:
511 dw = *(const DWORD*) curPos;
512 curPos = curPos + sizeof(DWORD);
513 /**/
514 vshader->input.V[reg].x = D3DCOLOR_R(dw);
515 vshader->input.V[reg].y = D3DCOLOR_G(dw);
516 vshader->input.V[reg].z = D3DCOLOR_B(dw);
517 vshader->input.V[reg].w = D3DCOLOR_A(dw);
518 break;
520 case D3DVSDT_SHORT2:
521 u = *(const SHORT*) curPos;
522 curPos = curPos + sizeof(SHORT);
523 v = *(const SHORT*) curPos;
524 curPos = curPos + sizeof(SHORT);
525 /**/
526 vshader->input.V[reg].x = (float) u;
527 vshader->input.V[reg].y = (float) v;
528 vshader->input.V[reg].z = 0.0f;
529 vshader->input.V[reg].w = 1.0f;
530 break;
532 case D3DVSDT_SHORT4:
533 u = *(const SHORT*) curPos;
534 curPos = curPos + sizeof(SHORT);
535 v = *(const SHORT*) curPos;
536 curPos = curPos + sizeof(SHORT);
537 r = *(const SHORT*) curPos;
538 curPos = curPos + sizeof(SHORT);
539 t = *(const SHORT*) curPos;
540 curPos = curPos + sizeof(SHORT);
541 /**/
542 vshader->input.V[reg].x = (float) u;
543 vshader->input.V[reg].y = (float) v;
544 vshader->input.V[reg].z = (float) r;
545 vshader->input.V[reg].w = (float) t;
546 break;
548 case D3DVSDT_UBYTE4:
549 dw = *(const DWORD*) curPos;
550 curPos = curPos + sizeof(DWORD);
551 /**/
552 vshader->input.V[reg].x = (float) ((dw & 0x000000FF) >> 0);
553 vshader->input.V[reg].y = (float) ((dw & 0x0000FF00) >> 8);
554 vshader->input.V[reg].z = (float) ((dw & 0x00FF0000) >> 16);
555 vshader->input.V[reg].w = (float) ((dw & 0xFF000000) >> 24);
556 break;
558 default: /** errooooorr what to do ? */
559 ERR("Error in VertexShader declaration of %s register: unsupported type %s\n", VertexShaderDeclRegister[reg], VertexShaderDeclDataTypes[type]);
564 /* here D3DVSD_END() */
565 return D3D_OK;
568 HRESULT WINAPI IDirect3DDeviceImpl_FillVertexShaderInputArbHW(IDirect3DDevice8Impl* This,
569 IDirect3DVertexShaderImpl* vshader,
570 DWORD SkipnStrides) {
571 /** parser data */
572 const DWORD* pToken = This->UpdateStateBlock->vertexShaderDecl->pDeclaration8;
573 DWORD stream = 0;
574 DWORD token;
575 /*DWORD tokenlen;*/
576 DWORD tokentype;
577 /** for input readers */
578 const BYTE* curPos = NULL;
579 int skip = 0;
581 TRACE("(%p) - This:%p, skipstrides=%lu\n", vshader, This, SkipnStrides);
583 while (D3DVSD_END() != *pToken) {
584 token = *pToken;
585 tokentype = ((token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT);
587 /** FVF generation block */
588 if (D3DVSD_TOKEN_STREAM == tokentype && 0 == (D3DVSD_STREAMTESSMASK & token)) {
589 IDirect3DVertexBuffer8* pVB;
591 ++pToken;
592 /**
593 * how really works streams,
594 * in DolphinVS dx8 dsk sample use it !!!
596 stream = ((token & D3DVSD_STREAMNUMBERMASK) >> D3DVSD_STREAMNUMBERSHIFT);
597 skip = This->StateBlock->stream_stride[stream];
598 pVB = This->StateBlock->stream_source[stream];
600 if (NULL == pVB) {
601 ERR("using unitialised stream[%lu]\n", stream);
602 return D3DERR_INVALIDCALL;
603 } else {
604 if (This->StateBlock->streamIsUP) {
605 curPos = ((BYTE *) pVB) + (SkipnStrides * skip); /* Not really a VB */
606 } else {
607 curPos = ((IDirect3DVertexBuffer8Impl*) pVB)->allocatedMemory + (SkipnStrides * skip);
610 TRACE(" using stream[%lu] with %p (%p + (Stride %d * skip %ld))\n", stream, curPos,
611 ((IDirect3DVertexBuffer8Impl*) pVB)->allocatedMemory, skip, SkipnStrides);
613 } else if (D3DVSD_TOKEN_CONSTMEM == tokentype) {
614 /** Const decl */
615 DWORD i;
616 DWORD count = ((token & D3DVSD_CONSTCOUNTMASK) >> D3DVSD_CONSTCOUNTSHIFT);
617 DWORD constaddress = ((token & D3DVSD_CONSTADDRESSMASK) >> D3DVSD_CONSTADDRESSSHIFT);
619 ++pToken;
620 for (i = 0; i < count; ++i) {
621 FIXME("Confirm this is correct handling of consts inside the hw vertex shader\n");
622 GL_EXTCALL(glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, constaddress+i, (GLfloat *)pToken));
623 vshader->data->C[constaddress + i].x = *(const float*)pToken;
624 vshader->data->C[constaddress + i].y = *(const float*)(pToken + 1);
625 vshader->data->C[constaddress + i].z = *(const float*)(pToken + 2);
626 vshader->data->C[constaddress + i].w = *(const float*)(pToken + 3);
627 pToken += 4;
630 } else if (D3DVSD_TOKEN_STREAMDATA == tokentype && 0 != (0x10000000 & tokentype)) {
631 /** skip datas */
632 DWORD skipCount = ((token & D3DVSD_SKIPCOUNTMASK) >> D3DVSD_SKIPCOUNTSHIFT);
633 TRACE(" skipping %ld dwords\n", skipCount);
634 curPos = curPos + skipCount * sizeof(DWORD);
635 ++pToken;
637 } else if (D3DVSD_TOKEN_STREAMDATA == tokentype && 0 == (0x10000000 & tokentype)) {
638 DWORD type = ((token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
639 DWORD reg = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
640 ++pToken;
642 TRACE(" type : %ld, reg = %ld\n", type, reg);
643 switch (type) {
644 case D3DVSDT_FLOAT1:
645 TRACE("HW VS glVertexAttribPointerARB(reg=%ld,num=%d,skip=%d,ptr=%p)\n", reg, 1, skip, curPos);
646 GL_EXTCALL(glVertexAttribPointerARB(reg, 1, GL_FLOAT, GL_FALSE, skip, curPos));
647 GL_EXTCALL(glEnableVertexAttribArrayARB(reg));
648 curPos = curPos + sizeof(float);
649 break;
651 case D3DVSDT_FLOAT2:
652 TRACE("HW VS glVertexAttribPointerARB(reg=%ld,num=%d,skip=%d,ptr=%p)\n", reg, 2, skip, curPos);
653 GL_EXTCALL(glVertexAttribPointerARB(reg, 2, GL_FLOAT, GL_FALSE, skip, curPos));
654 GL_EXTCALL(glEnableVertexAttribArrayARB(reg));
655 curPos = curPos + 2*sizeof(float);
656 break;
658 case D3DVSDT_FLOAT3:
659 TRACE("HW VS glVertexAttribPointerARB(reg=%ld,num=%d,skip=%d,ptr=%p)\n", reg, 3, skip, curPos);
660 GL_EXTCALL(glVertexAttribPointerARB(reg, 3, GL_FLOAT, GL_FALSE, skip, curPos));
661 GL_EXTCALL(glEnableVertexAttribArrayARB(reg));
662 curPos = curPos + 3*sizeof(float);
663 break;
665 case D3DVSDT_FLOAT4:
666 TRACE("HW VS glVertexAttribPointerARB(reg=%ld,num=%d,skip=%d,ptr=%p)\n", reg, 4, skip, curPos);
667 GL_EXTCALL(glVertexAttribPointerARB(reg, 4, GL_FLOAT, GL_FALSE, skip, curPos));
668 GL_EXTCALL(glEnableVertexAttribArrayARB(reg));
669 curPos = curPos + 4*sizeof(float);
670 break;
672 case D3DVSDT_D3DCOLOR:
673 TRACE("HW VS glVertexAttribPointerARB(reg=%ld,num=%d,skip=%d,ptr=%p)\n", reg, 4, skip, curPos);
674 FIXME("D3DVSDT_D3DCOLOR in hw shader - To confirm\n");
675 GL_EXTCALL(glVertexAttribPointerARB(reg, 4, GL_UNSIGNED_BYTE, GL_TRUE, skip, curPos));
676 GL_EXTCALL(glEnableVertexAttribArrayARB(reg));
677 curPos = curPos + 4*sizeof(BYTE);
678 break;
680 case D3DVSDT_SHORT2:
681 TRACE("HW VS glVertexAttribPointerARB(reg=%ld,num=%d,skip=%d,ptr=%p)\n", reg, 2, skip, curPos);
682 GL_EXTCALL(glVertexAttribPointerARB(reg, 2, GL_UNSIGNED_SHORT, GL_FALSE, skip, curPos));
683 GL_EXTCALL(glEnableVertexAttribArrayARB(reg));
684 curPos = curPos + 2*sizeof(short int);
685 break;
687 case D3DVSDT_SHORT4:
688 TRACE("HW VS glVertexAttribPointerARB(reg=%ld,num=%d,skip=%d,ptr=%p)\n", reg, 1, skip, curPos);
689 GL_EXTCALL(glVertexAttribPointerARB(reg, 4, GL_UNSIGNED_SHORT, GL_FALSE, skip, curPos));
690 GL_EXTCALL(glEnableVertexAttribArrayARB(reg));
691 curPos = curPos + 4*sizeof(short int);
692 break;
694 case D3DVSDT_UBYTE4:
695 FIXME("D3DVSDT_UBYTE4 in hw shader - To confirm\n");
696 GL_EXTCALL(glVertexAttribPointerARB(reg, 4, GL_UNSIGNED_BYTE, GL_TRUE, skip, curPos));
697 GL_EXTCALL(glEnableVertexAttribArrayARB(reg));
698 curPos = curPos + 4*sizeof(BYTE);
699 break;
701 default: /** errooooorr what to do ? */
702 ERR("Error in VertexShader declaration of %s register: unsupported type %s\n", VertexShaderDeclRegister[reg], VertexShaderDeclDataTypes[type]);
707 /* here D3DVSD_END() */
708 return D3D_OK;
711 HRESULT WINAPI IDirect3DVertexShaderDeclarationImpl_GetDeclaration8(IDirect3DVertexShaderDeclarationImpl* This, DWORD* pData, UINT* pSizeOfData) {
712 if (NULL == pData) {
713 *pSizeOfData = This->declaration8Length;
714 return D3D_OK;
716 if (*pSizeOfData < This->declaration8Length) {
717 *pSizeOfData = This->declaration8Length;
718 return D3DERR_MOREDATA;
720 TRACE("(%p) : GetVertexShaderDeclaration copying to %p\n", This, pData);
721 memcpy(pData, This->pDeclaration8, This->declaration8Length);
722 return D3D_OK;