wined3d: Cast more things.
[wine/multimedia.git] / dlls / wined3d / glsl_shader.c
blobbed5b986a3ebe145fb09d77243a4b4138178aff7
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 FIXME("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 char 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 /* Pixel shaders >= 3.0 */
181 if (D3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
182 sprintf(tmpStr, "IN%lu", reg);
183 else {
184 if (reg==0)
185 strcpy(tmpStr, "gl_Color");
186 else
187 strcpy(tmpStr, "gl_SecondaryColor");
189 } else {
190 IWineD3DVertexShaderImpl *vshader = (IWineD3DVertexShaderImpl*) arg->shader;
192 if (vshader->arrayUsageMap[WINED3DSHADERDECLUSAGE_DIFFUSE] &&
193 reg == (vshader->arrayUsageMap[WINED3DSHADERDECLUSAGE_DIFFUSE] & D3DSP_REGNUM_MASK))
194 *is_color = TRUE;
196 if (vshader->arrayUsageMap[WINED3DSHADERDECLUSAGE_SPECULAR] &&
197 reg == (vshader->arrayUsageMap[WINED3DSHADERDECLUSAGE_SPECULAR] & D3DSP_REGNUM_MASK))
198 *is_color = TRUE;
200 /* FIXME: Shaders in 8.1 appear to not require a dcl statement - use
201 * the reg value from the vertex declaration. However, arrayUsageMap is not initialized
202 * in that case - how can we know if an input contains color data or not? */
204 sprintf(tmpStr, "attrib%lu", reg);
206 break;
207 case D3DSPR_CONST:
208 if (arg->reg_maps->constantsF[reg]) {
209 /* Use a local constant declared by "dcl" */
211 if (param & D3DVS_ADDRMODE_RELATIVE) {
212 /* FIXME: Copy all constants (local & global) into a single array
213 * to handle this case where we want a relative address from a
214 * local constant. */
215 FIXME("Relative addressing not yet supported on named constants\n");
216 } else {
217 sprintf(tmpStr, "C%lu", reg);
219 } else {
220 /* Use a global constant declared in Set____ShaderConstantF() */
221 if (param & D3DVS_ADDRMODE_RELATIVE) {
222 /* Relative addressing on shaders 2.0+ have a relative address token,
223 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
224 if (D3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2) {
225 char relStr[100], relReg[50], relMask[6];
226 shader_glsl_add_param(arg, addr_token, 0, TRUE, relReg, relMask, relStr);
227 sprintf(tmpStr, "C[%s + %lu]", relStr, reg);
228 } else {
229 sprintf(tmpStr, "C[A0.x + %lu]", reg);
231 } else {
232 /* Just a normal global constant - no relative addressing */
233 sprintf(tmpStr, "C[%lu]", reg);
236 break;
237 case D3DSPR_TEXTURE: /* case D3DSPR_ADDR: */
238 if (pshader) {
239 sprintf(tmpStr, "T%lu", reg);
240 } else {
241 sprintf(tmpStr, "A%lu", reg);
243 break;
244 case D3DSPR_SAMPLER:
245 sprintf(tmpStr, "mytex%lu", reg);
246 break;
247 case D3DSPR_COLOROUT:
248 if (reg == 0)
249 sprintf(tmpStr, "gl_FragColor");
250 else {
251 /* TODO: See GL_ARB_draw_buffers */
252 FIXME("Unsupported write to render target %lu\n", reg);
253 sprintf(tmpStr, "unsupported_register");
255 break;
256 case D3DSPR_RASTOUT:
257 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
258 break;
259 case D3DSPR_DEPTHOUT:
260 sprintf(tmpStr, "gl_FragDepth");
261 break;
262 case D3DSPR_ATTROUT:
263 if (reg == 0) {
264 sprintf(tmpStr, "gl_FrontColor");
265 } else {
266 sprintf(tmpStr, "gl_FrontSecondaryColor");
268 break;
269 case D3DSPR_TEXCRDOUT:
270 /* Vertex shaders >= 3.0: D3DSPR_OUTPUT */
271 if (D3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
272 sprintf(tmpStr, "OUT%lu", reg);
273 else
274 sprintf(tmpStr, "gl_TexCoord[%lu]", reg);
275 break;
276 default:
277 FIXME("Unhandled register name Type(%ld)\n", regtype);
278 sprintf(tmpStr, "unrecognized_register");
279 break;
282 strcat(regstr, tmpStr);
285 /* Writes the GLSL writemask for the destination register */
286 static void shader_glsl_get_output_register_swizzle(
287 const DWORD param,
288 char *write_mask) {
290 *write_mask = 0;
291 if ((param & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
292 strcat(write_mask, ".");
293 if (param & D3DSP_WRITEMASK_0) strcat(write_mask, "x");
294 if (param & D3DSP_WRITEMASK_1) strcat(write_mask, "y");
295 if (param & D3DSP_WRITEMASK_2) strcat(write_mask, "z");
296 if (param & D3DSP_WRITEMASK_3) strcat(write_mask, "w");
300 static void shader_glsl_get_input_register_swizzle(
301 const DWORD param,
302 BOOL is_color,
303 char *reg_mask) {
305 const char swizzle_reg_chars_color_fix[] = "zyxw";
306 const char swizzle_reg_chars[] = "xyzw";
307 const char* swizzle_regs = NULL;
309 /** operand input */
310 DWORD swizzle = (param & D3DVS_SWIZZLE_MASK) >> D3DVS_SWIZZLE_SHIFT;
311 DWORD swizzle_x = swizzle & 0x03;
312 DWORD swizzle_y = (swizzle >> 2) & 0x03;
313 DWORD swizzle_z = (swizzle >> 4) & 0x03;
314 DWORD swizzle_w = (swizzle >> 6) & 0x03;
316 if (is_color) {
317 swizzle_regs = swizzle_reg_chars_color_fix;
318 } else {
319 swizzle_regs = swizzle_reg_chars;
323 * swizzle bits fields:
324 * WWZZYYXX
326 if ((D3DVS_NOSWIZZLE >> D3DVS_SWIZZLE_SHIFT) == swizzle) { /* D3DVS_NOSWIZZLE == 0xE4 << D3DVS_SWIZZLE_SHIFT */
327 if (is_color) {
328 sprintf(reg_mask, ".%c%c%c%c",
329 swizzle_regs[swizzle_x],
330 swizzle_regs[swizzle_y],
331 swizzle_regs[swizzle_z],
332 swizzle_regs[swizzle_w]);
334 return ;
336 if (swizzle_x == swizzle_y &&
337 swizzle_x == swizzle_z &&
338 swizzle_x == swizzle_w)
340 sprintf(reg_mask, ".%c", swizzle_regs[swizzle_x]);
341 } else {
342 sprintf(reg_mask, ".%c%c%c%c",
343 swizzle_regs[swizzle_x],
344 swizzle_regs[swizzle_y],
345 swizzle_regs[swizzle_z],
346 swizzle_regs[swizzle_w]);
350 /** From a given parameter token, generate the corresponding GLSL string.
351 * Also, return the actual register name and swizzle in case the
352 * caller needs this information as well. */
353 static void shader_glsl_add_param(
354 SHADER_OPCODE_ARG* arg,
355 const DWORD param,
356 const DWORD addr_token,
357 BOOL is_input,
358 char *reg_name,
359 char *reg_mask,
360 char *out_str) {
362 BOOL is_color = FALSE;
363 reg_mask[0] = reg_name[0] = out_str[0] = 0;
365 shader_glsl_get_register_name(param, addr_token, reg_name, &is_color, arg);
367 if (is_input) {
368 shader_glsl_get_input_register_swizzle(param, is_color, reg_mask);
369 shader_glsl_gen_modifier(param, reg_name, reg_mask, out_str);
370 } else {
371 shader_glsl_get_output_register_swizzle(param, reg_mask);
372 sprintf(out_str, "%s%s", reg_name, reg_mask);
376 /** Process GLSL instruction modifiers */
377 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
379 if (0 != (arg->dst & D3DSP_DSTMOD_MASK)) {
380 DWORD mask = arg->dst & D3DSP_DSTMOD_MASK;
381 char dst_reg[50];
382 char dst_mask[6];
383 char dst_str[100];
385 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
387 if (mask & D3DSPDM_SATURATE) {
388 /* _SAT means to clamp the value of the register to between 0 and 1 */
389 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_reg, dst_mask, dst_reg, dst_mask);
391 if (mask & D3DSPDM_MSAMPCENTROID) {
392 FIXME("_centroid modifier not handled\n");
394 if (mask & D3DSPDM_PARTIALPRECISION) {
395 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
400 /*****************************************************************************
402 * Begin processing individual instruction opcodes
404 ****************************************************************************/
406 /* Generate GLSL arithmatic functions (dst = src1 + src2) */
407 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
409 CONST SHADER_OPCODE* curOpcode = arg->opcode;
410 SHADER_BUFFER* buffer = arg->buffer;
411 char tmpLine[256];
412 char dst_reg[50], src0_reg[50], src1_reg[50];
413 char dst_mask[6], src0_mask[6], src1_mask[6];
414 char dst_str[100], src0_str[100], src1_str[100];
416 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
417 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
418 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
419 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
420 strcat(tmpLine, "vec4(");
421 strcat(tmpLine, src0_str);
422 strcat(tmpLine, ")");
424 /* Determine the GLSL operator to use based on the opcode */
425 switch (curOpcode->opcode) {
426 case D3DSIO_MUL: strcat(tmpLine, " * "); break;
427 case D3DSIO_ADD: strcat(tmpLine, " + "); break;
428 case D3DSIO_SUB: strcat(tmpLine, " - "); break;
429 default:
430 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
431 break;
433 shader_addline(buffer, "%svec4(%s))%s;\n", tmpLine, src1_str, dst_mask);
436 /* Process the D3DSIO_MOV opcode using GLSL (dst = src) */
437 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
439 SHADER_BUFFER* buffer = arg->buffer;
440 char tmpLine[256];
441 char dst_str[100], src0_str[100];
442 char dst_reg[50], src0_reg[50];
443 char dst_mask[6], src0_mask[6];
445 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
446 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
447 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
448 shader_addline(buffer, "%s%s)%s;\n", tmpLine, src0_str, dst_mask);
451 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
452 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
454 CONST SHADER_OPCODE* curOpcode = arg->opcode;
455 SHADER_BUFFER* buffer = arg->buffer;
456 char tmpDest[100];
457 char dst_str[100], src0_str[100], src1_str[100];
458 char dst_reg[50], src0_reg[50], src1_reg[50];
459 char dst_mask[6], src0_mask[6], src1_mask[6];
460 char cast[6];
462 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
463 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
464 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
466 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpDest);
468 /* Need to cast the src vectors to vec3 for dp3, and vec4 for dp4 */
469 if (curOpcode->opcode == D3DSIO_DP4)
470 strcpy(cast, "vec4(");
471 else
472 strcpy(cast, "vec3(");
474 shader_addline(buffer, "%sdot(%s%s), %s%s)))%s;\n",
475 tmpDest, cast, src0_str, cast, src1_str, dst_mask);
478 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
479 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
481 CONST SHADER_OPCODE* curOpcode = arg->opcode;
482 SHADER_BUFFER* buffer = arg->buffer;
483 char tmpLine[256];
484 char dst_str[100], src_str[100];
485 char dst_reg[50], src_reg[50];
486 char dst_mask[6], src_mask[6];
487 unsigned i;
489 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
491 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
493 /* Determine the GLSL function to use based on the opcode */
494 /* TODO: Possibly make this a table for faster lookups */
495 switch (curOpcode->opcode) {
496 case D3DSIO_MIN: strcat(tmpLine, "min"); break;
497 case D3DSIO_MAX: strcat(tmpLine, "max"); break;
498 case D3DSIO_RSQ: strcat(tmpLine, "inversesqrt"); break;
499 case D3DSIO_ABS: strcat(tmpLine, "abs"); break;
500 case D3DSIO_FRC: strcat(tmpLine, "fract"); break;
501 case D3DSIO_POW: strcat(tmpLine, "pow"); break;
502 case D3DSIO_CRS: strcat(tmpLine, "cross"); break;
503 case D3DSIO_NRM: strcat(tmpLine, "normalize"); break;
504 case D3DSIO_LOG: strcat(tmpLine, "log2"); break;
505 case D3DSIO_EXPP:
506 case D3DSIO_EXP: strcat(tmpLine, "exp2"); break;
507 case D3DSIO_SGE: strcat(tmpLine, "greaterThanEqual"); break;
508 case D3DSIO_SLT: strcat(tmpLine, "lessThan"); break;
509 default:
510 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
511 break;
514 strcat(tmpLine, "(");
516 if (curOpcode->num_params > 0) {
517 strcat(tmpLine, "vec4(");
518 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
519 strcat(tmpLine, src_str);
520 strcat(tmpLine, ")");
521 for (i = 2; i < curOpcode->num_params; ++i) {
522 strcat(tmpLine, ", vec4(");
523 shader_glsl_add_param(arg, arg->src[i-1], arg->src_addr[i-1], TRUE, src_reg, src_mask, src_str);
524 strcat(tmpLine, src_str);
525 strcat(tmpLine, ")");
528 shader_addline(buffer, "%s))%s;\n", tmpLine, dst_mask);
532 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
533 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
535 char tmpLine[256];
536 char dst_str[100], src_str[100];
537 char dst_reg[50], src_reg[50];
538 char dst_mask[6], src_mask[6];
540 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
541 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
542 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
543 strcat(tmpLine, "1.0 / ");
544 shader_addline(arg->buffer, "%s%s)%s;\n", tmpLine, src_str, dst_mask);
547 /** Process signed comparison opcodes in GLSL. */
548 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
550 char tmpLine[256];
551 char dst_str[100], src0_str[100], src1_str[100];
552 char dst_reg[50], src0_reg[50], src1_reg[50];
553 char dst_mask[6], src0_mask[6], src1_mask[6];
555 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
556 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
557 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
559 /* If we are comparing vectors and not scalars, we should process this through map2gl using the GLSL functions. */
560 if (strlen(src0_mask) != 2) {
561 shader_glsl_map2gl(arg);
562 } else {
563 char compareStr[3];
564 compareStr[0] = 0;
565 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
567 switch (arg->opcode->opcode) {
568 case D3DSIO_SLT: strcpy(compareStr, "<"); break;
569 case D3DSIO_SGE: strcpy(compareStr, ">="); break;
570 default:
571 FIXME("Can't handle opcode %s\n", arg->opcode->name);
573 shader_addline(arg->buffer, "%s(float(%s) %s float(%s)) ? 1.0 : 0.0)%s;\n",
574 tmpLine, src0_str, compareStr, src1_str, dst_mask);
578 /** Process CMP instruction in GLSL (dst = src0.x > 0.0 ? src1.x : src2.x), per channel */
579 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
581 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
582 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
583 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
585 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
586 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
587 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
588 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
590 /* FIXME: This isn't correct - doesn't take the dst's swizzle into account. */
591 shader_addline(arg->buffer, "%s.x = (%s.x > 0.0) ? %s.x : %s.x;\n", dst_reg, src0_reg, src1_reg, src2_reg);
592 shader_addline(arg->buffer, "%s.y = (%s.y > 0.0) ? %s.y : %s.y;\n", dst_reg, src0_reg, src1_reg, src2_reg);
593 shader_addline(arg->buffer, "%s.z = (%s.z > 0.0) ? %s.z : %s.z;\n", dst_reg, src0_reg, src1_reg, src2_reg);
594 shader_addline(arg->buffer, "%s.w = (%s.w > 0.0) ? %s.w : %s.w;\n", dst_reg, src0_reg, src1_reg, src2_reg);
597 /** Process the CND opcode in GLSL (dst = (src0 < 0.5) ? src1 : src2) */
598 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
600 char tmpLine[256];
601 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
602 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
603 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
605 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
606 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
607 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
608 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
609 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
610 shader_addline(arg->buffer, "%s(%s < 0.5) ? %s : %s)%s;\n",
611 tmpLine, src0_str, src1_str, src2_str, dst_mask);
614 /** GLSL code generation for D3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
615 void shader_glsl_mad(SHADER_OPCODE_ARG* arg) {
617 char tmpLine[256];
618 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
619 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
620 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
622 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
623 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
624 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
625 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
626 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
628 shader_addline(arg->buffer, "%s(vec4(%s) * vec4(%s)) + vec4(%s))%s;\n",
629 tmpLine, src0_str, src1_str, src2_str, dst_mask);
632 /** Handles transforming all D3DSIO_M?x? opcodes for
633 Vertex shaders to GLSL codes */
634 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
635 int i;
636 int nComponents = 0;
637 SHADER_OPCODE_ARG tmpArg;
639 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
641 /* Set constants for the temporary argument */
642 tmpArg.shader = arg->shader;
643 tmpArg.buffer = arg->buffer;
644 tmpArg.src[0] = arg->src[0];
645 tmpArg.src_addr[0] = arg->src_addr[0];
646 tmpArg.reg_maps = arg->reg_maps;
648 switch(arg->opcode->opcode) {
649 case D3DSIO_M4x4:
650 nComponents = 4;
651 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP4];
652 break;
653 case D3DSIO_M4x3:
654 nComponents = 3;
655 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP4];
656 break;
657 case D3DSIO_M3x4:
658 nComponents = 4;
659 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP3];
660 break;
661 case D3DSIO_M3x3:
662 nComponents = 3;
663 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP3];
664 break;
665 case D3DSIO_M3x2:
666 nComponents = 2;
667 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP3];
668 break;
669 default:
670 break;
673 for (i = 0; i < nComponents; i++) {
674 tmpArg.dst = ((arg->dst) & ~D3DSP_WRITEMASK_ALL)|(D3DSP_WRITEMASK_0<<i);
675 tmpArg.src[1] = arg->src[1]+i;
676 tmpArg.src_addr[1] = arg->src[1]+i;
677 shader_glsl_dot(&tmpArg);
682 The LRP instruction performs a component-wise linear interpolation
683 between the second and third operands using the first operand as the
684 blend factor. Equation: (dst = src2 * (src1 - src0) + src0)
686 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
688 char tmpLine[256];
689 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
690 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
691 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
693 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
694 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
695 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
696 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
698 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
700 shader_addline(arg->buffer, "%s(%s * (%s - %s) + %s))%s;\n",
701 tmpLine, src2_str, src1_str, src0_str, src0_str, dst_mask);
704 /** Process the D3DSIO_DCL opcode into a GLSL string - creates a local vec4
705 * float constant, and stores it's usage on the regmaps. */
706 void shader_glsl_def(SHADER_OPCODE_ARG* arg) {
708 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
710 shader_addline(arg->buffer,
711 "const vec4 C%lu = { %f, %f, %f, %f };\n", reg,
712 *((const float *)(arg->src + 0)),
713 *((const float *)(arg->src + 1)),
714 *((const float *)(arg->src + 2)),
715 *((const float *)(arg->src + 3)) );
717 arg->reg_maps->constantsF[reg] = 1;
720 /*********************************************
721 * Pixel Shader Specific Code begins here
722 ********************************************/
723 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
725 /* FIXME: Make this work for more than just 2D textures */
727 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
728 SHADER_BUFFER* buffer = arg->buffer;
729 DWORD hex_version = This->baseShader.hex_version;
731 char dst_str[100], dst_reg[50], dst_mask[6];
732 char src0_str[100], src0_reg[50], src0_mask[6];
733 char src1_str[100], src1_reg[50], src1_mask[6];
734 DWORD reg_dest_code = arg->dst & D3DSP_REGNUM_MASK;
736 /* All versions have a destination register */
737 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
739 /* 1.0-1.3: Use destination register as coordinate source.
740 * 2.0+: Use provided coordinate source register. */
741 if (hex_version == D3DPS_VERSION(1,4)) {
742 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
743 sprintf(src1_str, "mytex%lu", reg_dest_code);
744 } else if (hex_version > D3DPS_VERSION(1,4)) {
745 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
746 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
749 /* 1.0-1.4: Use destination register number as texture code.
750 * 2.0+: Use provided sampler number as texure code. */
751 if (hex_version < D3DPS_VERSION(1,4)) {
752 shader_addline(buffer, "%s = texture2D(mytex%lu, gl_TexCoord[%lu].st);\n",
753 dst_str, reg_dest_code, reg_dest_code);
754 } else {
755 shader_addline(buffer, "%s = texture2D(%s, %s.st);\n", dst_str, src1_str, src0_reg);
759 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
761 /* FIXME: Make this work for more than just 2D textures */
763 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
764 SHADER_BUFFER* buffer = arg->buffer;
765 DWORD hex_version = This->baseShader.hex_version;
767 char tmpStr[100];
768 char tmpReg[50];
769 char tmpMask[6];
770 tmpReg[0] = 0;
772 shader_glsl_add_param(arg, arg->dst, 0, FALSE, tmpReg, tmpMask, tmpStr);
774 if (hex_version != D3DPS_VERSION(1,4)) {
775 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
776 shader_addline(buffer, "%s = gl_TexCoord[%lu];\n", tmpReg, reg);
777 } else {
778 DWORD reg2 = arg->src[0] & D3DSP_REGNUM_MASK;
779 shader_addline(buffer, "%s = gl_TexCoord[%lu]%s;\n", tmpStr, reg2, tmpMask);
783 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
785 /* FIXME: Make this work for more than just 2D textures */
787 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
788 SHADER_BUFFER* buffer = arg->buffer;
789 char src0_str[100];
790 char src0_name[50];
791 char src0_mask[6];
793 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
794 shader_addline(buffer, "tmp0.x = dot(vec3(T%lu), vec3(%s));\n", reg, src0_name, src0_mask, src0_str);
797 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
799 /* FIXME: Make this work for more than just 2D textures */
801 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
802 SHADER_BUFFER* buffer = arg->buffer;
803 char src0_str[100];
804 char src0_name[50];
805 char src0_mask[6];
807 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
808 shader_addline(buffer, "tmp0.y = dot(vec3(T%lu), vec3(%s));\n", reg, src0_str);
809 shader_addline(buffer, "T%lu = texture2D(mytex%lu, tmp0.st);\n", reg, reg);
812 void pshader_glsl_input_pack(
813 SHADER_BUFFER* buffer,
814 DWORD* semantics_in) {
816 unsigned int i;
818 for (i = 0; i < WINED3DSHADERDECLUSAGE_MAX_USAGE; i++) {
820 DWORD reg = semantics_in[i];
821 unsigned int regnum = reg & D3DSP_REGNUM_MASK;
822 char reg_mask[6];
824 /* Uninitialized */
825 if (!reg) continue;
827 shader_glsl_get_output_register_swizzle(reg, reg_mask);
829 switch(i) {
831 case WINED3DSHADERDECLUSAGE_DIFFUSE:
832 shader_addline(buffer, "IN%lu%s = vec4(gl_Color)%s;\n",
833 regnum, reg_mask, reg_mask);
834 break;
836 case WINED3DSHADERDECLUSAGE_SPECULAR:
837 shader_addline(buffer, "IN%lu%s = vec4(gl_SecondaryColor)%s;\n",
838 regnum, reg_mask, reg_mask);
839 break;
841 case WINED3DSHADERDECLUSAGE_TEXCOORD0:
842 case WINED3DSHADERDECLUSAGE_TEXCOORD1:
843 case WINED3DSHADERDECLUSAGE_TEXCOORD2:
844 case WINED3DSHADERDECLUSAGE_TEXCOORD3:
845 case WINED3DSHADERDECLUSAGE_TEXCOORD4:
846 case WINED3DSHADERDECLUSAGE_TEXCOORD5:
847 case WINED3DSHADERDECLUSAGE_TEXCOORD6:
848 case WINED3DSHADERDECLUSAGE_TEXCOORD7:
849 shader_addline(buffer, "IN%lu%s = vec4(gl_TexCoord[%lu])%s;\n",
850 regnum, reg_mask, i - WINED3DSHADERDECLUSAGE_TEXCOORD0, reg_mask );
851 break;
853 case WINED3DSHADERDECLUSAGE_FOG:
854 shader_addline(buffer, "IN%lu%s = vec4(gl_FogFragCoord)%s;\n",
855 regnum, reg_mask, reg_mask);
856 break;
858 default:
859 shader_addline(buffer, "IN%lu%s = vec4(unsupported_input)%s;\n",
860 regnum, reg_mask, reg_mask);
865 /*********************************************
866 * Vertex Shader Specific Code begins here
867 ********************************************/
869 void vshader_glsl_output_unpack(
870 SHADER_BUFFER* buffer,
871 DWORD* semantics_out) {
873 unsigned int i;
875 for (i = 0; i < WINED3DSHADERDECLUSAGE_MAX_USAGE; i++) {
877 DWORD reg = semantics_out[i];
878 unsigned int regnum = reg & D3DSP_REGNUM_MASK;
879 char reg_mask[6];
881 /* Uninitialized */
882 if (!reg) continue;
884 shader_glsl_get_output_register_swizzle(reg, reg_mask);
886 switch(i) {
888 case WINED3DSHADERDECLUSAGE_DIFFUSE:
889 shader_addline(buffer, "gl_FrontColor%s = OUT%lu%s;\n", reg_mask, regnum, reg_mask);
890 break;
892 case WINED3DSHADERDECLUSAGE_SPECULAR:
893 shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT%lu%s;\n", reg_mask, regnum, reg_mask);
894 break;
896 case WINED3DSHADERDECLUSAGE_POSITION:
897 shader_addline(buffer, "gl_Position%s = OUT%lu%s;\n", reg_mask, regnum, reg_mask);
898 break;
900 case WINED3DSHADERDECLUSAGE_TEXCOORD0:
901 case WINED3DSHADERDECLUSAGE_TEXCOORD1:
902 case WINED3DSHADERDECLUSAGE_TEXCOORD2:
903 case WINED3DSHADERDECLUSAGE_TEXCOORD3:
904 case WINED3DSHADERDECLUSAGE_TEXCOORD4:
905 case WINED3DSHADERDECLUSAGE_TEXCOORD5:
906 case WINED3DSHADERDECLUSAGE_TEXCOORD6:
907 case WINED3DSHADERDECLUSAGE_TEXCOORD7:
908 shader_addline(buffer, "gl_TexCoord[%lu]%s = OUT%lu%s;\n",
909 i - WINED3DSHADERDECLUSAGE_TEXCOORD0, reg_mask, regnum, reg_mask);
910 break;
912 case WINED3DSHADERDECLUSAGE_PSIZE:
913 shader_addline(buffer, "gl_PointSize = OUT%lu.x;\n", regnum);
914 break;
916 case WINED3DSHADERDECLUSAGE_FOG:
917 shader_addline(buffer, "gl_FogFragCoord%s = OUT%lu%s;\n", reg_mask, regnum, reg_mask);
918 break;
920 default:
921 shader_addline(buffer, "unsupported_output%s = OUT%lu%s;\n", reg_mask, regnum, reg_mask);