2 * Pixel and vertex shaders implementation using ARB_vertex_program
3 * and ARB_fragment_program GL extensions.
5 * Copyright 2002-2003 Jason Edmeades
6 * Copyright 2002-2003 Raphael Junqueira
7 * Copyright 2004 Christian Costa
8 * Copyright 2005 Oliver Stieber
9 * Copyright 2006 Ivan Gyurdiev
10 * Copyright 2006 Jason Green
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
32 #include "wined3d_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
36 #define GLINFO_LOCATION (*gl_info)
38 /********************************************************
39 * ARB_[vertex/fragment]_program helper functions follow
40 ********************************************************/
43 * Loads floating point constants into the currently set ARB_vertex/fragment_program.
44 * When @constants_set == NULL, it will load all the constants.
46 * @target_type should be either GL_VERTEX_PROGRAM_ARB (for vertex shaders)
47 * or GL_FRAGMENT_PROGRAM_ARB (for pixel shaders)
49 void shader_arb_load_constantsF(
50 IWineD3DBaseShaderImpl* This,
51 WineD3D_GL_Info *gl_info,
53 unsigned max_constants,
55 BOOL* constants_set) {
60 for (i=0; i<max_constants; ++i) {
61 if (NULL == constants_set || constants_set[i]) {
62 TRACE("Loading constants %i: %f, %f, %f, %f\n", i,
63 constants[i * sizeof(float) + 0], constants[i * sizeof(float) + 1],
64 constants[i * sizeof(float) + 2], constants[i * sizeof(float) + 3]);
66 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, i, &constants[i * sizeof(float)]));
67 checkGLcall("glProgramEnvParameter4fvARB");
71 /* Load immediate constants */
72 ptr = list_head(&This->baseShader.constantsF);
75 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
76 unsigned int idx = lconst->idx;
77 GLfloat* values = (GLfloat*) lconst->value;
79 TRACE("Loading local constants %i: %f, %f, %f, %f\n", idx,
80 values[0], values[1], values[2], values[3]);
82 GL_EXTCALL(glProgramEnvParameter4fvARB(target_type, idx, values));
83 checkGLcall("glProgramEnvParameter4fvARB");
84 ptr = list_next(&This->baseShader.constantsF, ptr);
89 * Loads the app-supplied constants into the currently set ARB_[vertex/fragment]_programs.
91 * We only support float constants in ARB at the moment, so don't
92 * worry about the Integers or Booleans
94 void shader_arb_load_constants(
95 IWineD3DStateBlock* iface,
97 char useVertexShader) {
99 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
100 WineD3D_GL_Info *gl_info = &((IWineD3DImpl*)stateBlock->wineD3DDevice->wineD3D)->gl_info;
102 if (useVertexShader) {
103 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
104 IWineD3DVertexShaderImpl* vshader_impl = (IWineD3DVertexShaderImpl*) stateBlock->vertexShader;
105 IWineD3DVertexDeclarationImpl* vertexDeclaration =
106 (IWineD3DVertexDeclarationImpl*) vshader_impl->vertexDeclaration;
108 if (NULL != vertexDeclaration && NULL != vertexDeclaration->constants) {
109 /* Load DirectX 8 float constants for vertex shader */
110 shader_arb_load_constantsF(vshader, gl_info, GL_VERTEX_PROGRAM_ARB,
111 GL_LIMITS(vshader_constantsF),
112 vertexDeclaration->constants, NULL);
115 /* Load DirectX 9 float constants for vertex shader */
116 shader_arb_load_constantsF(vshader, gl_info, GL_VERTEX_PROGRAM_ARB,
117 GL_LIMITS(vshader_constantsF),
118 stateBlock->vertexShaderConstantF,
119 stateBlock->set.vertexShaderConstantsF);
122 if (usePixelShader) {
124 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
126 /* Load DirectX 9 float constants for pixel shader */
127 shader_arb_load_constantsF(pshader, gl_info, GL_FRAGMENT_PROGRAM_ARB,
128 GL_LIMITS(pshader_constantsF),
129 stateBlock->pixelShaderConstantF,
130 stateBlock->set.pixelShaderConstantsF);
134 /* Generate the variable & register declarations for the ARB_vertex_program output target */
135 void shader_generate_arb_declarations(
136 IWineD3DBaseShader *iface,
137 shader_reg_maps* reg_maps,
138 SHADER_BUFFER* buffer,
139 WineD3D_GL_Info* gl_info) {
141 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
143 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
144 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
145 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
147 for(i = 0; i < This->baseShader.limits.temporary; i++) {
148 if (reg_maps->temporary[i])
149 shader_addline(buffer, "TEMP R%lu;\n", i);
152 for (i = 0; i < This->baseShader.limits.address; i++) {
153 if (reg_maps->address[i])
154 shader_addline(buffer, "ADDRESS A%ld;\n", i);
157 for(i = 0; i < This->baseShader.limits.texcoord; i++) {
158 if (reg_maps->texcoord[i])
159 shader_addline(buffer,"TEMP T%lu;\n", i);
162 /* Texture coordinate registers must be pre-loaded */
163 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
164 if (reg_maps->texcoord[i])
165 shader_addline(buffer, "MOV T%lu, fragment.texcoord[%lu];\n", i, i);
168 /* Need to PARAM the environment parameters (constants) so we can use relative addressing */
169 shader_addline(buffer, "PARAM C[%d] = { program.env[0..%d] };\n",
170 max_constantsF, max_constantsF - 1);
173 static const char* shift_tab[] = {
174 "dummy", /* 0 (none) */
175 "coefmul.x", /* 1 (x2) */
176 "coefmul.y", /* 2 (x4) */
177 "coefmul.z", /* 3 (x8) */
178 "coefmul.w", /* 4 (x16) */
179 "dummy", /* 5 (x32) */
180 "dummy", /* 6 (x64) */
181 "dummy", /* 7 (x128) */
182 "dummy", /* 8 (d256) */
183 "dummy", /* 9 (d128) */
184 "dummy", /* 10 (d64) */
185 "dummy", /* 11 (d32) */
186 "coefdiv.w", /* 12 (d16) */
187 "coefdiv.z", /* 13 (d8) */
188 "coefdiv.y", /* 14 (d4) */
189 "coefdiv.x" /* 15 (d2) */
192 static void pshader_get_write_mask(const DWORD output_reg, char *write_mask) {
194 if ((output_reg & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
195 strcat(write_mask, ".");
196 if (output_reg & D3DSP_WRITEMASK_0) strcat(write_mask, "r");
197 if (output_reg & D3DSP_WRITEMASK_1) strcat(write_mask, "g");
198 if (output_reg & D3DSP_WRITEMASK_2) strcat(write_mask, "b");
199 if (output_reg & D3DSP_WRITEMASK_3) strcat(write_mask, "a");
203 /* TODO: merge with pixel shader */
204 static void vshader_program_add_output_param_swizzle(const DWORD param, int is_color, char *hwLine) {
205 /** operand output */
206 if ((param & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
208 if (param & D3DSP_WRITEMASK_0) { strcat(hwLine, "x"); }
209 if (param & D3DSP_WRITEMASK_1) { strcat(hwLine, "y"); }
210 if (param & D3DSP_WRITEMASK_2) { strcat(hwLine, "z"); }
211 if (param & D3DSP_WRITEMASK_3) { strcat(hwLine, "w"); }
215 static void pshader_get_input_register_swizzle(const DWORD instr, char *swzstring) {
216 static const char swizzle_reg_chars[] = "rgba";
217 DWORD swizzle = (instr & D3DSP_SWIZZLE_MASK) >> D3DSP_SWIZZLE_SHIFT;
218 DWORD swizzle_x = swizzle & 0x03;
219 DWORD swizzle_y = (swizzle >> 2) & 0x03;
220 DWORD swizzle_z = (swizzle >> 4) & 0x03;
221 DWORD swizzle_w = (swizzle >> 6) & 0x03;
223 * swizzle bits fields:
227 if ((D3DSP_NOSWIZZLE >> D3DSP_SWIZZLE_SHIFT) != swizzle) { /* ! D3DVS_NOSWIZZLE == 0xE4 << D3DVS_SWIZZLE_SHIFT */
228 if (swizzle_x == swizzle_y &&
229 swizzle_x == swizzle_z &&
230 swizzle_x == swizzle_w) {
231 sprintf(swzstring, ".%c", swizzle_reg_chars[swizzle_x]);
233 sprintf(swzstring, ".%c%c%c%c",
234 swizzle_reg_chars[swizzle_x],
235 swizzle_reg_chars[swizzle_y],
236 swizzle_reg_chars[swizzle_z],
237 swizzle_reg_chars[swizzle_w]);
242 /* TODO: merge with pixel shader */
243 static void vshader_program_add_input_param_swizzle(const DWORD param, int is_color, char *hwLine) {
244 static const char swizzle_reg_chars_color_fix[] = "zyxw";
245 static const char swizzle_reg_chars[] = "xyzw";
246 const char* swizzle_regs = NULL;
250 DWORD swizzle = (param & D3DVS_SWIZZLE_MASK) >> D3DVS_SWIZZLE_SHIFT;
251 DWORD swizzle_x = swizzle & 0x03;
252 DWORD swizzle_y = (swizzle >> 2) & 0x03;
253 DWORD swizzle_z = (swizzle >> 4) & 0x03;
254 DWORD swizzle_w = (swizzle >> 6) & 0x03;
257 swizzle_regs = swizzle_reg_chars_color_fix;
259 swizzle_regs = swizzle_reg_chars;
263 * swizzle bits fields:
266 if ((D3DVS_NOSWIZZLE >> D3DVS_SWIZZLE_SHIFT) == swizzle) { /* D3DVS_NOSWIZZLE == 0xE4 << D3DVS_SWIZZLE_SHIFT */
268 sprintf(tmpReg, ".%c%c%c%c",
269 swizzle_regs[swizzle_x],
270 swizzle_regs[swizzle_y],
271 swizzle_regs[swizzle_z],
272 swizzle_regs[swizzle_w]);
273 strcat(hwLine, tmpReg);
277 if (swizzle_x == swizzle_y &&
278 swizzle_x == swizzle_z &&
279 swizzle_x == swizzle_w)
281 sprintf(tmpReg, ".%c", swizzle_regs[swizzle_x]);
282 strcat(hwLine, tmpReg);
284 sprintf(tmpReg, ".%c%c%c%c",
285 swizzle_regs[swizzle_x],
286 swizzle_regs[swizzle_y],
287 swizzle_regs[swizzle_z],
288 swizzle_regs[swizzle_w]);
289 strcat(hwLine, tmpReg);
293 static void pshader_get_register_name(
294 const DWORD param, char* regstr) {
296 DWORD reg = param & D3DSP_REGNUM_MASK;
297 DWORD regtype = shader_get_regtype(param);
301 sprintf(regstr, "R%lu", reg);
305 strcpy(regstr, "fragment.color.primary");
307 strcpy(regstr, "fragment.color.secondary");
311 sprintf(regstr, "C[%lu]", reg);
313 case D3DSPR_TEXTURE: /* case D3DSPR_ADDR: */
314 sprintf(regstr,"T%lu", reg);
316 case D3DSPR_COLOROUT:
318 sprintf(regstr, "result.color");
320 /* TODO: See GL_ARB_draw_buffers */
321 FIXME("Unsupported write to render target %lu\n", reg);
322 sprintf(regstr, "unsupported_register");
325 case D3DSPR_DEPTHOUT:
326 sprintf(regstr, "result.depth");
329 sprintf(regstr, "oD[%lu]", reg);
331 case D3DSPR_TEXCRDOUT:
332 sprintf(regstr, "oT[%lu]", reg);
335 FIXME("Unhandled register name Type(%ld)\n", regtype);
336 sprintf(regstr, "unrecognized_register");
341 /* TODO: merge with pixel shader */
342 static void vshader_program_add_param(SHADER_OPCODE_ARG *arg, const DWORD param, BOOL is_input, char *hwLine) {
344 IWineD3DVertexShaderImpl* This = (IWineD3DVertexShaderImpl*) arg->shader;
346 /* oPos, oFog and oPts in D3D */
347 static const char* hwrastout_reg_names[] = { "result.position", "TMP_FOG", "result.pointsize" };
349 DWORD reg = param & D3DSP_REGNUM_MASK;
350 DWORD regtype = shader_get_regtype(param);
352 BOOL is_color = FALSE;
354 if ((param & D3DSP_SRCMOD_MASK) == D3DSPSM_NEG) {
355 strcat(hwLine, " -");
362 sprintf(tmpReg, "R%lu", reg);
363 strcat(hwLine, tmpReg);
367 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
370 sprintf(tmpReg, "vertex.attrib[%lu]", reg);
371 strcat(hwLine, tmpReg);
374 sprintf(tmpReg, "C[%s%lu]", (param & D3DVS_ADDRMODE_RELATIVE) ? "A0.x + " : "", reg);
375 strcat(hwLine, tmpReg);
377 case D3DSPR_ADDR: /*case D3DSPR_TEXTURE:*/
378 sprintf(tmpReg, "A%lu", reg);
379 strcat(hwLine, tmpReg);
382 sprintf(tmpReg, "%s", hwrastout_reg_names[reg]);
383 strcat(hwLine, tmpReg);
387 strcat(hwLine, "result.color.primary");
389 strcat(hwLine, "result.color.secondary");
392 case D3DSPR_TEXCRDOUT:
393 sprintf(tmpReg, "result.texcoord[%lu]", reg);
394 strcat(hwLine, tmpReg);
397 FIXME("Unknown reg type %ld %ld\n", regtype, reg);
398 strcat(hwLine, "unrecognized_register");
403 vshader_program_add_output_param_swizzle(param, is_color, hwLine);
405 vshader_program_add_input_param_swizzle(param, is_color, hwLine);
409 static void pshader_gen_input_modifier_line (
410 SHADER_BUFFER* buffer,
415 /* Generate a line that does the input modifier computation and return the input register to use */
420 /* Assume a new line will be added */
423 /* Get register name */
424 pshader_get_register_name(instr, regstr);
425 pshader_get_input_register_swizzle(instr, swzstr);
427 switch (instr & D3DSP_SRCMOD_MASK) {
429 sprintf(outregstr, "%s%s", regstr, swzstr);
433 sprintf(outregstr, "-%s%s", regstr, swzstr);
437 shader_addline(buffer, "ADD T%c, %s, -coefdiv.x;\n", 'A' + tmpreg, regstr);
439 case D3DSPSM_BIASNEG:
440 shader_addline(buffer, "ADD T%c, -%s, coefdiv.x;\n", 'A' + tmpreg, regstr);
443 shader_addline(buffer, "MAD T%c, %s, coefmul.x, -one.x;\n", 'A' + tmpreg, regstr);
445 case D3DSPSM_SIGNNEG:
446 shader_addline(buffer, "MAD T%c, %s, -coefmul.x, one.x;\n", 'A' + tmpreg, regstr);
449 shader_addline(buffer, "SUB T%c, one.x, %s;\n", 'A' + tmpreg, regstr);
452 shader_addline(buffer, "ADD T%c, %s, %s;\n", 'A' + tmpreg, regstr, regstr);
455 shader_addline(buffer, "ADD T%c, -%s, -%s;\n", 'A' + tmpreg, regstr, regstr);
458 shader_addline(buffer, "RCP T%c, %s.z;\n", 'A' + tmpreg, regstr);
459 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
462 shader_addline(buffer, "RCP T%c, %s.w;\n", 'A' + tmpreg, regstr);
463 shader_addline(buffer, "MUL T%c, %s, T%c;\n", 'A' + tmpreg, regstr, 'A' + tmpreg);
466 sprintf(outregstr, "%s%s", regstr, swzstr);
470 /* Return modified or original register, with swizzle */
472 sprintf(outregstr, "T%c%s", 'A' + tmpreg, swzstr);
475 inline static void pshader_gen_output_modifier_line(
476 SHADER_BUFFER* buffer,
482 /* Generate a line that does the output modifier computation */
483 shader_addline(buffer, "MUL%s %s%s, %s, %s;\n", saturate ? "_SAT" : "",
484 regstr, write_mask, regstr, shift_tab[shift]);
487 void pshader_hw_cnd(SHADER_OPCODE_ARG* arg) {
489 SHADER_BUFFER* buffer = arg->buffer;
492 char src_name[3][50];
494 /* FIXME: support output modifiers */
496 /* Handle output register */
497 pshader_get_register_name(arg->dst, dst_name);
498 pshader_get_write_mask(arg->dst, dst_wmask);
499 strcat(dst_name, dst_wmask);
501 /* Generate input register names (with modifiers) */
502 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src_name[0]);
503 pshader_gen_input_modifier_line(buffer, arg->src[1], 1, src_name[1]);
504 pshader_gen_input_modifier_line(buffer, arg->src[2], 2, src_name[2]);
506 shader_addline(buffer, "ADD TMP, -%s, coefdiv.x;\n", src_name[0]);
507 shader_addline(buffer, "CMP %s, TMP, %s, %s;\n", dst_name, src_name[1], src_name[2]);
510 void pshader_hw_cmp(SHADER_OPCODE_ARG* arg) {
512 SHADER_BUFFER* buffer = arg->buffer;
515 char src_name[3][50];
517 /* FIXME: support output modifiers */
519 /* Handle output register */
520 pshader_get_register_name(arg->dst, dst_name);
521 pshader_get_write_mask(arg->dst, dst_wmask);
522 strcat(dst_name, dst_wmask);
524 /* Generate input register names (with modifiers) */
525 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src_name[0]);
526 pshader_gen_input_modifier_line(buffer, arg->src[1], 1, src_name[1]);
527 pshader_gen_input_modifier_line(buffer, arg->src[2], 2, src_name[2]);
529 shader_addline(buffer, "CMP %s, %s, %s, %s;\n", dst_name,
530 src_name[0], src_name[2], src_name[1]);
533 /* Map the opcode 1-to-1 to the GL code */
534 void pshader_hw_map2gl(SHADER_OPCODE_ARG* arg) {
536 CONST SHADER_OPCODE* curOpcode = arg->opcode;
537 SHADER_BUFFER* buffer = arg->buffer;
538 DWORD dst = arg->dst;
539 DWORD* src = arg->src;
544 /* Output token related */
545 char output_rname[256];
546 char output_wmask[20];
547 BOOL saturate = FALSE;
548 BOOL centroid = FALSE;
549 BOOL partialprecision = FALSE;
552 strcpy(tmpLine, curOpcode->glname);
554 /* Process modifiers */
555 if (0 != (dst & D3DSP_DSTMOD_MASK)) {
556 DWORD mask = dst & D3DSP_DSTMOD_MASK;
558 saturate = mask & D3DSPDM_SATURATE;
559 centroid = mask & D3DSPDM_MSAMPCENTROID;
560 partialprecision = mask & D3DSPDM_PARTIALPRECISION;
561 mask &= ~(D3DSPDM_MSAMPCENTROID | D3DSPDM_PARTIALPRECISION | D3DSPDM_SATURATE);
563 FIXME("Unrecognized modifier(0x%#lx)\n", mask >> D3DSP_DSTMOD_SHIFT);
566 FIXME("Unhandled modifier(0x%#lx)\n", mask >> D3DSP_DSTMOD_SHIFT);
568 shift = (dst & D3DSP_DSTSHIFT_MASK) >> D3DSP_DSTSHIFT_SHIFT;
570 /* Generate input and output registers */
571 if (curOpcode->num_params > 0) {
572 char operands[4][100];
574 /* Generate input register names (with modifiers) */
575 for (i = 1; i < curOpcode->num_params; ++i)
576 pshader_gen_input_modifier_line(buffer, src[i-1], i-1, operands[i]);
578 /* Handle output register */
579 pshader_get_register_name(dst, output_rname);
580 strcpy(operands[0], output_rname);
581 pshader_get_write_mask(dst, output_wmask);
582 strcat(operands[0], output_wmask);
584 if (saturate && (shift == 0))
585 strcat(tmpLine, "_SAT");
586 strcat(tmpLine, " ");
587 strcat(tmpLine, operands[0]);
588 for (i = 1; i < curOpcode->num_params; i++) {
589 strcat(tmpLine, ", ");
590 strcat(tmpLine, operands[i]);
592 strcat(tmpLine,";\n");
593 shader_addline(buffer, tmpLine);
595 /* A shift requires another line. */
597 pshader_gen_output_modifier_line(buffer, saturate, output_wmask, shift, output_rname);
601 void pshader_hw_tex(SHADER_OPCODE_ARG* arg) {
603 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
604 DWORD dst = arg->dst;
605 DWORD* src = arg->src;
606 SHADER_BUFFER* buffer = arg->buffer;
607 DWORD hex_version = This->baseShader.hex_version;
612 DWORD reg_sampler_code;
614 /* All versions have a destination register */
615 reg_dest_code = dst & D3DSP_REGNUM_MASK;
616 pshader_get_register_name(dst, reg_dest);
618 /* 1.0-1.3: Use destination register as coordinate source.
619 1.4+: Use provided coordinate source register. */
620 if (hex_version < D3DPS_VERSION(1,4))
621 strcpy(reg_coord, reg_dest);
623 pshader_gen_input_modifier_line(buffer, src[0], 0, reg_coord);
625 /* 1.0-1.4: Use destination register number as texture code.
626 2.0+: Use provided sampler number as texure code. */
627 if (hex_version < D3DPS_VERSION(2,0))
628 reg_sampler_code = reg_dest_code;
630 reg_sampler_code = src[1] & D3DSP_REGNUM_MASK;
632 shader_addline(buffer, "TEX %s, %s, texture[%lu], 2D;\n",
633 reg_dest, reg_coord, reg_sampler_code);
636 void pshader_hw_texcoord(SHADER_OPCODE_ARG* arg) {
638 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
639 DWORD dst = arg->dst;
640 DWORD* src = arg->src;
641 SHADER_BUFFER* buffer = arg->buffer;
642 DWORD hex_version = This->baseShader.hex_version;
645 pshader_get_write_mask(dst, tmp);
646 if (hex_version != D3DPS_VERSION(1,4)) {
647 DWORD reg = dst & D3DSP_REGNUM_MASK;
648 shader_addline(buffer, "MOV_SAT T%lu%s, fragment.texcoord[%lu];\n", reg, tmp, reg);
650 DWORD reg1 = dst & D3DSP_REGNUM_MASK;
651 DWORD reg2 = src[0] & D3DSP_REGNUM_MASK;
652 shader_addline(buffer, "MOV R%lu%s, fragment.texcoord[%lu];\n", reg1, tmp, reg2);
656 void pshader_hw_texreg2ar(SHADER_OPCODE_ARG* arg) {
658 SHADER_BUFFER* buffer = arg->buffer;
660 DWORD reg1 = arg->dst & D3DSP_REGNUM_MASK;
661 DWORD reg2 = arg->src[0] & D3DSP_REGNUM_MASK;
662 shader_addline(buffer, "MOV TMP.r, T%lu.a;\n", reg2);
663 shader_addline(buffer, "MOV TMP.g, T%lu.r;\n", reg2);
664 shader_addline(buffer, "TEX T%lu, TMP, texture[%lu], 2D;\n", reg1, reg1);
667 void pshader_hw_texreg2gb(SHADER_OPCODE_ARG* arg) {
669 SHADER_BUFFER* buffer = arg->buffer;
671 DWORD reg1 = arg->dst & D3DSP_REGNUM_MASK;
672 DWORD reg2 = arg->src[0] & D3DSP_REGNUM_MASK;
673 shader_addline(buffer, "MOV TMP.r, T%lu.g;\n", reg2);
674 shader_addline(buffer, "MOV TMP.g, T%lu.b;\n", reg2);
675 shader_addline(buffer, "TEX T%lu, TMP, texture[%lu], 2D;\n", reg1, reg1);
678 void pshader_hw_texbem(SHADER_OPCODE_ARG* arg) {
680 SHADER_BUFFER* buffer = arg->buffer;
681 DWORD reg1 = arg->dst & D3DSP_REGNUM_MASK;
682 DWORD reg2 = arg->src[0] & D3DSP_REGNUM_MASK;
684 /* FIXME: Should apply the BUMPMAPENV matrix */
685 shader_addline(buffer, "ADD TMP.rg, fragment.texcoord[%lu], T%lu;\n", reg1, reg2);
686 shader_addline(buffer, "TEX T%lu, TMP, texture[%lu], 2D;\n", reg1, reg1);
689 void pshader_hw_texm3x2pad(SHADER_OPCODE_ARG* arg) {
691 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
692 SHADER_BUFFER* buffer = arg->buffer;
695 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
696 shader_addline(buffer, "DP3 TMP.x, T%lu, %s;\n", reg, src0_name);
699 void pshader_hw_texm3x2tex(SHADER_OPCODE_ARG* arg) {
701 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
702 SHADER_BUFFER* buffer = arg->buffer;
705 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
706 shader_addline(buffer, "DP3 TMP.y, T%lu, %s;\n", reg, src0_name);
707 shader_addline(buffer, "TEX T%lu, TMP, texture[%lu], 2D;\n", reg, reg);
710 void pshader_hw_texm3x3pad(SHADER_OPCODE_ARG* arg) {
712 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
713 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
714 SHADER_BUFFER* buffer = arg->buffer;
715 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
718 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
719 shader_addline(buffer, "DP3 TMP.%c, T%lu, %s;\n", 'x' + current_state->current_row, reg, src0_name);
720 current_state->texcoord_w[current_state->current_row++] = reg;
723 void pshader_hw_texm3x3tex(SHADER_OPCODE_ARG* arg) {
725 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
726 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
727 SHADER_BUFFER* buffer = arg->buffer;
728 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
731 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
732 shader_addline(buffer, "DP3 TMP.z, T%lu, %s;\n", reg, src0_name);
734 /* Cubemap textures will be more used than 3D ones. */
735 shader_addline(buffer, "TEX T%lu, TMP, texture[%lu], CUBE;\n", reg, reg);
736 current_state->current_row = 0;
739 void pshader_hw_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
741 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
742 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
743 SHADER_BUFFER* buffer = arg->buffer;
744 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
747 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
748 shader_addline(buffer, "DP3 TMP.z, T%lu, %s;\n", reg, src0_name);
750 /* Construct the eye-ray vector from w coordinates */
751 shader_addline(buffer, "MOV TMP2.x, fragment.texcoord[%lu].w;\n", current_state->texcoord_w[0]);
752 shader_addline(buffer, "MOV TMP2.y, fragment.texcoord[%lu].w;\n", current_state->texcoord_w[1]);
753 shader_addline(buffer, "MOV TMP2.z, fragment.texcoord[%lu].w;\n", reg);
755 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
756 shader_addline(buffer, "DP3 TMP.w, TMP, TMP2;\n");
757 shader_addline(buffer, "MUL TMP, TMP.w, TMP;\n");
758 shader_addline(buffer, "MAD TMP, coefmul.x, TMP, -TMP2;\n");
760 /* Cubemap textures will be more used than 3D ones. */
761 shader_addline(buffer, "TEX T%lu, TMP, texture[%lu], CUBE;\n", reg, reg);
762 current_state->current_row = 0;
765 void pshader_hw_texm3x3spec(SHADER_OPCODE_ARG* arg) {
767 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
768 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
769 DWORD reg3 = arg->src[1] & D3DSP_REGNUM_MASK;
770 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
771 SHADER_BUFFER* buffer = arg->buffer;
774 pshader_gen_input_modifier_line(buffer, arg->src[0], 0, src0_name);
775 shader_addline(buffer, "DP3 TMP.z, T%lu, %s;\n", reg, src0_name);
777 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
778 shader_addline(buffer, "DP3 TMP.w, TMP, C[%lu];\n", reg3);
779 shader_addline(buffer, "MUL TMP, TMP.w, TMP;\n");
780 shader_addline(buffer, "MAD TMP, coefmul.x, TMP, -C[%lu];\n", reg3);
782 /* Cubemap textures will be more used than 3D ones. */
783 shader_addline(buffer, "TEX T%lu, TMP, texture[%lu], CUBE;\n", reg, reg);
784 current_state->current_row = 0;
787 /** Handles transforming all D3DSIO_M?x? opcodes for
788 Vertex shaders to ARB_vertex_program codes */
789 void vshader_hw_mnxn(SHADER_OPCODE_ARG* arg) {
793 SHADER_OPCODE_ARG tmpArg;
795 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
797 /* Set constants for the temporary argument */
798 tmpArg.shader = arg->shader;
799 tmpArg.buffer = arg->buffer;
800 tmpArg.src[0] = arg->src[0];
801 tmpArg.src_addr[0] = arg->src_addr[0];
802 tmpArg.src_addr[1] = arg->src_addr[1];
803 tmpArg.reg_maps = arg->reg_maps;
805 switch(arg->opcode->opcode) {
808 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP4];
812 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP4];
816 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP3];
820 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP3];
824 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP3];
830 for (i = 0; i < nComponents; i++) {
831 tmpArg.dst = ((arg->dst) & ~D3DSP_WRITEMASK_ALL)|(D3DSP_WRITEMASK_0<<i);
832 tmpArg.src[1] = arg->src[1]+i;
833 vshader_hw_map2gl(&tmpArg);
837 /* TODO: merge with pixel shader */
838 /* Map the opcode 1-to-1 to the GL code */
839 void vshader_hw_map2gl(SHADER_OPCODE_ARG* arg) {
841 CONST SHADER_OPCODE* curOpcode = arg->opcode;
842 SHADER_BUFFER* buffer = arg->buffer;
843 DWORD dst = arg->dst;
844 DWORD* src = arg->src;
846 DWORD dst_regtype = shader_get_regtype(dst);
850 if (curOpcode->opcode == D3DSIO_MOV && dst_regtype == D3DSPR_ADDR)
851 strcpy(tmpLine, "ARL");
853 strcpy(tmpLine, curOpcode->glname);
855 if (curOpcode->num_params > 0) {
856 vshader_program_add_param(arg, dst, FALSE, tmpLine);
857 for (i = 1; i < curOpcode->num_params; ++i) {
858 strcat(tmpLine, ",");
859 vshader_program_add_param(arg, src[i-1], TRUE, tmpLine);
862 shader_addline(buffer, "%s;\n", tmpLine);