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 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
46 HeapFree(GetProcessHeap(), 0, infoLog);
51 * Loads (pixel shader) samplers
53 void shader_glsl_load_psamplers(
54 WineD3D_GL_Info *gl_info,
55 IWineD3DStateBlock* iface) {
57 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
58 GLhandleARB programId = stateBlock->shaderPrgId;
61 char sampler_name[20];
63 for (i=0; i< GL_LIMITS(textures); ++i) {
64 if (stateBlock->textures[i] != NULL) {
65 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
66 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
68 TRACE_(d3d_shader)("Loading %s for texture %d\n", sampler_name, i);
69 GL_EXTCALL(glUniform1iARB(name_loc, i));
70 checkGLcall("glUniform1iARB");
77 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
78 * When @constants_set == NULL, it will load all the constants.
80 void shader_glsl_load_constantsF(
81 WineD3D_GL_Info *gl_info,
82 GLhandleARB programId,
83 unsigned max_constants,
91 const char* prefix = is_pshader? "PC":"VC";
93 for (i=0; i<max_constants; ++i) {
94 if (NULL == constants_set || constants_set[i]) {
96 TRACE("Loading constants %i: %f, %f, %f, %f\n", i,
97 constants[i * sizeof(float) + 0], constants[i * sizeof(float) + 1],
98 constants[i * sizeof(float) + 2], constants[i * sizeof(float) + 3]);
100 /* TODO: Benchmark and see if it would be beneficial to store the
101 * locations of the constants to avoid looking up each time */
102 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
103 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
105 /* We found this uniform name in the program - go ahead and send the data */
106 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, &constants[i * sizeof(float)]));
107 checkGLcall("glUniform4fvARB");
114 * Loads the app-supplied constants into the currently set GLSL program.
116 void shader_glsl_load_constants(
117 IWineD3DStateBlock* iface,
119 char useVertexShader) {
121 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
122 WineD3D_GL_Info *gl_info = &((IWineD3DImpl*)stateBlock->wineD3DDevice->wineD3D)->gl_info;
123 GLhandleARB programId = stateBlock->shaderPrgId;
125 if (programId == 0) {
126 /* No GLSL program set - nothing to do. */
130 if (useVertexShader) {
131 IWineD3DVertexShaderImpl* vshader = (IWineD3DVertexShaderImpl*) stateBlock->vertexShader;
132 IWineD3DVertexDeclarationImpl* vertexDeclaration =
133 (IWineD3DVertexDeclarationImpl*) vshader->vertexDeclaration;
135 if (NULL != vertexDeclaration && NULL != vertexDeclaration->constants) {
136 /* Load DirectX 8 float constants/uniforms for vertex shader */
137 shader_glsl_load_constantsF(gl_info, programId, WINED3D_VSHADER_MAX_CONSTANTS,
138 vertexDeclaration->constants, NULL, 0);
141 /* Load DirectX 9 float constants/uniforms for vertex shader */
142 shader_glsl_load_constantsF(gl_info, programId, WINED3D_VSHADER_MAX_CONSTANTS,
143 stateBlock->vertexShaderConstantF,
144 stateBlock->set.vertexShaderConstantsF, 0);
146 /* TODO: Load boolean & integer constants for vertex shader */
149 if (usePixelShader) {
151 /* Load pixel shader samplers */
152 shader_glsl_load_psamplers(gl_info, iface);
154 /* Load DirectX 9 float constants/uniforms for pixel shader */
155 shader_glsl_load_constantsF(gl_info, programId, WINED3D_PSHADER_MAX_CONSTANTS,
156 stateBlock->pixelShaderConstantF,
157 stateBlock->set.pixelShaderConstantsF, 1);
159 /* TODO: Load boolean & integer constants for pixel shader */
163 /** Generate the variable & register declarations for the GLSL output target */
164 void shader_generate_glsl_declarations(
165 IWineD3DBaseShader *iface,
166 shader_reg_maps* reg_maps,
167 SHADER_BUFFER* buffer) {
169 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
172 /* There are some minor differences between pixel and vertex shaders */
173 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
174 char prefix = pshader ? 'P' : 'V';
176 /* Declare the constants (aka uniforms) */
177 if (This->baseShader.limits.constant_float > 0)
178 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, This->baseShader.limits.constant_float);
180 if (This->baseShader.limits.constant_int > 0)
181 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
183 if (This->baseShader.limits.constant_bool > 0)
184 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
186 /* Declare texture samplers */
187 for (i = 0; i < This->baseShader.limits.sampler; i++) {
188 if (reg_maps->samplers[i]) {
190 DWORD stype = reg_maps->samplers[i] & D3DSP_TEXTURETYPE_MASK;
194 shader_addline(buffer, "uniform sampler2D %csampler%lu;\n", prefix, i);
197 shader_addline(buffer, "uniform samplerCube %csampler%lu;\n", prefix, i);
200 shader_addline(buffer, "uniform sampler3D %csampler%lu;\n", prefix, i);
203 shader_addline(buffer, "uniform unsupported_sampler %csampler%lu;\n", prefix, i);
204 FIXME("Unrecognized sampler type: %#lx\n", stype);
210 /* Declare address variables */
211 for (i = 0; i < This->baseShader.limits.address; i++) {
212 if (reg_maps->address[i])
213 shader_addline(buffer, "ivec4 A%ld;\n", i);
216 /* Declare texture coordinate temporaries and initialize them */
217 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
218 if (reg_maps->texcoord[i])
219 shader_addline(buffer, "vec4 T%lu = gl_TexCoord[%lu];\n", i, i);
222 /* Declare input register temporaries */
223 for (i=0; i < This->baseShader.limits.packed_input; i++) {
224 if (reg_maps->packed_input[i])
225 shader_addline(buffer, "vec4 IN%lu;\n", i);
228 /* Declare output register temporaries */
229 for (i = 0; i < This->baseShader.limits.packed_output; i++) {
230 if (reg_maps->packed_output[i])
231 shader_addline(buffer, "vec4 OUT%lu;\n", i);
234 /* Declare temporary variables */
235 for(i = 0; i < This->baseShader.limits.temporary; i++) {
236 if (reg_maps->temporary[i])
237 shader_addline(buffer, "vec4 R%lu;\n", i);
240 /* Declare attributes */
241 for (i = 0; i < This->baseShader.limits.attributes; i++) {
242 if (reg_maps->attributes[i])
243 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
246 /* Declare loop register aL */
248 shader_addline(buffer, "int aL;\n");
250 /* Temporary variables for matrix operations */
251 shader_addline(buffer, "vec4 tmp0;\n");
252 shader_addline(buffer, "vec4 tmp1;\n");
254 /* Start the main program */
255 shader_addline(buffer, "void main() {\n");
258 /*****************************************************************************
259 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
261 * For more information, see http://wiki.winehq.org/DirectX-Shaders
262 ****************************************************************************/
265 static void shader_glsl_add_param(
266 SHADER_OPCODE_ARG* arg,
268 const DWORD addr_token,
274 /** Used for opcode modifiers - They multiply the result by the specified amount */
275 static const char* shift_glsl_tab[] = {
277 "2.0 * ", /* 1 (x2) */
278 "4.0 * ", /* 2 (x4) */
279 "8.0 * ", /* 3 (x8) */
280 "16.0 * ", /* 4 (x16) */
281 "32.0 * ", /* 5 (x32) */
288 "0.0625 * ", /* 12 (d16) */
289 "0.125 * ", /* 13 (d8) */
290 "0.25 * ", /* 14 (d4) */
291 "0.5 * " /* 15 (d2) */
294 /** Print the beginning of the generated GLSL string. example: "reg_name.xyzw = vec4(" */
295 static void shader_glsl_add_dst(DWORD param, const char* reg_name, const char* reg_mask, char* outStr) {
297 int shift = (param & D3DSP_DSTSHIFT_MASK) >> D3DSP_DSTSHIFT_SHIFT;
299 /* TODO: determine if destination is anything other than a float vector and accommodate*/
300 if (reg_name[0] == 'A')
301 sprintf(outStr, "%s%s = %sivec4(", reg_name, reg_mask, shift_glsl_tab[shift]);
303 sprintf(outStr, "%s%s = %svec4(", reg_name, reg_mask, shift_glsl_tab[shift]);
307 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
308 static void shader_glsl_gen_modifier (
311 const char *in_regswizzle,
316 switch (instr & D3DSP_SRCMOD_MASK) {
318 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
321 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
324 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
326 case D3DSPSM_BIASNEG:
327 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
330 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
332 case D3DSPSM_SIGNNEG:
333 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
336 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
339 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
342 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
344 case D3DSPSM_DZ: /* reg1_db = { reg1.r/b, reg1.g/b, ...} The g & a components are undefined, so we'll leave them alone */
345 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);
348 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);
351 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
354 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
357 FIXME("Unhandled modifier %lu\n", (instr & D3DSP_SRCMOD_MASK));
358 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
362 /** Writes the GLSL variable name that corresponds to the register that the
363 * DX opcode parameter is trying to access */
364 static void shader_glsl_get_register_name(
366 const DWORD addr_token,
369 SHADER_OPCODE_ARG* arg) {
371 /* oPos, oFog and oPts in D3D */
372 const char* hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
374 DWORD reg = param & D3DSP_REGNUM_MASK;
375 DWORD regtype = shader_get_regtype(param);
376 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
377 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
384 sprintf(tmpStr, "R%lu", reg);
388 /* Pixel shaders >= 3.0 */
389 if (D3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
390 sprintf(tmpStr, "IN%lu", reg);
393 strcpy(tmpStr, "gl_Color");
395 strcpy(tmpStr, "gl_SecondaryColor");
398 IWineD3DVertexShaderImpl *vshader = (IWineD3DVertexShaderImpl*) arg->shader;
400 if (vshader->arrayUsageMap[WINED3DSHADERDECLUSAGE_DIFFUSE] &&
401 reg == (vshader->arrayUsageMap[WINED3DSHADERDECLUSAGE_DIFFUSE] & D3DSP_REGNUM_MASK))
404 if (vshader->arrayUsageMap[WINED3DSHADERDECLUSAGE_SPECULAR] &&
405 reg == (vshader->arrayUsageMap[WINED3DSHADERDECLUSAGE_SPECULAR] & D3DSP_REGNUM_MASK))
408 /* FIXME: Shaders in 8.1 appear to not require a dcl statement - use
409 * the reg value from the vertex declaration. However, arrayUsageMap is not initialized
410 * in that case - how can we know if an input contains color data or not? */
412 sprintf(tmpStr, "attrib%lu", reg);
417 const char* prefix = pshader? "PC":"VC";
419 if (arg->reg_maps->constantsF[reg]) {
420 /* Use a local constant declared by "def" */
422 if (param & D3DVS_ADDRMODE_RELATIVE) {
423 /* FIXME: Copy all constants (local & global) into a single array
424 * to handle this case where we want a relative address from a
426 FIXME("Relative addressing not yet supported on named constants\n");
428 sprintf(tmpStr, "%s%lu", prefix, reg);
431 /* Use a global constant declared in Set____ShaderConstantF() */
432 if (param & D3DVS_ADDRMODE_RELATIVE) {
433 /* Relative addressing on shaders 2.0+ have a relative address token,
434 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
435 if (D3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2) {
436 char relStr[100], relReg[50], relMask[6];
437 shader_glsl_add_param(arg, addr_token, 0, TRUE, relReg, relMask, relStr);
438 sprintf(tmpStr, "%s[%s + %lu]", prefix, relStr, reg);
440 sprintf(tmpStr, "%s[A0.x + %lu]", prefix, reg);
443 /* Just a normal global constant - no relative addressing */
444 sprintf(tmpStr, "%s[%lu]", prefix, reg);
449 case D3DSPR_CONSTINT:
450 if (arg->reg_maps->constantsI[reg]) {
451 /* Integer vector was defined locally using "defi" */
452 sprintf(tmpStr, "I%lu", reg);
454 /* Uniform integer value - pixel or vertex specific */
456 sprintf(tmpStr, "PI[%lu]", reg);
458 sprintf(tmpStr, "VI[%lu]", reg);
462 case D3DSPR_CONSTBOOL:
463 if (arg->reg_maps->constantsB[reg]) {
464 /* Boolean was defined locally using "defb" */
465 sprintf(tmpStr, "B%lu", reg);
467 /* Uniform boolean value - pixel or vertex specific */
469 sprintf(tmpStr, "PB[%lu]", reg);
471 sprintf(tmpStr, "VB[%lu]", reg);
475 case D3DSPR_TEXTURE: /* case D3DSPR_ADDR: */
477 sprintf(tmpStr, "T%lu", reg);
479 sprintf(tmpStr, "A%lu", reg);
483 sprintf(tmpStr, "aL");
487 sprintf(tmpStr, "Psampler%lu", reg);
489 sprintf(tmpStr, "Vsampler%lu", reg);
491 case D3DSPR_COLOROUT:
493 sprintf(tmpStr, "gl_FragColor");
495 /* TODO: See GL_ARB_draw_buffers */
496 FIXME("Unsupported write to render target %lu\n", reg);
497 sprintf(tmpStr, "unsupported_register");
501 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
503 case D3DSPR_DEPTHOUT:
504 sprintf(tmpStr, "gl_FragDepth");
508 sprintf(tmpStr, "gl_FrontColor");
510 sprintf(tmpStr, "gl_FrontSecondaryColor");
513 case D3DSPR_TEXCRDOUT:
514 /* Vertex shaders >= 3.0: D3DSPR_OUTPUT */
515 if (D3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
516 sprintf(tmpStr, "OUT%lu", reg);
518 sprintf(tmpStr, "gl_TexCoord[%lu]", reg);
521 FIXME("Unhandled register name Type(%ld)\n", regtype);
522 sprintf(tmpStr, "unrecognized_register");
526 strcat(regstr, tmpStr);
529 /* Writes the GLSL writemask for the destination register */
530 static void shader_glsl_get_output_register_swizzle(
535 if ((param & D3DSP_WRITEMASK_ALL) != D3DSP_WRITEMASK_ALL) {
536 strcat(write_mask, ".");
537 if (param & D3DSP_WRITEMASK_0) strcat(write_mask, "x");
538 if (param & D3DSP_WRITEMASK_1) strcat(write_mask, "y");
539 if (param & D3DSP_WRITEMASK_2) strcat(write_mask, "z");
540 if (param & D3DSP_WRITEMASK_3) strcat(write_mask, "w");
544 static void shader_glsl_get_input_register_swizzle(
549 const char swizzle_reg_chars_color_fix[] = "zyxw";
550 const char swizzle_reg_chars[] = "xyzw";
551 const char* swizzle_regs = NULL;
554 DWORD swizzle = (param & D3DVS_SWIZZLE_MASK) >> D3DVS_SWIZZLE_SHIFT;
555 DWORD swizzle_x = swizzle & 0x03;
556 DWORD swizzle_y = (swizzle >> 2) & 0x03;
557 DWORD swizzle_z = (swizzle >> 4) & 0x03;
558 DWORD swizzle_w = (swizzle >> 6) & 0x03;
561 swizzle_regs = swizzle_reg_chars_color_fix;
563 swizzle_regs = swizzle_reg_chars;
567 * swizzle bits fields:
570 if ((D3DVS_NOSWIZZLE >> D3DVS_SWIZZLE_SHIFT) == swizzle) { /* D3DVS_NOSWIZZLE == 0xE4 << D3DVS_SWIZZLE_SHIFT */
572 sprintf(reg_mask, ".%c%c%c%c",
573 swizzle_regs[swizzle_x],
574 swizzle_regs[swizzle_y],
575 swizzle_regs[swizzle_z],
576 swizzle_regs[swizzle_w]);
580 if (swizzle_x == swizzle_y &&
581 swizzle_x == swizzle_z &&
582 swizzle_x == swizzle_w)
584 sprintf(reg_mask, ".%c", swizzle_regs[swizzle_x]);
586 sprintf(reg_mask, ".%c%c%c%c",
587 swizzle_regs[swizzle_x],
588 swizzle_regs[swizzle_y],
589 swizzle_regs[swizzle_z],
590 swizzle_regs[swizzle_w]);
594 /** From a given parameter token, generate the corresponding GLSL string.
595 * Also, return the actual register name and swizzle in case the
596 * caller needs this information as well. */
597 static void shader_glsl_add_param(
598 SHADER_OPCODE_ARG* arg,
600 const DWORD addr_token,
606 BOOL is_color = FALSE;
607 reg_mask[0] = reg_name[0] = out_str[0] = 0;
609 shader_glsl_get_register_name(param, addr_token, reg_name, &is_color, arg);
612 shader_glsl_get_input_register_swizzle(param, is_color, reg_mask);
613 shader_glsl_gen_modifier(param, reg_name, reg_mask, out_str);
615 shader_glsl_get_output_register_swizzle(param, reg_mask);
616 sprintf(out_str, "%s%s", reg_name, reg_mask);
620 /** Process GLSL instruction modifiers */
621 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
623 if (0 != (arg->dst & D3DSP_DSTMOD_MASK)) {
624 DWORD mask = arg->dst & D3DSP_DSTMOD_MASK;
629 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
631 if (mask & D3DSPDM_SATURATE) {
632 /* _SAT means to clamp the value of the register to between 0 and 1 */
633 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_reg, dst_mask, dst_reg, dst_mask);
635 if (mask & D3DSPDM_MSAMPCENTROID) {
636 FIXME("_centroid modifier not handled\n");
638 if (mask & D3DSPDM_PARTIALPRECISION) {
639 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
644 /*****************************************************************************
646 * Begin processing individual instruction opcodes
648 ****************************************************************************/
650 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
651 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
653 CONST SHADER_OPCODE* curOpcode = arg->opcode;
654 SHADER_BUFFER* buffer = arg->buffer;
656 char dst_reg[50], src0_reg[50], src1_reg[50];
657 char dst_mask[6], src0_mask[6], src1_mask[6];
658 char dst_str[100], src0_str[100], src1_str[100];
660 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
661 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
662 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
663 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
664 strcat(tmpLine, "vec4(");
665 strcat(tmpLine, src0_str);
666 strcat(tmpLine, ")");
668 /* Determine the GLSL operator to use based on the opcode */
669 switch (curOpcode->opcode) {
670 case D3DSIO_MUL: strcat(tmpLine, " * "); break;
671 case D3DSIO_ADD: strcat(tmpLine, " + "); break;
672 case D3DSIO_SUB: strcat(tmpLine, " - "); break;
674 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
677 shader_addline(buffer, "%svec4(%s))%s;\n", tmpLine, src1_str, dst_mask);
680 /* Process the D3DSIO_MOV opcode using GLSL (dst = src) */
681 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
683 SHADER_BUFFER* buffer = arg->buffer;
685 char dst_str[100], src0_str[100];
686 char dst_reg[50], src0_reg[50];
687 char dst_mask[6], src0_mask[6];
689 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
690 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
691 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
692 shader_addline(buffer, "%s%s)%s;\n", tmpLine, src0_str, dst_mask);
695 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
696 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
698 CONST SHADER_OPCODE* curOpcode = arg->opcode;
699 SHADER_BUFFER* buffer = arg->buffer;
701 char dst_str[100], src0_str[100], src1_str[100];
702 char dst_reg[50], src0_reg[50], src1_reg[50];
703 char dst_mask[6], src0_mask[6], src1_mask[6];
706 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
707 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
708 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
710 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpDest);
712 /* Need to cast the src vectors to vec3 for dp3, and vec4 for dp4 */
713 if (curOpcode->opcode == D3DSIO_DP4)
714 strcpy(cast, "vec4(");
716 strcpy(cast, "vec3(");
718 shader_addline(buffer, "%sdot(%s%s), %s%s)))%s;\n",
719 tmpDest, cast, src0_str, cast, src1_str, dst_mask);
722 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
723 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
725 CONST SHADER_OPCODE* curOpcode = arg->opcode;
726 SHADER_BUFFER* buffer = arg->buffer;
728 char dst_str[100], src_str[100];
729 char dst_reg[50], src_reg[50];
730 char dst_mask[6], src_mask[6];
733 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
735 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
737 /* Determine the GLSL function to use based on the opcode */
738 /* TODO: Possibly make this a table for faster lookups */
739 switch (curOpcode->opcode) {
740 case D3DSIO_MIN: strcat(tmpLine, "min"); break;
741 case D3DSIO_MAX: strcat(tmpLine, "max"); break;
742 case D3DSIO_RSQ: strcat(tmpLine, "inversesqrt"); break;
743 case D3DSIO_ABS: strcat(tmpLine, "abs"); break;
744 case D3DSIO_FRC: strcat(tmpLine, "fract"); break;
745 case D3DSIO_POW: strcat(tmpLine, "pow"); break;
746 case D3DSIO_CRS: strcat(tmpLine, "cross"); break;
747 case D3DSIO_NRM: strcat(tmpLine, "normalize"); break;
749 case D3DSIO_LOG: strcat(tmpLine, "log2"); break;
751 case D3DSIO_EXP: strcat(tmpLine, "exp2"); break;
752 case D3DSIO_SGE: strcat(tmpLine, "greaterThanEqual"); break;
753 case D3DSIO_SLT: strcat(tmpLine, "lessThan"); break;
754 case D3DSIO_SGN: strcat(tmpLine, "sign"); break;
756 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
760 strcat(tmpLine, "(");
762 if (curOpcode->num_params > 0) {
763 strcat(tmpLine, "vec4(");
764 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
765 strcat(tmpLine, src_str);
766 strcat(tmpLine, ")");
767 for (i = 2; i < curOpcode->num_params; ++i) {
768 strcat(tmpLine, ", vec4(");
769 shader_glsl_add_param(arg, arg->src[i-1], arg->src_addr[i-1], TRUE, src_reg, src_mask, src_str);
770 strcat(tmpLine, src_str);
771 strcat(tmpLine, ")");
774 shader_addline(buffer, "%s))%s;\n", tmpLine, dst_mask);
778 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
779 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
782 char dst_str[100], src_str[100];
783 char dst_reg[50], src_reg[50];
784 char dst_mask[6], src_mask[6];
786 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
787 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
788 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
789 strcat(tmpLine, "1.0 / ");
790 shader_addline(arg->buffer, "%s%s)%s;\n", tmpLine, src_str, dst_mask);
793 /** Process signed comparison opcodes in GLSL. */
794 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
797 char dst_str[100], src0_str[100], src1_str[100];
798 char dst_reg[50], src0_reg[50], src1_reg[50];
799 char dst_mask[6], src0_mask[6], src1_mask[6];
801 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
802 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
803 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
805 /* If we are comparing vectors and not scalars, we should process this through map2gl using the GLSL functions. */
806 if (strlen(src0_mask) != 2) {
807 shader_glsl_map2gl(arg);
811 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
813 switch (arg->opcode->opcode) {
814 case D3DSIO_SLT: strcpy(compareStr, "<"); break;
815 case D3DSIO_SGE: strcpy(compareStr, ">="); break;
817 FIXME("Can't handle opcode %s\n", arg->opcode->name);
819 shader_addline(arg->buffer, "%s(float(%s) %s float(%s)) ? 1.0 : 0.0)%s;\n",
820 tmpLine, src0_str, compareStr, src1_str, dst_mask);
824 /** Process CMP instruction in GLSL (dst = src0.x > 0.0 ? src1.x : src2.x), per channel */
825 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
827 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
828 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
829 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
831 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
832 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
833 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
834 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
836 /* FIXME: This isn't correct - doesn't take the dst's swizzle into account. */
837 shader_addline(arg->buffer, "%s.x = (%s.x > 0.0) ? %s.x : %s.x;\n", dst_reg, src0_reg, src1_reg, src2_reg);
838 shader_addline(arg->buffer, "%s.y = (%s.y > 0.0) ? %s.y : %s.y;\n", dst_reg, src0_reg, src1_reg, src2_reg);
839 shader_addline(arg->buffer, "%s.z = (%s.z > 0.0) ? %s.z : %s.z;\n", dst_reg, src0_reg, src1_reg, src2_reg);
840 shader_addline(arg->buffer, "%s.w = (%s.w > 0.0) ? %s.w : %s.w;\n", dst_reg, src0_reg, src1_reg, src2_reg);
843 /** Process the CND opcode in GLSL (dst = (src0 < 0.5) ? src1 : src2) */
844 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
847 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
848 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
849 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
851 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
852 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
853 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
854 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
855 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
856 shader_addline(arg->buffer, "%s(%s < 0.5) ? %s : %s)%s;\n",
857 tmpLine, src0_str, src1_str, src2_str, dst_mask);
860 /** GLSL code generation for D3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
861 void shader_glsl_mad(SHADER_OPCODE_ARG* arg) {
864 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
865 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
866 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
868 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
869 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
870 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
871 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
872 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
874 shader_addline(arg->buffer, "%s(vec4(%s) * vec4(%s)) + vec4(%s))%s;\n",
875 tmpLine, src0_str, src1_str, src2_str, dst_mask);
878 /** Handles transforming all D3DSIO_M?x? opcodes for
879 Vertex shaders to GLSL codes */
880 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
883 SHADER_OPCODE_ARG tmpArg;
885 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
887 /* Set constants for the temporary argument */
888 tmpArg.shader = arg->shader;
889 tmpArg.buffer = arg->buffer;
890 tmpArg.src[0] = arg->src[0];
891 tmpArg.src_addr[0] = arg->src_addr[0];
892 tmpArg.reg_maps = arg->reg_maps;
894 switch(arg->opcode->opcode) {
897 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP4];
901 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP4];
905 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP3];
909 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP3];
913 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[D3DSIO_DP3];
919 for (i = 0; i < nComponents; i++) {
920 tmpArg.dst = ((arg->dst) & ~D3DSP_WRITEMASK_ALL)|(D3DSP_WRITEMASK_0<<i);
921 tmpArg.src[1] = arg->src[1]+i;
922 tmpArg.src_addr[1] = arg->src[1]+i;
923 shader_glsl_dot(&tmpArg);
928 The LRP instruction performs a component-wise linear interpolation
929 between the second and third operands using the first operand as the
930 blend factor. Equation: (dst = src2 * (src1 - src0) + src0)
932 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
935 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
936 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
937 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
939 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
940 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
941 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
942 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
944 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
946 shader_addline(arg->buffer, "%s(%s * (%s - %s) + %s))%s;\n",
947 tmpLine, src2_str, src1_str, src0_str, src0_str, dst_mask);
950 /** Process the D3DSIO_DEF opcode into a GLSL string - creates a local vec4
951 * float constant, and stores it's usage on the regmaps. */
952 void shader_glsl_def(SHADER_OPCODE_ARG* arg) {
954 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
956 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
957 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
958 const char* prefix = pshader? "PC":"VC";
960 shader_addline(arg->buffer,
961 "const vec4 %s%lu = { %f, %f, %f, %f };\n", prefix, reg,
962 *((const float *)(arg->src + 0)),
963 *((const float *)(arg->src + 1)),
964 *((const float *)(arg->src + 2)),
965 *((const float *)(arg->src + 3)) );
967 arg->reg_maps->constantsF[reg] = 1;
970 /** Process the D3DSIO_DEFI opcode into a GLSL string - creates a local ivec4
971 * integer constant, and stores it's usage on the regmaps. */
972 void shader_glsl_defi(SHADER_OPCODE_ARG* arg) {
974 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
976 shader_addline(arg->buffer,
977 "const ivec4 I%lu = { %ld, %ld, %ld, %ld };\n", reg,
978 (long)arg->src[0], (long)arg->src[1],
979 (long)arg->src[2], (long)arg->src[3]);
981 arg->reg_maps->constantsI[reg] = 1;
984 /** Process the D3DSIO_DEFB opcode into a GLSL string - creates a local boolean
985 * constant, and stores it's usage on the regmaps. */
986 void shader_glsl_defb(SHADER_OPCODE_ARG* arg) {
988 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
990 shader_addline(arg->buffer, "const bool B%lu = %s;\n", reg, (arg->src[0]) ? "true" : "false");
992 arg->reg_maps->constantsB[reg] = 1;
995 /** Process the D3DSIO_LIT instruction in GLSL:
996 * dst.x = dst.w = 1.0
997 * dst.y = (src0.x > 0) ? src0.x
998 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
999 * where src.w is clamped at +- 128
1001 void shader_glsl_lit(SHADER_OPCODE_ARG* arg) {
1003 char dst_str[100], src0_str[100];
1004 char dst_reg[50], src0_reg[50];
1005 char dst_mask[6], src0_mask[6];
1007 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1008 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1010 shader_addline(arg->buffer,
1011 "%s = vec4(1.0, (%s.x > 0.0 ? %s.x : 0.0), (%s.x > 0.0 ? ((%s.y > 0.0) ? pow(%s.y, clamp(%s.w, -128.0, 128.0)) : 0.0) : 0.0), 1.0)%s;\n",
1012 dst_str, src0_reg, src0_reg, src0_reg, src0_reg, src0_reg, src0_reg, dst_mask);
1015 /** Process the D3DSIO_DST instruction in GLSL:
1017 * dst.y = src0.x * src0.y
1021 void shader_glsl_dst(SHADER_OPCODE_ARG* arg) {
1023 char dst_str[100], src0_str[100], src1_str[100];
1024 char dst_reg[50], src0_reg[50], src1_reg[50];
1025 char dst_mask[6], src0_mask[6], src1_mask[6];
1027 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1028 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1029 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1031 shader_addline(arg->buffer, "%s = vec4(1.0, %s.x * %s.y, %s.z, %s.w)%s;\n",
1032 dst_str, src0_reg, src1_reg, src0_reg, src1_reg, dst_mask);
1035 /** Process the D3DSIO_SINCOS instruction in GLSL:
1036 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1037 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1039 * dst.x = cos(src0.?)
1040 * dst.y = sin(src0.?)
1044 void shader_glsl_sincos(SHADER_OPCODE_ARG* arg) {
1046 char dst_str[100], src0_str[100];
1047 char dst_reg[50], src0_reg[50];
1048 char dst_mask[6], src0_mask[6];
1050 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1051 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1053 shader_addline(arg->buffer, "%s = vec4(cos(%s), sin(%s), %s.z, %s.w)%s;\n",
1054 dst_str, src0_str, src0_str, dst_reg, dst_reg, dst_mask);
1057 /** Process the D3DSIO_LOOP instruction in GLSL:
1058 * Start a for() loop where src0.y is the initial value of aL,
1059 * increment aL by src0.z while (aL < src0.x).
1061 void shader_glsl_loop(SHADER_OPCODE_ARG* arg) {
1067 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1069 shader_addline(arg->buffer, "for (aL = %s.y; aL < %s.x; aL += %s.z) {\n",
1070 src0_reg, src0_reg, src0_reg);
1073 /** Process the D3DSIO_ENDLOOP instruction in GLSL:
1074 * End the for() loop
1076 void shader_glsl_endloop(SHADER_OPCODE_ARG* arg) {
1078 shader_addline(arg->buffer, "}\n");
1082 /*********************************************
1083 * Pixel Shader Specific Code begins here
1084 ********************************************/
1085 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
1087 /* FIXME: Make this work for more than just 2D textures */
1089 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1090 SHADER_BUFFER* buffer = arg->buffer;
1091 DWORD hex_version = This->baseShader.hex_version;
1093 char dst_str[100], dst_reg[50], dst_mask[6];
1094 char coord_str[100], coord_reg[50], coord_mask[6];
1095 char sampler_str[100], sampler_reg[50], sampler_mask[6];
1096 DWORD reg_dest_code = arg->dst & D3DSP_REGNUM_MASK;
1097 DWORD sampler_code, sampler_type;
1099 /* All versions have a destination register */
1100 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1102 /* 1.0-1.3: Use destination register as coordinate source.
1103 1.4+: Use provided coordinate source register. */
1104 if (hex_version < D3DPS_VERSION(1,4))
1105 strcpy(coord_reg, dst_reg);
1107 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, coord_reg, coord_mask, coord_str);
1109 /* 1.0-1.4: Use destination register as coordinate source.
1110 * 2.0+: Use provided coordinate source register. */
1111 if (hex_version < D3DPS_VERSION(2,0)) {
1112 sprintf(sampler_str, "Psampler%lu", reg_dest_code);
1113 sampler_code = reg_dest_code;
1116 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, sampler_reg, sampler_mask, sampler_str);
1117 sampler_code = arg->src[1] & D3DSP_REGNUM_MASK;
1120 sampler_type = arg->reg_maps->samplers[sampler_code] & D3DSP_TEXTURETYPE_MASK;
1121 switch(sampler_type) {
1124 shader_addline(buffer, "%s = texture2D(%s, %s.st);\n", dst_str, sampler_str, coord_reg);
1127 shader_addline(buffer, "%s = textureCube(%s, %s.stp);\n", dst_str, sampler_str, coord_reg);
1130 shader_addline(buffer, "%s = texture3D(%s, %s.stp);\n", dst_str, sampler_str, coord_reg);
1133 shader_addline(buffer, "%s = unrecognized_stype(%s, %s.stp);\n", dst_str, sampler_str, coord_reg);
1134 FIXME("Unrecognized sampler type: %#lx;\n", sampler_type);
1139 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
1141 /* FIXME: Make this work for more than just 2D textures */
1143 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1144 SHADER_BUFFER* buffer = arg->buffer;
1145 DWORD hex_version = This->baseShader.hex_version;
1152 shader_glsl_add_param(arg, arg->dst, 0, FALSE, tmpReg, tmpMask, tmpStr);
1154 if (hex_version != D3DPS_VERSION(1,4)) {
1155 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
1156 shader_addline(buffer, "%s = gl_TexCoord[%lu];\n", tmpReg, reg);
1158 DWORD reg2 = arg->src[0] & D3DSP_REGNUM_MASK;
1159 shader_addline(buffer, "%s = gl_TexCoord[%lu]%s;\n", tmpStr, reg2, tmpMask);
1163 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
1165 /* FIXME: Make this work for more than just 2D textures */
1167 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
1168 SHADER_BUFFER* buffer = arg->buffer;
1173 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1174 shader_addline(buffer, "tmp0.x = dot(vec3(T%lu), vec3(%s));\n", reg, src0_name, src0_mask, src0_str);
1177 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
1179 /* FIXME: Make this work for more than just 2D textures */
1181 DWORD reg = arg->dst & D3DSP_REGNUM_MASK;
1182 SHADER_BUFFER* buffer = arg->buffer;
1187 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1188 shader_addline(buffer, "tmp0.y = dot(vec3(T%lu), vec3(%s));\n", reg, src0_str);
1189 shader_addline(buffer, "T%lu = texture2D(Psampler%lu, tmp0.st);\n", reg, reg);
1192 void pshader_glsl_input_pack(
1193 SHADER_BUFFER* buffer,
1194 DWORD* semantics_in) {
1198 for (i = 0; i < WINED3DSHADERDECLUSAGE_MAX_USAGE; i++) {
1200 DWORD reg = semantics_in[i];
1201 unsigned int regnum = reg & D3DSP_REGNUM_MASK;
1207 shader_glsl_get_output_register_swizzle(reg, reg_mask);
1211 case WINED3DSHADERDECLUSAGE_DIFFUSE:
1212 shader_addline(buffer, "IN%lu%s = vec4(gl_Color)%s;\n",
1213 regnum, reg_mask, reg_mask);
1216 case WINED3DSHADERDECLUSAGE_SPECULAR:
1217 shader_addline(buffer, "IN%lu%s = vec4(gl_SecondaryColor)%s;\n",
1218 regnum, reg_mask, reg_mask);
1221 case WINED3DSHADERDECLUSAGE_TEXCOORD0:
1222 case WINED3DSHADERDECLUSAGE_TEXCOORD1:
1223 case WINED3DSHADERDECLUSAGE_TEXCOORD2:
1224 case WINED3DSHADERDECLUSAGE_TEXCOORD3:
1225 case WINED3DSHADERDECLUSAGE_TEXCOORD4:
1226 case WINED3DSHADERDECLUSAGE_TEXCOORD5:
1227 case WINED3DSHADERDECLUSAGE_TEXCOORD6:
1228 case WINED3DSHADERDECLUSAGE_TEXCOORD7:
1229 shader_addline(buffer, "IN%lu%s = vec4(gl_TexCoord[%lu])%s;\n",
1230 regnum, reg_mask, i - WINED3DSHADERDECLUSAGE_TEXCOORD0, reg_mask );
1233 case WINED3DSHADERDECLUSAGE_FOG:
1234 shader_addline(buffer, "IN%lu%s = vec4(gl_FogFragCoord)%s;\n",
1235 regnum, reg_mask, reg_mask);
1239 shader_addline(buffer, "IN%lu%s = vec4(unsupported_input)%s;\n",
1240 regnum, reg_mask, reg_mask);
1245 /*********************************************
1246 * Vertex Shader Specific Code begins here
1247 ********************************************/
1249 void vshader_glsl_output_unpack(
1250 SHADER_BUFFER* buffer,
1251 DWORD* semantics_out) {
1255 for (i = 0; i < WINED3DSHADERDECLUSAGE_MAX_USAGE; i++) {
1257 DWORD reg = semantics_out[i];
1258 unsigned int regnum = reg & D3DSP_REGNUM_MASK;
1264 shader_glsl_get_output_register_swizzle(reg, reg_mask);
1268 case WINED3DSHADERDECLUSAGE_DIFFUSE:
1269 shader_addline(buffer, "gl_FrontColor%s = OUT%lu%s;\n", reg_mask, regnum, reg_mask);
1272 case WINED3DSHADERDECLUSAGE_SPECULAR:
1273 shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT%lu%s;\n", reg_mask, regnum, reg_mask);
1276 case WINED3DSHADERDECLUSAGE_POSITION:
1277 shader_addline(buffer, "gl_Position%s = OUT%lu%s;\n", reg_mask, regnum, reg_mask);
1280 case WINED3DSHADERDECLUSAGE_TEXCOORD0:
1281 case WINED3DSHADERDECLUSAGE_TEXCOORD1:
1282 case WINED3DSHADERDECLUSAGE_TEXCOORD2:
1283 case WINED3DSHADERDECLUSAGE_TEXCOORD3:
1284 case WINED3DSHADERDECLUSAGE_TEXCOORD4:
1285 case WINED3DSHADERDECLUSAGE_TEXCOORD5:
1286 case WINED3DSHADERDECLUSAGE_TEXCOORD6:
1287 case WINED3DSHADERDECLUSAGE_TEXCOORD7:
1288 shader_addline(buffer, "gl_TexCoord[%lu]%s = OUT%lu%s;\n",
1289 i - WINED3DSHADERDECLUSAGE_TEXCOORD0, reg_mask, regnum, reg_mask);
1292 case WINED3DSHADERDECLUSAGE_PSIZE:
1293 shader_addline(buffer, "gl_PointSize = OUT%lu.x;\n", regnum);
1296 case WINED3DSHADERDECLUSAGE_FOG:
1297 shader_addline(buffer, "gl_FogFragCoord%s = OUT%lu%s;\n", reg_mask, regnum, reg_mask);
1301 shader_addline(buffer, "unsupported_output%s = OUT%lu%s;\n", reg_mask, regnum, reg_mask);