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 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 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 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 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_FogCoord", "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 /* if the attributes come in as named dcl's then use a named vertex (called namedVertexN) */
190 if (vshader->namedArrays) {
191 sprintf(tmpStr, "namedVertex%lu", reg);
193 /* otherwise the input is on a numbered attribute so use opengl numbered attributes */
194 sprintf(tmpStr, "attrib%lu", reg);
199 if (arg->reg_maps->constantsF[reg]) {
200 /* Use a local constant declared by "dcl" */
202 if (param & D3DVS_ADDRMODE_RELATIVE) {
203 /* FIXME: Copy all constants (local & global) into a single array
204 * to handle this case where we want a relative address from a
206 FIXME("Relative addressing not yet supported on named constants\n");
208 sprintf(tmpStr, "C%lu", reg);
211 /* Use a global constant declared in Set____ShaderConstantF() */
212 if (param & D3DVS_ADDRMODE_RELATIVE) {
213 /* Relative addressing on shaders 2.0+ have a relative address token,
214 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
215 if (This->baseShader.version >= 20) {
216 char relStr[100], relReg[50], relMask[6];
217 shader_glsl_add_param(arg, addr_token, 0, TRUE, relReg, relMask, relStr);
218 sprintf(tmpStr, "C[%s + %lu]", relStr, reg);
220 sprintf(tmpStr, "C[A0.x + %lu]", reg);
223 /* Just a normal global constant - no relative addressing */
224 sprintf(tmpStr, "C[%lu]", reg);
228 case D3DSPR_TEXTURE: /* case D3DSPR_ADDR: */
230 sprintf(tmpStr, "T%lu", reg);
232 sprintf(tmpStr, "A%lu", reg);
236 sprintf(tmpStr, "mytex%lu", reg);
238 case D3DSPR_COLOROUT:
240 sprintf(tmpStr, "gl_FragColor");
242 /* TODO: See GL_ARB_draw_buffers */
243 FIXME("Unsupported write to render target %lu\n", reg);
244 sprintf(tmpStr, "unsupported_register");
248 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
250 case D3DSPR_DEPTHOUT:
251 sprintf(tmpStr, "gl_FragDepth");
255 sprintf(tmpStr, "gl_FrontColor");
257 sprintf(tmpStr, "gl_FrontSecondaryColor");
260 case D3DSPR_TEXCRDOUT:
261 sprintf(tmpStr, "gl_TexCoord[%lu]", reg);
264 FIXME("Unhandled register name Type(%ld)\n", regtype);
265 sprintf(tmpStr, "unrecognized_register");
269 strcat(regstr, tmpStr);
272 /* Writes the GLSL writemask for the destination register */
273 void shader_glsl_get_output_register_swizzle(
278 if ((param & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
279 strcat(write_mask, ".");
280 if (param & D3DSP_WRITEMASK_0) strcat(write_mask, "x");
281 if (param & D3DSP_WRITEMASK_1) strcat(write_mask, "y");
282 if (param & D3DSP_WRITEMASK_2) strcat(write_mask, "z");
283 if (param & D3DSP_WRITEMASK_3) strcat(write_mask, "w");
287 void shader_glsl_get_input_register_swizzle(
292 const char swizzle_reg_chars_color_fix[] = "zyxw";
293 const char swizzle_reg_chars[] = "xyzw";
294 const char* swizzle_regs = NULL;
297 DWORD swizzle = (param & D3DVS_SWIZZLE_MASK) >> D3DVS_SWIZZLE_SHIFT;
298 DWORD swizzle_x = swizzle & 0x03;
299 DWORD swizzle_y = (swizzle >> 2) & 0x03;
300 DWORD swizzle_z = (swizzle >> 4) & 0x03;
301 DWORD swizzle_w = (swizzle >> 6) & 0x03;
304 swizzle_regs = swizzle_reg_chars_color_fix;
306 swizzle_regs = swizzle_reg_chars;
310 * swizzle bits fields:
313 if ((D3DVS_NOSWIZZLE >> D3DVS_SWIZZLE_SHIFT) == swizzle) { /* D3DVS_NOSWIZZLE == 0xE4 << D3DVS_SWIZZLE_SHIFT */
315 sprintf(reg_mask, ".%c%c%c%c",
316 swizzle_regs[swizzle_x],
317 swizzle_regs[swizzle_y],
318 swizzle_regs[swizzle_z],
319 swizzle_regs[swizzle_w]);
323 if (swizzle_x == swizzle_y &&
324 swizzle_x == swizzle_z &&
325 swizzle_x == swizzle_w)
327 sprintf(reg_mask, ".%c", swizzle_regs[swizzle_x]);
329 sprintf(reg_mask, ".%c%c%c%c",
330 swizzle_regs[swizzle_x],
331 swizzle_regs[swizzle_y],
332 swizzle_regs[swizzle_z],
333 swizzle_regs[swizzle_w]);
337 /** From a given parameter token, generate the corresponding GLSL string.
338 * Also, return the actual register name and swizzle in case the
339 * caller needs this information as well. */
340 void shader_glsl_add_param(
341 SHADER_OPCODE_ARG* arg,
343 const DWORD addr_token,
349 BOOL is_color = FALSE;
350 reg_mask[0] = reg_name[0] = out_str[0] = 0;
352 shader_glsl_get_register_name(param, addr_token, reg_name, &is_color, arg);
355 shader_glsl_get_input_register_swizzle(param, is_color, reg_mask);
356 shader_glsl_gen_modifier(param, reg_name, reg_mask, out_str);
358 shader_glsl_get_output_register_swizzle(param, reg_mask);
359 sprintf(out_str, "%s%s", reg_name, reg_mask);
363 /** Process GLSL instruction modifiers */
364 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
366 if (0 != (arg->dst & D3DSP_DSTMOD_MASK)) {
367 DWORD mask = arg->dst & D3DSP_DSTMOD_MASK;
372 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
374 if (mask & D3DSPDM_SATURATE) {
375 /* _SAT means to clamp the value of the register to between 0 and 1 */
376 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_reg, dst_mask, dst_reg, dst_mask);
378 if (mask & D3DSPDM_MSAMPCENTROID) {
379 FIXME("_centroid modifier not handled\n");
381 if (mask & D3DSPDM_PARTIALPRECISION) {
382 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
387 /*****************************************************************************
389 * Begin processing individual instruction opcodes
391 ****************************************************************************/
393 /* Generate GLSL arithmatic functions (dst = src1 + src2) */
394 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
396 CONST SHADER_OPCODE* curOpcode = arg->opcode;
397 SHADER_BUFFER* buffer = arg->buffer;
399 char dst_reg[50], src0_reg[50], src1_reg[50];
400 char dst_mask[6], src0_mask[6], src1_mask[6];
401 char dst_str[100], src0_str[100], src1_str[100];
403 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
404 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
405 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
406 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
407 strcat(tmpLine, src0_str);
409 /* Determine the GLSL operator to use based on the opcode */
410 switch (curOpcode->opcode) {
411 case D3DSIO_MUL: strcat(tmpLine, " * "); break;
412 case D3DSIO_ADD: strcat(tmpLine, " + "); break;
413 case D3DSIO_SUB: strcat(tmpLine, " - "); break;
415 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
418 shader_addline(buffer, "%s%s)%s;\n", tmpLine, src1_str, dst_mask);
421 /* Process the D3DSIO_MOV opcode using GLSL (dst = src) */
422 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
424 SHADER_BUFFER* buffer = arg->buffer;
426 char dst_str[100], src0_str[100];
427 char dst_reg[50], src0_reg[50];
428 char dst_mask[6], src0_mask[6];
430 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
431 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
432 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
433 shader_addline(buffer, "%s%s)%s;\n", tmpLine, src0_str, dst_mask);
436 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
437 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
439 CONST SHADER_OPCODE* curOpcode = arg->opcode;
440 SHADER_BUFFER* buffer = arg->buffer;
442 char dst_str[100], src0_str[100], src1_str[100];
443 char dst_reg[50], src0_reg[50], src1_reg[50];
444 char dst_mask[6], src0_mask[6], src1_mask[6];
447 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
448 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
449 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
451 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpDest);
453 /* Need to cast the src vectors to vec3 for dp3, and vec4 for dp4 */
454 if (curOpcode->opcode == D3DSIO_DP4)
455 strcpy(cast, "vec4(");
457 strcpy(cast, "vec3(");
459 shader_addline(buffer, "%sdot(%s%s), %s%s)))%s;\n",
460 tmpDest, cast, src0_str, cast, src1_str, dst_mask);
463 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
464 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
466 CONST SHADER_OPCODE* curOpcode = arg->opcode;
467 SHADER_BUFFER* buffer = arg->buffer;
469 char dst_str[100], src_str[100];
470 char dst_reg[50], src_reg[50];
471 char dst_mask[6], src_mask[6];
474 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
476 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
478 /* Determine the GLSL function to use based on the opcode */
479 /* TODO: Possibly make this a table for faster lookups */
480 switch (curOpcode->opcode) {
481 case D3DSIO_MIN: strcat(tmpLine, "min"); break;
482 case D3DSIO_MAX: strcat(tmpLine, "max"); break;
483 case D3DSIO_RSQ: strcat(tmpLine, "inversesqrt"); break;
484 case D3DSIO_ABS: strcat(tmpLine, "abs"); break;
485 case D3DSIO_FRC: strcat(tmpLine, "fract"); break;
486 case D3DSIO_POW: strcat(tmpLine, "pow"); break;
487 case D3DSIO_CRS: strcat(tmpLine, "cross"); break;
488 case D3DSIO_NRM: strcat(tmpLine, "normalize"); break;
489 case D3DSIO_LOG: strcat(tmpLine, "log2"); break;
491 case D3DSIO_EXP: strcat(tmpLine, "exp2"); break;
492 case D3DSIO_SGE: strcat(tmpLine, "greaterThanEqual"); break;
493 case D3DSIO_SLT: strcat(tmpLine, "lessThan"); break;
495 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
499 strcat(tmpLine, "(");
501 if (curOpcode->num_params > 0) {
502 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
503 strcat(tmpLine, src_str);
504 for (i = 2; i < curOpcode->num_params; ++i) {
505 strcat(tmpLine, ", ");
506 shader_glsl_add_param(arg, arg->src[i-1], arg->src_addr[i-1], TRUE, src_reg, src_mask, src_str);
507 strcat(tmpLine, src_str);
510 shader_addline(buffer, "%s))%s;\n", tmpLine, dst_mask);
514 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
515 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
518 char dst_str[100], src_str[100];
519 char dst_reg[50], src_reg[50];
520 char dst_mask[6], src_mask[6];
522 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
523 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
524 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
525 strcat(tmpLine, "1.0 / ");
526 shader_addline(arg->buffer, "%s%s)%s;\n", tmpLine, src_str, dst_mask);
529 /** Process signed comparison opcodes in GLSL. */
530 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
533 char dst_str[100], src0_str[100], src1_str[100];
534 char dst_reg[50], src0_reg[50], src1_reg[50];
535 char dst_mask[6], src0_mask[6], src1_mask[6];
537 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
538 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
539 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
541 /* If we are comparing vectors and not scalars, we should process this through map2gl using the GLSL functions. */
542 if (strlen(src0_mask) != 2) {
543 shader_glsl_map2gl(arg);
547 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
549 switch (arg->opcode->opcode) {
550 case D3DSIO_SLT: strcpy(compareStr, "<"); break;
551 case D3DSIO_SGE: strcpy(compareStr, ">="); break;
553 FIXME("Can't handle opcode %s\n", arg->opcode->name);
555 shader_addline(arg->buffer, "%s(float(%s) %s float(%s)) ? 1.0 : 0.0)%s;\n",
556 tmpLine, src0_str, compareStr, src1_str, dst_mask);
560 /** Process CMP instruction in GLSL (dst = src0.x > 0.0 ? src1.x : src2.x), per channel */
561 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
563 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
564 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
565 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
567 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
568 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
569 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
570 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
572 /* FIXME: This isn't correct - doesn't take the dst's swizzle into account. */
573 shader_addline(arg->buffer, "%s.x = (%s.x > 0.0) ? %s.x : %s.x;\n", dst_reg, src0_reg, src1_reg, src2_reg);
574 shader_addline(arg->buffer, "%s.y = (%s.y > 0.0) ? %s.y : %s.y;\n", dst_reg, src0_reg, src1_reg, src2_reg);
575 shader_addline(arg->buffer, "%s.z = (%s.z > 0.0) ? %s.z : %s.z;\n", dst_reg, src0_reg, src1_reg, src2_reg);
576 shader_addline(arg->buffer, "%s.w = (%s.w > 0.0) ? %s.w : %s.w;\n", dst_reg, src0_reg, src1_reg, src2_reg);
579 /** Process the CND opcode in GLSL (dst = (src0 < 0.5) ? src1 : src2) */
580 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
583 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
584 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
585 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
587 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
588 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
589 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
590 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
591 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
592 shader_addline(arg->buffer, "%s(%s < 0.5) ? %s : %s)%s;\n",
593 tmpLine, src0_str, src1_str, src2_str, dst_mask);
596 /** GLSL code generation for D3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
597 void shader_glsl_mad(SHADER_OPCODE_ARG* arg) {
600 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
601 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
602 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
604 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
605 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
606 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
607 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
608 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
610 shader_addline(arg->buffer, "%s((%s) * (%s)) + (%s))%s;\n",
611 tmpLine, src0_str, src1_str, src2_str, dst_mask);
614 /** Handles transforming all D3DSIO_M?x? opcodes for
615 Vertex shaders to GLSL codes */
616 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
619 SHADER_OPCODE_ARG tmpArg;
621 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
623 /* Set constants for the temporary argument */
624 tmpArg.shader = arg->shader;
625 tmpArg.buffer = arg->buffer;
626 tmpArg.src[0] = arg->src[0];
627 tmpArg.src_addr[0] = arg->src_addr[0];
628 tmpArg.reg_maps = arg->reg_maps;
630 switch(arg->opcode->opcode) {
633 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP4];
637 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP4];
641 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP3];
645 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP3];
649 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP3];
655 for (i = 0; i < nComponents; i++) {
656 tmpArg.dst = ((arg->dst) & ~D3DSP_WRITEMASK_ALL)|(D3DSP_WRITEMASK_0<<i);
657 tmpArg.src[1] = arg->src[1]+i;
658 tmpArg.src_addr[1] = arg->src[1]+i;
659 shader_glsl_dot(&tmpArg);
664 The LRP instruction performs a component-wise linear interpolation
665 between the second and third operands using the first operand as the
666 blend factor. Equation: (dst = src2 * (src1 - src0) + src0)
668 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
671 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
672 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
673 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
675 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
676 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
677 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
678 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
680 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
682 shader_addline(arg->buffer, "%s(%s * (%s - %s) + %s))%s;\n",
683 tmpLine, src2_str, src1_str, src0_str, src0_str, dst_mask);
686 /** Process the D3DSIO_DCL opcode into a GLSL string - creates a local vec4
687 * float constant, and stores it's usage on the regmaps. */
688 void shader_glsl_def(SHADER_OPCODE_ARG* arg) {
690 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
692 shader_addline(arg->buffer,
693 "const vec4 C%lu = { %f, %f, %f, %f };\n", reg & 0xFF,
694 *((const float *)(arg->src + 0)),
695 *((const float *)(arg->src + 1)),
696 *((const float *)(arg->src + 2)),
697 *((const float *)(arg->src + 3)) );
699 arg->reg_maps->constantsF[reg] = 1;
702 /*********************************************
703 * Pixel Shader Specific Code begins here
704 ********************************************/
705 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
707 /* FIXME: Make this work for more than just 2D textures */
709 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
710 SHADER_BUFFER* buffer = arg->buffer;
711 DWORD version = This->baseShader.version;
713 char dst_str[100], dst_reg[50], dst_mask[6];
714 char src0_str[100], src0_reg[50], src0_mask[6];
715 char src1_str[100], src1_reg[50], src1_mask[6];
716 DWORD reg_dest_code = arg->dst & D3DSP_REGNUM_MASK;
718 /* All versions have a destination register */
719 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
721 /* 1.0-1.3: Use destination register as coordinate source.
722 2.0+: Use provided coordinate source register. */
724 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
725 sprintf(src1_str, "mytex%lu", reg_dest_code);
726 } else if (version > 14) {
727 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
728 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
731 /* 1.0-1.4: Use destination register number as texture code.
732 2.0+: Use provided sampler number as texure code. */
734 shader_addline(buffer, "%s = texture2D(mytex%lu, gl_TexCoord[%lu].st);\n",
735 dst_str, reg_dest_code, reg_dest_code);
737 shader_addline(buffer, "%s = texture2D(%s, %s.st);\n", dst_str, src1_str, src0_reg);
741 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
743 /* FIXME: Make this work for more than just 2D textures */
745 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
746 SHADER_BUFFER* buffer = arg->buffer;
747 DWORD version = This->baseShader.version;
754 shader_glsl_add_param(arg, arg->dst, 0, FALSE, tmpReg, tmpMask, tmpStr);
757 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
758 shader_addline(buffer, "%s = gl_TexCoord[%lu];\n", tmpReg, reg);
760 DWORD reg2 = arg->src[0] & D3DSP_REGNUM_MASK;
761 shader_addline(buffer, "%s = gl_TexCoord[%lu]%s;\n", tmpStr, reg2, tmpMask);
765 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
767 /* FIXME: Make this work for more than just 2D textures */
769 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
770 SHADER_BUFFER* buffer = arg->buffer;
775 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
776 shader_addline(buffer, "tmp0.x = dot(vec3(T%lu), vec3(%s));\n", reg, src0_name, src0_mask, src0_str);
779 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
781 /* FIXME: Make this work for more than just 2D textures */
783 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
784 SHADER_BUFFER* buffer = arg->buffer;
789 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
790 shader_addline(buffer, "tmp0.y = dot(vec3(T%lu), vec3(%s));\n", reg, src0_str);
791 shader_addline(buffer, "T%lu = texture2D(mytex%lu, tmp0.st);\n", reg, reg);