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
,
81 GLhandleARB programId
) {
83 IWineD3DStateBlockImpl
* stateBlock
= (IWineD3DStateBlockImpl
*) iface
;
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
, GLhandleARB programId
) {
105 IWineD3DStateBlockImpl
* stateBlock
= (IWineD3DStateBlockImpl
*) iface
;
106 GLhandleARB name_loc
;
107 char sampler_name
[20];
110 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
) {
111 snprintf(sampler_name
, sizeof(sampler_name
), "Vsampler%d", i
);
112 name_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, sampler_name
));
113 if (name_loc
!= -1) {
114 int mapped_unit
= stateBlock
->wineD3DDevice
->texUnitMap
[MAX_FRAGMENT_SAMPLERS
+ i
];
115 if (mapped_unit
!= -1 && mapped_unit
< GL_LIMITS(combined_samplers
)) {
116 TRACE("Loading %s for texture %d\n", sampler_name
, mapped_unit
);
117 GL_EXTCALL(glUniform1iARB(name_loc
, mapped_unit
));
118 checkGLcall("glUniform1iARB");
120 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name
, mapped_unit
);
127 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
128 * When constant_list == NULL, it will load all the constants.
130 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl
* This
, WineD3D_GL_Info
*gl_info
,
131 unsigned int max_constants
, float* constants
, GLhandleARB
*constant_locations
,
132 struct list
*constant_list
) {
133 constants_entry
*constant
;
134 local_constant
* lconst
;
139 if (TRACE_ON(d3d_shader
)) {
140 LIST_FOR_EACH_ENTRY(constant
, constant_list
, constants_entry
, entry
) {
145 tmp_loc
= constant_locations
[i
];
147 TRACE_(d3d_constants
)("Loading constants %i: %f, %f, %f, %f\n", i
,
148 constants
[i
* 4 + 0], constants
[i
* 4 + 1],
149 constants
[i
* 4 + 2], constants
[i
* 4 + 3]);
155 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
156 if(WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) == 1 &&
157 shader_is_pshader_version(This
->baseShader
.hex_version
)) {
160 LIST_FOR_EACH_ENTRY(constant
, constant_list
, constants_entry
, entry
) {
165 tmp_loc
= constant_locations
[i
];
167 /* We found this uniform name in the program - go ahead and send the data */
169 if(constants
[k
+ 0] < -1.0) lcl_const
[0] = -1.0;
170 else if(constants
[k
+ 0] > 1.0) lcl_const
[0] = 1.0;
171 else lcl_const
[0] = constants
[k
+ 0];
172 if(constants
[k
+ 1] < -1.0) lcl_const
[1] = -1.0;
173 else if(constants
[k
+ 1] > 1.0) lcl_const
[1] = 1.0;
174 else lcl_const
[1] = constants
[k
+ 1];
175 if(constants
[k
+ 2] < -1.0) lcl_const
[2] = -1.0;
176 else if(constants
[k
+ 2] > 1.0) lcl_const
[2] = 1.0;
177 else lcl_const
[2] = constants
[k
+ 2];
178 if(constants
[k
+ 3] < -1.0) lcl_const
[3] = -1.0;
179 else if(constants
[k
+ 3] > 1.0) lcl_const
[3] = 1.0;
180 else lcl_const
[3] = constants
[k
+ 3];
182 GL_EXTCALL(glUniform4fvARB(tmp_loc
, 1, lcl_const
));
187 LIST_FOR_EACH_ENTRY(constant
, constant_list
, constants_entry
, entry
) {
192 tmp_loc
= constant_locations
[i
];
194 /* We found this uniform name in the program - go ahead and send the data */
195 GL_EXTCALL(glUniform4fvARB(tmp_loc
, 1, constants
+ (i
* 4)));
200 checkGLcall("glUniform4fvARB()");
202 if(!This
->baseShader
.load_local_constsF
) {
203 TRACE("No need to load local float constants for this shader\n");
207 /* Load immediate constants */
208 if (TRACE_ON(d3d_shader
)) {
209 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
210 tmp_loc
= constant_locations
[lconst
->idx
];
212 GLfloat
* values
= (GLfloat
*)lconst
->value
;
213 TRACE_(d3d_constants
)("Loading local constants %i: %f, %f, %f, %f\n", lconst
->idx
,
214 values
[0], values
[1], values
[2], values
[3]);
218 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
219 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
220 tmp_loc
= constant_locations
[lconst
->idx
];
222 /* We found this uniform name in the program - go ahead and send the data */
223 GL_EXTCALL(glUniform4fvARB(tmp_loc
, 1, (GLfloat
*)lconst
->value
));
226 checkGLcall("glUniform4fvARB()");
230 * Loads integer constants (aka uniforms) into the currently set GLSL program.
231 * When @constants_set == NULL, it will load all the constants.
233 static void shader_glsl_load_constantsI(
234 IWineD3DBaseShaderImpl
* This
,
235 WineD3D_GL_Info
*gl_info
,
236 GLhandleARB programId
,
237 GLhandleARB locations
[MAX_CONST_I
],
238 unsigned max_constants
,
240 BOOL
* constants_set
) {
245 for (i
=0; i
<max_constants
; ++i
) {
246 if (NULL
== constants_set
|| constants_set
[i
]) {
248 TRACE_(d3d_constants
)("Loading constants %i: %i, %i, %i, %i\n",
249 i
, constants
[i
*4], constants
[i
*4+1], constants
[i
*4+2], constants
[i
*4+3]);
251 /* We found this uniform name in the program - go ahead and send the data */
252 GL_EXTCALL(glUniform4ivARB(locations
[i
], 1, &constants
[i
*4]));
253 checkGLcall("glUniform4ivARB");
257 /* Load immediate constants */
258 ptr
= list_head(&This
->baseShader
.constantsI
);
260 local_constant
* lconst
= LIST_ENTRY(ptr
, struct local_constant
, entry
);
261 unsigned int idx
= lconst
->idx
;
262 GLint
* values
= (GLint
*) lconst
->value
;
264 TRACE_(d3d_constants
)("Loading local constants %i: %i, %i, %i, %i\n", idx
,
265 values
[0], values
[1], values
[2], values
[3]);
267 /* We found this uniform name in the program - go ahead and send the data */
268 GL_EXTCALL(glUniform4ivARB(locations
[idx
], 1, values
));
269 checkGLcall("glUniform4ivARB");
270 ptr
= list_next(&This
->baseShader
.constantsI
, ptr
);
275 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
276 * When @constants_set == NULL, it will load all the constants.
278 static void shader_glsl_load_constantsB(
279 IWineD3DBaseShaderImpl
* This
,
280 WineD3D_GL_Info
*gl_info
,
281 GLhandleARB programId
,
282 unsigned max_constants
,
284 BOOL
* constants_set
) {
289 char is_pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
290 const char* prefix
= is_pshader
? "PB":"VB";
293 for (i
=0; i
<max_constants
; ++i
) {
294 if (NULL
== constants_set
|| constants_set
[i
]) {
296 TRACE_(d3d_constants
)("Loading constants %i: %i;\n", i
, constants
[i
]);
298 /* TODO: Benchmark and see if it would be beneficial to store the
299 * locations of the constants to avoid looking up each time */
300 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, i
);
301 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
303 /* We found this uniform name in the program - go ahead and send the data */
304 GL_EXTCALL(glUniform1ivARB(tmp_loc
, 1, &constants
[i
]));
305 checkGLcall("glUniform1ivARB");
310 /* Load immediate constants */
311 ptr
= list_head(&This
->baseShader
.constantsB
);
313 local_constant
* lconst
= LIST_ENTRY(ptr
, struct local_constant
, entry
);
314 unsigned int idx
= lconst
->idx
;
315 GLint
* values
= (GLint
*) lconst
->value
;
317 TRACE_(d3d_constants
)("Loading local constants %i: %i\n", idx
, values
[0]);
319 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, idx
);
320 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
322 /* We found this uniform name in the program - go ahead and send the data */
323 GL_EXTCALL(glUniform1ivARB(tmp_loc
, 1, values
));
324 checkGLcall("glUniform1ivARB");
326 ptr
= list_next(&This
->baseShader
.constantsB
, ptr
);
333 * Loads the app-supplied constants into the currently set GLSL program.
335 void shader_glsl_load_constants(
336 IWineD3DDevice
* device
,
338 char useVertexShader
) {
340 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) device
;
341 IWineD3DStateBlockImpl
* stateBlock
= deviceImpl
->stateBlock
;
342 WineD3D_GL_Info
*gl_info
= &deviceImpl
->adapter
->gl_info
;
344 GLhandleARB
*constant_locations
;
345 struct list
*constant_list
;
346 GLhandleARB programId
;
347 struct glsl_shader_prog_link
*prog
= stateBlock
->glsl_program
;
350 /* No GLSL program set - nothing to do. */
353 programId
= prog
->programId
;
355 if (useVertexShader
) {
356 IWineD3DBaseShaderImpl
* vshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->vertexShader
;
358 constant_locations
= prog
->vuniformF_locations
;
359 constant_list
= &stateBlock
->set_vconstantsF
;
361 /* Load DirectX 9 float constants/uniforms for vertex shader */
362 shader_glsl_load_constantsF(vshader
, gl_info
, GL_LIMITS(vshader_constantsF
),
363 stateBlock
->vertexShaderConstantF
, constant_locations
, constant_list
);
365 /* Load DirectX 9 integer constants/uniforms for vertex shader */
366 shader_glsl_load_constantsI(vshader
, gl_info
, programId
,
367 prog
->vuniformI_locations
, MAX_CONST_I
,
368 stateBlock
->vertexShaderConstantI
,
369 stateBlock
->changed
.vertexShaderConstantsI
);
371 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
372 shader_glsl_load_constantsB(vshader
, gl_info
, programId
, MAX_CONST_B
,
373 stateBlock
->vertexShaderConstantB
,
374 stateBlock
->changed
.vertexShaderConstantsB
);
376 /* Upload the position fixup params */
377 GL_EXTCALL(glUniform4fvARB(prog
->posFixup_location
, 1, &deviceImpl
->posFixup
[0]));
378 checkGLcall("glUniform4fvARB");
381 if (usePixelShader
) {
383 IWineD3DBaseShaderImpl
* pshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->pixelShader
;
385 constant_locations
= prog
->puniformF_locations
;
386 constant_list
= &stateBlock
->set_pconstantsF
;
388 /* Load DirectX 9 float constants/uniforms for pixel shader */
389 shader_glsl_load_constantsF(pshader
, gl_info
, GL_LIMITS(pshader_constantsF
),
390 stateBlock
->pixelShaderConstantF
, constant_locations
, constant_list
);
392 /* Load DirectX 9 integer constants/uniforms for pixel shader */
393 shader_glsl_load_constantsI(pshader
, gl_info
, programId
,
394 prog
->puniformI_locations
, MAX_CONST_I
,
395 stateBlock
->pixelShaderConstantI
,
396 stateBlock
->changed
.pixelShaderConstantsI
);
398 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
399 shader_glsl_load_constantsB(pshader
, gl_info
, programId
, MAX_CONST_B
,
400 stateBlock
->pixelShaderConstantB
,
401 stateBlock
->changed
.pixelShaderConstantsB
);
403 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
404 * It can't be 0 for a valid texbem instruction.
406 if(((IWineD3DPixelShaderImpl
*) pshader
)->needsbumpmat
!= -1) {
407 float *data
= (float *) &stateBlock
->textureState
[(int) ((IWineD3DPixelShaderImpl
*) pshader
)->needsbumpmat
][WINED3DTSS_BUMPENVMAT00
];
408 GL_EXTCALL(glUniformMatrix2fvARB(prog
->bumpenvmat_location
, 1, 0, data
));
409 checkGLcall("glUniformMatrix2fvARB");
411 /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
412 * is set too, so we can check that in the needsbumpmat check
414 if(((IWineD3DPixelShaderImpl
*) pshader
)->baseShader
.reg_maps
.luminanceparams
!= -1) {
415 int stage
= ((IWineD3DPixelShaderImpl
*) pshader
)->baseShader
.reg_maps
.luminanceparams
;
416 GLfloat
*scale
= (GLfloat
*) &stateBlock
->textureState
[stage
][WINED3DTSS_BUMPENVLSCALE
];
417 GLfloat
*offset
= (GLfloat
*) &stateBlock
->textureState
[stage
][WINED3DTSS_BUMPENVLOFFSET
];
419 GL_EXTCALL(glUniform1fvARB(prog
->luminancescale_location
, 1, scale
));
420 checkGLcall("glUniform1fvARB");
421 GL_EXTCALL(glUniform1fvARB(prog
->luminanceoffset_location
, 1, offset
));
422 checkGLcall("glUniform1fvARB");
424 } else if(((IWineD3DPixelShaderImpl
*) pshader
)->srgb_enabled
&&
425 !((IWineD3DPixelShaderImpl
*) pshader
)->srgb_mode_hardcoded
) {
429 if(stateBlock
->renderState
[WINED3DRS_SRGBWRITEENABLE
]) {
430 comparison
[0] = srgb_cmp
; comparison
[1] = srgb_cmp
;
431 comparison
[2] = srgb_cmp
; comparison
[3] = srgb_cmp
;
433 mul_low
[0] = srgb_mul_low
; mul_low
[1] = srgb_mul_low
;
434 mul_low
[2] = srgb_mul_low
; mul_low
[3] = srgb_mul_low
;
436 comparison
[0] = 1.0 / 0.0; comparison
[1] = 1.0 / 0.0;
437 comparison
[2] = 1.0 / 0.0; comparison
[3] = 1.0 / 0.0;
439 mul_low
[0] = 1.0; mul_low
[1] = 1.0;
440 mul_low
[2] = 1.0; mul_low
[3] = 1.0;
443 GL_EXTCALL(glUniform4fvARB(prog
->srgb_comparison_location
, 1, comparison
));
444 GL_EXTCALL(glUniform4fvARB(prog
->srgb_mul_low_location
, 1, mul_low
));
446 if(((IWineD3DPixelShaderImpl
*) pshader
)->vpos_uniform
) {
447 float correction_params
[4];
448 if(deviceImpl
->render_offscreen
) {
449 correction_params
[0] = 0.0;
450 correction_params
[1] = 1.0;
452 /* position is window relative, not viewport relative */
453 correction_params
[0] = ((IWineD3DSurfaceImpl
*) deviceImpl
->render_targets
[0])->currentDesc
.Height
;
454 correction_params
[1] = -1.0;
456 GL_EXTCALL(glUniform4fvARB(prog
->ycorrection_location
, 1, correction_params
));
461 /** Generate the variable & register declarations for the GLSL output target */
462 void shader_generate_glsl_declarations(
463 IWineD3DBaseShader
*iface
,
464 shader_reg_maps
* reg_maps
,
465 SHADER_BUFFER
* buffer
,
466 WineD3D_GL_Info
* gl_info
) {
468 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
469 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
471 unsigned int extra_constants_needed
= 0;
472 local_constant
* lconst
;
474 /* There are some minor differences between pixel and vertex shaders */
475 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
476 char prefix
= pshader
? 'P' : 'V';
478 /* Prototype the subroutines */
479 for (i
= 0; i
< This
->baseShader
.limits
.label
; i
++) {
480 if (reg_maps
->labels
[i
])
481 shader_addline(buffer
, "void subroutine%u();\n", i
);
484 /* Declare the constants (aka uniforms) */
485 if (This
->baseShader
.limits
.constant_float
> 0) {
486 unsigned max_constantsF
= min(This
->baseShader
.limits
.constant_float
,
487 (pshader
? GL_LIMITS(pshader_constantsF
) : GL_LIMITS(vshader_constantsF
)));
488 shader_addline(buffer
, "uniform vec4 %cC[%u];\n", prefix
, max_constantsF
);
491 if (This
->baseShader
.limits
.constant_int
> 0)
492 shader_addline(buffer
, "uniform ivec4 %cI[%u];\n", prefix
, This
->baseShader
.limits
.constant_int
);
494 if (This
->baseShader
.limits
.constant_bool
> 0)
495 shader_addline(buffer
, "uniform bool %cB[%u];\n", prefix
, This
->baseShader
.limits
.constant_bool
);
498 shader_addline(buffer
, "uniform vec4 posFixup;\n");
499 /* Predeclaration; This function is added at link time based on the pixel shader.
500 * VS 3.0 shaders have an array OUT[] the shader writes to, earlier versions don't have
501 * that. We know the input to the reorder function at vertex shader compile time, so
502 * we can deal with that. The reorder function for a 1.x and 2.x vertex shader can just
503 * read gl_FrontColor. The output depends on the pixel shader. The reorder function for a
504 * 1.x and 2.x pshader or for fixed function will write gl_FrontColor, and for a 3.0 shader
505 * it will write to the varying array. Here we depend on the shader optimizer on sorting that
506 * out. The nvidia driver only does that if the parameter is inout instead of out, hence the
509 if(This
->baseShader
.hex_version
>= WINED3DVS_VERSION(3, 0)) {
510 shader_addline(buffer
, "void order_ps_input(in vec4[%u]);\n", MAX_REG_OUTPUT
);
512 shader_addline(buffer
, "void order_ps_input();\n");
515 IWineD3DPixelShaderImpl
*ps_impl
= (IWineD3DPixelShaderImpl
*) This
;
517 if(reg_maps
->bumpmat
!= -1) {
518 shader_addline(buffer
, "uniform mat2 bumpenvmat;\n");
519 if(reg_maps
->luminanceparams
) {
520 shader_addline(buffer
, "uniform float luminancescale;\n");
521 shader_addline(buffer
, "uniform float luminanceoffset;\n");
522 extra_constants_needed
++;
524 extra_constants_needed
++;
527 if(device
->stateBlock
->renderState
[WINED3DRS_SRGBWRITEENABLE
]) {
528 ps_impl
->srgb_enabled
= 1;
529 if(This
->baseShader
.limits
.constant_float
+ extra_constants_needed
+ 1 < GL_LIMITS(pshader_constantsF
)) {
530 shader_addline(buffer
, "uniform vec4 srgb_mul_low;\n");
531 shader_addline(buffer
, "uniform vec4 srgb_comparison;\n");
532 ps_impl
->srgb_mode_hardcoded
= 0;
533 extra_constants_needed
++;
535 ps_impl
->srgb_mode_hardcoded
= 1;
536 shader_addline(buffer
, "const vec4 srgb_mul_low = vec4(%f, %f, %f, %f);\n",
537 srgb_mul_low
, srgb_mul_low
, srgb_mul_low
, srgb_mul_low
);
538 shader_addline(buffer
, "const vec4 srgb_comparison = vec4(%f, %f, %f, %f);\n",
539 srgb_cmp
, srgb_cmp
, srgb_cmp
, srgb_cmp
);
542 IWineD3DPixelShaderImpl
*ps_impl
= (IWineD3DPixelShaderImpl
*) This
;
544 /* Do not write any srgb fixup into the shader to save shader size and processing time.
545 * As a consequence, we can't toggle srgb write on without recompilation
547 ps_impl
->srgb_enabled
= 0;
548 ps_impl
->srgb_mode_hardcoded
= 1;
550 if(reg_maps
->vpos
|| reg_maps
->usesdsy
) {
551 if(This
->baseShader
.limits
.constant_float
+ extra_constants_needed
+ 1 < GL_LIMITS(pshader_constantsF
)) {
552 shader_addline(buffer
, "uniform vec4 ycorrection;\n");
553 ((IWineD3DPixelShaderImpl
*) This
)->vpos_uniform
= 1;
554 extra_constants_needed
++;
556 /* This happens because we do not have proper tracking of the constant registers that are
557 * actually used, only the max limit of the shader version
559 FIXME("Cannot find a free uniform for vpos correction params\n");
560 shader_addline(buffer
, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
561 device
->render_offscreen
? 0.0 : ((IWineD3DSurfaceImpl
*) device
->render_targets
[0])->currentDesc
.Height
,
562 device
->render_offscreen
? 1.0 : -1.0);
564 shader_addline(buffer
, "vec4 vpos;\n");
568 /* Declare texture samplers */
569 for (i
= 0; i
< This
->baseShader
.limits
.sampler
; i
++) {
570 if (reg_maps
->samplers
[i
]) {
572 DWORD stype
= reg_maps
->samplers
[i
] & WINED3DSP_TEXTURETYPE_MASK
;
576 shader_addline(buffer
, "uniform sampler1D %csampler%u;\n", prefix
, i
);
579 if(device
->stateBlock
->textures
[i
] &&
580 IWineD3DBaseTexture_GetTextureDimensions(device
->stateBlock
->textures
[i
]) == GL_TEXTURE_RECTANGLE_ARB
) {
581 shader_addline(buffer
, "uniform sampler2DRect %csampler%u;\n", prefix
, i
);
583 shader_addline(buffer
, "uniform sampler2D %csampler%u;\n", prefix
, i
);
586 case WINED3DSTT_CUBE
:
587 shader_addline(buffer
, "uniform samplerCube %csampler%u;\n", prefix
, i
);
589 case WINED3DSTT_VOLUME
:
590 shader_addline(buffer
, "uniform sampler3D %csampler%u;\n", prefix
, i
);
593 shader_addline(buffer
, "uniform unsupported_sampler %csampler%u;\n", prefix
, i
);
594 FIXME("Unrecognized sampler type: %#x\n", stype
);
600 /* Declare address variables */
601 for (i
= 0; i
< This
->baseShader
.limits
.address
; i
++) {
602 if (reg_maps
->address
[i
])
603 shader_addline(buffer
, "ivec4 A%d;\n", i
);
606 /* Declare texture coordinate temporaries and initialize them */
607 for (i
= 0; i
< This
->baseShader
.limits
.texcoord
; i
++) {
608 if (reg_maps
->texcoord
[i
])
609 shader_addline(buffer
, "vec4 T%u = gl_TexCoord[%u];\n", i
, i
);
612 /* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
613 * helper function shader that is linked in at link time
615 if(pshader
&& This
->baseShader
.hex_version
>= WINED3DVS_VERSION(3, 0)) {
617 shader_addline(buffer
, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings
) / 4);
619 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
620 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
621 * pixel shader that reads the fixed function color into the packed input registers.
623 shader_addline(buffer
, "vec4 IN[%u];\n", GL_LIMITS(glsl_varyings
) / 4);
627 /* Declare output register temporaries */
628 if(This
->baseShader
.limits
.packed_output
) {
629 shader_addline(buffer
, "vec4 OUT[%u];\n", This
->baseShader
.limits
.packed_output
);
632 /* Declare temporary variables */
633 for(i
= 0; i
< This
->baseShader
.limits
.temporary
; i
++) {
634 if (reg_maps
->temporary
[i
])
635 shader_addline(buffer
, "vec4 R%u;\n", i
);
638 /* Declare attributes */
639 for (i
= 0; i
< This
->baseShader
.limits
.attributes
; i
++) {
640 if (reg_maps
->attributes
[i
])
641 shader_addline(buffer
, "attribute vec4 attrib%i;\n", i
);
644 /* Declare loop registers aLx */
645 for (i
= 0; i
< reg_maps
->loop_depth
; i
++) {
646 shader_addline(buffer
, "int aL%u;\n", i
);
647 shader_addline(buffer
, "int tmpInt%u;\n", i
);
650 /* Temporary variables for matrix operations */
651 shader_addline(buffer
, "vec4 tmp0;\n");
652 shader_addline(buffer
, "vec4 tmp1;\n");
654 /* Hardcodeable local constants */
655 if(!This
->baseShader
.load_local_constsF
) {
656 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
657 float *value
= (float *) lconst
->value
;
658 shader_addline(buffer
, "const vec4 LC%u = vec4(%f, %f, %f, %f);\n", lconst
->idx
,
659 value
[0], value
[1], value
[2], value
[3]);
663 /* Start the main program */
664 shader_addline(buffer
, "void main() {\n");
665 if(pshader
&& reg_maps
->vpos
) {
666 shader_addline(buffer
, "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1) - 0.5;\n");
670 /*****************************************************************************
671 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
673 * For more information, see http://wiki.winehq.org/DirectX-Shaders
674 ****************************************************************************/
677 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG
* arg
, const DWORD param
,
678 const DWORD addr_token
, DWORD mask
, glsl_src_param_t
*src_param
);
680 /** Used for opcode modifiers - They multiply the result by the specified amount */
681 static const char * const shift_glsl_tab
[] = {
683 "2.0 * ", /* 1 (x2) */
684 "4.0 * ", /* 2 (x4) */
685 "8.0 * ", /* 3 (x8) */
686 "16.0 * ", /* 4 (x16) */
687 "32.0 * ", /* 5 (x32) */
694 "0.0625 * ", /* 12 (d16) */
695 "0.125 * ", /* 13 (d8) */
696 "0.25 * ", /* 14 (d4) */
697 "0.5 * " /* 15 (d2) */
700 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
701 static void shader_glsl_gen_modifier (
704 const char *in_regswizzle
,
709 if (instr
== WINED3DSIO_TEXKILL
)
712 switch (instr
& WINED3DSP_SRCMOD_MASK
) {
713 case WINED3DSPSM_DZ
: /* Need to handle this in the instructions itself (texld & texcrd). */
715 case WINED3DSPSM_NONE
:
716 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
718 case WINED3DSPSM_NEG
:
719 sprintf(out_str
, "-%s%s", in_reg
, in_regswizzle
);
721 case WINED3DSPSM_NOT
:
722 sprintf(out_str
, "!%s%s", in_reg
, in_regswizzle
);
724 case WINED3DSPSM_BIAS
:
725 sprintf(out_str
, "(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
727 case WINED3DSPSM_BIASNEG
:
728 sprintf(out_str
, "-(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
730 case WINED3DSPSM_SIGN
:
731 sprintf(out_str
, "(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
733 case WINED3DSPSM_SIGNNEG
:
734 sprintf(out_str
, "-(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
736 case WINED3DSPSM_COMP
:
737 sprintf(out_str
, "(1.0 - %s%s)", in_reg
, in_regswizzle
);
740 sprintf(out_str
, "(2.0 * %s%s)", in_reg
, in_regswizzle
);
742 case WINED3DSPSM_X2NEG
:
743 sprintf(out_str
, "-(2.0 * %s%s)", in_reg
, in_regswizzle
);
745 case WINED3DSPSM_ABS
:
746 sprintf(out_str
, "abs(%s%s)", in_reg
, in_regswizzle
);
748 case WINED3DSPSM_ABSNEG
:
749 sprintf(out_str
, "-abs(%s%s)", in_reg
, in_regswizzle
);
752 FIXME("Unhandled modifier %u\n", (instr
& WINED3DSP_SRCMOD_MASK
));
753 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
757 static BOOL
constant_is_local(IWineD3DBaseShaderImpl
* This
, DWORD reg
) {
758 local_constant
* lconst
;
760 if(This
->baseShader
.load_local_constsF
) return FALSE
;
761 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
762 if(lconst
->idx
== reg
) return TRUE
;
768 /** Writes the GLSL variable name that corresponds to the register that the
769 * DX opcode parameter is trying to access */
770 static void shader_glsl_get_register_name(
772 const DWORD addr_token
,
775 SHADER_OPCODE_ARG
* arg
) {
777 /* oPos, oFog and oPts in D3D */
778 static const char * const hwrastout_reg_names
[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
780 DWORD reg
= param
& WINED3DSP_REGNUM_MASK
;
781 DWORD regtype
= shader_get_regtype(param
);
782 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
783 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
784 WineD3D_GL_Info
* gl_info
= &deviceImpl
->adapter
->gl_info
;
786 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
792 case WINED3DSPR_TEMP
:
793 sprintf(tmpStr
, "R%u", reg
);
795 case WINED3DSPR_INPUT
:
797 /* Pixel shaders >= 3.0 */
798 if (WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 3) {
799 if (param
& WINED3DSHADER_ADDRMODE_RELATIVE
) {
800 glsl_src_param_t rel_param
;
801 shader_glsl_add_src_param(arg
, addr_token
, 0, WINED3DSP_WRITEMASK_0
, &rel_param
);
803 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
806 if(((IWineD3DPixelShaderImpl
*) This
)->input_reg_map
[reg
]) {
807 sprintf(tmpStr
, "IN[%s + %u]", rel_param
.param_str
,
808 ((IWineD3DPixelShaderImpl
*) This
)->input_reg_map
[reg
]);
810 sprintf(tmpStr
, "IN[%s]", rel_param
.param_str
);
813 sprintf(tmpStr
, "IN[%u]",
814 ((IWineD3DPixelShaderImpl
*) This
)->input_reg_map
[reg
]);
818 strcpy(tmpStr
, "gl_Color");
820 strcpy(tmpStr
, "gl_SecondaryColor");
823 if (vshader_input_is_color((IWineD3DVertexShader
*) This
, reg
))
825 sprintf(tmpStr
, "attrib%u", reg
);
828 case WINED3DSPR_CONST
:
830 const char* prefix
= pshader
? "PC":"VC";
832 /* Relative addressing */
833 if (param
& WINED3DSHADER_ADDRMODE_RELATIVE
) {
835 /* Relative addressing on shaders 2.0+ have a relative address token,
836 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
837 if (WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 2) {
838 glsl_src_param_t rel_param
;
839 shader_glsl_add_src_param(arg
, addr_token
, 0, WINED3DSP_WRITEMASK_0
, &rel_param
);
841 sprintf(tmpStr
, "%s[%s + %u]", prefix
, rel_param
.param_str
, reg
);
843 sprintf(tmpStr
, "%s[%s]", prefix
, rel_param
.param_str
);
847 sprintf(tmpStr
, "%s[A0.x + %u]", prefix
, reg
);
849 sprintf(tmpStr
, "%s[A0.x]", prefix
);
854 if(constant_is_local(This
, reg
)) {
855 sprintf(tmpStr
, "LC%u", reg
);
857 sprintf(tmpStr
, "%s[%u]", prefix
, reg
);
863 case WINED3DSPR_CONSTINT
:
865 sprintf(tmpStr
, "PI[%u]", reg
);
867 sprintf(tmpStr
, "VI[%u]", reg
);
869 case WINED3DSPR_CONSTBOOL
:
871 sprintf(tmpStr
, "PB[%u]", reg
);
873 sprintf(tmpStr
, "VB[%u]", reg
);
875 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
877 sprintf(tmpStr
, "T%u", reg
);
879 sprintf(tmpStr
, "A%u", reg
);
882 case WINED3DSPR_LOOP
:
883 sprintf(tmpStr
, "aL%u", This
->baseShader
.cur_loop_regno
- 1);
885 case WINED3DSPR_SAMPLER
:
887 sprintf(tmpStr
, "Psampler%u", reg
);
889 sprintf(tmpStr
, "Vsampler%u", reg
);
891 case WINED3DSPR_COLOROUT
:
892 if (reg
>= GL_LIMITS(buffers
)) {
893 WARN("Write to render target %u, only %d supported\n", reg
, 4);
895 if (GL_SUPPORT(ARB_DRAW_BUFFERS
)) {
896 sprintf(tmpStr
, "gl_FragData[%u]", reg
);
897 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
898 sprintf(tmpStr
, "gl_FragColor");
901 case WINED3DSPR_RASTOUT
:
902 sprintf(tmpStr
, "%s", hwrastout_reg_names
[reg
]);
904 case WINED3DSPR_DEPTHOUT
:
905 sprintf(tmpStr
, "gl_FragDepth");
907 case WINED3DSPR_ATTROUT
:
909 sprintf(tmpStr
, "gl_FrontColor");
911 sprintf(tmpStr
, "gl_FrontSecondaryColor");
914 case WINED3DSPR_TEXCRDOUT
:
915 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
916 if (WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 3)
917 sprintf(tmpStr
, "OUT[%u]", reg
);
919 sprintf(tmpStr
, "gl_TexCoord[%u]", reg
);
921 case WINED3DSPR_MISCTYPE
:
924 sprintf(tmpStr
, "vpos");
925 } else if (reg
== 1){
926 /* Note that gl_FrontFacing is a bool, while vFace is
927 * a float for which the sign determines front/back
929 sprintf(tmpStr
, "(gl_FrontFacing ? 1.0 : -1.0)");
931 FIXME("Unhandled misctype register %d\n", reg
);
932 sprintf(tmpStr
, "unrecognized_register");
936 FIXME("Unhandled register name Type(%d)\n", regtype
);
937 sprintf(tmpStr
, "unrecognized_register");
941 strcat(regstr
, tmpStr
);
944 /* Get the GLSL write mask for the destination register */
945 static DWORD
shader_glsl_get_write_mask(const DWORD param
, char *write_mask
) {
946 char *ptr
= write_mask
;
947 DWORD mask
= param
& WINED3DSP_WRITEMASK_ALL
;
949 if (shader_is_scalar(param
)) {
950 mask
= WINED3DSP_WRITEMASK_0
;
953 if (param
& WINED3DSP_WRITEMASK_0
) *ptr
++ = 'x';
954 if (param
& WINED3DSP_WRITEMASK_1
) *ptr
++ = 'y';
955 if (param
& WINED3DSP_WRITEMASK_2
) *ptr
++ = 'z';
956 if (param
& WINED3DSP_WRITEMASK_3
) *ptr
++ = 'w';
964 static size_t shader_glsl_get_write_mask_size(DWORD write_mask
) {
967 if (write_mask
& WINED3DSP_WRITEMASK_0
) ++size
;
968 if (write_mask
& WINED3DSP_WRITEMASK_1
) ++size
;
969 if (write_mask
& WINED3DSP_WRITEMASK_2
) ++size
;
970 if (write_mask
& WINED3DSP_WRITEMASK_3
) ++size
;
975 static void shader_glsl_get_swizzle(const DWORD param
, BOOL fixup
, DWORD mask
, char *swizzle_str
) {
976 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
977 * but addressed as "rgba". To fix this we need to swap the register's x
978 * and z components. */
979 DWORD swizzle
= (param
& WINED3DSP_SWIZZLE_MASK
) >> WINED3DSP_SWIZZLE_SHIFT
;
980 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
981 char *ptr
= swizzle_str
;
983 if (!shader_is_scalar(param
)) {
985 /* swizzle bits fields: wwzzyyxx */
986 if (mask
& WINED3DSP_WRITEMASK_0
) *ptr
++ = swizzle_chars
[swizzle
& 0x03];
987 if (mask
& WINED3DSP_WRITEMASK_1
) *ptr
++ = swizzle_chars
[(swizzle
>> 2) & 0x03];
988 if (mask
& WINED3DSP_WRITEMASK_2
) *ptr
++ = swizzle_chars
[(swizzle
>> 4) & 0x03];
989 if (mask
& WINED3DSP_WRITEMASK_3
) *ptr
++ = swizzle_chars
[(swizzle
>> 6) & 0x03];
995 /* From a given parameter token, generate the corresponding GLSL string.
996 * Also, return the actual register name and swizzle in case the
997 * caller needs this information as well. */
998 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG
* arg
, const DWORD param
,
999 const DWORD addr_token
, DWORD mask
, glsl_src_param_t
*src_param
) {
1000 BOOL is_color
= FALSE
;
1001 char swizzle_str
[6];
1003 src_param
->reg_name
[0] = '\0';
1004 src_param
->param_str
[0] = '\0';
1005 swizzle_str
[0] = '\0';
1007 shader_glsl_get_register_name(param
, addr_token
, src_param
->reg_name
, &is_color
, arg
);
1009 shader_glsl_get_swizzle(param
, is_color
, mask
, swizzle_str
);
1010 shader_glsl_gen_modifier(param
, src_param
->reg_name
, swizzle_str
, src_param
->param_str
);
1013 /* From a given parameter token, generate the corresponding GLSL string.
1014 * Also, return the actual register name and swizzle in case the
1015 * caller needs this information as well. */
1016 static DWORD
shader_glsl_add_dst_param(SHADER_OPCODE_ARG
* arg
, const DWORD param
,
1017 const DWORD addr_token
, glsl_dst_param_t
*dst_param
) {
1018 BOOL is_color
= FALSE
;
1020 dst_param
->mask_str
[0] = '\0';
1021 dst_param
->reg_name
[0] = '\0';
1023 shader_glsl_get_register_name(param
, addr_token
, dst_param
->reg_name
, &is_color
, arg
);
1024 return shader_glsl_get_write_mask(param
, dst_param
->mask_str
);
1027 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1028 static DWORD
shader_glsl_append_dst_ext(SHADER_BUFFER
*buffer
, SHADER_OPCODE_ARG
*arg
, const DWORD param
) {
1029 glsl_dst_param_t dst_param
;
1033 mask
= shader_glsl_add_dst_param(arg
, param
, arg
->dst_addr
, &dst_param
);
1036 shift
= (param
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
1037 shader_addline(buffer
, "%s%s = %s(", dst_param
.reg_name
, dst_param
.mask_str
, shift_glsl_tab
[shift
]);
1043 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1044 static DWORD
shader_glsl_append_dst(SHADER_BUFFER
*buffer
, SHADER_OPCODE_ARG
*arg
) {
1045 return shader_glsl_append_dst_ext(buffer
, arg
, arg
->dst
);
1048 /** Process GLSL instruction modifiers */
1049 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG
* arg
) {
1051 DWORD mask
= arg
->dst
& WINED3DSP_DSTMOD_MASK
;
1053 if (arg
->opcode
->dst_token
&& mask
!= 0) {
1054 glsl_dst_param_t dst_param
;
1056 shader_glsl_add_dst_param(arg
, arg
->dst
, 0, &dst_param
);
1058 if (mask
& WINED3DSPDM_SATURATE
) {
1059 /* _SAT means to clamp the value of the register to between 0 and 1 */
1060 shader_addline(arg
->buffer
, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param
.reg_name
,
1061 dst_param
.mask_str
, dst_param
.reg_name
, dst_param
.mask_str
);
1063 if (mask
& WINED3DSPDM_MSAMPCENTROID
) {
1064 FIXME("_centroid modifier not handled\n");
1066 if (mask
& WINED3DSPDM_PARTIALPRECISION
) {
1067 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1072 static inline const char* shader_get_comp_op(
1073 const DWORD opcode
) {
1075 DWORD op
= (opcode
& INST_CONTROLS_MASK
) >> INST_CONTROLS_SHIFT
;
1077 case COMPARISON_GT
: return ">";
1078 case COMPARISON_EQ
: return "==";
1079 case COMPARISON_GE
: return ">=";
1080 case COMPARISON_LT
: return "<";
1081 case COMPARISON_NE
: return "!=";
1082 case COMPARISON_LE
: return "<=";
1084 FIXME("Unrecognized comparison value: %u\n", op
);
1089 static void shader_glsl_get_sample_function(DWORD sampler_type
, BOOL projected
, BOOL texrect
, glsl_sample_function_t
*sample_function
) {
1090 /* Note that there's no such thing as a projected cube texture. */
1091 switch(sampler_type
) {
1093 sample_function
->name
= projected
? "texture1DProj" : "texture1D";
1094 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
;
1098 sample_function
->name
= projected
? "texture2DRectProj" : "texture2DRect";
1100 sample_function
->name
= projected
? "texture2DProj" : "texture2D";
1102 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
;
1104 case WINED3DSTT_CUBE
:
1105 sample_function
->name
= "textureCube";
1106 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1108 case WINED3DSTT_VOLUME
:
1109 sample_function
->name
= projected
? "texture3DProj" : "texture3D";
1110 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1113 sample_function
->name
= "";
1114 FIXME("Unrecognized sampler type: %#x;\n", sampler_type
);
1119 static void shader_glsl_color_correction(SHADER_OPCODE_ARG
* arg
) {
1120 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
1121 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) shader
->baseShader
.device
;
1122 WineD3D_GL_Info
*gl_info
= &deviceImpl
->adapter
->gl_info
;
1123 glsl_dst_param_t dst_param
;
1124 glsl_dst_param_t dst_param2
;
1126 WINED3DFORMAT conversion_group
;
1127 IWineD3DBaseTextureImpl
*texture
;
1128 DWORD mask
, mask_size
;
1130 BOOL recorded
= FALSE
;
1132 DWORD hex_version
= shader
->baseShader
.hex_version
;
1134 switch(arg
->opcode
->opcode
) {
1135 case WINED3DSIO_TEX
:
1136 if (hex_version
< WINED3DPS_VERSION(2,0)) {
1137 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1139 sampler_idx
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
1143 case WINED3DSIO_TEXLDL
:
1144 FIXME("Add color fixup for vertex texture WINED3DSIO_TEXLDL\n");
1147 case WINED3DSIO_TEXDP3TEX
:
1148 case WINED3DSIO_TEXM3x3TEX
:
1149 case WINED3DSIO_TEXM3x3SPEC
:
1150 case WINED3DSIO_TEXM3x3VSPEC
:
1151 case WINED3DSIO_TEXBEM
:
1152 case WINED3DSIO_TEXREG2AR
:
1153 case WINED3DSIO_TEXREG2GB
:
1154 case WINED3DSIO_TEXREG2RGB
:
1155 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1159 /* Not a texture sampling instruction, nothing to do */
1163 texture
= (IWineD3DBaseTextureImpl
*) deviceImpl
->stateBlock
->textures
[sampler_idx
];
1165 fmt
= texture
->resource
.format
;
1166 conversion_group
= texture
->baseTexture
.shader_conversion_group
;
1168 fmt
= WINED3DFMT_UNKNOWN
;
1169 conversion_group
= WINED3DFMT_UNKNOWN
;
1172 /* before doing anything, record the sampler with the format in the format conversion list,
1173 * but check if it's not there already
1175 for(i
= 0; i
< shader
->baseShader
.num_sampled_samplers
; i
++) {
1176 if(shader
->baseShader
.sampled_samplers
[i
] == sampler_idx
) {
1182 shader
->baseShader
.sampled_samplers
[shader
->baseShader
.num_sampled_samplers
] = sampler_idx
;
1183 shader
->baseShader
.num_sampled_samplers
++;
1184 shader
->baseShader
.sampled_format
[sampler_idx
] = conversion_group
;
1188 case WINED3DFMT_V8U8
:
1189 case WINED3DFMT_V16U16
:
1190 if(GL_SUPPORT(NV_TEXTURE_SHADER
) ||
1191 (GL_SUPPORT(ATI_ENVMAP_BUMPMAP
) && fmt
== WINED3DFMT_V8U8
)) {
1192 /* The 3rd channel returns 1.0 in d3d, but 0.0 in gl. Fix this while we're at it :-) */
1193 mask
= shader_glsl_add_dst_param(arg
, arg
->dst
, WINED3DSP_WRITEMASK_2
, &dst_param
);
1194 mask_size
= shader_glsl_get_write_mask_size(mask
);
1195 if(mask_size
>= 3) {
1196 shader_addline(arg
->buffer
, "%s.%c = 1.0;\n", dst_param
.reg_name
, dst_param
.mask_str
[3]);
1199 /* Correct the sign, but leave the blue as it is - it was loaded correctly already */
1200 mask
= shader_glsl_add_dst_param(arg
, arg
->dst
,
1201 WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
,
1203 mask_size
= shader_glsl_get_write_mask_size(mask
);
1204 if(mask_size
>= 2) {
1205 shader_addline(arg
->buffer
, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1206 dst_param
.reg_name
, dst_param
.mask_str
[1], dst_param
.mask_str
[2],
1207 dst_param
.reg_name
, dst_param
.mask_str
[1], dst_param
.mask_str
[2]);
1208 } else if(mask_size
== 1) {
1209 shader_addline(arg
->buffer
, "%s.%c = %s.%c * 2.0 - 1.0;\n", dst_param
.reg_name
, dst_param
.mask_str
[1],
1210 dst_param
.reg_name
, dst_param
.mask_str
[1]);
1215 case WINED3DFMT_X8L8V8U8
:
1216 if(!GL_SUPPORT(NV_TEXTURE_SHADER
)) {
1217 /* Red and blue are the signed channels, fix them up; Blue(=L) is correct already,
1218 * and a(X) is always 1.0
1220 mask
= shader_glsl_add_dst_param(arg
, arg
->dst
, WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &dst_param
);
1221 mask_size
= shader_glsl_get_write_mask_size(mask
);
1222 if(mask_size
>= 2) {
1223 shader_addline(arg
->buffer
, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1224 dst_param
.reg_name
, dst_param
.mask_str
[1], dst_param
.mask_str
[2],
1225 dst_param
.reg_name
, dst_param
.mask_str
[1], dst_param
.mask_str
[2]);
1226 } else if(mask_size
== 1) {
1227 shader_addline(arg
->buffer
, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1228 dst_param
.reg_name
, dst_param
.mask_str
[1],
1229 dst_param
.reg_name
, dst_param
.mask_str
[1]);
1234 case WINED3DFMT_L6V5U5
:
1235 if(!GL_SUPPORT(NV_TEXTURE_SHADER
)) {
1236 mask
= shader_glsl_add_dst_param(arg
, arg
->dst
, WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &dst_param
);
1237 mask_size
= shader_glsl_get_write_mask_size(mask
);
1238 shader_glsl_add_dst_param(arg
, arg
->dst
, WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_2
, &dst_param2
);
1239 if(mask_size
>= 3) {
1240 /* Swap y and z (U and L), and do a sign conversion on x and the new y(V and U) */
1241 shader_addline(arg
->buffer
, "tmp0.g = %s.%c;\n",
1242 dst_param
.reg_name
, dst_param
.mask_str
[2]);
1243 shader_addline(arg
->buffer
, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1244 dst_param
.reg_name
, dst_param
.mask_str
[2], dst_param
.mask_str
[1],
1245 dst_param2
.reg_name
, dst_param
.mask_str
[1], dst_param
.mask_str
[3]);
1246 shader_addline(arg
->buffer
, "%s.%c = tmp0.g;\n", dst_param
.reg_name
,
1247 dst_param
.mask_str
[3]);
1248 } else if(mask_size
== 2) {
1249 /* This is bad: We have VL, but we need VU */
1250 FIXME("2 components sampled from a converted L6V5U5 texture\n");
1252 shader_addline(arg
->buffer
, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1253 dst_param
.reg_name
, dst_param
.mask_str
[1],
1254 dst_param2
.reg_name
, dst_param
.mask_str
[1]);
1259 case WINED3DFMT_Q8W8V8U8
:
1260 if(!GL_SUPPORT(NV_TEXTURE_SHADER
)) {
1261 /* Correct the sign in all channels. The writemask just applies as-is, no
1262 * need for checking the mask size
1264 shader_glsl_add_dst_param(arg
, arg
->dst
,
1265 WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
|
1266 WINED3DSP_WRITEMASK_2
| WINED3DSP_WRITEMASK_3
,
1268 shader_addline(arg
->buffer
, "%s%s = %s%s * 2.0 - 1.0;\n", dst_param
.reg_name
, dst_param
.mask_str
,
1269 dst_param
.reg_name
, dst_param
.mask_str
);
1273 /* stupid compiler */
1279 /*****************************************************************************
1281 * Begin processing individual instruction opcodes
1283 ****************************************************************************/
1285 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
1286 void shader_glsl_arith(SHADER_OPCODE_ARG
* arg
) {
1287 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
1288 SHADER_BUFFER
* buffer
= arg
->buffer
;
1289 glsl_src_param_t src0_param
;
1290 glsl_src_param_t src1_param
;
1294 /* Determine the GLSL operator to use based on the opcode */
1295 switch (curOpcode
->opcode
) {
1296 case WINED3DSIO_MUL
: op
= '*'; break;
1297 case WINED3DSIO_ADD
: op
= '+'; break;
1298 case WINED3DSIO_SUB
: op
= '-'; break;
1301 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode
->name
);
1305 write_mask
= shader_glsl_append_dst(buffer
, arg
);
1306 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1307 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1308 shader_addline(buffer
, "%s %c %s);\n", src0_param
.param_str
, op
, src1_param
.param_str
);
1311 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1312 void shader_glsl_mov(SHADER_OPCODE_ARG
* arg
) {
1313 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
1314 SHADER_BUFFER
* buffer
= arg
->buffer
;
1315 glsl_src_param_t src0_param
;
1318 write_mask
= shader_glsl_append_dst(buffer
, arg
);
1319 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1321 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1322 * shader versions WINED3DSIO_MOVA is used for this. */
1323 if ((WINED3DSHADER_VERSION_MAJOR(shader
->baseShader
.hex_version
) == 1 &&
1324 !shader_is_pshader_version(shader
->baseShader
.hex_version
) &&
1325 shader_get_regtype(arg
->dst
) == WINED3DSPR_ADDR
) ||
1326 arg
->opcode
->opcode
== WINED3DSIO_MOVA
) {
1327 /* We need to *round* to the nearest int here. */
1328 size_t mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1329 if (mask_size
> 1) {
1330 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
);
1332 shader_addline(buffer
, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param
.param_str
, src0_param
.param_str
);
1335 shader_addline(buffer
, "%s);\n", src0_param
.param_str
);
1339 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
1340 void shader_glsl_dot(SHADER_OPCODE_ARG
* arg
) {
1341 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
1342 SHADER_BUFFER
* buffer
= arg
->buffer
;
1343 glsl_src_param_t src0_param
;
1344 glsl_src_param_t src1_param
;
1345 DWORD dst_write_mask
, src_write_mask
;
1346 size_t dst_size
= 0;
1348 dst_write_mask
= shader_glsl_append_dst(buffer
, arg
);
1349 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
1351 /* dp3 works on vec3, dp4 on vec4 */
1352 if (curOpcode
->opcode
== WINED3DSIO_DP4
) {
1353 src_write_mask
= WINED3DSP_WRITEMASK_ALL
;
1355 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1358 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_write_mask
, &src0_param
);
1359 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], src_write_mask
, &src1_param
);
1362 shader_addline(buffer
, "vec%d(dot(%s, %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
1364 shader_addline(buffer
, "dot(%s, %s));\n", src0_param
.param_str
, src1_param
.param_str
);
1368 /* Note that this instruction has some restrictions. The destination write mask
1369 * can't contain the w component, and the source swizzles have to be .xyzw */
1370 void shader_glsl_cross(SHADER_OPCODE_ARG
*arg
) {
1371 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1372 glsl_src_param_t src0_param
;
1373 glsl_src_param_t src1_param
;
1376 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1377 shader_glsl_append_dst(arg
->buffer
, arg
);
1378 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
1379 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], src_mask
, &src1_param
);
1380 shader_addline(arg
->buffer
, "cross(%s, %s)%s);\n", src0_param
.param_str
, src1_param
.param_str
, dst_mask
);
1383 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1384 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1385 * GLSL uses the value as-is. */
1386 void shader_glsl_pow(SHADER_OPCODE_ARG
*arg
) {
1387 SHADER_BUFFER
*buffer
= arg
->buffer
;
1388 glsl_src_param_t src0_param
;
1389 glsl_src_param_t src1_param
;
1390 DWORD dst_write_mask
;
1393 dst_write_mask
= shader_glsl_append_dst(buffer
, arg
);
1394 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
1396 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1397 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
1400 shader_addline(buffer
, "vec%d(pow(abs(%s), %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
1402 shader_addline(buffer
, "pow(abs(%s), %s));\n", src0_param
.param_str
, src1_param
.param_str
);
1406 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
1407 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
1408 * GLSL uses the value as-is. */
1409 void shader_glsl_log(SHADER_OPCODE_ARG
*arg
) {
1410 SHADER_BUFFER
*buffer
= arg
->buffer
;
1411 glsl_src_param_t src0_param
;
1412 DWORD dst_write_mask
;
1415 dst_write_mask
= shader_glsl_append_dst(buffer
, arg
);
1416 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
1418 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1421 shader_addline(buffer
, "vec%d(log2(abs(%s))));\n", dst_size
, src0_param
.param_str
);
1423 shader_addline(buffer
, "log2(abs(%s)));\n", src0_param
.param_str
);
1427 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1428 void shader_glsl_map2gl(SHADER_OPCODE_ARG
* arg
) {
1429 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
1430 SHADER_BUFFER
* buffer
= arg
->buffer
;
1431 glsl_src_param_t src_param
;
1432 const char *instruction
;
1433 char arguments
[256];
1437 /* Determine the GLSL function to use based on the opcode */
1438 /* TODO: Possibly make this a table for faster lookups */
1439 switch (curOpcode
->opcode
) {
1440 case WINED3DSIO_MIN
: instruction
= "min"; break;
1441 case WINED3DSIO_MAX
: instruction
= "max"; break;
1442 case WINED3DSIO_ABS
: instruction
= "abs"; break;
1443 case WINED3DSIO_FRC
: instruction
= "fract"; break;
1444 case WINED3DSIO_NRM
: instruction
= "normalize"; break;
1445 case WINED3DSIO_LOGP
:
1446 case WINED3DSIO_LOG
: instruction
= "log2"; break;
1447 case WINED3DSIO_EXP
: instruction
= "exp2"; break;
1448 case WINED3DSIO_SGN
: instruction
= "sign"; break;
1449 case WINED3DSIO_DSX
: instruction
= "dFdx"; break;
1450 case WINED3DSIO_DSY
: instruction
= "ycorrection.y * dFdy"; break;
1451 default: instruction
= "";
1452 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode
->name
);
1456 write_mask
= shader_glsl_append_dst(buffer
, arg
);
1458 arguments
[0] = '\0';
1459 if (curOpcode
->num_params
> 0) {
1460 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src_param
);
1461 strcat(arguments
, src_param
.param_str
);
1462 for (i
= 2; i
< curOpcode
->num_params
; ++i
) {
1463 strcat(arguments
, ", ");
1464 shader_glsl_add_src_param(arg
, arg
->src
[i
-1], arg
->src_addr
[i
-1], write_mask
, &src_param
);
1465 strcat(arguments
, src_param
.param_str
);
1469 shader_addline(buffer
, "%s(%s));\n", instruction
, arguments
);
1472 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1473 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1474 * dst.x = 2^(floor(src))
1475 * dst.y = src - floor(src)
1476 * dst.z = 2^src (partial precision is allowed, but optional)
1478 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1479 * dst = 2^src; (partial precision is allowed, but optional)
1481 void shader_glsl_expp(SHADER_OPCODE_ARG
* arg
) {
1482 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)arg
->shader
;
1483 glsl_src_param_t src_param
;
1485 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
1487 if (shader
->baseShader
.hex_version
< WINED3DPS_VERSION(2,0)) {
1490 shader_addline(arg
->buffer
, "tmp0.x = exp2(floor(%s));\n", src_param
.param_str
);
1491 shader_addline(arg
->buffer
, "tmp0.y = %s - floor(%s);\n", src_param
.param_str
, src_param
.param_str
);
1492 shader_addline(arg
->buffer
, "tmp0.z = exp2(%s);\n", src_param
.param_str
);
1493 shader_addline(arg
->buffer
, "tmp0.w = 1.0;\n");
1495 shader_glsl_append_dst(arg
->buffer
, arg
);
1496 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1497 shader_addline(arg
->buffer
, "tmp0%s);\n", dst_mask
);
1502 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1503 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1505 if (mask_size
> 1) {
1506 shader_addline(arg
->buffer
, "vec%d(exp2(%s)));\n", mask_size
, src_param
.param_str
);
1508 shader_addline(arg
->buffer
, "exp2(%s));\n", src_param
.param_str
);
1513 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1514 void shader_glsl_rcp(SHADER_OPCODE_ARG
* arg
) {
1515 glsl_src_param_t src_param
;
1519 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1520 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1521 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
1523 if (mask_size
> 1) {
1524 shader_addline(arg
->buffer
, "vec%d(1.0 / %s));\n", mask_size
, src_param
.param_str
);
1526 shader_addline(arg
->buffer
, "1.0 / %s);\n", src_param
.param_str
);
1530 void shader_glsl_rsq(SHADER_OPCODE_ARG
* arg
) {
1531 SHADER_BUFFER
* buffer
= arg
->buffer
;
1532 glsl_src_param_t src_param
;
1536 write_mask
= shader_glsl_append_dst(buffer
, arg
);
1537 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1539 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
1541 if (mask_size
> 1) {
1542 shader_addline(buffer
, "vec%d(inversesqrt(%s)));\n", mask_size
, src_param
.param_str
);
1544 shader_addline(buffer
, "inversesqrt(%s));\n", src_param
.param_str
);
1548 /** Process signed comparison opcodes in GLSL. */
1549 void shader_glsl_compare(SHADER_OPCODE_ARG
* arg
) {
1550 glsl_src_param_t src0_param
;
1551 glsl_src_param_t src1_param
;
1555 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1556 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1557 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1558 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1560 if (mask_size
> 1) {
1561 const char *compare
;
1563 switch(arg
->opcode
->opcode
) {
1564 case WINED3DSIO_SLT
: compare
= "lessThan"; break;
1565 case WINED3DSIO_SGE
: compare
= "greaterThanEqual"; break;
1566 default: compare
= "";
1567 FIXME("Can't handle opcode %s\n", arg
->opcode
->name
);
1570 shader_addline(arg
->buffer
, "vec%d(%s(%s, %s)));\n", mask_size
, compare
,
1571 src0_param
.param_str
, src1_param
.param_str
);
1573 switch(arg
->opcode
->opcode
) {
1574 case WINED3DSIO_SLT
:
1575 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
1576 * to return 0.0 but step returns 1.0 because step is not < x
1577 * An alternative is a bvec compare padded with an unused secound component.
1578 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
1579 * issue. Playing with not() is not possible either because not() does not accept
1582 shader_addline(arg
->buffer
, "(%s < %s) ? 1.0 : 0.0);\n", src0_param
.param_str
, src1_param
.param_str
);
1584 case WINED3DSIO_SGE
:
1585 /* Here we can use the step() function and safe a conditional */
1586 shader_addline(arg
->buffer
, "step(%s, %s));\n", src1_param
.param_str
, src0_param
.param_str
);
1589 FIXME("Can't handle opcode %s\n", arg
->opcode
->name
);
1595 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1596 void shader_glsl_cmp(SHADER_OPCODE_ARG
* arg
) {
1597 glsl_src_param_t src0_param
;
1598 glsl_src_param_t src1_param
;
1599 glsl_src_param_t src2_param
;
1600 DWORD write_mask
, cmp_channel
= 0;
1603 BOOL temp_destination
= FALSE
;
1605 DWORD src0reg
= arg
->src
[0] & WINED3DSP_REGNUM_MASK
;
1606 DWORD src1reg
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
1607 DWORD src2reg
= arg
->src
[2] & WINED3DSP_REGNUM_MASK
;
1608 DWORD src0regtype
= shader_get_regtype(arg
->src
[0]);
1609 DWORD src1regtype
= shader_get_regtype(arg
->src
[1]);
1610 DWORD src2regtype
= shader_get_regtype(arg
->src
[2]);
1611 DWORD dstreg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1612 DWORD dstregtype
= shader_get_regtype(arg
->dst
);
1614 /* Cycle through all source0 channels */
1615 for (i
=0; i
<4; i
++) {
1617 /* Find the destination channels which use the current source0 channel */
1618 for (j
=0; j
<4; j
++) {
1619 if ( ((arg
->src
[0] >> (WINED3DSP_SWIZZLE_SHIFT
+ 2*j
)) & 0x3) == i
) {
1620 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
1621 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
1625 /* Splitting the cmp instruction up in multiple lines imposes a problem:
1626 * The first lines may overwrite source parameters of the following lines.
1627 * Deal with that by using a temporary destination register if needed
1629 if((src0reg
== dstreg
&& src0regtype
== dstregtype
) ||
1630 (src1reg
== dstreg
&& src1regtype
== dstregtype
) ||
1631 (src2reg
== dstreg
&& src2regtype
== dstregtype
)) {
1633 write_mask
= shader_glsl_get_write_mask(arg
->dst
& (~WINED3DSP_SWIZZLE_MASK
| write_mask
), mask_char
);
1634 if (!write_mask
) continue;
1635 shader_addline(arg
->buffer
, "tmp0%s = (", mask_char
);
1636 temp_destination
= TRUE
;
1638 write_mask
= shader_glsl_append_dst_ext(arg
->buffer
, arg
, arg
->dst
& (~WINED3DSP_SWIZZLE_MASK
| write_mask
));
1639 if (!write_mask
) continue;
1642 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], cmp_channel
, &src0_param
);
1643 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1644 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1646 shader_addline(arg
->buffer
, "%s >= 0.0 ? %s : %s);\n",
1647 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1650 if(temp_destination
) {
1651 shader_glsl_get_write_mask(arg
->dst
, mask_char
);
1652 shader_glsl_append_dst_ext(arg
->buffer
, arg
, arg
->dst
);
1653 shader_addline(arg
->buffer
, "tmp0%s);\n", mask_char
);
1658 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
1659 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1660 * the compare is done per component of src0. */
1661 void shader_glsl_cnd(SHADER_OPCODE_ARG
* arg
) {
1662 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
1663 glsl_src_param_t src0_param
;
1664 glsl_src_param_t src1_param
;
1665 glsl_src_param_t src2_param
;
1666 DWORD write_mask
, cmp_channel
= 0;
1669 if (shader
->baseShader
.hex_version
< WINED3DPS_VERSION(1, 4)) {
1670 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1671 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1672 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1673 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1675 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
1676 if(arg
->opcode_token
& WINED3DSI_COISSUE
) {
1677 shader_addline(arg
->buffer
, "%s /* COISSUE! */);\n", src1_param
.param_str
);
1679 shader_addline(arg
->buffer
, "%s > 0.5 ? %s : %s);\n",
1680 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1684 /* Cycle through all source0 channels */
1685 for (i
=0; i
<4; i
++) {
1687 /* Find the destination channels which use the current source0 channel */
1688 for (j
=0; j
<4; j
++) {
1689 if ( ((arg
->src
[0] >> (WINED3DSP_SWIZZLE_SHIFT
+ 2*j
)) & 0x3) == i
) {
1690 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
1691 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
1694 write_mask
= shader_glsl_append_dst_ext(arg
->buffer
, arg
, arg
->dst
& (~WINED3DSP_SWIZZLE_MASK
| write_mask
));
1695 if (!write_mask
) continue;
1697 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], cmp_channel
, &src0_param
);
1698 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1699 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1701 shader_addline(arg
->buffer
, "%s > 0.5 ? %s : %s);\n",
1702 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1706 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1707 void shader_glsl_mad(SHADER_OPCODE_ARG
* arg
) {
1708 glsl_src_param_t src0_param
;
1709 glsl_src_param_t src1_param
;
1710 glsl_src_param_t src2_param
;
1713 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1714 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1715 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1716 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1717 shader_addline(arg
->buffer
, "(%s * %s) + %s);\n",
1718 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1721 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1722 Vertex shaders to GLSL codes */
1723 void shader_glsl_mnxn(SHADER_OPCODE_ARG
* arg
) {
1725 int nComponents
= 0;
1726 SHADER_OPCODE_ARG tmpArg
;
1728 memset(&tmpArg
, 0, sizeof(SHADER_OPCODE_ARG
));
1730 /* Set constants for the temporary argument */
1731 tmpArg
.shader
= arg
->shader
;
1732 tmpArg
.buffer
= arg
->buffer
;
1733 tmpArg
.src
[0] = arg
->src
[0];
1734 tmpArg
.src_addr
[0] = arg
->src_addr
[0];
1735 tmpArg
.src_addr
[1] = arg
->src_addr
[1];
1736 tmpArg
.reg_maps
= arg
->reg_maps
;
1738 switch(arg
->opcode
->opcode
) {
1739 case WINED3DSIO_M4x4
:
1741 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP4
);
1743 case WINED3DSIO_M4x3
:
1745 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP4
);
1747 case WINED3DSIO_M3x4
:
1749 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1751 case WINED3DSIO_M3x3
:
1753 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1755 case WINED3DSIO_M3x2
:
1757 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1763 for (i
= 0; i
< nComponents
; i
++) {
1764 tmpArg
.dst
= ((arg
->dst
) & ~WINED3DSP_WRITEMASK_ALL
)|(WINED3DSP_WRITEMASK_0
<<i
);
1765 tmpArg
.src
[1] = arg
->src
[1]+i
;
1766 shader_glsl_dot(&tmpArg
);
1771 The LRP instruction performs a component-wise linear interpolation
1772 between the second and third operands using the first operand as the
1773 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
1774 This is equivalent to mix(src2, src1, src0);
1776 void shader_glsl_lrp(SHADER_OPCODE_ARG
* arg
) {
1777 glsl_src_param_t src0_param
;
1778 glsl_src_param_t src1_param
;
1779 glsl_src_param_t src2_param
;
1782 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1784 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1785 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1786 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1788 shader_addline(arg
->buffer
, "mix(%s, %s, %s));\n",
1789 src2_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
);
1792 /** Process the WINED3DSIO_LIT instruction in GLSL:
1793 * dst.x = dst.w = 1.0
1794 * dst.y = (src0.x > 0) ? src0.x
1795 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1796 * where src.w is clamped at +- 128
1798 void shader_glsl_lit(SHADER_OPCODE_ARG
* arg
) {
1799 glsl_src_param_t src0_param
;
1800 glsl_src_param_t src1_param
;
1801 glsl_src_param_t src3_param
;
1804 shader_glsl_append_dst(arg
->buffer
, arg
);
1805 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1807 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1808 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_1
, &src1_param
);
1809 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &src3_param
);
1811 /* The sdk specifies the instruction like this
1813 * if(src.x > 0.0) dst.y = src.x
1815 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
1819 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
1820 * dst.x = 1.0 ... No further explanation needed
1821 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
1822 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
1823 * dst.w = 1.0. ... Nothing fancy.
1825 * So we still have one conditional in there. So do this:
1826 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
1828 * step(0.0, x) will return 1 if src.x > 0.0, and 0 otherwise. So if y is 0 we get pow(0.0 * 1.0, power),
1829 * which sets dst.z to 0. If y > 0, but x = 0.0, we get pow(y * 0.0, power), which results in 0 too.
1830 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
1832 shader_addline(arg
->buffer
, "vec4(1.0, max(%s, 0.0), pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
1833 src0_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
, src3_param
.param_str
, dst_mask
);
1836 /** Process the WINED3DSIO_DST instruction in GLSL:
1838 * dst.y = src0.x * src0.y
1842 void shader_glsl_dst(SHADER_OPCODE_ARG
* arg
) {
1843 glsl_src_param_t src0y_param
;
1844 glsl_src_param_t src0z_param
;
1845 glsl_src_param_t src1y_param
;
1846 glsl_src_param_t src1w_param
;
1849 shader_glsl_append_dst(arg
->buffer
, arg
);
1850 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1852 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_1
, &src0y_param
);
1853 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_2
, &src0z_param
);
1854 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_1
, &src1y_param
);
1855 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_3
, &src1w_param
);
1857 shader_addline(arg
->buffer
, "vec4(1.0, %s * %s, %s, %s))%s;\n",
1858 src0y_param
.param_str
, src1y_param
.param_str
, src0z_param
.param_str
, src1w_param
.param_str
, dst_mask
);
1861 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1862 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1863 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1865 * dst.x = cos(src0.?)
1866 * dst.y = sin(src0.?)
1870 void shader_glsl_sincos(SHADER_OPCODE_ARG
* arg
) {
1871 glsl_src_param_t src0_param
;
1874 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1875 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1877 switch (write_mask
) {
1878 case WINED3DSP_WRITEMASK_0
:
1879 shader_addline(arg
->buffer
, "cos(%s));\n", src0_param
.param_str
);
1882 case WINED3DSP_WRITEMASK_1
:
1883 shader_addline(arg
->buffer
, "sin(%s));\n", src0_param
.param_str
);
1886 case (WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
):
1887 shader_addline(arg
->buffer
, "vec2(cos(%s), sin(%s)));\n", src0_param
.param_str
, src0_param
.param_str
);
1891 ERR("Write mask should be .x, .y or .xy\n");
1896 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1897 * Start a for() loop where src1.y is the initial value of aL,
1898 * increment aL by src1.z for a total of src1.x iterations.
1899 * Need to use a temporary variable for this operation.
1901 /* FIXME: I don't think nested loops will work correctly this way. */
1902 void shader_glsl_loop(SHADER_OPCODE_ARG
* arg
) {
1903 glsl_src_param_t src1_param
;
1904 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
1905 DWORD regtype
= shader_get_regtype(arg
->src
[1]);
1906 DWORD reg
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
1907 const DWORD
*control_values
= NULL
;
1908 local_constant
*constant
;
1910 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_ALL
, &src1_param
);
1912 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
1913 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
1914 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
1917 if(regtype
== WINED3DSPR_CONSTINT
) {
1918 LIST_FOR_EACH_ENTRY(constant
, &shader
->baseShader
.constantsI
, local_constant
, entry
) {
1919 if(constant
->idx
== reg
) {
1920 control_values
= constant
->value
;
1926 if(control_values
) {
1927 if(control_values
[2] > 0) {
1928 shader_addline(arg
->buffer
, "for (aL%u = %d; aL%u < (%d * %d + %d); aL%u += %d) {\n",
1929 shader
->baseShader
.cur_loop_depth
, control_values
[1],
1930 shader
->baseShader
.cur_loop_depth
, control_values
[0], control_values
[2], control_values
[1],
1931 shader
->baseShader
.cur_loop_depth
, control_values
[2]);
1932 } else if(control_values
[2] == 0) {
1933 shader_addline(arg
->buffer
, "for (aL%u = %d, tmpInt%u = 0; tmpInt%u < %d; tmpInt%u++) {\n",
1934 shader
->baseShader
.cur_loop_depth
, control_values
[1], shader
->baseShader
.cur_loop_depth
,
1935 shader
->baseShader
.cur_loop_depth
, control_values
[0],
1936 shader
->baseShader
.cur_loop_depth
);
1938 shader_addline(arg
->buffer
, "for (aL%u = %d; aL%u > (%d * %d + %d); aL%u += %d) {\n",
1939 shader
->baseShader
.cur_loop_depth
, control_values
[1],
1940 shader
->baseShader
.cur_loop_depth
, control_values
[0], control_values
[2], control_values
[1],
1941 shader
->baseShader
.cur_loop_depth
, control_values
[2]);
1944 shader_addline(arg
->buffer
, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
1945 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_regno
,
1946 src1_param
.reg_name
, shader
->baseShader
.cur_loop_depth
, src1_param
.reg_name
,
1947 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_regno
, src1_param
.reg_name
);
1950 shader
->baseShader
.cur_loop_depth
++;
1951 shader
->baseShader
.cur_loop_regno
++;
1954 void shader_glsl_end(SHADER_OPCODE_ARG
* arg
) {
1955 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
1957 shader_addline(arg
->buffer
, "}\n");
1959 if(arg
->opcode
->opcode
== WINED3DSIO_ENDLOOP
) {
1960 shader
->baseShader
.cur_loop_depth
--;
1961 shader
->baseShader
.cur_loop_regno
--;
1963 if(arg
->opcode
->opcode
== WINED3DSIO_ENDREP
) {
1964 shader
->baseShader
.cur_loop_depth
--;
1968 void shader_glsl_rep(SHADER_OPCODE_ARG
* arg
) {
1969 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
1970 glsl_src_param_t src0_param
;
1972 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1973 shader_addline(arg
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
1974 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_depth
,
1975 src0_param
.param_str
, shader
->baseShader
.cur_loop_depth
);
1976 shader
->baseShader
.cur_loop_depth
++;
1979 void shader_glsl_if(SHADER_OPCODE_ARG
* arg
) {
1980 glsl_src_param_t src0_param
;
1982 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1983 shader_addline(arg
->buffer
, "if (%s) {\n", src0_param
.param_str
);
1986 void shader_glsl_ifc(SHADER_OPCODE_ARG
* arg
) {
1987 glsl_src_param_t src0_param
;
1988 glsl_src_param_t src1_param
;
1990 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1991 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
1993 shader_addline(arg
->buffer
, "if (%s %s %s) {\n",
1994 src0_param
.param_str
, shader_get_comp_op(arg
->opcode_token
), src1_param
.param_str
);
1997 void shader_glsl_else(SHADER_OPCODE_ARG
* arg
) {
1998 shader_addline(arg
->buffer
, "} else {\n");
2001 void shader_glsl_break(SHADER_OPCODE_ARG
* arg
) {
2002 shader_addline(arg
->buffer
, "break;\n");
2005 /* FIXME: According to MSDN the compare is done per component. */
2006 void shader_glsl_breakc(SHADER_OPCODE_ARG
* arg
) {
2007 glsl_src_param_t src0_param
;
2008 glsl_src_param_t src1_param
;
2010 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2011 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2013 shader_addline(arg
->buffer
, "if (%s %s %s) break;\n",
2014 src0_param
.param_str
, shader_get_comp_op(arg
->opcode_token
), src1_param
.param_str
);
2017 void shader_glsl_label(SHADER_OPCODE_ARG
* arg
) {
2019 DWORD snum
= (arg
->src
[0]) & WINED3DSP_REGNUM_MASK
;
2020 shader_addline(arg
->buffer
, "}\n");
2021 shader_addline(arg
->buffer
, "void subroutine%u () {\n", snum
);
2024 void shader_glsl_call(SHADER_OPCODE_ARG
* arg
) {
2025 DWORD snum
= (arg
->src
[0]) & WINED3DSP_REGNUM_MASK
;
2026 shader_addline(arg
->buffer
, "subroutine%u();\n", snum
);
2029 void shader_glsl_callnz(SHADER_OPCODE_ARG
* arg
) {
2030 glsl_src_param_t src1_param
;
2032 DWORD snum
= (arg
->src
[0]) & WINED3DSP_REGNUM_MASK
;
2033 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2034 shader_addline(arg
->buffer
, "if (%s) subroutine%u();\n", src1_param
.param_str
, snum
);
2037 /*********************************************
2038 * Pixel Shader Specific Code begins here
2039 ********************************************/
2040 void pshader_glsl_tex(SHADER_OPCODE_ARG
* arg
) {
2041 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2042 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
2043 DWORD hex_version
= This
->baseShader
.hex_version
;
2044 char dst_swizzle
[6];
2045 glsl_sample_function_t sample_function
;
2048 BOOL projected
, texrect
= FALSE
;
2051 /* All versions have a destination register */
2052 shader_glsl_append_dst(arg
->buffer
, arg
);
2054 /* 1.0-1.4: Use destination register as sampler source.
2055 * 2.0+: Use provided sampler source. */
2056 if (hex_version
< WINED3DPS_VERSION(1,4)) {
2059 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2060 flags
= deviceImpl
->stateBlock
->textureState
[sampler_idx
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
2062 if (flags
& WINED3DTTFF_PROJECTED
) {
2064 switch (flags
& ~WINED3DTTFF_PROJECTED
) {
2065 case WINED3DTTFF_COUNT1
: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2066 case WINED3DTTFF_COUNT2
: mask
= WINED3DSP_WRITEMASK_1
; break;
2067 case WINED3DTTFF_COUNT3
: mask
= WINED3DSP_WRITEMASK_2
; break;
2068 case WINED3DTTFF_COUNT4
:
2069 case WINED3DTTFF_DISABLE
: mask
= WINED3DSP_WRITEMASK_3
; break;
2074 } else if (hex_version
< WINED3DPS_VERSION(2,0)) {
2075 DWORD src_mod
= arg
->src
[0] & WINED3DSP_SRCMOD_MASK
;
2076 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2078 if (src_mod
== WINED3DSPSM_DZ
) {
2080 mask
= WINED3DSP_WRITEMASK_2
;
2081 } else if (src_mod
== WINED3DSPSM_DW
) {
2083 mask
= WINED3DSP_WRITEMASK_3
;
2088 sampler_idx
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
2089 if(arg
->opcode_token
& WINED3DSI_TEXLD_PROJECT
) {
2090 /* ps 2.0 texldp instruction always divides by the fourth component. */
2092 mask
= WINED3DSP_WRITEMASK_3
;
2098 if(deviceImpl
->stateBlock
->textures
[sampler_idx
] &&
2099 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl
->stateBlock
->textures
[sampler_idx
]) == GL_TEXTURE_RECTANGLE_ARB
) {
2103 sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
2104 shader_glsl_get_sample_function(sampler_type
, projected
, texrect
, &sample_function
);
2105 mask
|= sample_function
.coord_mask
;
2107 if (hex_version
< WINED3DPS_VERSION(2,0)) {
2108 shader_glsl_get_write_mask(arg
->dst
, dst_swizzle
);
2110 shader_glsl_get_swizzle(arg
->src
[1], FALSE
, arg
->dst
, dst_swizzle
);
2113 /* 1.0-1.3: Use destination register as coordinate source.
2114 1.4+: Use provided coordinate source register. */
2115 if (hex_version
< WINED3DPS_VERSION(1,4)) {
2117 shader_glsl_get_write_mask(mask
, coord_mask
);
2118 shader_addline(arg
->buffer
, "%s(Psampler%u, T%u%s)%s);\n",
2119 sample_function
.name
, sampler_idx
, sampler_idx
, coord_mask
, dst_swizzle
);
2121 glsl_src_param_t coord_param
;
2122 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], mask
, &coord_param
);
2123 if(arg
->opcode_token
& WINED3DSI_TEXLD_BIAS
) {
2124 glsl_src_param_t bias
;
2125 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &bias
);
2127 shader_addline(arg
->buffer
, "%s(Psampler%u, %s, %s)%s);\n",
2128 sample_function
.name
, sampler_idx
, coord_param
.param_str
,
2129 bias
.param_str
, dst_swizzle
);
2131 shader_addline(arg
->buffer
, "%s(Psampler%u, %s)%s);\n",
2132 sample_function
.name
, sampler_idx
, coord_param
.param_str
, dst_swizzle
);
2137 void shader_glsl_texldl(SHADER_OPCODE_ARG
* arg
) {
2138 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*)arg
->shader
;
2139 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
2140 glsl_sample_function_t sample_function
;
2141 glsl_src_param_t coord_param
, lod_param
;
2142 char dst_swizzle
[6];
2145 BOOL texrect
= FALSE
;
2147 shader_glsl_append_dst(arg
->buffer
, arg
);
2148 shader_glsl_get_swizzle(arg
->src
[1], FALSE
, arg
->dst
, dst_swizzle
);
2150 sampler_idx
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
2151 sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
2152 if(deviceImpl
->stateBlock
->textures
[sampler_idx
] &&
2153 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl
->stateBlock
->textures
[sampler_idx
]) == GL_TEXTURE_RECTANGLE_ARB
) {
2156 shader_glsl_get_sample_function(sampler_type
, FALSE
, texrect
, &sample_function
); shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], sample_function
.coord_mask
, &coord_param
);
2158 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
2160 if (shader_is_pshader_version(This
->baseShader
.hex_version
)) {
2161 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
2162 * However, they seem to work just fine in fragment shaders as well. */
2163 WARN("Using %sLod in fragment shader.\n", sample_function
.name
);
2164 shader_addline(arg
->buffer
, "%sLod(Psampler%u, %s, %s)%s);\n",
2165 sample_function
.name
, sampler_idx
, coord_param
.param_str
, lod_param
.param_str
, dst_swizzle
);
2167 shader_addline(arg
->buffer
, "%sLod(Vsampler%u, %s, %s)%s);\n",
2168 sample_function
.name
, sampler_idx
, coord_param
.param_str
, lod_param
.param_str
, dst_swizzle
);
2172 void pshader_glsl_texcoord(SHADER_OPCODE_ARG
* arg
) {
2174 /* FIXME: Make this work for more than just 2D textures */
2176 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2177 SHADER_BUFFER
* buffer
= arg
->buffer
;
2178 DWORD hex_version
= This
->baseShader
.hex_version
;
2182 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
2183 shader_glsl_get_write_mask(write_mask
, dst_mask
);
2185 if (hex_version
!= WINED3DPS_VERSION(1,4)) {
2186 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2187 shader_addline(buffer
, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n", reg
, dst_mask
);
2189 DWORD reg
= arg
->src
[0] & WINED3DSP_REGNUM_MASK
;
2190 DWORD src_mod
= arg
->src
[0] & WINED3DSP_SRCMOD_MASK
;
2191 char dst_swizzle
[6];
2193 shader_glsl_get_swizzle(arg
->src
[0], FALSE
, write_mask
, dst_swizzle
);
2195 if (src_mod
== WINED3DSPSM_DZ
) {
2196 glsl_src_param_t div_param
;
2197 size_t mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2198 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_2
, &div_param
);
2200 if (mask_size
> 1) {
2201 shader_addline(buffer
, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
2203 shader_addline(buffer
, "gl_TexCoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
2205 } else if (src_mod
== WINED3DSPSM_DW
) {
2206 glsl_src_param_t div_param
;
2207 size_t mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2208 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &div_param
);
2210 if (mask_size
> 1) {
2211 shader_addline(buffer
, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
2213 shader_addline(buffer
, "gl_TexCoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
2216 shader_addline(buffer
, "gl_TexCoord[%u]%s);\n", reg
, dst_swizzle
);
2221 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
2222 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2223 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2224 void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG
* arg
) {
2225 glsl_src_param_t src0_param
;
2227 glsl_sample_function_t sample_function
;
2228 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2229 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2230 DWORD sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
2232 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2234 shader_glsl_append_dst(arg
->buffer
, arg
);
2235 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2237 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
2238 * scalar, and projected sampling would require 4.
2240 * It is a dependent read - not valid with conditional NP2 textures
2242 shader_glsl_get_sample_function(sampler_type
, FALSE
, FALSE
, &sample_function
);
2244 switch(count_bits(sample_function
.coord_mask
)) {
2246 shader_addline(arg
->buffer
, "%s(Psampler%u, dot(gl_TexCoord[%u].xyz, %s))%s);\n",
2247 sample_function
.name
, sampler_idx
, sampler_idx
, src0_param
.param_str
, dst_mask
);
2251 shader_addline(arg
->buffer
, "%s(Psampler%u, vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0))%s);\n",
2252 sample_function
.name
, sampler_idx
, sampler_idx
, src0_param
.param_str
, dst_mask
);
2256 shader_addline(arg
->buffer
, "%s(Psampler%u, vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0))%s);\n",
2257 sample_function
.name
, sampler_idx
, sampler_idx
, src0_param
.param_str
, dst_mask
);
2260 FIXME("Unexpected mask bitcount %d\n", count_bits(sample_function
.coord_mask
));
2264 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2265 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2266 void pshader_glsl_texdp3(SHADER_OPCODE_ARG
* arg
) {
2267 glsl_src_param_t src0_param
;
2268 DWORD dstreg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2269 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2273 dst_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
2274 mask_size
= shader_glsl_get_write_mask_size(dst_mask
);
2275 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2277 if (mask_size
> 1) {
2278 shader_addline(arg
->buffer
, "vec%d(dot(T%u.xyz, %s)));\n", mask_size
, dstreg
, src0_param
.param_str
);
2280 shader_addline(arg
->buffer
, "dot(T%u.xyz, %s));\n", dstreg
, src0_param
.param_str
);
2284 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2285 * Calculate the depth as dst.x / dst.y */
2286 void pshader_glsl_texdepth(SHADER_OPCODE_ARG
* arg
) {
2287 glsl_dst_param_t dst_param
;
2289 shader_glsl_add_dst_param(arg
, arg
->dst
, 0, &dst_param
);
2291 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2292 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2293 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2294 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2297 shader_addline(arg
->buffer
, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n", dst_param
.reg_name
, dst_param
.reg_name
);
2300 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2301 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2302 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2303 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2305 void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG
* arg
) {
2306 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2307 DWORD dstreg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2308 glsl_src_param_t src0_param
;
2310 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2312 shader_addline(arg
->buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg
, src0_param
.param_str
);
2313 shader_addline(arg
->buffer
, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2316 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2317 * Calculate the 1st of a 2-row matrix multiplication. */
2318 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG
* arg
) {
2319 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2320 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2321 SHADER_BUFFER
* buffer
= arg
->buffer
;
2322 glsl_src_param_t src0_param
;
2324 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2325 shader_addline(buffer
, "tmp0.x = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2328 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2329 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2330 void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG
* arg
) {
2332 IWineD3DPixelShaderImpl
* shader
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2333 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2334 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2335 SHADER_BUFFER
* buffer
= arg
->buffer
;
2336 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
2337 glsl_src_param_t src0_param
;
2339 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2340 shader_addline(buffer
, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state
->current_row
, reg
, src0_param
.param_str
);
2341 current_state
->texcoord_w
[current_state
->current_row
++] = reg
;
2344 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG
* arg
) {
2345 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2346 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2347 SHADER_BUFFER
* buffer
= arg
->buffer
;
2348 glsl_src_param_t src0_param
;
2351 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2352 shader_addline(buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2354 shader_glsl_append_dst(buffer
, arg
);
2355 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2357 /* Sample the texture using the calculated coordinates */
2358 shader_addline(buffer
, "texture2D(Psampler%u, tmp0.xy)%s);\n", reg
, dst_mask
);
2361 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2362 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2363 void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG
* arg
) {
2364 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2365 glsl_src_param_t src0_param
;
2367 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2368 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2369 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
2370 DWORD sampler_type
= arg
->reg_maps
->samplers
[reg
] & WINED3DSP_TEXTURETYPE_MASK
;
2371 glsl_sample_function_t sample_function
;
2373 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2374 shader_addline(arg
->buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2376 shader_glsl_append_dst(arg
->buffer
, arg
);
2377 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2378 /* Dependent read, not valid with conditional NP2 */
2379 shader_glsl_get_sample_function(sampler_type
, FALSE
, FALSE
, &sample_function
);
2381 /* Sample the texture using the calculated coordinates */
2382 shader_addline(arg
->buffer
, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function
.name
, reg
, dst_mask
);
2384 current_state
->current_row
= 0;
2387 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2388 * Perform the 3rd row of a 3x3 matrix multiply */
2389 void pshader_glsl_texm3x3(SHADER_OPCODE_ARG
* arg
) {
2390 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2391 glsl_src_param_t src0_param
;
2393 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2394 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2395 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
2397 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2399 shader_glsl_append_dst(arg
->buffer
, arg
);
2400 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2401 shader_addline(arg
->buffer
, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg
, src0_param
.param_str
, dst_mask
);
2403 current_state
->current_row
= 0;
2406 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
2407 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2408 void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG
* arg
) {
2410 IWineD3DPixelShaderImpl
* shader
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2411 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2412 glsl_src_param_t src0_param
;
2413 glsl_src_param_t src1_param
;
2415 SHADER_BUFFER
* buffer
= arg
->buffer
;
2416 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
2417 DWORD stype
= arg
->reg_maps
->samplers
[reg
] & WINED3DSP_TEXTURETYPE_MASK
;
2418 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2419 glsl_sample_function_t sample_function
;
2421 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2422 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], src_mask
, &src1_param
);
2424 /* Perform the last matrix multiply operation */
2425 shader_addline(buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2426 /* Reflection calculation */
2427 shader_addline(buffer
, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param
.param_str
);
2429 shader_glsl_append_dst(buffer
, arg
);
2430 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2431 /* Dependent read, not valid with conditional NP2 */
2432 shader_glsl_get_sample_function(stype
, FALSE
, FALSE
, &sample_function
);
2434 /* Sample the texture */
2435 shader_addline(buffer
, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function
.name
, reg
, dst_mask
);
2437 current_state
->current_row
= 0;
2440 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
2441 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2442 void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG
* arg
) {
2444 IWineD3DPixelShaderImpl
* shader
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2445 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2446 SHADER_BUFFER
* buffer
= arg
->buffer
;
2447 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
2448 glsl_src_param_t src0_param
;
2450 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2451 DWORD sampler_type
= arg
->reg_maps
->samplers
[reg
] & WINED3DSP_TEXTURETYPE_MASK
;
2452 glsl_sample_function_t sample_function
;
2454 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2456 /* Perform the last matrix multiply operation */
2457 shader_addline(buffer
, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg
, src0_param
.param_str
);
2459 /* Construct the eye-ray vector from w coordinates */
2460 shader_addline(buffer
, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
2461 current_state
->texcoord_w
[0], current_state
->texcoord_w
[1], reg
);
2462 shader_addline(buffer
, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
2464 shader_glsl_append_dst(buffer
, arg
);
2465 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2466 /* Dependent read, not valid with conditional NP2 */
2467 shader_glsl_get_sample_function(sampler_type
, FALSE
, FALSE
, &sample_function
);
2469 /* Sample the texture using the calculated coordinates */
2470 shader_addline(buffer
, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function
.name
, reg
, dst_mask
);
2472 current_state
->current_row
= 0;
2475 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
2476 * Apply a fake bump map transform.
2477 * texbem is pshader <= 1.3 only, this saves a few version checks
2479 void pshader_glsl_texbem(SHADER_OPCODE_ARG
* arg
) {
2480 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2481 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
2482 char dst_swizzle
[6];
2483 glsl_sample_function_t sample_function
;
2484 glsl_src_param_t coord_param
;
2491 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2492 flags
= deviceImpl
->stateBlock
->textureState
[sampler_idx
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
2494 sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
2495 /* Dependent read, not valid with conditional NP2 */
2496 shader_glsl_get_sample_function(sampler_type
, FALSE
, FALSE
, &sample_function
);
2497 mask
= sample_function
.coord_mask
;
2499 shader_glsl_get_write_mask(arg
->dst
, dst_swizzle
);
2501 shader_glsl_get_write_mask(mask
, coord_mask
);
2503 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2504 * so we can't let the GL handle this.
2506 if (flags
& WINED3DTTFF_PROJECTED
) {
2508 char coord_div_mask
[3];
2509 switch (flags
& ~WINED3DTTFF_PROJECTED
) {
2510 case WINED3DTTFF_COUNT1
: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2511 case WINED3DTTFF_COUNT2
: div_mask
= WINED3DSP_WRITEMASK_1
; break;
2512 case WINED3DTTFF_COUNT3
: div_mask
= WINED3DSP_WRITEMASK_2
; break;
2513 case WINED3DTTFF_COUNT4
:
2514 case WINED3DTTFF_DISABLE
: div_mask
= WINED3DSP_WRITEMASK_3
; break;
2516 shader_glsl_get_write_mask(div_mask
, coord_div_mask
);
2517 shader_addline(arg
->buffer
, "T%u%s /= T%u%s;\n", sampler_idx
, coord_mask
, sampler_idx
, coord_div_mask
);
2520 shader_glsl_append_dst(arg
->buffer
, arg
);
2521 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
|WINED3DSP_WRITEMASK_1
, &coord_param
);
2522 if(arg
->opcode
->opcode
== WINED3DSIO_TEXBEML
) {
2523 glsl_src_param_t luminance_param
;
2524 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_2
, &luminance_param
);
2525 shader_addline(arg
->buffer
, "(%s(Psampler%u, T%u%s + vec4(bumpenvmat * %s, 0.0, 0.0)%s )*(%s * luminancescale + luminanceoffset))%s);\n",
2526 sample_function
.name
, sampler_idx
, sampler_idx
, coord_mask
, coord_param
.param_str
, coord_mask
,
2527 luminance_param
.param_str
, dst_swizzle
);
2529 shader_addline(arg
->buffer
, "%s(Psampler%u, T%u%s + vec4(bumpenvmat * %s, 0.0, 0.0)%s )%s);\n",
2530 sample_function
.name
, sampler_idx
, sampler_idx
, coord_mask
, coord_param
.param_str
, coord_mask
, dst_swizzle
);
2534 void pshader_glsl_bem(SHADER_OPCODE_ARG
* arg
) {
2535 glsl_src_param_t src0_param
, src1_param
;
2537 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
|WINED3DSP_WRITEMASK_1
, &src0_param
);
2538 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
|WINED3DSP_WRITEMASK_1
, &src1_param
);
2540 shader_glsl_append_dst(arg
->buffer
, arg
);
2541 shader_addline(arg
->buffer
, "%s + bumpenvmat * %s);\n",
2542 src0_param
.param_str
, src1_param
.param_str
);
2545 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
2546 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
2547 void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG
* arg
) {
2549 glsl_src_param_t src0_param
;
2550 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2553 shader_glsl_append_dst(arg
->buffer
, arg
);
2554 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2555 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
2557 shader_addline(arg
->buffer
, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx
, src0_param
.reg_name
, dst_mask
);
2560 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
2561 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
2562 void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG
* arg
) {
2563 glsl_src_param_t src0_param
;
2564 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2567 shader_glsl_append_dst(arg
->buffer
, arg
);
2568 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2569 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
2571 shader_addline(arg
->buffer
, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx
, src0_param
.reg_name
, dst_mask
);
2574 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
2575 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
2576 void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG
* arg
) {
2577 glsl_src_param_t src0_param
;
2579 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2580 DWORD sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
2581 glsl_sample_function_t sample_function
;
2583 shader_glsl_append_dst(arg
->buffer
, arg
);
2584 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2585 /* Dependent read, not valid with conditional NP2 */
2586 shader_glsl_get_sample_function(sampler_type
, FALSE
, FALSE
, &sample_function
);
2587 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], sample_function
.coord_mask
, &src0_param
);
2589 shader_addline(arg
->buffer
, "%s(Psampler%u, %s)%s);\n", sample_function
.name
, sampler_idx
, src0_param
.param_str
, dst_mask
);
2592 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
2593 * If any of the first 3 components are < 0, discard this pixel */
2594 void pshader_glsl_texkill(SHADER_OPCODE_ARG
* arg
) {
2595 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2596 DWORD hex_version
= This
->baseShader
.hex_version
;
2597 glsl_dst_param_t dst_param
;
2599 /* The argument is a destination parameter, and no writemasks are allowed */
2600 shader_glsl_add_dst_param(arg
, arg
->dst
, 0, &dst_param
);
2601 if((hex_version
>= WINED3DPS_VERSION(2,0))) {
2602 /* 2.0 shaders compare all 4 components in texkill */
2603 shader_addline(arg
->buffer
, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param
.reg_name
);
2605 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
2606 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
2607 * 4 components are defined, only the first 3 are used
2609 shader_addline(arg
->buffer
, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param
.reg_name
);
2613 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
2614 * dst = dot2(src0, src1) + src2 */
2615 void pshader_glsl_dp2add(SHADER_OPCODE_ARG
* arg
) {
2616 glsl_src_param_t src0_param
;
2617 glsl_src_param_t src1_param
;
2618 glsl_src_param_t src2_param
;
2622 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
2623 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2625 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
2626 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
2627 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], WINED3DSP_WRITEMASK_0
, &src2_param
);
2629 shader_addline(arg
->buffer
, "dot(%s, %s) + %s);\n", src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2632 void pshader_glsl_input_pack(
2633 SHADER_BUFFER
* buffer
,
2634 semantic
* semantics_in
,
2635 IWineD3DPixelShader
*iface
) {
2638 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*) iface
;
2640 for (i
= 0; i
< MAX_REG_INPUT
; i
++) {
2642 DWORD usage_token
= semantics_in
[i
].usage
;
2643 DWORD register_token
= semantics_in
[i
].reg
;
2644 DWORD usage
, usage_idx
;
2648 if (!usage_token
) continue;
2649 usage
= (usage_token
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
2650 usage_idx
= (usage_token
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
2651 shader_glsl_get_write_mask(register_token
, reg_mask
);
2655 case WINED3DDECLUSAGE_TEXCOORD
:
2656 if(usage_idx
< 8 && This
->vertexprocessing
== pretransformed
) {
2657 shader_addline(buffer
, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
2658 This
->input_reg_map
[i
], reg_mask
, usage_idx
, reg_mask
);
2660 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2661 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
2665 case WINED3DDECLUSAGE_COLOR
:
2667 shader_addline(buffer
, "IN[%u]%s = vec4(gl_Color)%s;\n",
2668 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
2669 else if (usage_idx
== 1)
2670 shader_addline(buffer
, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
2671 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
2673 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2674 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
2678 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2679 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
2684 /*********************************************
2685 * Vertex Shader Specific Code begins here
2686 ********************************************/
2688 static void add_glsl_program_entry(IWineD3DDeviceImpl
*device
, struct glsl_shader_prog_link
*entry
) {
2689 glsl_program_key_t
*key
;
2691 key
= HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t
));
2692 key
->vshader
= entry
->vshader
;
2693 key
->pshader
= entry
->pshader
;
2695 hash_table_put(device
->glsl_program_lookup
, key
, entry
);
2698 static struct glsl_shader_prog_link
*get_glsl_program_entry(IWineD3DDeviceImpl
*device
,
2699 GLhandleARB vshader
, GLhandleARB pshader
) {
2700 glsl_program_key_t key
;
2702 key
.vshader
= vshader
;
2703 key
.pshader
= pshader
;
2705 return (struct glsl_shader_prog_link
*)hash_table_get(device
->glsl_program_lookup
, &key
);
2708 void delete_glsl_program_entry(IWineD3DDevice
*iface
, struct glsl_shader_prog_link
*entry
) {
2709 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
2710 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
2711 glsl_program_key_t
*key
;
2713 key
= HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t
));
2714 key
->vshader
= entry
->vshader
;
2715 key
->pshader
= entry
->pshader
;
2716 hash_table_remove(This
->glsl_program_lookup
, key
);
2718 GL_EXTCALL(glDeleteObjectARB(entry
->programId
));
2719 if (entry
->vshader
) list_remove(&entry
->vshader_entry
);
2720 if (entry
->pshader
) list_remove(&entry
->pshader_entry
);
2721 HeapFree(GetProcessHeap(), 0, entry
->vuniformF_locations
);
2722 HeapFree(GetProcessHeap(), 0, entry
->puniformF_locations
);
2723 HeapFree(GetProcessHeap(), 0, entry
);
2726 static void handle_ps3_input(SHADER_BUFFER
*buffer
, semantic
*semantics_in
, semantic
*semantics_out
, WineD3D_GL_Info
*gl_info
, DWORD
*map
) {
2728 DWORD usage_token
, usage_token_out
;
2729 DWORD register_token
, register_token_out
;
2730 DWORD usage
, usage_idx
, usage_out
, usage_idx_out
;
2732 char reg_mask
[6], reg_mask_out
[6];
2734 set
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*set
) * (GL_LIMITS(glsl_varyings
) / 4));
2736 for(i
= 0; i
< MAX_REG_INPUT
; i
++) {
2737 usage_token
= semantics_in
[i
].usage
;
2738 if (!usage_token
) continue;
2739 if(map
[i
] >= (GL_LIMITS(glsl_varyings
) / 4)) {
2740 FIXME("More input varyings declared than supported, expect issues\n");
2742 } else if(map
[i
] == -1) {
2743 /* Declared, but not read register */
2746 register_token
= semantics_in
[i
].reg
;
2748 usage
= (usage_token
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
2749 usage_idx
= (usage_token
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
2750 set
[map
[i
]] = shader_glsl_get_write_mask(register_token
, reg_mask
);
2752 if(!semantics_out
) {
2754 case WINED3DDECLUSAGE_COLOR
:
2756 shader_addline(buffer
, "IN[%u]%s = gl_FrontColor%s;\n",
2757 map
[i
], reg_mask
, reg_mask
);
2758 else if (usage_idx
== 1)
2759 shader_addline(buffer
, "IN[%u]%s = gl_FrontSecondaryColor%s;\n",
2760 map
[i
], reg_mask
, reg_mask
);
2762 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2763 map
[i
], reg_mask
, reg_mask
);
2766 case WINED3DDECLUSAGE_TEXCOORD
:
2767 if (usage_idx
< 8) {
2768 shader_addline(buffer
, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
2769 map
[i
], reg_mask
, usage_idx
, reg_mask
);
2771 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2772 map
[i
], reg_mask
, reg_mask
);
2776 case WINED3DDECLUSAGE_FOG
:
2777 shader_addline(buffer
, "IN[%u]%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
2778 map
[i
], reg_mask
, reg_mask
);
2782 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2783 map
[i
], reg_mask
, reg_mask
);
2787 for(j
= 0; j
< MAX_REG_OUTPUT
; j
++) {
2788 usage_token_out
= semantics_out
[j
].usage
;
2789 if (!usage_token_out
) continue;
2790 register_token_out
= semantics_out
[j
].reg
;
2792 usage_out
= (usage_token_out
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
2793 usage_idx_out
= (usage_token_out
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
2794 shader_glsl_get_write_mask(register_token_out
, reg_mask_out
);
2796 if(usage
== usage_out
&&
2797 usage_idx
== usage_idx_out
) {
2798 shader_addline(buffer
, "IN[%u]%s = OUT[%u]%s;\n",
2799 map
[i
], reg_mask
, j
, reg_mask
);
2804 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2805 map
[i
], reg_mask
, reg_mask
);
2810 /* This is solely to make the compiler / linker happy and avoid warning about undefined
2811 * varyings. It shouldn't result in any real code executed on the GPU, since all read
2812 * input varyings are assigned above, if the optimizer works properly.
2814 for(i
= 0; i
< GL_LIMITS(glsl_varyings
) / 4; i
++) {
2815 if(set
[i
] != WINED3DSP_WRITEMASK_ALL
) {
2816 unsigned int size
= 0;
2817 memset(reg_mask
, 0, sizeof(reg_mask
));
2818 if(!(set
[i
] & WINED3DSP_WRITEMASK_0
)) {
2819 reg_mask
[size
] = 'x';
2822 if(!(set
[i
] & WINED3DSP_WRITEMASK_1
)) {
2823 reg_mask
[size
] = 'y';
2826 if(!(set
[i
] & WINED3DSP_WRITEMASK_2
)) {
2827 reg_mask
[size
] = 'z';
2830 if(!(set
[i
] & WINED3DSP_WRITEMASK_3
)) {
2831 reg_mask
[size
] = 'w';
2836 shader_addline(buffer
, "IN[%u].%s = 0.0;\n", i
, reg_mask
);
2839 shader_addline(buffer
, "IN[%u].%s = vec2(0.0, 0.0);\n", i
, reg_mask
);
2842 shader_addline(buffer
, "IN[%u].%s = vec3(0.0, 0.0, 0.0);\n", i
, reg_mask
);
2845 shader_addline(buffer
, "IN[%u].%s = vec4(0.0, 0.0, 0.0, 0.0);\n", i
, reg_mask
);
2851 HeapFree(GetProcessHeap(), 0, set
);
2854 static GLhandleARB
generate_param_reorder_function(IWineD3DVertexShader
*vertexshader
,
2855 IWineD3DPixelShader
*pixelshader
,
2856 WineD3D_GL_Info
*gl_info
) {
2857 GLhandleARB ret
= 0;
2858 IWineD3DVertexShaderImpl
*vs
= (IWineD3DVertexShaderImpl
*) vertexshader
;
2859 IWineD3DPixelShaderImpl
*ps
= (IWineD3DPixelShaderImpl
*) pixelshader
;
2860 DWORD vs_major
= vs
? WINED3DSHADER_VERSION_MAJOR(vs
->baseShader
.hex_version
) : 0;
2861 DWORD ps_major
= ps
? WINED3DSHADER_VERSION_MAJOR(ps
->baseShader
.hex_version
) : 0;
2863 SHADER_BUFFER buffer
;
2865 DWORD register_token
;
2866 DWORD usage
, usage_idx
;
2868 semantic
*semantics_out
, *semantics_in
;
2870 buffer
.buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, SHADER_PGMSIZE
);
2873 buffer
.newline
= TRUE
;
2875 if(vs_major
< 3 && ps_major
< 3) {
2876 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them */
2877 shader_addline(&buffer
, "void order_ps_input() { /* do nothing */ }\n");
2878 } else if(ps_major
< 3 && vs_major
>= 3) {
2879 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
2880 semantics_out
= vs
->semantics_out
;
2882 shader_addline(&buffer
, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT
);
2883 for(i
= 0; i
< MAX_REG_OUTPUT
; i
++) {
2884 usage_token
= semantics_out
[i
].usage
;
2885 if (!usage_token
) continue;
2886 register_token
= semantics_out
[i
].reg
;
2888 usage
= (usage_token
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
2889 usage_idx
= (usage_token
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
2890 shader_glsl_get_write_mask(register_token
, reg_mask
);
2893 case WINED3DDECLUSAGE_COLOR
:
2895 shader_addline(&buffer
, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
2896 else if (usage_idx
== 1)
2897 shader_addline(&buffer
, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
2900 case WINED3DDECLUSAGE_POSITION
:
2901 shader_addline(&buffer
, "gl_Position%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
2904 case WINED3DDECLUSAGE_TEXCOORD
:
2905 if (usage_idx
< 8) {
2906 shader_addline(&buffer
, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
2907 usage_idx
, reg_mask
, i
, reg_mask
);
2911 case WINED3DDECLUSAGE_PSIZE
:
2912 shader_addline(&buffer
, "gl_PointSize = OUT[%u].x;\n", i
);
2915 case WINED3DDECLUSAGE_FOG
:
2916 shader_addline(&buffer
, "gl_FogFragCoord = OUT[%u].%c;\n", i
, reg_mask
[1]);
2923 shader_addline(&buffer
, "}\n");
2925 } else if(ps_major
>= 3 && vs_major
>= 3) {
2926 semantics_out
= vs
->semantics_out
;
2927 semantics_in
= ps
->semantics_in
;
2929 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
2930 shader_addline(&buffer
, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings
) / 4);
2931 shader_addline(&buffer
, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT
);
2933 /* First, sort out position and point size. Those are not passed to the pixel shader */
2934 for(i
= 0; i
< MAX_REG_OUTPUT
; i
++) {
2935 usage_token
= semantics_out
[i
].usage
;
2936 if (!usage_token
) continue;
2937 register_token
= semantics_out
[i
].reg
;
2939 usage
= (usage_token
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
2940 usage_idx
= (usage_token
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
2941 shader_glsl_get_write_mask(register_token
, reg_mask
);
2944 case WINED3DDECLUSAGE_POSITION
:
2945 shader_addline(&buffer
, "gl_Position%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
2948 case WINED3DDECLUSAGE_PSIZE
:
2949 shader_addline(&buffer
, "gl_PointSize = OUT[%u].x;\n", i
);
2957 /* Then, fix the pixel shader input */
2958 handle_ps3_input(&buffer
, semantics_in
, semantics_out
, gl_info
, ps
->input_reg_map
);
2960 shader_addline(&buffer
, "}\n");
2961 } else if(ps_major
>= 3 && vs_major
< 3) {
2962 semantics_in
= ps
->semantics_in
;
2964 shader_addline(&buffer
, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings
) / 4);
2965 shader_addline(&buffer
, "void order_ps_input() {\n");
2966 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
2967 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
2968 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
2970 handle_ps3_input(&buffer
, semantics_in
, NULL
, gl_info
, ps
->input_reg_map
);
2971 shader_addline(&buffer
, "}\n");
2973 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major
, ps_major
);
2976 ret
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
2977 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
2978 GL_EXTCALL(glShaderSourceARB(ret
, 1, (const char**)&buffer
.buffer
, NULL
));
2979 checkGLcall("glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL)");
2980 GL_EXTCALL(glCompileShaderARB(ret
));
2981 checkGLcall("glCompileShaderARB(ret)");
2983 HeapFree(GetProcessHeap(), 0, buffer
.buffer
);
2987 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
2988 * It sets the programId on the current StateBlock (because it should be called
2989 * inside of the DrawPrimitive() part of the render loop).
2991 * If a program for the given combination does not exist, create one, and store
2992 * the program in the hash table. If it creates a program, it will link the
2993 * given objects, too.
2995 static void set_glsl_shader_program(IWineD3DDevice
*iface
, BOOL use_ps
, BOOL use_vs
) {
2996 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
2997 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
2998 IWineD3DPixelShader
*pshader
= This
->stateBlock
->pixelShader
;
2999 IWineD3DVertexShader
*vshader
= This
->stateBlock
->vertexShader
;
3000 struct glsl_shader_prog_link
*entry
= NULL
;
3001 GLhandleARB programId
= 0;
3002 GLhandleARB reorder_shader_id
= 0;
3006 GLhandleARB vshader_id
= use_vs
? ((IWineD3DBaseShaderImpl
*)vshader
)->baseShader
.prgId
: 0;
3007 GLhandleARB pshader_id
= use_ps
? ((IWineD3DBaseShaderImpl
*)pshader
)->baseShader
.prgId
: 0;
3008 entry
= get_glsl_program_entry(This
, vshader_id
, pshader_id
);
3010 This
->stateBlock
->glsl_program
= entry
;
3014 /* If we get to this point, then no matching program exists, so we create one */
3015 programId
= GL_EXTCALL(glCreateProgramObjectARB());
3016 TRACE("Created new GLSL shader program %u\n", programId
);
3018 /* Create the entry */
3019 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link
));
3020 entry
->programId
= programId
;
3021 entry
->vshader
= vshader_id
;
3022 entry
->pshader
= pshader_id
;
3023 /* Add the hash table entry */
3024 add_glsl_program_entry(This
, entry
);
3026 /* Set the current program */
3027 This
->stateBlock
->glsl_program
= entry
;
3029 /* Attach GLSL vshader */
3031 int max_attribs
= 16; /* TODO: Will this always be the case? It is at the moment... */
3034 reorder_shader_id
= generate_param_reorder_function(vshader
, pshader
, gl_info
);
3035 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id
, programId
);
3036 GL_EXTCALL(glAttachObjectARB(programId
, reorder_shader_id
));
3037 checkGLcall("glAttachObjectARB");
3038 /* Flag the reorder function for deletion, then it will be freed automatically when the program
3041 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id
));
3043 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id
, programId
);
3044 GL_EXTCALL(glAttachObjectARB(programId
, vshader_id
));
3045 checkGLcall("glAttachObjectARB");
3047 /* Bind vertex attributes to a corresponding index number to match
3048 * the same index numbers as ARB_vertex_programs (makes loading
3049 * vertex attributes simpler). With this method, we can use the
3050 * exact same code to load the attributes later for both ARB and
3053 * We have to do this here because we need to know the Program ID
3054 * in order to make the bindings work, and it has to be done prior
3055 * to linking the GLSL program. */
3056 for (i
= 0; i
< max_attribs
; ++i
) {
3057 if (((IWineD3DBaseShaderImpl
*)vshader
)->baseShader
.reg_maps
.attributes
[i
]) {
3058 snprintf(tmp_name
, sizeof(tmp_name
), "attrib%i", i
);
3059 GL_EXTCALL(glBindAttribLocationARB(programId
, i
, tmp_name
));
3062 checkGLcall("glBindAttribLocationARB");
3064 list_add_head(&((IWineD3DBaseShaderImpl
*)vshader
)->baseShader
.linked_programs
, &entry
->vshader_entry
);
3067 /* Attach GLSL pshader */
3069 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id
, programId
);
3070 GL_EXTCALL(glAttachObjectARB(programId
, pshader_id
));
3071 checkGLcall("glAttachObjectARB");
3073 list_add_head(&((IWineD3DBaseShaderImpl
*)pshader
)->baseShader
.linked_programs
, &entry
->pshader_entry
);
3076 /* Link the program */
3077 TRACE("Linking GLSL shader program %u\n", programId
);
3078 GL_EXTCALL(glLinkProgramARB(programId
));
3079 print_glsl_info_log(&GLINFO_LOCATION
, programId
);
3081 entry
->vuniformF_locations
= HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB
) * GL_LIMITS(vshader_constantsF
));
3082 for (i
= 0; i
< GL_LIMITS(vshader_constantsF
); ++i
) {
3083 snprintf(glsl_name
, sizeof(glsl_name
), "VC[%i]", i
);
3084 entry
->vuniformF_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
3086 for (i
= 0; i
< MAX_CONST_I
; ++i
) {
3087 snprintf(glsl_name
, sizeof(glsl_name
), "VI[%i]", i
);
3088 entry
->vuniformI_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
3090 entry
->puniformF_locations
= HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB
) * GL_LIMITS(pshader_constantsF
));
3091 for (i
= 0; i
< GL_LIMITS(pshader_constantsF
); ++i
) {
3092 snprintf(glsl_name
, sizeof(glsl_name
), "PC[%i]", i
);
3093 entry
->puniformF_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
3095 for (i
= 0; i
< MAX_CONST_I
; ++i
) {
3096 snprintf(glsl_name
, sizeof(glsl_name
), "PI[%i]", i
);
3097 entry
->puniformI_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
3100 entry
->posFixup_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "posFixup"));
3101 entry
->bumpenvmat_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "bumpenvmat"));
3102 entry
->luminancescale_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "luminancescale"));
3103 entry
->luminanceoffset_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "luminanceoffset"));
3104 entry
->srgb_comparison_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "srgb_comparison"));
3105 entry
->srgb_mul_low_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "srgb_mul_low"));
3106 entry
->ycorrection_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "ycorrection"));
3107 checkGLcall("Find glsl program uniform locations");
3109 /* Set the shader to allow uniform loading on it */
3110 GL_EXTCALL(glUseProgramObjectARB(programId
));
3111 checkGLcall("glUseProgramObjectARB(programId)");
3113 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
3114 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
3115 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
3116 * vertex shader with fixed function pixel processing is used we make sure that the card
3117 * supports enough samplers to allow the max number of vertex samplers with all possible
3118 * fixed function fragment processing setups. So once the program is linked these samplers
3122 /* Load vertex shader samplers */
3123 shader_glsl_load_vsamplers(gl_info
, (IWineD3DStateBlock
*)This
->stateBlock
, programId
);
3126 /* Load pixel shader samplers */
3127 shader_glsl_load_psamplers(gl_info
, (IWineD3DStateBlock
*)This
->stateBlock
, programId
);
3131 static GLhandleARB
create_glsl_blt_shader(WineD3D_GL_Info
*gl_info
) {
3132 GLhandleARB program_id
;
3133 GLhandleARB vshader_id
, pshader_id
;
3134 const char *blt_vshader
[] = {
3137 " gl_Position = gl_Vertex;\n"
3138 " gl_FrontColor = vec4(1.0);\n"
3139 " gl_TexCoord[0].x = (gl_Vertex.x * 0.5) + 0.5;\n"
3140 " gl_TexCoord[0].y = (-gl_Vertex.y * 0.5) + 0.5;\n"
3144 const char *blt_pshader
[] = {
3145 "uniform sampler2D sampler;\n"
3148 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
3152 vshader_id
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
3153 GL_EXTCALL(glShaderSourceARB(vshader_id
, 1, blt_vshader
, NULL
));
3154 GL_EXTCALL(glCompileShaderARB(vshader_id
));
3156 pshader_id
= GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB
));
3157 GL_EXTCALL(glShaderSourceARB(pshader_id
, 1, blt_pshader
, NULL
));
3158 GL_EXTCALL(glCompileShaderARB(pshader_id
));
3160 program_id
= GL_EXTCALL(glCreateProgramObjectARB());
3161 GL_EXTCALL(glAttachObjectARB(program_id
, vshader_id
));
3162 GL_EXTCALL(glAttachObjectARB(program_id
, pshader_id
));
3163 GL_EXTCALL(glLinkProgramARB(program_id
));
3165 print_glsl_info_log(&GLINFO_LOCATION
, program_id
);
3170 static void shader_glsl_select(IWineD3DDevice
*iface
, BOOL usePS
, BOOL useVS
) {
3171 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3172 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3173 GLhandleARB program_id
= 0;
3175 if (useVS
|| usePS
) set_glsl_shader_program(iface
, usePS
, useVS
);
3176 else This
->stateBlock
->glsl_program
= NULL
;
3178 program_id
= This
->stateBlock
->glsl_program
? This
->stateBlock
->glsl_program
->programId
: 0;
3179 if (program_id
) TRACE("Using GLSL program %u\n", program_id
);
3180 GL_EXTCALL(glUseProgramObjectARB(program_id
));
3181 checkGLcall("glUseProgramObjectARB");
3184 static void shader_glsl_select_depth_blt(IWineD3DDevice
*iface
) {
3185 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3186 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3187 static GLhandleARB program_id
= 0;
3188 static GLhandleARB loc
= -1;
3191 program_id
= create_glsl_blt_shader(gl_info
);
3192 loc
= GL_EXTCALL(glGetUniformLocationARB(program_id
, "sampler"));
3195 GL_EXTCALL(glUseProgramObjectARB(program_id
));
3196 GL_EXTCALL(glUniform1iARB(loc
, 0));
3199 static void shader_glsl_cleanup(IWineD3DDevice
*iface
) {
3200 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3201 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3202 GL_EXTCALL(glUseProgramObjectARB(0));
3205 static void shader_glsl_destroy(IWineD3DBaseShader
*iface
) {
3206 struct list
*linked_programs
;
3207 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*) iface
;
3208 WineD3D_GL_Info
*gl_info
= &((IWineD3DDeviceImpl
*) This
->baseShader
.device
)->adapter
->gl_info
;
3210 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
3211 * can be called from IWineD3DBaseShader::Release
3213 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
3215 if(This
->baseShader
.prgId
== 0) return;
3216 linked_programs
= &This
->baseShader
.linked_programs
;
3218 TRACE("Deleting linked programs\n");
3219 if (linked_programs
->next
) {
3220 struct glsl_shader_prog_link
*entry
, *entry2
;
3223 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
, struct glsl_shader_prog_link
, pshader_entry
) {
3224 delete_glsl_program_entry(This
->baseShader
.device
, entry
);
3227 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
, struct glsl_shader_prog_link
, vshader_entry
) {
3228 delete_glsl_program_entry(This
->baseShader
.device
, entry
);
3233 TRACE("Deleting shader object %u\n", This
->baseShader
.prgId
);
3234 GL_EXTCALL(glDeleteObjectARB(This
->baseShader
.prgId
));
3235 checkGLcall("glDeleteObjectARB");
3238 const shader_backend_t glsl_shader_backend
= {
3239 &shader_glsl_select
,
3240 &shader_glsl_select_depth_blt
,
3241 &shader_glsl_load_constants
,
3242 &shader_glsl_cleanup
,
3243 &shader_glsl_color_correction
,
3244 &shader_glsl_destroy