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_shader
);
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 #define MAX_VSHADER_DECL_TYPES 8
69 static CONST
char* VertexShaderDeclDataTypes
[] = {
81 static CONST
char* VertexShaderDeclRegister
[] = {
83 "D3DVSDE_BLENDWEIGHT",
84 "D3DVSDE_BLENDINDICES",
102 /** todo check decl validity */
103 /*inline static*/ DWORD
Direct3DVextexShaderDeclarationImpl_ParseToken(const DWORD
* pToken
) {
104 const DWORD token
= *pToken
;
107 switch ((token
& D3DVSD_TOKENTYPEMASK
) >> D3DVSD_TOKENTYPESHIFT
) { /* maybe a macro to inverse ... */
108 case D3DVSD_TOKEN_NOP
:
109 TRACE(" 0x%08lx NOP()\n", token
);
111 case D3DVSD_TOKEN_STREAM
:
112 if (token
& D3DVSD_STREAMTESSMASK
) {
113 TRACE(" 0x%08lx STREAM_TESS()\n", token
);
115 TRACE(" 0x%08lx STREAM(%lu)\n", token
, ((token
& D3DVSD_STREAMNUMBERMASK
) >> D3DVSD_STREAMNUMBERSHIFT
));
118 case D3DVSD_TOKEN_STREAMDATA
:
119 if (token
& 0x10000000) {
120 TRACE(" 0x%08lx SKIP(%lu)\n", token
, ((token
& D3DVSD_SKIPCOUNTMASK
) >> D3DVSD_SKIPCOUNTSHIFT
));
122 DWORD type
= ((token
& D3DVSD_DATATYPEMASK
) >> D3DVSD_DATATYPESHIFT
);
123 DWORD reg
= ((token
& D3DVSD_VERTEXREGMASK
) >> D3DVSD_VERTEXREGSHIFT
);
124 TRACE(" 0x%08lx REG(%s, %s)\n", token
, VertexShaderDeclRegister
[reg
], VertexShaderDeclDataTypes
[type
]);
127 case D3DVSD_TOKEN_TESSELLATOR
:
128 if (token
& 0x10000000) {
129 DWORD type
= ((token
& D3DVSD_DATATYPEMASK
) >> D3DVSD_DATATYPESHIFT
);
130 DWORD reg
= ((token
& D3DVSD_VERTEXREGMASK
) >> D3DVSD_VERTEXREGSHIFT
);
131 TRACE(" 0x%08lx TESSUV(%s) as %s\n", token
, VertexShaderDeclRegister
[reg
], VertexShaderDeclDataTypes
[type
]);
133 DWORD type
= ((token
& D3DVSD_DATATYPEMASK
) >> D3DVSD_DATATYPESHIFT
);
134 DWORD regout
= ((token
& D3DVSD_VERTEXREGMASK
) >> D3DVSD_VERTEXREGSHIFT
);
135 DWORD regin
= ((token
& D3DVSD_VERTEXREGINMASK
) >> D3DVSD_VERTEXREGINSHIFT
);
136 TRACE(" 0x%08lx TESSNORMAL(%s, %s) as %s\n", token
, VertexShaderDeclRegister
[regin
], VertexShaderDeclRegister
[regout
], VertexShaderDeclDataTypes
[type
]);
139 case D3DVSD_TOKEN_CONSTMEM
:
142 DWORD count
= ((token
& D3DVSD_CONSTCOUNTMASK
) >> D3DVSD_CONSTCOUNTSHIFT
);
143 DWORD constaddress
= ((token
& D3DVSD_CONSTADDRESSMASK
) >> D3DVSD_CONSTADDRESSSHIFT
);
144 TRACE(" 0x%08lx CONST(%lu, %lu)\n", token
, constaddress
, count
);
146 for (i
= 0; i
< count
; ++i
) {
148 TRACE(" c[%lu] = (0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx)\n",
155 TRACE(" c[%lu] = (%8f, %8f, %8f, %8f)\n",
158 *(float*) (pToken
+ 1),
159 *(float*) (pToken
+ 2),
160 *(float*) (pToken
+ 3));
164 tokenlen
= (4 * count
) + 1;
167 case D3DVSD_TOKEN_EXT
:
169 DWORD count
= ((token
& D3DVSD_CONSTCOUNTMASK
) >> D3DVSD_CONSTCOUNTSHIFT
);
170 DWORD extinfo
= ((token
& D3DVSD_EXTINFOMASK
) >> D3DVSD_EXTINFOSHIFT
);
171 TRACE(" 0x%08lx EXT(%lu, %lu)\n", token
, count
, extinfo
);
172 /* todo ... print extension */
173 tokenlen
= count
+ 1;
176 case D3DVSD_TOKEN_END
:
177 TRACE(" 0x%08lx END()\n", token
);
180 TRACE(" 0x%08lx UNKNOWN\n", token
);
186 HRESULT WINAPI
IDirect3DDeviceImpl_CreateVertexShaderDeclaration8(IDirect3DDevice8Impl
* This
, CONST DWORD
* pDeclaration8
, IDirect3DVertexShaderDeclarationImpl
** ppVertexShaderDecl
) {
188 const DWORD
* pToken
= pDeclaration8
;
195 DWORD tex
= D3DFVF_TEX0
;
196 /** TRUE if declaration can be matched by a fvf */
197 IDirect3DVertexShaderDeclarationImpl
* object
;
198 BOOL invalid_fvf
= FALSE
;
200 TRACE("(%p) : pDeclaration8(%p)\n", This
, pDeclaration8
);
202 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IDirect3DVertexShaderDeclarationImpl
));
203 /*object->lpVtbl = &Direct3DVextexShaderDeclaration8_Vtbl;*/
204 object
->device
= This
; /* FIXME: AddRef(This) */
207 while (D3DVSD_END() != *pToken
) {
209 tokenlen
= Direct3DVextexShaderDeclarationImpl_ParseToken(pToken
);
210 tokentype
= ((token
& D3DVSD_TOKENTYPEMASK
) >> D3DVSD_TOKENTYPESHIFT
);
212 /** FVF generation block */
213 if (D3DVSD_TOKEN_STREAM
== tokentype
&& 0 == (D3DVSD_STREAMTESSMASK
& token
)) {
215 * how really works streams,
216 * in DolphinVS dx8 dsk sample they seems to decal reg numbers !!!
218 stream
= ((token
& D3DVSD_STREAMNUMBERMASK
) >> D3DVSD_STREAMNUMBERSHIFT
);
221 /** fvf cannot map mutliple streams, so invalid fvf computing */
225 } else if (D3DVSD_TOKEN_STREAMDATA
== tokentype
&& 0 == (0x10000000 & tokentype
)) {
226 DWORD type
= ((token
& D3DVSD_DATATYPEMASK
) >> D3DVSD_DATATYPESHIFT
);
227 DWORD reg
= ((token
& D3DVSD_VERTEXREGMASK
) >> D3DVSD_VERTEXREGSHIFT
) - stream
;
230 case D3DVSDE_POSITION
:
232 case D3DVSDT_FLOAT3
: fvf
|= D3DFVF_XYZ
; break;
233 case D3DVSDT_FLOAT4
: fvf
|= D3DFVF_XYZRHW
; break;
235 /** errooooorr mismatched use of a register, invalid fvf computing */
237 if (type
>= MAX_VSHADER_DECL_TYPES
) {
238 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_POSITION register: unsupported and unrecognized type %08lx\n", type
);
240 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_POSITION register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
245 case D3DVSDE_BLENDWEIGHT
:
247 case D3DVSDT_FLOAT1
: fvf
|= D3DFVF_XYZB1
; break;
248 case D3DVSDT_FLOAT2
: fvf
|= D3DFVF_XYZB2
; break;
249 case D3DVSDT_FLOAT3
: fvf
|= D3DFVF_XYZB3
; break;
250 case D3DVSDT_FLOAT4
: fvf
|= D3DFVF_XYZB4
; break;
252 /** errooooorr mismatched use of a register, invalid fvf computing */
254 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_BLENDWEIGHT register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
258 case D3DVSDE_BLENDINDICES
: /* seem to be B5 as said in MSDN Dx9SDK ?? */
260 case D3DVSDT_UBYTE4
: fvf
|= D3DFVF_LASTBETA_UBYTE4
; break;
262 /** errooooorr mismatched use of a register, invalid fvf computing */
264 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_BLENDINDINCES register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
268 case D3DVSDE_NORMAL
: /* TODO: only FLOAT3 supported ... another choice possible ? */
270 case D3DVSDT_FLOAT3
: fvf
|= D3DFVF_NORMAL
; break;
272 /** errooooorr mismatched use of a register, invalid fvf computing */
274 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_NORMAL register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
278 case D3DVSDE_PSIZE
: /* TODO: only FLOAT1 supported ... another choice possible ? */
280 case D3DVSDT_FLOAT1
: fvf
|= D3DFVF_PSIZE
; break;
282 /** errooooorr mismatched use of a register, invalid fvf computing */
284 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_PSIZE register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
288 case D3DVSDE_DIFFUSE
: /* TODO: only D3DCOLOR supported */
290 case D3DVSDT_D3DCOLOR
: fvf
|= D3DFVF_DIFFUSE
; break;
292 /** errooooorr mismatched use of a register, invalid fvf computing */
294 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_DIFFUSE register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
298 case D3DVSDE_SPECULAR
: /* TODO: only D3DCOLOR supported */
300 case D3DVSDT_D3DCOLOR
: fvf
|= D3DFVF_SPECULAR
; break;
302 /** errooooorr mismatched use of a register, invalid fvf computing */
304 TRACE("Mismatched use in VertexShader declaration of D3DVSDE_SPECULAR register: unsupported type %s\n", VertexShaderDeclDataTypes
[type
]);
309 * TODO: for TEX* only FLOAT2 supported
310 * by default using texture type info
312 case D3DVSDE_TEXCOORD0
: tex
= max(tex
, D3DFVF_TEX1
); break;
313 case D3DVSDE_TEXCOORD1
: tex
= max(tex
, D3DFVF_TEX2
); break;
314 case D3DVSDE_TEXCOORD2
: tex
= max(tex
, D3DFVF_TEX3
); break;
315 case D3DVSDE_TEXCOORD3
: tex
= max(tex
, D3DFVF_TEX4
); break;
316 case D3DVSDE_TEXCOORD4
: tex
= max(tex
, D3DFVF_TEX5
); break;
317 case D3DVSDE_TEXCOORD5
: tex
= max(tex
, D3DFVF_TEX6
); break;
318 case D3DVSDE_TEXCOORD6
: tex
= max(tex
, D3DFVF_TEX7
); break;
319 case D3DVSDE_TEXCOORD7
: tex
= max(tex
, D3DFVF_TEX8
); break;
321 case D3DVSDE_POSITION2
: /* maybe D3DFVF_XYZRHW instead D3DFVF_XYZ (of D3DVDE_POSITION) ... to see */
322 case D3DVSDE_NORMAL2
: /* FIXME i don't know what to do here ;( */
323 FIXME("[%lu] registers in VertexShader declaration not supported yet (token:0x%08lx)\n", reg
, token
);
326 /*TRACE("VertexShader declaration define %x as current FVF\n", fvf);*/
332 /*TRACE("VertexShader declaration define %x as texture level\n", tex);*/
335 /* here D3DVSD_END() */
336 len
+= Direct3DVextexShaderDeclarationImpl_ParseToken(pToken
);
337 /* copy fvf if valid */
338 if (FALSE
== invalid_fvf
)
343 object
->declaration8Length
= len
* sizeof(DWORD
);
344 /* copy the declaration */
345 object
->pDeclaration8
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, object
->declaration8Length
);
346 memcpy(object
->pDeclaration8
, pDeclaration8
, object
->declaration8Length
);
348 *ppVertexShaderDecl
= object
;
353 HRESULT WINAPI
IDirect3DDeviceImpl_FillVertexShaderInput(IDirect3DDevice8Impl
* This
,
354 IDirect3DVertexShaderImpl
* vshader
,
355 const void* vertexFirstStream
,
356 DWORD StartVertexIndex
,
359 const DWORD
* pToken
= This
->UpdateStateBlock
->vertexShaderDecl
->pDeclaration8
;
364 /** for input readers */
365 const char* curPos
= NULL
;
370 TRACE("(%p) - This:%p - stream:%p, startIdx=%lu, idxDecal=%lu\n", vshader
, This
, vertexFirstStream
, StartVertexIndex
, idxDecal
);
372 while (D3DVSD_END() != *pToken
) {
374 tokentype
= ((token
& D3DVSD_TOKENTYPEMASK
) >> D3DVSD_TOKENTYPESHIFT
);
376 /** FVF generation block */
377 if (D3DVSD_TOKEN_STREAM
== tokentype
&& 0 == (D3DVSD_STREAMTESSMASK
& token
)) {
378 IDirect3DVertexBuffer8
* pVB
;
379 const char* startVtx
= NULL
;
384 * how really works streams,
385 * in DolphinVS dx8 dsk sample use it !!!
387 stream
= ((token
& D3DVSD_STREAMNUMBERMASK
) >> D3DVSD_STREAMNUMBERSHIFT
);
390 skip
= This
->StateBlock
->stream_stride
[0];
391 startVtx
= (const char*) vertexFirstStream
+ (StartVertexIndex
* skip
);
392 curPos
= startVtx
+ idxDecal
;
393 /*TRACE(" using stream[%lu] with %lu decal => curPos %p\n", stream, idxDecal, curPos);*/
395 skip
= This
->StateBlock
->stream_stride
[stream
];
396 pVB
= This
->StateBlock
->stream_source
[stream
];
399 ERR("using unitialised stream[%lu]\n", stream
);
400 return D3DERR_INVALIDCALL
;
402 startVtx
= ((IDirect3DVertexBuffer8Impl
*) pVB
)->allocatedMemory
+ (StartVertexIndex
* skip
);
403 /** do we need to decal if we use idxBuffer */
404 curPos
= startVtx
+ idxDecal
;
405 /*TRACE(" using stream[%lu] with %lu decal\n", stream, idxDecal);*/
408 } else if (D3DVSD_TOKEN_CONSTMEM
== tokentype
) {
411 DWORD count
= ((token
& D3DVSD_CONSTCOUNTMASK
) >> D3DVSD_CONSTCOUNTSHIFT
);
412 DWORD constaddress
= ((token
& D3DVSD_CONSTADDRESSMASK
) >> D3DVSD_CONSTADDRESSSHIFT
);
414 for (i
= 0; i
< count
; ++i
) {
415 vshader
->data
->C
[constaddress
+ i
].x
= *(float*)pToken
;
416 vshader
->data
->C
[constaddress
+ i
].y
= *(float*)(pToken
+ 1);
417 vshader
->data
->C
[constaddress
+ i
].z
= *(float*)(pToken
+ 2);
418 vshader
->data
->C
[constaddress
+ i
].w
= *(float*)(pToken
+ 3);
422 } else if (D3DVSD_TOKEN_STREAMDATA
== tokentype
&& 0 != (0x10000000 & tokentype
)) {
424 DWORD skipCount
= ((token
& D3DVSD_SKIPCOUNTMASK
) >> D3DVSD_SKIPCOUNTSHIFT
);
425 curPos
= curPos
+ skipCount
* sizeof(DWORD
);
428 } else if (D3DVSD_TOKEN_STREAMDATA
== tokentype
&& 0 == (0x10000000 & tokentype
)) {
429 DWORD type
= ((token
& D3DVSD_DATATYPEMASK
) >> D3DVSD_DATATYPESHIFT
);
430 DWORD reg
= ((token
& D3DVSD_VERTEXREGMASK
) >> D3DVSD_VERTEXREGSHIFT
);
435 x
= *(float*) curPos
;
436 curPos
= curPos
+ sizeof(float);
438 vshader
->input
.V
[reg
].x
= x
;
439 vshader
->input
.V
[reg
].y
= 0.0f
;
440 vshader
->input
.V
[reg
].z
= 0.0f
;
441 vshader
->input
.V
[reg
].w
= 1.0f
;
445 x
= *(float*) curPos
;
446 curPos
= curPos
+ sizeof(float);
447 y
= *(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
= 0.0f
;
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);
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
= 1.0f
;
471 x
= *(float*) curPos
;
472 curPos
= curPos
+ sizeof(float);
473 y
= *(float*) curPos
;
474 curPos
= curPos
+ sizeof(float);
475 z
= *(float*) curPos
;
476 curPos
= curPos
+ sizeof(float);
477 w
= *(float*) curPos
;
478 curPos
= curPos
+ sizeof(float);
480 vshader
->input
.V
[reg
].x
= x
;
481 vshader
->input
.V
[reg
].y
= y
;
482 vshader
->input
.V
[reg
].z
= z
;
483 vshader
->input
.V
[reg
].w
= w
;
486 case D3DVSDT_D3DCOLOR
:
487 dw
= *(DWORD
*) curPos
;
488 curPos
= curPos
+ sizeof(DWORD
);
490 vshader
->input
.V
[reg
].x
= (float) (((dw
>> 16) & 0xFF) / 255.0f
);
491 vshader
->input
.V
[reg
].y
= (float) (((dw
>> 8) & 0xFF) / 255.0f
);
492 vshader
->input
.V
[reg
].z
= (float) (((dw
>> 0) & 0xFF) / 255.0f
);
493 vshader
->input
.V
[reg
].w
= (float) (((dw
>> 24) & 0xFF) / 255.0f
);
497 u
= *(SHORT
*) curPos
;
498 curPos
= curPos
+ sizeof(SHORT
);
499 v
= *(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
= 0.0f
;
505 vshader
->input
.V
[reg
].w
= 1.0f
;
509 u
= *(SHORT
*) curPos
;
510 curPos
= curPos
+ sizeof(SHORT
);
511 v
= *(SHORT
*) curPos
;
512 curPos
= curPos
+ sizeof(SHORT
);
513 r
= *(SHORT
*) curPos
;
514 curPos
= curPos
+ sizeof(SHORT
);
515 t
= *(SHORT
*) curPos
;
516 curPos
= curPos
+ sizeof(SHORT
);
518 vshader
->input
.V
[reg
].x
= (float) u
;
519 vshader
->input
.V
[reg
].y
= (float) v
;
520 vshader
->input
.V
[reg
].z
= (float) r
;
521 vshader
->input
.V
[reg
].w
= (float) t
;
525 dw
= *(DWORD
*) curPos
;
526 curPos
= curPos
+ sizeof(DWORD
);
528 vshader
->input
.V
[reg
].x
= (float) ((dw
& 0x000F) >> 0);
529 vshader
->input
.V
[reg
].y
= (float) ((dw
& 0x00F0) >> 8);
530 vshader
->input
.V
[reg
].z
= (float) ((dw
& 0x0F00) >> 16);
531 vshader
->input
.V
[reg
].w
= (float) ((dw
& 0xF000) >> 24);
535 default: /** errooooorr what to do ? */
536 ERR("Error in VertexShader declaration of %s register: unsupported type %s\n", VertexShaderDeclRegister
[reg
], VertexShaderDeclDataTypes
[type
]);
541 /* here D3DVSD_END() */
545 HRESULT WINAPI
IDirect3DVertexShaderDeclarationImpl_GetDeclaration8(IDirect3DVertexShaderDeclarationImpl
* This
, DWORD
* pData
, UINT
* pSizeOfData
) {
547 *pSizeOfData
= This
->declaration8Length
;
550 if (*pSizeOfData
< This
->declaration8Length
) {
551 *pSizeOfData
= This
->declaration8Length
;
552 return D3DERR_MOREDATA
;
554 TRACE("(%p) : GetVertexShaderDeclaration copying to %p\n", This
, pData
);
555 memcpy(pData
, This
->pDeclaration8
, This
->declaration8Length
);