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
27 #include "wine/debug.h"
31 #include "d3d8_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
36 * DirectX9 SDK download
37 * http://msdn.microsoft.com/library/default.asp?url=/downloads/list/directx.asp
40 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndrive/html/directx07162002.asp
42 * Using Vertex Shaders
43 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndrive/html/directx02192001.asp
46 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/whatsnew.asp
49 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader2_0/VertexShader2_0.asp
50 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader2_0/Instructions/Instructions.asp
51 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/GettingStarted/VertexDeclaration/VertexDeclaration.asp
52 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader3_0/VertexShader3_0.asp
55 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/advancedtopics/VertexPipe/matrixstack/matrixstack.asp
58 * http://msdn.microsoft.com/library/en-us/directx9_c/directx/graphics/programmingguide/GettingStarted/VertexFormats/vformats.asp
60 * NVIDIA: DX8 Vertex Shader to NV Vertex Program
61 * http://developer.nvidia.com/view.asp?IO=vstovp
63 * NVIDIA: Memory Management with VAR
64 * http://developer.nvidia.com/view.asp?IO=var_memory_management
67 /** Vertex Shader Declaration data types tokens */
68 static CONST
char* VertexShaderDeclDataTypes
[] = {
80 static CONST
char* VertexShaderDeclRegister
[] = {
82 "D3DVSDE_BLENDWEIGHT",
83 "D3DVSDE_BLENDINDICES",
101 /** todo check decl validity */
102 inline static DWORD
Direct3DVextexShaderDeclarationImpl_ParseToken(const DWORD
* pToken
) {
103 const DWORD token
= *pToken
;
106 switch ((token
& D3DVSD_TOKENTYPEMASK
) >> D3DVSD_TOKENTYPESHIFT
) { /* maybe a macro to inverse ... */
107 case D3DVSD_TOKEN_NOP
:
108 TRACE(" 0x%08lx NOP()\n", token
);
110 case D3DVSD_TOKEN_STREAM
:
111 if (token
& D3DVSD_STREAMTESSMASK
) {
112 TRACE(" 0x%08lx STREAM_TESS()\n", token
);
114 TRACE(" 0x%08lx STREAM(%lu)\n", token
, ((token
& D3DVSD_STREAMNUMBERMASK
) >> D3DVSD_STREAMNUMBERSHIFT
));
117 case D3DVSD_TOKEN_STREAMDATA
:
118 if (token
& 0x10000000) {
119 TRACE(" 0x%08lx SKIP(%lu)\n", token
, ((token
& D3DVSD_SKIPCOUNTMASK
) >> D3DVSD_SKIPCOUNTSHIFT
));
121 DWORD type
= ((token
& D3DVSD_DATATYPEMASK
) >> D3DVSD_DATATYPESHIFT
);
122 DWORD reg
= ((token
& D3DVSD_VERTEXREGMASK
) >> D3DVSD_VERTEXREGSHIFT
);
123 TRACE(" 0x%08lx REG(%s, %s)\n", token
, VertexShaderDeclRegister
[reg
], VertexShaderDeclDataTypes
[type
]);
126 case D3DVSD_TOKEN_TESSELLATOR
:
127 if (token
& 0x10000000) {
128 DWORD type
= ((token
& D3DVSD_DATATYPEMASK
) >> D3DVSD_DATATYPESHIFT
);
129 DWORD reg
= ((token
& D3DVSD_VERTEXREGMASK
) >> D3DVSD_VERTEXREGSHIFT
);
130 TRACE(" 0x%08lx TESSUV(%s) as %s\n", token
, VertexShaderDeclRegister
[reg
], VertexShaderDeclDataTypes
[type
]);
132 DWORD type
= ((token
& D3DVSD_DATATYPEMASK
) >> D3DVSD_DATATYPESHIFT
);
133 DWORD regout
= ((token
& D3DVSD_VERTEXREGMASK
) >> D3DVSD_VERTEXREGSHIFT
);
134 DWORD regin
= ((token
& D3DVSD_VERTEXREGINMASK
) >> D3DVSD_VERTEXREGINSHIFT
);
135 TRACE(" 0x%08lx TESSNORMAL(%s, %s) as %s\n", token
, VertexShaderDeclRegister
[regin
], VertexShaderDeclRegister
[regout
], VertexShaderDeclDataTypes
[type
]);
138 case D3DVSD_TOKEN_CONSTMEM
:
141 DWORD count
= ((token
& D3DVSD_CONSTCOUNTMASK
) >> D3DVSD_CONSTCOUNTSHIFT
);
142 DWORD constaddress
= ((token
& D3DVSD_CONSTADDRESSMASK
) >> D3DVSD_CONSTADDRESSSHIFT
);
143 TRACE(" 0x%08lx CONST(%lu, %lu)\n", token
, constaddress
, count
);
145 for (i
= 0; i
< count
; ++i
) {
146 TRACE(" c[%lu] = (0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx)\n",
155 tokenlen
= count
+ 1;
158 case D3DVSD_TOKEN_EXT
:
160 DWORD count
= ((token
& D3DVSD_CONSTCOUNTMASK
) >> D3DVSD_CONSTCOUNTSHIFT
);
161 DWORD extinfo
= ((token
& D3DVSD_EXTINFOMASK
) >> D3DVSD_EXTINFOSHIFT
);
162 TRACE(" 0x%08lx EXT(%lu, %lu)\n", token
, count
, extinfo
);
163 /* todo ... print extension */
164 tokenlen
= count
+ 1;
167 case D3DVSD_TOKEN_END
:
168 TRACE(" 0x%08lx END()\n", token
);
171 TRACE(" 0x%08lx UNKNOWN\n", token
);
177 HRESULT WINAPI
IDirect3DDeviceImpl_CreateVertexShaderDeclaration8(IDirect3DDevice8Impl
* This
, CONST DWORD
* pDeclaration8
, IDirect3DVertexShaderDeclarationImpl
** ppVertexShaderDecl
) {
179 const DWORD
* pToken
= pDeclaration8
;
186 DWORD tex
= D3DFVF_TEX0
;
187 /** TRUE if declaration can be matched by a fvf */
188 IDirect3DVertexShaderDeclarationImpl
* object
;
189 BOOL invalid_fvf
= FALSE
;
191 TRACE("(%p) : pDeclaration8(%p)\n", This
, pDeclaration8
);
193 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IDirect3DVertexShaderDeclarationImpl
));
194 /*object->lpVtbl = &Direct3DVextexShaderDeclaration8_Vtbl;*/
195 object
->device
= This
; /* FIXME: AddRef(This) */
198 while (D3DVSD_END() != *pToken
) {
200 tokenlen
= Direct3DVextexShaderDeclarationImpl_ParseToken(pToken
);
201 tokentype
= ((token
& D3DVSD_TOKENTYPEMASK
) >> D3DVSD_TOKENTYPESHIFT
);
203 /** FVF generation block */
204 if (D3DVSD_TOKEN_STREAM
== tokentype
&& 0 == (D3DVSD_STREAMTESSMASK
& token
)) {
206 * how really works streams,
207 * in DolphinVS dx8 dsk sample they seems to decal reg numbers !!!
209 stream
= ((token
& D3DVSD_STREAMNUMBERMASK
) >> D3DVSD_STREAMNUMBERSHIFT
);
212 /** fvf cannot map mutliple streams, so invalid fvf computing */
216 } else if (D3DVSD_TOKEN_STREAMDATA
== tokentype
&& 0 == (0x10000000 & tokentype
)) {
217 DWORD type
= ((token
& D3DVSD_DATATYPEMASK
) >> D3DVSD_DATATYPESHIFT
);
218 DWORD reg
= ((token
& D3DVSD_VERTEXREGMASK
) >> D3DVSD_VERTEXREGSHIFT
) - stream
;
221 case D3DVSDE_POSITION
:
223 case D3DVSDT_FLOAT3
: fvf
|= D3DFVF_XYZ
; break;
224 case D3DVSDT_FLOAT4
: fvf
|= D3DFVF_XYZRHW
; break;
226 /** errooooorr mismatched use of a register, invalid fvf computing */
228 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_POSITION register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
232 case D3DVSDE_BLENDWEIGHT
:
234 case D3DVSDT_FLOAT1
: fvf
|= D3DFVF_XYZB1
; break;
235 case D3DVSDT_FLOAT2
: fvf
|= D3DFVF_XYZB2
; break;
236 case D3DVSDT_FLOAT3
: fvf
|= D3DFVF_XYZB3
; break;
237 case D3DVSDT_FLOAT4
: fvf
|= D3DFVF_XYZB4
; break;
239 /** errooooorr mismatched use of a register, invalid fvf computing */
241 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_BLENDWEIGHT register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
245 case D3DVSDE_BLENDINDICES
: /* seem to be B5 as said in MSDN Dx9SDK ?? */
247 case D3DVSDT_UBYTE4
: fvf
|= D3DFVF_LASTBETA_UBYTE4
; break;
249 /** errooooorr mismatched use of a register, invalid fvf computing */
251 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_BLENDINDINCES register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
255 case D3DVSDE_NORMAL
: /* TODO: only FLOAT3 supported ... another choice possible ? */
257 case D3DVSDT_FLOAT3
: fvf
|= D3DFVF_NORMAL
; break;
259 /** errooooorr mismatched use of a register, invalid fvf computing */
261 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_NORMAL register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
265 case D3DVSDE_PSIZE
: /* TODO: only FLOAT1 supported ... another choice possible ? */
267 case D3DVSDT_FLOAT1
: fvf
|= D3DFVF_PSIZE
; break;
269 /** errooooorr mismatched use of a register, invalid fvf computing */
271 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_PSIZE register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
275 case D3DVSDE_DIFFUSE
: /* TODO: only D3DCOLOR supported */
277 case D3DVSDT_D3DCOLOR
: fvf
|= D3DFVF_DIFFUSE
; break;
279 /** errooooorr mismatched use of a register, invalid fvf computing */
281 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_DIFFUSE register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
285 case D3DVSDE_SPECULAR
: /* TODO: only D3DCOLOR supported */
287 case D3DVSDT_D3DCOLOR
: fvf
|= D3DFVF_SPECULAR
; break;
289 /** errooooorr mismatched use of a register, invalid fvf computing */
291 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_SPECULAR register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
296 * TODO: for TEX* only FLOAT2 supported
297 * by default using texture type info
299 case D3DVSDE_TEXCOORD0
: tex
= max(tex
, D3DFVF_TEX1
); break;
300 case D3DVSDE_TEXCOORD1
: tex
= max(tex
, D3DFVF_TEX2
); break;
301 case D3DVSDE_TEXCOORD2
: tex
= max(tex
, D3DFVF_TEX3
); break;
302 case D3DVSDE_TEXCOORD3
: tex
= max(tex
, D3DFVF_TEX4
); break;
303 case D3DVSDE_TEXCOORD4
: tex
= max(tex
, D3DFVF_TEX5
); break;
304 case D3DVSDE_TEXCOORD5
: tex
= max(tex
, D3DFVF_TEX6
); break;
305 case D3DVSDE_TEXCOORD6
: tex
= max(tex
, D3DFVF_TEX7
); break;
306 case D3DVSDE_TEXCOORD7
: tex
= max(tex
, D3DFVF_TEX8
); break;
308 case D3DVSDE_POSITION2
: /* maybe D3DFVF_XYZRHW instead D3DFVF_XYZ (of D3DVDE_POSITION) ... to see */
309 case D3DVSDE_NORMAL2
: /* FIXME i don't know what to do here ;( */
310 FIXME("[%lu] registers in VertexShader declaration not supported yet (token:0x%08lx)\n", reg
, token
);
313 /*TRACE("VertexShader declaration define %x as current FVF\n", fvf);*/
319 /*TRACE("VertexShader declaration define %x as texture level\n", tex);*/
322 /* here D3DVSD_END() */
323 len
+= Direct3DVextexShaderDeclarationImpl_ParseToken(pToken
);
324 /* copy fvf if valid */
325 if (FALSE
== invalid_fvf
)
330 object
->declaration8Length
= len
* sizeof(DWORD
);
331 /* copy the declaration */
332 object
->pDeclaration8
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, object
->declaration8Length
);
333 memcpy(object
->pDeclaration8
, pDeclaration8
, object
->declaration8Length
);
335 *ppVertexShaderDecl
= object
;
340 HRESULT WINAPI
IDirect3DDeviceImpl_FillVertexShaderInput(IDirect3DDevice8Impl
* This
,
341 IDirect3DVertexShaderImpl
* vshader
,
342 const void* vertexFirstStream
,
343 DWORD StartVertexIndex
,
346 const DWORD
* pToken
= This
->UpdateStateBlock
->vertexShaderDecl
->pDeclaration8
;
351 /** for input readers */
352 const char* curPos
= NULL
;
357 /*TRACE("(%p) - This:%p - stream:%p, startIdx=%lu, idxDecal=%lu\n", vshader, This, vertexFirstStream, StartVertexIndex, idxDecal);*/
358 while (D3DVSD_END() != *pToken
) {
360 tokentype
= ((token
& D3DVSD_TOKENTYPEMASK
) >> D3DVSD_TOKENTYPESHIFT
);
362 /** FVF generation block */
363 if (D3DVSD_TOKEN_STREAM
== tokentype
&& 0 == (D3DVSD_STREAMTESSMASK
& token
)) {
364 IDirect3DVertexBuffer8
* pVB
;
365 const char* startVtx
= NULL
;
370 * how really works streams,
371 * in DolphinVS dx8 dsk sample use it !!!
373 stream
= ((token
& D3DVSD_STREAMNUMBERMASK
) >> D3DVSD_STREAMNUMBERSHIFT
);
376 skip
= This
->StateBlock
->stream_stride
[0];
377 startVtx
= (const char*) vertexFirstStream
+ (StartVertexIndex
* skip
);
378 curPos
= startVtx
+ idxDecal
;
379 /*TRACE(" using stream[%lu] with %lu decal => curPos %p\n", stream, idxDecal, curPos);*/
381 skip
= This
->StateBlock
->stream_stride
[stream
];
382 pVB
= This
->StateBlock
->stream_source
[stream
];
385 ERR("using unitialised stream[%lu]\n", stream
);
386 return D3DERR_INVALIDCALL
;
388 startVtx
= ((IDirect3DVertexBuffer8Impl
*) pVB
)->allocatedMemory
+ (StartVertexIndex
* skip
);
389 /** do we need to decal if we use idxBuffer */
390 curPos
= startVtx
+ idxDecal
;
391 /*TRACE(" using stream[%lu] with %lu decal\n", stream, idxDecal);*/
394 } else if (D3DVSD_TOKEN_CONSTMEM
== tokentype
) {
397 DWORD count
= ((token
& D3DVSD_CONSTCOUNTMASK
) >> D3DVSD_CONSTCOUNTSHIFT
);
398 DWORD constaddress
= ((token
& D3DVSD_CONSTADDRESSMASK
) >> D3DVSD_CONSTADDRESSSHIFT
);
400 for (i
= 0; i
< count
; ++i
) {
401 vshader
->data
->C
[constaddress
+ i
].x
= *(float*)pToken
;
402 vshader
->data
->C
[constaddress
+ i
].y
= *(float*)(pToken
+ 1);
403 vshader
->data
->C
[constaddress
+ i
].z
= *(float*)(pToken
+ 2);
404 vshader
->data
->C
[constaddress
+ i
].w
= *(float*)(pToken
+ 3);
408 } else if (D3DVSD_TOKEN_STREAMDATA
== tokentype
&& 0 != (0x10000000 & tokentype
)) {
410 DWORD skipCount
= ((token
& D3DVSD_SKIPCOUNTMASK
) >> D3DVSD_SKIPCOUNTSHIFT
);
411 curPos
= curPos
+ skipCount
* sizeof(DWORD
);
414 } else if (D3DVSD_TOKEN_STREAMDATA
== tokentype
&& 0 == (0x10000000 & tokentype
)) {
415 DWORD type
= ((token
& D3DVSD_DATATYPEMASK
) >> D3DVSD_DATATYPESHIFT
);
416 DWORD reg
= ((token
& D3DVSD_VERTEXREGMASK
) >> D3DVSD_VERTEXREGSHIFT
);
421 x
= *(float*) curPos
;
422 curPos
= curPos
+ sizeof(float);
424 vshader
->input
.V
[reg
].x
= x
;
425 vshader
->input
.V
[reg
].y
= 0.0f
;
426 vshader
->input
.V
[reg
].z
= 0.0f
;
427 vshader
->input
.V
[reg
].w
= 1.0f
;
431 x
= *(float*) curPos
;
432 curPos
= curPos
+ sizeof(float);
433 y
= *(float*) curPos
;
434 curPos
= curPos
+ sizeof(float);
436 vshader
->input
.V
[reg
].x
= x
;
437 vshader
->input
.V
[reg
].y
= y
;
438 vshader
->input
.V
[reg
].z
= 0.0f
;
439 vshader
->input
.V
[reg
].w
= 1.0f
;
443 x
= *(float*) curPos
;
444 curPos
= curPos
+ sizeof(float);
445 y
= *(float*) curPos
;
446 curPos
= curPos
+ sizeof(float);
447 z
= *(float*) curPos
;
448 curPos
= curPos
+ sizeof(float);
450 vshader
->input
.V
[reg
].x
= x
;
451 vshader
->input
.V
[reg
].y
= y
;
452 vshader
->input
.V
[reg
].z
= z
;
453 vshader
->input
.V
[reg
].w
= 1.0f
;
457 x
= *(float*) curPos
;
458 curPos
= curPos
+ sizeof(float);
459 y
= *(float*) curPos
;
460 curPos
= curPos
+ sizeof(float);
461 z
= *(float*) curPos
;
462 curPos
= curPos
+ sizeof(float);
463 w
= *(float*) curPos
;
464 curPos
= curPos
+ sizeof(float);
466 vshader
->input
.V
[reg
].x
= x
;
467 vshader
->input
.V
[reg
].y
= y
;
468 vshader
->input
.V
[reg
].z
= z
;
469 vshader
->input
.V
[reg
].w
= w
;
472 case D3DVSDT_D3DCOLOR
:
473 dw
= *(DWORD
*) curPos
;
474 curPos
= curPos
+ sizeof(DWORD
);
476 vshader
->input
.V
[reg
].x
= (float) (((dw
>> 16) & 0xFF) / 255.0f
);
477 vshader
->input
.V
[reg
].y
= (float) (((dw
>> 8) & 0xFF) / 255.0f
);
478 vshader
->input
.V
[reg
].z
= (float) (((dw
>> 0) & 0xFF) / 255.0f
);
479 vshader
->input
.V
[reg
].w
= (float) (((dw
>> 24) & 0xFF) / 255.0f
);
483 u
= *(SHORT
*) curPos
;
484 curPos
= curPos
+ sizeof(SHORT
);
485 v
= *(SHORT
*) curPos
;
486 curPos
= curPos
+ sizeof(SHORT
);
488 vshader
->input
.V
[reg
].x
= (float) u
;
489 vshader
->input
.V
[reg
].y
= (float) v
;
490 vshader
->input
.V
[reg
].z
= 0.0f
;
491 vshader
->input
.V
[reg
].w
= 1.0f
;
495 u
= *(SHORT
*) curPos
;
496 curPos
= curPos
+ sizeof(SHORT
);
497 v
= *(SHORT
*) curPos
;
498 curPos
= curPos
+ sizeof(SHORT
);
499 r
= *(SHORT
*) curPos
;
500 curPos
= curPos
+ sizeof(SHORT
);
501 t
= *(SHORT
*) curPos
;
502 curPos
= curPos
+ sizeof(SHORT
);
504 vshader
->input
.V
[reg
].x
= (float) u
;
505 vshader
->input
.V
[reg
].y
= (float) v
;
506 vshader
->input
.V
[reg
].z
= (float) r
;
507 vshader
->input
.V
[reg
].w
= (float) t
;
511 dw
= *(DWORD
*) curPos
;
512 curPos
= curPos
+ sizeof(DWORD
);
514 vshader
->input
.V
[reg
].x
= (float) ((dw
& 0x000F) >> 0);
515 vshader
->input
.V
[reg
].y
= (float) ((dw
& 0x00F0) >> 8);
516 vshader
->input
.V
[reg
].z
= (float) ((dw
& 0x0F00) >> 16);
517 vshader
->input
.V
[reg
].w
= (float) ((dw
& 0xF000) >> 24);
521 default: /** errooooorr what to do ? */
522 ERR("Error in VertexShader declaration of %s register: unsupported type %s\n", VertexShaderDeclRegister
[reg
], VertexShaderDeclDataTypes
[type
]);
527 /* here D3DVSD_END() */
531 HRESULT WINAPI
IDirect3DVertexShaderDeclarationImpl_GetDeclaration8(IDirect3DVertexShaderDeclarationImpl
* This
, DWORD
* pData
, UINT
* pSizeOfData
) {
533 *pSizeOfData
= This
->declaration8Length
;
536 if (*pSizeOfData
< This
->declaration8Length
) {
537 *pSizeOfData
= This
->declaration8Length
;
538 return D3DERR_MOREDATA
;
540 TRACE("(%p) : GetVertexShaderDeclaration copying to %p\n", This
, pData
);
541 memcpy(pData
, This
->pDeclaration8
, This
->declaration8Length
);