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]);
156 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
157 if(WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) == 1 &&
158 shader_is_pshader_version(This
->baseShader
.hex_version
)) {
161 LIST_FOR_EACH_ENTRY(constant
, constant_list
, constants_entry
, entry
) {
166 tmp_loc
= constant_locations
[i
];
168 /* We found this uniform name in the program - go ahead and send the data */
170 if(constants
[k
+ 0] < -1.0) lcl_const
[0] = -1.0;
171 else if(constants
[k
+ 0] > 1.0) lcl_const
[0] = 1.0;
172 else lcl_const
[0] = constants
[k
+ 0];
173 if(constants
[k
+ 1] < -1.0) lcl_const
[1] = -1.0;
174 else if(constants
[k
+ 1] > 1.0) lcl_const
[1] = 1.0;
175 else lcl_const
[1] = constants
[k
+ 1];
176 if(constants
[k
+ 2] < -1.0) lcl_const
[2] = -1.0;
177 else if(constants
[k
+ 2] > 1.0) lcl_const
[2] = 1.0;
178 else lcl_const
[2] = constants
[k
+ 2];
179 if(constants
[k
+ 3] < -1.0) lcl_const
[3] = -1.0;
180 else if(constants
[k
+ 3] > 1.0) lcl_const
[3] = 1.0;
181 else lcl_const
[3] = constants
[k
+ 3];
183 GL_EXTCALL(glUniform4fvARB(tmp_loc
, 1, lcl_const
));
188 LIST_FOR_EACH_ENTRY(constant
, constant_list
, constants_entry
, entry
) {
193 tmp_loc
= constant_locations
[i
];
195 /* We found this uniform name in the program - go ahead and send the data */
196 GL_EXTCALL(glUniform4fvARB(tmp_loc
, 1, constants
+ (i
* 4)));
201 checkGLcall("glUniform4fvARB()");
203 /* Load immediate constants */
204 if (TRACE_ON(d3d_shader
)) {
205 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
206 tmp_loc
= constant_locations
[lconst
->idx
];
208 GLfloat
* values
= (GLfloat
*)lconst
->value
;
209 TRACE_(d3d_constants
)("Loading local constants %i: %f, %f, %f, %f\n", lconst
->idx
,
210 values
[0], values
[1], values
[2], values
[3]);
214 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
215 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
216 tmp_loc
= constant_locations
[lconst
->idx
];
218 /* We found this uniform name in the program - go ahead and send the data */
219 GL_EXTCALL(glUniform4fvARB(tmp_loc
, 1, (GLfloat
*)lconst
->value
));
222 checkGLcall("glUniform4fvARB()");
226 * Loads integer constants (aka uniforms) into the currently set GLSL program.
227 * When @constants_set == NULL, it will load all the constants.
229 static void shader_glsl_load_constantsI(
230 IWineD3DBaseShaderImpl
* This
,
231 WineD3D_GL_Info
*gl_info
,
232 GLhandleARB programId
,
233 unsigned max_constants
,
235 BOOL
* constants_set
) {
240 char is_pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
241 const char* prefix
= is_pshader
? "PI":"VI";
244 for (i
=0; i
<max_constants
; ++i
) {
245 if (NULL
== constants_set
|| constants_set
[i
]) {
247 TRACE_(d3d_constants
)("Loading constants %i: %i, %i, %i, %i\n",
248 i
, constants
[i
*4], constants
[i
*4+1], constants
[i
*4+2], constants
[i
*4+3]);
250 /* TODO: Benchmark and see if it would be beneficial to store the
251 * locations of the constants to avoid looking up each time */
252 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, i
);
253 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
255 /* We found this uniform name in the program - go ahead and send the data */
256 GL_EXTCALL(glUniform4ivARB(tmp_loc
, 1, &constants
[i
*4]));
257 checkGLcall("glUniform4ivARB");
262 /* Load immediate constants */
263 ptr
= list_head(&This
->baseShader
.constantsI
);
265 local_constant
* lconst
= LIST_ENTRY(ptr
, struct local_constant
, entry
);
266 unsigned int idx
= lconst
->idx
;
267 GLint
* values
= (GLint
*) lconst
->value
;
269 TRACE_(d3d_constants
)("Loading local constants %i: %i, %i, %i, %i\n", idx
,
270 values
[0], values
[1], values
[2], values
[3]);
272 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, idx
);
273 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
275 /* We found this uniform name in the program - go ahead and send the data */
276 GL_EXTCALL(glUniform4ivARB(tmp_loc
, 1, values
));
277 checkGLcall("glUniform4ivARB");
279 ptr
= list_next(&This
->baseShader
.constantsI
, ptr
);
284 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
285 * When @constants_set == NULL, it will load all the constants.
287 static void shader_glsl_load_constantsB(
288 IWineD3DBaseShaderImpl
* This
,
289 WineD3D_GL_Info
*gl_info
,
290 GLhandleARB programId
,
291 unsigned max_constants
,
293 BOOL
* constants_set
) {
298 char is_pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
299 const char* prefix
= is_pshader
? "PB":"VB";
302 for (i
=0; i
<max_constants
; ++i
) {
303 if (NULL
== constants_set
|| constants_set
[i
]) {
305 TRACE_(d3d_constants
)("Loading constants %i: %i;\n", i
, constants
[i
*4]);
307 /* TODO: Benchmark and see if it would be beneficial to store the
308 * locations of the constants to avoid looking up each time */
309 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, i
);
310 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
312 /* We found this uniform name in the program - go ahead and send the data */
313 GL_EXTCALL(glUniform1ivARB(tmp_loc
, 1, &constants
[i
*4]));
314 checkGLcall("glUniform1ivARB");
319 /* Load immediate constants */
320 ptr
= list_head(&This
->baseShader
.constantsB
);
322 local_constant
* lconst
= LIST_ENTRY(ptr
, struct local_constant
, entry
);
323 unsigned int idx
= lconst
->idx
;
324 GLint
* values
= (GLint
*) lconst
->value
;
326 TRACE_(d3d_constants
)("Loading local constants %i: %i\n", idx
, values
[0]);
328 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, idx
);
329 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
331 /* We found this uniform name in the program - go ahead and send the data */
332 GL_EXTCALL(glUniform1ivARB(tmp_loc
, 1, values
));
333 checkGLcall("glUniform1ivARB");
335 ptr
= list_next(&This
->baseShader
.constantsB
, ptr
);
342 * Loads the app-supplied constants into the currently set GLSL program.
344 void shader_glsl_load_constants(
345 IWineD3DDevice
* device
,
347 char useVertexShader
) {
349 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) device
;
350 IWineD3DStateBlockImpl
* stateBlock
= deviceImpl
->stateBlock
;
351 WineD3D_GL_Info
*gl_info
= &deviceImpl
->adapter
->gl_info
;
353 GLhandleARB
*constant_locations
;
354 struct list
*constant_list
;
355 GLhandleARB programId
;
358 if (!stateBlock
->glsl_program
) {
359 /* No GLSL program set - nothing to do. */
362 programId
= stateBlock
->glsl_program
->programId
;
364 if (useVertexShader
) {
365 IWineD3DBaseShaderImpl
* vshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->vertexShader
;
368 constant_locations
= stateBlock
->glsl_program
->vuniformF_locations
;
369 constant_list
= &stateBlock
->set_vconstantsF
;
371 /* Load vertex shader samplers */
372 shader_glsl_load_vsamplers(gl_info
, (IWineD3DStateBlock
*)stateBlock
);
374 /* Load DirectX 9 float constants/uniforms for vertex shader */
375 shader_glsl_load_constantsF(vshader
, gl_info
, GL_LIMITS(vshader_constantsF
),
376 stateBlock
->vertexShaderConstantF
, constant_locations
, constant_list
);
378 /* Load DirectX 9 integer constants/uniforms for vertex shader */
379 shader_glsl_load_constantsI(vshader
, gl_info
, programId
, MAX_CONST_I
,
380 stateBlock
->vertexShaderConstantI
,
381 stateBlock
->changed
.vertexShaderConstantsI
);
383 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
384 shader_glsl_load_constantsB(vshader
, gl_info
, programId
, MAX_CONST_B
,
385 stateBlock
->vertexShaderConstantB
,
386 stateBlock
->changed
.vertexShaderConstantsB
);
388 /* Upload the position fixup params */
389 pos
= GL_EXTCALL(glGetUniformLocationARB(programId
, "posFixup"));
390 checkGLcall("glGetUniformLocationARB");
391 GL_EXTCALL(glUniform4fvARB(pos
, 1, &deviceImpl
->posFixup
[0]));
392 checkGLcall("glUniform4fvARB");
395 if (usePixelShader
) {
397 IWineD3DBaseShaderImpl
* pshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->pixelShader
;
399 constant_locations
= stateBlock
->glsl_program
->puniformF_locations
;
400 constant_list
= &stateBlock
->set_pconstantsF
;
402 /* Load pixel shader samplers */
403 shader_glsl_load_psamplers(gl_info
, (IWineD3DStateBlock
*) stateBlock
);
405 /* Load DirectX 9 float constants/uniforms for pixel shader */
406 shader_glsl_load_constantsF(pshader
, gl_info
, GL_LIMITS(pshader_constantsF
),
407 stateBlock
->pixelShaderConstantF
, constant_locations
, constant_list
);
409 /* Load DirectX 9 integer constants/uniforms for pixel shader */
410 shader_glsl_load_constantsI(pshader
, gl_info
, programId
, MAX_CONST_I
,
411 stateBlock
->pixelShaderConstantI
,
412 stateBlock
->changed
.pixelShaderConstantsI
);
414 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
415 shader_glsl_load_constantsB(pshader
, gl_info
, programId
, MAX_CONST_B
,
416 stateBlock
->pixelShaderConstantB
,
417 stateBlock
->changed
.pixelShaderConstantsB
);
419 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
420 * It can't be 0 for a valid texbem instruction.
422 if(((IWineD3DPixelShaderImpl
*) pshader
)->needsbumpmat
!= -1) {
423 float *data
= (float *) &stateBlock
->textureState
[(int) ((IWineD3DPixelShaderImpl
*) pshader
)->needsbumpmat
][WINED3DTSS_BUMPENVMAT00
];
424 pos
= GL_EXTCALL(glGetUniformLocationARB(programId
, "bumpenvmat"));
425 checkGLcall("glGetUniformLocationARB");
426 GL_EXTCALL(glUniformMatrix2fvARB(pos
, 1, 0, data
));
427 checkGLcall("glUniformMatrix2fvARB");
429 /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
430 * is set too, so we can check that in the needsbumpmat check
432 if(((IWineD3DPixelShaderImpl
*) pshader
)->baseShader
.reg_maps
.luminanceparams
!= -1) {
433 int stage
= ((IWineD3DPixelShaderImpl
*) pshader
)->baseShader
.reg_maps
.luminanceparams
;
434 GLfloat
*scale
= (GLfloat
*) &stateBlock
->textureState
[stage
][WINED3DTSS_BUMPENVLSCALE
];
435 GLfloat
*offset
= (GLfloat
*) &stateBlock
->textureState
[stage
][WINED3DTSS_BUMPENVLOFFSET
];
437 pos
= GL_EXTCALL(glGetUniformLocationARB(programId
, "luminancescale"));
438 checkGLcall("glGetUniformLocationARB");
439 GL_EXTCALL(glUniform1fvARB(pos
, 1, scale
));
440 checkGLcall("glUniform1fvARB");
441 pos
= GL_EXTCALL(glGetUniformLocationARB(programId
, "luminanceoffset"));
442 checkGLcall("glGetUniformLocationARB");
443 GL_EXTCALL(glUniform1fvARB(pos
, 1, offset
));
444 checkGLcall("glUniform1fvARB");
446 } else if(((IWineD3DPixelShaderImpl
*) pshader
)->srgb_enabled
&&
447 !((IWineD3DPixelShaderImpl
*) pshader
)->srgb_mode_hardcoded
) {
451 if(stateBlock
->renderState
[WINED3DRS_SRGBWRITEENABLE
]) {
452 comparison
[0] = srgb_cmp
; comparison
[1] = srgb_cmp
;
453 comparison
[2] = srgb_cmp
; comparison
[3] = srgb_cmp
;
455 mul_low
[0] = srgb_mul_low
; mul_low
[1] = srgb_mul_low
;
456 mul_low
[2] = srgb_mul_low
; mul_low
[3] = srgb_mul_low
;
458 comparison
[0] = 1.0 / 0.0; comparison
[1] = 1.0 / 0.0;
459 comparison
[2] = 1.0 / 0.0; comparison
[3] = 1.0 / 0.0;
461 mul_low
[0] = 1.0; mul_low
[1] = 1.0;
462 mul_low
[2] = 1.0; mul_low
[3] = 1.0;
465 pos
= GL_EXTCALL(glGetUniformLocationARB(programId
, "srgb_comparison"));
466 checkGLcall("glGetUniformLocationARB");
467 GL_EXTCALL(glUniform4fvARB(pos
, 1, comparison
));
468 pos
= GL_EXTCALL(glGetUniformLocationARB(programId
, "srgb_mul_low"));
469 checkGLcall("glGetUniformLocationARB");
470 GL_EXTCALL(glUniform4fvARB(pos
, 1, mul_low
));
475 /** Generate the variable & register declarations for the GLSL output target */
476 void shader_generate_glsl_declarations(
477 IWineD3DBaseShader
*iface
,
478 shader_reg_maps
* reg_maps
,
479 SHADER_BUFFER
* buffer
,
480 WineD3D_GL_Info
* gl_info
) {
482 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
483 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
485 unsigned int extra_constants_needed
= 0;
487 /* There are some minor differences between pixel and vertex shaders */
488 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
489 char prefix
= pshader
? 'P' : 'V';
491 /* Prototype the subroutines */
492 for (i
= 0; i
< This
->baseShader
.limits
.label
; i
++) {
493 if (reg_maps
->labels
[i
])
494 shader_addline(buffer
, "void subroutine%lu();\n", i
);
497 /* Declare the constants (aka uniforms) */
498 if (This
->baseShader
.limits
.constant_float
> 0) {
499 unsigned max_constantsF
= min(This
->baseShader
.limits
.constant_float
,
500 (pshader
? GL_LIMITS(pshader_constantsF
) : GL_LIMITS(vshader_constantsF
)));
501 shader_addline(buffer
, "uniform vec4 %cC[%u];\n", prefix
, max_constantsF
);
504 if (This
->baseShader
.limits
.constant_int
> 0)
505 shader_addline(buffer
, "uniform ivec4 %cI[%u];\n", prefix
, This
->baseShader
.limits
.constant_int
);
507 if (This
->baseShader
.limits
.constant_bool
> 0)
508 shader_addline(buffer
, "uniform bool %cB[%u];\n", prefix
, This
->baseShader
.limits
.constant_bool
);
511 shader_addline(buffer
, "uniform vec4 posFixup;\n");
513 IWineD3DPixelShaderImpl
*ps_impl
= (IWineD3DPixelShaderImpl
*) This
;
515 if(reg_maps
->bumpmat
!= -1) {
516 shader_addline(buffer
, "uniform mat2 bumpenvmat;\n");
517 if(reg_maps
->luminanceparams
) {
518 shader_addline(buffer
, "uniform float luminancescale;\n");
519 shader_addline(buffer
, "uniform float luminanceoffset;\n");
520 extra_constants_needed
++;
522 extra_constants_needed
++;
525 if(device
->stateBlock
->renderState
[WINED3DRS_SRGBWRITEENABLE
]) {
526 ps_impl
->srgb_enabled
= 1;
527 if(This
->baseShader
.limits
.constant_float
+ extra_constants_needed
+ 1 < GL_LIMITS(pshader_constantsF
)) {
528 shader_addline(buffer
, "uniform vec4 srgb_mul_low;\n");
529 shader_addline(buffer
, "uniform vec4 srgb_comparison;\n");
530 ps_impl
->srgb_mode_hardcoded
= 0;
532 ps_impl
->srgb_mode_hardcoded
= 1;
533 shader_addline(buffer
, "const vec4 srgb_mul_low = {%f, %f, %f, %f};\n",
534 srgb_mul_low
, srgb_mul_low
, srgb_mul_low
, srgb_mul_low
);
535 shader_addline(buffer
, "const vec4 srgb_comparison = {%f, %f, %f, %f};\n",
536 srgb_cmp
, srgb_cmp
, srgb_cmp
, srgb_cmp
);
539 IWineD3DPixelShaderImpl
*ps_impl
= (IWineD3DPixelShaderImpl
*) This
;
541 /* Do not write any srgb fixup into the shader to save shader size and processing time.
542 * As a consequence, we can't toggle srgb write on without recompilation
544 ps_impl
->srgb_enabled
= 0;
545 ps_impl
->srgb_mode_hardcoded
= 1;
549 /* Declare texture samplers */
550 for (i
= 0; i
< This
->baseShader
.limits
.sampler
; i
++) {
551 if (reg_maps
->samplers
[i
]) {
553 DWORD stype
= reg_maps
->samplers
[i
] & WINED3DSP_TEXTURETYPE_MASK
;
557 shader_addline(buffer
, "uniform sampler1D %csampler%lu;\n", prefix
, i
);
560 shader_addline(buffer
, "uniform sampler2D %csampler%lu;\n", prefix
, i
);
562 case WINED3DSTT_CUBE
:
563 shader_addline(buffer
, "uniform samplerCube %csampler%lu;\n", prefix
, i
);
565 case WINED3DSTT_VOLUME
:
566 shader_addline(buffer
, "uniform sampler3D %csampler%lu;\n", prefix
, i
);
569 shader_addline(buffer
, "uniform unsupported_sampler %csampler%lu;\n", prefix
, i
);
570 FIXME("Unrecognized sampler type: %#x\n", stype
);
576 /* Declare address variables */
577 for (i
= 0; i
< This
->baseShader
.limits
.address
; i
++) {
578 if (reg_maps
->address
[i
])
579 shader_addline(buffer
, "ivec4 A%d;\n", i
);
582 /* Declare texture coordinate temporaries and initialize them */
583 for (i
= 0; i
< This
->baseShader
.limits
.texcoord
; i
++) {
584 if (reg_maps
->texcoord
[i
])
585 shader_addline(buffer
, "vec4 T%lu = gl_TexCoord[%lu];\n", i
, i
);
588 /* Declare input register temporaries */
589 for (i
=0; i
< This
->baseShader
.limits
.packed_input
; i
++) {
590 if (reg_maps
->packed_input
[i
])
591 shader_addline(buffer
, "vec4 IN%lu;\n", i
);
594 /* Declare output register temporaries */
595 for (i
= 0; i
< This
->baseShader
.limits
.packed_output
; i
++) {
596 if (reg_maps
->packed_output
[i
])
597 shader_addline(buffer
, "vec4 OUT%lu;\n", i
);
600 /* Declare temporary variables */
601 for(i
= 0; i
< This
->baseShader
.limits
.temporary
; i
++) {
602 if (reg_maps
->temporary
[i
])
603 shader_addline(buffer
, "vec4 R%lu;\n", i
);
606 /* Declare attributes */
607 for (i
= 0; i
< This
->baseShader
.limits
.attributes
; i
++) {
608 if (reg_maps
->attributes
[i
])
609 shader_addline(buffer
, "attribute vec4 attrib%i;\n", i
);
612 /* Declare loop register aL */
613 if (reg_maps
->loop
) {
614 shader_addline(buffer
, "int aL;\n");
615 shader_addline(buffer
, "int tmpInt;\n");
618 /* Temporary variables for matrix operations */
619 shader_addline(buffer
, "vec4 tmp0;\n");
620 shader_addline(buffer
, "vec4 tmp1;\n");
622 /* Start the main program */
623 shader_addline(buffer
, "void main() {\n");
626 /*****************************************************************************
627 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
629 * For more information, see http://wiki.winehq.org/DirectX-Shaders
630 ****************************************************************************/
633 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG
* arg
, const DWORD param
,
634 const DWORD addr_token
, DWORD mask
, glsl_src_param_t
*src_param
);
636 /** Used for opcode modifiers - They multiply the result by the specified amount */
637 static const char * const shift_glsl_tab
[] = {
639 "2.0 * ", /* 1 (x2) */
640 "4.0 * ", /* 2 (x4) */
641 "8.0 * ", /* 3 (x8) */
642 "16.0 * ", /* 4 (x16) */
643 "32.0 * ", /* 5 (x32) */
650 "0.0625 * ", /* 12 (d16) */
651 "0.125 * ", /* 13 (d8) */
652 "0.25 * ", /* 14 (d4) */
653 "0.5 * " /* 15 (d2) */
656 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
657 static void shader_glsl_gen_modifier (
660 const char *in_regswizzle
,
665 if (instr
== WINED3DSIO_TEXKILL
)
668 switch (instr
& WINED3DSP_SRCMOD_MASK
) {
669 case WINED3DSPSM_DZ
: /* Need to handle this in the instructions itself (texld & texcrd). */
671 case WINED3DSPSM_NONE
:
672 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
674 case WINED3DSPSM_NEG
:
675 sprintf(out_str
, "-%s%s", in_reg
, in_regswizzle
);
677 case WINED3DSPSM_NOT
:
678 sprintf(out_str
, "!%s%s", in_reg
, in_regswizzle
);
680 case WINED3DSPSM_BIAS
:
681 sprintf(out_str
, "(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
683 case WINED3DSPSM_BIASNEG
:
684 sprintf(out_str
, "-(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
686 case WINED3DSPSM_SIGN
:
687 sprintf(out_str
, "(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
689 case WINED3DSPSM_SIGNNEG
:
690 sprintf(out_str
, "-(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
692 case WINED3DSPSM_COMP
:
693 sprintf(out_str
, "(1.0 - %s%s)", in_reg
, in_regswizzle
);
696 sprintf(out_str
, "(2.0 * %s%s)", in_reg
, in_regswizzle
);
698 case WINED3DSPSM_X2NEG
:
699 sprintf(out_str
, "-(2.0 * %s%s)", in_reg
, in_regswizzle
);
701 case WINED3DSPSM_ABS
:
702 sprintf(out_str
, "abs(%s%s)", in_reg
, in_regswizzle
);
704 case WINED3DSPSM_ABSNEG
:
705 sprintf(out_str
, "-abs(%s%s)", in_reg
, in_regswizzle
);
708 FIXME("Unhandled modifier %u\n", (instr
& WINED3DSP_SRCMOD_MASK
));
709 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
713 /** Writes the GLSL variable name that corresponds to the register that the
714 * DX opcode parameter is trying to access */
715 static void shader_glsl_get_register_name(
717 const DWORD addr_token
,
720 SHADER_OPCODE_ARG
* arg
) {
722 /* oPos, oFog and oPts in D3D */
723 static const char * const hwrastout_reg_names
[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
725 DWORD reg
= param
& WINED3DSP_REGNUM_MASK
;
726 DWORD regtype
= shader_get_regtype(param
);
727 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
728 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
729 WineD3D_GL_Info
* gl_info
= &deviceImpl
->adapter
->gl_info
;
731 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
737 case WINED3DSPR_TEMP
:
738 sprintf(tmpStr
, "R%u", reg
);
740 case WINED3DSPR_INPUT
:
742 /* Pixel shaders >= 3.0 */
743 if (WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 3)
744 sprintf(tmpStr
, "IN%u", reg
);
747 strcpy(tmpStr
, "gl_Color");
749 strcpy(tmpStr
, "gl_SecondaryColor");
752 if (vshader_input_is_color((IWineD3DVertexShader
*) This
, reg
))
754 sprintf(tmpStr
, "attrib%u", reg
);
757 case WINED3DSPR_CONST
:
759 const char* prefix
= pshader
? "PC":"VC";
761 /* Relative addressing */
762 if (param
& WINED3DSHADER_ADDRMODE_RELATIVE
) {
764 /* Relative addressing on shaders 2.0+ have a relative address token,
765 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
766 if (WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 2) {
767 glsl_src_param_t rel_param
;
768 shader_glsl_add_src_param(arg
, addr_token
, 0, WINED3DSP_WRITEMASK_0
, &rel_param
);
769 sprintf(tmpStr
, "%s[%s + %u]", prefix
, rel_param
.param_str
, reg
);
771 sprintf(tmpStr
, "%s[A0.x + %u]", prefix
, reg
);
774 sprintf(tmpStr
, "%s[%u]", prefix
, reg
);
778 case WINED3DSPR_CONSTINT
:
780 sprintf(tmpStr
, "PI[%u]", reg
);
782 sprintf(tmpStr
, "VI[%u]", reg
);
784 case WINED3DSPR_CONSTBOOL
:
786 sprintf(tmpStr
, "PB[%u]", reg
);
788 sprintf(tmpStr
, "VB[%u]", reg
);
790 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
792 sprintf(tmpStr
, "T%u", reg
);
794 sprintf(tmpStr
, "A%u", reg
);
797 case WINED3DSPR_LOOP
:
798 sprintf(tmpStr
, "aL");
800 case WINED3DSPR_SAMPLER
:
802 sprintf(tmpStr
, "Psampler%u", reg
);
804 sprintf(tmpStr
, "Vsampler%u", reg
);
806 case WINED3DSPR_COLOROUT
:
807 if (reg
>= GL_LIMITS(buffers
)) {
808 WARN("Write to render target %u, only %d supported\n", reg
, 4);
810 if (GL_SUPPORT(ARB_DRAW_BUFFERS
)) {
811 sprintf(tmpStr
, "gl_FragData[%u]", reg
);
812 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
813 sprintf(tmpStr
, "gl_FragColor");
816 case WINED3DSPR_RASTOUT
:
817 sprintf(tmpStr
, "%s", hwrastout_reg_names
[reg
]);
819 case WINED3DSPR_DEPTHOUT
:
820 sprintf(tmpStr
, "gl_FragDepth");
822 case WINED3DSPR_ATTROUT
:
824 sprintf(tmpStr
, "gl_FrontColor");
826 sprintf(tmpStr
, "gl_FrontSecondaryColor");
829 case WINED3DSPR_TEXCRDOUT
:
830 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
831 if (WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 3)
832 sprintf(tmpStr
, "OUT%u", reg
);
834 sprintf(tmpStr
, "gl_TexCoord[%u]", reg
);
836 case WINED3DSPR_MISCTYPE
:
839 sprintf(tmpStr
, "gl_FragCoord");
841 /* gl_FrontFacing could be used for vFace, but note that
842 * gl_FrontFacing is a bool, while vFace is a float for
843 * which the sign determines front/back */
844 FIXME("Unhandled misctype register %d\n", reg
);
845 sprintf(tmpStr
, "unrecognized_register");
849 FIXME("Unhandled register name Type(%d)\n", regtype
);
850 sprintf(tmpStr
, "unrecognized_register");
854 strcat(regstr
, tmpStr
);
857 /* Get the GLSL write mask for the destination register */
858 static DWORD
shader_glsl_get_write_mask(const DWORD param
, char *write_mask
) {
859 char *ptr
= write_mask
;
860 DWORD mask
= param
& WINED3DSP_WRITEMASK_ALL
;
862 if (shader_is_scalar(param
)) {
863 mask
= WINED3DSP_WRITEMASK_0
;
866 if (param
& WINED3DSP_WRITEMASK_0
) *ptr
++ = 'x';
867 if (param
& WINED3DSP_WRITEMASK_1
) *ptr
++ = 'y';
868 if (param
& WINED3DSP_WRITEMASK_2
) *ptr
++ = 'z';
869 if (param
& WINED3DSP_WRITEMASK_3
) *ptr
++ = 'w';
877 static size_t shader_glsl_get_write_mask_size(DWORD write_mask
) {
880 if (write_mask
& WINED3DSP_WRITEMASK_0
) ++size
;
881 if (write_mask
& WINED3DSP_WRITEMASK_1
) ++size
;
882 if (write_mask
& WINED3DSP_WRITEMASK_2
) ++size
;
883 if (write_mask
& WINED3DSP_WRITEMASK_3
) ++size
;
888 static void shader_glsl_get_swizzle(const DWORD param
, BOOL fixup
, DWORD mask
, char *swizzle_str
) {
889 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
890 * but addressed as "rgba". To fix this we need to swap the register's x
891 * and z components. */
892 DWORD swizzle
= (param
& WINED3DSP_SWIZZLE_MASK
) >> WINED3DSP_SWIZZLE_SHIFT
;
893 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
894 char *ptr
= swizzle_str
;
896 if (!shader_is_scalar(param
)) {
898 /* swizzle bits fields: wwzzyyxx */
899 if (mask
& WINED3DSP_WRITEMASK_0
) *ptr
++ = swizzle_chars
[swizzle
& 0x03];
900 if (mask
& WINED3DSP_WRITEMASK_1
) *ptr
++ = swizzle_chars
[(swizzle
>> 2) & 0x03];
901 if (mask
& WINED3DSP_WRITEMASK_2
) *ptr
++ = swizzle_chars
[(swizzle
>> 4) & 0x03];
902 if (mask
& WINED3DSP_WRITEMASK_3
) *ptr
++ = swizzle_chars
[(swizzle
>> 6) & 0x03];
908 /* From a given parameter token, generate the corresponding GLSL string.
909 * Also, return the actual register name and swizzle in case the
910 * caller needs this information as well. */
911 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG
* arg
, const DWORD param
,
912 const DWORD addr_token
, DWORD mask
, glsl_src_param_t
*src_param
) {
913 BOOL is_color
= FALSE
;
916 src_param
->reg_name
[0] = '\0';
917 src_param
->param_str
[0] = '\0';
918 swizzle_str
[0] = '\0';
920 shader_glsl_get_register_name(param
, addr_token
, src_param
->reg_name
, &is_color
, arg
);
922 shader_glsl_get_swizzle(param
, is_color
, mask
, swizzle_str
);
923 shader_glsl_gen_modifier(param
, src_param
->reg_name
, swizzle_str
, src_param
->param_str
);
926 /* From a given parameter token, generate the corresponding GLSL string.
927 * Also, return the actual register name and swizzle in case the
928 * caller needs this information as well. */
929 static DWORD
shader_glsl_add_dst_param(SHADER_OPCODE_ARG
* arg
, const DWORD param
,
930 const DWORD addr_token
, glsl_dst_param_t
*dst_param
) {
931 BOOL is_color
= FALSE
;
933 dst_param
->mask_str
[0] = '\0';
934 dst_param
->reg_name
[0] = '\0';
936 shader_glsl_get_register_name(param
, addr_token
, dst_param
->reg_name
, &is_color
, arg
);
937 return shader_glsl_get_write_mask(param
, dst_param
->mask_str
);
940 /* Append the destination part of the instruction to the buffer, return the effective write mask */
941 static DWORD
shader_glsl_append_dst_ext(SHADER_BUFFER
*buffer
, SHADER_OPCODE_ARG
*arg
, const DWORD param
) {
942 glsl_dst_param_t dst_param
;
946 mask
= shader_glsl_add_dst_param(arg
, param
, arg
->dst_addr
, &dst_param
);
949 shift
= (param
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
950 shader_addline(buffer
, "%s%s = %s(", dst_param
.reg_name
, dst_param
.mask_str
, shift_glsl_tab
[shift
]);
956 /* Append the destination part of the instruction to the buffer, return the effective write mask */
957 static DWORD
shader_glsl_append_dst(SHADER_BUFFER
*buffer
, SHADER_OPCODE_ARG
*arg
) {
958 return shader_glsl_append_dst_ext(buffer
, arg
, arg
->dst
);
961 /** Process GLSL instruction modifiers */
962 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG
* arg
) {
964 DWORD mask
= arg
->dst
& WINED3DSP_DSTMOD_MASK
;
966 if (arg
->opcode
->dst_token
&& mask
!= 0) {
967 glsl_dst_param_t dst_param
;
969 shader_glsl_add_dst_param(arg
, arg
->dst
, 0, &dst_param
);
971 if (mask
& WINED3DSPDM_SATURATE
) {
972 /* _SAT means to clamp the value of the register to between 0 and 1 */
973 shader_addline(arg
->buffer
, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param
.reg_name
,
974 dst_param
.mask_str
, dst_param
.reg_name
, dst_param
.mask_str
);
976 if (mask
& WINED3DSPDM_MSAMPCENTROID
) {
977 FIXME("_centroid modifier not handled\n");
979 if (mask
& WINED3DSPDM_PARTIALPRECISION
) {
980 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
985 static inline const char* shader_get_comp_op(
986 const DWORD opcode
) {
988 DWORD op
= (opcode
& INST_CONTROLS_MASK
) >> INST_CONTROLS_SHIFT
;
990 case COMPARISON_GT
: return ">";
991 case COMPARISON_EQ
: return "==";
992 case COMPARISON_GE
: return ">=";
993 case COMPARISON_LT
: return "<";
994 case COMPARISON_NE
: return "!=";
995 case COMPARISON_LE
: return "<=";
997 FIXME("Unrecognized comparison value: %u\n", op
);
1002 static void shader_glsl_get_sample_function(DWORD sampler_type
, BOOL projected
, glsl_sample_function_t
*sample_function
) {
1003 /* Note that there's no such thing as a projected cube texture. */
1004 switch(sampler_type
) {
1006 sample_function
->name
= projected
? "texture1DProj" : "texture1D";
1007 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
;
1010 sample_function
->name
= projected
? "texture2DProj" : "texture2D";
1011 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
;
1013 case WINED3DSTT_CUBE
:
1014 sample_function
->name
= "textureCube";
1015 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1017 case WINED3DSTT_VOLUME
:
1018 sample_function
->name
= projected
? "texture3DProj" : "texture3D";
1019 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1022 sample_function
->name
= "";
1023 FIXME("Unrecognized sampler type: %#x;\n", sampler_type
);
1028 static void shader_glsl_color_correction(SHADER_OPCODE_ARG
* arg
) {
1029 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
1030 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) shader
->baseShader
.device
;
1031 WineD3D_GL_Info
*gl_info
= &deviceImpl
->adapter
->gl_info
;
1032 glsl_dst_param_t dst_param
;
1033 glsl_dst_param_t dst_param2
;
1035 WINED3DFORMAT conversion_group
;
1036 IWineD3DBaseTextureImpl
*texture
;
1037 DWORD mask
, mask_size
;
1039 BOOL recorded
= FALSE
;
1041 DWORD hex_version
= shader
->baseShader
.hex_version
;
1043 switch(arg
->opcode
->opcode
) {
1044 case WINED3DSIO_TEX
:
1045 if (hex_version
< WINED3DPS_VERSION(2,0)) {
1046 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1048 sampler_idx
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
1052 case WINED3DSIO_TEXLDL
:
1053 FIXME("Add color fixup for vertex texture WINED3DSIO_TEXLDL\n");
1056 case WINED3DSIO_TEXDP3TEX
:
1057 case WINED3DSIO_TEXM3x3TEX
:
1058 case WINED3DSIO_TEXM3x3SPEC
:
1059 case WINED3DSIO_TEXM3x3VSPEC
:
1060 case WINED3DSIO_TEXBEM
:
1061 case WINED3DSIO_TEXREG2AR
:
1062 case WINED3DSIO_TEXREG2GB
:
1063 case WINED3DSIO_TEXREG2RGB
:
1064 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1068 /* Not a texture sampling instruction, nothing to do */
1072 texture
= (IWineD3DBaseTextureImpl
*) deviceImpl
->stateBlock
->textures
[sampler_idx
];
1074 fmt
= texture
->resource
.format
;
1075 conversion_group
= texture
->baseTexture
.shader_conversion_group
;
1077 fmt
= WINED3DFMT_UNKNOWN
;
1078 conversion_group
= WINED3DFMT_UNKNOWN
;
1081 /* before doing anything, record the sampler with the format in the format conversion list,
1082 * but check if it's not there already
1084 for(i
= 0; i
< shader
->baseShader
.num_sampled_samplers
; i
++) {
1085 if(shader
->baseShader
.sampled_samplers
[i
] == sampler_idx
) {
1091 shader
->baseShader
.sampled_samplers
[shader
->baseShader
.num_sampled_samplers
] = sampler_idx
;
1092 shader
->baseShader
.num_sampled_samplers
++;
1093 shader
->baseShader
.sampled_format
[sampler_idx
] = conversion_group
;
1097 case WINED3DFMT_V8U8
:
1098 case WINED3DFMT_V16U16
:
1099 if(GL_SUPPORT(NV_TEXTURE_SHADER
) ||
1100 (GL_SUPPORT(ATI_ENVMAP_BUMPMAP
) && fmt
== WINED3DFMT_V8U8
)) {
1101 /* The 3rd channel returns 1.0 in d3d, but 0.0 in gl. Fix this while we're at it :-) */
1102 mask
= shader_glsl_add_dst_param(arg
, arg
->dst
, WINED3DSP_WRITEMASK_2
, &dst_param
);
1103 mask_size
= shader_glsl_get_write_mask_size(mask
);
1104 if(mask_size
>= 3) {
1105 shader_addline(arg
->buffer
, "%s.%c = 1.0;\n", dst_param
.reg_name
, dst_param
.mask_str
[3]);
1108 /* Correct the sign, but leave the blue as it is - it was loaded correctly already */
1109 mask
= shader_glsl_add_dst_param(arg
, arg
->dst
,
1110 WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
,
1112 mask_size
= shader_glsl_get_write_mask_size(mask
);
1113 if(mask_size
>= 2) {
1114 shader_addline(arg
->buffer
, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1115 dst_param
.reg_name
, dst_param
.mask_str
[1], dst_param
.mask_str
[2],
1116 dst_param
.reg_name
, dst_param
.mask_str
[1], dst_param
.mask_str
[2]);
1117 } else if(mask_size
== 1) {
1118 shader_addline(arg
->buffer
, "%s.%c = %s.%c * 2.0 - 1.0;\n", dst_param
.reg_name
, dst_param
.mask_str
[1],
1119 dst_param
.reg_name
, dst_param
.mask_str
[1]);
1124 case WINED3DFMT_X8L8V8U8
:
1125 if(!GL_SUPPORT(NV_TEXTURE_SHADER
)) {
1126 /* Red and blue are the signed channels, fix them up; Blue(=L) is correct already,
1127 * and a(X) is always 1.0
1129 mask
= shader_glsl_add_dst_param(arg
, arg
->dst
, WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &dst_param
);
1130 mask_size
= shader_glsl_get_write_mask_size(mask
);
1131 if(mask_size
>= 2) {
1132 shader_addline(arg
->buffer
, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1133 dst_param
.reg_name
, dst_param
.mask_str
[1], dst_param
.mask_str
[2],
1134 dst_param
.reg_name
, dst_param
.mask_str
[1], dst_param
.mask_str
[2]);
1135 } else if(mask_size
== 1) {
1136 shader_addline(arg
->buffer
, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1137 dst_param
.reg_name
, dst_param
.mask_str
[1],
1138 dst_param
.reg_name
, dst_param
.mask_str
[1]);
1143 case WINED3DFMT_L6V5U5
:
1144 if(!GL_SUPPORT(NV_TEXTURE_SHADER
)) {
1145 mask
= shader_glsl_add_dst_param(arg
, arg
->dst
, WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &dst_param
);
1146 mask_size
= shader_glsl_get_write_mask_size(mask
);
1147 shader_glsl_add_dst_param(arg
, arg
->dst
, WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_2
, &dst_param2
);
1148 if(mask_size
>= 3) {
1149 /* Swap y and z (U and L), and do a sign conversion on x and the new y(V and U) */
1150 shader_addline(arg
->buffer
, "tmp0.g = %s.%c;\n",
1151 dst_param
.reg_name
, dst_param
.mask_str
[2]);
1152 shader_addline(arg
->buffer
, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1153 dst_param
.reg_name
, dst_param
.mask_str
[2], dst_param
.mask_str
[1],
1154 dst_param2
.reg_name
, dst_param
.mask_str
[1], dst_param
.mask_str
[3]);
1155 shader_addline(arg
->buffer
, "%s.%c = tmp0.g;\n", dst_param
.reg_name
,
1156 dst_param
.mask_str
[3]);
1157 } else if(mask_size
== 2) {
1158 /* This is bad: We have VL, but we need VU */
1159 FIXME("2 components sampled from a converted L6V5U5 texture\n");
1161 shader_addline(arg
->buffer
, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1162 dst_param
.reg_name
, dst_param
.mask_str
[1],
1163 dst_param2
.reg_name
, dst_param
.mask_str
[1]);
1168 case WINED3DFMT_Q8W8V8U8
:
1169 if(!GL_SUPPORT(NV_TEXTURE_SHADER
)) {
1170 /* Correct the sign in all channels. The writemask just applies as-is, no
1171 * need for checking the mask size
1173 shader_glsl_add_dst_param(arg
, arg
->dst
,
1174 WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
|
1175 WINED3DSP_WRITEMASK_2
| WINED3DSP_WRITEMASK_3
,
1177 shader_addline(arg
->buffer
, "%s%s = %s%s * 2.0 - 1.0;\n", dst_param
.reg_name
, dst_param
.mask_str
,
1178 dst_param
.reg_name
, dst_param
.mask_str
);
1182 /* stupid compiler */
1188 /*****************************************************************************
1190 * Begin processing individual instruction opcodes
1192 ****************************************************************************/
1194 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
1195 void shader_glsl_arith(SHADER_OPCODE_ARG
* arg
) {
1196 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
1197 SHADER_BUFFER
* buffer
= arg
->buffer
;
1198 glsl_src_param_t src0_param
;
1199 glsl_src_param_t src1_param
;
1203 /* Determine the GLSL operator to use based on the opcode */
1204 switch (curOpcode
->opcode
) {
1205 case WINED3DSIO_MUL
: op
= '*'; break;
1206 case WINED3DSIO_ADD
: op
= '+'; break;
1207 case WINED3DSIO_SUB
: op
= '-'; break;
1210 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode
->name
);
1214 write_mask
= shader_glsl_append_dst(buffer
, arg
);
1215 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1216 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1217 shader_addline(buffer
, "%s %c %s);\n", src0_param
.param_str
, op
, src1_param
.param_str
);
1220 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1221 void shader_glsl_mov(SHADER_OPCODE_ARG
* arg
) {
1222 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
1223 SHADER_BUFFER
* buffer
= arg
->buffer
;
1224 glsl_src_param_t src0_param
;
1227 write_mask
= shader_glsl_append_dst(buffer
, arg
);
1228 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1230 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1231 * shader versions WINED3DSIO_MOVA is used for this. */
1232 if ((WINED3DSHADER_VERSION_MAJOR(shader
->baseShader
.hex_version
) == 1 &&
1233 !shader_is_pshader_version(shader
->baseShader
.hex_version
) &&
1234 shader_get_regtype(arg
->dst
) == WINED3DSPR_ADDR
) ||
1235 arg
->opcode
->opcode
== WINED3DSIO_MOVA
) {
1236 /* We need to *round* to the nearest int here. */
1237 size_t mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1238 if (mask_size
> 1) {
1239 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
);
1241 shader_addline(buffer
, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param
.param_str
, src0_param
.param_str
);
1244 shader_addline(buffer
, "%s);\n", src0_param
.param_str
);
1248 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
1249 void shader_glsl_dot(SHADER_OPCODE_ARG
* arg
) {
1250 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
1251 SHADER_BUFFER
* buffer
= arg
->buffer
;
1252 glsl_src_param_t src0_param
;
1253 glsl_src_param_t src1_param
;
1254 DWORD dst_write_mask
, src_write_mask
;
1255 size_t dst_size
= 0;
1257 dst_write_mask
= shader_glsl_append_dst(buffer
, arg
);
1258 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
1260 /* dp3 works on vec3, dp4 on vec4 */
1261 if (curOpcode
->opcode
== WINED3DSIO_DP4
) {
1262 src_write_mask
= WINED3DSP_WRITEMASK_ALL
;
1264 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1267 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_write_mask
, &src0_param
);
1268 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], src_write_mask
, &src1_param
);
1271 shader_addline(buffer
, "vec%d(dot(%s, %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
1273 shader_addline(buffer
, "dot(%s, %s));\n", src0_param
.param_str
, src1_param
.param_str
);
1277 /* Note that this instruction has some restrictions. The destination write mask
1278 * can't contain the w component, and the source swizzles have to be .xyzw */
1279 void shader_glsl_cross(SHADER_OPCODE_ARG
*arg
) {
1280 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1281 glsl_src_param_t src0_param
;
1282 glsl_src_param_t src1_param
;
1285 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1286 shader_glsl_append_dst(arg
->buffer
, arg
);
1287 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
1288 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], src_mask
, &src1_param
);
1289 shader_addline(arg
->buffer
, "cross(%s, %s)%s);\n", src0_param
.param_str
, src1_param
.param_str
, dst_mask
);
1292 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1293 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1294 * GLSL uses the value as-is. */
1295 void shader_glsl_pow(SHADER_OPCODE_ARG
*arg
) {
1296 SHADER_BUFFER
*buffer
= arg
->buffer
;
1297 glsl_src_param_t src0_param
;
1298 glsl_src_param_t src1_param
;
1299 DWORD dst_write_mask
;
1302 dst_write_mask
= shader_glsl_append_dst(buffer
, arg
);
1303 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
1305 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1306 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
1309 shader_addline(buffer
, "vec%d(pow(abs(%s), %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
1311 shader_addline(buffer
, "pow(abs(%s), %s));\n", src0_param
.param_str
, src1_param
.param_str
);
1315 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1316 void shader_glsl_map2gl(SHADER_OPCODE_ARG
* arg
) {
1317 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
1318 SHADER_BUFFER
* buffer
= arg
->buffer
;
1319 glsl_src_param_t src_param
;
1320 const char *instruction
;
1321 char arguments
[256];
1325 /* Determine the GLSL function to use based on the opcode */
1326 /* TODO: Possibly make this a table for faster lookups */
1327 switch (curOpcode
->opcode
) {
1328 case WINED3DSIO_MIN
: instruction
= "min"; break;
1329 case WINED3DSIO_MAX
: instruction
= "max"; break;
1330 case WINED3DSIO_ABS
: instruction
= "abs"; break;
1331 case WINED3DSIO_FRC
: instruction
= "fract"; break;
1332 case WINED3DSIO_NRM
: instruction
= "normalize"; break;
1333 case WINED3DSIO_LOGP
:
1334 case WINED3DSIO_LOG
: instruction
= "log2"; break;
1335 case WINED3DSIO_EXP
: instruction
= "exp2"; break;
1336 case WINED3DSIO_SGN
: instruction
= "sign"; break;
1337 case WINED3DSIO_DSX
: instruction
= "dFdx"; break;
1338 case WINED3DSIO_DSY
: instruction
= "dFdy"; break;
1339 default: instruction
= "";
1340 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode
->name
);
1344 write_mask
= shader_glsl_append_dst(buffer
, arg
);
1346 arguments
[0] = '\0';
1347 if (curOpcode
->num_params
> 0) {
1348 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src_param
);
1349 strcat(arguments
, src_param
.param_str
);
1350 for (i
= 2; i
< curOpcode
->num_params
; ++i
) {
1351 strcat(arguments
, ", ");
1352 shader_glsl_add_src_param(arg
, arg
->src
[i
-1], arg
->src_addr
[i
-1], write_mask
, &src_param
);
1353 strcat(arguments
, src_param
.param_str
);
1357 shader_addline(buffer
, "%s(%s));\n", instruction
, arguments
);
1360 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1361 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1362 * dst.x = 2^(floor(src))
1363 * dst.y = src - floor(src)
1364 * dst.z = 2^src (partial precision is allowed, but optional)
1366 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1367 * dst = 2^src; (partial precision is allowed, but optional)
1369 void shader_glsl_expp(SHADER_OPCODE_ARG
* arg
) {
1370 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)arg
->shader
;
1371 glsl_src_param_t src_param
;
1373 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
1375 if (shader
->baseShader
.hex_version
< WINED3DPS_VERSION(2,0)) {
1378 shader_addline(arg
->buffer
, "tmp0.x = exp2(floor(%s));\n", src_param
.param_str
);
1379 shader_addline(arg
->buffer
, "tmp0.y = %s - floor(%s);\n", src_param
.param_str
, src_param
.param_str
);
1380 shader_addline(arg
->buffer
, "tmp0.z = exp2(%s);\n", src_param
.param_str
);
1381 shader_addline(arg
->buffer
, "tmp0.w = 1.0;\n");
1383 shader_glsl_append_dst(arg
->buffer
, arg
);
1384 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1385 shader_addline(arg
->buffer
, "tmp0%s);\n", dst_mask
);
1390 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1391 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1393 if (mask_size
> 1) {
1394 shader_addline(arg
->buffer
, "vec%d(exp2(%s)));\n", mask_size
, src_param
.param_str
);
1396 shader_addline(arg
->buffer
, "exp2(%s));\n", src_param
.param_str
);
1401 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1402 void shader_glsl_rcp(SHADER_OPCODE_ARG
* arg
) {
1403 glsl_src_param_t src_param
;
1407 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1408 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1409 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
1411 if (mask_size
> 1) {
1412 shader_addline(arg
->buffer
, "vec%d(1.0 / %s));\n", mask_size
, src_param
.param_str
);
1414 shader_addline(arg
->buffer
, "1.0 / %s);\n", src_param
.param_str
);
1418 void shader_glsl_rsq(SHADER_OPCODE_ARG
* arg
) {
1419 SHADER_BUFFER
* buffer
= arg
->buffer
;
1420 glsl_src_param_t src_param
;
1424 write_mask
= shader_glsl_append_dst(buffer
, arg
);
1425 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1427 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
1429 if (mask_size
> 1) {
1430 shader_addline(buffer
, "vec%d(inversesqrt(%s)));\n", mask_size
, src_param
.param_str
);
1432 shader_addline(buffer
, "inversesqrt(%s));\n", src_param
.param_str
);
1436 /** Process signed comparison opcodes in GLSL. */
1437 void shader_glsl_compare(SHADER_OPCODE_ARG
* arg
) {
1438 glsl_src_param_t src0_param
;
1439 glsl_src_param_t src1_param
;
1443 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1444 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1445 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1446 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1448 if (mask_size
> 1) {
1449 const char *compare
;
1451 switch(arg
->opcode
->opcode
) {
1452 case WINED3DSIO_SLT
: compare
= "lessThan"; break;
1453 case WINED3DSIO_SGE
: compare
= "greaterThanEqual"; break;
1454 default: compare
= "";
1455 FIXME("Can't handle opcode %s\n", arg
->opcode
->name
);
1458 shader_addline(arg
->buffer
, "vec%d(%s(%s, %s)));\n", mask_size
, compare
,
1459 src0_param
.param_str
, src1_param
.param_str
);
1461 const char *compare
;
1463 switch(arg
->opcode
->opcode
) {
1464 case WINED3DSIO_SLT
: compare
= "<"; break;
1465 case WINED3DSIO_SGE
: compare
= ">="; break;
1466 default: compare
= "";
1467 FIXME("Can't handle opcode %s\n", arg
->opcode
->name
);
1470 shader_addline(arg
->buffer
, "(%s %s %s) ? 1.0 : 0.0);\n",
1471 src0_param
.param_str
, compare
, src1_param
.param_str
);
1475 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1476 void shader_glsl_cmp(SHADER_OPCODE_ARG
* arg
) {
1477 glsl_src_param_t src0_param
;
1478 glsl_src_param_t src1_param
;
1479 glsl_src_param_t src2_param
;
1480 DWORD write_mask
, cmp_channel
= 0;
1483 /* Cycle through all source0 channels */
1484 for (i
=0; i
<4; i
++) {
1486 /* Find the destination channels which use the current source0 channel */
1487 for (j
=0; j
<4; j
++) {
1488 if ( ((arg
->src
[0] >> (WINED3DSP_SWIZZLE_SHIFT
+ 2*j
)) & 0x3) == i
) {
1489 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
1490 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
1493 write_mask
= shader_glsl_append_dst_ext(arg
->buffer
, arg
, arg
->dst
& (~WINED3DSP_SWIZZLE_MASK
| write_mask
));
1494 if (!write_mask
) continue;
1496 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], cmp_channel
, &src0_param
);
1497 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1498 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1500 shader_addline(arg
->buffer
, "%s >= 0.0 ? %s : %s);\n",
1501 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1505 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
1506 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1507 * the compare is done per component of src0. */
1508 void shader_glsl_cnd(SHADER_OPCODE_ARG
* arg
) {
1509 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
1510 glsl_src_param_t src0_param
;
1511 glsl_src_param_t src1_param
;
1512 glsl_src_param_t src2_param
;
1513 DWORD write_mask
, cmp_channel
= 0;
1516 if (shader
->baseShader
.hex_version
< WINED3DPS_VERSION(1, 4)) {
1517 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1518 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1519 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1520 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1522 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
1523 if(arg
->opcode_token
& WINED3DSI_COISSUE
) {
1524 shader_addline(arg
->buffer
, "%s /* COISSUE! */);\n", src1_param
.param_str
);
1526 shader_addline(arg
->buffer
, "%s > 0.5 ? %s : %s);\n",
1527 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1531 /* Cycle through all source0 channels */
1532 for (i
=0; i
<4; i
++) {
1534 /* Find the destination channels which use the current source0 channel */
1535 for (j
=0; j
<4; j
++) {
1536 if ( ((arg
->src
[0] >> (WINED3DSP_SWIZZLE_SHIFT
+ 2*j
)) & 0x3) == i
) {
1537 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
1538 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
1541 write_mask
= shader_glsl_append_dst_ext(arg
->buffer
, arg
, arg
->dst
& (~WINED3DSP_SWIZZLE_MASK
| write_mask
));
1542 if (!write_mask
) continue;
1544 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], cmp_channel
, &src0_param
);
1545 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1546 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1548 shader_addline(arg
->buffer
, "%s > 0.5 ? %s : %s);\n",
1549 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1553 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1554 void shader_glsl_mad(SHADER_OPCODE_ARG
* arg
) {
1555 glsl_src_param_t src0_param
;
1556 glsl_src_param_t src1_param
;
1557 glsl_src_param_t src2_param
;
1560 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1561 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1562 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1563 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1564 shader_addline(arg
->buffer
, "(%s * %s) + %s);\n",
1565 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1568 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1569 Vertex shaders to GLSL codes */
1570 void shader_glsl_mnxn(SHADER_OPCODE_ARG
* arg
) {
1572 int nComponents
= 0;
1573 SHADER_OPCODE_ARG tmpArg
;
1575 memset(&tmpArg
, 0, sizeof(SHADER_OPCODE_ARG
));
1577 /* Set constants for the temporary argument */
1578 tmpArg
.shader
= arg
->shader
;
1579 tmpArg
.buffer
= arg
->buffer
;
1580 tmpArg
.src
[0] = arg
->src
[0];
1581 tmpArg
.src_addr
[0] = arg
->src_addr
[0];
1582 tmpArg
.src_addr
[1] = arg
->src_addr
[1];
1583 tmpArg
.reg_maps
= arg
->reg_maps
;
1585 switch(arg
->opcode
->opcode
) {
1586 case WINED3DSIO_M4x4
:
1588 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP4
);
1590 case WINED3DSIO_M4x3
:
1592 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP4
);
1594 case WINED3DSIO_M3x4
:
1596 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1598 case WINED3DSIO_M3x3
:
1600 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1602 case WINED3DSIO_M3x2
:
1604 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1610 for (i
= 0; i
< nComponents
; i
++) {
1611 tmpArg
.dst
= ((arg
->dst
) & ~WINED3DSP_WRITEMASK_ALL
)|(WINED3DSP_WRITEMASK_0
<<i
);
1612 tmpArg
.src
[1] = arg
->src
[1]+i
;
1613 shader_glsl_dot(&tmpArg
);
1618 The LRP instruction performs a component-wise linear interpolation
1619 between the second and third operands using the first operand as the
1620 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
1621 This is equivalent to mix(src2, src1, src0);
1623 void shader_glsl_lrp(SHADER_OPCODE_ARG
* arg
) {
1624 glsl_src_param_t src0_param
;
1625 glsl_src_param_t src1_param
;
1626 glsl_src_param_t src2_param
;
1629 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1631 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1632 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1633 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1635 shader_addline(arg
->buffer
, "mix(%s, %s, %s));\n",
1636 src2_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
);
1639 /** Process the WINED3DSIO_LIT instruction in GLSL:
1640 * dst.x = dst.w = 1.0
1641 * dst.y = (src0.x > 0) ? src0.x
1642 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1643 * where src.w is clamped at +- 128
1645 void shader_glsl_lit(SHADER_OPCODE_ARG
* arg
) {
1646 glsl_src_param_t src0_param
;
1647 glsl_src_param_t src1_param
;
1648 glsl_src_param_t src3_param
;
1651 shader_glsl_append_dst(arg
->buffer
, arg
);
1652 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1654 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1655 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_1
, &src1_param
);
1656 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &src3_param
);
1658 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",
1659 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
);
1662 /** Process the WINED3DSIO_DST instruction in GLSL:
1664 * dst.y = src0.x * src0.y
1668 void shader_glsl_dst(SHADER_OPCODE_ARG
* arg
) {
1669 glsl_src_param_t src0y_param
;
1670 glsl_src_param_t src0z_param
;
1671 glsl_src_param_t src1y_param
;
1672 glsl_src_param_t src1w_param
;
1675 shader_glsl_append_dst(arg
->buffer
, arg
);
1676 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1678 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_1
, &src0y_param
);
1679 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_2
, &src0z_param
);
1680 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_1
, &src1y_param
);
1681 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_3
, &src1w_param
);
1683 shader_addline(arg
->buffer
, "vec4(1.0, %s * %s, %s, %s))%s;\n",
1684 src0y_param
.param_str
, src1y_param
.param_str
, src0z_param
.param_str
, src1w_param
.param_str
, dst_mask
);
1687 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1688 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1689 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1691 * dst.x = cos(src0.?)
1692 * dst.y = sin(src0.?)
1696 void shader_glsl_sincos(SHADER_OPCODE_ARG
* arg
) {
1697 glsl_src_param_t src0_param
;
1700 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1701 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1703 switch (write_mask
) {
1704 case WINED3DSP_WRITEMASK_0
:
1705 shader_addline(arg
->buffer
, "cos(%s));\n", src0_param
.param_str
);
1708 case WINED3DSP_WRITEMASK_1
:
1709 shader_addline(arg
->buffer
, "sin(%s));\n", src0_param
.param_str
);
1712 case (WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
):
1713 shader_addline(arg
->buffer
, "vec2(cos(%s), sin(%s)));\n", src0_param
.param_str
, src0_param
.param_str
);
1717 ERR("Write mask should be .x, .y or .xy\n");
1722 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1723 * Start a for() loop where src1.y is the initial value of aL,
1724 * increment aL by src1.z for a total of src1.x iterations.
1725 * Need to use a temporary variable for this operation.
1727 /* FIXME: I don't think nested loops will work correctly this way. */
1728 void shader_glsl_loop(SHADER_OPCODE_ARG
* arg
) {
1729 glsl_src_param_t src1_param
;
1731 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_ALL
, &src1_param
);
1733 shader_addline(arg
->buffer
, "for (tmpInt = 0, aL = %s.y; tmpInt < %s.x; tmpInt++, aL += %s.z) {\n",
1734 src1_param
.reg_name
, src1_param
.reg_name
, src1_param
.reg_name
);
1737 void shader_glsl_end(SHADER_OPCODE_ARG
* arg
) {
1738 shader_addline(arg
->buffer
, "}\n");
1741 void shader_glsl_rep(SHADER_OPCODE_ARG
* arg
) {
1742 glsl_src_param_t src0_param
;
1744 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1745 shader_addline(arg
->buffer
, "for (tmpInt = 0; tmpInt < %s; tmpInt++) {\n", src0_param
.param_str
);
1748 void shader_glsl_if(SHADER_OPCODE_ARG
* arg
) {
1749 glsl_src_param_t src0_param
;
1751 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1752 shader_addline(arg
->buffer
, "if (%s) {\n", src0_param
.param_str
);
1755 void shader_glsl_ifc(SHADER_OPCODE_ARG
* arg
) {
1756 glsl_src_param_t src0_param
;
1757 glsl_src_param_t src1_param
;
1759 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1760 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
1762 shader_addline(arg
->buffer
, "if (%s %s %s) {\n",
1763 src0_param
.param_str
, shader_get_comp_op(arg
->opcode_token
), src1_param
.param_str
);
1766 void shader_glsl_else(SHADER_OPCODE_ARG
* arg
) {
1767 shader_addline(arg
->buffer
, "} else {\n");
1770 void shader_glsl_break(SHADER_OPCODE_ARG
* arg
) {
1771 shader_addline(arg
->buffer
, "break;\n");
1774 /* FIXME: According to MSDN the compare is done per component. */
1775 void shader_glsl_breakc(SHADER_OPCODE_ARG
* arg
) {
1776 glsl_src_param_t src0_param
;
1777 glsl_src_param_t src1_param
;
1779 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1780 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
1782 shader_addline(arg
->buffer
, "if (%s %s %s) break;\n",
1783 src0_param
.param_str
, shader_get_comp_op(arg
->opcode_token
), src1_param
.param_str
);
1786 void shader_glsl_label(SHADER_OPCODE_ARG
* arg
) {
1788 DWORD snum
= (arg
->src
[0]) & WINED3DSP_REGNUM_MASK
;
1789 shader_addline(arg
->buffer
, "}\n");
1790 shader_addline(arg
->buffer
, "void subroutine%lu () {\n", snum
);
1793 void shader_glsl_call(SHADER_OPCODE_ARG
* arg
) {
1794 DWORD snum
= (arg
->src
[0]) & WINED3DSP_REGNUM_MASK
;
1795 shader_addline(arg
->buffer
, "subroutine%lu();\n", snum
);
1798 void shader_glsl_callnz(SHADER_OPCODE_ARG
* arg
) {
1799 glsl_src_param_t src1_param
;
1801 DWORD snum
= (arg
->src
[0]) & WINED3DSP_REGNUM_MASK
;
1802 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
1803 shader_addline(arg
->buffer
, "if (%s) subroutine%lu();\n", src1_param
.param_str
, snum
);
1806 /*********************************************
1807 * Pixel Shader Specific Code begins here
1808 ********************************************/
1809 void pshader_glsl_tex(SHADER_OPCODE_ARG
* arg
) {
1810 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1811 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1812 DWORD hex_version
= This
->baseShader
.hex_version
;
1813 char dst_swizzle
[6];
1814 glsl_sample_function_t sample_function
;
1820 /* All versions have a destination register */
1821 shader_glsl_append_dst(arg
->buffer
, arg
);
1823 /* 1.0-1.4: Use destination register as sampler source.
1824 * 2.0+: Use provided sampler source. */
1825 if (hex_version
< WINED3DPS_VERSION(1,4)) {
1828 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1829 flags
= deviceImpl
->stateBlock
->textureState
[sampler_idx
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
1831 if (flags
& WINED3DTTFF_PROJECTED
) {
1833 switch (flags
& ~WINED3DTTFF_PROJECTED
) {
1834 case WINED3DTTFF_COUNT1
: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
1835 case WINED3DTTFF_COUNT2
: mask
= WINED3DSP_WRITEMASK_1
; break;
1836 case WINED3DTTFF_COUNT3
: mask
= WINED3DSP_WRITEMASK_2
; break;
1837 case WINED3DTTFF_COUNT4
:
1838 case WINED3DTTFF_DISABLE
: mask
= WINED3DSP_WRITEMASK_3
; break;
1843 } else if (hex_version
< WINED3DPS_VERSION(2,0)) {
1844 DWORD src_mod
= arg
->src
[0] & WINED3DSP_SRCMOD_MASK
;
1845 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1847 if (src_mod
== WINED3DSPSM_DZ
) {
1849 mask
= WINED3DSP_WRITEMASK_2
;
1850 } else if (src_mod
== WINED3DSPSM_DW
) {
1852 mask
= WINED3DSP_WRITEMASK_3
;
1857 sampler_idx
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
1858 if(arg
->opcode_token
& WINED3DSI_TEXLD_PROJECT
) {
1859 /* ps 2.0 texldp instruction always divides by the fourth component. */
1861 mask
= WINED3DSP_WRITEMASK_3
;
1867 sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
1868 shader_glsl_get_sample_function(sampler_type
, projected
, &sample_function
);
1869 mask
|= sample_function
.coord_mask
;
1871 if (hex_version
< WINED3DPS_VERSION(2,0)) {
1872 shader_glsl_get_write_mask(arg
->dst
, dst_swizzle
);
1874 shader_glsl_get_swizzle(arg
->src
[1], FALSE
, arg
->dst
, dst_swizzle
);
1877 /* 1.0-1.3: Use destination register as coordinate source.
1878 1.4+: Use provided coordinate source register. */
1879 if (hex_version
< WINED3DPS_VERSION(1,4)) {
1881 shader_glsl_get_write_mask(mask
, coord_mask
);
1882 shader_addline(arg
->buffer
, "%s(Psampler%u, T%u%s)%s);\n",
1883 sample_function
.name
, sampler_idx
, sampler_idx
, coord_mask
, dst_swizzle
);
1885 glsl_src_param_t coord_param
;
1886 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], mask
, &coord_param
);
1887 if(arg
->opcode_token
& WINED3DSI_TEXLD_BIAS
) {
1888 glsl_src_param_t bias
;
1889 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &bias
);
1891 shader_addline(arg
->buffer
, "%s(Psampler%u, %s, %s)%s);\n",
1892 sample_function
.name
, sampler_idx
, coord_param
.param_str
,
1893 bias
.param_str
, dst_swizzle
);
1895 shader_addline(arg
->buffer
, "%s(Psampler%u, %s)%s);\n",
1896 sample_function
.name
, sampler_idx
, coord_param
.param_str
, dst_swizzle
);
1901 void shader_glsl_texldl(SHADER_OPCODE_ARG
* arg
) {
1902 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*)arg
->shader
;
1903 glsl_sample_function_t sample_function
;
1904 glsl_src_param_t coord_param
, lod_param
;
1905 char dst_swizzle
[6];
1909 shader_glsl_append_dst(arg
->buffer
, arg
);
1910 shader_glsl_get_swizzle(arg
->src
[1], FALSE
, arg
->dst
, dst_swizzle
);
1912 sampler_idx
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
1913 sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
1914 shader_glsl_get_sample_function(sampler_type
, FALSE
, &sample_function
);
1915 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], sample_function
.coord_mask
, &coord_param
);
1917 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
1919 if (shader_is_pshader_version(This
->baseShader
.hex_version
)) {
1920 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
1921 * However, they seem to work just fine in fragment shaders as well. */
1922 WARN("Using %sLod in fragment shader.\n", sample_function
.name
);
1923 shader_addline(arg
->buffer
, "%sLod(Psampler%u, %s, %s)%s);\n",
1924 sample_function
.name
, sampler_idx
, coord_param
.param_str
, lod_param
.param_str
, dst_swizzle
);
1926 shader_addline(arg
->buffer
, "%sLod(Vsampler%u, %s, %s)%s);\n",
1927 sample_function
.name
, sampler_idx
, coord_param
.param_str
, lod_param
.param_str
, dst_swizzle
);
1931 void pshader_glsl_texcoord(SHADER_OPCODE_ARG
* arg
) {
1933 /* FIXME: Make this work for more than just 2D textures */
1935 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
1936 SHADER_BUFFER
* buffer
= arg
->buffer
;
1937 DWORD hex_version
= This
->baseShader
.hex_version
;
1941 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1942 shader_glsl_get_write_mask(write_mask
, dst_mask
);
1944 if (hex_version
!= WINED3DPS_VERSION(1,4)) {
1945 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1946 shader_addline(buffer
, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n", reg
, dst_mask
);
1948 DWORD reg
= arg
->src
[0] & WINED3DSP_REGNUM_MASK
;
1949 DWORD src_mod
= arg
->src
[0] & WINED3DSP_SRCMOD_MASK
;
1950 char dst_swizzle
[6];
1952 shader_glsl_get_swizzle(arg
->src
[0], FALSE
, write_mask
, dst_swizzle
);
1954 if (src_mod
== WINED3DSPSM_DZ
) {
1955 glsl_src_param_t div_param
;
1956 size_t mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1957 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_2
, &div_param
);
1959 if (mask_size
> 1) {
1960 shader_addline(buffer
, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
1962 shader_addline(buffer
, "gl_TexCoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
1964 } else if (src_mod
== WINED3DSPSM_DW
) {
1965 glsl_src_param_t div_param
;
1966 size_t mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1967 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &div_param
);
1969 if (mask_size
> 1) {
1970 shader_addline(buffer
, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
1972 shader_addline(buffer
, "gl_TexCoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
1975 shader_addline(buffer
, "gl_TexCoord[%u]%s);\n", reg
, dst_swizzle
);
1980 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
1981 * Take a 3-component dot product of the TexCoord[dstreg] and src,
1982 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
1983 void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG
* arg
) {
1984 glsl_src_param_t src0_param
;
1986 glsl_sample_function_t sample_function
;
1987 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1988 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1989 DWORD sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
1991 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
1993 shader_glsl_append_dst(arg
->buffer
, arg
);
1994 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1996 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
1997 * scalar, and projected sampling would require 4
1999 shader_glsl_get_sample_function(sampler_type
, FALSE
, &sample_function
);
2001 switch(count_bits(sample_function
.coord_mask
)) {
2003 shader_addline(arg
->buffer
, "%s(Psampler%u, dot(gl_TexCoord[%u].xyz, %s))%s);\n",
2004 sample_function
.name
, sampler_idx
, sampler_idx
, src0_param
.param_str
, dst_mask
);
2008 shader_addline(arg
->buffer
, "%s(Psampler%u, vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0))%s);\n",
2009 sample_function
.name
, sampler_idx
, sampler_idx
, src0_param
.param_str
, dst_mask
);
2013 shader_addline(arg
->buffer
, "%s(Psampler%u, vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0))%s);\n",
2014 sample_function
.name
, sampler_idx
, sampler_idx
, src0_param
.param_str
, dst_mask
);
2017 FIXME("Unexpected mask bitcount %d\n", count_bits(sample_function
.coord_mask
));
2021 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2022 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2023 void pshader_glsl_texdp3(SHADER_OPCODE_ARG
* arg
) {
2024 glsl_src_param_t src0_param
;
2025 DWORD dstreg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2026 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2030 dst_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
2031 mask_size
= shader_glsl_get_write_mask_size(dst_mask
);
2032 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2034 if (mask_size
> 1) {
2035 shader_addline(arg
->buffer
, "vec%d(dot(T%u.xyz, %s)));\n", mask_size
, dstreg
, src0_param
.param_str
);
2037 shader_addline(arg
->buffer
, "dot(T%u.xyz, %s));\n", dstreg
, src0_param
.param_str
);
2041 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2042 * Calculate the depth as dst.x / dst.y */
2043 void pshader_glsl_texdepth(SHADER_OPCODE_ARG
* arg
) {
2044 glsl_dst_param_t dst_param
;
2046 shader_glsl_add_dst_param(arg
, arg
->dst
, 0, &dst_param
);
2048 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2049 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2050 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2051 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2054 shader_addline(arg
->buffer
, "gl_FragDepth = (%s.y == 0.0) ? 1.0 : clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n", dst_param
.reg_name
, dst_param
.reg_name
, dst_param
.reg_name
);
2057 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2058 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2059 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2060 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2062 void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG
* arg
) {
2063 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2064 DWORD dstreg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2065 glsl_src_param_t src0_param
;
2067 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2069 shader_addline(arg
->buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg
, src0_param
.param_str
);
2070 shader_addline(arg
->buffer
, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2073 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2074 * Calculate the 1st of a 2-row matrix multiplication. */
2075 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG
* arg
) {
2076 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2077 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2078 SHADER_BUFFER
* buffer
= arg
->buffer
;
2079 glsl_src_param_t src0_param
;
2081 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2082 shader_addline(buffer
, "tmp0.x = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2085 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2086 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2087 void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG
* arg
) {
2089 IWineD3DPixelShaderImpl
* shader
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2090 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2091 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2092 SHADER_BUFFER
* buffer
= arg
->buffer
;
2093 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
2094 glsl_src_param_t src0_param
;
2096 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2097 shader_addline(buffer
, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state
->current_row
, reg
, src0_param
.param_str
);
2098 current_state
->texcoord_w
[current_state
->current_row
++] = reg
;
2101 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG
* arg
) {
2102 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2103 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2104 SHADER_BUFFER
* buffer
= arg
->buffer
;
2105 glsl_src_param_t src0_param
;
2108 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2109 shader_addline(buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2111 shader_glsl_append_dst(buffer
, arg
);
2112 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2114 /* Sample the texture using the calculated coordinates */
2115 shader_addline(buffer
, "texture2D(Psampler%u, tmp0.xy)%s);\n", reg
, dst_mask
);
2118 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2119 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2120 void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG
* arg
) {
2121 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2122 glsl_src_param_t src0_param
;
2124 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2125 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2126 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
2127 DWORD sampler_type
= arg
->reg_maps
->samplers
[reg
] & WINED3DSP_TEXTURETYPE_MASK
;
2128 glsl_sample_function_t sample_function
;
2130 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2131 shader_addline(arg
->buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2133 shader_glsl_append_dst(arg
->buffer
, arg
);
2134 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2135 shader_glsl_get_sample_function(sampler_type
, FALSE
, &sample_function
);
2137 /* Sample the texture using the calculated coordinates */
2138 shader_addline(arg
->buffer
, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function
.name
, reg
, dst_mask
);
2140 current_state
->current_row
= 0;
2143 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2144 * Perform the 3rd row of a 3x3 matrix multiply */
2145 void pshader_glsl_texm3x3(SHADER_OPCODE_ARG
* arg
) {
2146 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2147 glsl_src_param_t src0_param
;
2149 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2150 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2151 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
2153 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2155 shader_glsl_append_dst(arg
->buffer
, arg
);
2156 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2157 shader_addline(arg
->buffer
, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg
, src0_param
.param_str
, dst_mask
);
2159 current_state
->current_row
= 0;
2162 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
2163 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2164 void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG
* arg
) {
2166 IWineD3DPixelShaderImpl
* shader
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2167 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2168 glsl_src_param_t src0_param
;
2169 glsl_src_param_t src1_param
;
2171 SHADER_BUFFER
* buffer
= arg
->buffer
;
2172 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
2173 DWORD stype
= arg
->reg_maps
->samplers
[reg
] & WINED3DSP_TEXTURETYPE_MASK
;
2174 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2175 glsl_sample_function_t sample_function
;
2177 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2178 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], src_mask
, &src1_param
);
2180 /* Perform the last matrix multiply operation */
2181 shader_addline(buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2182 /* Reflection calculation */
2183 shader_addline(buffer
, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param
.param_str
);
2185 shader_glsl_append_dst(buffer
, arg
);
2186 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2187 shader_glsl_get_sample_function(stype
, FALSE
, &sample_function
);
2189 /* Sample the texture */
2190 shader_addline(buffer
, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function
.name
, reg
, dst_mask
);
2192 current_state
->current_row
= 0;
2195 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
2196 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2197 void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG
* arg
) {
2199 IWineD3DPixelShaderImpl
* shader
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2200 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2201 SHADER_BUFFER
* buffer
= arg
->buffer
;
2202 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
2203 glsl_src_param_t src0_param
;
2205 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2206 DWORD sampler_type
= arg
->reg_maps
->samplers
[reg
] & WINED3DSP_TEXTURETYPE_MASK
;
2207 glsl_sample_function_t sample_function
;
2209 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2211 /* Perform the last matrix multiply operation */
2212 shader_addline(buffer
, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg
, src0_param
.param_str
);
2214 /* Construct the eye-ray vector from w coordinates */
2215 shader_addline(buffer
, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
2216 current_state
->texcoord_w
[0], current_state
->texcoord_w
[1], reg
);
2217 shader_addline(buffer
, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
2219 shader_glsl_append_dst(buffer
, arg
);
2220 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2221 shader_glsl_get_sample_function(sampler_type
, FALSE
, &sample_function
);
2223 /* Sample the texture using the calculated coordinates */
2224 shader_addline(buffer
, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function
.name
, reg
, dst_mask
);
2226 current_state
->current_row
= 0;
2229 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
2230 * Apply a fake bump map transform.
2231 * texbem is pshader <= 1.3 only, this saves a few version checks
2233 void pshader_glsl_texbem(SHADER_OPCODE_ARG
* arg
) {
2234 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2235 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
2236 char dst_swizzle
[6];
2237 glsl_sample_function_t sample_function
;
2238 glsl_src_param_t coord_param
;
2245 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2246 flags
= deviceImpl
->stateBlock
->textureState
[sampler_idx
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
2248 sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
2249 shader_glsl_get_sample_function(sampler_type
, FALSE
, &sample_function
);
2250 mask
= sample_function
.coord_mask
;
2252 shader_glsl_get_write_mask(arg
->dst
, dst_swizzle
);
2254 shader_glsl_get_write_mask(mask
, coord_mask
);
2256 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2257 * so we can't let the GL handle this.
2259 if (flags
& WINED3DTTFF_PROJECTED
) {
2261 char coord_div_mask
[3];
2262 switch (flags
& ~WINED3DTTFF_PROJECTED
) {
2263 case WINED3DTTFF_COUNT1
: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2264 case WINED3DTTFF_COUNT2
: div_mask
= WINED3DSP_WRITEMASK_1
; break;
2265 case WINED3DTTFF_COUNT3
: div_mask
= WINED3DSP_WRITEMASK_2
; break;
2266 case WINED3DTTFF_COUNT4
:
2267 case WINED3DTTFF_DISABLE
: div_mask
= WINED3DSP_WRITEMASK_3
; break;
2269 shader_glsl_get_write_mask(div_mask
, coord_div_mask
);
2270 shader_addline(arg
->buffer
, "T%u%s /= T%u%s;\n", sampler_idx
, coord_mask
, sampler_idx
, coord_div_mask
);
2273 shader_glsl_append_dst(arg
->buffer
, arg
);
2274 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
|WINED3DSP_WRITEMASK_1
, &coord_param
);
2275 if(arg
->opcode
->opcode
== WINED3DSIO_TEXBEML
) {
2276 glsl_src_param_t luminance_param
;
2277 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_2
, &luminance_param
);
2278 shader_addline(arg
->buffer
, "(%s(Psampler%u, T%u%s + vec4(bumpenvmat * %s, 0.0, 0.0)%s )*(%s * luminancescale + luminanceoffset))%s);\n",
2279 sample_function
.name
, sampler_idx
, sampler_idx
, coord_mask
, coord_param
.param_str
, coord_mask
,
2280 luminance_param
.param_str
, dst_swizzle
);
2282 shader_addline(arg
->buffer
, "%s(Psampler%u, T%u%s + vec4(bumpenvmat * %s, 0.0, 0.0)%s )%s);\n",
2283 sample_function
.name
, sampler_idx
, sampler_idx
, coord_mask
, coord_param
.param_str
, coord_mask
, dst_swizzle
);
2287 void pshader_glsl_bem(SHADER_OPCODE_ARG
* arg
) {
2288 glsl_src_param_t src0_param
, src1_param
;
2290 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
|WINED3DSP_WRITEMASK_1
, &src0_param
);
2291 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
|WINED3DSP_WRITEMASK_1
, &src1_param
);
2293 shader_glsl_append_dst(arg
->buffer
, arg
);
2294 shader_addline(arg
->buffer
, "%s + bumpenvmat * %s);\n",
2295 src0_param
.param_str
, src1_param
.param_str
);
2298 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
2299 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
2300 void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG
* arg
) {
2302 glsl_src_param_t src0_param
;
2303 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2306 shader_glsl_append_dst(arg
->buffer
, arg
);
2307 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2308 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
2310 shader_addline(arg
->buffer
, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx
, src0_param
.reg_name
, dst_mask
);
2313 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
2314 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
2315 void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG
* arg
) {
2316 glsl_src_param_t src0_param
;
2317 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2320 shader_glsl_append_dst(arg
->buffer
, arg
);
2321 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2322 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
2324 shader_addline(arg
->buffer
, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx
, src0_param
.reg_name
, dst_mask
);
2327 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
2328 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
2329 void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG
* arg
) {
2330 glsl_src_param_t src0_param
;
2332 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2333 DWORD sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
2334 glsl_sample_function_t sample_function
;
2336 shader_glsl_append_dst(arg
->buffer
, arg
);
2337 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2338 shader_glsl_get_sample_function(sampler_type
, FALSE
, &sample_function
);
2339 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], sample_function
.coord_mask
, &src0_param
);
2341 shader_addline(arg
->buffer
, "%s(Psampler%u, %s)%s);\n", sample_function
.name
, sampler_idx
, src0_param
.param_str
, dst_mask
);
2344 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
2345 * If any of the first 3 components are < 0, discard this pixel */
2346 void pshader_glsl_texkill(SHADER_OPCODE_ARG
* arg
) {
2347 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2348 DWORD hex_version
= This
->baseShader
.hex_version
;
2349 glsl_dst_param_t dst_param
;
2351 /* The argument is a destination parameter, and no writemasks are allowed */
2352 shader_glsl_add_dst_param(arg
, arg
->dst
, 0, &dst_param
);
2353 if((hex_version
>= WINED3DPS_VERSION(2,0))) {
2354 /* 2.0 shaders compare all 4 components in texkill */
2355 shader_addline(arg
->buffer
, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param
.reg_name
);
2357 /* 1.X shaders only compare the first 3 components, propably due to the nature of the texkill
2358 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
2359 * 4 components are defined, only the first 3 are used
2361 shader_addline(arg
->buffer
, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param
.reg_name
);
2365 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
2366 * dst = dot2(src0, src1) + src2 */
2367 void pshader_glsl_dp2add(SHADER_OPCODE_ARG
* arg
) {
2368 glsl_src_param_t src0_param
;
2369 glsl_src_param_t src1_param
;
2370 glsl_src_param_t src2_param
;
2374 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
2375 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2377 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
2378 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
2379 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], WINED3DSP_WRITEMASK_0
, &src2_param
);
2381 shader_addline(arg
->buffer
, "dot(%s, %s) + %s);\n", src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2384 void pshader_glsl_input_pack(
2385 SHADER_BUFFER
* buffer
,
2386 semantic
* semantics_in
) {
2390 for (i
= 0; i
< MAX_REG_INPUT
; i
++) {
2392 DWORD usage_token
= semantics_in
[i
].usage
;
2393 DWORD register_token
= semantics_in
[i
].reg
;
2394 DWORD usage
, usage_idx
;
2398 if (!usage_token
) continue;
2399 usage
= (usage_token
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
2400 usage_idx
= (usage_token
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
2401 shader_glsl_get_write_mask(register_token
, reg_mask
);
2405 case WINED3DDECLUSAGE_COLOR
:
2407 shader_addline(buffer
, "IN%u%s = vec4(gl_Color)%s;\n",
2408 i
, reg_mask
, reg_mask
);
2409 else if (usage_idx
== 1)
2410 shader_addline(buffer
, "IN%u%s = vec4(gl_SecondaryColor)%s;\n",
2411 i
, reg_mask
, reg_mask
);
2413 shader_addline(buffer
, "IN%u%s = vec4(unsupported_color_input)%s;\n",
2414 i
, reg_mask
, reg_mask
);
2417 case WINED3DDECLUSAGE_TEXCOORD
:
2418 shader_addline(buffer
, "IN%u%s = vec4(gl_TexCoord[%u])%s;\n",
2419 i
, reg_mask
, usage_idx
, reg_mask
);
2422 case WINED3DDECLUSAGE_FOG
:
2423 shader_addline(buffer
, "IN%u%s = vec4(gl_FogFragCoord)%s;\n",
2424 i
, reg_mask
, reg_mask
);
2428 shader_addline(buffer
, "IN%u%s = vec4(unsupported_input)%s;\n",
2429 i
, reg_mask
, reg_mask
);
2434 /*********************************************
2435 * Vertex Shader Specific Code begins here
2436 ********************************************/
2438 void vshader_glsl_output_unpack(
2439 SHADER_BUFFER
* buffer
,
2440 semantic
* semantics_out
) {
2444 for (i
= 0; i
< MAX_REG_OUTPUT
; i
++) {
2446 DWORD usage_token
= semantics_out
[i
].usage
;
2447 DWORD register_token
= semantics_out
[i
].reg
;
2448 DWORD usage
, usage_idx
;
2452 if (!usage_token
) continue;
2454 usage
= (usage_token
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
2455 usage_idx
= (usage_token
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
2456 shader_glsl_get_write_mask(register_token
, reg_mask
);
2460 case WINED3DDECLUSAGE_COLOR
:
2462 shader_addline(buffer
, "gl_FrontColor%s = OUT%u%s;\n", reg_mask
, i
, reg_mask
);
2463 else if (usage_idx
== 1)
2464 shader_addline(buffer
, "gl_FrontSecondaryColor%s = OUT%u%s;\n", reg_mask
, i
, reg_mask
);
2466 shader_addline(buffer
, "unsupported_color_output%s = OUT%u%s;\n", reg_mask
, i
, reg_mask
);
2469 case WINED3DDECLUSAGE_POSITION
:
2470 shader_addline(buffer
, "gl_Position%s = OUT%u%s;\n", reg_mask
, i
, reg_mask
);
2473 case WINED3DDECLUSAGE_TEXCOORD
:
2474 shader_addline(buffer
, "gl_TexCoord[%u]%s = OUT%u%s;\n",
2475 usage_idx
, reg_mask
, i
, reg_mask
);
2478 case WINED3DDECLUSAGE_PSIZE
:
2479 shader_addline(buffer
, "gl_PointSize = OUT%u.x;\n", i
);
2482 case WINED3DDECLUSAGE_FOG
:
2483 shader_addline(buffer
, "gl_FogFragCoord = OUT%u%s;\n", i
, reg_mask
);
2487 shader_addline(buffer
, "unsupported_output%s = OUT%u%s;\n", reg_mask
, i
, reg_mask
);
2492 static void add_glsl_program_entry(IWineD3DDeviceImpl
*device
, struct glsl_shader_prog_link
*entry
) {
2493 glsl_program_key_t
*key
;
2495 key
= HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t
));
2496 key
->vshader
= entry
->vshader
;
2497 key
->pshader
= entry
->pshader
;
2499 hash_table_put(device
->glsl_program_lookup
, key
, entry
);
2502 static struct glsl_shader_prog_link
*get_glsl_program_entry(IWineD3DDeviceImpl
*device
,
2503 GLhandleARB vshader
, GLhandleARB pshader
) {
2504 glsl_program_key_t key
;
2506 key
.vshader
= vshader
;
2507 key
.pshader
= pshader
;
2509 return (struct glsl_shader_prog_link
*)hash_table_get(device
->glsl_program_lookup
, &key
);
2512 void delete_glsl_program_entry(IWineD3DDevice
*iface
, struct glsl_shader_prog_link
*entry
) {
2513 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
2514 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
2515 glsl_program_key_t
*key
;
2517 key
= HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t
));
2518 key
->vshader
= entry
->vshader
;
2519 key
->pshader
= entry
->pshader
;
2520 hash_table_remove(This
->glsl_program_lookup
, key
);
2522 GL_EXTCALL(glDeleteObjectARB(entry
->programId
));
2523 if (entry
->vshader
) list_remove(&entry
->vshader_entry
);
2524 if (entry
->pshader
) list_remove(&entry
->pshader_entry
);
2525 HeapFree(GetProcessHeap(), 0, entry
->vuniformF_locations
);
2526 HeapFree(GetProcessHeap(), 0, entry
->puniformF_locations
);
2527 HeapFree(GetProcessHeap(), 0, entry
);
2530 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
2531 * It sets the programId on the current StateBlock (because it should be called
2532 * inside of the DrawPrimitive() part of the render loop).
2534 * If a program for the given combination does not exist, create one, and store
2535 * the program in the hash table. If it creates a program, it will link the
2536 * given objects, too.
2538 static void set_glsl_shader_program(IWineD3DDevice
*iface
, BOOL use_ps
, BOOL use_vs
) {
2539 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
2540 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
2541 IWineD3DPixelShader
*pshader
= This
->stateBlock
->pixelShader
;
2542 IWineD3DVertexShader
*vshader
= This
->stateBlock
->vertexShader
;
2543 struct glsl_shader_prog_link
*entry
= NULL
;
2544 GLhandleARB programId
= 0;
2548 GLhandleARB vshader_id
= use_vs
? ((IWineD3DBaseShaderImpl
*)vshader
)->baseShader
.prgId
: 0;
2549 GLhandleARB pshader_id
= use_ps
? ((IWineD3DBaseShaderImpl
*)pshader
)->baseShader
.prgId
: 0;
2550 entry
= get_glsl_program_entry(This
, vshader_id
, pshader_id
);
2552 This
->stateBlock
->glsl_program
= entry
;
2556 /* If we get to this point, then no matching program exists, so we create one */
2557 programId
= GL_EXTCALL(glCreateProgramObjectARB());
2558 TRACE("Created new GLSL shader program %u\n", programId
);
2560 /* Create the entry */
2561 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link
));
2562 entry
->programId
= programId
;
2563 entry
->vshader
= vshader_id
;
2564 entry
->pshader
= pshader_id
;
2565 /* Add the hash table entry */
2566 add_glsl_program_entry(This
, entry
);
2568 /* Set the current program */
2569 This
->stateBlock
->glsl_program
= entry
;
2571 /* Attach GLSL vshader */
2573 int max_attribs
= 16; /* TODO: Will this always be the case? It is at the moment... */
2576 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id
, programId
);
2577 GL_EXTCALL(glAttachObjectARB(programId
, vshader_id
));
2578 checkGLcall("glAttachObjectARB");
2580 /* Bind vertex attributes to a corresponding index number to match
2581 * the same index numbers as ARB_vertex_programs (makes loading
2582 * vertex attributes simpler). With this method, we can use the
2583 * exact same code to load the attributes later for both ARB and
2586 * We have to do this here because we need to know the Program ID
2587 * in order to make the bindings work, and it has to be done prior
2588 * to linking the GLSL program. */
2589 for (i
= 0; i
< max_attribs
; ++i
) {
2590 snprintf(tmp_name
, sizeof(tmp_name
), "attrib%i", i
);
2591 GL_EXTCALL(glBindAttribLocationARB(programId
, i
, tmp_name
));
2593 checkGLcall("glBindAttribLocationARB");
2595 list_add_head(&((IWineD3DBaseShaderImpl
*)vshader
)->baseShader
.linked_programs
, &entry
->vshader_entry
);
2598 /* Attach GLSL pshader */
2600 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id
, programId
);
2601 GL_EXTCALL(glAttachObjectARB(programId
, pshader_id
));
2602 checkGLcall("glAttachObjectARB");
2604 list_add_head(&((IWineD3DBaseShaderImpl
*)pshader
)->baseShader
.linked_programs
, &entry
->pshader_entry
);
2607 /* Link the program */
2608 TRACE("Linking GLSL shader program %u\n", programId
);
2609 GL_EXTCALL(glLinkProgramARB(programId
));
2610 print_glsl_info_log(&GLINFO_LOCATION
, programId
);
2612 entry
->vuniformF_locations
= HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB
) * GL_LIMITS(vshader_constantsF
));
2613 for (i
= 0; i
< GL_LIMITS(vshader_constantsF
); ++i
) {
2614 snprintf(glsl_name
, sizeof(glsl_name
), "VC[%i]", i
);
2615 entry
->vuniformF_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
2617 entry
->puniformF_locations
= HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB
) * GL_LIMITS(pshader_constantsF
));
2618 for (i
= 0; i
< GL_LIMITS(pshader_constantsF
); ++i
) {
2619 snprintf(glsl_name
, sizeof(glsl_name
), "PC[%i]", i
);
2620 entry
->puniformF_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
2624 static GLhandleARB
create_glsl_blt_shader(WineD3D_GL_Info
*gl_info
) {
2625 GLhandleARB program_id
;
2626 GLhandleARB vshader_id
, pshader_id
;
2627 const char *blt_vshader
[] = {
2630 " gl_Position = gl_Vertex;\n"
2631 " gl_FrontColor = vec4(1.0);\n"
2632 " gl_TexCoord[0].x = (gl_Vertex.x * 0.5) + 0.5;\n"
2633 " gl_TexCoord[0].y = (-gl_Vertex.y * 0.5) + 0.5;\n"
2637 const char *blt_pshader
[] = {
2638 "uniform sampler2D sampler;\n"
2641 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
2645 vshader_id
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
2646 GL_EXTCALL(glShaderSourceARB(vshader_id
, 1, blt_vshader
, NULL
));
2647 GL_EXTCALL(glCompileShaderARB(vshader_id
));
2649 pshader_id
= GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB
));
2650 GL_EXTCALL(glShaderSourceARB(pshader_id
, 1, blt_pshader
, NULL
));
2651 GL_EXTCALL(glCompileShaderARB(pshader_id
));
2653 program_id
= GL_EXTCALL(glCreateProgramObjectARB());
2654 GL_EXTCALL(glAttachObjectARB(program_id
, vshader_id
));
2655 GL_EXTCALL(glAttachObjectARB(program_id
, pshader_id
));
2656 GL_EXTCALL(glLinkProgramARB(program_id
));
2658 print_glsl_info_log(&GLINFO_LOCATION
, program_id
);
2663 static void shader_glsl_select(IWineD3DDevice
*iface
, BOOL usePS
, BOOL useVS
) {
2664 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
2665 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
2666 GLhandleARB program_id
= 0;
2668 if (useVS
|| usePS
) set_glsl_shader_program(iface
, usePS
, useVS
);
2669 else This
->stateBlock
->glsl_program
= NULL
;
2671 program_id
= This
->stateBlock
->glsl_program
? This
->stateBlock
->glsl_program
->programId
: 0;
2672 if (program_id
) TRACE("Using GLSL program %u\n", program_id
);
2673 GL_EXTCALL(glUseProgramObjectARB(program_id
));
2674 checkGLcall("glUseProgramObjectARB");
2677 static void shader_glsl_select_depth_blt(IWineD3DDevice
*iface
) {
2678 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
2679 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
2680 static GLhandleARB program_id
= 0;
2681 static GLhandleARB loc
= -1;
2684 program_id
= create_glsl_blt_shader(gl_info
);
2685 loc
= GL_EXTCALL(glGetUniformLocationARB(program_id
, "sampler"));
2688 GL_EXTCALL(glUseProgramObjectARB(program_id
));
2689 GL_EXTCALL(glUniform1iARB(loc
, 0));
2692 static void shader_glsl_cleanup(IWineD3DDevice
*iface
) {
2693 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
2694 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
2695 GL_EXTCALL(glUseProgramObjectARB(0));
2698 const shader_backend_t glsl_shader_backend
= {
2699 &shader_glsl_select
,
2700 &shader_glsl_select_depth_blt
,
2701 &shader_glsl_load_constants
,
2702 &shader_glsl_cleanup
,
2703 &shader_glsl_color_correction