2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * D3D shader asm has swizzles on source parameters, and write masks for
24 * destination parameters. GLSL uses swizzles for both. The result of this is
25 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
26 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
27 * mask for the destination parameter into account.
32 #include "wined3d_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader
);
35 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants
);
37 #define GLINFO_LOCATION (*gl_info)
52 } glsl_sample_function_t
;
54 /** Prints the GLSL info log which will contain error messages if they exist */
55 void print_glsl_info_log(WineD3D_GL_Info
*gl_info
, GLhandleARB obj
) {
57 int infologLength
= 0;
60 GL_EXTCALL(glGetObjectParameterivARB(obj
,
61 GL_OBJECT_INFO_LOG_LENGTH_ARB
,
64 /* A size of 1 is just a null-terminated string, so the log should be bigger than
65 * that if there are errors. */
66 if (infologLength
> 1)
68 infoLog
= HeapAlloc(GetProcessHeap(), 0, infologLength
);
69 GL_EXTCALL(glGetInfoLogARB(obj
, infologLength
, NULL
, infoLog
));
70 FIXME("Error received from GLSL shader #%u: %s\n", obj
, debugstr_a(infoLog
));
71 HeapFree(GetProcessHeap(), 0, infoLog
);
76 * Loads (pixel shader) samplers
78 static void shader_glsl_load_psamplers(
79 WineD3D_GL_Info
*gl_info
,
80 IWineD3DStateBlock
* iface
) {
82 IWineD3DStateBlockImpl
* stateBlock
= (IWineD3DStateBlockImpl
*) iface
;
83 GLhandleARB programId
= stateBlock
->glsl_program
->programId
;
86 char sampler_name
[20];
88 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
) {
89 snprintf(sampler_name
, sizeof(sampler_name
), "Psampler%d", i
);
90 name_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, sampler_name
));
92 int mapped_unit
= stateBlock
->wineD3DDevice
->texUnitMap
[i
];
93 if (mapped_unit
!= -1 && mapped_unit
< GL_LIMITS(fragment_samplers
)) {
94 TRACE("Loading %s for texture %d\n", sampler_name
, mapped_unit
);
95 GL_EXTCALL(glUniform1iARB(name_loc
, mapped_unit
));
96 checkGLcall("glUniform1iARB");
98 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name
, mapped_unit
);
104 static void shader_glsl_load_vsamplers(WineD3D_GL_Info
*gl_info
, IWineD3DStateBlock
* iface
) {
105 IWineD3DStateBlockImpl
* stateBlock
= (IWineD3DStateBlockImpl
*) iface
;
106 GLhandleARB programId
= stateBlock
->glsl_program
->programId
;
107 GLhandleARB name_loc
;
108 char sampler_name
[20];
111 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
) {
112 snprintf(sampler_name
, sizeof(sampler_name
), "Vsampler%d", i
);
113 name_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, sampler_name
));
114 if (name_loc
!= -1) {
115 int mapped_unit
= stateBlock
->wineD3DDevice
->texUnitMap
[MAX_FRAGMENT_SAMPLERS
+ i
];
116 if (mapped_unit
!= -1 && mapped_unit
< GL_LIMITS(combined_samplers
)) {
117 TRACE("Loading %s for texture %d\n", sampler_name
, mapped_unit
);
118 GL_EXTCALL(glUniform1iARB(name_loc
, mapped_unit
));
119 checkGLcall("glUniform1iARB");
121 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name
, mapped_unit
);
128 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
129 * When constant_list == NULL, it will load all the constants.
131 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl
* This
, WineD3D_GL_Info
*gl_info
,
132 unsigned int max_constants
, float* constants
, GLhandleARB
*constant_locations
,
133 struct list
*constant_list
) {
134 constants_entry
*constant
;
135 local_constant
* lconst
;
140 if (TRACE_ON(d3d_shader
)) {
141 LIST_FOR_EACH_ENTRY(constant
, constant_list
, constants_entry
, entry
) {
146 tmp_loc
= constant_locations
[i
];
148 TRACE_(d3d_constants
)("Loading constants %i: %f, %f, %f, %f\n", i
,
149 constants
[i
* 4 + 0], constants
[i
* 4 + 1],
150 constants
[i
* 4 + 2], constants
[i
* 4 + 3]);
155 LIST_FOR_EACH_ENTRY(constant
, constant_list
, constants_entry
, entry
) {
160 tmp_loc
= constant_locations
[i
];
162 /* We found this uniform name in the program - go ahead and send the data */
163 GL_EXTCALL(glUniform4fvARB(tmp_loc
, 1, constants
+ (i
* 4)));
167 checkGLcall("glUniform4fvARB()");
169 /* Load immediate constants */
170 if (TRACE_ON(d3d_shader
)) {
171 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
172 tmp_loc
= constant_locations
[lconst
->idx
];
174 GLfloat
* values
= (GLfloat
*)lconst
->value
;
175 TRACE_(d3d_constants
)("Loading local constants %i: %f, %f, %f, %f\n", lconst
->idx
,
176 values
[0], values
[1], values
[2], values
[3]);
180 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
181 tmp_loc
= constant_locations
[lconst
->idx
];
183 /* We found this uniform name in the program - go ahead and send the data */
184 GL_EXTCALL(glUniform4fvARB(tmp_loc
, 1, (GLfloat
*)lconst
->value
));
187 checkGLcall("glUniform4fvARB()");
191 * Loads integer constants (aka uniforms) into the currently set GLSL program.
192 * When @constants_set == NULL, it will load all the constants.
194 static void shader_glsl_load_constantsI(
195 IWineD3DBaseShaderImpl
* This
,
196 WineD3D_GL_Info
*gl_info
,
197 GLhandleARB programId
,
198 unsigned max_constants
,
200 BOOL
* constants_set
) {
205 char is_pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
206 const char* prefix
= is_pshader
? "PI":"VI";
209 for (i
=0; i
<max_constants
; ++i
) {
210 if (NULL
== constants_set
|| constants_set
[i
]) {
212 TRACE_(d3d_constants
)("Loading constants %i: %i, %i, %i, %i\n",
213 i
, constants
[i
*4], constants
[i
*4+1], constants
[i
*4+2], constants
[i
*4+3]);
215 /* TODO: Benchmark and see if it would be beneficial to store the
216 * locations of the constants to avoid looking up each time */
217 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, i
);
218 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
220 /* We found this uniform name in the program - go ahead and send the data */
221 GL_EXTCALL(glUniform4ivARB(tmp_loc
, 1, &constants
[i
*4]));
222 checkGLcall("glUniform4ivARB");
227 /* Load immediate constants */
228 ptr
= list_head(&This
->baseShader
.constantsI
);
230 local_constant
* lconst
= LIST_ENTRY(ptr
, struct local_constant
, entry
);
231 unsigned int idx
= lconst
->idx
;
232 GLint
* values
= (GLint
*) lconst
->value
;
234 TRACE_(d3d_constants
)("Loading local constants %i: %i, %i, %i, %i\n", idx
,
235 values
[0], values
[1], values
[2], values
[3]);
237 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, idx
);
238 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
240 /* We found this uniform name in the program - go ahead and send the data */
241 GL_EXTCALL(glUniform4ivARB(tmp_loc
, 1, values
));
242 checkGLcall("glUniform4ivARB");
244 ptr
= list_next(&This
->baseShader
.constantsI
, ptr
);
249 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
250 * When @constants_set == NULL, it will load all the constants.
252 static void shader_glsl_load_constantsB(
253 IWineD3DBaseShaderImpl
* This
,
254 WineD3D_GL_Info
*gl_info
,
255 GLhandleARB programId
,
256 unsigned max_constants
,
258 BOOL
* constants_set
) {
263 char is_pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
264 const char* prefix
= is_pshader
? "PB":"VB";
267 for (i
=0; i
<max_constants
; ++i
) {
268 if (NULL
== constants_set
|| constants_set
[i
]) {
270 TRACE_(d3d_constants
)("Loading constants %i: %i;\n", i
, constants
[i
*4]);
272 /* TODO: Benchmark and see if it would be beneficial to store the
273 * locations of the constants to avoid looking up each time */
274 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, i
);
275 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
277 /* We found this uniform name in the program - go ahead and send the data */
278 GL_EXTCALL(glUniform1ivARB(tmp_loc
, 1, &constants
[i
*4]));
279 checkGLcall("glUniform1ivARB");
284 /* Load immediate constants */
285 ptr
= list_head(&This
->baseShader
.constantsB
);
287 local_constant
* lconst
= LIST_ENTRY(ptr
, struct local_constant
, entry
);
288 unsigned int idx
= lconst
->idx
;
289 GLint
* values
= (GLint
*) lconst
->value
;
291 TRACE_(d3d_constants
)("Loading local constants %i: %i\n", idx
, values
[0]);
293 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, idx
);
294 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
296 /* We found this uniform name in the program - go ahead and send the data */
297 GL_EXTCALL(glUniform1ivARB(tmp_loc
, 1, values
));
298 checkGLcall("glUniform1ivARB");
300 ptr
= list_next(&This
->baseShader
.constantsB
, ptr
);
307 * Loads the app-supplied constants into the currently set GLSL program.
309 void shader_glsl_load_constants(
310 IWineD3DDevice
* device
,
312 char useVertexShader
) {
314 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) device
;
315 IWineD3DStateBlockImpl
* stateBlock
= deviceImpl
->stateBlock
;
316 WineD3D_GL_Info
*gl_info
= &deviceImpl
->adapter
->gl_info
;
318 GLhandleARB
*constant_locations
;
319 struct list
*constant_list
;
320 GLhandleARB programId
;
323 if (!stateBlock
->glsl_program
) {
324 /* No GLSL program set - nothing to do. */
327 programId
= stateBlock
->glsl_program
->programId
;
329 if (useVertexShader
) {
330 IWineD3DBaseShaderImpl
* vshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->vertexShader
;
333 constant_locations
= stateBlock
->glsl_program
->vuniformF_locations
;
334 constant_list
= &stateBlock
->set_vconstantsF
;
336 /* Load vertex shader samplers */
337 shader_glsl_load_vsamplers(gl_info
, (IWineD3DStateBlock
*)stateBlock
);
339 /* Load DirectX 9 float constants/uniforms for vertex shader */
340 shader_glsl_load_constantsF(vshader
, gl_info
, GL_LIMITS(vshader_constantsF
),
341 stateBlock
->vertexShaderConstantF
, constant_locations
, constant_list
);
343 /* Load DirectX 9 integer constants/uniforms for vertex shader */
344 shader_glsl_load_constantsI(vshader
, gl_info
, programId
, MAX_CONST_I
,
345 stateBlock
->vertexShaderConstantI
,
346 stateBlock
->changed
.vertexShaderConstantsI
);
348 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
349 shader_glsl_load_constantsB(vshader
, gl_info
, programId
, MAX_CONST_B
,
350 stateBlock
->vertexShaderConstantB
,
351 stateBlock
->changed
.vertexShaderConstantsB
);
353 /* Upload the position fixup params */
354 pos
= GL_EXTCALL(glGetUniformLocationARB(programId
, "posFixup"));
355 checkGLcall("glGetUniformLocationARB");
356 GL_EXTCALL(glUniform4fvARB(pos
, 1, &deviceImpl
->posFixup
[0]));
357 checkGLcall("glUniform4fvARB");
360 if (usePixelShader
) {
362 IWineD3DBaseShaderImpl
* pshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->pixelShader
;
364 constant_locations
= stateBlock
->glsl_program
->puniformF_locations
;
365 constant_list
= &stateBlock
->set_pconstantsF
;
367 /* Load pixel shader samplers */
368 shader_glsl_load_psamplers(gl_info
, (IWineD3DStateBlock
*) stateBlock
);
370 /* Load DirectX 9 float constants/uniforms for pixel shader */
371 shader_glsl_load_constantsF(pshader
, gl_info
, GL_LIMITS(pshader_constantsF
),
372 stateBlock
->pixelShaderConstantF
, constant_locations
, constant_list
);
374 /* Load DirectX 9 integer constants/uniforms for pixel shader */
375 shader_glsl_load_constantsI(pshader
, gl_info
, programId
, MAX_CONST_I
,
376 stateBlock
->pixelShaderConstantI
,
377 stateBlock
->changed
.pixelShaderConstantsI
);
379 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
380 shader_glsl_load_constantsB(pshader
, gl_info
, programId
, MAX_CONST_B
,
381 stateBlock
->pixelShaderConstantB
,
382 stateBlock
->changed
.pixelShaderConstantsB
);
384 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
385 * It can't be 0 for a valid texbem instruction.
387 if(((IWineD3DPixelShaderImpl
*) pshader
)->needsbumpmat
!= -1) {
388 float *data
= (float *) &stateBlock
->textureState
[(int) ((IWineD3DPixelShaderImpl
*) pshader
)->needsbumpmat
][WINED3DTSS_BUMPENVMAT00
];
389 pos
= GL_EXTCALL(glGetUniformLocationARB(programId
, "bumpenvmat"));
390 checkGLcall("glGetUniformLocationARB");
391 GL_EXTCALL(glUniformMatrix2fvARB(pos
, 1, 0, data
));
392 checkGLcall("glUniform4fvARB");
397 /** Generate the variable & register declarations for the GLSL output target */
398 void shader_generate_glsl_declarations(
399 IWineD3DBaseShader
*iface
,
400 shader_reg_maps
* reg_maps
,
401 SHADER_BUFFER
* buffer
,
402 WineD3D_GL_Info
* gl_info
) {
404 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
407 /* There are some minor differences between pixel and vertex shaders */
408 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
409 char prefix
= pshader
? 'P' : 'V';
411 /* Prototype the subroutines */
412 for (i
= 0; i
< This
->baseShader
.limits
.label
; i
++) {
413 if (reg_maps
->labels
[i
])
414 shader_addline(buffer
, "void subroutine%lu();\n", i
);
417 /* Declare the constants (aka uniforms) */
418 if (This
->baseShader
.limits
.constant_float
> 0) {
419 unsigned max_constantsF
= min(This
->baseShader
.limits
.constant_float
,
420 (pshader
? GL_LIMITS(pshader_constantsF
) : GL_LIMITS(vshader_constantsF
)));
421 shader_addline(buffer
, "uniform vec4 %cC[%u];\n", prefix
, max_constantsF
);
424 if (This
->baseShader
.limits
.constant_int
> 0)
425 shader_addline(buffer
, "uniform ivec4 %cI[%u];\n", prefix
, This
->baseShader
.limits
.constant_int
);
427 if (This
->baseShader
.limits
.constant_bool
> 0)
428 shader_addline(buffer
, "uniform bool %cB[%u];\n", prefix
, This
->baseShader
.limits
.constant_bool
);
431 shader_addline(buffer
, "uniform vec4 posFixup;\n");
432 else if(reg_maps
->bumpmat
!= -1)
433 shader_addline(buffer
, "uniform mat2 bumpenvmat;\n");
435 /* Declare texture samplers */
436 for (i
= 0; i
< This
->baseShader
.limits
.sampler
; i
++) {
437 if (reg_maps
->samplers
[i
]) {
439 DWORD stype
= reg_maps
->samplers
[i
] & WINED3DSP_TEXTURETYPE_MASK
;
443 shader_addline(buffer
, "uniform sampler1D %csampler%lu;\n", prefix
, i
);
446 shader_addline(buffer
, "uniform sampler2D %csampler%lu;\n", prefix
, i
);
448 case WINED3DSTT_CUBE
:
449 shader_addline(buffer
, "uniform samplerCube %csampler%lu;\n", prefix
, i
);
451 case WINED3DSTT_VOLUME
:
452 shader_addline(buffer
, "uniform sampler3D %csampler%lu;\n", prefix
, i
);
455 shader_addline(buffer
, "uniform unsupported_sampler %csampler%lu;\n", prefix
, i
);
456 FIXME("Unrecognized sampler type: %#x\n", stype
);
462 /* Declare address variables */
463 for (i
= 0; i
< This
->baseShader
.limits
.address
; i
++) {
464 if (reg_maps
->address
[i
])
465 shader_addline(buffer
, "ivec4 A%d;\n", i
);
468 /* Declare texture coordinate temporaries and initialize them */
469 for (i
= 0; i
< This
->baseShader
.limits
.texcoord
; i
++) {
470 if (reg_maps
->texcoord
[i
])
471 shader_addline(buffer
, "vec4 T%lu = gl_TexCoord[%lu];\n", i
, i
);
474 /* Declare input register temporaries */
475 for (i
=0; i
< This
->baseShader
.limits
.packed_input
; i
++) {
476 if (reg_maps
->packed_input
[i
])
477 shader_addline(buffer
, "vec4 IN%lu;\n", i
);
480 /* Declare output register temporaries */
481 for (i
= 0; i
< This
->baseShader
.limits
.packed_output
; i
++) {
482 if (reg_maps
->packed_output
[i
])
483 shader_addline(buffer
, "vec4 OUT%lu;\n", i
);
486 /* Declare temporary variables */
487 for(i
= 0; i
< This
->baseShader
.limits
.temporary
; i
++) {
488 if (reg_maps
->temporary
[i
])
489 shader_addline(buffer
, "vec4 R%lu;\n", i
);
492 /* Declare attributes */
493 for (i
= 0; i
< This
->baseShader
.limits
.attributes
; i
++) {
494 if (reg_maps
->attributes
[i
])
495 shader_addline(buffer
, "attribute vec4 attrib%i;\n", i
);
498 /* Declare loop register aL */
499 if (reg_maps
->loop
) {
500 shader_addline(buffer
, "int aL;\n");
501 shader_addline(buffer
, "int tmpInt;\n");
504 /* Temporary variables for matrix operations */
505 shader_addline(buffer
, "vec4 tmp0;\n");
506 shader_addline(buffer
, "vec4 tmp1;\n");
508 /* Start the main program */
509 shader_addline(buffer
, "void main() {\n");
512 /*****************************************************************************
513 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
515 * For more information, see http://wiki.winehq.org/DirectX-Shaders
516 ****************************************************************************/
519 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG
* arg
, const DWORD param
,
520 const DWORD addr_token
, DWORD mask
, glsl_src_param_t
*src_param
);
522 /** Used for opcode modifiers - They multiply the result by the specified amount */
523 static const char * const shift_glsl_tab
[] = {
525 "2.0 * ", /* 1 (x2) */
526 "4.0 * ", /* 2 (x4) */
527 "8.0 * ", /* 3 (x8) */
528 "16.0 * ", /* 4 (x16) */
529 "32.0 * ", /* 5 (x32) */
536 "0.0625 * ", /* 12 (d16) */
537 "0.125 * ", /* 13 (d8) */
538 "0.25 * ", /* 14 (d4) */
539 "0.5 * " /* 15 (d2) */
542 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
543 static void shader_glsl_gen_modifier (
546 const char *in_regswizzle
,
551 if (instr
== WINED3DSIO_TEXKILL
)
554 switch (instr
& WINED3DSP_SRCMOD_MASK
) {
555 case WINED3DSPSM_DZ
: /* Need to handle this in the instructions itself (texld & texcrd). */
557 case WINED3DSPSM_NONE
:
558 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
560 case WINED3DSPSM_NEG
:
561 sprintf(out_str
, "-%s%s", in_reg
, in_regswizzle
);
563 case WINED3DSPSM_NOT
:
564 sprintf(out_str
, "!%s%s", in_reg
, in_regswizzle
);
566 case WINED3DSPSM_BIAS
:
567 sprintf(out_str
, "(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
569 case WINED3DSPSM_BIASNEG
:
570 sprintf(out_str
, "-(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
572 case WINED3DSPSM_SIGN
:
573 sprintf(out_str
, "(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
575 case WINED3DSPSM_SIGNNEG
:
576 sprintf(out_str
, "-(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
578 case WINED3DSPSM_COMP
:
579 sprintf(out_str
, "(1.0 - %s%s)", in_reg
, in_regswizzle
);
582 sprintf(out_str
, "(2.0 * %s%s)", in_reg
, in_regswizzle
);
584 case WINED3DSPSM_X2NEG
:
585 sprintf(out_str
, "-(2.0 * %s%s)", in_reg
, in_regswizzle
);
587 case WINED3DSPSM_ABS
:
588 sprintf(out_str
, "abs(%s%s)", in_reg
, in_regswizzle
);
590 case WINED3DSPSM_ABSNEG
:
591 sprintf(out_str
, "-abs(%s%s)", in_reg
, in_regswizzle
);
594 FIXME("Unhandled modifier %u\n", (instr
& WINED3DSP_SRCMOD_MASK
));
595 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
599 /** Writes the GLSL variable name that corresponds to the register that the
600 * DX opcode parameter is trying to access */
601 static void shader_glsl_get_register_name(
603 const DWORD addr_token
,
606 SHADER_OPCODE_ARG
* arg
) {
608 /* oPos, oFog and oPts in D3D */
609 static const char * const hwrastout_reg_names
[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
611 DWORD reg
= param
& WINED3DSP_REGNUM_MASK
;
612 DWORD regtype
= shader_get_regtype(param
);
613 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
614 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
615 WineD3D_GL_Info
* gl_info
= &deviceImpl
->adapter
->gl_info
;
617 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
623 case WINED3DSPR_TEMP
:
624 sprintf(tmpStr
, "R%u", reg
);
626 case WINED3DSPR_INPUT
:
628 /* Pixel shaders >= 3.0 */
629 if (WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 3)
630 sprintf(tmpStr
, "IN%u", reg
);
633 strcpy(tmpStr
, "gl_Color");
635 strcpy(tmpStr
, "gl_SecondaryColor");
638 if (vshader_input_is_color((IWineD3DVertexShader
*) This
, reg
))
640 sprintf(tmpStr
, "attrib%u", reg
);
643 case WINED3DSPR_CONST
:
645 const char* prefix
= pshader
? "PC":"VC";
647 /* Relative addressing */
648 if (param
& WINED3DSHADER_ADDRMODE_RELATIVE
) {
650 /* Relative addressing on shaders 2.0+ have a relative address token,
651 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
652 if (WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 2) {
653 glsl_src_param_t rel_param
;
654 shader_glsl_add_src_param(arg
, addr_token
, 0, WINED3DSP_WRITEMASK_0
, &rel_param
);
655 sprintf(tmpStr
, "%s[%s + %u]", prefix
, rel_param
.param_str
, reg
);
657 sprintf(tmpStr
, "%s[A0.x + %u]", prefix
, reg
);
660 sprintf(tmpStr
, "%s[%u]", prefix
, reg
);
664 case WINED3DSPR_CONSTINT
:
666 sprintf(tmpStr
, "PI[%u]", reg
);
668 sprintf(tmpStr
, "VI[%u]", reg
);
670 case WINED3DSPR_CONSTBOOL
:
672 sprintf(tmpStr
, "PB[%u]", reg
);
674 sprintf(tmpStr
, "VB[%u]", reg
);
676 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
678 sprintf(tmpStr
, "T%u", reg
);
680 sprintf(tmpStr
, "A%u", reg
);
683 case WINED3DSPR_LOOP
:
684 sprintf(tmpStr
, "aL");
686 case WINED3DSPR_SAMPLER
:
688 sprintf(tmpStr
, "Psampler%u", reg
);
690 sprintf(tmpStr
, "Vsampler%u", reg
);
692 case WINED3DSPR_COLOROUT
:
693 if (reg
>= GL_LIMITS(buffers
)) {
694 WARN("Write to render target %u, only %d supported\n", reg
, 4);
696 if (GL_SUPPORT(ARB_DRAW_BUFFERS
)) {
697 sprintf(tmpStr
, "gl_FragData[%u]", reg
);
698 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
699 sprintf(tmpStr
, "gl_FragColor");
702 case WINED3DSPR_RASTOUT
:
703 sprintf(tmpStr
, "%s", hwrastout_reg_names
[reg
]);
705 case WINED3DSPR_DEPTHOUT
:
706 sprintf(tmpStr
, "gl_FragDepth");
708 case WINED3DSPR_ATTROUT
:
710 sprintf(tmpStr
, "gl_FrontColor");
712 sprintf(tmpStr
, "gl_FrontSecondaryColor");
715 case WINED3DSPR_TEXCRDOUT
:
716 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
717 if (WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 3)
718 sprintf(tmpStr
, "OUT%u", reg
);
720 sprintf(tmpStr
, "gl_TexCoord[%u]", reg
);
722 case WINED3DSPR_MISCTYPE
:
725 sprintf(tmpStr
, "gl_FragCoord");
727 /* gl_FrontFacing could be used for vFace, but note that
728 * gl_FrontFacing is a bool, while vFace is a float for
729 * which the sign determines front/back */
730 FIXME("Unhandled misctype register %d\n", reg
);
731 sprintf(tmpStr
, "unrecognized_register");
735 FIXME("Unhandled register name Type(%d)\n", regtype
);
736 sprintf(tmpStr
, "unrecognized_register");
740 strcat(regstr
, tmpStr
);
743 /* Get the GLSL write mask for the destination register */
744 static DWORD
shader_glsl_get_write_mask(const DWORD param
, char *write_mask
) {
745 char *ptr
= write_mask
;
746 DWORD mask
= param
& WINED3DSP_WRITEMASK_ALL
;
748 if (shader_is_scalar(param
)) {
749 mask
= WINED3DSP_WRITEMASK_0
;
752 if (param
& WINED3DSP_WRITEMASK_0
) *ptr
++ = 'x';
753 if (param
& WINED3DSP_WRITEMASK_1
) *ptr
++ = 'y';
754 if (param
& WINED3DSP_WRITEMASK_2
) *ptr
++ = 'z';
755 if (param
& WINED3DSP_WRITEMASK_3
) *ptr
++ = 'w';
763 static size_t shader_glsl_get_write_mask_size(DWORD write_mask
) {
766 if (write_mask
& WINED3DSP_WRITEMASK_0
) ++size
;
767 if (write_mask
& WINED3DSP_WRITEMASK_1
) ++size
;
768 if (write_mask
& WINED3DSP_WRITEMASK_2
) ++size
;
769 if (write_mask
& WINED3DSP_WRITEMASK_3
) ++size
;
774 static void shader_glsl_get_swizzle(const DWORD param
, BOOL fixup
, DWORD mask
, char *swizzle_str
) {
775 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
776 * but addressed as "rgba". To fix this we need to swap the register's x
777 * and z components. */
778 DWORD swizzle
= (param
& WINED3DSP_SWIZZLE_MASK
) >> WINED3DSP_SWIZZLE_SHIFT
;
779 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
780 char *ptr
= swizzle_str
;
782 if (!shader_is_scalar(param
)) {
784 /* swizzle bits fields: wwzzyyxx */
785 if (mask
& WINED3DSP_WRITEMASK_0
) *ptr
++ = swizzle_chars
[swizzle
& 0x03];
786 if (mask
& WINED3DSP_WRITEMASK_1
) *ptr
++ = swizzle_chars
[(swizzle
>> 2) & 0x03];
787 if (mask
& WINED3DSP_WRITEMASK_2
) *ptr
++ = swizzle_chars
[(swizzle
>> 4) & 0x03];
788 if (mask
& WINED3DSP_WRITEMASK_3
) *ptr
++ = swizzle_chars
[(swizzle
>> 6) & 0x03];
794 /* From a given parameter token, generate the corresponding GLSL string.
795 * Also, return the actual register name and swizzle in case the
796 * caller needs this information as well. */
797 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG
* arg
, const DWORD param
,
798 const DWORD addr_token
, DWORD mask
, glsl_src_param_t
*src_param
) {
799 BOOL is_color
= FALSE
;
802 src_param
->reg_name
[0] = '\0';
803 src_param
->param_str
[0] = '\0';
804 swizzle_str
[0] = '\0';
806 shader_glsl_get_register_name(param
, addr_token
, src_param
->reg_name
, &is_color
, arg
);
808 shader_glsl_get_swizzle(param
, is_color
, mask
, swizzle_str
);
809 shader_glsl_gen_modifier(param
, src_param
->reg_name
, swizzle_str
, src_param
->param_str
);
812 /* From a given parameter token, generate the corresponding GLSL string.
813 * Also, return the actual register name and swizzle in case the
814 * caller needs this information as well. */
815 static DWORD
shader_glsl_add_dst_param(SHADER_OPCODE_ARG
* arg
, const DWORD param
,
816 const DWORD addr_token
, glsl_dst_param_t
*dst_param
) {
817 BOOL is_color
= FALSE
;
819 dst_param
->mask_str
[0] = '\0';
820 dst_param
->reg_name
[0] = '\0';
822 shader_glsl_get_register_name(param
, addr_token
, dst_param
->reg_name
, &is_color
, arg
);
823 return shader_glsl_get_write_mask(param
, dst_param
->mask_str
);
826 /* Append the destination part of the instruction to the buffer, return the effective write mask */
827 static DWORD
shader_glsl_append_dst_ext(SHADER_BUFFER
*buffer
, SHADER_OPCODE_ARG
*arg
, const DWORD param
) {
828 glsl_dst_param_t dst_param
;
832 mask
= shader_glsl_add_dst_param(arg
, param
, arg
->dst_addr
, &dst_param
);
835 shift
= (param
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
836 shader_addline(buffer
, "%s%s = %s(", dst_param
.reg_name
, dst_param
.mask_str
, shift_glsl_tab
[shift
]);
842 /* Append the destination part of the instruction to the buffer, return the effective write mask */
843 static DWORD
shader_glsl_append_dst(SHADER_BUFFER
*buffer
, SHADER_OPCODE_ARG
*arg
) {
844 return shader_glsl_append_dst_ext(buffer
, arg
, arg
->dst
);
847 /** Process GLSL instruction modifiers */
848 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG
* arg
) {
850 DWORD mask
= arg
->dst
& WINED3DSP_DSTMOD_MASK
;
852 if (arg
->opcode
->dst_token
&& mask
!= 0) {
853 glsl_dst_param_t dst_param
;
855 shader_glsl_add_dst_param(arg
, arg
->dst
, 0, &dst_param
);
857 if (mask
& WINED3DSPDM_SATURATE
) {
858 /* _SAT means to clamp the value of the register to between 0 and 1 */
859 shader_addline(arg
->buffer
, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param
.reg_name
,
860 dst_param
.mask_str
, dst_param
.reg_name
, dst_param
.mask_str
);
862 if (mask
& WINED3DSPDM_MSAMPCENTROID
) {
863 FIXME("_centroid modifier not handled\n");
865 if (mask
& WINED3DSPDM_PARTIALPRECISION
) {
866 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
871 static inline const char* shader_get_comp_op(
872 const DWORD opcode
) {
874 DWORD op
= (opcode
& INST_CONTROLS_MASK
) >> INST_CONTROLS_SHIFT
;
876 case COMPARISON_GT
: return ">";
877 case COMPARISON_EQ
: return "==";
878 case COMPARISON_GE
: return ">=";
879 case COMPARISON_LT
: return "<";
880 case COMPARISON_NE
: return "!=";
881 case COMPARISON_LE
: return "<=";
883 FIXME("Unrecognized comparison value: %u\n", op
);
888 static void shader_glsl_get_sample_function(DWORD sampler_type
, BOOL projected
, glsl_sample_function_t
*sample_function
) {
889 /* Note that there's no such thing as a projected cube texture. */
890 switch(sampler_type
) {
892 sample_function
->name
= projected
? "texture1DProj" : "texture1D";
893 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
;
896 sample_function
->name
= projected
? "texture2DProj" : "texture2D";
897 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
;
899 case WINED3DSTT_CUBE
:
900 sample_function
->name
= "textureCube";
901 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
903 case WINED3DSTT_VOLUME
:
904 sample_function
->name
= projected
? "texture3DProj" : "texture3D";
905 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
908 sample_function
->name
= "";
909 FIXME("Unrecognized sampler type: %#x;\n", sampler_type
);
915 /*****************************************************************************
917 * Begin processing individual instruction opcodes
919 ****************************************************************************/
921 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
922 void shader_glsl_arith(SHADER_OPCODE_ARG
* arg
) {
923 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
924 SHADER_BUFFER
* buffer
= arg
->buffer
;
925 glsl_src_param_t src0_param
;
926 glsl_src_param_t src1_param
;
930 /* Determine the GLSL operator to use based on the opcode */
931 switch (curOpcode
->opcode
) {
932 case WINED3DSIO_MUL
: op
= '*'; break;
933 case WINED3DSIO_ADD
: op
= '+'; break;
934 case WINED3DSIO_SUB
: op
= '-'; break;
937 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode
->name
);
941 write_mask
= shader_glsl_append_dst(buffer
, arg
);
942 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
943 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
944 shader_addline(buffer
, "%s %c %s);\n", src0_param
.param_str
, op
, src1_param
.param_str
);
947 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
948 void shader_glsl_mov(SHADER_OPCODE_ARG
* arg
) {
949 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
950 SHADER_BUFFER
* buffer
= arg
->buffer
;
951 glsl_src_param_t src0_param
;
954 write_mask
= shader_glsl_append_dst(buffer
, arg
);
955 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
957 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
958 * shader versions WINED3DSIO_MOVA is used for this. */
959 if ((WINED3DSHADER_VERSION_MAJOR(shader
->baseShader
.hex_version
) == 1 &&
960 !shader_is_pshader_version(shader
->baseShader
.hex_version
) &&
961 shader_get_regtype(arg
->dst
) == WINED3DSPR_ADDR
) ||
962 arg
->opcode
->opcode
== WINED3DSIO_MOVA
) {
963 /* We need to *round* to the nearest int here. */
964 size_t mask_size
= shader_glsl_get_write_mask_size(write_mask
);
966 shader_addline(buffer
, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n", mask_size
, src0_param
.param_str
, mask_size
, src0_param
.param_str
);
968 shader_addline(buffer
, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param
.param_str
, src0_param
.param_str
);
971 shader_addline(buffer
, "%s);\n", src0_param
.param_str
);
975 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
976 void shader_glsl_dot(SHADER_OPCODE_ARG
* arg
) {
977 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
978 SHADER_BUFFER
* buffer
= arg
->buffer
;
979 glsl_src_param_t src0_param
;
980 glsl_src_param_t src1_param
;
981 DWORD dst_write_mask
, src_write_mask
;
984 dst_write_mask
= shader_glsl_append_dst(buffer
, arg
);
985 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
987 /* dp3 works on vec3, dp4 on vec4 */
988 if (curOpcode
->opcode
== WINED3DSIO_DP4
) {
989 src_write_mask
= WINED3DSP_WRITEMASK_ALL
;
991 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
994 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_write_mask
, &src0_param
);
995 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], src_write_mask
, &src1_param
);
998 shader_addline(buffer
, "vec%d(dot(%s, %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
1000 shader_addline(buffer
, "dot(%s, %s));\n", src0_param
.param_str
, src1_param
.param_str
);
1004 /* Note that this instruction has some restrictions. The destination write mask
1005 * can't contain the w component, and the source swizzles have to be .xyzw */
1006 void shader_glsl_cross(SHADER_OPCODE_ARG
*arg
) {
1007 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1008 glsl_src_param_t src0_param
;
1009 glsl_src_param_t src1_param
;
1012 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1013 shader_glsl_append_dst(arg
->buffer
, arg
);
1014 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
1015 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], src_mask
, &src1_param
);
1016 shader_addline(arg
->buffer
, "cross(%s, %s).%s);\n", src0_param
.param_str
, src1_param
.param_str
, dst_mask
);
1019 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1020 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1021 * GLSL uses the value as-is. */
1022 void shader_glsl_pow(SHADER_OPCODE_ARG
*arg
) {
1023 SHADER_BUFFER
*buffer
= arg
->buffer
;
1024 glsl_src_param_t src0_param
;
1025 glsl_src_param_t src1_param
;
1026 DWORD dst_write_mask
;
1029 dst_write_mask
= shader_glsl_append_dst(buffer
, arg
);
1030 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
1032 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1033 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
1036 shader_addline(buffer
, "vec%d(pow(abs(%s), %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
1038 shader_addline(buffer
, "pow(abs(%s), %s));\n", src0_param
.param_str
, src1_param
.param_str
);
1042 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1043 void shader_glsl_map2gl(SHADER_OPCODE_ARG
* arg
) {
1044 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
1045 SHADER_BUFFER
* buffer
= arg
->buffer
;
1046 glsl_src_param_t src_param
;
1047 const char *instruction
;
1048 char arguments
[256];
1052 /* Determine the GLSL function to use based on the opcode */
1053 /* TODO: Possibly make this a table for faster lookups */
1054 switch (curOpcode
->opcode
) {
1055 case WINED3DSIO_MIN
: instruction
= "min"; break;
1056 case WINED3DSIO_MAX
: instruction
= "max"; break;
1057 case WINED3DSIO_ABS
: instruction
= "abs"; break;
1058 case WINED3DSIO_FRC
: instruction
= "fract"; break;
1059 case WINED3DSIO_NRM
: instruction
= "normalize"; break;
1060 case WINED3DSIO_LOGP
:
1061 case WINED3DSIO_LOG
: instruction
= "log2"; break;
1062 case WINED3DSIO_EXP
: instruction
= "exp2"; break;
1063 case WINED3DSIO_SGN
: instruction
= "sign"; break;
1064 case WINED3DSIO_DSX
: instruction
= "dFdx"; break;
1065 case WINED3DSIO_DSY
: instruction
= "dFdy"; break;
1066 default: instruction
= "";
1067 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode
->name
);
1071 write_mask
= shader_glsl_append_dst(buffer
, arg
);
1073 arguments
[0] = '\0';
1074 if (curOpcode
->num_params
> 0) {
1075 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src_param
);
1076 strcat(arguments
, src_param
.param_str
);
1077 for (i
= 2; i
< curOpcode
->num_params
; ++i
) {
1078 strcat(arguments
, ", ");
1079 shader_glsl_add_src_param(arg
, arg
->src
[i
-1], arg
->src_addr
[i
-1], write_mask
, &src_param
);
1080 strcat(arguments
, src_param
.param_str
);
1084 shader_addline(buffer
, "%s(%s));\n", instruction
, arguments
);
1087 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1088 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1089 * dst.x = 2^(floor(src))
1090 * dst.y = src - floor(src)
1091 * dst.z = 2^src (partial precision is allowed, but optional)
1093 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1094 * dst = 2^src; (partial precision is allowed, but optional)
1096 void shader_glsl_expp(SHADER_OPCODE_ARG
* arg
) {
1097 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)arg
->shader
;
1098 glsl_src_param_t src_param
;
1100 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
1102 if (shader
->baseShader
.hex_version
< WINED3DPS_VERSION(2,0)) {
1105 shader_addline(arg
->buffer
, "tmp0.x = exp2(floor(%s));\n", src_param
.param_str
);
1106 shader_addline(arg
->buffer
, "tmp0.y = %s - floor(%s);\n", src_param
.param_str
, src_param
.param_str
);
1107 shader_addline(arg
->buffer
, "tmp0.z = exp2(%s);\n", src_param
.param_str
);
1108 shader_addline(arg
->buffer
, "tmp0.w = 1.0;\n");
1110 shader_glsl_append_dst(arg
->buffer
, arg
);
1111 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1112 shader_addline(arg
->buffer
, "tmp0%s);\n", dst_mask
);
1117 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1118 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1120 if (mask_size
> 1) {
1121 shader_addline(arg
->buffer
, "vec%d(exp2(%s)));\n", mask_size
, src_param
.param_str
);
1123 shader_addline(arg
->buffer
, "exp2(%s));\n", src_param
.param_str
);
1128 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1129 void shader_glsl_rcp(SHADER_OPCODE_ARG
* arg
) {
1130 glsl_src_param_t src_param
;
1134 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1135 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1136 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
1138 if (mask_size
> 1) {
1139 shader_addline(arg
->buffer
, "vec%d(1.0 / %s));\n", mask_size
, src_param
.param_str
);
1141 shader_addline(arg
->buffer
, "1.0 / %s);\n", src_param
.param_str
);
1145 void shader_glsl_rsq(SHADER_OPCODE_ARG
* arg
) {
1146 SHADER_BUFFER
* buffer
= arg
->buffer
;
1147 glsl_src_param_t src_param
;
1151 write_mask
= shader_glsl_append_dst(buffer
, arg
);
1152 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1154 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
1156 if (mask_size
> 1) {
1157 shader_addline(buffer
, "vec%d(inversesqrt(%s)));\n", mask_size
, src_param
.param_str
);
1159 shader_addline(buffer
, "inversesqrt(%s));\n", src_param
.param_str
);
1163 /** Process signed comparison opcodes in GLSL. */
1164 void shader_glsl_compare(SHADER_OPCODE_ARG
* arg
) {
1165 glsl_src_param_t src0_param
;
1166 glsl_src_param_t src1_param
;
1170 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1171 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1172 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1173 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1175 if (mask_size
> 1) {
1176 const char *compare
;
1178 switch(arg
->opcode
->opcode
) {
1179 case WINED3DSIO_SLT
: compare
= "lessThan"; break;
1180 case WINED3DSIO_SGE
: compare
= "greaterThanEqual"; break;
1181 default: compare
= "";
1182 FIXME("Can't handle opcode %s\n", arg
->opcode
->name
);
1185 shader_addline(arg
->buffer
, "vec%d(%s(%s, %s)));\n", mask_size
, compare
,
1186 src0_param
.param_str
, src1_param
.param_str
);
1188 const char *compare
;
1190 switch(arg
->opcode
->opcode
) {
1191 case WINED3DSIO_SLT
: compare
= "<"; break;
1192 case WINED3DSIO_SGE
: compare
= ">="; break;
1193 default: compare
= "";
1194 FIXME("Can't handle opcode %s\n", arg
->opcode
->name
);
1197 shader_addline(arg
->buffer
, "(%s %s %s) ? 1.0 : 0.0);\n",
1198 src0_param
.param_str
, compare
, src1_param
.param_str
);
1202 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1203 void shader_glsl_cmp(SHADER_OPCODE_ARG
* arg
) {
1204 glsl_src_param_t src0_param
;
1205 glsl_src_param_t src1_param
;
1206 glsl_src_param_t src2_param
;
1207 DWORD write_mask
, cmp_channel
= 0;
1210 /* Cycle through all source0 channels */
1211 for (i
=0; i
<4; i
++) {
1213 /* Find the destination channels which use the current source0 channel */
1214 for (j
=0; j
<4; j
++) {
1215 if ( ((arg
->src
[0] >> (WINED3DSP_SWIZZLE_SHIFT
+ 2*j
)) & 0x3) == i
) {
1216 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
1217 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
1220 write_mask
= shader_glsl_append_dst_ext(arg
->buffer
, arg
, arg
->dst
& (~WINED3DSP_SWIZZLE_MASK
| write_mask
));
1221 if (!write_mask
) continue;
1223 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], cmp_channel
, &src0_param
);
1224 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1225 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1227 shader_addline(arg
->buffer
, "%s >= 0.0 ? %s : %s);\n",
1228 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1232 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
1233 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1234 * the compare is done per component of src0. */
1235 void shader_glsl_cnd(SHADER_OPCODE_ARG
* arg
) {
1236 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
1237 glsl_src_param_t src0_param
;
1238 glsl_src_param_t src1_param
;
1239 glsl_src_param_t src2_param
;
1240 DWORD write_mask
, cmp_channel
= 0;
1243 if (shader
->baseShader
.hex_version
< WINED3DPS_VERSION(1, 4)) {
1244 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1245 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1246 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1247 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1248 shader_addline(arg
->buffer
, "%s > 0.5 ? %s : %s);\n",
1249 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1252 /* Cycle through all source0 channels */
1253 for (i
=0; i
<4; i
++) {
1255 /* Find the destination channels which use the current source0 channel */
1256 for (j
=0; j
<4; j
++) {
1257 if ( ((arg
->src
[0] >> (WINED3DSP_SWIZZLE_SHIFT
+ 2*j
)) & 0x3) == i
) {
1258 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
1259 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
1262 write_mask
= shader_glsl_append_dst_ext(arg
->buffer
, arg
, arg
->dst
& (~WINED3DSP_SWIZZLE_MASK
| write_mask
));
1263 if (!write_mask
) continue;
1265 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], cmp_channel
, &src0_param
);
1266 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1267 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1269 shader_addline(arg
->buffer
, "%s > 0.5 ? %s : %s);\n",
1270 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1274 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1275 void shader_glsl_mad(SHADER_OPCODE_ARG
* arg
) {
1276 glsl_src_param_t src0_param
;
1277 glsl_src_param_t src1_param
;
1278 glsl_src_param_t src2_param
;
1281 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1282 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1283 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1284 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1285 shader_addline(arg
->buffer
, "(%s * %s) + %s);\n",
1286 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1289 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1290 Vertex shaders to GLSL codes */
1291 void shader_glsl_mnxn(SHADER_OPCODE_ARG
* arg
) {
1293 int nComponents
= 0;
1294 SHADER_OPCODE_ARG tmpArg
;
1296 memset(&tmpArg
, 0, sizeof(SHADER_OPCODE_ARG
));
1298 /* Set constants for the temporary argument */
1299 tmpArg
.shader
= arg
->shader
;
1300 tmpArg
.buffer
= arg
->buffer
;
1301 tmpArg
.src
[0] = arg
->src
[0];
1302 tmpArg
.src_addr
[0] = arg
->src_addr
[0];
1303 tmpArg
.src_addr
[1] = arg
->src_addr
[1];
1304 tmpArg
.reg_maps
= arg
->reg_maps
;
1306 switch(arg
->opcode
->opcode
) {
1307 case WINED3DSIO_M4x4
:
1309 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP4
);
1311 case WINED3DSIO_M4x3
:
1313 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP4
);
1315 case WINED3DSIO_M3x4
:
1317 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1319 case WINED3DSIO_M3x3
:
1321 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1323 case WINED3DSIO_M3x2
:
1325 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1331 for (i
= 0; i
< nComponents
; i
++) {
1332 tmpArg
.dst
= ((arg
->dst
) & ~WINED3DSP_WRITEMASK_ALL
)|(WINED3DSP_WRITEMASK_0
<<i
);
1333 tmpArg
.src
[1] = arg
->src
[1]+i
;
1334 shader_glsl_dot(&tmpArg
);
1339 The LRP instruction performs a component-wise linear interpolation
1340 between the second and third operands using the first operand as the
1341 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
1342 This is equivalent to mix(src2, src1, src0);
1344 void shader_glsl_lrp(SHADER_OPCODE_ARG
* arg
) {
1345 glsl_src_param_t src0_param
;
1346 glsl_src_param_t src1_param
;
1347 glsl_src_param_t src2_param
;
1350 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1352 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1353 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1354 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1356 shader_addline(arg
->buffer
, "mix(%s, %s, %s));\n",
1357 src2_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
);
1360 /** Process the WINED3DSIO_LIT instruction in GLSL:
1361 * dst.x = dst.w = 1.0
1362 * dst.y = (src0.x > 0) ? src0.x
1363 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1364 * where src.w is clamped at +- 128
1366 void shader_glsl_lit(SHADER_OPCODE_ARG
* arg
) {
1367 glsl_src_param_t src0_param
;
1368 glsl_src_param_t src1_param
;
1369 glsl_src_param_t src3_param
;
1372 shader_glsl_append_dst(arg
->buffer
, arg
);
1373 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1375 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1376 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_1
, &src1_param
);
1377 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &src3_param
);
1379 shader_addline(arg
->buffer
, "vec4(1.0, (%s > 0.0 ? %s : 0.0), (%s > 0.0 ? ((%s > 0.0) ? pow(%s, clamp(%s, -128.0, 128.0)) : 0.0) : 0.0), 1.0)%s);\n",
1380 src0_param
.param_str
, src0_param
.param_str
, src0_param
.param_str
, src1_param
.param_str
, src1_param
.param_str
, src3_param
.param_str
, dst_mask
);
1383 /** Process the WINED3DSIO_DST instruction in GLSL:
1385 * dst.y = src0.x * src0.y
1389 void shader_glsl_dst(SHADER_OPCODE_ARG
* arg
) {
1390 glsl_src_param_t src0y_param
;
1391 glsl_src_param_t src0z_param
;
1392 glsl_src_param_t src1y_param
;
1393 glsl_src_param_t src1w_param
;
1396 shader_glsl_append_dst(arg
->buffer
, arg
);
1397 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1399 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_1
, &src0y_param
);
1400 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_2
, &src0z_param
);
1401 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_1
, &src1y_param
);
1402 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_3
, &src1w_param
);
1404 shader_addline(arg
->buffer
, "vec4(1.0, %s * %s, %s, %s))%s;\n",
1405 src0y_param
.param_str
, src1y_param
.param_str
, src0z_param
.param_str
, src1w_param
.param_str
, dst_mask
);
1408 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1409 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1410 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1412 * dst.x = cos(src0.?)
1413 * dst.y = sin(src0.?)
1417 void shader_glsl_sincos(SHADER_OPCODE_ARG
* arg
) {
1418 glsl_src_param_t src0_param
;
1421 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1422 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1424 switch (write_mask
) {
1425 case WINED3DSP_WRITEMASK_0
:
1426 shader_addline(arg
->buffer
, "cos(%s));\n", src0_param
.param_str
);
1429 case WINED3DSP_WRITEMASK_1
:
1430 shader_addline(arg
->buffer
, "sin(%s));\n", src0_param
.param_str
);
1433 case (WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
):
1434 shader_addline(arg
->buffer
, "vec2(cos(%s), sin(%s)));\n", src0_param
.param_str
, src0_param
.param_str
);
1438 ERR("Write mask should be .x, .y or .xy\n");
1443 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1444 * Start a for() loop where src1.y is the initial value of aL,
1445 * increment aL by src1.z for a total of src1.x iterations.
1446 * Need to use a temporary variable for this operation.
1448 /* FIXME: I don't think nested loops will work correctly this way. */
1449 void shader_glsl_loop(SHADER_OPCODE_ARG
* arg
) {
1450 glsl_src_param_t src1_param
;
1452 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_ALL
, &src1_param
);
1454 shader_addline(arg
->buffer
, "for (tmpInt = 0, aL = %s.y; tmpInt < %s.x; tmpInt++, aL += %s.z) {\n",
1455 src1_param
.reg_name
, src1_param
.reg_name
, src1_param
.reg_name
);
1458 void shader_glsl_end(SHADER_OPCODE_ARG
* arg
) {
1459 shader_addline(arg
->buffer
, "}\n");
1462 void shader_glsl_rep(SHADER_OPCODE_ARG
* arg
) {
1463 glsl_src_param_t src0_param
;
1465 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1466 shader_addline(arg
->buffer
, "for (tmpInt = 0; tmpInt < %s; tmpInt++) {\n", src0_param
.param_str
);
1469 void shader_glsl_if(SHADER_OPCODE_ARG
* arg
) {
1470 glsl_src_param_t src0_param
;
1472 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1473 shader_addline(arg
->buffer
, "if (%s) {\n", src0_param
.param_str
);
1476 void shader_glsl_ifc(SHADER_OPCODE_ARG
* arg
) {
1477 glsl_src_param_t src0_param
;
1478 glsl_src_param_t src1_param
;
1480 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1481 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
1483 shader_addline(arg
->buffer
, "if (%s %s %s) {\n",
1484 src0_param
.param_str
, shader_get_comp_op(arg
->opcode_token
), src1_param
.param_str
);
1487 void shader_glsl_else(SHADER_OPCODE_ARG
* arg
) {
1488 shader_addline(arg
->buffer
, "} else {\n");
1491 void shader_glsl_break(SHADER_OPCODE_ARG
* arg
) {
1492 shader_addline(arg
->buffer
, "break;\n");
1495 /* FIXME: According to MSDN the compare is done per component. */
1496 void shader_glsl_breakc(SHADER_OPCODE_ARG
* arg
) {
1497 glsl_src_param_t src0_param
;
1498 glsl_src_param_t src1_param
;
1500 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1501 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
1503 shader_addline(arg
->buffer
, "if (%s %s %s) break;\n",
1504 src0_param
.param_str
, shader_get_comp_op(arg
->opcode_token
), src1_param
.param_str
);
1507 void shader_glsl_label(SHADER_OPCODE_ARG
* arg
) {
1509 DWORD snum
= (arg
->src
[0]) & WINED3DSP_REGNUM_MASK
;
1510 shader_addline(arg
->buffer
, "}\n");
1511 shader_addline(arg
->buffer
, "void subroutine%lu () {\n", snum
);
1514 void shader_glsl_call(SHADER_OPCODE_ARG
* arg
) {
1515 DWORD snum
= (arg
->src
[0]) & WINED3DSP_REGNUM_MASK
;
1516 shader_addline(arg
->buffer
, "subroutine%lu();\n", snum
);
1519 void shader_glsl_callnz(SHADER_OPCODE_ARG
* arg
) {
1520 glsl_src_param_t src1_param
;
1522 DWORD snum
= (arg
->src
[0]) & WINED3DSP_REGNUM_MASK
;
1523 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
1524 shader_addline(arg
->buffer
, "if (%s) subroutine%lu();\n", src1_param
.param_str
, snum
);
1527 /*********************************************
1528 * Pixel Shader Specific Code begins here
1529 ********************************************/
1530 void pshader_glsl_tex(SHADER_OPCODE_ARG
* arg
) {
1531 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1532 DWORD hex_version
= This
->baseShader
.hex_version
;
1533 char dst_swizzle
[6];
1534 glsl_sample_function_t sample_function
;
1540 /* All versions have a destination register */
1541 shader_glsl_append_dst(arg
->buffer
, arg
);
1543 /* 1.0-1.4: Use destination register as sampler source.
1544 * 2.0+: Use provided sampler source. */
1545 if (hex_version
< WINED3DPS_VERSION(1,4)) {
1546 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1549 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1550 flags
= deviceImpl
->stateBlock
->textureState
[sampler_idx
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
1552 if (flags
& WINED3DTTFF_PROJECTED
) {
1554 switch (flags
& ~WINED3DTTFF_PROJECTED
) {
1555 case WINED3DTTFF_COUNT1
: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
1556 case WINED3DTTFF_COUNT2
: mask
= WINED3DSP_WRITEMASK_1
; break;
1557 case WINED3DTTFF_COUNT3
: mask
= WINED3DSP_WRITEMASK_2
; break;
1558 case WINED3DTTFF_COUNT4
:
1559 case WINED3DTTFF_DISABLE
: mask
= WINED3DSP_WRITEMASK_3
; break;
1564 } else if (hex_version
< WINED3DPS_VERSION(2,0)) {
1565 DWORD src_mod
= arg
->src
[0] & WINED3DSP_SRCMOD_MASK
;
1566 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1568 if (src_mod
== WINED3DSPSM_DZ
) {
1570 mask
= WINED3DSP_WRITEMASK_2
;
1571 } else if (src_mod
== WINED3DSPSM_DW
) {
1573 mask
= WINED3DSP_WRITEMASK_3
;
1578 sampler_idx
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
1579 if(arg
->opcode_token
& WINED3DSI_TEXLD_PROJECT
) {
1580 /* ps 2.0 texldp instruction always divides by the fourth component. */
1582 mask
= WINED3DSP_WRITEMASK_3
;
1588 sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
1589 shader_glsl_get_sample_function(sampler_type
, projected
, &sample_function
);
1590 mask
|= sample_function
.coord_mask
;
1592 if (hex_version
< WINED3DPS_VERSION(2,0)) {
1593 shader_glsl_get_write_mask(arg
->dst
, dst_swizzle
);
1595 shader_glsl_get_swizzle(arg
->src
[1], FALSE
, arg
->dst
, dst_swizzle
);
1598 /* 1.0-1.3: Use destination register as coordinate source.
1599 1.4+: Use provided coordinate source register. */
1600 if (hex_version
< WINED3DPS_VERSION(1,4)) {
1602 shader_glsl_get_write_mask(mask
, coord_mask
);
1603 shader_addline(arg
->buffer
, "%s(Psampler%u, T%u%s)%s);\n",
1604 sample_function
.name
, sampler_idx
, sampler_idx
, coord_mask
, dst_swizzle
);
1606 glsl_src_param_t coord_param
;
1607 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], mask
, &coord_param
);
1608 shader_addline(arg
->buffer
, "%s(Psampler%u, %s)%s);\n",
1609 sample_function
.name
, sampler_idx
, coord_param
.param_str
, dst_swizzle
);
1613 void shader_glsl_texldl(SHADER_OPCODE_ARG
* arg
) {
1614 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*)arg
->shader
;
1615 glsl_sample_function_t sample_function
;
1616 glsl_src_param_t coord_param
, lod_param
;
1617 char dst_swizzle
[6];
1621 shader_glsl_append_dst(arg
->buffer
, arg
);
1622 shader_glsl_get_swizzle(arg
->src
[1], FALSE
, arg
->dst
, dst_swizzle
);
1624 sampler_idx
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
1625 sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
1626 shader_glsl_get_sample_function(sampler_type
, FALSE
, &sample_function
);
1627 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], sample_function
.coord_mask
, &coord_param
);
1629 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
1631 if (shader_is_pshader_version(This
->baseShader
.hex_version
)) {
1632 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
1633 * However, they seem to work just fine in fragment shaders as well. */
1634 WARN("Using %sLod in fragment shader.\n", sample_function
.name
);
1635 shader_addline(arg
->buffer
, "%sLod(Psampler%u, %s, %s)%s);\n",
1636 sample_function
.name
, sampler_idx
, coord_param
.param_str
, lod_param
.param_str
, dst_swizzle
);
1638 shader_addline(arg
->buffer
, "%sLod(Vsampler%u, %s, %s)%s);\n",
1639 sample_function
.name
, sampler_idx
, coord_param
.param_str
, lod_param
.param_str
, dst_swizzle
);
1643 void pshader_glsl_texcoord(SHADER_OPCODE_ARG
* arg
) {
1645 /* FIXME: Make this work for more than just 2D textures */
1647 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1648 SHADER_BUFFER
* buffer
= arg
->buffer
;
1649 DWORD hex_version
= This
->baseShader
.hex_version
;
1653 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1654 shader_glsl_get_write_mask(write_mask
, dst_mask
);
1656 if (hex_version
!= WINED3DPS_VERSION(1,4)) {
1657 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1658 shader_addline(buffer
, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n", reg
, dst_mask
);
1660 DWORD reg
= arg
->src
[0] & WINED3DSP_REGNUM_MASK
;
1661 DWORD src_mod
= arg
->src
[0] & WINED3DSP_SRCMOD_MASK
;
1662 char dst_swizzle
[6];
1664 shader_glsl_get_swizzle(arg
->src
[0], FALSE
, write_mask
, dst_swizzle
);
1666 if (src_mod
== WINED3DSPSM_DZ
) {
1667 glsl_src_param_t div_param
;
1668 size_t mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1669 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_2
, &div_param
);
1671 if (mask_size
> 1) {
1672 shader_addline(buffer
, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
1674 shader_addline(buffer
, "gl_TexCoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
1676 } else if (src_mod
== WINED3DSPSM_DW
) {
1677 glsl_src_param_t div_param
;
1678 size_t mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1679 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &div_param
);
1681 if (mask_size
> 1) {
1682 shader_addline(buffer
, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
1684 shader_addline(buffer
, "gl_TexCoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
1687 shader_addline(buffer
, "gl_TexCoord[%u]%s);\n", reg
, dst_swizzle
);
1692 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
1693 * Take a 3-component dot product of the TexCoord[dstreg] and src,
1694 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
1695 void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG
* arg
) {
1696 glsl_src_param_t src0_param
;
1698 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1699 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1701 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
1703 shader_glsl_append_dst(arg
->buffer
, arg
);
1704 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1705 shader_addline(arg
->buffer
, "texture2D(Psampler%u, vec2(dot(gl_TexCoord[%u].xyz, %s), 0.5))%s);\n",
1706 sampler_idx
, sampler_idx
, src0_param
.param_str
, dst_mask
);
1709 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
1710 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
1711 void pshader_glsl_texdp3(SHADER_OPCODE_ARG
* arg
) {
1712 glsl_src_param_t src0_param
;
1713 DWORD dstreg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1714 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1718 dst_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1719 mask_size
= shader_glsl_get_write_mask_size(dst_mask
);
1720 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
1722 if (mask_size
> 1) {
1723 shader_addline(arg
->buffer
, "vec%d(dot(T%u.xyz, %s)));\n", mask_size
, dstreg
, src0_param
.param_str
);
1725 shader_addline(arg
->buffer
, "dot(T%u.xyz, %s));\n", dstreg
, src0_param
.param_str
);
1729 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
1730 * Calculate the depth as dst.x / dst.y */
1731 void pshader_glsl_texdepth(SHADER_OPCODE_ARG
* arg
) {
1732 glsl_dst_param_t dst_param
;
1734 shader_glsl_add_dst_param(arg
, arg
->dst
, 0, &dst_param
);
1736 shader_addline(arg
->buffer
, "gl_FragDepth = %s.x / %s.y;\n", dst_param
.reg_name
, dst_param
.reg_name
);
1739 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
1740 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
1741 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
1742 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
1744 void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG
* arg
) {
1745 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1746 DWORD dstreg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1747 glsl_src_param_t src0_param
;
1749 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
1751 shader_addline(arg
->buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg
, src0_param
.param_str
);
1752 shader_addline(arg
->buffer
, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
1755 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
1756 * Calculate the 1st of a 2-row matrix multiplication. */
1757 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG
* arg
) {
1758 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1759 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1760 SHADER_BUFFER
* buffer
= arg
->buffer
;
1761 glsl_src_param_t src0_param
;
1763 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
1764 shader_addline(buffer
, "tmp0.x = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
1767 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
1768 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
1769 void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG
* arg
) {
1771 IWineD3DPixelShaderImpl
* shader
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1772 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1773 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1774 SHADER_BUFFER
* buffer
= arg
->buffer
;
1775 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
1776 glsl_src_param_t src0_param
;
1778 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
1779 shader_addline(buffer
, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state
->current_row
, reg
, src0_param
.param_str
);
1780 current_state
->texcoord_w
[current_state
->current_row
++] = reg
;
1783 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG
* arg
) {
1784 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1785 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1786 SHADER_BUFFER
* buffer
= arg
->buffer
;
1787 glsl_src_param_t src0_param
;
1790 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
1791 shader_addline(buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
1793 shader_glsl_append_dst(buffer
, arg
);
1794 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1796 /* Sample the texture using the calculated coordinates */
1797 shader_addline(buffer
, "texture2D(Psampler%u, tmp0.xy)%s);\n", reg
, dst_mask
);
1800 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
1801 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
1802 void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG
* arg
) {
1803 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1804 glsl_src_param_t src0_param
;
1806 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1807 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1808 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
1809 DWORD sampler_type
= arg
->reg_maps
->samplers
[reg
] & WINED3DSP_TEXTURETYPE_MASK
;
1810 glsl_sample_function_t sample_function
;
1812 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
1813 shader_addline(arg
->buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
1815 shader_glsl_append_dst(arg
->buffer
, arg
);
1816 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1817 shader_glsl_get_sample_function(sampler_type
, FALSE
, &sample_function
);
1819 /* Sample the texture using the calculated coordinates */
1820 shader_addline(arg
->buffer
, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function
.name
, reg
, dst_mask
);
1822 current_state
->current_row
= 0;
1825 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
1826 * Perform the 3rd row of a 3x3 matrix multiply */
1827 void pshader_glsl_texm3x3(SHADER_OPCODE_ARG
* arg
) {
1828 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1829 glsl_src_param_t src0_param
;
1831 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1832 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1833 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
1835 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
1837 shader_glsl_append_dst(arg
->buffer
, arg
);
1838 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1839 shader_addline(arg
->buffer
, "vec4(tmp.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg
, src0_param
.param_str
, dst_mask
);
1841 current_state
->current_row
= 0;
1844 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
1845 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1846 void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG
* arg
) {
1848 IWineD3DPixelShaderImpl
* shader
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1849 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1850 glsl_src_param_t src0_param
;
1851 glsl_src_param_t src1_param
;
1853 SHADER_BUFFER
* buffer
= arg
->buffer
;
1854 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
1855 DWORD stype
= arg
->reg_maps
->samplers
[reg
] & WINED3DSP_TEXTURETYPE_MASK
;
1856 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1857 glsl_sample_function_t sample_function
;
1859 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
1860 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], src_mask
, &src1_param
);
1862 /* Perform the last matrix multiply operation */
1863 shader_addline(buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
1865 /* Calculate reflection vector, 2*(tmp0.src1)*tmp0-src1
1866 * This is equivalent to reflect(-src1, tmp0); */
1867 shader_addline(buffer
, "tmp0.xyz = reflect(-(%s), tmp0.xyz);\n", src1_param
.param_str
);
1869 shader_glsl_append_dst(buffer
, arg
);
1870 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1871 shader_glsl_get_sample_function(stype
, FALSE
, &sample_function
);
1873 /* Sample the texture */
1874 shader_addline(buffer
, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function
.name
, reg
, dst_mask
);
1876 current_state
->current_row
= 0;
1879 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
1880 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1881 void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG
* arg
) {
1883 IWineD3DPixelShaderImpl
* shader
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1884 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1885 SHADER_BUFFER
* buffer
= arg
->buffer
;
1886 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
1887 glsl_src_param_t src0_param
;
1889 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1890 DWORD sampler_type
= arg
->reg_maps
->samplers
[reg
] & WINED3DSP_TEXTURETYPE_MASK
;
1891 glsl_sample_function_t sample_function
;
1893 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
1895 /* Perform the last matrix multiply operation */
1896 shader_addline(buffer
, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg
, src0_param
.param_str
);
1898 /* Construct the eye-ray vector from w coordinates */
1899 shader_addline(buffer
, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
1900 current_state
->texcoord_w
[0], current_state
->texcoord_w
[1], reg
);
1902 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(tmp0.tmp1)*tmp0-tmp1
1903 * This is equivalent to reflect(-tmp1, tmp0); */
1904 shader_addline(buffer
, "tmp0.xyz = reflect(-tmp1.xyz, tmp0.xyz);\n");
1906 shader_glsl_append_dst(buffer
, arg
);
1907 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1908 shader_glsl_get_sample_function(sampler_type
, FALSE
, &sample_function
);
1910 /* Sample the texture using the calculated coordinates */
1911 shader_addline(buffer
, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function
.name
, reg
, dst_mask
);
1913 current_state
->current_row
= 0;
1916 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
1917 * Apply a fake bump map transform.
1918 * texbem is pshader <= 1.3 only, this saves a few version checks
1920 void pshader_glsl_texbem(SHADER_OPCODE_ARG
* arg
) {
1921 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1922 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1923 char dst_swizzle
[6];
1924 glsl_sample_function_t sample_function
;
1925 glsl_src_param_t coord_param
;
1932 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1933 flags
= deviceImpl
->stateBlock
->textureState
[sampler_idx
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
1935 sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
1936 shader_glsl_get_sample_function(sampler_type
, FALSE
, &sample_function
);
1937 mask
= sample_function
.coord_mask
;
1939 shader_glsl_get_write_mask(arg
->dst
, dst_swizzle
);
1941 shader_glsl_get_write_mask(mask
, coord_mask
);
1943 /* with projective textures, texbem only divides the static texture coord, not the displacement,
1944 * so we can't let the GL handle this.
1946 if (flags
& WINED3DTTFF_PROJECTED
) {
1948 char coord_div_mask
[3];
1949 switch (flags
& ~WINED3DTTFF_PROJECTED
) {
1950 case WINED3DTTFF_COUNT1
: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
1951 case WINED3DTTFF_COUNT2
: div_mask
= WINED3DSP_WRITEMASK_1
; break;
1952 case WINED3DTTFF_COUNT3
: div_mask
= WINED3DSP_WRITEMASK_2
; break;
1953 case WINED3DTTFF_COUNT4
:
1954 case WINED3DTTFF_DISABLE
: div_mask
= WINED3DSP_WRITEMASK_3
; break;
1956 shader_glsl_get_write_mask(div_mask
, coord_div_mask
);
1957 shader_addline(arg
->buffer
, "T%u%s /= T%u%s;\n", sampler_idx
, coord_mask
, sampler_idx
, coord_div_mask
);
1960 shader_glsl_append_dst(arg
->buffer
, arg
);
1961 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
|WINED3DSP_WRITEMASK_1
, &coord_param
);
1962 shader_addline(arg
->buffer
, "%s(Psampler%u, T%u%s + vec4(bumpenvmat * %s, 0.0, 0.0)%s )%s);\n",
1963 sample_function
.name
, sampler_idx
, sampler_idx
, coord_mask
, coord_param
.param_str
, coord_mask
, dst_swizzle
);
1966 void pshader_glsl_bem(SHADER_OPCODE_ARG
* arg
) {
1967 glsl_src_param_t src0_param
, src1_param
;
1969 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
|WINED3DSP_WRITEMASK_1
, &src0_param
);
1970 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
|WINED3DSP_WRITEMASK_1
, &src1_param
);
1972 shader_glsl_append_dst(arg
->buffer
, arg
);
1973 shader_addline(arg
->buffer
, "%s + bumpenvmat * %s);\n",
1974 src0_param
.param_str
, src1_param
.param_str
);
1977 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
1978 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
1979 void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG
* arg
) {
1980 glsl_src_param_t src0_param
;
1981 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1984 shader_glsl_append_dst(arg
->buffer
, arg
);
1985 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1986 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
1988 shader_addline(arg
->buffer
, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx
, src0_param
.reg_name
, dst_mask
);
1991 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
1992 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
1993 void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG
* arg
) {
1994 glsl_src_param_t src0_param
;
1995 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1998 shader_glsl_append_dst(arg
->buffer
, arg
);
1999 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2000 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
2002 shader_addline(arg
->buffer
, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx
, src0_param
.reg_name
, dst_mask
);
2005 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
2006 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
2007 void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG
* arg
) {
2008 glsl_src_param_t src0_param
;
2010 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2011 DWORD sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
2012 glsl_sample_function_t sample_function
;
2014 shader_glsl_append_dst(arg
->buffer
, arg
);
2015 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2016 shader_glsl_get_sample_function(sampler_type
, FALSE
, &sample_function
);
2017 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], sample_function
.coord_mask
, &src0_param
);
2019 shader_addline(arg
->buffer
, "%s(Psampler%u, %s)%s);\n", sample_function
.name
, sampler_idx
, src0_param
.param_str
, dst_mask
);
2022 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
2023 * If any of the first 3 components are < 0, discard this pixel */
2024 void pshader_glsl_texkill(SHADER_OPCODE_ARG
* arg
) {
2025 glsl_dst_param_t dst_param
;
2027 shader_glsl_add_dst_param(arg
, arg
->dst
, 0, &dst_param
);
2028 shader_addline(arg
->buffer
, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param
.reg_name
);
2031 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
2032 * dst = dot2(src0, src1) + src2 */
2033 void pshader_glsl_dp2add(SHADER_OPCODE_ARG
* arg
) {
2034 glsl_src_param_t src0_param
;
2035 glsl_src_param_t src1_param
;
2036 glsl_src_param_t src2_param
;
2040 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
2041 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2043 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
2044 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
2045 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], WINED3DSP_WRITEMASK_0
, &src2_param
);
2047 shader_addline(arg
->buffer
, "dot(%s, %s) + %s);\n", src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2050 void pshader_glsl_input_pack(
2051 SHADER_BUFFER
* buffer
,
2052 semantic
* semantics_in
) {
2056 for (i
= 0; i
< MAX_REG_INPUT
; i
++) {
2058 DWORD usage_token
= semantics_in
[i
].usage
;
2059 DWORD register_token
= semantics_in
[i
].reg
;
2060 DWORD usage
, usage_idx
;
2064 if (!usage_token
) continue;
2065 usage
= (usage_token
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
2066 usage_idx
= (usage_token
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
2067 shader_glsl_get_write_mask(register_token
, reg_mask
);
2071 case WINED3DDECLUSAGE_COLOR
:
2073 shader_addline(buffer
, "IN%u%s = vec4(gl_Color)%s;\n",
2074 i
, reg_mask
, reg_mask
);
2075 else if (usage_idx
== 1)
2076 shader_addline(buffer
, "IN%u%s = vec4(gl_SecondaryColor)%s;\n",
2077 i
, reg_mask
, reg_mask
);
2079 shader_addline(buffer
, "IN%u%s = vec4(unsupported_color_input)%s;\n",
2080 i
, reg_mask
, reg_mask
);
2083 case WINED3DDECLUSAGE_TEXCOORD
:
2084 shader_addline(buffer
, "IN%u%s = vec4(gl_TexCoord[%u])%s;\n",
2085 i
, reg_mask
, usage_idx
, reg_mask
);
2088 case WINED3DDECLUSAGE_FOG
:
2089 shader_addline(buffer
, "IN%u%s = vec4(gl_FogFragCoord)%s;\n",
2090 i
, reg_mask
, reg_mask
);
2094 shader_addline(buffer
, "IN%u%s = vec4(unsupported_input)%s;\n",
2095 i
, reg_mask
, reg_mask
);
2100 /*********************************************
2101 * Vertex Shader Specific Code begins here
2102 ********************************************/
2104 void vshader_glsl_output_unpack(
2105 SHADER_BUFFER
* buffer
,
2106 semantic
* semantics_out
) {
2110 for (i
= 0; i
< MAX_REG_OUTPUT
; i
++) {
2112 DWORD usage_token
= semantics_out
[i
].usage
;
2113 DWORD register_token
= semantics_out
[i
].reg
;
2114 DWORD usage
, usage_idx
;
2118 if (!usage_token
) continue;
2120 usage
= (usage_token
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
2121 usage_idx
= (usage_token
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
2122 shader_glsl_get_write_mask(register_token
, reg_mask
);
2126 case WINED3DDECLUSAGE_COLOR
:
2128 shader_addline(buffer
, "gl_FrontColor%s = OUT%u%s;\n", reg_mask
, i
, reg_mask
);
2129 else if (usage_idx
== 1)
2130 shader_addline(buffer
, "gl_FrontSecondaryColor%s = OUT%u%s;\n", reg_mask
, i
, reg_mask
);
2132 shader_addline(buffer
, "unsupported_color_output%s = OUT%u%s;\n", reg_mask
, i
, reg_mask
);
2135 case WINED3DDECLUSAGE_POSITION
:
2136 shader_addline(buffer
, "gl_Position%s = OUT%u%s;\n", reg_mask
, i
, reg_mask
);
2139 case WINED3DDECLUSAGE_TEXCOORD
:
2140 shader_addline(buffer
, "gl_TexCoord[%u]%s = OUT%u%s;\n",
2141 usage_idx
, reg_mask
, i
, reg_mask
);
2144 case WINED3DDECLUSAGE_PSIZE
:
2145 shader_addline(buffer
, "gl_PointSize = OUT%u.x;\n", i
);
2148 case WINED3DDECLUSAGE_FOG
:
2149 shader_addline(buffer
, "gl_FogFragCoord = OUT%u%s;\n", i
, reg_mask
);
2153 shader_addline(buffer
, "unsupported_output%s = OUT%u%s;\n", reg_mask
, i
, reg_mask
);
2158 static void add_glsl_program_entry(IWineD3DDeviceImpl
*device
, struct glsl_shader_prog_link
*entry
) {
2159 glsl_program_key_t
*key
;
2161 key
= HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t
));
2162 key
->vshader
= entry
->vshader
;
2163 key
->pshader
= entry
->pshader
;
2165 hash_table_put(device
->glsl_program_lookup
, key
, entry
);
2168 static struct glsl_shader_prog_link
*get_glsl_program_entry(IWineD3DDeviceImpl
*device
,
2169 GLhandleARB vshader
, GLhandleARB pshader
) {
2170 glsl_program_key_t key
;
2172 key
.vshader
= vshader
;
2173 key
.pshader
= pshader
;
2175 return (struct glsl_shader_prog_link
*)hash_table_get(device
->glsl_program_lookup
, &key
);
2178 void delete_glsl_program_entry(IWineD3DDevice
*iface
, struct glsl_shader_prog_link
*entry
) {
2179 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
2180 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
2181 glsl_program_key_t
*key
;
2183 key
= HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t
));
2184 key
->vshader
= entry
->vshader
;
2185 key
->pshader
= entry
->pshader
;
2186 hash_table_remove(This
->glsl_program_lookup
, key
);
2188 GL_EXTCALL(glDeleteObjectARB(entry
->programId
));
2189 if (entry
->vshader
) list_remove(&entry
->vshader_entry
);
2190 if (entry
->pshader
) list_remove(&entry
->pshader_entry
);
2191 HeapFree(GetProcessHeap(), 0, entry
->vuniformF_locations
);
2192 HeapFree(GetProcessHeap(), 0, entry
->puniformF_locations
);
2193 HeapFree(GetProcessHeap(), 0, entry
);
2196 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
2197 * It sets the programId on the current StateBlock (because it should be called
2198 * inside of the DrawPrimitive() part of the render loop).
2200 * If a program for the given combination does not exist, create one, and store
2201 * the program in the hash table. If it creates a program, it will link the
2202 * given objects, too.
2204 static void set_glsl_shader_program(IWineD3DDevice
*iface
, BOOL use_ps
, BOOL use_vs
) {
2205 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
2206 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
2207 IWineD3DPixelShader
*pshader
= This
->stateBlock
->pixelShader
;
2208 IWineD3DVertexShader
*vshader
= This
->stateBlock
->vertexShader
;
2209 struct glsl_shader_prog_link
*entry
= NULL
;
2210 GLhandleARB programId
= 0;
2214 GLhandleARB vshader_id
= use_vs
? ((IWineD3DBaseShaderImpl
*)vshader
)->baseShader
.prgId
: 0;
2215 GLhandleARB pshader_id
= use_ps
? ((IWineD3DBaseShaderImpl
*)pshader
)->baseShader
.prgId
: 0;
2216 entry
= get_glsl_program_entry(This
, vshader_id
, pshader_id
);
2218 This
->stateBlock
->glsl_program
= entry
;
2222 /* If we get to this point, then no matching program exists, so we create one */
2223 programId
= GL_EXTCALL(glCreateProgramObjectARB());
2224 TRACE("Created new GLSL shader program %u\n", programId
);
2226 /* Create the entry */
2227 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link
));
2228 entry
->programId
= programId
;
2229 entry
->vshader
= vshader_id
;
2230 entry
->pshader
= pshader_id
;
2231 /* Add the hash table entry */
2232 add_glsl_program_entry(This
, entry
);
2234 /* Set the current program */
2235 This
->stateBlock
->glsl_program
= entry
;
2237 /* Attach GLSL vshader */
2239 int max_attribs
= 16; /* TODO: Will this always be the case? It is at the moment... */
2242 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id
, programId
);
2243 GL_EXTCALL(glAttachObjectARB(programId
, vshader_id
));
2244 checkGLcall("glAttachObjectARB");
2246 /* Bind vertex attributes to a corresponding index number to match
2247 * the same index numbers as ARB_vertex_programs (makes loading
2248 * vertex attributes simpler). With this method, we can use the
2249 * exact same code to load the attributes later for both ARB and
2252 * We have to do this here because we need to know the Program ID
2253 * in order to make the bindings work, and it has to be done prior
2254 * to linking the GLSL program. */
2255 for (i
= 0; i
< max_attribs
; ++i
) {
2256 snprintf(tmp_name
, sizeof(tmp_name
), "attrib%i", i
);
2257 GL_EXTCALL(glBindAttribLocationARB(programId
, i
, tmp_name
));
2259 checkGLcall("glBindAttribLocationARB");
2261 list_add_head(&((IWineD3DBaseShaderImpl
*)vshader
)->baseShader
.linked_programs
, &entry
->vshader_entry
);
2264 /* Attach GLSL pshader */
2266 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id
, programId
);
2267 GL_EXTCALL(glAttachObjectARB(programId
, pshader_id
));
2268 checkGLcall("glAttachObjectARB");
2270 list_add_head(&((IWineD3DBaseShaderImpl
*)pshader
)->baseShader
.linked_programs
, &entry
->pshader_entry
);
2273 /* Link the program */
2274 TRACE("Linking GLSL shader program %u\n", programId
);
2275 GL_EXTCALL(glLinkProgramARB(programId
));
2276 print_glsl_info_log(&GLINFO_LOCATION
, programId
);
2278 entry
->vuniformF_locations
= HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB
) * GL_LIMITS(vshader_constantsF
));
2279 for (i
= 0; i
< GL_LIMITS(vshader_constantsF
); ++i
) {
2280 snprintf(glsl_name
, sizeof(glsl_name
), "VC[%i]", i
);
2281 entry
->vuniformF_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
2283 entry
->puniformF_locations
= HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB
) * GL_LIMITS(pshader_constantsF
));
2284 for (i
= 0; i
< GL_LIMITS(pshader_constantsF
); ++i
) {
2285 snprintf(glsl_name
, sizeof(glsl_name
), "PC[%i]", i
);
2286 entry
->puniformF_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
2290 static GLhandleARB
create_glsl_blt_shader(WineD3D_GL_Info
*gl_info
) {
2291 GLhandleARB program_id
;
2292 GLhandleARB vshader_id
, pshader_id
;
2293 const char *blt_vshader
[] = {
2296 " gl_Position = gl_Vertex;\n"
2297 " gl_FrontColor = vec4(1.0);\n"
2298 " gl_TexCoord[0].x = (gl_Vertex.x * 0.5) + 0.5;\n"
2299 " gl_TexCoord[0].y = (-gl_Vertex.y * 0.5) + 0.5;\n"
2303 const char *blt_pshader
[] = {
2304 "uniform sampler2D sampler;\n"
2307 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
2311 vshader_id
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
2312 GL_EXTCALL(glShaderSourceARB(vshader_id
, 1, blt_vshader
, NULL
));
2313 GL_EXTCALL(glCompileShaderARB(vshader_id
));
2315 pshader_id
= GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB
));
2316 GL_EXTCALL(glShaderSourceARB(pshader_id
, 1, blt_pshader
, NULL
));
2317 GL_EXTCALL(glCompileShaderARB(pshader_id
));
2319 program_id
= GL_EXTCALL(glCreateProgramObjectARB());
2320 GL_EXTCALL(glAttachObjectARB(program_id
, vshader_id
));
2321 GL_EXTCALL(glAttachObjectARB(program_id
, pshader_id
));
2322 GL_EXTCALL(glLinkProgramARB(program_id
));
2324 print_glsl_info_log(&GLINFO_LOCATION
, program_id
);
2329 static void shader_glsl_select(IWineD3DDevice
*iface
, BOOL usePS
, BOOL useVS
) {
2330 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
2331 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
2332 GLhandleARB program_id
= 0;
2334 if (useVS
|| usePS
) set_glsl_shader_program(iface
, usePS
, useVS
);
2335 else This
->stateBlock
->glsl_program
= NULL
;
2337 program_id
= This
->stateBlock
->glsl_program
? This
->stateBlock
->glsl_program
->programId
: 0;
2338 if (program_id
) TRACE("Using GLSL program %u\n", program_id
);
2339 GL_EXTCALL(glUseProgramObjectARB(program_id
));
2340 checkGLcall("glUseProgramObjectARB");
2343 static void shader_glsl_select_depth_blt(IWineD3DDevice
*iface
) {
2344 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
2345 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
2346 static GLhandleARB program_id
= 0;
2347 static GLhandleARB loc
= -1;
2350 program_id
= create_glsl_blt_shader(gl_info
);
2351 loc
= GL_EXTCALL(glGetUniformLocationARB(program_id
, "sampler"));
2354 GL_EXTCALL(glUseProgramObjectARB(program_id
));
2355 GL_EXTCALL(glUniform1iARB(loc
, 0));
2358 static void shader_glsl_cleanup(IWineD3DDevice
*iface
) {
2359 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
2360 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
2361 GL_EXTCALL(glUseProgramObjectARB(0));
2364 const shader_backend_t glsl_shader_backend
= {
2365 &shader_glsl_select
,
2366 &shader_glsl_select_depth_blt
,
2367 &shader_glsl_load_constants
,
2368 &shader_glsl_cleanup