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 integer constants (aka uniforms) into the currently set GLSL program.
115 * When @constants_set == NULL, it will load all the constants.
117 void shader_glsl_load_constantsI(
118 WineD3D_GL_Info
*gl_info
,
119 GLhandleARB programId
,
120 unsigned max_constants
,
128 const char* prefix
= is_pshader
? "PI":"VI";
130 for (i
=0; i
<max_constants
; ++i
) {
131 if (NULL
== constants_set
|| constants_set
[i
]) {
133 TRACE("Loading constants %i: %i, %i, %i, %i\n",
134 i
, constants
[i
*4], constants
[i
*4+1], constants
[i
*4+2], constants
[i
*4+3]);
136 /* TODO: Benchmark and see if it would be beneficial to store the
137 * locations of the constants to avoid looking up each time */
138 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, i
);
139 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
141 /* We found this uniform name in the program - go ahead and send the data */
142 GL_EXTCALL(glUniform4ivARB(tmp_loc
, 1, &constants
[i
*4]));
143 checkGLcall("glUniform4ivARB");
150 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
151 * When @constants_set == NULL, it will load all the constants.
153 void shader_glsl_load_constantsB(
154 WineD3D_GL_Info
*gl_info
,
155 GLhandleARB programId
,
156 unsigned max_constants
,
164 const char* prefix
= is_pshader
? "PB":"VB";
166 for (i
=0; i
<max_constants
; ++i
) {
167 if (NULL
== constants_set
|| constants_set
[i
]) {
169 TRACE("Loading constants %i: %i;\n", i
, constants
[i
*4]);
171 /* TODO: Benchmark and see if it would be beneficial to store the
172 * locations of the constants to avoid looking up each time */
173 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, i
);
174 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
176 /* We found this uniform name in the program - go ahead and send the data */
177 GL_EXTCALL(glUniform1ivARB(tmp_loc
, 1, &constants
[i
*4]));
178 checkGLcall("glUniform1ivARB");
187 * Loads the app-supplied constants into the currently set GLSL program.
189 void shader_glsl_load_constants(
190 IWineD3DStateBlock
* iface
,
192 char useVertexShader
) {
194 IWineD3DStateBlockImpl
* stateBlock
= (IWineD3DStateBlockImpl
*) iface
;
195 WineD3D_GL_Info
*gl_info
= &((IWineD3DImpl
*)stateBlock
->wineD3DDevice
->wineD3D
)->gl_info
;
196 GLhandleARB programId
= stateBlock
->shaderPrgId
;
198 if (programId
== 0) {
199 /* No GLSL program set - nothing to do. */
203 if (useVertexShader
) {
204 IWineD3DVertexShaderImpl
* vshader
= (IWineD3DVertexShaderImpl
*) stateBlock
->vertexShader
;
205 IWineD3DVertexDeclarationImpl
* vertexDeclaration
=
206 (IWineD3DVertexDeclarationImpl
*) vshader
->vertexDeclaration
;
208 if (NULL
!= vertexDeclaration
&& NULL
!= vertexDeclaration
->constants
) {
209 /* Load DirectX 8 float constants/uniforms for vertex shader */
210 shader_glsl_load_constantsF(gl_info
, programId
, WINED3D_VSHADER_MAX_CONSTANTS
,
211 vertexDeclaration
->constants
, NULL
, 0);
214 /* Load DirectX 9 float constants/uniforms for vertex shader */
215 shader_glsl_load_constantsF(gl_info
, programId
, WINED3D_VSHADER_MAX_CONSTANTS
,
216 stateBlock
->vertexShaderConstantF
,
217 stateBlock
->set
.vertexShaderConstantsF
, 0);
219 /* Load DirectX 9 integer constants/uniforms for vertex shader */
220 shader_glsl_load_constantsI(gl_info
, programId
, MAX_CONST_I
,
221 stateBlock
->vertexShaderConstantI
,
222 stateBlock
->set
.vertexShaderConstantsI
, 0);
224 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
225 shader_glsl_load_constantsB(gl_info
, programId
, MAX_CONST_B
,
226 stateBlock
->vertexShaderConstantB
,
227 stateBlock
->set
.vertexShaderConstantsB
, 0);
230 if (usePixelShader
) {
232 /* Load pixel shader samplers */
233 shader_glsl_load_psamplers(gl_info
, iface
);
235 /* Load DirectX 9 float constants/uniforms for pixel shader */
236 shader_glsl_load_constantsF(gl_info
, programId
, WINED3D_PSHADER_MAX_CONSTANTS
,
237 stateBlock
->pixelShaderConstantF
,
238 stateBlock
->set
.pixelShaderConstantsF
, 1);
240 /* Load DirectX 9 integer constants/uniforms for pixel shader */
241 shader_glsl_load_constantsI(gl_info
, programId
, MAX_CONST_I
,
242 stateBlock
->pixelShaderConstantI
,
243 stateBlock
->set
.pixelShaderConstantsI
, 1);
245 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
246 shader_glsl_load_constantsB(gl_info
, programId
, MAX_CONST_B
,
247 stateBlock
->pixelShaderConstantB
,
248 stateBlock
->set
.pixelShaderConstantsB
, 1);
252 /** Generate the variable & register declarations for the GLSL output target */
253 void shader_generate_glsl_declarations(
254 IWineD3DBaseShader
*iface
,
255 shader_reg_maps
* reg_maps
,
256 SHADER_BUFFER
* buffer
) {
258 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
261 /* There are some minor differences between pixel and vertex shaders */
262 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
263 char prefix
= pshader
? 'P' : 'V';
265 /* Declare the constants (aka uniforms) */
266 if (This
->baseShader
.limits
.constant_float
> 0)
267 shader_addline(buffer
, "uniform vec4 %cC[%u];\n", prefix
, This
->baseShader
.limits
.constant_float
);
269 if (This
->baseShader
.limits
.constant_int
> 0)
270 shader_addline(buffer
, "uniform ivec4 %cI[%u];\n", prefix
, This
->baseShader
.limits
.constant_int
);
272 if (This
->baseShader
.limits
.constant_bool
> 0)
273 shader_addline(buffer
, "uniform bool %cB[%u];\n", prefix
, This
->baseShader
.limits
.constant_bool
);
275 /* Declare texture samplers */
276 for (i
= 0; i
< This
->baseShader
.limits
.sampler
; i
++) {
277 if (reg_maps
->samplers
[i
]) {
279 DWORD stype
= reg_maps
->samplers
[i
] & D3DSP_TEXTURETYPE_MASK
;
283 shader_addline(buffer
, "uniform sampler2D %csampler%lu;\n", prefix
, i
);
286 shader_addline(buffer
, "uniform samplerCube %csampler%lu;\n", prefix
, i
);
289 shader_addline(buffer
, "uniform sampler3D %csampler%lu;\n", prefix
, i
);
292 shader_addline(buffer
, "uniform unsupported_sampler %csampler%lu;\n", prefix
, i
);
293 FIXME("Unrecognized sampler type: %#lx\n", stype
);
299 /* Declare address variables */
300 for (i
= 0; i
< This
->baseShader
.limits
.address
; i
++) {
301 if (reg_maps
->address
[i
])
302 shader_addline(buffer
, "ivec4 A%ld;\n", i
);
305 /* Declare texture coordinate temporaries and initialize them */
306 for (i
= 0; i
< This
->baseShader
.limits
.texcoord
; i
++) {
307 if (reg_maps
->texcoord
[i
])
308 shader_addline(buffer
, "vec4 T%lu = gl_TexCoord[%lu];\n", i
, i
);
311 /* Declare input register temporaries */
312 for (i
=0; i
< This
->baseShader
.limits
.packed_input
; i
++) {
313 if (reg_maps
->packed_input
[i
])
314 shader_addline(buffer
, "vec4 IN%lu;\n", i
);
317 /* Declare output register temporaries */
318 for (i
= 0; i
< This
->baseShader
.limits
.packed_output
; i
++) {
319 if (reg_maps
->packed_output
[i
])
320 shader_addline(buffer
, "vec4 OUT%lu;\n", i
);
323 /* Declare temporary variables */
324 for(i
= 0; i
< This
->baseShader
.limits
.temporary
; i
++) {
325 if (reg_maps
->temporary
[i
])
326 shader_addline(buffer
, "vec4 R%lu;\n", i
);
329 /* Declare attributes */
330 for (i
= 0; i
< This
->baseShader
.limits
.attributes
; i
++) {
331 if (reg_maps
->attributes
[i
])
332 shader_addline(buffer
, "attribute vec4 attrib%i;\n", i
);
335 /* Declare loop register aL */
336 if (reg_maps
->loop
) {
337 shader_addline(buffer
, "int aL;\n");
338 shader_addline(buffer
, "int tmpInt;\n");
341 /* Temporary variables for matrix operations */
342 shader_addline(buffer
, "vec4 tmp0;\n");
343 shader_addline(buffer
, "vec4 tmp1;\n");
345 /* Start the main program */
346 shader_addline(buffer
, "void main() {\n");
349 /*****************************************************************************
350 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
352 * For more information, see http://wiki.winehq.org/DirectX-Shaders
353 ****************************************************************************/
356 static void shader_glsl_add_param(
357 SHADER_OPCODE_ARG
* arg
,
359 const DWORD addr_token
,
365 /** Used for opcode modifiers - They multiply the result by the specified amount */
366 static const char* shift_glsl_tab
[] = {
368 "2.0 * ", /* 1 (x2) */
369 "4.0 * ", /* 2 (x4) */
370 "8.0 * ", /* 3 (x8) */
371 "16.0 * ", /* 4 (x16) */
372 "32.0 * ", /* 5 (x32) */
379 "0.0625 * ", /* 12 (d16) */
380 "0.125 * ", /* 13 (d8) */
381 "0.25 * ", /* 14 (d4) */
382 "0.5 * " /* 15 (d2) */
385 /** Print the beginning of the generated GLSL string. example: "reg_name.xyzw = vec4("
386 * Will also change the reg_mask if necessary (not all register types are equal in DX vs GL) */
387 static void shader_glsl_add_dst(DWORD param
, const char* reg_name
, char* reg_mask
, char* outStr
) {
389 int shift
= (param
& D3DSP_DSTSHIFT_MASK
) >> D3DSP_DSTSHIFT_SHIFT
;
392 if ((shader_get_regtype(param
) == D3DSPR_RASTOUT
)
393 && ((param
& D3DSP_REGNUM_MASK
) != 0)) {
394 /* gl_FogFragCoord or glPointSize - both floats */
395 strcpy(cast
, "float");
396 strcpy(reg_mask
, "");
398 } else if (reg_name
[0] == 'A') {
399 /* Address register for vertex shaders (ivec4) */
400 strcpy(cast
, "ivec4");
403 /* Everything else should be a 4 component float vector */
404 strcpy(cast
, "vec4");
407 sprintf(outStr
, "%s%s = %s%s(", reg_name
, reg_mask
, shift_glsl_tab
[shift
], cast
);
410 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
411 static void shader_glsl_gen_modifier (
414 const char *in_regswizzle
,
419 switch (instr
& D3DSP_SRCMOD_MASK
) {
421 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
424 sprintf(out_str
, "-%s%s", in_reg
, in_regswizzle
);
427 sprintf(out_str
, "(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
429 case D3DSPSM_BIASNEG
:
430 sprintf(out_str
, "-(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
433 sprintf(out_str
, "(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
435 case D3DSPSM_SIGNNEG
:
436 sprintf(out_str
, "-(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
439 sprintf(out_str
, "(1.0 - %s%s)", in_reg
, in_regswizzle
);
442 sprintf(out_str
, "(2.0 * %s%s)", in_reg
, in_regswizzle
);
445 sprintf(out_str
, "-(2.0 * %s%s)", in_reg
, in_regswizzle
);
447 case D3DSPSM_DZ
: /* reg1_db = { reg1.r/b, reg1.g/b, ...} The g & a components are undefined, so we'll leave them alone */
448 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
);
451 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
);
454 sprintf(out_str
, "abs(%s%s)", in_reg
, in_regswizzle
);
457 sprintf(out_str
, "-abs(%s%s)", in_reg
, in_regswizzle
);
460 FIXME("Unhandled modifier %lu\n", (instr
& D3DSP_SRCMOD_MASK
));
461 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
465 /** Writes the GLSL variable name that corresponds to the register that the
466 * DX opcode parameter is trying to access */
467 static void shader_glsl_get_register_name(
469 const DWORD addr_token
,
472 SHADER_OPCODE_ARG
* arg
) {
474 /* oPos, oFog and oPts in D3D */
475 const char* hwrastout_reg_names
[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
477 DWORD reg
= param
& D3DSP_REGNUM_MASK
;
478 DWORD regtype
= shader_get_regtype(param
);
479 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
480 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
487 sprintf(tmpStr
, "R%lu", reg
);
491 /* Pixel shaders >= 3.0 */
492 if (D3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 3)
493 sprintf(tmpStr
, "IN%lu", reg
);
496 strcpy(tmpStr
, "gl_Color");
498 strcpy(tmpStr
, "gl_SecondaryColor");
501 IWineD3DVertexShaderImpl
*vshader
= (IWineD3DVertexShaderImpl
*) arg
->shader
;
503 if (vshader
->arrayUsageMap
[WINED3DSHADERDECLUSAGE_DIFFUSE
] &&
504 reg
== (vshader
->arrayUsageMap
[WINED3DSHADERDECLUSAGE_DIFFUSE
] & D3DSP_REGNUM_MASK
))
507 if (vshader
->arrayUsageMap
[WINED3DSHADERDECLUSAGE_SPECULAR
] &&
508 reg
== (vshader
->arrayUsageMap
[WINED3DSHADERDECLUSAGE_SPECULAR
] & D3DSP_REGNUM_MASK
))
511 /* FIXME: Shaders in 8.1 appear to not require a dcl statement - use
512 * the reg value from the vertex declaration. However, arrayUsageMap is not initialized
513 * in that case - how can we know if an input contains color data or not? */
515 sprintf(tmpStr
, "attrib%lu", reg
);
520 const char* prefix
= pshader
? "PC":"VC";
522 if (arg
->reg_maps
->constantsF
[reg
]) {
523 /* Use a local constant declared by "def" */
525 if (param
& D3DVS_ADDRMODE_RELATIVE
) {
526 /* FIXME: Copy all constants (local & global) into a single array
527 * to handle this case where we want a relative address from a
529 FIXME("Relative addressing not yet supported on named constants\n");
531 sprintf(tmpStr
, "%s%lu", prefix
, reg
);
534 /* Use a global constant declared in Set____ShaderConstantF() */
535 if (param
& D3DVS_ADDRMODE_RELATIVE
) {
536 /* Relative addressing on shaders 2.0+ have a relative address token,
537 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
538 if (D3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 2) {
539 char relStr
[100], relReg
[50], relMask
[6];
540 shader_glsl_add_param(arg
, addr_token
, 0, TRUE
, relReg
, relMask
, relStr
);
541 sprintf(tmpStr
, "%s[%s + %lu]", prefix
, relStr
, reg
);
543 sprintf(tmpStr
, "%s[A0.x + %lu]", prefix
, reg
);
546 /* Just a normal global constant - no relative addressing */
547 sprintf(tmpStr
, "%s[%lu]", prefix
, reg
);
552 case D3DSPR_CONSTINT
:
553 if (arg
->reg_maps
->constantsI
[reg
]) {
554 /* Integer vector was defined locally using "defi" */
555 sprintf(tmpStr
, "I%lu", reg
);
557 /* Uniform integer value - pixel or vertex specific */
559 sprintf(tmpStr
, "PI[%lu]", reg
);
561 sprintf(tmpStr
, "VI[%lu]", reg
);
565 case D3DSPR_CONSTBOOL
:
566 if (arg
->reg_maps
->constantsB
[reg
]) {
567 /* Boolean was defined locally using "defb" */
568 sprintf(tmpStr
, "B%lu", reg
);
570 /* Uniform boolean value - pixel or vertex specific */
572 sprintf(tmpStr
, "PB[%lu]", reg
);
574 sprintf(tmpStr
, "VB[%lu]", reg
);
578 case D3DSPR_TEXTURE
: /* case D3DSPR_ADDR: */
580 sprintf(tmpStr
, "T%lu", reg
);
582 sprintf(tmpStr
, "A%lu", reg
);
586 sprintf(tmpStr
, "aL");
590 sprintf(tmpStr
, "Psampler%lu", reg
);
592 sprintf(tmpStr
, "Vsampler%lu", reg
);
594 case D3DSPR_COLOROUT
:
596 sprintf(tmpStr
, "gl_FragColor");
598 /* TODO: See GL_ARB_draw_buffers */
599 FIXME("Unsupported write to render target %lu\n", reg
);
600 sprintf(tmpStr
, "unsupported_register");
604 sprintf(tmpStr
, "%s", hwrastout_reg_names
[reg
]);
606 case D3DSPR_DEPTHOUT
:
607 sprintf(tmpStr
, "gl_FragDepth");
611 sprintf(tmpStr
, "gl_FrontColor");
613 sprintf(tmpStr
, "gl_FrontSecondaryColor");
616 case D3DSPR_TEXCRDOUT
:
617 /* Vertex shaders >= 3.0: D3DSPR_OUTPUT */
618 if (D3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 3)
619 sprintf(tmpStr
, "OUT%lu", reg
);
621 sprintf(tmpStr
, "gl_TexCoord[%lu]", reg
);
624 FIXME("Unhandled register name Type(%ld)\n", regtype
);
625 sprintf(tmpStr
, "unrecognized_register");
629 strcat(regstr
, tmpStr
);
632 /* Writes the GLSL writemask for the destination register */
633 static void shader_glsl_get_output_register_swizzle(
638 if ((param
& D3DSP_WRITEMASK_ALL
) != D3DSP_WRITEMASK_ALL
) {
639 strcat(write_mask
, ".");
640 if (param
& D3DSP_WRITEMASK_0
) strcat(write_mask
, "x");
641 if (param
& D3DSP_WRITEMASK_1
) strcat(write_mask
, "y");
642 if (param
& D3DSP_WRITEMASK_2
) strcat(write_mask
, "z");
643 if (param
& D3DSP_WRITEMASK_3
) strcat(write_mask
, "w");
647 static void shader_glsl_get_input_register_swizzle(
652 const char swizzle_reg_chars_color_fix
[] = "zyxw";
653 const char swizzle_reg_chars
[] = "xyzw";
654 const char* swizzle_regs
= NULL
;
657 DWORD swizzle
= (param
& D3DVS_SWIZZLE_MASK
) >> D3DVS_SWIZZLE_SHIFT
;
658 DWORD swizzle_x
= swizzle
& 0x03;
659 DWORD swizzle_y
= (swizzle
>> 2) & 0x03;
660 DWORD swizzle_z
= (swizzle
>> 4) & 0x03;
661 DWORD swizzle_w
= (swizzle
>> 6) & 0x03;
664 swizzle_regs
= swizzle_reg_chars_color_fix
;
666 swizzle_regs
= swizzle_reg_chars
;
670 * swizzle bits fields:
673 if ((D3DVS_NOSWIZZLE
>> D3DVS_SWIZZLE_SHIFT
) == swizzle
) { /* D3DVS_NOSWIZZLE == 0xE4 << D3DVS_SWIZZLE_SHIFT */
675 sprintf(reg_mask
, ".%c%c%c%c",
676 swizzle_regs
[swizzle_x
],
677 swizzle_regs
[swizzle_y
],
678 swizzle_regs
[swizzle_z
],
679 swizzle_regs
[swizzle_w
]);
683 if (swizzle_x
== swizzle_y
&&
684 swizzle_x
== swizzle_z
&&
685 swizzle_x
== swizzle_w
)
687 sprintf(reg_mask
, ".%c", swizzle_regs
[swizzle_x
]);
689 sprintf(reg_mask
, ".%c%c%c%c",
690 swizzle_regs
[swizzle_x
],
691 swizzle_regs
[swizzle_y
],
692 swizzle_regs
[swizzle_z
],
693 swizzle_regs
[swizzle_w
]);
697 /** From a given parameter token, generate the corresponding GLSL string.
698 * Also, return the actual register name and swizzle in case the
699 * caller needs this information as well. */
700 static void shader_glsl_add_param(
701 SHADER_OPCODE_ARG
* arg
,
703 const DWORD addr_token
,
709 BOOL is_color
= FALSE
;
710 reg_mask
[0] = reg_name
[0] = out_str
[0] = 0;
712 shader_glsl_get_register_name(param
, addr_token
, reg_name
, &is_color
, arg
);
715 shader_glsl_get_input_register_swizzle(param
, is_color
, reg_mask
);
716 shader_glsl_gen_modifier(param
, reg_name
, reg_mask
, out_str
);
718 shader_glsl_get_output_register_swizzle(param
, reg_mask
);
719 sprintf(out_str
, "%s%s", reg_name
, reg_mask
);
723 /** Process GLSL instruction modifiers */
724 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG
* arg
) {
726 if (0 != (arg
->dst
& D3DSP_DSTMOD_MASK
)) {
727 DWORD mask
= arg
->dst
& D3DSP_DSTMOD_MASK
;
732 shader_glsl_add_param(arg
, arg
->dst
, 0, FALSE
, dst_reg
, dst_mask
, dst_str
);
734 if (mask
& D3DSPDM_SATURATE
) {
735 /* _SAT means to clamp the value of the register to between 0 and 1 */
736 shader_addline(arg
->buffer
, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_reg
, dst_mask
, dst_reg
, dst_mask
);
738 if (mask
& D3DSPDM_MSAMPCENTROID
) {
739 FIXME("_centroid modifier not handled\n");
741 if (mask
& D3DSPDM_PARTIALPRECISION
) {
742 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
747 /*****************************************************************************
749 * Begin processing individual instruction opcodes
751 ****************************************************************************/
753 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
754 void shader_glsl_arith(SHADER_OPCODE_ARG
* arg
) {
756 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
757 SHADER_BUFFER
* buffer
= arg
->buffer
;
759 char dst_reg
[50], src0_reg
[50], src1_reg
[50];
760 char dst_mask
[6], src0_mask
[6], src1_mask
[6];
761 char dst_str
[100], src0_str
[100], src1_str
[100];
763 shader_glsl_add_param(arg
, arg
->dst
, 0, FALSE
, dst_reg
, dst_mask
, dst_str
);
764 shader_glsl_add_param(arg
, arg
->src
[0], arg
->src_addr
[0], TRUE
, src0_reg
, src0_mask
, src0_str
);
765 shader_glsl_add_param(arg
, arg
->src
[1], arg
->src_addr
[1], TRUE
, src1_reg
, src1_mask
, src1_str
);
766 shader_glsl_add_dst(arg
->dst
, dst_reg
, dst_mask
, tmpLine
);
767 strcat(tmpLine
, "vec4(");
768 strcat(tmpLine
, src0_str
);
769 strcat(tmpLine
, ")");
771 /* Determine the GLSL operator to use based on the opcode */
772 switch (curOpcode
->opcode
) {
773 case D3DSIO_MUL
: strcat(tmpLine
, " * "); break;
774 case D3DSIO_ADD
: strcat(tmpLine
, " + "); break;
775 case D3DSIO_SUB
: strcat(tmpLine
, " - "); break;
777 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode
->name
);
780 shader_addline(buffer
, "%svec4(%s))%s;\n", tmpLine
, src1_str
, dst_mask
);
783 /* Process the D3DSIO_MOV opcode using GLSL (dst = src) */
784 void shader_glsl_mov(SHADER_OPCODE_ARG
* arg
) {
786 SHADER_BUFFER
* buffer
= arg
->buffer
;
788 char dst_str
[100], src0_str
[100];
789 char dst_reg
[50], src0_reg
[50];
790 char dst_mask
[6], src0_mask
[6];
792 shader_glsl_add_param(arg
, arg
->dst
, 0, FALSE
, dst_reg
, dst_mask
, dst_str
);
793 shader_glsl_add_param(arg
, arg
->src
[0], arg
->src_addr
[0], TRUE
, src0_reg
, src0_mask
, src0_str
);
794 shader_glsl_add_dst(arg
->dst
, dst_reg
, dst_mask
, tmpLine
);
795 shader_addline(buffer
, "%s%s)%s;\n", tmpLine
, src0_str
, dst_mask
);
798 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
799 void shader_glsl_dot(SHADER_OPCODE_ARG
* arg
) {
801 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
802 SHADER_BUFFER
* buffer
= arg
->buffer
;
804 char dst_str
[100], src0_str
[100], src1_str
[100];
805 char dst_reg
[50], src0_reg
[50], src1_reg
[50];
806 char dst_mask
[6], src0_mask
[6], src1_mask
[6];
809 shader_glsl_add_param(arg
, arg
->dst
, 0, FALSE
, dst_reg
, dst_mask
, dst_str
);
810 shader_glsl_add_param(arg
, arg
->src
[0], arg
->src_addr
[0], TRUE
, src0_reg
, src0_mask
, src0_str
);
811 shader_glsl_add_param(arg
, arg
->src
[1], arg
->src_addr
[1], TRUE
, src1_reg
, src1_mask
, src1_str
);
813 shader_glsl_add_dst(arg
->dst
, dst_reg
, dst_mask
, tmpDest
);
815 /* Need to cast the src vectors to vec3 for dp3, and vec4 for dp4 */
816 if (curOpcode
->opcode
== D3DSIO_DP4
)
817 strcpy(cast
, "vec4(");
819 strcpy(cast
, "vec3(");
821 shader_addline(buffer
, "%sdot(%s%s), %s%s)))%s;\n",
822 tmpDest
, cast
, src0_str
, cast
, src1_str
, dst_mask
);
825 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
826 void shader_glsl_map2gl(SHADER_OPCODE_ARG
* arg
) {
828 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
829 SHADER_BUFFER
* buffer
= arg
->buffer
;
831 char dst_str
[100], src_str
[100];
832 char dst_reg
[50], src_reg
[50];
833 char dst_mask
[6], src_mask
[6];
836 shader_glsl_add_param(arg
, arg
->dst
, 0, FALSE
, dst_reg
, dst_mask
, dst_str
);
838 shader_glsl_add_dst(arg
->dst
, dst_reg
, dst_mask
, tmpLine
);
840 /* Determine the GLSL function to use based on the opcode */
841 /* TODO: Possibly make this a table for faster lookups */
842 switch (curOpcode
->opcode
) {
843 case D3DSIO_MIN
: strcat(tmpLine
, "min"); break;
844 case D3DSIO_MAX
: strcat(tmpLine
, "max"); break;
845 case D3DSIO_RSQ
: strcat(tmpLine
, "inversesqrt"); break;
846 case D3DSIO_ABS
: strcat(tmpLine
, "abs"); break;
847 case D3DSIO_FRC
: strcat(tmpLine
, "fract"); break;
848 case D3DSIO_POW
: strcat(tmpLine
, "pow"); break;
849 case D3DSIO_CRS
: strcat(tmpLine
, "cross"); break;
850 case D3DSIO_NRM
: strcat(tmpLine
, "normalize"); break;
852 case D3DSIO_LOG
: strcat(tmpLine
, "log2"); break;
854 case D3DSIO_EXP
: strcat(tmpLine
, "exp2"); break;
855 case D3DSIO_SGE
: strcat(tmpLine
, "greaterThanEqual"); break;
856 case D3DSIO_SLT
: strcat(tmpLine
, "lessThan"); break;
857 case D3DSIO_SGN
: strcat(tmpLine
, "sign"); break;
859 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode
->name
);
863 strcat(tmpLine
, "(");
865 if (curOpcode
->num_params
> 0) {
866 strcat(tmpLine
, "vec4(");
867 shader_glsl_add_param(arg
, arg
->src
[0], arg
->src_addr
[0], TRUE
, src_reg
, src_mask
, src_str
);
868 strcat(tmpLine
, src_str
);
869 strcat(tmpLine
, ")");
870 for (i
= 2; i
< curOpcode
->num_params
; ++i
) {
871 strcat(tmpLine
, ", vec4(");
872 shader_glsl_add_param(arg
, arg
->src
[i
-1], arg
->src_addr
[i
-1], TRUE
, src_reg
, src_mask
, src_str
);
873 strcat(tmpLine
, src_str
);
874 strcat(tmpLine
, ")");
877 shader_addline(buffer
, "%s))%s;\n", tmpLine
, dst_mask
);
881 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
882 void shader_glsl_rcp(SHADER_OPCODE_ARG
* arg
) {
885 char dst_str
[100], src_str
[100];
886 char dst_reg
[50], src_reg
[50];
887 char dst_mask
[6], src_mask
[6];
889 shader_glsl_add_param(arg
, arg
->dst
, 0, FALSE
, dst_reg
, dst_mask
, dst_str
);
890 shader_glsl_add_param(arg
, arg
->src
[0], arg
->src_addr
[0], TRUE
, src_reg
, src_mask
, src_str
);
891 shader_glsl_add_dst(arg
->dst
, dst_reg
, dst_mask
, tmpLine
);
892 strcat(tmpLine
, "1.0 / ");
893 shader_addline(arg
->buffer
, "%s%s)%s;\n", tmpLine
, src_str
, dst_mask
);
896 /** Process signed comparison opcodes in GLSL. */
897 void shader_glsl_compare(SHADER_OPCODE_ARG
* arg
) {
900 char dst_str
[100], src0_str
[100], src1_str
[100];
901 char dst_reg
[50], src0_reg
[50], src1_reg
[50];
902 char dst_mask
[6], src0_mask
[6], src1_mask
[6];
904 shader_glsl_add_param(arg
, arg
->dst
, 0, FALSE
, dst_reg
, dst_mask
, dst_str
);
905 shader_glsl_add_param(arg
, arg
->src
[0], arg
->src_addr
[0], TRUE
, src0_reg
, src0_mask
, src0_str
);
906 shader_glsl_add_dst(arg
->dst
, dst_reg
, dst_mask
, tmpLine
);
908 /* If we are comparing vectors and not scalars, we should process this through map2gl using the GLSL functions. */
909 if (strlen(src0_mask
) != 2) {
910 shader_glsl_map2gl(arg
);
914 shader_glsl_add_param(arg
, arg
->src
[1], arg
->src_addr
[1], TRUE
, src1_reg
, src1_mask
, src1_str
);
916 switch (arg
->opcode
->opcode
) {
917 case D3DSIO_SLT
: strcpy(compareStr
, "<"); break;
918 case D3DSIO_SGE
: strcpy(compareStr
, ">="); break;
920 FIXME("Can't handle opcode %s\n", arg
->opcode
->name
);
922 shader_addline(arg
->buffer
, "%s(float(%s) %s float(%s)) ? 1.0 : 0.0)%s;\n",
923 tmpLine
, src0_str
, compareStr
, src1_str
, dst_mask
);
927 /** Process CMP instruction in GLSL (dst = src0.x > 0.0 ? src1.x : src2.x), per channel */
928 void shader_glsl_cmp(SHADER_OPCODE_ARG
* arg
) {
930 char dst_str
[100], src0_str
[100], src1_str
[100], src2_str
[100];
931 char dst_reg
[50], src0_reg
[50], src1_reg
[50], src2_reg
[50];
932 char dst_mask
[6], src0_mask
[6], src1_mask
[6], src2_mask
[6];
934 shader_glsl_add_param(arg
, arg
->dst
, 0, FALSE
, dst_reg
, dst_mask
, dst_str
);
935 shader_glsl_add_param(arg
, arg
->src
[0], arg
->src_addr
[0], TRUE
, src0_reg
, src0_mask
, src0_str
);
936 shader_glsl_add_param(arg
, arg
->src
[1], arg
->src_addr
[1], TRUE
, src1_reg
, src1_mask
, src1_str
);
937 shader_glsl_add_param(arg
, arg
->src
[2], arg
->src_addr
[2], TRUE
, src2_reg
, src2_mask
, src2_str
);
939 /* FIXME: This isn't correct - doesn't take the dst's swizzle into account. */
940 shader_addline(arg
->buffer
, "%s.x = (%s.x > 0.0) ? %s.x : %s.x;\n", dst_reg
, src0_reg
, src1_reg
, src2_reg
);
941 shader_addline(arg
->buffer
, "%s.y = (%s.y > 0.0) ? %s.y : %s.y;\n", dst_reg
, src0_reg
, src1_reg
, src2_reg
);
942 shader_addline(arg
->buffer
, "%s.z = (%s.z > 0.0) ? %s.z : %s.z;\n", dst_reg
, src0_reg
, src1_reg
, src2_reg
);
943 shader_addline(arg
->buffer
, "%s.w = (%s.w > 0.0) ? %s.w : %s.w;\n", dst_reg
, src0_reg
, src1_reg
, src2_reg
);
946 /** Process the CND opcode in GLSL (dst = (src0 < 0.5) ? src1 : src2) */
947 void shader_glsl_cnd(SHADER_OPCODE_ARG
* arg
) {
950 char dst_str
[100], src0_str
[100], src1_str
[100], src2_str
[100];
951 char dst_reg
[50], src0_reg
[50], src1_reg
[50], src2_reg
[50];
952 char dst_mask
[6], src0_mask
[6], src1_mask
[6], src2_mask
[6];
954 shader_glsl_add_param(arg
, arg
->dst
, 0, FALSE
, dst_reg
, dst_mask
, dst_str
);
955 shader_glsl_add_param(arg
, arg
->src
[0], arg
->src_addr
[0], TRUE
, src0_reg
, src0_mask
, src0_str
);
956 shader_glsl_add_param(arg
, arg
->src
[1], arg
->src_addr
[1], TRUE
, src1_reg
, src1_mask
, src1_str
);
957 shader_glsl_add_param(arg
, arg
->src
[2], arg
->src_addr
[2], TRUE
, src2_reg
, src2_mask
, src2_str
);
958 shader_glsl_add_dst(arg
->dst
, dst_reg
, dst_mask
, tmpLine
);
959 shader_addline(arg
->buffer
, "%s(%s < 0.5) ? %s : %s)%s;\n",
960 tmpLine
, src0_str
, src1_str
, src2_str
, dst_mask
);
963 /** GLSL code generation for D3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
964 void shader_glsl_mad(SHADER_OPCODE_ARG
* arg
) {
967 char dst_str
[100], src0_str
[100], src1_str
[100], src2_str
[100];
968 char dst_reg
[50], src0_reg
[50], src1_reg
[50], src2_reg
[50];
969 char dst_mask
[6], src0_mask
[6], src1_mask
[6], src2_mask
[6];
971 shader_glsl_add_param(arg
, arg
->dst
, 0, FALSE
, dst_reg
, dst_mask
, dst_str
);
972 shader_glsl_add_param(arg
, arg
->src
[0], arg
->src_addr
[0], TRUE
, src0_reg
, src0_mask
, src0_str
);
973 shader_glsl_add_param(arg
, arg
->src
[1], arg
->src_addr
[1], TRUE
, src1_reg
, src1_mask
, src1_str
);
974 shader_glsl_add_param(arg
, arg
->src
[2], arg
->src_addr
[2], TRUE
, src2_reg
, src2_mask
, src2_str
);
975 shader_glsl_add_dst(arg
->dst
, dst_reg
, dst_mask
, tmpLine
);
977 shader_addline(arg
->buffer
, "%s(vec4(%s) * vec4(%s)) + vec4(%s))%s;\n",
978 tmpLine
, src0_str
, src1_str
, src2_str
, dst_mask
);
981 /** Handles transforming all D3DSIO_M?x? opcodes for
982 Vertex shaders to GLSL codes */
983 void shader_glsl_mnxn(SHADER_OPCODE_ARG
* arg
) {
986 SHADER_OPCODE_ARG tmpArg
;
988 memset(&tmpArg
, 0, sizeof(SHADER_OPCODE_ARG
));
990 /* Set constants for the temporary argument */
991 tmpArg
.shader
= arg
->shader
;
992 tmpArg
.buffer
= arg
->buffer
;
993 tmpArg
.src
[0] = arg
->src
[0];
994 tmpArg
.src_addr
[0] = arg
->src_addr
[0];
995 tmpArg
.reg_maps
= arg
->reg_maps
;
997 switch(arg
->opcode
->opcode
) {
1000 tmpArg
.opcode
= &IWineD3DVertexShaderImpl_shader_ins
[D3DSIO_DP4
];
1004 tmpArg
.opcode
= &IWineD3DVertexShaderImpl_shader_ins
[D3DSIO_DP4
];
1008 tmpArg
.opcode
= &IWineD3DVertexShaderImpl_shader_ins
[D3DSIO_DP3
];
1012 tmpArg
.opcode
= &IWineD3DVertexShaderImpl_shader_ins
[D3DSIO_DP3
];
1016 tmpArg
.opcode
= &IWineD3DVertexShaderImpl_shader_ins
[D3DSIO_DP3
];
1022 for (i
= 0; i
< nComponents
; i
++) {
1023 tmpArg
.dst
= ((arg
->dst
) & ~D3DSP_WRITEMASK_ALL
)|(D3DSP_WRITEMASK_0
<<i
);
1024 tmpArg
.src
[1] = arg
->src
[1]+i
;
1025 tmpArg
.src_addr
[1] = arg
->src
[1]+i
;
1026 shader_glsl_dot(&tmpArg
);
1031 The LRP instruction performs a component-wise linear interpolation
1032 between the second and third operands using the first operand as the
1033 blend factor. Equation: (dst = src2 * (src1 - src0) + src0)
1035 void shader_glsl_lrp(SHADER_OPCODE_ARG
* arg
) {
1038 char dst_str
[100], src0_str
[100], src1_str
[100], src2_str
[100];
1039 char dst_reg
[50], src0_reg
[50], src1_reg
[50], src2_reg
[50];
1040 char dst_mask
[6], src0_mask
[6], src1_mask
[6], src2_mask
[6];
1042 shader_glsl_add_param(arg
, arg
->dst
, 0, FALSE
, dst_reg
, dst_mask
, dst_str
);
1043 shader_glsl_add_param(arg
, arg
->src
[0], arg
->src_addr
[0], TRUE
, src0_reg
, src0_mask
, src0_str
);
1044 shader_glsl_add_param(arg
, arg
->src
[1], arg
->src_addr
[1], TRUE
, src1_reg
, src1_mask
, src1_str
);
1045 shader_glsl_add_param(arg
, arg
->src
[2], arg
->src_addr
[2], TRUE
, src2_reg
, src2_mask
, src2_str
);
1047 shader_glsl_add_dst(arg
->dst
, dst_reg
, dst_mask
, tmpLine
);
1049 shader_addline(arg
->buffer
, "%s(%s * (%s - %s) + %s))%s;\n",
1050 tmpLine
, src2_str
, src1_str
, src0_str
, src0_str
, dst_mask
);
1053 /** Process the D3DSIO_DEF opcode into a GLSL string - creates a local vec4
1054 * float constant, and stores it's usage on the regmaps. */
1055 void shader_glsl_def(SHADER_OPCODE_ARG
* arg
) {
1057 DWORD reg
= arg
->dst
& D3DSP_REGNUM_MASK
;
1059 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
1060 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
1061 const char* prefix
= pshader
? "PC":"VC";
1063 shader_addline(arg
->buffer
,
1064 "const vec4 %s%lu = { %f, %f, %f, %f };\n", prefix
, reg
,
1065 *((const float *)(arg
->src
+ 0)),
1066 *((const float *)(arg
->src
+ 1)),
1067 *((const float *)(arg
->src
+ 2)),
1068 *((const float *)(arg
->src
+ 3)) );
1070 arg
->reg_maps
->constantsF
[reg
] = 1;
1073 /** Process the D3DSIO_DEFI opcode into a GLSL string - creates a local ivec4
1074 * integer constant, and stores it's usage on the regmaps. */
1075 void shader_glsl_defi(SHADER_OPCODE_ARG
* arg
) {
1077 DWORD reg
= arg
->dst
& D3DSP_REGNUM_MASK
;
1079 shader_addline(arg
->buffer
,
1080 "const ivec4 I%lu = { %ld, %ld, %ld, %ld };\n", reg
,
1081 (long)arg
->src
[0], (long)arg
->src
[1],
1082 (long)arg
->src
[2], (long)arg
->src
[3]);
1084 arg
->reg_maps
->constantsI
[reg
] = 1;
1087 /** Process the D3DSIO_DEFB opcode into a GLSL string - creates a local boolean
1088 * constant, and stores it's usage on the regmaps. */
1089 void shader_glsl_defb(SHADER_OPCODE_ARG
* arg
) {
1091 DWORD reg
= arg
->dst
& D3DSP_REGNUM_MASK
;
1093 shader_addline(arg
->buffer
, "const bool B%lu = %s;\n", reg
, (arg
->src
[0]) ? "true" : "false");
1095 arg
->reg_maps
->constantsB
[reg
] = 1;
1098 /** Process the D3DSIO_LIT instruction in GLSL:
1099 * dst.x = dst.w = 1.0
1100 * dst.y = (src0.x > 0) ? src0.x
1101 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1102 * where src.w is clamped at +- 128
1104 void shader_glsl_lit(SHADER_OPCODE_ARG
* arg
) {
1106 char dst_str
[100], src0_str
[100];
1107 char dst_reg
[50], src0_reg
[50];
1108 char dst_mask
[6], src0_mask
[6];
1110 shader_glsl_add_param(arg
, arg
->dst
, 0, FALSE
, dst_reg
, dst_mask
, dst_str
);
1111 shader_glsl_add_param(arg
, arg
->src
[0], arg
->src_addr
[0], TRUE
, src0_reg
, src0_mask
, src0_str
);
1113 shader_addline(arg
->buffer
,
1114 "%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",
1115 dst_str
, src0_reg
, src0_reg
, src0_reg
, src0_reg
, src0_reg
, src0_reg
, dst_mask
);
1118 /** Process the D3DSIO_DST instruction in GLSL:
1120 * dst.y = src0.x * src0.y
1124 void shader_glsl_dst(SHADER_OPCODE_ARG
* arg
) {
1126 char dst_str
[100], src0_str
[100], src1_str
[100];
1127 char dst_reg
[50], src0_reg
[50], src1_reg
[50];
1128 char dst_mask
[6], src0_mask
[6], src1_mask
[6];
1130 shader_glsl_add_param(arg
, arg
->dst
, 0, FALSE
, dst_reg
, dst_mask
, dst_str
);
1131 shader_glsl_add_param(arg
, arg
->src
[0], arg
->src_addr
[0], TRUE
, src0_reg
, src0_mask
, src0_str
);
1132 shader_glsl_add_param(arg
, arg
->src
[1], arg
->src_addr
[1], TRUE
, src1_reg
, src1_mask
, src1_str
);
1134 shader_addline(arg
->buffer
, "%s = vec4(1.0, %s.x * %s.y, %s.z, %s.w)%s;\n",
1135 dst_str
, src0_reg
, src1_reg
, src0_reg
, src1_reg
, dst_mask
);
1138 /** Process the D3DSIO_SINCOS instruction in GLSL:
1139 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1140 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1142 * dst.x = cos(src0.?)
1143 * dst.y = sin(src0.?)
1147 void shader_glsl_sincos(SHADER_OPCODE_ARG
* arg
) {
1149 char dst_str
[100], src0_str
[100];
1150 char dst_reg
[50], src0_reg
[50];
1151 char dst_mask
[6], src0_mask
[6];
1153 shader_glsl_add_param(arg
, arg
->dst
, 0, FALSE
, dst_reg
, dst_mask
, dst_str
);
1154 shader_glsl_add_param(arg
, arg
->src
[0], arg
->src_addr
[0], TRUE
, src0_reg
, src0_mask
, src0_str
);
1156 shader_addline(arg
->buffer
, "%s = vec4(cos(%s), sin(%s), %s.z, %s.w)%s;\n",
1157 dst_str
, src0_str
, src0_str
, dst_reg
, dst_reg
, dst_mask
);
1160 /** Process the D3DSIO_LOOP instruction in GLSL:
1161 * Start a for() loop where src0.y is the initial value of aL,
1162 * increment aL by src0.z for a total of src0.x iterations.
1163 * Need to use a temporary variable for this operation.
1165 void shader_glsl_loop(SHADER_OPCODE_ARG
* arg
) {
1171 shader_glsl_add_param(arg
, arg
->src
[0], arg
->src_addr
[0], TRUE
, src0_reg
, src0_mask
, src0_str
);
1173 shader_addline(arg
->buffer
, "for (tmpInt = 0, aL = %s.y; tmpInt < %s.x; tmpInt++, aL += %s.z) {\n",
1174 src0_reg
, src0_reg
, src0_reg
);
1177 /** Process the D3DSIO_ENDLOOP instruction in GLSL:
1178 * End the for() loop
1180 void shader_glsl_endloop(SHADER_OPCODE_ARG
* arg
) {
1182 shader_addline(arg
->buffer
, "}\n");
1186 /*********************************************
1187 * Pixel Shader Specific Code begins here
1188 ********************************************/
1189 void pshader_glsl_tex(SHADER_OPCODE_ARG
* arg
) {
1191 /* FIXME: Make this work for more than just 2D textures */
1193 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1194 SHADER_BUFFER
* buffer
= arg
->buffer
;
1195 DWORD hex_version
= This
->baseShader
.hex_version
;
1197 char dst_str
[100], dst_reg
[50], dst_mask
[6];
1198 char coord_str
[100], coord_reg
[50], coord_mask
[6];
1199 char sampler_str
[100], sampler_reg
[50], sampler_mask
[6];
1200 DWORD reg_dest_code
= arg
->dst
& D3DSP_REGNUM_MASK
;
1201 DWORD sampler_code
, sampler_type
;
1203 /* All versions have a destination register */
1204 shader_glsl_add_param(arg
, arg
->dst
, 0, FALSE
, dst_reg
, dst_mask
, dst_str
);
1206 /* 1.0-1.3: Use destination register as coordinate source.
1207 1.4+: Use provided coordinate source register. */
1208 if (hex_version
< D3DPS_VERSION(1,4))
1209 strcpy(coord_reg
, dst_reg
);
1211 shader_glsl_add_param(arg
, arg
->src
[0], arg
->src_addr
[0], TRUE
, coord_reg
, coord_mask
, coord_str
);
1213 /* 1.0-1.4: Use destination register as coordinate source.
1214 * 2.0+: Use provided coordinate source register. */
1215 if (hex_version
< D3DPS_VERSION(2,0)) {
1216 sprintf(sampler_str
, "Psampler%lu", reg_dest_code
);
1217 sampler_code
= reg_dest_code
;
1220 shader_glsl_add_param(arg
, arg
->src
[1], arg
->src_addr
[1], TRUE
, sampler_reg
, sampler_mask
, sampler_str
);
1221 sampler_code
= arg
->src
[1] & D3DSP_REGNUM_MASK
;
1224 sampler_type
= arg
->reg_maps
->samplers
[sampler_code
] & D3DSP_TEXTURETYPE_MASK
;
1225 switch(sampler_type
) {
1228 shader_addline(buffer
, "%s = texture2D(%s, %s.st);\n", dst_str
, sampler_str
, coord_reg
);
1231 shader_addline(buffer
, "%s = textureCube(%s, %s.stp);\n", dst_str
, sampler_str
, coord_reg
);
1234 shader_addline(buffer
, "%s = texture3D(%s, %s.stp);\n", dst_str
, sampler_str
, coord_reg
);
1237 shader_addline(buffer
, "%s = unrecognized_stype(%s, %s.stp);\n", dst_str
, sampler_str
, coord_reg
);
1238 FIXME("Unrecognized sampler type: %#lx;\n", sampler_type
);
1243 void pshader_glsl_texcoord(SHADER_OPCODE_ARG
* arg
) {
1245 /* FIXME: Make this work for more than just 2D textures */
1247 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1248 SHADER_BUFFER
* buffer
= arg
->buffer
;
1249 DWORD hex_version
= This
->baseShader
.hex_version
;
1256 shader_glsl_add_param(arg
, arg
->dst
, 0, FALSE
, tmpReg
, tmpMask
, tmpStr
);
1258 if (hex_version
!= D3DPS_VERSION(1,4)) {
1259 DWORD reg
= arg
->dst
& D3DSP_REGNUM_MASK
;
1260 shader_addline(buffer
, "%s = gl_TexCoord[%lu];\n", tmpReg
, reg
);
1262 DWORD reg2
= arg
->src
[0] & D3DSP_REGNUM_MASK
;
1263 shader_addline(buffer
, "%s = gl_TexCoord[%lu]%s;\n", tmpStr
, reg2
, tmpMask
);
1267 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG
* arg
) {
1269 /* FIXME: Make this work for more than just 2D textures */
1271 DWORD reg
= arg
->dst
& D3DSP_REGNUM_MASK
;
1272 SHADER_BUFFER
* buffer
= arg
->buffer
;
1277 shader_glsl_add_param(arg
, arg
->src
[0], arg
->src_addr
[0], TRUE
, src0_name
, src0_mask
, src0_str
);
1278 shader_addline(buffer
, "tmp0.x = dot(vec3(T%lu), vec3(%s));\n", reg
, src0_name
, src0_mask
, src0_str
);
1281 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG
* arg
) {
1283 /* FIXME: Make this work for more than just 2D textures */
1285 DWORD reg
= arg
->dst
& D3DSP_REGNUM_MASK
;
1286 SHADER_BUFFER
* buffer
= arg
->buffer
;
1291 shader_glsl_add_param(arg
, arg
->src
[0], arg
->src_addr
[0], TRUE
, src0_name
, src0_mask
, src0_str
);
1292 shader_addline(buffer
, "tmp0.y = dot(vec3(T%lu), vec3(%s));\n", reg
, src0_str
);
1293 shader_addline(buffer
, "T%lu = texture2D(Psampler%lu, tmp0.st);\n", reg
, reg
);
1296 void pshader_glsl_input_pack(
1297 SHADER_BUFFER
* buffer
,
1298 DWORD
* semantics_in
) {
1302 for (i
= 0; i
< WINED3DSHADERDECLUSAGE_MAX_USAGE
; i
++) {
1304 DWORD reg
= semantics_in
[i
];
1305 unsigned int regnum
= reg
& D3DSP_REGNUM_MASK
;
1311 shader_glsl_get_output_register_swizzle(reg
, reg_mask
);
1315 case WINED3DSHADERDECLUSAGE_DIFFUSE
:
1316 shader_addline(buffer
, "IN%lu%s = vec4(gl_Color)%s;\n",
1317 regnum
, reg_mask
, reg_mask
);
1320 case WINED3DSHADERDECLUSAGE_SPECULAR
:
1321 shader_addline(buffer
, "IN%lu%s = vec4(gl_SecondaryColor)%s;\n",
1322 regnum
, reg_mask
, reg_mask
);
1325 case WINED3DSHADERDECLUSAGE_TEXCOORD0
:
1326 case WINED3DSHADERDECLUSAGE_TEXCOORD1
:
1327 case WINED3DSHADERDECLUSAGE_TEXCOORD2
:
1328 case WINED3DSHADERDECLUSAGE_TEXCOORD3
:
1329 case WINED3DSHADERDECLUSAGE_TEXCOORD4
:
1330 case WINED3DSHADERDECLUSAGE_TEXCOORD5
:
1331 case WINED3DSHADERDECLUSAGE_TEXCOORD6
:
1332 case WINED3DSHADERDECLUSAGE_TEXCOORD7
:
1333 shader_addline(buffer
, "IN%lu%s = vec4(gl_TexCoord[%lu])%s;\n",
1334 regnum
, reg_mask
, i
- WINED3DSHADERDECLUSAGE_TEXCOORD0
, reg_mask
);
1337 case WINED3DSHADERDECLUSAGE_FOG
:
1338 shader_addline(buffer
, "IN%lu%s = vec4(gl_FogFragCoord)%s;\n",
1339 regnum
, reg_mask
, reg_mask
);
1343 shader_addline(buffer
, "IN%lu%s = vec4(unsupported_input)%s;\n",
1344 regnum
, reg_mask
, reg_mask
);
1349 /*********************************************
1350 * Vertex Shader Specific Code begins here
1351 ********************************************/
1353 void vshader_glsl_output_unpack(
1354 SHADER_BUFFER
* buffer
,
1355 DWORD
* semantics_out
) {
1359 for (i
= 0; i
< WINED3DSHADERDECLUSAGE_MAX_USAGE
; i
++) {
1361 DWORD reg
= semantics_out
[i
];
1362 unsigned int regnum
= reg
& D3DSP_REGNUM_MASK
;
1368 shader_glsl_get_output_register_swizzle(reg
, reg_mask
);
1372 case WINED3DSHADERDECLUSAGE_DIFFUSE
:
1373 shader_addline(buffer
, "gl_FrontColor%s = OUT%lu%s;\n", reg_mask
, regnum
, reg_mask
);
1376 case WINED3DSHADERDECLUSAGE_SPECULAR
:
1377 shader_addline(buffer
, "gl_FrontSecondaryColor%s = OUT%lu%s;\n", reg_mask
, regnum
, reg_mask
);
1380 case WINED3DSHADERDECLUSAGE_POSITION
:
1381 shader_addline(buffer
, "gl_Position%s = OUT%lu%s;\n", reg_mask
, regnum
, reg_mask
);
1384 case WINED3DSHADERDECLUSAGE_TEXCOORD0
:
1385 case WINED3DSHADERDECLUSAGE_TEXCOORD1
:
1386 case WINED3DSHADERDECLUSAGE_TEXCOORD2
:
1387 case WINED3DSHADERDECLUSAGE_TEXCOORD3
:
1388 case WINED3DSHADERDECLUSAGE_TEXCOORD4
:
1389 case WINED3DSHADERDECLUSAGE_TEXCOORD5
:
1390 case WINED3DSHADERDECLUSAGE_TEXCOORD6
:
1391 case WINED3DSHADERDECLUSAGE_TEXCOORD7
:
1392 shader_addline(buffer
, "gl_TexCoord[%lu]%s = OUT%lu%s;\n",
1393 i
- WINED3DSHADERDECLUSAGE_TEXCOORD0
, reg_mask
, regnum
, reg_mask
);
1396 case WINED3DSHADERDECLUSAGE_PSIZE
:
1397 shader_addline(buffer
, "gl_PointSize = OUT%lu.x;\n", regnum
);
1400 case WINED3DSHADERDECLUSAGE_FOG
:
1401 shader_addline(buffer
, "gl_FogFragCoord%s = OUT%lu%s;\n", reg_mask
, regnum
, reg_mask
);
1405 shader_addline(buffer
, "unsupported_output%s = OUT%lu%s;\n", reg_mask
, regnum
, reg_mask
);