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
25 #include "wine/debug.h"
29 #include "d3d8_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
34 * DirectX9 SDK download
35 * http://msdn.microsoft.com/library/default.asp?url=/downloads/list/directx.asp
38 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndrive/html/directx07162002.asp
40 * Using Vertex Shaders
41 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndrive/html/directx02192001.asp
44 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/whatsnew.asp
47 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader2_0/VertexShader2_0.asp
48 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader2_0/Instructions/Instructions.asp
49 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/GettingStarted/VertexDeclaration/VertexDeclaration.asp
50 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader3_0/VertexShader3_0.asp
53 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/advancedtopics/VertexPipe/matrixstack/matrixstack.asp
56 * http://msdn.microsoft.com/library/en-us/directx9_c/directx/graphics/programmingguide/GettingStarted/VertexFormats/vformats.asp
58 * NVIDIA: DX8 Vertex Shader to NV Vertex Program
59 * http://developer.nvidia.com/view.asp?IO=vstovp
61 * NVIDIA: Memory Management with VAR
62 * http://developer.nvidia.com/view.asp?IO=var_memory_management
65 /** Vertex Shader Declaration data types tokens */
66 static CONST
char* VertexShaderDeclDataTypes
[] = {
78 static CONST
char* VertexShaderDeclRegister
[] = {
80 "D3DVSDE_BLENDWEIGHT",
81 "D3DVSDE_BLENDINDICES",
99 /** todo check decl validity */
100 inline static DWORD
Direct3DVextexShaderDeclarationImpl_ParseToken(const DWORD
* pToken
) {
101 const DWORD token
= *pToken
;
104 switch ((token
& D3DVSD_TOKENTYPEMASK
) >> D3DVSD_TOKENTYPESHIFT
) { /* maybe a macro to inverse ... */
105 case D3DVSD_TOKEN_NOP
:
106 TRACE(" 0x%08lx NOP()\n", token
);
108 case D3DVSD_TOKEN_STREAM
:
109 if (token
& D3DVSD_STREAMTESSMASK
) {
110 TRACE(" 0x%08lx STREAM_TESS()\n", token
);
112 TRACE(" 0x%08lx STREAM(%lu)\n", token
, ((token
& D3DVSD_STREAMNUMBERMASK
) >> D3DVSD_STREAMNUMBERSHIFT
));
115 case D3DVSD_TOKEN_STREAMDATA
:
116 if (token
& 0x10000000) {
117 TRACE(" 0x%08lx SKIP(%lu)\n", token
, ((token
& D3DVSD_SKIPCOUNTMASK
) >> D3DVSD_SKIPCOUNTSHIFT
));
119 DWORD type
= ((token
& D3DVSD_DATATYPEMASK
) >> D3DVSD_DATATYPESHIFT
);
120 DWORD reg
= ((token
& D3DVSD_VERTEXREGMASK
) >> D3DVSD_VERTEXREGSHIFT
);
121 TRACE(" 0x%08lx REG(%s, %s)\n", token
, VertexShaderDeclRegister
[reg
], VertexShaderDeclDataTypes
[type
]);
124 case D3DVSD_TOKEN_TESSELLATOR
:
125 if (token
& 0x10000000) {
126 DWORD type
= ((token
& D3DVSD_DATATYPEMASK
) >> D3DVSD_DATATYPESHIFT
);
127 DWORD reg
= ((token
& D3DVSD_VERTEXREGMASK
) >> D3DVSD_VERTEXREGSHIFT
);
128 TRACE(" 0x%08lx TESSUV(%s) as %s\n", token
, VertexShaderDeclRegister
[reg
], VertexShaderDeclDataTypes
[type
]);
130 DWORD type
= ((token
& D3DVSD_DATATYPEMASK
) >> D3DVSD_DATATYPESHIFT
);
131 DWORD regout
= ((token
& D3DVSD_VERTEXREGMASK
) >> D3DVSD_VERTEXREGSHIFT
);
132 DWORD regin
= ((token
& D3DVSD_VERTEXREGINMASK
) >> D3DVSD_VERTEXREGINSHIFT
);
133 TRACE(" 0x%08lx TESSNORMAL(%s, %s) as %s\n", token
, VertexShaderDeclRegister
[regin
], VertexShaderDeclRegister
[regout
], VertexShaderDeclDataTypes
[type
]);
136 case D3DVSD_TOKEN_CONSTMEM
:
139 DWORD count
= ((token
& D3DVSD_CONSTCOUNTMASK
) >> D3DVSD_CONSTCOUNTSHIFT
);
140 DWORD constaddress
= ((token
& D3DVSD_CONSTADDRESSMASK
) >> D3DVSD_CONSTADDRESSSHIFT
);
141 TRACE(" 0x%08lx CONST(%lu, %lu)\n", token
, constaddress
, count
);
143 for (i
= 0; i
< count
; ++i
) {
144 TRACE(" c[%lu] = (0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx)\n",
153 tokenlen
= count
+ 1;
156 case D3DVSD_TOKEN_EXT
:
158 DWORD count
= ((token
& D3DVSD_CONSTCOUNTMASK
) >> D3DVSD_CONSTCOUNTSHIFT
);
159 DWORD extinfo
= ((token
& D3DVSD_EXTINFOMASK
) >> D3DVSD_EXTINFOSHIFT
);
160 TRACE(" 0x%08lx EXT(%lu, %lu)\n", token
, count
, extinfo
);
161 /* todo ... print extension */
162 tokenlen
= count
+ 1;
165 case D3DVSD_TOKEN_END
:
166 TRACE(" 0x%08lx END()\n", token
);
169 TRACE(" 0x%08lx UNKNOWN\n", token
);
175 HRESULT WINAPI
IDirect3DDeviceImpl_CreateVertexShaderDeclaration8(IDirect3DDevice8Impl
* This
, CONST DWORD
* pDeclaration8
, IDirect3DVertexShaderDeclarationImpl
** ppVertexShaderDecl
) {
177 const DWORD
* pToken
= pDeclaration8
;
184 DWORD tex
= D3DFVF_TEX0
;
185 /** TRUE if declaration can be matched by a fvf */
186 IDirect3DVertexShaderDeclarationImpl
* object
;
187 BOOL invalid_fvf
= FALSE
;
189 TRACE("(%p) : pDeclaration8(%p)\n", This
, pDeclaration8
);
191 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IDirect3DVertexShaderDeclarationImpl
));
192 /*object->lpVtbl = &Direct3DVextexShaderDeclaration8_Vtbl;*/
193 object
->device
= This
; /* FIXME: AddRef(This) */
196 while (D3DVSD_END() != *pToken
) {
198 tokenlen
= Direct3DVextexShaderDeclarationImpl_ParseToken(pToken
);
199 tokentype
= ((token
& D3DVSD_TOKENTYPEMASK
) >> D3DVSD_TOKENTYPESHIFT
);
201 /** FVF generation block */
202 if (D3DVSD_TOKEN_STREAM
== tokentype
&& 0 == (D3DVSD_STREAMTESSMASK
& token
)) {
204 * how really works streams,
205 * in DolphinVS dx8 dsk sample they seems to decal reg numbers !!!
207 stream
= ((token
& D3DVSD_STREAMNUMBERMASK
) >> D3DVSD_STREAMNUMBERSHIFT
);
210 /** fvf cannot map mutliple streams, so invalid fvf computing */
214 } else if (D3DVSD_TOKEN_STREAMDATA
== tokentype
&& 0 == (0x10000000 & tokentype
)) {
215 DWORD type
= ((token
& D3DVSD_DATATYPEMASK
) >> D3DVSD_DATATYPESHIFT
);
216 DWORD reg
= ((token
& D3DVSD_VERTEXREGMASK
) >> D3DVSD_VERTEXREGSHIFT
) - stream
;
219 case D3DVSDE_POSITION
:
221 case D3DVSDT_FLOAT3
: fvf
|= D3DFVF_XYZ
; break;
222 case D3DVSDT_FLOAT4
: fvf
|= D3DFVF_XYZRHW
; break;
224 /** errooooorr mismatched use of a register, invalid fvf computing */
226 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_POSITION register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
230 case D3DVSDE_BLENDWEIGHT
:
232 case D3DVSDT_FLOAT1
: fvf
|= D3DFVF_XYZB1
; break;
233 case D3DVSDT_FLOAT2
: fvf
|= D3DFVF_XYZB2
; break;
234 case D3DVSDT_FLOAT3
: fvf
|= D3DFVF_XYZB3
; break;
235 case D3DVSDT_FLOAT4
: fvf
|= D3DFVF_XYZB4
; break;
237 /** errooooorr mismatched use of a register, invalid fvf computing */
239 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_BLENDWEIGHT register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
243 case D3DVSDE_BLENDINDICES
: /* seem to be B5 as said in MSDN Dx9SDK ?? */
245 case D3DVSDT_UBYTE4
: fvf
|= D3DFVF_LASTBETA_UBYTE4
; break;
247 /** errooooorr mismatched use of a register, invalid fvf computing */
249 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_BLENDINDINCES register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
253 case D3DVSDE_NORMAL
: /* TODO: only FLOAT3 supported ... another choice possible ? */
255 case D3DVSDT_FLOAT3
: fvf
|= D3DFVF_NORMAL
; break;
257 /** errooooorr mismatched use of a register, invalid fvf computing */
259 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_NORMAL register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
263 case D3DVSDE_PSIZE
: /* TODO: only FLOAT1 supported ... another choice possible ? */
265 case D3DVSDT_FLOAT1
: fvf
|= D3DFVF_PSIZE
; break;
267 /** errooooorr mismatched use of a register, invalid fvf computing */
269 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_PSIZE register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
273 case D3DVSDE_DIFFUSE
: /* TODO: only D3DCOLOR supported */
275 case D3DVSDT_D3DCOLOR
: fvf
|= D3DFVF_DIFFUSE
; break;
277 /** errooooorr mismatched use of a register, invalid fvf computing */
279 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_DIFFUSE register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
283 case D3DVSDE_SPECULAR
: /* TODO: only D3DCOLOR supported */
285 case D3DVSDT_D3DCOLOR
: fvf
|= D3DFVF_SPECULAR
; break;
287 /** errooooorr mismatched use of a register, invalid fvf computing */
289 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_SPECULAR register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
294 * TODO: for TEX* only FLOAT2 supported
295 * by default using texture type info
297 case D3DVSDE_TEXCOORD0
: tex
= max(tex
, D3DFVF_TEX1
); break;
298 case D3DVSDE_TEXCOORD1
: tex
= max(tex
, D3DFVF_TEX2
); break;
299 case D3DVSDE_TEXCOORD2
: tex
= max(tex
, D3DFVF_TEX3
); break;
300 case D3DVSDE_TEXCOORD3
: tex
= max(tex
, D3DFVF_TEX4
); break;
301 case D3DVSDE_TEXCOORD4
: tex
= max(tex
, D3DFVF_TEX5
); break;
302 case D3DVSDE_TEXCOORD5
: tex
= max(tex
, D3DFVF_TEX6
); break;
303 case D3DVSDE_TEXCOORD6
: tex
= max(tex
, D3DFVF_TEX7
); break;
304 case D3DVSDE_TEXCOORD7
: tex
= max(tex
, D3DFVF_TEX8
); break;
306 case D3DVSDE_POSITION2
: /* maybe D3DFVF_XYZRHW instead D3DFVF_XYZ (of D3DVDE_POSITION) ... to see */
307 case D3DVSDE_NORMAL2
: /* FIXME i don't know what to do here ;( */
308 FIXME("[%lu] registers in VertexShader declaration not supported yet (token:0x%08lx)\n", reg
, token
);
311 /*TRACE("VertexShader declaration define %x as current FVF\n", fvf);*/
317 /*TRACE("VertexShader declaration define %x as texture level\n", tex);*/
320 /* here D3DVSD_END() */
321 len
+= Direct3DVextexShaderDeclarationImpl_ParseToken(pToken
);
322 /* copy fvf if valid */
323 if (FALSE
== invalid_fvf
)
328 object
->declaration8Length
= len
* sizeof(DWORD
);
329 /* copy the declaration */
330 object
->pDeclaration8
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, object
->declaration8Length
);
331 memcpy(object
->pDeclaration8
, pDeclaration8
, object
->declaration8Length
);
333 *ppVertexShaderDecl
= object
;
338 HRESULT WINAPI
IDirect3DDeviceImpl_FillVertexShaderInput(IDirect3DDevice8Impl
* This
,
339 IDirect3DVertexShaderImpl
* vshader
,
340 const void* vertexFirstStream
,
341 DWORD StartVertexIndex
,
344 const DWORD
* pToken
= This
->UpdateStateBlock
->vertexShaderDecl
->pDeclaration8
;
349 /** for input readers */
350 const char* curPos
= NULL
;
355 /*TRACE("(%p) - This:%p - stream:%p, startIdx=%lu, idxDecal=%lu\n", vshader, This, vertexFirstStream, StartVertexIndex, idxDecal);*/
356 while (D3DVSD_END() != *pToken
) {
358 tokentype
= ((token
& D3DVSD_TOKENTYPEMASK
) >> D3DVSD_TOKENTYPESHIFT
);
360 /** FVF generation block */
361 if (D3DVSD_TOKEN_STREAM
== tokentype
&& 0 == (D3DVSD_STREAMTESSMASK
& token
)) {
362 IDirect3DVertexBuffer8
* pVB
;
363 const char* startVtx
= NULL
;
368 * how really works streams,
369 * in DolphinVS dx8 dsk sample use it !!!
371 stream
= ((token
& D3DVSD_STREAMNUMBERMASK
) >> D3DVSD_STREAMNUMBERSHIFT
);
374 skip
= This
->StateBlock
->stream_stride
[0];
375 startVtx
= (const char*) vertexFirstStream
+ (StartVertexIndex
* skip
);
376 curPos
= startVtx
+ idxDecal
;
377 /*TRACE(" using stream[%lu] with %lu decal => curPos %p\n", stream, idxDecal, curPos);*/
379 skip
= This
->StateBlock
->stream_stride
[stream
];
380 pVB
= This
->StateBlock
->stream_source
[stream
];
383 ERR("using unitialised stream[%lu]\n", stream
);
384 return D3DERR_INVALIDCALL
;
386 startVtx
= ((IDirect3DVertexBuffer8Impl
*) pVB
)->allocatedMemory
+ (StartVertexIndex
* skip
);
387 /** do we need to decal if we use idxBuffer */
388 curPos
= startVtx
+ idxDecal
;
389 /*TRACE(" using stream[%lu] with %lu decal\n", stream, idxDecal);*/
392 } else if (D3DVSD_TOKEN_CONSTMEM
== tokentype
) {
395 DWORD count
= ((token
& D3DVSD_CONSTCOUNTMASK
) >> D3DVSD_CONSTCOUNTSHIFT
);
396 DWORD constaddress
= ((token
& D3DVSD_CONSTADDRESSMASK
) >> D3DVSD_CONSTADDRESSSHIFT
);
398 for (i
= 0; i
< count
; ++i
) {
399 vshader
->data
->C
[constaddress
+ i
].x
= *(float*)pToken
;
400 vshader
->data
->C
[constaddress
+ i
].y
= *(float*)(pToken
+ 1);
401 vshader
->data
->C
[constaddress
+ i
].z
= *(float*)(pToken
+ 2);
402 vshader
->data
->C
[constaddress
+ i
].w
= *(float*)(pToken
+ 3);
406 } else if (D3DVSD_TOKEN_STREAMDATA
== tokentype
&& 0 != (0x10000000 & tokentype
)) {
408 DWORD skipCount
= ((token
& D3DVSD_SKIPCOUNTMASK
) >> D3DVSD_SKIPCOUNTSHIFT
);
409 curPos
= curPos
+ skipCount
* sizeof(DWORD
);
412 } else if (D3DVSD_TOKEN_STREAMDATA
== tokentype
&& 0 == (0x10000000 & tokentype
)) {
413 DWORD type
= ((token
& D3DVSD_DATATYPEMASK
) >> D3DVSD_DATATYPESHIFT
);
414 DWORD reg
= ((token
& D3DVSD_VERTEXREGMASK
) >> D3DVSD_VERTEXREGSHIFT
);
419 x
= *(float*) curPos
;
420 curPos
= curPos
+ sizeof(float);
422 vshader
->input
.V
[reg
].x
= x
;
423 vshader
->input
.V
[reg
].y
= 0.0f
;
424 vshader
->input
.V
[reg
].z
= 0.0f
;
425 vshader
->input
.V
[reg
].w
= 1.0f
;
429 x
= *(float*) curPos
;
430 curPos
= curPos
+ sizeof(float);
431 y
= *(float*) curPos
;
432 curPos
= curPos
+ sizeof(float);
434 vshader
->input
.V
[reg
].x
= x
;
435 vshader
->input
.V
[reg
].y
= y
;
436 vshader
->input
.V
[reg
].z
= 0.0f
;
437 vshader
->input
.V
[reg
].w
= 1.0f
;
441 x
= *(float*) curPos
;
442 curPos
= curPos
+ sizeof(float);
443 y
= *(float*) curPos
;
444 curPos
= curPos
+ sizeof(float);
445 z
= *(float*) curPos
;
446 curPos
= curPos
+ sizeof(float);
448 vshader
->input
.V
[reg
].x
= x
;
449 vshader
->input
.V
[reg
].y
= y
;
450 vshader
->input
.V
[reg
].z
= z
;
451 vshader
->input
.V
[reg
].w
= 1.0f
;
455 x
= *(float*) curPos
;
456 curPos
= curPos
+ sizeof(float);
457 y
= *(float*) curPos
;
458 curPos
= curPos
+ sizeof(float);
459 z
= *(float*) curPos
;
460 curPos
= curPos
+ sizeof(float);
461 w
= *(float*) curPos
;
462 curPos
= curPos
+ sizeof(float);
464 vshader
->input
.V
[reg
].x
= x
;
465 vshader
->input
.V
[reg
].y
= y
;
466 vshader
->input
.V
[reg
].z
= z
;
467 vshader
->input
.V
[reg
].w
= w
;
470 case D3DVSDT_D3DCOLOR
:
471 dw
= *(DWORD
*) curPos
;
472 curPos
= curPos
+ sizeof(DWORD
);
474 vshader
->input
.V
[reg
].x
= (float) (((dw
>> 16) & 0xFF) / 255.0f
);
475 vshader
->input
.V
[reg
].y
= (float) (((dw
>> 8) & 0xFF) / 255.0f
);
476 vshader
->input
.V
[reg
].z
= (float) (((dw
>> 0) & 0xFF) / 255.0f
);
477 vshader
->input
.V
[reg
].w
= (float) (((dw
>> 24) & 0xFF) / 255.0f
);
481 u
= *(SHORT
*) curPos
;
482 curPos
= curPos
+ sizeof(SHORT
);
483 v
= *(SHORT
*) curPos
;
484 curPos
= curPos
+ sizeof(SHORT
);
486 vshader
->input
.V
[reg
].x
= (float) u
;
487 vshader
->input
.V
[reg
].y
= (float) v
;
488 vshader
->input
.V
[reg
].z
= 0.0f
;
489 vshader
->input
.V
[reg
].w
= 1.0f
;
493 u
= *(SHORT
*) curPos
;
494 curPos
= curPos
+ sizeof(SHORT
);
495 v
= *(SHORT
*) curPos
;
496 curPos
= curPos
+ sizeof(SHORT
);
497 r
= *(SHORT
*) curPos
;
498 curPos
= curPos
+ sizeof(SHORT
);
499 t
= *(SHORT
*) curPos
;
500 curPos
= curPos
+ sizeof(SHORT
);
502 vshader
->input
.V
[reg
].x
= (float) u
;
503 vshader
->input
.V
[reg
].y
= (float) v
;
504 vshader
->input
.V
[reg
].z
= (float) r
;
505 vshader
->input
.V
[reg
].w
= (float) t
;
509 dw
= *(DWORD
*) curPos
;
510 curPos
= curPos
+ sizeof(DWORD
);
512 vshader
->input
.V
[reg
].x
= (float) ((dw
& 0x000F) >> 0);
513 vshader
->input
.V
[reg
].y
= (float) ((dw
& 0x00F0) >> 8);
514 vshader
->input
.V
[reg
].z
= (float) ((dw
& 0x0F00) >> 16);
515 vshader
->input
.V
[reg
].w
= (float) ((dw
& 0xF000) >> 24);
519 default: /** errooooorr what to do ? */
520 ERR("Error in VertexShader declaration of %s register: unsupported type %s\n", VertexShaderDeclRegister
[reg
], VertexShaderDeclDataTypes
[type
]);
525 /* here D3DVSD_END() */
529 HRESULT WINAPI
IDirect3DVertexShaderDeclarationImpl_GetDeclaration8(IDirect3DVertexShaderDeclarationImpl
* This
, DWORD
* pData
, UINT
* pSizeOfData
) {
531 *pSizeOfData
= This
->declaration8Length
;
534 if (*pSizeOfData
< This
->declaration8Length
) {
535 *pSizeOfData
= This
->declaration8Length
;
536 return D3DERR_MOREDATA
;
538 TRACE("(%p) : GetVertexShaderDeclaration copying to %p\n", This
, pData
);
539 memcpy(pData
, This
->pDeclaration8
, This
->declaration8Length
);