2 * shaders 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 typedef void (*shader_fct_t)();
67 typedef struct SHADER_OPCODE {
70 CONST UINT num_params;
71 shader_fct_t soft_fct;
74 /** Vertex Shader Declaration data types tokens */
75 static CONST char* VertexShaderDeclDataTypes [] = {
87 static CONST char* VertexShaderDeclRegister [] = {
89 "D3DVSDE_BLENDWEIGHT",
90 "D3DVSDE_BLENDINDICES",
108 /*******************************
109 * vshader functions software VM
112 void vshader_add(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
113 d->x = s0->x + s1->x;
114 d->y = s0->y + s1->y;
115 d->z = s0->z + s1->z;
116 d->w = s0->w + s1->w;
119 void vshader_dp3(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
120 d->x = d->y = d->z = d->w = s0->x * s1->x + s0->y * s1->y + s0->z * s1->z;
123 void vshader_dp4(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
124 d->x = d->y = d->z = d->w = s0->x * s1->x + s0->y * s1->y + s0->z * s1->z + s0->w * s1->w;
127 DPRINTF("executing dp4: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
128 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w);
132 void vshader_dst(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
134 d->y = s0->y * s1->y;
139 DPRINTF("executing dst: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
140 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w);
144 void vshader_expp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
145 float tmp_f = floorf(s0->w);
147 tmp_f = powf(2.0f, s0->w);
148 tmp_d = *((DWORD*) &tmp_f) & 0xFFFFFF00;
150 d->x = powf(2.0f, tmp_f);
151 d->y = s0->w - tmp_f;
152 d->z = *((float*) &tmp_d);
156 DPRINTF("executing exp: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
157 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w);
161 void vshader_lit(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
163 d->y = (0.0f < s0->x) ? s0->x : 0.0f;
164 d->z = (0.0f < s0->x && 0.0f < s0->y) ? powf(s0->y, s0->w) : 0.0f;
168 DPRINTF("executing lit: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
169 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w);
173 void vshader_logp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
174 float tmp_f = fabsf(s0->w);
175 d->x = d->y = d->z = d->w = (0.0f != tmp_f) ? logf(tmp_f) / logf(2.0f) : -HUGE;
178 void vshader_mad(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1, D3DSHADERVECTOR* s2) {
179 d->x = s0->x * s1->x + s2->x;
180 d->y = s0->y * s1->y + s2->y;
181 d->z = s0->z * s1->z + s2->z;
182 d->w = s0->w * s1->w + s2->w;
185 void vshader_max(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
186 d->x = (s0->x >= s1->x) ? s0->x : s1->x;
187 d->y = (s0->y >= s1->y) ? s0->y : s1->y;
188 d->z = (s0->z >= s1->z) ? s0->z : s1->z;
189 d->w = (s0->w >= s1->w) ? s0->w : s1->w;
192 void vshader_min(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
193 d->x = (s0->x < s1->x) ? s0->x : s1->x;
194 d->y = (s0->y < s1->y) ? s0->y : s1->y;
195 d->z = (s0->z < s1->z) ? s0->z : s1->z;
196 d->w = (s0->w < s1->w) ? s0->w : s1->w;
199 void vshader_mov(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
206 DPRINTF("executing mov: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
207 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w);
211 void vshader_mul(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
212 d->x = s0->x * s1->x;
213 d->y = s0->y * s1->y;
214 d->z = s0->z * s1->z;
215 d->w = s0->w * s1->w;
218 DPRINTF("executing mul: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
219 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w);
223 void vshader_nop(void) {
224 /* NOPPPP ahhh too easy ;) */
227 void vshader_rcp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
228 d->x = d->y = d->z = d->w = (0.0f == s0->w) ? HUGE : 1.0f / s0->w;
231 void vshader_rsq(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
232 float tmp_f = fabsf(s0->w);
233 d->x = d->y = d->z = d->w = (0.0f == tmp_f) ? HUGE : ((1.0f != tmp_f) ? 1.0f / sqrtf(tmp_f) : 1.0f);
236 void vshader_sge(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
237 d->x = (s0->x >= s1->x) ? 1.0f : 0.0f;
238 d->y = (s0->y >= s1->y) ? 1.0f : 0.0f;
239 d->z = (s0->z >= s1->z) ? 1.0f : 0.0f;
240 d->w = (s0->w >= s1->w) ? 1.0f : 0.0f;
243 void vshader_slt(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
244 d->x = (s0->x < s1->x) ? 1.0f : 0.0f;
245 d->y = (s0->y < s1->y) ? 1.0f : 0.0f;
246 d->z = (s0->z < s1->z) ? 1.0f : 0.0f;
247 d->w = (s0->w < s1->w) ? 1.0f : 0.0f;
250 void vshader_sub(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1) {
251 d->x = s0->x - s1->x;
252 d->y = s0->y - s1->y;
253 d->z = s0->z - s1->z;
254 d->w = s0->w - s1->w;
258 * Version 1.1 specific
261 void vshader_exp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
262 d->x = d->y = d->z = d->w = powf(2.0f, s0->w);
265 void vshader_log(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
266 float tmp_f = fabsf(s0->w);
267 d->x = d->y = d->z = d->w = (0.0f != tmp_f) ? logf(tmp_f) / logf(2.0f) : -HUGE;
270 void vshader_frc(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0) {
271 d->x = s0->x - floorf(s0->x);
272 d->y = s0->y - floorf(s0->y);
277 typedef FLOAT D3DMATRIX44[4][4];
278 typedef FLOAT D3DMATRIX43[4][3];
279 typedef FLOAT D3DMATRIX34[4][4];
280 typedef FLOAT D3DMATRIX33[4][3];
281 typedef FLOAT D3DMATRIX32[4][2];
283 void vshader_m4x4(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, /*D3DSHADERVECTOR* mat1*/ D3DMATRIX44 mat) {
285 * BuGGY CODE: here only if cast not work for copy/paste
286 D3DSHADERVECTOR* mat2 = mat1 + 1;
287 D3DSHADERVECTOR* mat3 = mat1 + 2;
288 D3DSHADERVECTOR* mat4 = mat1 + 3;
289 d->x = mat1->x * s0->x + mat2->x * s0->y + mat3->x * s0->z + mat4->x * s0->w;
290 d->y = mat1->y * s0->x + mat2->y * s0->y + mat3->y * s0->z + mat4->y * s0->w;
291 d->z = mat1->z * s0->x + mat2->z * s0->y + mat3->z * s0->z + mat4->z * s0->w;
292 d->w = mat1->w * s0->x + mat2->w * s0->y + mat3->w * s0->z + mat4->w * s0->w;
294 d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z + mat[0][3] * s0->w;
295 d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z + mat[1][3] * s0->w;
296 d->z = mat[2][0] * s0->x + mat[2][1] * s0->y + mat[2][2] * s0->z + mat[2][3] * s0->w;
297 d->w = mat[3][0] * s0->x + mat[3][1] * s0->y + mat[3][2] * s0->z + mat[3][3] * s0->w;
300 void vshader_m4x3(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DMATRIX43 mat) {
302 d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z + mat[0][3] * s0->w;
303 d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z + mat[1][3] * s0->w;
304 d->z = mat[2][0] * s0->x + mat[2][1] * s0->y + mat[2][2] * s0->z + mat[2][3] * s0->w;
308 void vshader_m3x4(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DMATRIX34 mat) {
310 d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z;
311 d->y = mat[2][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z;
312 d->z = mat[2][0] * s0->x + mat[2][1] * s0->y + mat[2][2] * s0->z;
313 d->w = mat[3][0] * s0->x + mat[3][1] * s0->y + mat[3][2] * s0->z;
316 void vshader_m3x3(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DMATRIX33 mat) {
318 d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[2][2] * s0->z;
319 d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[2][2] * s0->z;
320 d->z = mat[2][0] * s0->x + mat[2][1] * s0->y + mat[2][2] * s0->z;
324 void vshader_m3x2(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DMATRIX32 mat) {
326 d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z;
327 d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z;
333 * Version 2.0 specific
335 void vshader_lrp(D3DSHADERVECTOR* d, D3DSHADERVECTOR* s0, D3DSHADERVECTOR* s1, D3DSHADERVECTOR* s2, D3DSHADERVECTOR* s3) {
336 d->x = s0->x * (s1->x - s2->x) + s2->x;
337 d->y = s0->y * (s1->y - s2->y) + s2->y;
338 d->z = s0->z * (s1->z - s2->z) + s2->z;
339 d->w = s0->w * (s1->w - s2->w) + s2->x;
343 * log, exp, frc, m*x* seems to be macros ins ... to see
345 static CONST SHADER_OPCODE vshader_ins [] = {
346 {D3DSIO_NOP, "nop", 0, vshader_nop},
347 {D3DSIO_MOV, "mov", 2, vshader_mov},
348 {D3DSIO_ADD, "add", 3, vshader_add},
349 {D3DSIO_SUB, "sub", 3, vshader_sub},
350 {D3DSIO_MAD, "mad", 4, vshader_mad},
351 {D3DSIO_MUL, "mul", 3, vshader_mul},
352 {D3DSIO_RCP, "rcp", 2, vshader_rcp},
353 {D3DSIO_RSQ, "rsq", 2, vshader_rsq},
354 {D3DSIO_DP3, "dp3", 3, vshader_dp3},
355 {D3DSIO_DP4, "dp4", 3, vshader_dp4},
356 {D3DSIO_MIN, "min", 3, vshader_min},
357 {D3DSIO_MAX, "max", 3, vshader_max},
358 {D3DSIO_SLT, "slt", 3, vshader_slt},
359 {D3DSIO_SGE, "sge", 3, vshader_sge},
360 {D3DSIO_EXP, "exp", 2, vshader_exp},
361 {D3DSIO_LOG, "log", 2, vshader_log},
362 {D3DSIO_LIT, "lit", 2, vshader_lit},
363 {D3DSIO_DST, "dst", 3, vshader_dst},
364 {D3DSIO_LRP, "lrp", 5, vshader_lrp},
365 {D3DSIO_FRC, "frc", 2, vshader_frc},
366 {D3DSIO_M4x4, "m4x4", 3, vshader_m4x4},
367 {D3DSIO_M4x3, "m4x3", 3, vshader_m4x3},
368 {D3DSIO_M3x4, "m3x4", 3, vshader_m3x4},
369 {D3DSIO_M3x3, "m3x3", 3, vshader_m3x3},
370 {D3DSIO_M3x2, "m3x2", 3, vshader_m3x2},
371 /** FIXME: use direct acces so add the others opcodes as stubs */
372 {D3DSIO_EXPP, "expp", 2, vshader_expp},
373 {D3DSIO_LOGP, "logp", 2, vshader_logp},
379 const SHADER_OPCODE* vshader_program_get_opcode(const DWORD code) {
381 /** TODO: use dichotomic search */
382 while (NULL != vshader_ins[i].name) {
383 if ((code & D3DSI_OPCODE_MASK) == vshader_ins[i].opcode) {
384 return &vshader_ins[i];
391 void vshader_program_dump_param(const DWORD param, int input) {
392 static const char* rastout_reg_names[] = { "oPos", "oFog", "oPts" };
393 static const char swizzle_reg_chars[] = "xyzw";
395 DWORD reg = param & 0x00001FFF;
396 DWORD regtype = ((param & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT);
398 if ((param & D3DSP_SRCMOD_MASK) == D3DSPSM_NEG) DPRINTF("-");
400 switch (regtype << D3DSP_REGTYPE_SHIFT) {
402 DPRINTF("R[%lu]", reg);
405 DPRINTF("V[%lu]", reg);
408 DPRINTF("C[%s%lu]", (reg & D3DVS_ADDRMODE_RELATIVE) ? "a0.x + " : "", reg);
410 case D3DSPR_ADDR: /*case D3DSPR_TEXTURE:*/
411 DPRINTF("a[%lu]", reg);
414 DPRINTF("%s", rastout_reg_names[reg]);
417 DPRINTF("oD[%lu]", reg);
419 case D3DSPR_TEXCRDOUT:
420 DPRINTF("oT[%lu]", reg);
427 /** operand output */
428 if ((param & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
429 if (param & D3DSP_WRITEMASK_0) DPRINTF(".x");
430 if (param & D3DSP_WRITEMASK_1) DPRINTF(".y");
431 if (param & D3DSP_WRITEMASK_2) DPRINTF(".z");
432 if (param & D3DSP_WRITEMASK_3) DPRINTF(".w");
436 DWORD swizzle = (param & D3DVS_SWIZZLE_MASK) >> D3DVS_SWIZZLE_SHIFT;
437 DWORD swizzle_x = swizzle & 0x03;
438 DWORD swizzle_y = (swizzle >> 2) & 0x03;
439 DWORD swizzle_z = (swizzle >> 4) & 0x03;
440 DWORD swizzle_w = (swizzle >> 6) & 0x03;
442 * swizzle bits fields:
445 if ((D3DVS_NOSWIZZLE >> D3DVS_SWIZZLE_SHIFT) != swizzle) { /* ! D3DVS_NOSWIZZLE == 0xE4 << D3DVS_SWIZZLE_SHIFT */
446 if (swizzle_x == swizzle_y &&
447 swizzle_x == swizzle_z &&
448 swizzle_x == swizzle_w) {
449 DPRINTF(".%c", swizzle_reg_chars[swizzle_x]);
452 swizzle_reg_chars[swizzle_x],
453 swizzle_reg_chars[swizzle_y],
454 swizzle_reg_chars[swizzle_z],
455 swizzle_reg_chars[swizzle_w]);
461 inline static BOOL vshader_is_version_token(DWORD token) {
462 return 0xFFFE0000 == (token & 0xFFFE0000);
465 inline static BOOL vshader_is_comment_token(DWORD token) {
466 return D3DSIO_COMMENT == (token & D3DSI_OPCODE_MASK);
470 * Function parser ...
472 DWORD vshader_program_parse(VERTEXSHADER8* vshader) {
473 const DWORD* pToken = vshader->function;
474 const SHADER_OPCODE* curOpcode = NULL;
478 if (NULL != pToken) {
479 while (D3DVS_END() != *pToken) {
480 if (vshader_is_version_token(*pToken)) { /** version */
481 DPRINTF("vs.%lu.%lu\n", (*pToken >> 8) & 0x0F, (*pToken & 0x0F));
486 if (vshader_is_comment_token(*pToken)) { /** comment */
487 DWORD comment_len = (*pToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
489 /*DPRINTF("comment[%ld] ;%s\n", comment_len, (char*)pToken);*/
490 pToken += comment_len;
491 len += comment_len + 1;
494 curOpcode = vshader_program_get_opcode(*pToken);
497 if (NULL == curOpcode) {
498 /* unkown current opcode ... */
499 while (*pToken & 0x80000000) {
500 DPRINTF("unrecognized opcode: %08lx\n", *pToken);
505 DPRINTF("%s ", curOpcode->name);
506 if (curOpcode->num_params > 0) {
507 vshader_program_dump_param(*pToken, 0);
510 for (i = 1; i < curOpcode->num_params; ++i) {
512 vshader_program_dump_param(*pToken, 1);
520 vshader->functionLength = (len + 1) * sizeof(DWORD);
522 vshader->functionLength = 1; /* no Function defined use fixed function vertex processing */
524 return len * sizeof(DWORD);
527 BOOL vshader_program_execute_HAL(VERTEXSHADER8* vshader,
528 VSHADERINPUTDATA8* input,
529 VSHADEROUTPUTDATA8* output) {
531 * TODO: use the NV_vertex_program (or 1_1) extension
532 * and specifics vendors (ARB_vertex_program??) variants for it
537 #define TRACE_VECTOR(name) DPRINTF( #name "=(%f, %f, %f, %f)\n", name.x, name.y, name.z, name.w)
539 BOOL vshader_program_execute_SW(VERTEXSHADER8* vshader,
540 VSHADERINPUTDATA8* input,
541 VSHADEROUTPUTDATA8* output) {
542 /** Vertex Shader Temporary Registers */
543 D3DSHADERVECTOR R[12];
544 /*D3DSHADERSCALAR A0;*/
545 D3DSHADERVECTOR A[1];
546 /** temporary Vector for modifier management */
548 D3DSHADERVECTOR s[3];
550 const DWORD* pToken = vshader->function;
551 const SHADER_OPCODE* curOpcode = NULL;
552 /** functions parameters */
553 D3DSHADERVECTOR* p[4];
554 D3DSHADERVECTOR* p_send[4];
557 /** init temporary register */
558 memset(R, 0, 12 * sizeof(D3DSHADERVECTOR));
560 /* vshader_program_parse(vshader); */
562 TRACE_VECTOR(vshader->data->C[0]);
563 TRACE_VECTOR(vshader->data->C[1]);
564 TRACE_VECTOR(vshader->data->C[2]);
565 TRACE_VECTOR(vshader->data->C[3]);
566 TRACE_VECTOR(vshader->data->C[4]);
567 TRACE_VECTOR(vshader->data->C[5]);
568 TRACE_VECTOR(input->V[D3DVSDE_POSITION]);
569 TRACE_VECTOR(input->V[D3DVSDE_BLENDWEIGHT]);
570 TRACE_VECTOR(input->V[D3DVSDE_BLENDINDICES]);
571 TRACE_VECTOR(input->V[D3DVSDE_NORMAL]);
572 TRACE_VECTOR(input->V[D3DVSDE_PSIZE]);
573 TRACE_VECTOR(input->V[D3DVSDE_DIFFUSE]);
574 TRACE_VECTOR(input->V[D3DVSDE_SPECULAR]);
575 TRACE_VECTOR(input->V[D3DVSDE_TEXCOORD0]);
576 TRACE_VECTOR(input->V[D3DVSDE_TEXCOORD1]);
579 /* the first dword is the version tag */
582 if (vshader_is_version_token(*pToken)) { /** version */
585 while (D3DVS_END() != *pToken) {
586 if (vshader_is_comment_token(*pToken)) { /** comment */
587 DWORD comment_len = (*pToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
589 pToken += comment_len;
592 curOpcode = vshader_program_get_opcode(*pToken);
594 if (NULL == curOpcode) {
596 /* unkown current opcode ... */
597 while (*pToken & 0x80000000) {
599 DPRINTF("unrecognized opcode: pos=%d token=%08lX\n", (pToken - 1) - vshader->function, *(pToken - 1));
601 DPRINTF("unrecognized opcode param: pos=%d token=%08lX what=", pToken - vshader->function, *pToken);
602 vshader_program_dump_param(*pToken, i);
609 if (curOpcode->num_params > 0) {
610 /*DPRINTF(">> execting opcode: pos=%d opcode_name=%s token=%08lX\n", pToken - vshader->function, curOpcode->name, *pToken);*/
611 for (i = 0; i < curOpcode->num_params; ++i) {
612 DWORD reg = pToken[i] & 0x00001FFF;
613 DWORD regtype = ((pToken[i] & D3DSP_REGTYPE_MASK) >> D3DSP_REGTYPE_SHIFT);
615 switch (regtype << D3DSP_REGTYPE_SHIFT) {
617 /*DPRINTF("p[%d]=R[%d]\n", i, reg);*/
621 /*DPRINTF("p[%d]=V[%s]\n", i, VertexShaderDeclRegister[reg]);*/
622 p[i] = &input->V[reg];
625 if (reg & D3DVS_ADDRMODE_RELATIVE) {
626 p[i] = &vshader->data->C[(DWORD) A[0].x + reg];
628 p[i] = &vshader->data->C[reg];
631 case D3DSPR_ADDR: /*case D3DSPR_TEXTURE:*/
633 ERR("cannot handle address registers != a0, forcing use of a0\n");
636 /*DPRINTF("p[%d]=A[%d]\n", i, reg);*/
641 case D3DSRO_POSITION:
642 p[i] = &output->oPos;
645 p[i] = &output->oFog;
647 case D3DSRO_POINT_SIZE:
648 p[i] = &output->oPts;
653 /*DPRINTF("p[%d]=oD[%d]\n", i, reg);*/
654 p[i] = &output->oD[reg];
656 case D3DSPR_TEXCRDOUT:
657 /*DPRINTF("p[%d]=oT[%d]\n", i, reg);*/
658 p[i] = &output->oT[reg];
664 if (i > 0) { /* input reg */
665 DWORD swizzle = (pToken[i] & D3DVS_SWIZZLE_MASK) >> D3DVS_SWIZZLE_SHIFT;
666 UINT isNegative = ((*pToken & D3DSP_SRCMOD_MASK) == D3DSPSM_NEG);
668 if (!isNegative && (D3DVS_NOSWIZZLE >> D3DVS_SWIZZLE_SHIFT) == swizzle) {
669 /*DPRINTF("p[%d] not swizzled\n", i);*/
672 DWORD swizzle_x = swizzle & 0x03;
673 DWORD swizzle_y = (swizzle >> 2) & 0x03;
674 DWORD swizzle_z = (swizzle >> 4) & 0x03;
675 DWORD swizzle_w = (swizzle >> 6) & 0x03;
676 /*DPRINTF("p[%d] swizzled\n", i);*/
677 float* tt = (float*) p[i];
678 s[i].x = (isNegative) ? -tt[swizzle_x] : tt[swizzle_x];
679 s[i].y = (isNegative) ? -tt[swizzle_y] : tt[swizzle_y];
680 s[i].z = (isNegative) ? -tt[swizzle_z] : tt[swizzle_z];
681 s[i].w = (isNegative) ? -tt[swizzle_w] : tt[swizzle_w];
684 } else { /* output reg */
685 if ((pToken[i] & D3DSP_WRITEMASK_ALL) == D3DSP_WRITEMASK_ALL) {
688 p_send[i] = &d; /* to be post-processed for modifiers management */
694 switch (curOpcode->num_params) {
696 curOpcode->soft_fct();
699 curOpcode->soft_fct(p_send[0]);
702 curOpcode->soft_fct(p_send[0], p_send[1]);
705 curOpcode->soft_fct(p_send[0], p_send[1], p_send[2]);
708 curOpcode->soft_fct(p_send[0], p_send[1], p_send[2], p_send[3]);
711 curOpcode->soft_fct(p_send[0], p_send[1], p_send[2], p_send[3], p_send[4]);
714 ERR("%s too many params: %u\n", curOpcode->name, curOpcode->num_params);
717 /* check if output reg modifier post-process */
718 if (curOpcode->num_params > 0 && (pToken[0] & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
719 if (pToken[0] & D3DSP_WRITEMASK_0) p[0]->x = d.x;
720 if (pToken[0] & D3DSP_WRITEMASK_1) p[0]->y = d.y;
721 if (pToken[0] & D3DSP_WRITEMASK_2) p[0]->z = d.z;
722 if (pToken[0] & D3DSP_WRITEMASK_3) p[0]->w = d.w;
726 TRACE_VECTOR(output->oPos);
727 TRACE_VECTOR(output->oD[0]);
728 TRACE_VECTOR(output->oD[1]);
729 TRACE_VECTOR(output->oT[0]);
730 TRACE_VECTOR(output->oT[1]);
738 /* to next opcode token */
739 pToken += curOpcode->num_params;
745 /************************************
746 * Vertex Shader Declaration Parser First draft ...
749 /** todo check decl validity */
750 DWORD vshader_decl_parse_token(const DWORD* pToken) {
751 const DWORD token = *pToken;
754 switch ((token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT) { /* maybe a macro to inverse ... */
755 case D3DVSD_TOKEN_NOP:
756 TRACE(" 0x%08lx NOP()\n", token);
758 case D3DVSD_TOKEN_STREAM:
759 if (token & D3DVSD_STREAMTESSMASK) {
760 TRACE(" 0x%08lx STREAM_TESS()\n", token);
762 TRACE(" 0x%08lx STREAM(%lu)\n", token, ((token & D3DVSD_STREAMNUMBERMASK) >> D3DVSD_STREAMNUMBERSHIFT));
765 case D3DVSD_TOKEN_STREAMDATA:
766 if (token & 0x10000000) {
767 TRACE(" 0x%08lx SKIP(%lu)\n", token, ((token & D3DVSD_SKIPCOUNTMASK) >> D3DVSD_SKIPCOUNTSHIFT));
769 DWORD type = ((token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
770 DWORD reg = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
771 TRACE(" 0x%08lx REG(%s, %s)\n", token, VertexShaderDeclRegister[reg], VertexShaderDeclDataTypes[type]);
774 case D3DVSD_TOKEN_TESSELLATOR:
775 if (token & 0x10000000) {
776 DWORD type = ((token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
777 DWORD reg = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
778 TRACE(" 0x%08lx TESSUV(%s) as %s\n", token, VertexShaderDeclRegister[reg], VertexShaderDeclDataTypes[type]);
780 DWORD type = ((token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
781 DWORD regout = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
782 DWORD regin = ((token & D3DVSD_VERTEXREGINMASK) >> D3DVSD_VERTEXREGINSHIFT);
783 TRACE(" 0x%08lx TESSNORMAL(%s, %s) as %s\n", token, VertexShaderDeclRegister[regin], VertexShaderDeclRegister[regout], VertexShaderDeclDataTypes[type]);
786 case D3DVSD_TOKEN_CONSTMEM:
789 DWORD count = ((token & D3DVSD_CONSTCOUNTMASK) >> D3DVSD_CONSTCOUNTSHIFT);
790 DWORD constaddress = ((token & D3DVSD_CONSTADDRESSMASK) >> D3DVSD_CONSTADDRESSSHIFT);
791 TRACE(" 0x%08lx CONST(%lu, %lu)\n", token, constaddress, count);
793 for (i = 0; i < count; ++i) {
794 TRACE(" c[%lu] = (0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx)\n",
803 tokenlen = count + 1;
806 case D3DVSD_TOKEN_EXT:
808 DWORD count = ((token & D3DVSD_CONSTCOUNTMASK) >> D3DVSD_CONSTCOUNTSHIFT);
809 DWORD extinfo = ((token & D3DVSD_EXTINFOMASK) >> D3DVSD_EXTINFOSHIFT);
810 TRACE(" 0x%08lx EXT(%lu, %lu)\n", token, count, extinfo);
811 /* todo ... print extension */
812 tokenlen = count + 1;
815 case D3DVSD_TOKEN_END:
816 TRACE(" 0x%08lx END()\n", token);
819 TRACE(" 0x%08lx UNKNOWN\n", token);
825 DWORD vshader_decl_parse(VERTEXSHADER8* vshader) {
827 const DWORD* pToken = vshader->decl;
834 DWORD tex = D3DFVF_TEX0;
836 while (D3DVSD_END() != *pToken) {
838 tokenlen = vshader_decl_parse_token(pToken);
839 tokentype = ((token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT);
841 /** FVF generation block */
842 if (D3DVSD_TOKEN_STREAM == tokentype && 0 == (D3DVSD_STREAMTESSMASK & token)) {
844 * how really works streams,
845 * in DolphinVS dx8 dsk sample they seems to decal reg numbers !!!
847 stream = ((token & D3DVSD_STREAMNUMBERMASK) >> D3DVSD_STREAMNUMBERSHIFT);
849 } else if (D3DVSD_TOKEN_STREAMDATA == tokentype && 0 == (0x10000000 & tokentype)) {
850 DWORD type = ((token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
851 DWORD reg = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT) - stream;
854 case D3DVSDE_POSITION:
856 case D3DVSDT_FLOAT3: fvf |= D3DFVF_XYZ; break;
857 case D3DVSDT_FLOAT4: fvf |= D3DFVF_XYZRHW; break;
858 default: /** errooooorr what to do ? */
859 ERR("Error in VertexShader declaration of D3DVSDE_POSITION register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
863 case D3DVSDE_BLENDWEIGHT:
865 case D3DVSDT_FLOAT1: fvf |= D3DFVF_XYZB1; break;
866 case D3DVSDT_FLOAT2: fvf |= D3DFVF_XYZB2; break;
867 case D3DVSDT_FLOAT3: fvf |= D3DFVF_XYZB3; break;
868 case D3DVSDT_FLOAT4: fvf |= D3DFVF_XYZB4; break;
869 default: /** errooooorr what to do ? */
870 ERR("Error in VertexShader declaration of D3DVSDE_BLENDWEIGHT register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
874 case D3DVSDE_BLENDINDICES: /* seem to be B5 as said in MSDN Dx9SDK ?? */
876 case D3DVSDT_UBYTE4: fvf |= D3DFVF_LASTBETA_UBYTE4; break;
877 default: /** errooooorr what to do ? */
878 ERR("Error in VertexShader declaration of D3DVSDE_BLENDINDINCES register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
882 case D3DVSDE_NORMAL: /* TODO: only FLOAT3 supported ... another choice possible ? */
884 case D3DVSDT_FLOAT3: fvf |= D3DFVF_NORMAL; break;
885 default: /** errooooorr what to do ? */
886 ERR("Error in VertexShader declaration of D3DVSDE_NORMAL register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
890 case D3DVSDE_PSIZE: /* TODO: only FLOAT1 supported ... another choice possible ? */
892 case D3DVSDT_FLOAT1: fvf |= D3DFVF_PSIZE; break;
893 default: /** errooooorr what to do ? */
894 ERR("Error in VertexShader declaration of D3DVSDE_PSIZE register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
898 case D3DVSDE_DIFFUSE: /* TODO: only D3DCOLOR supported */
900 case D3DVSDT_D3DCOLOR: fvf |= D3DFVF_DIFFUSE; break;
901 default: /** errooooorr what to do ? */
902 ERR("Error in VertexShader declaration of D3DVSDE_DIFFUSE register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
906 case D3DVSDE_SPECULAR: /* TODO: only D3DCOLOR supported */
908 case D3DVSDT_D3DCOLOR: fvf |= D3DFVF_SPECULAR; break;
909 default: /** errooooorr what to do ? */
910 ERR("Error in VertexShader declaration of D3DVSDE_SPECULAR register: unsupported type %s\n", VertexShaderDeclDataTypes[type]);
915 * TODO: for TEX* only FLOAT2 supported
916 * by default using texture type info
918 case D3DVSDE_TEXCOORD0: tex = max(tex, D3DFVF_TEX1); break;
919 case D3DVSDE_TEXCOORD1: tex = max(tex, D3DFVF_TEX2); break;
920 case D3DVSDE_TEXCOORD2: tex = max(tex, D3DFVF_TEX3); break;
921 case D3DVSDE_TEXCOORD3: tex = max(tex, D3DFVF_TEX4); break;
922 case D3DVSDE_TEXCOORD4: tex = max(tex, D3DFVF_TEX5); break;
923 case D3DVSDE_TEXCOORD5: tex = max(tex, D3DFVF_TEX6); break;
924 case D3DVSDE_TEXCOORD6: tex = max(tex, D3DFVF_TEX7); break;
925 case D3DVSDE_TEXCOORD7: tex = max(tex, D3DFVF_TEX8); break;
926 case D3DVSDE_POSITION2: /* maybe D3DFVF_XYZRHW instead D3DFVF_XYZ (of D3DVDE_POSITION) ... to see */
927 case D3DVSDE_NORMAL2: /* FIXME i don't know what to do here ;( */
928 FIXME("[%lu] registers in VertexShader declaration not supported yet (token:0x%08lx)\n", reg, token);
931 /*TRACE("VertexShader declaration define %x as current FVF\n", fvf);*/
937 /*TRACE("VertexShader declaration define %x as texture level\n", tex);*/
940 /* here D3DVSD_END() */
941 len += vshader_decl_parse_token(pToken);
943 vshader->declLength = len * sizeof(DWORD);
944 return len * sizeof(DWORD);
948 void vshader_fill_input(VERTEXSHADER8* vshader,
949 IDirect3DDevice8Impl* device,
950 const void* vertexFirstStream,
951 DWORD StartVertexIndex,
954 const DWORD* pToken = vshader->decl;
959 /** for input readers */
960 const char* curPos = NULL;
965 /*TRACE("(%p) - device:%p - stream:%p, startIdx=%lu, idxDecal=%lu\n", vshader, device, vertexFirstStream, StartVertexIndex, idxDecal);*/
966 while (D3DVSD_END() != *pToken) {
968 tokentype = ((token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT);
970 /** FVF generation block */
971 if (D3DVSD_TOKEN_STREAM == tokentype && 0 == (D3DVSD_STREAMTESSMASK & token)) {
972 IDirect3DVertexBuffer8* pVB;
973 const char* startVtx = NULL;
978 * how really works streams,
979 * in DolphinVS dx8 dsk sample use it !!!
981 stream = ((token & D3DVSD_STREAMNUMBERMASK) >> D3DVSD_STREAMNUMBERSHIFT);
984 skip = device->StateBlock.stream_stride[0];
985 startVtx = (const char *)vertexFirstStream + (StartVertexIndex * skip);
986 curPos = startVtx + idxDecal;
987 /*TRACE(" using stream[%lu] with %lu decal => curPos %p\n", stream, idxDecal, curPos);*/
989 skip = device->StateBlock.stream_stride[stream];
990 pVB = device->StateBlock.stream_source[stream];
993 ERR("using unitialised stream[%lu]\n", stream);
996 startVtx = ((IDirect3DVertexBuffer8Impl*) pVB)->allocatedMemory + (StartVertexIndex * skip);
997 /** do we need to decal if we use idxBuffer */
998 curPos = startVtx + idxDecal;
999 /*TRACE(" using stream[%lu] with %lu decal\n", stream, idxDecal);*/
1002 } else if (D3DVSD_TOKEN_CONSTMEM == tokentype) {
1005 DWORD count = ((token & D3DVSD_CONSTCOUNTMASK) >> D3DVSD_CONSTCOUNTSHIFT);
1006 DWORD constaddress = ((token & D3DVSD_CONSTADDRESSMASK) >> D3DVSD_CONSTADDRESSSHIFT);
1008 for (i = 0; i < count; ++i) {
1009 vshader->data->C[constaddress + i].x = *(float*)pToken;
1010 vshader->data->C[constaddress + i].y = *(float*)(pToken + 1);
1011 vshader->data->C[constaddress + i].z = *(float*)(pToken + 2);
1012 vshader->data->C[constaddress + i].w = *(float*)(pToken + 3);
1016 } else if (D3DVSD_TOKEN_STREAMDATA == tokentype && 0 != (0x10000000 & tokentype)) {
1018 DWORD skipCount = ((token & D3DVSD_SKIPCOUNTMASK) >> D3DVSD_SKIPCOUNTSHIFT);
1019 curPos = curPos + skipCount * sizeof(DWORD);
1022 } else if (D3DVSD_TOKEN_STREAMDATA == tokentype && 0 == (0x10000000 & tokentype)) {
1023 DWORD type = ((token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
1024 DWORD reg = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
1028 case D3DVSDT_FLOAT1:
1029 x = *(float*) curPos;
1030 curPos = curPos + sizeof(float);
1032 vshader->input.V[reg].x = x;
1033 vshader->input.V[reg].y = 0.0f;
1034 vshader->input.V[reg].z = 0.0f;
1035 vshader->input.V[reg].w = 1.0f;
1038 case D3DVSDT_FLOAT2:
1039 x = *(float*) curPos;
1040 curPos = curPos + sizeof(float);
1041 y = *(float*) curPos;
1042 curPos = curPos + sizeof(float);
1044 vshader->input.V[reg].x = x;
1045 vshader->input.V[reg].y = y;
1046 vshader->input.V[reg].z = 0.0f;
1047 vshader->input.V[reg].w = 1.0f;
1050 case D3DVSDT_FLOAT3:
1051 x = *(float*) curPos;
1052 curPos = curPos + sizeof(float);
1053 y = *(float*) curPos;
1054 curPos = curPos + sizeof(float);
1055 z = *(float*) curPos;
1056 curPos = curPos + sizeof(float);
1058 vshader->input.V[reg].x = x;
1059 vshader->input.V[reg].y = y;
1060 vshader->input.V[reg].z = z;
1061 vshader->input.V[reg].w = 1.0f;
1064 case D3DVSDT_FLOAT4:
1065 x = *(float*) curPos;
1066 curPos = curPos + sizeof(float);
1067 y = *(float*) curPos;
1068 curPos = curPos + sizeof(float);
1069 z = *(float*) curPos;
1070 curPos = curPos + sizeof(float);
1071 w = *(float*) curPos;
1072 curPos = curPos + sizeof(float);
1074 vshader->input.V[reg].x = x;
1075 vshader->input.V[reg].y = y;
1076 vshader->input.V[reg].z = z;
1077 vshader->input.V[reg].w = w;
1080 case D3DVSDT_D3DCOLOR:
1081 dw = *(DWORD*) curPos;
1082 curPos = curPos + sizeof(DWORD);
1084 vshader->input.V[reg].x = (float) (((dw >> 16) & 0xFF) / 255.0f);
1085 vshader->input.V[reg].y = (float) (((dw >> 8) & 0xFF) / 255.0f);
1086 vshader->input.V[reg].z = (float) (((dw >> 0) & 0xFF) / 255.0f);
1087 vshader->input.V[reg].w = (float) (((dw >> 24) & 0xFF) / 255.0f);
1090 case D3DVSDT_SHORT2:
1091 u = *(SHORT*) curPos;
1092 curPos = curPos + sizeof(SHORT);
1093 v = *(SHORT*) curPos;
1094 curPos = curPos + sizeof(SHORT);
1096 vshader->input.V[reg].x = (float) u;
1097 vshader->input.V[reg].y = (float) v;
1098 vshader->input.V[reg].z = 0.0f;
1099 vshader->input.V[reg].w = 1.0f;
1102 case D3DVSDT_SHORT4:
1103 u = *(SHORT*) curPos;
1104 curPos = curPos + sizeof(SHORT);
1105 v = *(SHORT*) curPos;
1106 curPos = curPos + sizeof(SHORT);
1107 r = *(SHORT*) curPos;
1108 curPos = curPos + sizeof(SHORT);
1109 t = *(SHORT*) curPos;
1110 curPos = curPos + sizeof(SHORT);
1112 vshader->input.V[reg].x = (float) u;
1113 vshader->input.V[reg].y = (float) v;
1114 vshader->input.V[reg].z = (float) r;
1115 vshader->input.V[reg].w = (float) t;
1118 case D3DVSDT_UBYTE4:
1119 dw = *(DWORD*) curPos;
1120 curPos = curPos + sizeof(DWORD);
1122 vshader->input.V[reg].x = (float) ((dw & 0x000F) >> 0);
1123 vshader->input.V[reg].y = (float) ((dw & 0x00F0) >> 8);
1124 vshader->input.V[reg].z = (float) ((dw & 0x0F00) >> 16);
1125 vshader->input.V[reg].w = (float) ((dw & 0xF000) >> 24);
1129 default: /** errooooorr what to do ? */
1130 ERR("Error in VertexShader declaration of %s register: unsupported type %s\n", VertexShaderDeclRegister[reg], VertexShaderDeclDataTypes[type]);
1135 /* here D3DVSD_END() */
1139 /***********************************************************************
1140 * ValidateVertexShader (D3D8.@)
1142 BOOL WINAPI ValidateVertexShader(LPVOID what, LPVOID toto) {
1143 FIXME("(void): stub: %p %p\n", what, toto);
1147 /***********************************************************************
1148 * ValidatePixelShader (D3D8.@)
1150 BOOL WINAPI ValidatePixelShader(LPVOID what) {
1151 FIXME("(void): stub: %p\n", what);