wined3d: Rework dcl processing.
[wine/multimedia.git] / dlls / wined3d / glsl_shader.c
blob59a0b0589baac315fb01129a846e4444f7c1a52c
1 /*
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
21 #include "config.h"
22 #include <stdio.h>
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;
33 char *infoLog;
35 GL_EXTCALL(glGetObjectParameterivARB(obj,
36 GL_OBJECT_INFO_LOG_LENGTH_ARB,
37 &infologLength));
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 ****************************************************************************/
56 /* Prototypes */
57 static void shader_glsl_add_param(
58 SHADER_OPCODE_ARG* arg,
59 const DWORD param,
60 const DWORD addr_token,
61 BOOL is_input,
62 char *reg_name,
63 char *reg_mask,
64 char *out_str);
66 /** Used for opcode modifiers - They multiply the result by the specified amount */
67 static const char* shift_glsl_tab[] = {
68 "", /* 0 (none) */
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) */
74 "", /* 6 (x64) */
75 "", /* 7 (x128) */
76 "", /* 8 (d256) */
77 "", /* 9 (d128) */
78 "", /* 10 (d64) */
79 "", /* 11 (d32) */
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]);
94 else
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 (
101 const DWORD instr,
102 const char *in_reg,
103 const char *in_regswizzle,
104 char *out_str) {
106 out_str[0] = 0;
108 switch (instr & D3DSP_SRCMOD_MASK) {
109 case D3DSPSM_NONE:
110 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
111 break;
112 case D3DSPSM_NEG:
113 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
114 break;
115 case D3DSPSM_BIAS:
116 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
117 break;
118 case D3DSPSM_BIASNEG:
119 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
120 break;
121 case D3DSPSM_SIGN:
122 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
123 break;
124 case D3DSPSM_SIGNNEG:
125 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
126 break;
127 case D3DSPSM_COMP:
128 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
129 break;
130 case D3DSPSM_X2:
131 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
132 break;
133 case D3DSPSM_X2NEG:
134 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
135 break;
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);
138 break;
139 case D3DSPSM_DW:
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);
141 break;
142 case D3DSPSM_ABS:
143 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
144 break;
145 case D3DSPSM_ABSNEG:
146 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
147 break;
148 default:
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(
157 const DWORD param,
158 const DWORD addr_token,
159 char* regstr,
160 BOOL* is_color,
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);
170 char tmpStr[50];
172 *is_color = FALSE;
174 switch (regtype) {
175 case D3DSPR_TEMP:
176 sprintf(tmpStr, "R%lu", reg);
177 break;
178 case D3DSPR_INPUT:
179 if (pshader) {
180 if (reg==0) {
181 strcpy(tmpStr, "gl_Color");
182 } else {
183 strcpy(tmpStr, "gl_SecondaryColor");
185 } else {
186 IWineD3DVertexShaderImpl *vshader = (IWineD3DVertexShaderImpl*) arg->shader;
188 if (vshader->arrayUsageMap[WINED3DSHADERDECLUSAGE_DIFFUSE] &&
189 reg == (vshader->arrayUsageMap[WINED3DSHADERDECLUSAGE_DIFFUSE] & D3DSP_REGNUM_MASK))
190 *is_color = TRUE;
192 if (vshader->arrayUsageMap[WINED3DSHADERDECLUSAGE_SPECULAR] &&
193 reg == (vshader->arrayUsageMap[WINED3DSHADERDECLUSAGE_SPECULAR] & D3DSP_REGNUM_MASK))
194 *is_color = TRUE;
196 /* FIXME: Shaders in 8.1 appear to not require a dcl statement - use
197 * the reg value from the vertex declaration. However, arrayUsageMap is not initialized
198 * in that case - how can we know if an input contains color data or not? */
200 sprintf(tmpStr, "attrib%lu", reg);
202 break;
203 case D3DSPR_CONST:
204 if (arg->reg_maps->constantsF[reg]) {
205 /* Use a local constant declared by "dcl" */
207 if (param & D3DVS_ADDRMODE_RELATIVE) {
208 /* FIXME: Copy all constants (local & global) into a single array
209 * to handle this case where we want a relative address from a
210 * local constant. */
211 FIXME("Relative addressing not yet supported on named constants\n");
212 } else {
213 sprintf(tmpStr, "C%lu", reg);
215 } else {
216 /* Use a global constant declared in Set____ShaderConstantF() */
217 if (param & D3DVS_ADDRMODE_RELATIVE) {
218 /* Relative addressing on shaders 2.0+ have a relative address token,
219 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
220 if (This->baseShader.version >= 20) {
221 char relStr[100], relReg[50], relMask[6];
222 shader_glsl_add_param(arg, addr_token, 0, TRUE, relReg, relMask, relStr);
223 sprintf(tmpStr, "C[%s + %lu]", relStr, reg);
224 } else {
225 sprintf(tmpStr, "C[A0.x + %lu]", reg);
227 } else {
228 /* Just a normal global constant - no relative addressing */
229 sprintf(tmpStr, "C[%lu]", reg);
232 break;
233 case D3DSPR_TEXTURE: /* case D3DSPR_ADDR: */
234 if (pshader) {
235 sprintf(tmpStr, "T%lu", reg);
236 } else {
237 sprintf(tmpStr, "A%lu", reg);
239 break;
240 case D3DSPR_SAMPLER:
241 sprintf(tmpStr, "mytex%lu", reg);
242 break;
243 case D3DSPR_COLOROUT:
244 if (reg == 0)
245 sprintf(tmpStr, "gl_FragColor");
246 else {
247 /* TODO: See GL_ARB_draw_buffers */
248 FIXME("Unsupported write to render target %lu\n", reg);
249 sprintf(tmpStr, "unsupported_register");
251 break;
252 case D3DSPR_RASTOUT:
253 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
254 break;
255 case D3DSPR_DEPTHOUT:
256 sprintf(tmpStr, "gl_FragDepth");
257 break;
258 case D3DSPR_ATTROUT:
259 if (reg == 0) {
260 sprintf(tmpStr, "gl_FrontColor");
261 } else {
262 sprintf(tmpStr, "gl_FrontSecondaryColor");
264 break;
265 case D3DSPR_TEXCRDOUT:
266 sprintf(tmpStr, "gl_TexCoord[%lu]", reg);
267 break;
268 default:
269 FIXME("Unhandled register name Type(%ld)\n", regtype);
270 sprintf(tmpStr, "unrecognized_register");
271 break;
274 strcat(regstr, tmpStr);
277 /* Writes the GLSL writemask for the destination register */
278 static void shader_glsl_get_output_register_swizzle(
279 const DWORD param,
280 char *write_mask) {
282 *write_mask = 0;
283 if ((param & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
284 strcat(write_mask, ".");
285 if (param & D3DSP_WRITEMASK_0) strcat(write_mask, "x");
286 if (param & D3DSP_WRITEMASK_1) strcat(write_mask, "y");
287 if (param & D3DSP_WRITEMASK_2) strcat(write_mask, "z");
288 if (param & D3DSP_WRITEMASK_3) strcat(write_mask, "w");
292 static void shader_glsl_get_input_register_swizzle(
293 const DWORD param,
294 BOOL is_color,
295 char *reg_mask) {
297 const char swizzle_reg_chars_color_fix[] = "zyxw";
298 const char swizzle_reg_chars[] = "xyzw";
299 const char* swizzle_regs = NULL;
301 /** operand input */
302 DWORD swizzle = (param & D3DVS_SWIZZLE_MASK) >> D3DVS_SWIZZLE_SHIFT;
303 DWORD swizzle_x = swizzle & 0x03;
304 DWORD swizzle_y = (swizzle >> 2) & 0x03;
305 DWORD swizzle_z = (swizzle >> 4) & 0x03;
306 DWORD swizzle_w = (swizzle >> 6) & 0x03;
308 if (is_color) {
309 swizzle_regs = swizzle_reg_chars_color_fix;
310 } else {
311 swizzle_regs = swizzle_reg_chars;
315 * swizzle bits fields:
316 * WWZZYYXX
318 if ((D3DVS_NOSWIZZLE >> D3DVS_SWIZZLE_SHIFT) == swizzle) { /* D3DVS_NOSWIZZLE == 0xE4 << D3DVS_SWIZZLE_SHIFT */
319 if (is_color) {
320 sprintf(reg_mask, ".%c%c%c%c",
321 swizzle_regs[swizzle_x],
322 swizzle_regs[swizzle_y],
323 swizzle_regs[swizzle_z],
324 swizzle_regs[swizzle_w]);
326 return ;
328 if (swizzle_x == swizzle_y &&
329 swizzle_x == swizzle_z &&
330 swizzle_x == swizzle_w)
332 sprintf(reg_mask, ".%c", swizzle_regs[swizzle_x]);
333 } else {
334 sprintf(reg_mask, ".%c%c%c%c",
335 swizzle_regs[swizzle_x],
336 swizzle_regs[swizzle_y],
337 swizzle_regs[swizzle_z],
338 swizzle_regs[swizzle_w]);
342 /** From a given parameter token, generate the corresponding GLSL string.
343 * Also, return the actual register name and swizzle in case the
344 * caller needs this information as well. */
345 static void shader_glsl_add_param(
346 SHADER_OPCODE_ARG* arg,
347 const DWORD param,
348 const DWORD addr_token,
349 BOOL is_input,
350 char *reg_name,
351 char *reg_mask,
352 char *out_str) {
354 BOOL is_color = FALSE;
355 reg_mask[0] = reg_name[0] = out_str[0] = 0;
357 shader_glsl_get_register_name(param, addr_token, reg_name, &is_color, arg);
359 if (is_input) {
360 shader_glsl_get_input_register_swizzle(param, is_color, reg_mask);
361 shader_glsl_gen_modifier(param, reg_name, reg_mask, out_str);
362 } else {
363 shader_glsl_get_output_register_swizzle(param, reg_mask);
364 sprintf(out_str, "%s%s", reg_name, reg_mask);
368 /** Process GLSL instruction modifiers */
369 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
371 if (0 != (arg->dst & D3DSP_DSTMOD_MASK)) {
372 DWORD mask = arg->dst & D3DSP_DSTMOD_MASK;
373 char dst_reg[50];
374 char dst_mask[6];
375 char dst_str[100];
377 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
379 if (mask & D3DSPDM_SATURATE) {
380 /* _SAT means to clamp the value of the register to between 0 and 1 */
381 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_reg, dst_mask, dst_reg, dst_mask);
383 if (mask & D3DSPDM_MSAMPCENTROID) {
384 FIXME("_centroid modifier not handled\n");
386 if (mask & D3DSPDM_PARTIALPRECISION) {
387 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
392 /*****************************************************************************
394 * Begin processing individual instruction opcodes
396 ****************************************************************************/
398 /* Generate GLSL arithmatic functions (dst = src1 + src2) */
399 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
401 CONST SHADER_OPCODE* curOpcode = arg->opcode;
402 SHADER_BUFFER* buffer = arg->buffer;
403 char tmpLine[256];
404 char dst_reg[50], src0_reg[50], src1_reg[50];
405 char dst_mask[6], src0_mask[6], src1_mask[6];
406 char dst_str[100], src0_str[100], src1_str[100];
408 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
409 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
410 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
411 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
412 strcat(tmpLine, src0_str);
414 /* Determine the GLSL operator to use based on the opcode */
415 switch (curOpcode->opcode) {
416 case D3DSIO_MUL: strcat(tmpLine, " * "); break;
417 case D3DSIO_ADD: strcat(tmpLine, " + "); break;
418 case D3DSIO_SUB: strcat(tmpLine, " - "); break;
419 default:
420 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
421 break;
423 shader_addline(buffer, "%s%s)%s;\n", tmpLine, src1_str, dst_mask);
426 /* Process the D3DSIO_MOV opcode using GLSL (dst = src) */
427 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
429 SHADER_BUFFER* buffer = arg->buffer;
430 char tmpLine[256];
431 char dst_str[100], src0_str[100];
432 char dst_reg[50], src0_reg[50];
433 char dst_mask[6], src0_mask[6];
435 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
436 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
437 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
438 shader_addline(buffer, "%s%s)%s;\n", tmpLine, src0_str, dst_mask);
441 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
442 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
444 CONST SHADER_OPCODE* curOpcode = arg->opcode;
445 SHADER_BUFFER* buffer = arg->buffer;
446 char tmpDest[100];
447 char dst_str[100], src0_str[100], src1_str[100];
448 char dst_reg[50], src0_reg[50], src1_reg[50];
449 char dst_mask[6], src0_mask[6], src1_mask[6];
450 char cast[6];
452 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
453 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
454 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
456 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpDest);
458 /* Need to cast the src vectors to vec3 for dp3, and vec4 for dp4 */
459 if (curOpcode->opcode == D3DSIO_DP4)
460 strcpy(cast, "vec4(");
461 else
462 strcpy(cast, "vec3(");
464 shader_addline(buffer, "%sdot(%s%s), %s%s)))%s;\n",
465 tmpDest, cast, src0_str, cast, src1_str, dst_mask);
468 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
469 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
471 CONST SHADER_OPCODE* curOpcode = arg->opcode;
472 SHADER_BUFFER* buffer = arg->buffer;
473 char tmpLine[256];
474 char dst_str[100], src_str[100];
475 char dst_reg[50], src_reg[50];
476 char dst_mask[6], src_mask[6];
477 unsigned i;
479 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
481 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
483 /* Determine the GLSL function to use based on the opcode */
484 /* TODO: Possibly make this a table for faster lookups */
485 switch (curOpcode->opcode) {
486 case D3DSIO_MIN: strcat(tmpLine, "min"); break;
487 case D3DSIO_MAX: strcat(tmpLine, "max"); break;
488 case D3DSIO_RSQ: strcat(tmpLine, "inversesqrt"); break;
489 case D3DSIO_ABS: strcat(tmpLine, "abs"); break;
490 case D3DSIO_FRC: strcat(tmpLine, "fract"); break;
491 case D3DSIO_POW: strcat(tmpLine, "pow"); break;
492 case D3DSIO_CRS: strcat(tmpLine, "cross"); break;
493 case D3DSIO_NRM: strcat(tmpLine, "normalize"); break;
494 case D3DSIO_LOG: strcat(tmpLine, "log2"); break;
495 case D3DSIO_EXPP:
496 case D3DSIO_EXP: strcat(tmpLine, "exp2"); break;
497 case D3DSIO_SGE: strcat(tmpLine, "greaterThanEqual"); break;
498 case D3DSIO_SLT: strcat(tmpLine, "lessThan"); break;
499 default:
500 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
501 break;
504 strcat(tmpLine, "(");
506 if (curOpcode->num_params > 0) {
507 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
508 strcat(tmpLine, src_str);
509 for (i = 2; i < curOpcode->num_params; ++i) {
510 strcat(tmpLine, ", ");
511 shader_glsl_add_param(arg, arg->src[i-1], arg->src_addr[i-1], TRUE, src_reg, src_mask, src_str);
512 strcat(tmpLine, src_str);
515 shader_addline(buffer, "%s))%s;\n", tmpLine, dst_mask);
519 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
520 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
522 char tmpLine[256];
523 char dst_str[100], src_str[100];
524 char dst_reg[50], src_reg[50];
525 char dst_mask[6], src_mask[6];
527 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
528 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
529 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
530 strcat(tmpLine, "1.0 / ");
531 shader_addline(arg->buffer, "%s%s)%s;\n", tmpLine, src_str, dst_mask);
534 /** Process signed comparison opcodes in GLSL. */
535 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
537 char tmpLine[256];
538 char dst_str[100], src0_str[100], src1_str[100];
539 char dst_reg[50], src0_reg[50], src1_reg[50];
540 char dst_mask[6], src0_mask[6], src1_mask[6];
542 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
543 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
544 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
546 /* If we are comparing vectors and not scalars, we should process this through map2gl using the GLSL functions. */
547 if (strlen(src0_mask) != 2) {
548 shader_glsl_map2gl(arg);
549 } else {
550 char compareStr[3];
551 compareStr[0] = 0;
552 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
554 switch (arg->opcode->opcode) {
555 case D3DSIO_SLT: strcpy(compareStr, "<"); break;
556 case D3DSIO_SGE: strcpy(compareStr, ">="); break;
557 default:
558 FIXME("Can't handle opcode %s\n", arg->opcode->name);
560 shader_addline(arg->buffer, "%s(float(%s) %s float(%s)) ? 1.0 : 0.0)%s;\n",
561 tmpLine, src0_str, compareStr, src1_str, dst_mask);
565 /** Process CMP instruction in GLSL (dst = src0.x > 0.0 ? src1.x : src2.x), per channel */
566 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
568 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
569 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
570 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
572 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
573 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
574 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
575 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
577 /* FIXME: This isn't correct - doesn't take the dst's swizzle into account. */
578 shader_addline(arg->buffer, "%s.x = (%s.x > 0.0) ? %s.x : %s.x;\n", dst_reg, src0_reg, src1_reg, src2_reg);
579 shader_addline(arg->buffer, "%s.y = (%s.y > 0.0) ? %s.y : %s.y;\n", dst_reg, src0_reg, src1_reg, src2_reg);
580 shader_addline(arg->buffer, "%s.z = (%s.z > 0.0) ? %s.z : %s.z;\n", dst_reg, src0_reg, src1_reg, src2_reg);
581 shader_addline(arg->buffer, "%s.w = (%s.w > 0.0) ? %s.w : %s.w;\n", dst_reg, src0_reg, src1_reg, src2_reg);
584 /** Process the CND opcode in GLSL (dst = (src0 < 0.5) ? src1 : src2) */
585 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
587 char tmpLine[256];
588 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
589 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
590 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
592 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
593 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
594 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
595 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
596 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
597 shader_addline(arg->buffer, "%s(%s < 0.5) ? %s : %s)%s;\n",
598 tmpLine, src0_str, src1_str, src2_str, dst_mask);
601 /** GLSL code generation for D3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
602 void shader_glsl_mad(SHADER_OPCODE_ARG* arg) {
604 char tmpLine[256];
605 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
606 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
607 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
609 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
610 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
611 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
612 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
613 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
615 shader_addline(arg->buffer, "%s((%s) * (%s)) + (%s))%s;\n",
616 tmpLine, src0_str, src1_str, src2_str, dst_mask);
619 /** Handles transforming all D3DSIO_M?x? opcodes for
620 Vertex shaders to GLSL codes */
621 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
622 int i;
623 int nComponents = 0;
624 SHADER_OPCODE_ARG tmpArg;
626 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
628 /* Set constants for the temporary argument */
629 tmpArg.shader = arg->shader;
630 tmpArg.buffer = arg->buffer;
631 tmpArg.src[0] = arg->src[0];
632 tmpArg.src_addr[0] = arg->src_addr[0];
633 tmpArg.reg_maps = arg->reg_maps;
635 switch(arg->opcode->opcode) {
636 case D3DSIO_M4x4:
637 nComponents = 4;
638 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP4];
639 break;
640 case D3DSIO_M4x3:
641 nComponents = 3;
642 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP4];
643 break;
644 case D3DSIO_M3x4:
645 nComponents = 4;
646 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP3];
647 break;
648 case D3DSIO_M3x3:
649 nComponents = 3;
650 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP3];
651 break;
652 case D3DSIO_M3x2:
653 nComponents = 2;
654 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP3];
655 break;
656 default:
657 break;
660 for (i = 0; i < nComponents; i++) {
661 tmpArg.dst = ((arg->dst) & ~D3DSP_WRITEMASK_ALL)|(D3DSP_WRITEMASK_0<<i);
662 tmpArg.src[1] = arg->src[1]+i;
663 tmpArg.src_addr[1] = arg->src[1]+i;
664 shader_glsl_dot(&tmpArg);
669 The LRP instruction performs a component-wise linear interpolation
670 between the second and third operands using the first operand as the
671 blend factor. Equation: (dst = src2 * (src1 - src0) + src0)
673 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
675 char tmpLine[256];
676 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
677 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
678 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
680 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
681 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
682 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
683 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
685 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
687 shader_addline(arg->buffer, "%s(%s * (%s - %s) + %s))%s;\n",
688 tmpLine, src2_str, src1_str, src0_str, src0_str, dst_mask);
691 /** Process the D3DSIO_DCL opcode into a GLSL string - creates a local vec4
692 * float constant, and stores it's usage on the regmaps. */
693 void shader_glsl_def(SHADER_OPCODE_ARG* arg) {
695 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
697 shader_addline(arg->buffer,
698 "const vec4 C%lu = { %f, %f, %f, %f };\n", reg,
699 *((const float *)(arg->src + 0)),
700 *((const float *)(arg->src + 1)),
701 *((const float *)(arg->src + 2)),
702 *((const float *)(arg->src + 3)) );
704 arg->reg_maps->constantsF[reg] = 1;
707 /*********************************************
708 * Pixel Shader Specific Code begins here
709 ********************************************/
710 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
712 /* FIXME: Make this work for more than just 2D textures */
714 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
715 SHADER_BUFFER* buffer = arg->buffer;
716 DWORD version = This->baseShader.version;
718 char dst_str[100], dst_reg[50], dst_mask[6];
719 char src0_str[100], src0_reg[50], src0_mask[6];
720 char src1_str[100], src1_reg[50], src1_mask[6];
721 DWORD reg_dest_code = arg->dst & D3DSP_REGNUM_MASK;
723 /* All versions have a destination register */
724 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
726 /* 1.0-1.3: Use destination register as coordinate source.
727 2.0+: Use provided coordinate source register. */
728 if (version == 14) {
729 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
730 sprintf(src1_str, "mytex%lu", reg_dest_code);
731 } else if (version > 14) {
732 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
733 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
736 /* 1.0-1.4: Use destination register number as texture code.
737 2.0+: Use provided sampler number as texure code. */
738 if (version < 14) {
739 shader_addline(buffer, "%s = texture2D(mytex%lu, gl_TexCoord[%lu].st);\n",
740 dst_str, reg_dest_code, reg_dest_code);
741 } else {
742 shader_addline(buffer, "%s = texture2D(%s, %s.st);\n", dst_str, src1_str, src0_reg);
746 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
748 /* FIXME: Make this work for more than just 2D textures */
750 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
751 SHADER_BUFFER* buffer = arg->buffer;
752 DWORD version = This->baseShader.version;
754 char tmpStr[100];
755 char tmpReg[50];
756 char tmpMask[6];
757 tmpReg[0] = 0;
759 shader_glsl_add_param(arg, arg->dst, 0, FALSE, tmpReg, tmpMask, tmpStr);
761 if (version != 14) {
762 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
763 shader_addline(buffer, "%s = gl_TexCoord[%lu];\n", tmpReg, reg);
764 } else {
765 DWORD reg2 = arg->src[0] & D3DSP_REGNUM_MASK;
766 shader_addline(buffer, "%s = gl_TexCoord[%lu]%s;\n", tmpStr, reg2, tmpMask);
770 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
772 /* FIXME: Make this work for more than just 2D textures */
774 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
775 SHADER_BUFFER* buffer = arg->buffer;
776 char src0_str[100];
777 char src0_name[50];
778 char src0_mask[6];
780 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
781 shader_addline(buffer, "tmp0.x = dot(vec3(T%lu), vec3(%s));\n", reg, src0_name, src0_mask, src0_str);
784 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
786 /* FIXME: Make this work for more than just 2D textures */
788 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
789 SHADER_BUFFER* buffer = arg->buffer;
790 char src0_str[100];
791 char src0_name[50];
792 char src0_mask[6];
794 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
795 shader_addline(buffer, "tmp0.y = dot(vec3(T%lu), vec3(%s));\n", reg, src0_str);
796 shader_addline(buffer, "T%lu = texture2D(mytex%lu, tmp0.st);\n", reg, reg);