2 * vertex shaders declaration implementation
4 * Copyright 2002 Raphael Junqueira
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "wine/debug.h"
32 #include "d3d8_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
37 * DirectX9 SDK download
38 * http://msdn.microsoft.com/library/default.asp?url=/downloads/list/directx.asp
41 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndrive/html/directx07162002.asp
43 * Using Vertex Shaders
44 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndrive/html/directx02192001.asp
47 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/whatsnew.asp
50 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader2_0/VertexShader2_0.asp
51 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader2_0/Instructions/Instructions.asp
52 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/GettingStarted/VertexDeclaration/VertexDeclaration.asp
53 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader3_0/VertexShader3_0.asp
56 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/advancedtopics/VertexPipe/matrixstack/matrixstack.asp
59 * http://msdn.microsoft.com/library/en-us/directx9_c/directx/graphics/programmingguide/GettingStarted/VertexFormats/vformats.asp
61 * NVIDIA: DX8 Vertex Shader to NV Vertex Program
62 * http://developer.nvidia.com/view.asp?IO=vstovp
64 * NVIDIA: Memory Management with VAR
65 * http://developer.nvidia.com/view.asp?IO=var_memory_management
68 /** Vertex Shader Declaration data types tokens */
69 #define MAX_VSHADER_DECL_TYPES 8
70 static CONST char* VertexShaderDeclDataTypes[] = {
82 static CONST char* VertexShaderDeclRegister[] = {
84 "D3DVSDE_BLENDWEIGHT",
85 "D3DVSDE_BLENDINDICES",
103 /** todo check decl validity */
104 /*inline static*/ DWORD Direct3DVextexShaderDeclarationImpl_ParseToken(const DWORD* pToken) {
105 const DWORD token = *pToken;
108 switch ((token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT) { /* maybe a macro to inverse ... */
109 case D3DVSD_TOKEN_NOP:
110 TRACE(" 0x%08lx NOP()\n", token);
112 case D3DVSD_TOKEN_STREAM:
113 if (token & D3DVSD_STREAMTESSMASK) {
114 TRACE(" 0x%08lx STREAM_TESS()\n", token);
116 TRACE(" 0x%08lx STREAM(%lu)\n", token, ((token & D3DVSD_STREAMNUMBERMASK) >> D3DVSD_STREAMNUMBERSHIFT));
119 case D3DVSD_TOKEN_STREAMDATA:
120 if (token & 0x10000000) {
121 TRACE(" 0x%08lx SKIP(%lu)\n", token, ((token & D3DVSD_SKIPCOUNTMASK) >> D3DVSD_SKIPCOUNTSHIFT));
123 DWORD type = ((token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
124 DWORD reg = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
125 TRACE(" 0x%08lx REG(%s, %s)\n", token, VertexShaderDeclRegister[reg], VertexShaderDeclDataTypes[type]);
128 case D3DVSD_TOKEN_TESSELLATOR:
129 if (token & 0x10000000) {
130 DWORD type = ((token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
131 DWORD reg = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
132 TRACE(" 0x%08lx TESSUV(%s) as %s\n", token, VertexShaderDeclRegister[reg], VertexShaderDeclDataTypes[type]);
134 DWORD type = ((token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
135 DWORD regout = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
136 DWORD regin = ((token & D3DVSD_VERTEXREGINMASK) >> D3DVSD_VERTEXREGINSHIFT);
137 TRACE(" 0x%08lx TESSNORMAL(%s, %s) as %s\n", token, VertexShaderDeclRegister[regin], VertexShaderDeclRegister[regout], VertexShaderDeclDataTypes[type]);
140 case D3DVSD_TOKEN_CONSTMEM:
143 DWORD count = ((token & D3DVSD_CONSTCOUNTMASK) >> D3DVSD_CONSTCOUNTSHIFT);
144 DWORD constaddress = ((token & D3DVSD_CONSTADDRESSMASK) >> D3DVSD_CONSTADDRESSSHIFT);
145 TRACE(" 0x%08lx CONST(%lu, %lu)\n", token, constaddress, count);
147 for (i = 0; i < count; ++i) {
149 TRACE(" c[%lu] = (0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx)\n",
156 TRACE(" c[%lu] = (%8f, %8f, %8f, %8f)\n",
159 *(float*) (pToken + 1),
160 *(float*) (pToken + 2),
161 *(float*) (pToken + 3));
165 tokenlen = (4 * count) + 1;
168 case D3DVSD_TOKEN_EXT:
170 DWORD count = ((token & D3DVSD_CONSTCOUNTMASK) >> D3DVSD_CONSTCOUNTSHIFT);
171 DWORD extinfo = ((token & D3DVSD_EXTINFOMASK) >> D3DVSD_EXTINFOSHIFT);
172 TRACE(" 0x%08lx EXT(%lu, %lu)\n", token, count, extinfo);
173 /* todo ... print extension */
174 tokenlen = count + 1;
177 case D3DVSD_TOKEN_END:
178 TRACE(" 0x%08lx END()\n", token);
181 TRACE(" 0x%08lx UNKNOWN\n", token);
187 HRESULT WINAPI IDirect3DDeviceImpl_CreateVertexShaderDeclaration8(IDirect3DDevice8Impl* This, CONST DWORD* pDeclaration8, IDirect3DVertexShaderDeclarationImpl** ppVertexShaderDecl) {
189 const DWORD* pToken = pDeclaration8;
196 DWORD tex = D3DFVF_TEX0;
197 /** TRUE if declaration can be matched by a fvf */
198 IDirect3DVertexShaderDeclarationImpl* object;
199 BOOL invalid_fvf = FALSE;
201 TRACE("(%p) : pDeclaration8(%p)\n", This, pDeclaration8);
203 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DVertexShaderDeclarationImpl));
204 /*object->lpVtbl = &Direct3DVextexShaderDeclaration8_Vtbl;*/
205 object->device = This; /* FIXME: AddRef(This) */
209 while (D3DVSD_END() != *pToken) {
211 tokenlen = Direct3DVextexShaderDeclarationImpl_ParseToken(pToken);
212 tokentype = ((token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT);
214 /** FVF generation block */
215 if (D3DVSD_TOKEN_STREAM == tokentype && 0 == (D3DVSD_STREAMTESSMASK & token)) {
217 * how really works streams,
218 * in DolphinVS dx8 dsk sample they seems to decal reg numbers !!!
220 DWORD oldStream = stream;
221 stream = ((token & D3DVSD_STREAMNUMBERMASK) >> D3DVSD_STREAMNUMBERSHIFT);
223 /* copy fvf if valid */
224 if (FALSE == invalid_fvf) {
225 fvf |= tex << D3DFVF_TEXCOUNT_SHIFT;
227 object->fvf[oldStream] = fvf;
228 object->allFVF |= fvf;
230 object->fvf[oldStream] = 0;
234 /* reset valid/invalid fvf */
238 } else if (D3DVSD_TOKEN_STREAMDATA == tokentype && 0 == (0x10000000 & tokentype)) {
239 DWORD type = ((token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
240 DWORD reg = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
243 case D3DVSDE_POSITION:
245 case D3DVSDT_FLOAT3: fvf |= D3DFVF_XYZ; break;
246 case D3DVSDT_FLOAT4: fvf |= D3DFVF_XYZRHW; break;
248 /** Mismatched use of a register, invalid for fixed function fvf computing (ok for VS) */
250 if (type >= MAX_VSHADER_DECL_TYPES) {
251 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_POSITION register: unsupported and unrecognized type %08lx\n", type);
253 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_POSITION register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
258 case D3DVSDE_BLENDWEIGHT:
260 case D3DVSDT_FLOAT1: fvf |= D3DFVF_XYZB1; break;
261 case D3DVSDT_FLOAT2: fvf |= D3DFVF_XYZB2; break;
262 case D3DVSDT_FLOAT3: fvf |= D3DFVF_XYZB3; break;
263 case D3DVSDT_FLOAT4: fvf |= D3DFVF_XYZB4; break;
265 /** Mismatched use of a register, invalid for fixed function fvf computing (ok for VS) */
267 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_BLENDWEIGHT register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
271 case D3DVSDE_BLENDINDICES: /* seem to be B5 as said in MSDN Dx9SDK ?? */
273 case D3DVSDT_UBYTE4: fvf |= D3DFVF_LASTBETA_UBYTE4; break;
275 /** Mismatched use of a register, invalid for fixed function fvf computing (ok for VS) */
277 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_BLENDINDINCES register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
281 case D3DVSDE_NORMAL: /* TODO: only FLOAT3 supported ... another choice possible ? */
283 case D3DVSDT_FLOAT3: fvf |= D3DFVF_NORMAL; break;
285 /** Mismatched use of a register, invalid for fixed function fvf computing (ok for VS) */
287 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_NORMAL register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
291 case D3DVSDE_PSIZE: /* TODO: only FLOAT1 supported ... another choice possible ? */
293 case D3DVSDT_FLOAT1: fvf |= D3DFVF_PSIZE; break;
295 /** Mismatched use of a register, invalid for fixed function fvf computing (ok for VS) */
297 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_PSIZE register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
301 case D3DVSDE_DIFFUSE: /* TODO: only D3DCOLOR supported */
303 case D3DVSDT_D3DCOLOR: fvf |= D3DFVF_DIFFUSE; break;
305 /** Mismatched use of a register, invalid for fixed function fvf computing (ok for VS) */
307 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_DIFFUSE register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
311 case D3DVSDE_SPECULAR: /* TODO: only D3DCOLOR supported */
313 case D3DVSDT_D3DCOLOR: fvf |= D3DFVF_SPECULAR; break;
315 /** Mismatched use of a register, invalid for fixed function fvf computing (ok for VS) */
317 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_SPECULAR register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
321 case D3DVSDE_TEXCOORD0:
322 case D3DVSDE_TEXCOORD1:
323 case D3DVSDE_TEXCOORD2:
324 case D3DVSDE_TEXCOORD3:
325 case D3DVSDE_TEXCOORD4:
326 case D3DVSDE_TEXCOORD5:
327 case D3DVSDE_TEXCOORD6:
328 case D3DVSDE_TEXCOORD7:
329 /* Fixme? - assume all tex coords in same stream */
331 int texNo = 1 + (reg - D3DVSDE_TEXCOORD0);
332 tex = max(tex, texNo);
334 case D3DVSDT_FLOAT1: fvf |= D3DFVF_TEXCOORDSIZE1(texNo); break;
335 case D3DVSDT_FLOAT2: fvf |= D3DFVF_TEXCOORDSIZE2(texNo); break;
336 case D3DVSDT_FLOAT3: fvf |= D3DFVF_TEXCOORDSIZE3(texNo); break;
337 case D3DVSDT_FLOAT4: fvf |= D3DFVF_TEXCOORDSIZE4(texNo); break;
339 /** Mismatched use of a register, invalid for fixed function fvf computing (ok for VS) */
341 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_TEXCOORD? register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
346 case D3DVSDE_POSITION2: /* maybe D3DFVF_XYZRHW instead D3DFVF_XYZ (of D3DVDE_POSITION) ... to see */
347 case D3DVSDE_NORMAL2: /* FIXME i don't know what to do here ;( */
348 FIXME("[%lu] registers in VertexShader declaration not supported yet (token:0x%08lx)\n", reg, token);
351 TRACE("VertexShader declaration define %lx as current FVF\n", fvf);
356 /* here D3DVSD_END() */
357 len += Direct3DVextexShaderDeclarationImpl_ParseToken(pToken);
359 /* copy fvf if valid */
360 if (FALSE == invalid_fvf) {
361 fvf |= tex << D3DFVF_TEXCOUNT_SHIFT;
362 object->fvf[stream] = fvf;
363 object->allFVF |= fvf;
365 object->fvf[stream] = 0;
367 TRACE("Completed, allFVF = %lx\n", object->allFVF);
370 object->declaration8Length = len * sizeof(DWORD);
371 /* copy the declaration */
372 object->pDeclaration8 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, object->declaration8Length);
373 memcpy(object->pDeclaration8, pDeclaration8, object->declaration8Length);
375 *ppVertexShaderDecl = object;
380 HRESULT WINAPI IDirect3DDeviceImpl_FillVertexShaderInput(IDirect3DDevice8Impl* This,
381 IDirect3DVertexShaderImpl* vshader,
382 DWORD SkipnStrides) {
384 const DWORD* pToken = This->UpdateStateBlock->vertexShaderDecl->pDeclaration8;
389 /** for input readers */
390 const char* curPos = NULL;
395 TRACE("(%p) - This:%p, skipstrides=%lu\n", vshader, This, SkipnStrides);
397 while (D3DVSD_END() != *pToken) {
399 tokentype = ((token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT);
401 /** FVF generation block */
402 if (D3DVSD_TOKEN_STREAM == tokentype && 0 == (D3DVSD_STREAMTESSMASK & token)) {
403 IDirect3DVertexBuffer8* pVB;
408 * how really works streams,
409 * in DolphinVS dx8 dsk sample use it !!!
411 stream = ((token & D3DVSD_STREAMNUMBERMASK) >> D3DVSD_STREAMNUMBERSHIFT);
412 skip = This->StateBlock->stream_stride[stream];
413 pVB = This->StateBlock->stream_source[stream];
416 ERR("using unitialised stream[%lu]\n", stream);
417 return D3DERR_INVALIDCALL;
419 if (This->StateBlock->streamIsUP == TRUE) {
420 curPos = ((char *) pVB) + (SkipnStrides * skip); /* Not really a VB */
422 curPos = ((IDirect3DVertexBuffer8Impl*) pVB)->allocatedMemory + (SkipnStrides * skip);
425 TRACE(" using stream[%lu] with %p (%p + (Stride %d * skip %ld))\n", stream, curPos,
426 ((IDirect3DVertexBuffer8Impl*) pVB)->allocatedMemory, skip, SkipnStrides);
428 } else if (D3DVSD_TOKEN_CONSTMEM == tokentype) {
431 DWORD count = ((token & D3DVSD_CONSTCOUNTMASK) >> D3DVSD_CONSTCOUNTSHIFT);
432 DWORD constaddress = ((token & D3DVSD_CONSTADDRESSMASK) >> D3DVSD_CONSTADDRESSSHIFT);
434 for (i = 0; i < count; ++i) {
435 vshader->data->C[constaddress + i].x = *(float*)pToken;
436 vshader->data->C[constaddress + i].y = *(float*)(pToken + 1);
437 vshader->data->C[constaddress + i].z = *(float*)(pToken + 2);
438 vshader->data->C[constaddress + i].w = *(float*)(pToken + 3);
442 } else if (D3DVSD_TOKEN_STREAMDATA == tokentype && 0 != (0x10000000 & tokentype)) {
444 DWORD skipCount = ((token & D3DVSD_SKIPCOUNTMASK) >> D3DVSD_SKIPCOUNTSHIFT);
445 TRACE(" skipping %ld dwords\n", skipCount);
446 curPos = curPos + skipCount * sizeof(DWORD);
449 } else if (D3DVSD_TOKEN_STREAMDATA == tokentype && 0 == (0x10000000 & tokentype)) {
450 DWORD type = ((token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
451 DWORD reg = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
454 TRACE(" type : %ld, reg = %ld\n", type, reg);
457 x = *(float*) curPos;
458 curPos = curPos + sizeof(float);
460 vshader->input.V[reg].x = x;
461 vshader->input.V[reg].y = 0.0f;
462 vshader->input.V[reg].z = 0.0f;
463 vshader->input.V[reg].w = 1.0f;
467 x = *(float*) curPos;
468 curPos = curPos + sizeof(float);
469 y = *(float*) curPos;
470 curPos = curPos + sizeof(float);
472 vshader->input.V[reg].x = x;
473 vshader->input.V[reg].y = y;
474 vshader->input.V[reg].z = 0.0f;
475 vshader->input.V[reg].w = 1.0f;
479 x = *(float*) curPos;
480 curPos = curPos + sizeof(float);
481 y = *(float*) curPos;
482 curPos = curPos + sizeof(float);
483 z = *(float*) curPos;
484 curPos = curPos + sizeof(float);
486 vshader->input.V[reg].x = x;
487 vshader->input.V[reg].y = y;
488 vshader->input.V[reg].z = z;
489 vshader->input.V[reg].w = 1.0f;
493 x = *(float*) curPos;
494 curPos = curPos + sizeof(float);
495 y = *(float*) curPos;
496 curPos = curPos + sizeof(float);
497 z = *(float*) curPos;
498 curPos = curPos + sizeof(float);
499 w = *(float*) curPos;
500 curPos = curPos + sizeof(float);
502 vshader->input.V[reg].x = x;
503 vshader->input.V[reg].y = y;
504 vshader->input.V[reg].z = z;
505 vshader->input.V[reg].w = w;
508 case D3DVSDT_D3DCOLOR:
509 dw = *(DWORD*) curPos;
510 curPos = curPos + sizeof(DWORD);
512 vshader->input.V[reg].x = (float) (((dw >> 16) & 0xFF) / 255.0f);
513 vshader->input.V[reg].y = (float) (((dw >> 8) & 0xFF) / 255.0f);
514 vshader->input.V[reg].z = (float) (((dw >> 0) & 0xFF) / 255.0f);
515 vshader->input.V[reg].w = (float) (((dw >> 24) & 0xFF) / 255.0f);
519 u = *(SHORT*) curPos;
520 curPos = curPos + sizeof(SHORT);
521 v = *(SHORT*) curPos;
522 curPos = curPos + sizeof(SHORT);
524 vshader->input.V[reg].x = (float) u;
525 vshader->input.V[reg].y = (float) v;
526 vshader->input.V[reg].z = 0.0f;
527 vshader->input.V[reg].w = 1.0f;
531 u = *(SHORT*) curPos;
532 curPos = curPos + sizeof(SHORT);
533 v = *(SHORT*) curPos;
534 curPos = curPos + sizeof(SHORT);
535 r = *(SHORT*) curPos;
536 curPos = curPos + sizeof(SHORT);
537 t = *(SHORT*) curPos;
538 curPos = curPos + sizeof(SHORT);
540 vshader->input.V[reg].x = (float) u;
541 vshader->input.V[reg].y = (float) v;
542 vshader->input.V[reg].z = (float) r;
543 vshader->input.V[reg].w = (float) t;
547 dw = *(DWORD*) curPos;
548 curPos = curPos + sizeof(DWORD);
550 vshader->input.V[reg].x = (float) ((dw & 0x000F) >> 0);
551 vshader->input.V[reg].y = (float) ((dw & 0x00F0) >> 8);
552 vshader->input.V[reg].z = (float) ((dw & 0x0F00) >> 16);
553 vshader->input.V[reg].w = (float) ((dw & 0xF000) >> 24);
557 default: /** errooooorr what to do ? */
558 ERR("Error in VertexShader declaration of %s register: unsupported type %s\n", VertexShaderDeclRegister[reg], VertexShaderDeclDataTypes[type]);
563 /* here D3DVSD_END() */
567 HRESULT WINAPI IDirect3DVertexShaderDeclarationImpl_GetDeclaration8(IDirect3DVertexShaderDeclarationImpl* This, DWORD* pData, UINT* pSizeOfData) {
569 *pSizeOfData = This->declaration8Length;
572 if (*pSizeOfData < This->declaration8Length) {
573 *pSizeOfData = This->declaration8Length;
574 return D3DERR_MOREDATA;
576 TRACE("(%p) : GetVertexShaderDeclaration copying to %p\n", This, pData);
577 memcpy(pData, This->pDeclaration8, This->declaration8Length);