2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wined3d_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
27 #define GLINFO_LOCATION (*gl_info)
29 /** Prints the GLSL info log which will contain error messages if they exist */
30 void print_glsl_info_log(WineD3D_GL_Info *gl_info, GLhandleARB obj) {
32 int infologLength = 0;
35 GL_EXTCALL(glGetObjectParameterivARB(obj,
36 GL_OBJECT_INFO_LOG_LENGTH_ARB,
39 /* A size of 1 is just a null-terminated string, so the log should be bigger than
40 * that if there are errors. */
41 if (infologLength > 1)
43 infoLog = (char *)HeapAlloc(GetProcessHeap(), 0, infologLength);
44 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
45 TRACE("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
46 HeapFree(GetProcessHeap(), 0, infoLog);
50 /*****************************************************************************
51 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
53 * For more information, see http://wiki.winehq.org/DirectX-Shaders
54 ****************************************************************************/
57 static void shader_glsl_add_param(
58 SHADER_OPCODE_ARG* arg,
60 const DWORD addr_token,
66 /** Used for opcode modifiers - They multiply the result by the specified amount */
67 static const char* shift_glsl_tab[] = {
69 "2.0 * ", /* 1 (x2) */
70 "4.0 * ", /* 2 (x4) */
71 "8.0 * ", /* 3 (x8) */
72 "16.0 * ", /* 4 (x16) */
73 "32.0 * ", /* 5 (x32) */
80 "0.0625 * ", /* 12 (d16) */
81 "0.125 * ", /* 13 (d8) */
82 "0.25 * ", /* 14 (d4) */
83 "0.5 * " /* 15 (d2) */
86 /** Print the beginning of the generated GLSL string. example: "reg_name.xyzw = vec4(" */
87 static void shader_glsl_add_dst(DWORD param, const char* reg_name, const char* reg_mask, char* outStr) {
89 int shift = (param & D3DSP_DSTSHIFT_MASK) >> D3DSP_DSTSHIFT_SHIFT;
91 /* TODO: determine if destination is anything other than a float vector and accommodate*/
92 if (reg_name[0] == 'A')
93 sprintf(outStr, "%s%s = %sivec4(", reg_name, reg_mask, shift_glsl_tab[shift]);
95 sprintf(outStr, "%s%s = %svec4(", reg_name, reg_mask, shift_glsl_tab[shift]);
99 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
100 static void shader_glsl_gen_modifier (
103 const char *in_regswizzle,
108 switch (instr & D3DSP_SRCMOD_MASK) {
110 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
113 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
116 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
118 case D3DSPSM_BIASNEG:
119 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
122 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
124 case D3DSPSM_SIGNNEG:
125 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
128 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
131 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
134 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
136 case D3DSPSM_DZ: /* reg1_db = { reg1.r/b, reg1.g/b, ...} The g & a components are undefined, so we'll leave them alone */
137 sprintf(out_str, "vec4(%s.r / %s.b, %s.g / %s.b, %s.b, %s.a)", in_reg, in_reg, in_reg, in_reg, in_reg, in_reg);
140 sprintf(out_str, "vec4(%s.r / %s.a, %s.g / %s.a, %s.b, %s.a)", in_reg, in_reg, in_reg, in_reg, in_reg, in_reg);
143 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
146 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
149 FIXME("Unhandled modifier %lu\n", (instr & D3DSP_SRCMOD_MASK));
150 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
154 /** Writes the GLSL variable name that corresponds to the register that the
155 * DX opcode parameter is trying to access */
156 static void shader_glsl_get_register_name(
158 const DWORD addr_token,
161 SHADER_OPCODE_ARG* arg) {
163 /* oPos, oFog and oPts in D3D */
164 const char* hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
166 DWORD reg = param & D3DSP_REGNUM_MASK;
167 DWORD regtype = shader_get_regtype(param);
168 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
169 BOOL pshader = shader_is_pshader_version(This->baseShader.hex_version);
174 sprintf(tmpStr, "R%lu", reg);
179 strcpy(tmpStr, "gl_Color");
181 strcpy(tmpStr, "gl_SecondaryColor");
184 IWineD3DVertexShaderImpl *vshader = (IWineD3DVertexShaderImpl*) arg->shader;
185 if (reg == vshader->arrayUsageMap[WINED3DSHADERDECLUSAGE_DIFFUSE]
186 || reg == vshader->arrayUsageMap[WINED3DSHADERDECLUSAGE_SPECULAR]) {
189 sprintf(tmpStr, "attrib%lu", reg);
193 if (arg->reg_maps->constantsF[reg]) {
194 /* Use a local constant declared by "dcl" */
196 if (param & D3DVS_ADDRMODE_RELATIVE) {
197 /* FIXME: Copy all constants (local & global) into a single array
198 * to handle this case where we want a relative address from a
200 FIXME("Relative addressing not yet supported on named constants\n");
202 sprintf(tmpStr, "C%lu", reg);
205 /* Use a global constant declared in Set____ShaderConstantF() */
206 if (param & D3DVS_ADDRMODE_RELATIVE) {
207 /* Relative addressing on shaders 2.0+ have a relative address token,
208 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
209 if (This->baseShader.version >= 20) {
210 char relStr[100], relReg[50], relMask[6];
211 shader_glsl_add_param(arg, addr_token, 0, TRUE, relReg, relMask, relStr);
212 sprintf(tmpStr, "C[%s + %lu]", relStr, reg);
214 sprintf(tmpStr, "C[A0.x + %lu]", reg);
217 /* Just a normal global constant - no relative addressing */
218 sprintf(tmpStr, "C[%lu]", reg);
222 case D3DSPR_TEXTURE: /* case D3DSPR_ADDR: */
224 sprintf(tmpStr, "T%lu", reg);
226 sprintf(tmpStr, "A%lu", reg);
230 sprintf(tmpStr, "mytex%lu", reg);
232 case D3DSPR_COLOROUT:
234 sprintf(tmpStr, "gl_FragColor");
236 /* TODO: See GL_ARB_draw_buffers */
237 FIXME("Unsupported write to render target %lu\n", reg);
238 sprintf(tmpStr, "unsupported_register");
242 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
244 case D3DSPR_DEPTHOUT:
245 sprintf(tmpStr, "gl_FragDepth");
249 sprintf(tmpStr, "gl_FrontColor");
251 sprintf(tmpStr, "gl_FrontSecondaryColor");
254 case D3DSPR_TEXCRDOUT:
255 sprintf(tmpStr, "gl_TexCoord[%lu]", reg);
258 FIXME("Unhandled register name Type(%ld)\n", regtype);
259 sprintf(tmpStr, "unrecognized_register");
263 strcat(regstr, tmpStr);
266 /* Writes the GLSL writemask for the destination register */
267 static void shader_glsl_get_output_register_swizzle(
272 if ((param & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
273 strcat(write_mask, ".");
274 if (param & D3DSP_WRITEMASK_0) strcat(write_mask, "x");
275 if (param & D3DSP_WRITEMASK_1) strcat(write_mask, "y");
276 if (param & D3DSP_WRITEMASK_2) strcat(write_mask, "z");
277 if (param & D3DSP_WRITEMASK_3) strcat(write_mask, "w");
281 static void shader_glsl_get_input_register_swizzle(
286 const char swizzle_reg_chars_color_fix[] = "zyxw";
287 const char swizzle_reg_chars[] = "xyzw";
288 const char* swizzle_regs = NULL;
291 DWORD swizzle = (param & D3DVS_SWIZZLE_MASK) >> D3DVS_SWIZZLE_SHIFT;
292 DWORD swizzle_x = swizzle & 0x03;
293 DWORD swizzle_y = (swizzle >> 2) & 0x03;
294 DWORD swizzle_z = (swizzle >> 4) & 0x03;
295 DWORD swizzle_w = (swizzle >> 6) & 0x03;
298 swizzle_regs = swizzle_reg_chars_color_fix;
300 swizzle_regs = swizzle_reg_chars;
304 * swizzle bits fields:
307 if ((D3DVS_NOSWIZZLE >> D3DVS_SWIZZLE_SHIFT) == swizzle) { /* D3DVS_NOSWIZZLE == 0xE4 << D3DVS_SWIZZLE_SHIFT */
309 sprintf(reg_mask, ".%c%c%c%c",
310 swizzle_regs[swizzle_x],
311 swizzle_regs[swizzle_y],
312 swizzle_regs[swizzle_z],
313 swizzle_regs[swizzle_w]);
317 if (swizzle_x == swizzle_y &&
318 swizzle_x == swizzle_z &&
319 swizzle_x == swizzle_w)
321 sprintf(reg_mask, ".%c", swizzle_regs[swizzle_x]);
323 sprintf(reg_mask, ".%c%c%c%c",
324 swizzle_regs[swizzle_x],
325 swizzle_regs[swizzle_y],
326 swizzle_regs[swizzle_z],
327 swizzle_regs[swizzle_w]);
331 /** From a given parameter token, generate the corresponding GLSL string.
332 * Also, return the actual register name and swizzle in case the
333 * caller needs this information as well. */
334 static void shader_glsl_add_param(
335 SHADER_OPCODE_ARG* arg,
337 const DWORD addr_token,
343 BOOL is_color = FALSE;
344 reg_mask[0] = reg_name[0] = out_str[0] = 0;
346 shader_glsl_get_register_name(param, addr_token, reg_name, &is_color, arg);
349 shader_glsl_get_input_register_swizzle(param, is_color, reg_mask);
350 shader_glsl_gen_modifier(param, reg_name, reg_mask, out_str);
352 shader_glsl_get_output_register_swizzle(param, reg_mask);
353 sprintf(out_str, "%s%s", reg_name, reg_mask);
357 /** Process GLSL instruction modifiers */
358 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
360 if (0 != (arg->dst & D3DSP_DSTMOD_MASK)) {
361 DWORD mask = arg->dst & D3DSP_DSTMOD_MASK;
366 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
368 if (mask & D3DSPDM_SATURATE) {
369 /* _SAT means to clamp the value of the register to between 0 and 1 */
370 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_reg, dst_mask, dst_reg, dst_mask);
372 if (mask & D3DSPDM_MSAMPCENTROID) {
373 FIXME("_centroid modifier not handled\n");
375 if (mask & D3DSPDM_PARTIALPRECISION) {
376 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
381 /*****************************************************************************
383 * Begin processing individual instruction opcodes
385 ****************************************************************************/
387 /* Generate GLSL arithmatic functions (dst = src1 + src2) */
388 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
390 CONST SHADER_OPCODE* curOpcode = arg->opcode;
391 SHADER_BUFFER* buffer = arg->buffer;
393 char dst_reg[50], src0_reg[50], src1_reg[50];
394 char dst_mask[6], src0_mask[6], src1_mask[6];
395 char dst_str[100], src0_str[100], src1_str[100];
397 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
398 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
399 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
400 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
401 strcat(tmpLine, src0_str);
403 /* Determine the GLSL operator to use based on the opcode */
404 switch (curOpcode->opcode) {
405 case D3DSIO_MUL: strcat(tmpLine, " * "); break;
406 case D3DSIO_ADD: strcat(tmpLine, " + "); break;
407 case D3DSIO_SUB: strcat(tmpLine, " - "); break;
409 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
412 shader_addline(buffer, "%s%s)%s;\n", tmpLine, src1_str, dst_mask);
415 /* Process the D3DSIO_MOV opcode using GLSL (dst = src) */
416 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
418 SHADER_BUFFER* buffer = arg->buffer;
420 char dst_str[100], src0_str[100];
421 char dst_reg[50], src0_reg[50];
422 char dst_mask[6], src0_mask[6];
424 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
425 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
426 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
427 shader_addline(buffer, "%s%s)%s;\n", tmpLine, src0_str, dst_mask);
430 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
431 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
433 CONST SHADER_OPCODE* curOpcode = arg->opcode;
434 SHADER_BUFFER* buffer = arg->buffer;
436 char dst_str[100], src0_str[100], src1_str[100];
437 char dst_reg[50], src0_reg[50], src1_reg[50];
438 char dst_mask[6], src0_mask[6], src1_mask[6];
441 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
442 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
443 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
445 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpDest);
447 /* Need to cast the src vectors to vec3 for dp3, and vec4 for dp4 */
448 if (curOpcode->opcode == D3DSIO_DP4)
449 strcpy(cast, "vec4(");
451 strcpy(cast, "vec3(");
453 shader_addline(buffer, "%sdot(%s%s), %s%s)))%s;\n",
454 tmpDest, cast, src0_str, cast, src1_str, dst_mask);
457 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
458 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
460 CONST SHADER_OPCODE* curOpcode = arg->opcode;
461 SHADER_BUFFER* buffer = arg->buffer;
463 char dst_str[100], src_str[100];
464 char dst_reg[50], src_reg[50];
465 char dst_mask[6], src_mask[6];
468 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
470 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
472 /* Determine the GLSL function to use based on the opcode */
473 /* TODO: Possibly make this a table for faster lookups */
474 switch (curOpcode->opcode) {
475 case D3DSIO_MIN: strcat(tmpLine, "min"); break;
476 case D3DSIO_MAX: strcat(tmpLine, "max"); break;
477 case D3DSIO_RSQ: strcat(tmpLine, "inversesqrt"); break;
478 case D3DSIO_ABS: strcat(tmpLine, "abs"); break;
479 case D3DSIO_FRC: strcat(tmpLine, "fract"); break;
480 case D3DSIO_POW: strcat(tmpLine, "pow"); break;
481 case D3DSIO_CRS: strcat(tmpLine, "cross"); break;
482 case D3DSIO_NRM: strcat(tmpLine, "normalize"); break;
483 case D3DSIO_LOG: strcat(tmpLine, "log2"); break;
485 case D3DSIO_EXP: strcat(tmpLine, "exp2"); break;
486 case D3DSIO_SGE: strcat(tmpLine, "greaterThanEqual"); break;
487 case D3DSIO_SLT: strcat(tmpLine, "lessThan"); break;
489 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
493 strcat(tmpLine, "(");
495 if (curOpcode->num_params > 0) {
496 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
497 strcat(tmpLine, src_str);
498 for (i = 2; i < curOpcode->num_params; ++i) {
499 strcat(tmpLine, ", ");
500 shader_glsl_add_param(arg, arg->src[i-1], arg->src_addr[i-1], TRUE, src_reg, src_mask, src_str);
501 strcat(tmpLine, src_str);
504 shader_addline(buffer, "%s))%s;\n", tmpLine, dst_mask);
508 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
509 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
512 char dst_str[100], src_str[100];
513 char dst_reg[50], src_reg[50];
514 char dst_mask[6], src_mask[6];
516 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
517 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
518 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
519 strcat(tmpLine, "1.0 / ");
520 shader_addline(arg->buffer, "%s%s)%s;\n", tmpLine, src_str, dst_mask);
523 /** Process signed comparison opcodes in GLSL. */
524 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
527 char dst_str[100], src0_str[100], src1_str[100];
528 char dst_reg[50], src0_reg[50], src1_reg[50];
529 char dst_mask[6], src0_mask[6], src1_mask[6];
531 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
532 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
533 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
535 /* If we are comparing vectors and not scalars, we should process this through map2gl using the GLSL functions. */
536 if (strlen(src0_mask) != 2) {
537 shader_glsl_map2gl(arg);
541 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
543 switch (arg->opcode->opcode) {
544 case D3DSIO_SLT: strcpy(compareStr, "<"); break;
545 case D3DSIO_SGE: strcpy(compareStr, ">="); break;
547 FIXME("Can't handle opcode %s\n", arg->opcode->name);
549 shader_addline(arg->buffer, "%s(float(%s) %s float(%s)) ? 1.0 : 0.0)%s;\n",
550 tmpLine, src0_str, compareStr, src1_str, dst_mask);
554 /** Process CMP instruction in GLSL (dst = src0.x > 0.0 ? src1.x : src2.x), per channel */
555 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
557 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
558 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
559 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
561 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
562 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
563 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
564 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
566 /* FIXME: This isn't correct - doesn't take the dst's swizzle into account. */
567 shader_addline(arg->buffer, "%s.x = (%s.x > 0.0) ? %s.x : %s.x;\n", dst_reg, src0_reg, src1_reg, src2_reg);
568 shader_addline(arg->buffer, "%s.y = (%s.y > 0.0) ? %s.y : %s.y;\n", dst_reg, src0_reg, src1_reg, src2_reg);
569 shader_addline(arg->buffer, "%s.z = (%s.z > 0.0) ? %s.z : %s.z;\n", dst_reg, src0_reg, src1_reg, src2_reg);
570 shader_addline(arg->buffer, "%s.w = (%s.w > 0.0) ? %s.w : %s.w;\n", dst_reg, src0_reg, src1_reg, src2_reg);
573 /** Process the CND opcode in GLSL (dst = (src0 < 0.5) ? src1 : src2) */
574 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
577 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
578 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
579 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
581 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
582 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
583 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
584 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
585 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
586 shader_addline(arg->buffer, "%s(%s < 0.5) ? %s : %s)%s;\n",
587 tmpLine, src0_str, src1_str, src2_str, dst_mask);
590 /** GLSL code generation for D3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
591 void shader_glsl_mad(SHADER_OPCODE_ARG* arg) {
594 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
595 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
596 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
598 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
599 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
600 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
601 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
602 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
604 shader_addline(arg->buffer, "%s((%s) * (%s)) + (%s))%s;\n",
605 tmpLine, src0_str, src1_str, src2_str, dst_mask);
608 /** Handles transforming all D3DSIO_M?x? opcodes for
609 Vertex shaders to GLSL codes */
610 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
613 SHADER_OPCODE_ARG tmpArg;
615 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
617 /* Set constants for the temporary argument */
618 tmpArg.shader = arg->shader;
619 tmpArg.buffer = arg->buffer;
620 tmpArg.src[0] = arg->src[0];
621 tmpArg.src_addr[0] = arg->src_addr[0];
622 tmpArg.reg_maps = arg->reg_maps;
624 switch(arg->opcode->opcode) {
627 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP4];
631 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP4];
635 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP3];
639 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP3];
643 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP3];
649 for (i = 0; i < nComponents; i++) {
650 tmpArg.dst = ((arg->dst) & ~D3DSP_WRITEMASK_ALL)|(D3DSP_WRITEMASK_0<<i);
651 tmpArg.src[1] = arg->src[1]+i;
652 tmpArg.src_addr[1] = arg->src[1]+i;
653 shader_glsl_dot(&tmpArg);
658 The LRP instruction performs a component-wise linear interpolation
659 between the second and third operands using the first operand as the
660 blend factor. Equation: (dst = src2 * (src1 - src0) + src0)
662 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
665 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
666 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
667 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
669 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
670 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
671 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
672 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
674 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
676 shader_addline(arg->buffer, "%s(%s * (%s - %s) + %s))%s;\n",
677 tmpLine, src2_str, src1_str, src0_str, src0_str, dst_mask);
680 /** Process the D3DSIO_DCL opcode into a GLSL string - creates a local vec4
681 * float constant, and stores it's usage on the regmaps. */
682 void shader_glsl_def(SHADER_OPCODE_ARG* arg) {
684 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
686 shader_addline(arg->buffer,
687 "const vec4 C%lu = { %f, %f, %f, %f };\n", reg,
688 *((const float *)(arg->src + 0)),
689 *((const float *)(arg->src + 1)),
690 *((const float *)(arg->src + 2)),
691 *((const float *)(arg->src + 3)) );
693 arg->reg_maps->constantsF[reg] = 1;
696 /*********************************************
697 * Pixel Shader Specific Code begins here
698 ********************************************/
699 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
701 /* FIXME: Make this work for more than just 2D textures */
703 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
704 SHADER_BUFFER* buffer = arg->buffer;
705 DWORD version = This->baseShader.version;
707 char dst_str[100], dst_reg[50], dst_mask[6];
708 char src0_str[100], src0_reg[50], src0_mask[6];
709 char src1_str[100], src1_reg[50], src1_mask[6];
710 DWORD reg_dest_code = arg->dst & D3DSP_REGNUM_MASK;
712 /* All versions have a destination register */
713 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
715 /* 1.0-1.3: Use destination register as coordinate source.
716 2.0+: Use provided coordinate source register. */
718 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
719 sprintf(src1_str, "mytex%lu", reg_dest_code);
720 } else if (version > 14) {
721 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
722 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
725 /* 1.0-1.4: Use destination register number as texture code.
726 2.0+: Use provided sampler number as texure code. */
728 shader_addline(buffer, "%s = texture2D(mytex%lu, gl_TexCoord[%lu].st);\n",
729 dst_str, reg_dest_code, reg_dest_code);
731 shader_addline(buffer, "%s = texture2D(%s, %s.st);\n", dst_str, src1_str, src0_reg);
735 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
737 /* FIXME: Make this work for more than just 2D textures */
739 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
740 SHADER_BUFFER* buffer = arg->buffer;
741 DWORD version = This->baseShader.version;
748 shader_glsl_add_param(arg, arg->dst, 0, FALSE, tmpReg, tmpMask, tmpStr);
751 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
752 shader_addline(buffer, "%s = gl_TexCoord[%lu];\n", tmpReg, reg);
754 DWORD reg2 = arg->src[0] & D3DSP_REGNUM_MASK;
755 shader_addline(buffer, "%s = gl_TexCoord[%lu]%s;\n", tmpStr, reg2, tmpMask);
759 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
761 /* FIXME: Make this work for more than just 2D textures */
763 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
764 SHADER_BUFFER* buffer = arg->buffer;
769 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
770 shader_addline(buffer, "tmp0.x = dot(vec3(T%lu), vec3(%s));\n", reg, src0_name, src0_mask, src0_str);
773 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
775 /* FIXME: Make this work for more than just 2D textures */
777 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
778 SHADER_BUFFER* buffer = arg->buffer;
783 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
784 shader_addline(buffer, "tmp0.y = dot(vec3(T%lu), vec3(%s));\n", reg, src0_str);
785 shader_addline(buffer, "T%lu = texture2D(mytex%lu, tmp0.st);\n", reg, reg);