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;
62 const char *spam
[] = {
63 "Vertex shader was successfully compiled to run on hardware.\n", /* fglrx */
64 "Fragment shader was successfully compiled to run on hardware.\n", /* fglrx */
65 "Fragment shader(s) linked, vertex shader(s) linked." /* fglrx, no \n */
68 GL_EXTCALL(glGetObjectParameterivARB(obj
,
69 GL_OBJECT_INFO_LOG_LENGTH_ARB
,
72 /* A size of 1 is just a null-terminated string, so the log should be bigger than
73 * that if there are errors. */
74 if (infologLength
> 1)
76 infoLog
= HeapAlloc(GetProcessHeap(), 0, infologLength
);
77 GL_EXTCALL(glGetInfoLogARB(obj
, infologLength
, NULL
, infoLog
));
80 for(i
= 0; i
< sizeof(spam
) / sizeof(spam
[0]); i
++) {
81 if(strcmp(infoLog
, spam
[i
]) == 0) {
87 TRACE("Spam received from GLSL shader #%u: %s\n", obj
, debugstr_a(infoLog
));
89 FIXME("Error received from GLSL shader #%u: %s\n", obj
, debugstr_a(infoLog
));
91 HeapFree(GetProcessHeap(), 0, infoLog
);
96 * Loads (pixel shader) samplers
98 static void shader_glsl_load_psamplers(
99 WineD3D_GL_Info
*gl_info
,
100 IWineD3DStateBlock
* iface
,
101 GLhandleARB programId
) {
103 IWineD3DStateBlockImpl
* stateBlock
= (IWineD3DStateBlockImpl
*) iface
;
104 GLhandleARB name_loc
;
106 char sampler_name
[20];
108 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
) {
109 snprintf(sampler_name
, sizeof(sampler_name
), "Psampler%d", i
);
110 name_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, sampler_name
));
111 if (name_loc
!= -1) {
112 int mapped_unit
= stateBlock
->wineD3DDevice
->texUnitMap
[i
];
113 if (mapped_unit
!= -1 && mapped_unit
< GL_LIMITS(fragment_samplers
)) {
114 TRACE("Loading %s for texture %d\n", sampler_name
, mapped_unit
);
115 GL_EXTCALL(glUniform1iARB(name_loc
, mapped_unit
));
116 checkGLcall("glUniform1iARB");
118 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name
, mapped_unit
);
124 static void shader_glsl_load_vsamplers(WineD3D_GL_Info
*gl_info
, IWineD3DStateBlock
* iface
, GLhandleARB programId
) {
125 IWineD3DStateBlockImpl
* stateBlock
= (IWineD3DStateBlockImpl
*) iface
;
126 GLhandleARB name_loc
;
127 char sampler_name
[20];
130 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
) {
131 snprintf(sampler_name
, sizeof(sampler_name
), "Vsampler%d", i
);
132 name_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, sampler_name
));
133 if (name_loc
!= -1) {
134 int mapped_unit
= stateBlock
->wineD3DDevice
->texUnitMap
[MAX_FRAGMENT_SAMPLERS
+ i
];
135 if (mapped_unit
!= -1 && mapped_unit
< GL_LIMITS(combined_samplers
)) {
136 TRACE("Loading %s for texture %d\n", sampler_name
, mapped_unit
);
137 GL_EXTCALL(glUniform1iARB(name_loc
, mapped_unit
));
138 checkGLcall("glUniform1iARB");
140 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name
, mapped_unit
);
147 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
148 * When constant_list == NULL, it will load all the constants.
150 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl
* This
, WineD3D_GL_Info
*gl_info
,
151 unsigned int max_constants
, float* constants
, GLhandleARB
*constant_locations
,
152 struct list
*constant_list
) {
153 constants_entry
*constant
;
154 local_constant
* lconst
;
159 if (TRACE_ON(d3d_shader
)) {
160 LIST_FOR_EACH_ENTRY(constant
, constant_list
, constants_entry
, entry
) {
165 tmp_loc
= constant_locations
[i
];
167 TRACE_(d3d_constants
)("Loading constants %i: %f, %f, %f, %f\n", i
,
168 constants
[i
* 4 + 0], constants
[i
* 4 + 1],
169 constants
[i
* 4 + 2], constants
[i
* 4 + 3]);
175 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
176 if(WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) == 1 &&
177 shader_is_pshader_version(This
->baseShader
.hex_version
)) {
180 LIST_FOR_EACH_ENTRY(constant
, constant_list
, constants_entry
, entry
) {
185 tmp_loc
= constant_locations
[i
];
187 /* We found this uniform name in the program - go ahead and send the data */
189 if(constants
[k
+ 0] < -1.0) lcl_const
[0] = -1.0;
190 else if(constants
[k
+ 0] > 1.0) lcl_const
[0] = 1.0;
191 else lcl_const
[0] = constants
[k
+ 0];
192 if(constants
[k
+ 1] < -1.0) lcl_const
[1] = -1.0;
193 else if(constants
[k
+ 1] > 1.0) lcl_const
[1] = 1.0;
194 else lcl_const
[1] = constants
[k
+ 1];
195 if(constants
[k
+ 2] < -1.0) lcl_const
[2] = -1.0;
196 else if(constants
[k
+ 2] > 1.0) lcl_const
[2] = 1.0;
197 else lcl_const
[2] = constants
[k
+ 2];
198 if(constants
[k
+ 3] < -1.0) lcl_const
[3] = -1.0;
199 else if(constants
[k
+ 3] > 1.0) lcl_const
[3] = 1.0;
200 else lcl_const
[3] = constants
[k
+ 3];
202 GL_EXTCALL(glUniform4fvARB(tmp_loc
, 1, lcl_const
));
207 LIST_FOR_EACH_ENTRY(constant
, constant_list
, constants_entry
, entry
) {
212 tmp_loc
= constant_locations
[i
];
214 /* We found this uniform name in the program - go ahead and send the data */
215 GL_EXTCALL(glUniform4fvARB(tmp_loc
, 1, constants
+ (i
* 4)));
220 checkGLcall("glUniform4fvARB()");
222 if(!This
->baseShader
.load_local_constsF
) {
223 TRACE("No need to load local float constants for this shader\n");
227 /* Load immediate constants */
228 if (TRACE_ON(d3d_shader
)) {
229 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
230 tmp_loc
= constant_locations
[lconst
->idx
];
232 GLfloat
* values
= (GLfloat
*)lconst
->value
;
233 TRACE_(d3d_constants
)("Loading local constants %i: %f, %f, %f, %f\n", lconst
->idx
,
234 values
[0], values
[1], values
[2], values
[3]);
238 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
239 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
240 tmp_loc
= constant_locations
[lconst
->idx
];
242 /* We found this uniform name in the program - go ahead and send the data */
243 GL_EXTCALL(glUniform4fvARB(tmp_loc
, 1, (GLfloat
*)lconst
->value
));
246 checkGLcall("glUniform4fvARB()");
250 * Loads integer constants (aka uniforms) into the currently set GLSL program.
251 * When @constants_set == NULL, it will load all the constants.
253 static void shader_glsl_load_constantsI(
254 IWineD3DBaseShaderImpl
* This
,
255 WineD3D_GL_Info
*gl_info
,
256 GLhandleARB programId
,
257 GLhandleARB locations
[MAX_CONST_I
],
258 unsigned max_constants
,
260 BOOL
* constants_set
) {
265 for (i
=0; i
<max_constants
; ++i
) {
266 if (NULL
== constants_set
|| constants_set
[i
]) {
268 TRACE_(d3d_constants
)("Loading constants %i: %i, %i, %i, %i\n",
269 i
, constants
[i
*4], constants
[i
*4+1], constants
[i
*4+2], constants
[i
*4+3]);
271 /* We found this uniform name in the program - go ahead and send the data */
272 GL_EXTCALL(glUniform4ivARB(locations
[i
], 1, &constants
[i
*4]));
273 checkGLcall("glUniform4ivARB");
277 /* Load immediate constants */
278 ptr
= list_head(&This
->baseShader
.constantsI
);
280 local_constant
* lconst
= LIST_ENTRY(ptr
, struct local_constant
, entry
);
281 unsigned int idx
= lconst
->idx
;
282 GLint
* values
= (GLint
*) lconst
->value
;
284 TRACE_(d3d_constants
)("Loading local constants %i: %i, %i, %i, %i\n", idx
,
285 values
[0], values
[1], values
[2], values
[3]);
287 /* We found this uniform name in the program - go ahead and send the data */
288 GL_EXTCALL(glUniform4ivARB(locations
[idx
], 1, values
));
289 checkGLcall("glUniform4ivARB");
290 ptr
= list_next(&This
->baseShader
.constantsI
, ptr
);
295 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
296 * When @constants_set == NULL, it will load all the constants.
298 static void shader_glsl_load_constantsB(
299 IWineD3DBaseShaderImpl
* This
,
300 WineD3D_GL_Info
*gl_info
,
301 GLhandleARB programId
,
302 unsigned max_constants
,
304 BOOL
* constants_set
) {
309 char is_pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
310 const char* prefix
= is_pshader
? "PB":"VB";
313 for (i
=0; i
<max_constants
; ++i
) {
314 if (NULL
== constants_set
|| constants_set
[i
]) {
316 TRACE_(d3d_constants
)("Loading constants %i: %i;\n", i
, constants
[i
]);
318 /* TODO: Benchmark and see if it would be beneficial to store the
319 * locations of the constants to avoid looking up each time */
320 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, i
);
321 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
323 /* We found this uniform name in the program - go ahead and send the data */
324 GL_EXTCALL(glUniform1ivARB(tmp_loc
, 1, &constants
[i
]));
325 checkGLcall("glUniform1ivARB");
330 /* Load immediate constants */
331 ptr
= list_head(&This
->baseShader
.constantsB
);
333 local_constant
* lconst
= LIST_ENTRY(ptr
, struct local_constant
, entry
);
334 unsigned int idx
= lconst
->idx
;
335 GLint
* values
= (GLint
*) lconst
->value
;
337 TRACE_(d3d_constants
)("Loading local constants %i: %i\n", idx
, values
[0]);
339 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, idx
);
340 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
342 /* We found this uniform name in the program - go ahead and send the data */
343 GL_EXTCALL(glUniform1ivARB(tmp_loc
, 1, values
));
344 checkGLcall("glUniform1ivARB");
346 ptr
= list_next(&This
->baseShader
.constantsB
, ptr
);
353 * Loads the app-supplied constants into the currently set GLSL program.
355 void shader_glsl_load_constants(
356 IWineD3DDevice
* device
,
358 char useVertexShader
) {
360 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) device
;
361 IWineD3DStateBlockImpl
* stateBlock
= deviceImpl
->stateBlock
;
362 WineD3D_GL_Info
*gl_info
= &deviceImpl
->adapter
->gl_info
;
364 GLhandleARB
*constant_locations
;
365 struct list
*constant_list
;
366 GLhandleARB programId
;
367 struct glsl_shader_prog_link
*prog
= stateBlock
->glsl_program
;
370 /* No GLSL program set - nothing to do. */
373 programId
= prog
->programId
;
375 if (useVertexShader
) {
376 IWineD3DBaseShaderImpl
* vshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->vertexShader
;
378 constant_locations
= prog
->vuniformF_locations
;
379 constant_list
= &stateBlock
->set_vconstantsF
;
381 /* Load DirectX 9 float constants/uniforms for vertex shader */
382 shader_glsl_load_constantsF(vshader
, gl_info
, GL_LIMITS(vshader_constantsF
),
383 stateBlock
->vertexShaderConstantF
, constant_locations
, constant_list
);
385 /* Load DirectX 9 integer constants/uniforms for vertex shader */
386 shader_glsl_load_constantsI(vshader
, gl_info
, programId
,
387 prog
->vuniformI_locations
, MAX_CONST_I
,
388 stateBlock
->vertexShaderConstantI
,
389 stateBlock
->changed
.vertexShaderConstantsI
);
391 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
392 shader_glsl_load_constantsB(vshader
, gl_info
, programId
, MAX_CONST_B
,
393 stateBlock
->vertexShaderConstantB
,
394 stateBlock
->changed
.vertexShaderConstantsB
);
396 /* Upload the position fixup params */
397 GL_EXTCALL(glUniform4fvARB(prog
->posFixup_location
, 1, &deviceImpl
->posFixup
[0]));
398 checkGLcall("glUniform4fvARB");
401 if (usePixelShader
) {
403 IWineD3DBaseShaderImpl
* pshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->pixelShader
;
405 constant_locations
= prog
->puniformF_locations
;
406 constant_list
= &stateBlock
->set_pconstantsF
;
408 /* Load DirectX 9 float constants/uniforms for pixel shader */
409 shader_glsl_load_constantsF(pshader
, gl_info
, GL_LIMITS(pshader_constantsF
),
410 stateBlock
->pixelShaderConstantF
, constant_locations
, constant_list
);
412 /* Load DirectX 9 integer constants/uniforms for pixel shader */
413 shader_glsl_load_constantsI(pshader
, gl_info
, programId
,
414 prog
->puniformI_locations
, MAX_CONST_I
,
415 stateBlock
->pixelShaderConstantI
,
416 stateBlock
->changed
.pixelShaderConstantsI
);
418 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
419 shader_glsl_load_constantsB(pshader
, gl_info
, programId
, MAX_CONST_B
,
420 stateBlock
->pixelShaderConstantB
,
421 stateBlock
->changed
.pixelShaderConstantsB
);
423 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
424 * It can't be 0 for a valid texbem instruction.
426 if(((IWineD3DPixelShaderImpl
*) pshader
)->needsbumpmat
!= -1) {
427 float *data
= (float *) &stateBlock
->textureState
[(int) ((IWineD3DPixelShaderImpl
*) pshader
)->needsbumpmat
][WINED3DTSS_BUMPENVMAT00
];
428 GL_EXTCALL(glUniformMatrix2fvARB(prog
->bumpenvmat_location
, 1, 0, data
));
429 checkGLcall("glUniformMatrix2fvARB");
431 /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
432 * is set too, so we can check that in the needsbumpmat check
434 if(((IWineD3DPixelShaderImpl
*) pshader
)->baseShader
.reg_maps
.luminanceparams
!= -1) {
435 int stage
= ((IWineD3DPixelShaderImpl
*) pshader
)->baseShader
.reg_maps
.luminanceparams
;
436 GLfloat
*scale
= (GLfloat
*) &stateBlock
->textureState
[stage
][WINED3DTSS_BUMPENVLSCALE
];
437 GLfloat
*offset
= (GLfloat
*) &stateBlock
->textureState
[stage
][WINED3DTSS_BUMPENVLOFFSET
];
439 GL_EXTCALL(glUniform1fvARB(prog
->luminancescale_location
, 1, scale
));
440 checkGLcall("glUniform1fvARB");
441 GL_EXTCALL(glUniform1fvARB(prog
->luminanceoffset_location
, 1, offset
));
442 checkGLcall("glUniform1fvARB");
444 } else if(((IWineD3DPixelShaderImpl
*) pshader
)->srgb_enabled
&&
445 !((IWineD3DPixelShaderImpl
*) pshader
)->srgb_mode_hardcoded
) {
449 if(stateBlock
->renderState
[WINED3DRS_SRGBWRITEENABLE
]) {
450 comparison
[0] = srgb_cmp
; comparison
[1] = srgb_cmp
;
451 comparison
[2] = srgb_cmp
; comparison
[3] = srgb_cmp
;
453 mul_low
[0] = srgb_mul_low
; mul_low
[1] = srgb_mul_low
;
454 mul_low
[2] = srgb_mul_low
; mul_low
[3] = srgb_mul_low
;
456 comparison
[0] = 1.0 / 0.0; comparison
[1] = 1.0 / 0.0;
457 comparison
[2] = 1.0 / 0.0; comparison
[3] = 1.0 / 0.0;
459 mul_low
[0] = 1.0; mul_low
[1] = 1.0;
460 mul_low
[2] = 1.0; mul_low
[3] = 1.0;
463 GL_EXTCALL(glUniform4fvARB(prog
->srgb_comparison_location
, 1, comparison
));
464 GL_EXTCALL(glUniform4fvARB(prog
->srgb_mul_low_location
, 1, mul_low
));
466 if(((IWineD3DPixelShaderImpl
*) pshader
)->vpos_uniform
) {
467 float correction_params
[4];
468 if(deviceImpl
->render_offscreen
) {
469 correction_params
[0] = 0.0;
470 correction_params
[1] = 1.0;
472 /* position is window relative, not viewport relative */
473 correction_params
[0] = ((IWineD3DSurfaceImpl
*) deviceImpl
->render_targets
[0])->currentDesc
.Height
;
474 correction_params
[1] = -1.0;
476 GL_EXTCALL(glUniform4fvARB(prog
->ycorrection_location
, 1, correction_params
));
481 /** Generate the variable & register declarations for the GLSL output target */
482 void shader_generate_glsl_declarations(
483 IWineD3DBaseShader
*iface
,
484 shader_reg_maps
* reg_maps
,
485 SHADER_BUFFER
* buffer
,
486 WineD3D_GL_Info
* gl_info
) {
488 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
489 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
491 unsigned int extra_constants_needed
= 0;
492 local_constant
* lconst
;
494 /* There are some minor differences between pixel and vertex shaders */
495 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
496 char prefix
= pshader
? 'P' : 'V';
498 /* Prototype the subroutines */
499 for (i
= 0; i
< This
->baseShader
.limits
.label
; i
++) {
500 if (reg_maps
->labels
[i
])
501 shader_addline(buffer
, "void subroutine%u();\n", i
);
504 /* Declare the constants (aka uniforms) */
505 if (This
->baseShader
.limits
.constant_float
> 0) {
506 unsigned max_constantsF
= min(This
->baseShader
.limits
.constant_float
,
507 (pshader
? GL_LIMITS(pshader_constantsF
) : GL_LIMITS(vshader_constantsF
)));
508 shader_addline(buffer
, "uniform vec4 %cC[%u];\n", prefix
, max_constantsF
);
511 if (This
->baseShader
.limits
.constant_int
> 0)
512 shader_addline(buffer
, "uniform ivec4 %cI[%u];\n", prefix
, This
->baseShader
.limits
.constant_int
);
514 if (This
->baseShader
.limits
.constant_bool
> 0)
515 shader_addline(buffer
, "uniform bool %cB[%u];\n", prefix
, This
->baseShader
.limits
.constant_bool
);
518 shader_addline(buffer
, "uniform vec4 posFixup;\n");
519 /* Predeclaration; This function is added at link time based on the pixel shader.
520 * VS 3.0 shaders have an array OUT[] the shader writes to, earlier versions don't have
521 * that. We know the input to the reorder function at vertex shader compile time, so
522 * we can deal with that. The reorder function for a 1.x and 2.x vertex shader can just
523 * read gl_FrontColor. The output depends on the pixel shader. The reorder function for a
524 * 1.x and 2.x pshader or for fixed function will write gl_FrontColor, and for a 3.0 shader
525 * it will write to the varying array. Here we depend on the shader optimizer on sorting that
526 * out. The nvidia driver only does that if the parameter is inout instead of out, hence the
529 if(This
->baseShader
.hex_version
>= WINED3DVS_VERSION(3, 0)) {
530 shader_addline(buffer
, "void order_ps_input(in vec4[%u]);\n", MAX_REG_OUTPUT
);
532 shader_addline(buffer
, "void order_ps_input();\n");
535 IWineD3DPixelShaderImpl
*ps_impl
= (IWineD3DPixelShaderImpl
*) This
;
537 if(reg_maps
->bumpmat
!= -1) {
538 shader_addline(buffer
, "uniform mat2 bumpenvmat;\n");
539 if(reg_maps
->luminanceparams
) {
540 shader_addline(buffer
, "uniform float luminancescale;\n");
541 shader_addline(buffer
, "uniform float luminanceoffset;\n");
542 extra_constants_needed
++;
544 extra_constants_needed
++;
547 if(device
->stateBlock
->renderState
[WINED3DRS_SRGBWRITEENABLE
]) {
548 ps_impl
->srgb_enabled
= 1;
549 if(This
->baseShader
.limits
.constant_float
+ extra_constants_needed
+ 1 < GL_LIMITS(pshader_constantsF
)) {
550 shader_addline(buffer
, "uniform vec4 srgb_mul_low;\n");
551 shader_addline(buffer
, "uniform vec4 srgb_comparison;\n");
552 ps_impl
->srgb_mode_hardcoded
= 0;
553 extra_constants_needed
++;
555 ps_impl
->srgb_mode_hardcoded
= 1;
556 shader_addline(buffer
, "const vec4 srgb_mul_low = vec4(%f, %f, %f, %f);\n",
557 srgb_mul_low
, srgb_mul_low
, srgb_mul_low
, srgb_mul_low
);
558 shader_addline(buffer
, "const vec4 srgb_comparison = vec4(%f, %f, %f, %f);\n",
559 srgb_cmp
, srgb_cmp
, srgb_cmp
, srgb_cmp
);
562 IWineD3DPixelShaderImpl
*ps_impl
= (IWineD3DPixelShaderImpl
*) This
;
564 /* Do not write any srgb fixup into the shader to save shader size and processing time.
565 * As a consequence, we can't toggle srgb write on without recompilation
567 ps_impl
->srgb_enabled
= 0;
568 ps_impl
->srgb_mode_hardcoded
= 1;
570 if(reg_maps
->vpos
|| reg_maps
->usesdsy
) {
571 if(This
->baseShader
.limits
.constant_float
+ extra_constants_needed
+ 1 < GL_LIMITS(pshader_constantsF
)) {
572 shader_addline(buffer
, "uniform vec4 ycorrection;\n");
573 ((IWineD3DPixelShaderImpl
*) This
)->vpos_uniform
= 1;
574 extra_constants_needed
++;
576 /* This happens because we do not have proper tracking of the constant registers that are
577 * actually used, only the max limit of the shader version
579 FIXME("Cannot find a free uniform for vpos correction params\n");
580 shader_addline(buffer
, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
581 device
->render_offscreen
? 0.0 : ((IWineD3DSurfaceImpl
*) device
->render_targets
[0])->currentDesc
.Height
,
582 device
->render_offscreen
? 1.0 : -1.0);
584 shader_addline(buffer
, "vec4 vpos;\n");
588 /* Declare texture samplers */
589 for (i
= 0; i
< This
->baseShader
.limits
.sampler
; i
++) {
590 if (reg_maps
->samplers
[i
]) {
592 DWORD stype
= reg_maps
->samplers
[i
] & WINED3DSP_TEXTURETYPE_MASK
;
596 shader_addline(buffer
, "uniform sampler1D %csampler%u;\n", prefix
, i
);
599 if(device
->stateBlock
->textures
[i
] &&
600 IWineD3DBaseTexture_GetTextureDimensions(device
->stateBlock
->textures
[i
]) == GL_TEXTURE_RECTANGLE_ARB
) {
601 shader_addline(buffer
, "uniform sampler2DRect %csampler%u;\n", prefix
, i
);
603 shader_addline(buffer
, "uniform sampler2D %csampler%u;\n", prefix
, i
);
606 case WINED3DSTT_CUBE
:
607 shader_addline(buffer
, "uniform samplerCube %csampler%u;\n", prefix
, i
);
609 case WINED3DSTT_VOLUME
:
610 shader_addline(buffer
, "uniform sampler3D %csampler%u;\n", prefix
, i
);
613 shader_addline(buffer
, "uniform unsupported_sampler %csampler%u;\n", prefix
, i
);
614 FIXME("Unrecognized sampler type: %#x\n", stype
);
620 /* Declare address variables */
621 for (i
= 0; i
< This
->baseShader
.limits
.address
; i
++) {
622 if (reg_maps
->address
[i
])
623 shader_addline(buffer
, "ivec4 A%d;\n", i
);
626 /* Declare texture coordinate temporaries and initialize them */
627 for (i
= 0; i
< This
->baseShader
.limits
.texcoord
; i
++) {
628 if (reg_maps
->texcoord
[i
])
629 shader_addline(buffer
, "vec4 T%u = gl_TexCoord[%u];\n", i
, i
);
632 /* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
633 * helper function shader that is linked in at link time
635 if(pshader
&& This
->baseShader
.hex_version
>= WINED3DPS_VERSION(3, 0)) {
637 shader_addline(buffer
, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings
) / 4);
639 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
640 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
641 * pixel shader that reads the fixed function color into the packed input registers.
643 shader_addline(buffer
, "vec4 IN[%u];\n", GL_LIMITS(glsl_varyings
) / 4);
647 /* Declare output register temporaries */
648 if(This
->baseShader
.limits
.packed_output
) {
649 shader_addline(buffer
, "vec4 OUT[%u];\n", This
->baseShader
.limits
.packed_output
);
652 /* Declare temporary variables */
653 for(i
= 0; i
< This
->baseShader
.limits
.temporary
; i
++) {
654 if (reg_maps
->temporary
[i
])
655 shader_addline(buffer
, "vec4 R%u;\n", i
);
658 /* Declare attributes */
659 for (i
= 0; i
< This
->baseShader
.limits
.attributes
; i
++) {
660 if (reg_maps
->attributes
[i
])
661 shader_addline(buffer
, "attribute vec4 attrib%i;\n", i
);
664 /* Declare loop registers aLx */
665 for (i
= 0; i
< reg_maps
->loop_depth
; i
++) {
666 shader_addline(buffer
, "int aL%u;\n", i
);
667 shader_addline(buffer
, "int tmpInt%u;\n", i
);
670 /* Temporary variables for matrix operations */
671 shader_addline(buffer
, "vec4 tmp0;\n");
672 shader_addline(buffer
, "vec4 tmp1;\n");
674 /* Hardcodable local constants */
675 if(!This
->baseShader
.load_local_constsF
) {
676 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
677 float *value
= (float *) lconst
->value
;
678 shader_addline(buffer
, "const vec4 LC%u = vec4(%f, %f, %f, %f);\n", lconst
->idx
,
679 value
[0], value
[1], value
[2], value
[3]);
683 /* Start the main program */
684 shader_addline(buffer
, "void main() {\n");
685 if(pshader
&& reg_maps
->vpos
) {
686 shader_addline(buffer
, "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1) - 0.5;\n");
690 /*****************************************************************************
691 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
693 * For more information, see http://wiki.winehq.org/DirectX-Shaders
694 ****************************************************************************/
697 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG
* arg
, const DWORD param
,
698 const DWORD addr_token
, DWORD mask
, glsl_src_param_t
*src_param
);
700 /** Used for opcode modifiers - They multiply the result by the specified amount */
701 static const char * const shift_glsl_tab
[] = {
703 "2.0 * ", /* 1 (x2) */
704 "4.0 * ", /* 2 (x4) */
705 "8.0 * ", /* 3 (x8) */
706 "16.0 * ", /* 4 (x16) */
707 "32.0 * ", /* 5 (x32) */
714 "0.0625 * ", /* 12 (d16) */
715 "0.125 * ", /* 13 (d8) */
716 "0.25 * ", /* 14 (d4) */
717 "0.5 * " /* 15 (d2) */
720 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
721 static void shader_glsl_gen_modifier (
724 const char *in_regswizzle
,
729 if (instr
== WINED3DSIO_TEXKILL
)
732 switch (instr
& WINED3DSP_SRCMOD_MASK
) {
733 case WINED3DSPSM_DZ
: /* Need to handle this in the instructions itself (texld & texcrd). */
735 case WINED3DSPSM_NONE
:
736 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
738 case WINED3DSPSM_NEG
:
739 sprintf(out_str
, "-%s%s", in_reg
, in_regswizzle
);
741 case WINED3DSPSM_NOT
:
742 sprintf(out_str
, "!%s%s", in_reg
, in_regswizzle
);
744 case WINED3DSPSM_BIAS
:
745 sprintf(out_str
, "(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
747 case WINED3DSPSM_BIASNEG
:
748 sprintf(out_str
, "-(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
750 case WINED3DSPSM_SIGN
:
751 sprintf(out_str
, "(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
753 case WINED3DSPSM_SIGNNEG
:
754 sprintf(out_str
, "-(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
756 case WINED3DSPSM_COMP
:
757 sprintf(out_str
, "(1.0 - %s%s)", in_reg
, in_regswizzle
);
760 sprintf(out_str
, "(2.0 * %s%s)", in_reg
, in_regswizzle
);
762 case WINED3DSPSM_X2NEG
:
763 sprintf(out_str
, "-(2.0 * %s%s)", in_reg
, in_regswizzle
);
765 case WINED3DSPSM_ABS
:
766 sprintf(out_str
, "abs(%s%s)", in_reg
, in_regswizzle
);
768 case WINED3DSPSM_ABSNEG
:
769 sprintf(out_str
, "-abs(%s%s)", in_reg
, in_regswizzle
);
772 FIXME("Unhandled modifier %u\n", (instr
& WINED3DSP_SRCMOD_MASK
));
773 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
777 static BOOL
constant_is_local(IWineD3DBaseShaderImpl
* This
, DWORD reg
) {
778 local_constant
* lconst
;
780 if(This
->baseShader
.load_local_constsF
) return FALSE
;
781 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
782 if(lconst
->idx
== reg
) return TRUE
;
788 /** Writes the GLSL variable name that corresponds to the register that the
789 * DX opcode parameter is trying to access */
790 static void shader_glsl_get_register_name(
792 const DWORD addr_token
,
795 SHADER_OPCODE_ARG
* arg
) {
797 /* oPos, oFog and oPts in D3D */
798 static const char * const hwrastout_reg_names
[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
800 DWORD reg
= param
& WINED3DSP_REGNUM_MASK
;
801 DWORD regtype
= shader_get_regtype(param
);
802 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
803 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
804 WineD3D_GL_Info
* gl_info
= &deviceImpl
->adapter
->gl_info
;
806 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
812 case WINED3DSPR_TEMP
:
813 sprintf(tmpStr
, "R%u", reg
);
815 case WINED3DSPR_INPUT
:
817 /* Pixel shaders >= 3.0 */
818 if (WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 3) {
819 if (param
& WINED3DSHADER_ADDRMODE_RELATIVE
) {
820 glsl_src_param_t rel_param
;
821 shader_glsl_add_src_param(arg
, addr_token
, 0, WINED3DSP_WRITEMASK_0
, &rel_param
);
823 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
826 if(((IWineD3DPixelShaderImpl
*) This
)->input_reg_map
[reg
]) {
827 sprintf(tmpStr
, "IN[%s + %u]", rel_param
.param_str
,
828 ((IWineD3DPixelShaderImpl
*) This
)->input_reg_map
[reg
]);
830 sprintf(tmpStr
, "IN[%s]", rel_param
.param_str
);
833 sprintf(tmpStr
, "IN[%u]",
834 ((IWineD3DPixelShaderImpl
*) This
)->input_reg_map
[reg
]);
838 strcpy(tmpStr
, "gl_Color");
840 strcpy(tmpStr
, "gl_SecondaryColor");
843 if (vshader_input_is_color((IWineD3DVertexShader
*) This
, reg
))
845 sprintf(tmpStr
, "attrib%u", reg
);
848 case WINED3DSPR_CONST
:
850 const char* prefix
= pshader
? "PC":"VC";
852 /* Relative addressing */
853 if (param
& WINED3DSHADER_ADDRMODE_RELATIVE
) {
855 /* Relative addressing on shaders 2.0+ have a relative address token,
856 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
857 if (WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 2) {
858 glsl_src_param_t rel_param
;
859 shader_glsl_add_src_param(arg
, addr_token
, 0, WINED3DSP_WRITEMASK_0
, &rel_param
);
861 sprintf(tmpStr
, "%s[%s + %u]", prefix
, rel_param
.param_str
, reg
);
863 sprintf(tmpStr
, "%s[%s]", prefix
, rel_param
.param_str
);
867 sprintf(tmpStr
, "%s[A0.x + %u]", prefix
, reg
);
869 sprintf(tmpStr
, "%s[A0.x]", prefix
);
874 if(constant_is_local(This
, reg
)) {
875 sprintf(tmpStr
, "LC%u", reg
);
877 sprintf(tmpStr
, "%s[%u]", prefix
, reg
);
883 case WINED3DSPR_CONSTINT
:
885 sprintf(tmpStr
, "PI[%u]", reg
);
887 sprintf(tmpStr
, "VI[%u]", reg
);
889 case WINED3DSPR_CONSTBOOL
:
891 sprintf(tmpStr
, "PB[%u]", reg
);
893 sprintf(tmpStr
, "VB[%u]", reg
);
895 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
897 sprintf(tmpStr
, "T%u", reg
);
899 sprintf(tmpStr
, "A%u", reg
);
902 case WINED3DSPR_LOOP
:
903 sprintf(tmpStr
, "aL%u", This
->baseShader
.cur_loop_regno
- 1);
905 case WINED3DSPR_SAMPLER
:
907 sprintf(tmpStr
, "Psampler%u", reg
);
909 sprintf(tmpStr
, "Vsampler%u", reg
);
911 case WINED3DSPR_COLOROUT
:
912 if (reg
>= GL_LIMITS(buffers
)) {
913 WARN("Write to render target %u, only %d supported\n", reg
, 4);
915 if (GL_SUPPORT(ARB_DRAW_BUFFERS
)) {
916 sprintf(tmpStr
, "gl_FragData[%u]", reg
);
917 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
918 sprintf(tmpStr
, "gl_FragColor");
921 case WINED3DSPR_RASTOUT
:
922 sprintf(tmpStr
, "%s", hwrastout_reg_names
[reg
]);
924 case WINED3DSPR_DEPTHOUT
:
925 sprintf(tmpStr
, "gl_FragDepth");
927 case WINED3DSPR_ATTROUT
:
929 sprintf(tmpStr
, "gl_FrontColor");
931 sprintf(tmpStr
, "gl_FrontSecondaryColor");
934 case WINED3DSPR_TEXCRDOUT
:
935 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
936 if (WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 3)
937 sprintf(tmpStr
, "OUT[%u]", reg
);
939 sprintf(tmpStr
, "gl_TexCoord[%u]", reg
);
941 case WINED3DSPR_MISCTYPE
:
944 sprintf(tmpStr
, "vpos");
945 } else if (reg
== 1){
946 /* Note that gl_FrontFacing is a bool, while vFace is
947 * a float for which the sign determines front/back
949 sprintf(tmpStr
, "(gl_FrontFacing ? 1.0 : -1.0)");
951 FIXME("Unhandled misctype register %d\n", reg
);
952 sprintf(tmpStr
, "unrecognized_register");
956 FIXME("Unhandled register name Type(%d)\n", regtype
);
957 sprintf(tmpStr
, "unrecognized_register");
961 strcat(regstr
, tmpStr
);
964 /* Get the GLSL write mask for the destination register */
965 static DWORD
shader_glsl_get_write_mask(const DWORD param
, char *write_mask
) {
966 char *ptr
= write_mask
;
967 DWORD mask
= param
& WINED3DSP_WRITEMASK_ALL
;
969 if (shader_is_scalar(param
)) {
970 mask
= WINED3DSP_WRITEMASK_0
;
973 if (param
& WINED3DSP_WRITEMASK_0
) *ptr
++ = 'x';
974 if (param
& WINED3DSP_WRITEMASK_1
) *ptr
++ = 'y';
975 if (param
& WINED3DSP_WRITEMASK_2
) *ptr
++ = 'z';
976 if (param
& WINED3DSP_WRITEMASK_3
) *ptr
++ = 'w';
984 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask
) {
985 unsigned int size
= 0;
987 if (write_mask
& WINED3DSP_WRITEMASK_0
) ++size
;
988 if (write_mask
& WINED3DSP_WRITEMASK_1
) ++size
;
989 if (write_mask
& WINED3DSP_WRITEMASK_2
) ++size
;
990 if (write_mask
& WINED3DSP_WRITEMASK_3
) ++size
;
995 static void shader_glsl_get_swizzle(const DWORD param
, BOOL fixup
, DWORD mask
, char *swizzle_str
) {
996 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
997 * but addressed as "rgba". To fix this we need to swap the register's x
998 * and z components. */
999 DWORD swizzle
= (param
& WINED3DSP_SWIZZLE_MASK
) >> WINED3DSP_SWIZZLE_SHIFT
;
1000 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
1001 char *ptr
= swizzle_str
;
1003 if (!shader_is_scalar(param
)) {
1005 /* swizzle bits fields: wwzzyyxx */
1006 if (mask
& WINED3DSP_WRITEMASK_0
) *ptr
++ = swizzle_chars
[swizzle
& 0x03];
1007 if (mask
& WINED3DSP_WRITEMASK_1
) *ptr
++ = swizzle_chars
[(swizzle
>> 2) & 0x03];
1008 if (mask
& WINED3DSP_WRITEMASK_2
) *ptr
++ = swizzle_chars
[(swizzle
>> 4) & 0x03];
1009 if (mask
& WINED3DSP_WRITEMASK_3
) *ptr
++ = swizzle_chars
[(swizzle
>> 6) & 0x03];
1015 /* From a given parameter token, generate the corresponding GLSL string.
1016 * Also, return the actual register name and swizzle in case the
1017 * caller needs this information as well. */
1018 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG
* arg
, const DWORD param
,
1019 const DWORD addr_token
, DWORD mask
, glsl_src_param_t
*src_param
) {
1020 BOOL is_color
= FALSE
;
1021 char swizzle_str
[6];
1023 src_param
->reg_name
[0] = '\0';
1024 src_param
->param_str
[0] = '\0';
1025 swizzle_str
[0] = '\0';
1027 shader_glsl_get_register_name(param
, addr_token
, src_param
->reg_name
, &is_color
, arg
);
1029 shader_glsl_get_swizzle(param
, is_color
, mask
, swizzle_str
);
1030 shader_glsl_gen_modifier(param
, src_param
->reg_name
, swizzle_str
, src_param
->param_str
);
1033 /* From a given parameter token, generate the corresponding GLSL string.
1034 * Also, return the actual register name and swizzle in case the
1035 * caller needs this information as well. */
1036 static DWORD
shader_glsl_add_dst_param(SHADER_OPCODE_ARG
* arg
, const DWORD param
,
1037 const DWORD addr_token
, glsl_dst_param_t
*dst_param
) {
1038 BOOL is_color
= FALSE
;
1040 dst_param
->mask_str
[0] = '\0';
1041 dst_param
->reg_name
[0] = '\0';
1043 shader_glsl_get_register_name(param
, addr_token
, dst_param
->reg_name
, &is_color
, arg
);
1044 return shader_glsl_get_write_mask(param
, dst_param
->mask_str
);
1047 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1048 static DWORD
shader_glsl_append_dst_ext(SHADER_BUFFER
*buffer
, SHADER_OPCODE_ARG
*arg
, const DWORD param
) {
1049 glsl_dst_param_t dst_param
;
1053 mask
= shader_glsl_add_dst_param(arg
, param
, arg
->dst_addr
, &dst_param
);
1056 shift
= (param
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
1057 shader_addline(buffer
, "%s%s = %s(", dst_param
.reg_name
, dst_param
.mask_str
, shift_glsl_tab
[shift
]);
1063 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1064 static DWORD
shader_glsl_append_dst(SHADER_BUFFER
*buffer
, SHADER_OPCODE_ARG
*arg
) {
1065 return shader_glsl_append_dst_ext(buffer
, arg
, arg
->dst
);
1068 /** Process GLSL instruction modifiers */
1069 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG
* arg
) {
1071 DWORD mask
= arg
->dst
& WINED3DSP_DSTMOD_MASK
;
1073 if (arg
->opcode
->dst_token
&& mask
!= 0) {
1074 glsl_dst_param_t dst_param
;
1076 shader_glsl_add_dst_param(arg
, arg
->dst
, 0, &dst_param
);
1078 if (mask
& WINED3DSPDM_SATURATE
) {
1079 /* _SAT means to clamp the value of the register to between 0 and 1 */
1080 shader_addline(arg
->buffer
, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param
.reg_name
,
1081 dst_param
.mask_str
, dst_param
.reg_name
, dst_param
.mask_str
);
1083 if (mask
& WINED3DSPDM_MSAMPCENTROID
) {
1084 FIXME("_centroid modifier not handled\n");
1086 if (mask
& WINED3DSPDM_PARTIALPRECISION
) {
1087 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1092 static inline const char* shader_get_comp_op(
1093 const DWORD opcode
) {
1095 DWORD op
= (opcode
& INST_CONTROLS_MASK
) >> INST_CONTROLS_SHIFT
;
1097 case COMPARISON_GT
: return ">";
1098 case COMPARISON_EQ
: return "==";
1099 case COMPARISON_GE
: return ">=";
1100 case COMPARISON_LT
: return "<";
1101 case COMPARISON_NE
: return "!=";
1102 case COMPARISON_LE
: return "<=";
1104 FIXME("Unrecognized comparison value: %u\n", op
);
1109 static void shader_glsl_get_sample_function(DWORD sampler_type
, BOOL projected
, BOOL texrect
, glsl_sample_function_t
*sample_function
) {
1110 /* Note that there's no such thing as a projected cube texture. */
1111 switch(sampler_type
) {
1113 sample_function
->name
= projected
? "texture1DProj" : "texture1D";
1114 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
;
1118 sample_function
->name
= projected
? "texture2DRectProj" : "texture2DRect";
1120 sample_function
->name
= projected
? "texture2DProj" : "texture2D";
1122 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
;
1124 case WINED3DSTT_CUBE
:
1125 sample_function
->name
= "textureCube";
1126 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1128 case WINED3DSTT_VOLUME
:
1129 sample_function
->name
= projected
? "texture3DProj" : "texture3D";
1130 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1133 sample_function
->name
= "";
1134 FIXME("Unrecognized sampler type: %#x;\n", sampler_type
);
1139 static void shader_glsl_color_correction(SHADER_OPCODE_ARG
* arg
) {
1140 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
1141 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) shader
->baseShader
.device
;
1142 WineD3D_GL_Info
*gl_info
= &deviceImpl
->adapter
->gl_info
;
1143 glsl_dst_param_t dst_param
;
1144 glsl_dst_param_t dst_param2
;
1146 WINED3DFORMAT conversion_group
;
1147 IWineD3DBaseTextureImpl
*texture
;
1148 DWORD mask
, mask_size
;
1150 BOOL recorded
= FALSE
;
1152 DWORD hex_version
= shader
->baseShader
.hex_version
;
1154 switch(arg
->opcode
->opcode
) {
1155 case WINED3DSIO_TEX
:
1156 if (hex_version
< WINED3DPS_VERSION(2,0)) {
1157 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1159 sampler_idx
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
1163 case WINED3DSIO_TEXLDL
:
1164 FIXME("Add color fixup for vertex texture WINED3DSIO_TEXLDL\n");
1167 case WINED3DSIO_TEXDP3TEX
:
1168 case WINED3DSIO_TEXM3x3TEX
:
1169 case WINED3DSIO_TEXM3x3SPEC
:
1170 case WINED3DSIO_TEXM3x3VSPEC
:
1171 case WINED3DSIO_TEXBEM
:
1172 case WINED3DSIO_TEXREG2AR
:
1173 case WINED3DSIO_TEXREG2GB
:
1174 case WINED3DSIO_TEXREG2RGB
:
1175 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1179 /* Not a texture sampling instruction, nothing to do */
1183 texture
= (IWineD3DBaseTextureImpl
*) deviceImpl
->stateBlock
->textures
[sampler_idx
];
1185 fmt
= texture
->resource
.format
;
1186 conversion_group
= texture
->baseTexture
.shader_conversion_group
;
1188 fmt
= WINED3DFMT_UNKNOWN
;
1189 conversion_group
= WINED3DFMT_UNKNOWN
;
1192 /* before doing anything, record the sampler with the format in the format conversion list,
1193 * but check if it's not there already
1195 for(i
= 0; i
< shader
->baseShader
.num_sampled_samplers
; i
++) {
1196 if(shader
->baseShader
.sampled_samplers
[i
] == sampler_idx
) {
1202 shader
->baseShader
.sampled_samplers
[shader
->baseShader
.num_sampled_samplers
] = sampler_idx
;
1203 shader
->baseShader
.num_sampled_samplers
++;
1204 shader
->baseShader
.sampled_format
[sampler_idx
] = conversion_group
;
1208 case WINED3DFMT_V8U8
:
1209 case WINED3DFMT_V16U16
:
1210 if(GL_SUPPORT(NV_TEXTURE_SHADER
) ||
1211 (GL_SUPPORT(ATI_ENVMAP_BUMPMAP
) && fmt
== WINED3DFMT_V8U8
)) {
1212 /* The 3rd channel returns 1.0 in d3d, but 0.0 in gl. Fix this while we're at it :-) */
1213 mask
= shader_glsl_add_dst_param(arg
, arg
->dst
, WINED3DSP_WRITEMASK_2
, &dst_param
);
1214 mask_size
= shader_glsl_get_write_mask_size(mask
);
1215 if(mask_size
>= 3) {
1216 shader_addline(arg
->buffer
, "%s.%c = 1.0;\n", dst_param
.reg_name
, dst_param
.mask_str
[3]);
1219 /* Correct the sign, but leave the blue as it is - it was loaded correctly already */
1220 mask
= shader_glsl_add_dst_param(arg
, arg
->dst
,
1221 WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
,
1223 mask_size
= shader_glsl_get_write_mask_size(mask
);
1224 if(mask_size
>= 2) {
1225 shader_addline(arg
->buffer
, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1226 dst_param
.reg_name
, dst_param
.mask_str
[1], dst_param
.mask_str
[2],
1227 dst_param
.reg_name
, dst_param
.mask_str
[1], dst_param
.mask_str
[2]);
1228 } else if(mask_size
== 1) {
1229 shader_addline(arg
->buffer
, "%s.%c = %s.%c * 2.0 - 1.0;\n", dst_param
.reg_name
, dst_param
.mask_str
[1],
1230 dst_param
.reg_name
, dst_param
.mask_str
[1]);
1235 case WINED3DFMT_X8L8V8U8
:
1236 if(!GL_SUPPORT(NV_TEXTURE_SHADER
)) {
1237 /* Red and blue are the signed channels, fix them up; Blue(=L) is correct already,
1238 * and a(X) is always 1.0
1240 mask
= shader_glsl_add_dst_param(arg
, arg
->dst
, WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &dst_param
);
1241 mask_size
= shader_glsl_get_write_mask_size(mask
);
1242 if(mask_size
>= 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
[1], dst_param
.mask_str
[2],
1245 dst_param
.reg_name
, dst_param
.mask_str
[1], dst_param
.mask_str
[2]);
1246 } else if(mask_size
== 1) {
1247 shader_addline(arg
->buffer
, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1248 dst_param
.reg_name
, dst_param
.mask_str
[1],
1249 dst_param
.reg_name
, dst_param
.mask_str
[1]);
1254 case WINED3DFMT_L6V5U5
:
1255 if(!GL_SUPPORT(NV_TEXTURE_SHADER
)) {
1256 mask
= shader_glsl_add_dst_param(arg
, arg
->dst
, WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &dst_param
);
1257 mask_size
= shader_glsl_get_write_mask_size(mask
);
1258 shader_glsl_add_dst_param(arg
, arg
->dst
, WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_2
, &dst_param2
);
1259 if(mask_size
>= 3) {
1260 /* Swap y and z (U and L), and do a sign conversion on x and the new y(V and U) */
1261 shader_addline(arg
->buffer
, "tmp0.g = %s.%c;\n",
1262 dst_param
.reg_name
, dst_param
.mask_str
[2]);
1263 shader_addline(arg
->buffer
, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1264 dst_param
.reg_name
, dst_param
.mask_str
[2], dst_param
.mask_str
[1],
1265 dst_param2
.reg_name
, dst_param
.mask_str
[1], dst_param
.mask_str
[3]);
1266 shader_addline(arg
->buffer
, "%s.%c = tmp0.g;\n", dst_param
.reg_name
,
1267 dst_param
.mask_str
[3]);
1268 } else if(mask_size
== 2) {
1269 /* This is bad: We have VL, but we need VU */
1270 FIXME("2 components sampled from a converted L6V5U5 texture\n");
1272 shader_addline(arg
->buffer
, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1273 dst_param
.reg_name
, dst_param
.mask_str
[1],
1274 dst_param2
.reg_name
, dst_param
.mask_str
[1]);
1279 case WINED3DFMT_Q8W8V8U8
:
1280 if(!GL_SUPPORT(NV_TEXTURE_SHADER
)) {
1281 /* Correct the sign in all channels. The writemask just applies as-is, no
1282 * need for checking the mask size
1284 shader_glsl_add_dst_param(arg
, arg
->dst
,
1285 WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
|
1286 WINED3DSP_WRITEMASK_2
| WINED3DSP_WRITEMASK_3
,
1288 shader_addline(arg
->buffer
, "%s%s = %s%s * 2.0 - 1.0;\n", dst_param
.reg_name
, dst_param
.mask_str
,
1289 dst_param
.reg_name
, dst_param
.mask_str
);
1293 /* stupid compiler */
1299 /*****************************************************************************
1301 * Begin processing individual instruction opcodes
1303 ****************************************************************************/
1305 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
1306 void shader_glsl_arith(SHADER_OPCODE_ARG
* arg
) {
1307 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
1308 SHADER_BUFFER
* buffer
= arg
->buffer
;
1309 glsl_src_param_t src0_param
;
1310 glsl_src_param_t src1_param
;
1314 /* Determine the GLSL operator to use based on the opcode */
1315 switch (curOpcode
->opcode
) {
1316 case WINED3DSIO_MUL
: op
= '*'; break;
1317 case WINED3DSIO_ADD
: op
= '+'; break;
1318 case WINED3DSIO_SUB
: op
= '-'; break;
1321 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode
->name
);
1325 write_mask
= shader_glsl_append_dst(buffer
, arg
);
1326 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1327 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1328 shader_addline(buffer
, "%s %c %s);\n", src0_param
.param_str
, op
, src1_param
.param_str
);
1331 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1332 void shader_glsl_mov(SHADER_OPCODE_ARG
* arg
) {
1333 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
1334 SHADER_BUFFER
* buffer
= arg
->buffer
;
1335 glsl_src_param_t src0_param
;
1338 write_mask
= shader_glsl_append_dst(buffer
, arg
);
1339 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1341 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1342 * shader versions WINED3DSIO_MOVA is used for this. */
1343 if ((WINED3DSHADER_VERSION_MAJOR(shader
->baseShader
.hex_version
) == 1 &&
1344 !shader_is_pshader_version(shader
->baseShader
.hex_version
) &&
1345 shader_get_regtype(arg
->dst
) == WINED3DSPR_ADDR
)) {
1346 /* This is a simple floor() */
1347 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1348 if (mask_size
> 1) {
1349 shader_addline(buffer
, "ivec%d(floor(%s)));\n", mask_size
, src0_param
.param_str
);
1351 shader_addline(buffer
, "int(floor(%s)));\n", src0_param
.param_str
);
1353 } else if(arg
->opcode
->opcode
== WINED3DSIO_MOVA
) {
1354 /* We need to *round* to the nearest int here. */
1355 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1356 if (mask_size
> 1) {
1357 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
);
1359 shader_addline(buffer
, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param
.param_str
, src0_param
.param_str
);
1362 shader_addline(buffer
, "%s);\n", src0_param
.param_str
);
1366 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
1367 void shader_glsl_dot(SHADER_OPCODE_ARG
* arg
) {
1368 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
1369 SHADER_BUFFER
* buffer
= arg
->buffer
;
1370 glsl_src_param_t src0_param
;
1371 glsl_src_param_t src1_param
;
1372 DWORD dst_write_mask
, src_write_mask
;
1373 unsigned int dst_size
= 0;
1375 dst_write_mask
= shader_glsl_append_dst(buffer
, arg
);
1376 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
1378 /* dp3 works on vec3, dp4 on vec4 */
1379 if (curOpcode
->opcode
== WINED3DSIO_DP4
) {
1380 src_write_mask
= WINED3DSP_WRITEMASK_ALL
;
1382 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1385 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_write_mask
, &src0_param
);
1386 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], src_write_mask
, &src1_param
);
1389 shader_addline(buffer
, "vec%d(dot(%s, %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
1391 shader_addline(buffer
, "dot(%s, %s));\n", src0_param
.param_str
, src1_param
.param_str
);
1395 /* Note that this instruction has some restrictions. The destination write mask
1396 * can't contain the w component, and the source swizzles have to be .xyzw */
1397 void shader_glsl_cross(SHADER_OPCODE_ARG
*arg
) {
1398 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1399 glsl_src_param_t src0_param
;
1400 glsl_src_param_t src1_param
;
1403 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1404 shader_glsl_append_dst(arg
->buffer
, arg
);
1405 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
1406 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], src_mask
, &src1_param
);
1407 shader_addline(arg
->buffer
, "cross(%s, %s)%s);\n", src0_param
.param_str
, src1_param
.param_str
, dst_mask
);
1410 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1411 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1412 * GLSL uses the value as-is. */
1413 void shader_glsl_pow(SHADER_OPCODE_ARG
*arg
) {
1414 SHADER_BUFFER
*buffer
= arg
->buffer
;
1415 glsl_src_param_t src0_param
;
1416 glsl_src_param_t src1_param
;
1417 DWORD dst_write_mask
;
1418 unsigned int dst_size
;
1420 dst_write_mask
= shader_glsl_append_dst(buffer
, arg
);
1421 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
1423 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1424 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
1427 shader_addline(buffer
, "vec%d(pow(abs(%s), %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
1429 shader_addline(buffer
, "pow(abs(%s), %s));\n", src0_param
.param_str
, src1_param
.param_str
);
1433 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
1434 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
1435 * GLSL uses the value as-is. */
1436 void shader_glsl_log(SHADER_OPCODE_ARG
*arg
) {
1437 SHADER_BUFFER
*buffer
= arg
->buffer
;
1438 glsl_src_param_t src0_param
;
1439 DWORD dst_write_mask
;
1440 unsigned int dst_size
;
1442 dst_write_mask
= shader_glsl_append_dst(buffer
, arg
);
1443 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
1445 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1448 shader_addline(buffer
, "vec%d(log2(abs(%s))));\n", dst_size
, src0_param
.param_str
);
1450 shader_addline(buffer
, "log2(abs(%s)));\n", src0_param
.param_str
);
1454 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1455 void shader_glsl_map2gl(SHADER_OPCODE_ARG
* arg
) {
1456 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
1457 SHADER_BUFFER
* buffer
= arg
->buffer
;
1458 glsl_src_param_t src_param
;
1459 const char *instruction
;
1460 char arguments
[256];
1464 /* Determine the GLSL function to use based on the opcode */
1465 /* TODO: Possibly make this a table for faster lookups */
1466 switch (curOpcode
->opcode
) {
1467 case WINED3DSIO_MIN
: instruction
= "min"; break;
1468 case WINED3DSIO_MAX
: instruction
= "max"; break;
1469 case WINED3DSIO_ABS
: instruction
= "abs"; break;
1470 case WINED3DSIO_FRC
: instruction
= "fract"; break;
1471 case WINED3DSIO_NRM
: instruction
= "normalize"; break;
1472 case WINED3DSIO_LOGP
:
1473 case WINED3DSIO_LOG
: instruction
= "log2"; break;
1474 case WINED3DSIO_EXP
: instruction
= "exp2"; break;
1475 case WINED3DSIO_SGN
: instruction
= "sign"; break;
1476 case WINED3DSIO_DSX
: instruction
= "dFdx"; break;
1477 case WINED3DSIO_DSY
: instruction
= "ycorrection.y * dFdy"; break;
1478 default: instruction
= "";
1479 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode
->name
);
1483 write_mask
= shader_glsl_append_dst(buffer
, arg
);
1485 arguments
[0] = '\0';
1486 if (curOpcode
->num_params
> 0) {
1487 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src_param
);
1488 strcat(arguments
, src_param
.param_str
);
1489 for (i
= 2; i
< curOpcode
->num_params
; ++i
) {
1490 strcat(arguments
, ", ");
1491 shader_glsl_add_src_param(arg
, arg
->src
[i
-1], arg
->src_addr
[i
-1], write_mask
, &src_param
);
1492 strcat(arguments
, src_param
.param_str
);
1496 shader_addline(buffer
, "%s(%s));\n", instruction
, arguments
);
1499 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1500 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1501 * dst.x = 2^(floor(src))
1502 * dst.y = src - floor(src)
1503 * dst.z = 2^src (partial precision is allowed, but optional)
1505 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1506 * dst = 2^src; (partial precision is allowed, but optional)
1508 void shader_glsl_expp(SHADER_OPCODE_ARG
* arg
) {
1509 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)arg
->shader
;
1510 glsl_src_param_t src_param
;
1512 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
1514 if (shader
->baseShader
.hex_version
< WINED3DPS_VERSION(2,0)) {
1517 shader_addline(arg
->buffer
, "tmp0.x = exp2(floor(%s));\n", src_param
.param_str
);
1518 shader_addline(arg
->buffer
, "tmp0.y = %s - floor(%s);\n", src_param
.param_str
, src_param
.param_str
);
1519 shader_addline(arg
->buffer
, "tmp0.z = exp2(%s);\n", src_param
.param_str
);
1520 shader_addline(arg
->buffer
, "tmp0.w = 1.0;\n");
1522 shader_glsl_append_dst(arg
->buffer
, arg
);
1523 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1524 shader_addline(arg
->buffer
, "tmp0%s);\n", dst_mask
);
1527 unsigned int mask_size
;
1529 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1530 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1532 if (mask_size
> 1) {
1533 shader_addline(arg
->buffer
, "vec%d(exp2(%s)));\n", mask_size
, src_param
.param_str
);
1535 shader_addline(arg
->buffer
, "exp2(%s));\n", src_param
.param_str
);
1540 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1541 void shader_glsl_rcp(SHADER_OPCODE_ARG
* arg
) {
1542 glsl_src_param_t src_param
;
1544 unsigned int mask_size
;
1546 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1547 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1548 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
1550 if (mask_size
> 1) {
1551 shader_addline(arg
->buffer
, "vec%d(1.0 / %s));\n", mask_size
, src_param
.param_str
);
1553 shader_addline(arg
->buffer
, "1.0 / %s);\n", src_param
.param_str
);
1557 void shader_glsl_rsq(SHADER_OPCODE_ARG
* arg
) {
1558 SHADER_BUFFER
* buffer
= arg
->buffer
;
1559 glsl_src_param_t src_param
;
1561 unsigned int mask_size
;
1563 write_mask
= shader_glsl_append_dst(buffer
, arg
);
1564 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1566 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
1568 if (mask_size
> 1) {
1569 shader_addline(buffer
, "vec%d(inversesqrt(%s)));\n", mask_size
, src_param
.param_str
);
1571 shader_addline(buffer
, "inversesqrt(%s));\n", src_param
.param_str
);
1575 /** Process signed comparison opcodes in GLSL. */
1576 void shader_glsl_compare(SHADER_OPCODE_ARG
* arg
) {
1577 glsl_src_param_t src0_param
;
1578 glsl_src_param_t src1_param
;
1580 unsigned int mask_size
;
1582 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1583 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1584 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1585 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1587 if (mask_size
> 1) {
1588 const char *compare
;
1590 switch(arg
->opcode
->opcode
) {
1591 case WINED3DSIO_SLT
: compare
= "lessThan"; break;
1592 case WINED3DSIO_SGE
: compare
= "greaterThanEqual"; break;
1593 default: compare
= "";
1594 FIXME("Can't handle opcode %s\n", arg
->opcode
->name
);
1597 shader_addline(arg
->buffer
, "vec%d(%s(%s, %s)));\n", mask_size
, compare
,
1598 src0_param
.param_str
, src1_param
.param_str
);
1600 switch(arg
->opcode
->opcode
) {
1601 case WINED3DSIO_SLT
:
1602 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
1603 * to return 0.0 but step returns 1.0 because step is not < x
1604 * An alternative is a bvec compare padded with an unused second component.
1605 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
1606 * issue. Playing with not() is not possible either because not() does not accept
1609 shader_addline(arg
->buffer
, "(%s < %s) ? 1.0 : 0.0);\n", src0_param
.param_str
, src1_param
.param_str
);
1611 case WINED3DSIO_SGE
:
1612 /* Here we can use the step() function and safe a conditional */
1613 shader_addline(arg
->buffer
, "step(%s, %s));\n", src1_param
.param_str
, src0_param
.param_str
);
1616 FIXME("Can't handle opcode %s\n", arg
->opcode
->name
);
1622 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1623 void shader_glsl_cmp(SHADER_OPCODE_ARG
* arg
) {
1624 glsl_src_param_t src0_param
;
1625 glsl_src_param_t src1_param
;
1626 glsl_src_param_t src2_param
;
1627 DWORD write_mask
, cmp_channel
= 0;
1630 BOOL temp_destination
= FALSE
;
1632 DWORD src0reg
= arg
->src
[0] & WINED3DSP_REGNUM_MASK
;
1633 DWORD src1reg
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
1634 DWORD src2reg
= arg
->src
[2] & WINED3DSP_REGNUM_MASK
;
1635 DWORD src0regtype
= shader_get_regtype(arg
->src
[0]);
1636 DWORD src1regtype
= shader_get_regtype(arg
->src
[1]);
1637 DWORD src2regtype
= shader_get_regtype(arg
->src
[2]);
1638 DWORD dstreg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1639 DWORD dstregtype
= shader_get_regtype(arg
->dst
);
1641 /* Cycle through all source0 channels */
1642 for (i
=0; i
<4; i
++) {
1644 /* Find the destination channels which use the current source0 channel */
1645 for (j
=0; j
<4; j
++) {
1646 if ( ((arg
->src
[0] >> (WINED3DSP_SWIZZLE_SHIFT
+ 2*j
)) & 0x3) == i
) {
1647 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
1648 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
1652 /* Splitting the cmp instruction up in multiple lines imposes a problem:
1653 * The first lines may overwrite source parameters of the following lines.
1654 * Deal with that by using a temporary destination register if needed
1656 if((src0reg
== dstreg
&& src0regtype
== dstregtype
) ||
1657 (src1reg
== dstreg
&& src1regtype
== dstregtype
) ||
1658 (src2reg
== dstreg
&& src2regtype
== dstregtype
)) {
1660 write_mask
= shader_glsl_get_write_mask(arg
->dst
& (~WINED3DSP_SWIZZLE_MASK
| write_mask
), mask_char
);
1661 if (!write_mask
) continue;
1662 shader_addline(arg
->buffer
, "tmp0%s = (", mask_char
);
1663 temp_destination
= TRUE
;
1665 write_mask
= shader_glsl_append_dst_ext(arg
->buffer
, arg
, arg
->dst
& (~WINED3DSP_SWIZZLE_MASK
| write_mask
));
1666 if (!write_mask
) continue;
1669 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], cmp_channel
, &src0_param
);
1670 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1671 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1673 shader_addline(arg
->buffer
, "%s >= 0.0 ? %s : %s);\n",
1674 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1677 if(temp_destination
) {
1678 shader_glsl_get_write_mask(arg
->dst
, mask_char
);
1679 shader_glsl_append_dst_ext(arg
->buffer
, arg
, arg
->dst
);
1680 shader_addline(arg
->buffer
, "tmp0%s);\n", mask_char
);
1685 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
1686 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1687 * the compare is done per component of src0. */
1688 void shader_glsl_cnd(SHADER_OPCODE_ARG
* arg
) {
1689 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
1690 glsl_src_param_t src0_param
;
1691 glsl_src_param_t src1_param
;
1692 glsl_src_param_t src2_param
;
1693 DWORD write_mask
, cmp_channel
= 0;
1696 if (shader
->baseShader
.hex_version
< WINED3DPS_VERSION(1, 4)) {
1697 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1698 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1699 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1700 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1702 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
1703 if(arg
->opcode_token
& WINED3DSI_COISSUE
) {
1704 shader_addline(arg
->buffer
, "%s /* COISSUE! */);\n", src1_param
.param_str
);
1706 shader_addline(arg
->buffer
, "%s > 0.5 ? %s : %s);\n",
1707 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1711 /* Cycle through all source0 channels */
1712 for (i
=0; i
<4; i
++) {
1714 /* Find the destination channels which use the current source0 channel */
1715 for (j
=0; j
<4; j
++) {
1716 if ( ((arg
->src
[0] >> (WINED3DSP_SWIZZLE_SHIFT
+ 2*j
)) & 0x3) == i
) {
1717 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
1718 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
1721 write_mask
= shader_glsl_append_dst_ext(arg
->buffer
, arg
, arg
->dst
& (~WINED3DSP_SWIZZLE_MASK
| write_mask
));
1722 if (!write_mask
) continue;
1724 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], cmp_channel
, &src0_param
);
1725 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1726 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1728 shader_addline(arg
->buffer
, "%s > 0.5 ? %s : %s);\n",
1729 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1733 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1734 void shader_glsl_mad(SHADER_OPCODE_ARG
* arg
) {
1735 glsl_src_param_t src0_param
;
1736 glsl_src_param_t src1_param
;
1737 glsl_src_param_t src2_param
;
1740 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1741 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1742 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1743 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1744 shader_addline(arg
->buffer
, "(%s * %s) + %s);\n",
1745 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1748 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1749 Vertex shaders to GLSL codes */
1750 void shader_glsl_mnxn(SHADER_OPCODE_ARG
* arg
) {
1752 int nComponents
= 0;
1753 SHADER_OPCODE_ARG tmpArg
;
1755 memset(&tmpArg
, 0, sizeof(SHADER_OPCODE_ARG
));
1757 /* Set constants for the temporary argument */
1758 tmpArg
.shader
= arg
->shader
;
1759 tmpArg
.buffer
= arg
->buffer
;
1760 tmpArg
.src
[0] = arg
->src
[0];
1761 tmpArg
.src_addr
[0] = arg
->src_addr
[0];
1762 tmpArg
.src_addr
[1] = arg
->src_addr
[1];
1763 tmpArg
.reg_maps
= arg
->reg_maps
;
1765 switch(arg
->opcode
->opcode
) {
1766 case WINED3DSIO_M4x4
:
1768 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP4
);
1770 case WINED3DSIO_M4x3
:
1772 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP4
);
1774 case WINED3DSIO_M3x4
:
1776 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1778 case WINED3DSIO_M3x3
:
1780 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1782 case WINED3DSIO_M3x2
:
1784 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1790 for (i
= 0; i
< nComponents
; i
++) {
1791 tmpArg
.dst
= ((arg
->dst
) & ~WINED3DSP_WRITEMASK_ALL
)|(WINED3DSP_WRITEMASK_0
<<i
);
1792 tmpArg
.src
[1] = arg
->src
[1]+i
;
1793 shader_glsl_dot(&tmpArg
);
1798 The LRP instruction performs a component-wise linear interpolation
1799 between the second and third operands using the first operand as the
1800 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
1801 This is equivalent to mix(src2, src1, src0);
1803 void shader_glsl_lrp(SHADER_OPCODE_ARG
* arg
) {
1804 glsl_src_param_t src0_param
;
1805 glsl_src_param_t src1_param
;
1806 glsl_src_param_t src2_param
;
1809 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1811 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1812 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1813 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1815 shader_addline(arg
->buffer
, "mix(%s, %s, %s));\n",
1816 src2_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
);
1819 /** Process the WINED3DSIO_LIT instruction in GLSL:
1820 * dst.x = dst.w = 1.0
1821 * dst.y = (src0.x > 0) ? src0.x
1822 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1823 * where src.w is clamped at +- 128
1825 void shader_glsl_lit(SHADER_OPCODE_ARG
* arg
) {
1826 glsl_src_param_t src0_param
;
1827 glsl_src_param_t src1_param
;
1828 glsl_src_param_t src3_param
;
1831 shader_glsl_append_dst(arg
->buffer
, arg
);
1832 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1834 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1835 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_1
, &src1_param
);
1836 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &src3_param
);
1838 /* The sdk specifies the instruction like this
1840 * if(src.x > 0.0) dst.y = src.x
1842 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
1846 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
1847 * dst.x = 1.0 ... No further explanation needed
1848 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
1849 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
1850 * dst.w = 1.0. ... Nothing fancy.
1852 * So we still have one conditional in there. So do this:
1853 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
1855 * 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),
1856 * 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.
1857 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
1859 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",
1860 src0_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
, src3_param
.param_str
, dst_mask
);
1863 /** Process the WINED3DSIO_DST instruction in GLSL:
1865 * dst.y = src0.x * src0.y
1869 void shader_glsl_dst(SHADER_OPCODE_ARG
* arg
) {
1870 glsl_src_param_t src0y_param
;
1871 glsl_src_param_t src0z_param
;
1872 glsl_src_param_t src1y_param
;
1873 glsl_src_param_t src1w_param
;
1876 shader_glsl_append_dst(arg
->buffer
, arg
);
1877 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1879 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_1
, &src0y_param
);
1880 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_2
, &src0z_param
);
1881 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_1
, &src1y_param
);
1882 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_3
, &src1w_param
);
1884 shader_addline(arg
->buffer
, "vec4(1.0, %s * %s, %s, %s))%s;\n",
1885 src0y_param
.param_str
, src1y_param
.param_str
, src0z_param
.param_str
, src1w_param
.param_str
, dst_mask
);
1888 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1889 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1890 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1892 * dst.x = cos(src0.?)
1893 * dst.y = sin(src0.?)
1897 void shader_glsl_sincos(SHADER_OPCODE_ARG
* arg
) {
1898 glsl_src_param_t src0_param
;
1901 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1902 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1904 switch (write_mask
) {
1905 case WINED3DSP_WRITEMASK_0
:
1906 shader_addline(arg
->buffer
, "cos(%s));\n", src0_param
.param_str
);
1909 case WINED3DSP_WRITEMASK_1
:
1910 shader_addline(arg
->buffer
, "sin(%s));\n", src0_param
.param_str
);
1913 case (WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
):
1914 shader_addline(arg
->buffer
, "vec2(cos(%s), sin(%s)));\n", src0_param
.param_str
, src0_param
.param_str
);
1918 ERR("Write mask should be .x, .y or .xy\n");
1923 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1924 * Start a for() loop where src1.y is the initial value of aL,
1925 * increment aL by src1.z for a total of src1.x iterations.
1926 * Need to use a temporary variable for this operation.
1928 /* FIXME: I don't think nested loops will work correctly this way. */
1929 void shader_glsl_loop(SHADER_OPCODE_ARG
* arg
) {
1930 glsl_src_param_t src1_param
;
1931 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
1932 DWORD regtype
= shader_get_regtype(arg
->src
[1]);
1933 DWORD reg
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
1934 const DWORD
*control_values
= NULL
;
1935 local_constant
*constant
;
1937 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_ALL
, &src1_param
);
1939 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
1940 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
1941 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
1944 if(regtype
== WINED3DSPR_CONSTINT
) {
1945 LIST_FOR_EACH_ENTRY(constant
, &shader
->baseShader
.constantsI
, local_constant
, entry
) {
1946 if(constant
->idx
== reg
) {
1947 control_values
= constant
->value
;
1953 if(control_values
) {
1954 if(control_values
[2] > 0) {
1955 shader_addline(arg
->buffer
, "for (aL%u = %d; aL%u < (%d * %d + %d); aL%u += %d) {\n",
1956 shader
->baseShader
.cur_loop_depth
, control_values
[1],
1957 shader
->baseShader
.cur_loop_depth
, control_values
[0], control_values
[2], control_values
[1],
1958 shader
->baseShader
.cur_loop_depth
, control_values
[2]);
1959 } else if(control_values
[2] == 0) {
1960 shader_addline(arg
->buffer
, "for (aL%u = %d, tmpInt%u = 0; tmpInt%u < %d; tmpInt%u++) {\n",
1961 shader
->baseShader
.cur_loop_depth
, control_values
[1], shader
->baseShader
.cur_loop_depth
,
1962 shader
->baseShader
.cur_loop_depth
, control_values
[0],
1963 shader
->baseShader
.cur_loop_depth
);
1965 shader_addline(arg
->buffer
, "for (aL%u = %d; aL%u > (%d * %d + %d); aL%u += %d) {\n",
1966 shader
->baseShader
.cur_loop_depth
, control_values
[1],
1967 shader
->baseShader
.cur_loop_depth
, control_values
[0], control_values
[2], control_values
[1],
1968 shader
->baseShader
.cur_loop_depth
, control_values
[2]);
1971 shader_addline(arg
->buffer
, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
1972 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_regno
,
1973 src1_param
.reg_name
, shader
->baseShader
.cur_loop_depth
, src1_param
.reg_name
,
1974 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_regno
, src1_param
.reg_name
);
1977 shader
->baseShader
.cur_loop_depth
++;
1978 shader
->baseShader
.cur_loop_regno
++;
1981 void shader_glsl_end(SHADER_OPCODE_ARG
* arg
) {
1982 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
1984 shader_addline(arg
->buffer
, "}\n");
1986 if(arg
->opcode
->opcode
== WINED3DSIO_ENDLOOP
) {
1987 shader
->baseShader
.cur_loop_depth
--;
1988 shader
->baseShader
.cur_loop_regno
--;
1990 if(arg
->opcode
->opcode
== WINED3DSIO_ENDREP
) {
1991 shader
->baseShader
.cur_loop_depth
--;
1995 void shader_glsl_rep(SHADER_OPCODE_ARG
* arg
) {
1996 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
1997 glsl_src_param_t src0_param
;
1999 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2000 shader_addline(arg
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2001 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_depth
,
2002 src0_param
.param_str
, shader
->baseShader
.cur_loop_depth
);
2003 shader
->baseShader
.cur_loop_depth
++;
2006 void shader_glsl_if(SHADER_OPCODE_ARG
* arg
) {
2007 glsl_src_param_t src0_param
;
2009 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2010 shader_addline(arg
->buffer
, "if (%s) {\n", src0_param
.param_str
);
2013 void shader_glsl_ifc(SHADER_OPCODE_ARG
* arg
) {
2014 glsl_src_param_t src0_param
;
2015 glsl_src_param_t src1_param
;
2017 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2018 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2020 shader_addline(arg
->buffer
, "if (%s %s %s) {\n",
2021 src0_param
.param_str
, shader_get_comp_op(arg
->opcode_token
), src1_param
.param_str
);
2024 void shader_glsl_else(SHADER_OPCODE_ARG
* arg
) {
2025 shader_addline(arg
->buffer
, "} else {\n");
2028 void shader_glsl_break(SHADER_OPCODE_ARG
* arg
) {
2029 shader_addline(arg
->buffer
, "break;\n");
2032 /* FIXME: According to MSDN the compare is done per component. */
2033 void shader_glsl_breakc(SHADER_OPCODE_ARG
* arg
) {
2034 glsl_src_param_t src0_param
;
2035 glsl_src_param_t src1_param
;
2037 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2038 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2040 shader_addline(arg
->buffer
, "if (%s %s %s) break;\n",
2041 src0_param
.param_str
, shader_get_comp_op(arg
->opcode_token
), src1_param
.param_str
);
2044 void shader_glsl_label(SHADER_OPCODE_ARG
* arg
) {
2046 DWORD snum
= (arg
->src
[0]) & WINED3DSP_REGNUM_MASK
;
2047 shader_addline(arg
->buffer
, "}\n");
2048 shader_addline(arg
->buffer
, "void subroutine%u () {\n", snum
);
2051 void shader_glsl_call(SHADER_OPCODE_ARG
* arg
) {
2052 DWORD snum
= (arg
->src
[0]) & WINED3DSP_REGNUM_MASK
;
2053 shader_addline(arg
->buffer
, "subroutine%u();\n", snum
);
2056 void shader_glsl_callnz(SHADER_OPCODE_ARG
* arg
) {
2057 glsl_src_param_t src1_param
;
2059 DWORD snum
= (arg
->src
[0]) & WINED3DSP_REGNUM_MASK
;
2060 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2061 shader_addline(arg
->buffer
, "if (%s) subroutine%u();\n", src1_param
.param_str
, snum
);
2064 /*********************************************
2065 * Pixel Shader Specific Code begins here
2066 ********************************************/
2067 void pshader_glsl_tex(SHADER_OPCODE_ARG
* arg
) {
2068 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2069 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
2070 DWORD hex_version
= This
->baseShader
.hex_version
;
2071 char dst_swizzle
[6];
2072 glsl_sample_function_t sample_function
;
2075 BOOL projected
, texrect
= FALSE
;
2078 /* All versions have a destination register */
2079 shader_glsl_append_dst(arg
->buffer
, arg
);
2081 /* 1.0-1.4: Use destination register as sampler source.
2082 * 2.0+: Use provided sampler source. */
2083 if (hex_version
< WINED3DPS_VERSION(1,4)) {
2086 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2087 flags
= deviceImpl
->stateBlock
->textureState
[sampler_idx
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
2089 if (flags
& WINED3DTTFF_PROJECTED
) {
2091 switch (flags
& ~WINED3DTTFF_PROJECTED
) {
2092 case WINED3DTTFF_COUNT1
: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2093 case WINED3DTTFF_COUNT2
: mask
= WINED3DSP_WRITEMASK_1
; break;
2094 case WINED3DTTFF_COUNT3
: mask
= WINED3DSP_WRITEMASK_2
; break;
2095 case WINED3DTTFF_COUNT4
:
2096 case WINED3DTTFF_DISABLE
: mask
= WINED3DSP_WRITEMASK_3
; break;
2101 } else if (hex_version
< WINED3DPS_VERSION(2,0)) {
2102 DWORD src_mod
= arg
->src
[0] & WINED3DSP_SRCMOD_MASK
;
2103 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2105 if (src_mod
== WINED3DSPSM_DZ
) {
2107 mask
= WINED3DSP_WRITEMASK_2
;
2108 } else if (src_mod
== WINED3DSPSM_DW
) {
2110 mask
= WINED3DSP_WRITEMASK_3
;
2115 sampler_idx
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
2116 if(arg
->opcode_token
& WINED3DSI_TEXLD_PROJECT
) {
2117 /* ps 2.0 texldp instruction always divides by the fourth component. */
2119 mask
= WINED3DSP_WRITEMASK_3
;
2125 if(deviceImpl
->stateBlock
->textures
[sampler_idx
] &&
2126 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl
->stateBlock
->textures
[sampler_idx
]) == GL_TEXTURE_RECTANGLE_ARB
) {
2130 sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
2131 shader_glsl_get_sample_function(sampler_type
, projected
, texrect
, &sample_function
);
2132 mask
|= sample_function
.coord_mask
;
2134 if (hex_version
< WINED3DPS_VERSION(2,0)) {
2135 shader_glsl_get_write_mask(arg
->dst
, dst_swizzle
);
2137 shader_glsl_get_swizzle(arg
->src
[1], FALSE
, arg
->dst
, dst_swizzle
);
2140 /* 1.0-1.3: Use destination register as coordinate source.
2141 1.4+: Use provided coordinate source register. */
2142 if (hex_version
< WINED3DPS_VERSION(1,4)) {
2144 shader_glsl_get_write_mask(mask
, coord_mask
);
2145 shader_addline(arg
->buffer
, "%s(Psampler%u, T%u%s)%s);\n",
2146 sample_function
.name
, sampler_idx
, sampler_idx
, coord_mask
, dst_swizzle
);
2148 glsl_src_param_t coord_param
;
2149 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], mask
, &coord_param
);
2150 if(arg
->opcode_token
& WINED3DSI_TEXLD_BIAS
) {
2151 glsl_src_param_t bias
;
2152 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &bias
);
2154 shader_addline(arg
->buffer
, "%s(Psampler%u, %s, %s)%s);\n",
2155 sample_function
.name
, sampler_idx
, coord_param
.param_str
,
2156 bias
.param_str
, dst_swizzle
);
2158 shader_addline(arg
->buffer
, "%s(Psampler%u, %s)%s);\n",
2159 sample_function
.name
, sampler_idx
, coord_param
.param_str
, dst_swizzle
);
2164 void shader_glsl_texldl(SHADER_OPCODE_ARG
* arg
) {
2165 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*)arg
->shader
;
2166 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
2167 glsl_sample_function_t sample_function
;
2168 glsl_src_param_t coord_param
, lod_param
;
2169 char dst_swizzle
[6];
2172 BOOL texrect
= FALSE
;
2174 shader_glsl_append_dst(arg
->buffer
, arg
);
2175 shader_glsl_get_swizzle(arg
->src
[1], FALSE
, arg
->dst
, dst_swizzle
);
2177 sampler_idx
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
2178 sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
2179 if(deviceImpl
->stateBlock
->textures
[sampler_idx
] &&
2180 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl
->stateBlock
->textures
[sampler_idx
]) == GL_TEXTURE_RECTANGLE_ARB
) {
2183 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
);
2185 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
2187 if (shader_is_pshader_version(This
->baseShader
.hex_version
)) {
2188 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
2189 * However, they seem to work just fine in fragment shaders as well. */
2190 WARN("Using %sLod in fragment shader.\n", sample_function
.name
);
2191 shader_addline(arg
->buffer
, "%sLod(Psampler%u, %s, %s)%s);\n",
2192 sample_function
.name
, sampler_idx
, coord_param
.param_str
, lod_param
.param_str
, dst_swizzle
);
2194 shader_addline(arg
->buffer
, "%sLod(Vsampler%u, %s, %s)%s);\n",
2195 sample_function
.name
, sampler_idx
, coord_param
.param_str
, lod_param
.param_str
, dst_swizzle
);
2199 void pshader_glsl_texcoord(SHADER_OPCODE_ARG
* arg
) {
2201 /* FIXME: Make this work for more than just 2D textures */
2203 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2204 SHADER_BUFFER
* buffer
= arg
->buffer
;
2205 DWORD hex_version
= This
->baseShader
.hex_version
;
2209 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
2210 shader_glsl_get_write_mask(write_mask
, dst_mask
);
2212 if (hex_version
!= WINED3DPS_VERSION(1,4)) {
2213 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2214 shader_addline(buffer
, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n", reg
, dst_mask
);
2216 DWORD reg
= arg
->src
[0] & WINED3DSP_REGNUM_MASK
;
2217 DWORD src_mod
= arg
->src
[0] & WINED3DSP_SRCMOD_MASK
;
2218 char dst_swizzle
[6];
2220 shader_glsl_get_swizzle(arg
->src
[0], FALSE
, write_mask
, dst_swizzle
);
2222 if (src_mod
== WINED3DSPSM_DZ
) {
2223 glsl_src_param_t div_param
;
2224 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2225 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_2
, &div_param
);
2227 if (mask_size
> 1) {
2228 shader_addline(buffer
, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
2230 shader_addline(buffer
, "gl_TexCoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
2232 } else if (src_mod
== WINED3DSPSM_DW
) {
2233 glsl_src_param_t div_param
;
2234 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2235 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &div_param
);
2237 if (mask_size
> 1) {
2238 shader_addline(buffer
, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
2240 shader_addline(buffer
, "gl_TexCoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
2243 shader_addline(buffer
, "gl_TexCoord[%u]%s);\n", reg
, dst_swizzle
);
2248 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
2249 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2250 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2251 void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG
* arg
) {
2252 glsl_src_param_t src0_param
;
2254 glsl_sample_function_t sample_function
;
2255 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2256 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2257 DWORD sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
2259 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2261 shader_glsl_append_dst(arg
->buffer
, arg
);
2262 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2264 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
2265 * scalar, and projected sampling would require 4.
2267 * It is a dependent read - not valid with conditional NP2 textures
2269 shader_glsl_get_sample_function(sampler_type
, FALSE
, FALSE
, &sample_function
);
2271 switch(count_bits(sample_function
.coord_mask
)) {
2273 shader_addline(arg
->buffer
, "%s(Psampler%u, dot(gl_TexCoord[%u].xyz, %s))%s);\n",
2274 sample_function
.name
, sampler_idx
, sampler_idx
, src0_param
.param_str
, dst_mask
);
2278 shader_addline(arg
->buffer
, "%s(Psampler%u, vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0))%s);\n",
2279 sample_function
.name
, sampler_idx
, sampler_idx
, src0_param
.param_str
, dst_mask
);
2283 shader_addline(arg
->buffer
, "%s(Psampler%u, vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0))%s);\n",
2284 sample_function
.name
, sampler_idx
, sampler_idx
, src0_param
.param_str
, dst_mask
);
2287 FIXME("Unexpected mask bitcount %d\n", count_bits(sample_function
.coord_mask
));
2291 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2292 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2293 void pshader_glsl_texdp3(SHADER_OPCODE_ARG
* arg
) {
2294 glsl_src_param_t src0_param
;
2295 DWORD dstreg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2296 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2298 unsigned int mask_size
;
2300 dst_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
2301 mask_size
= shader_glsl_get_write_mask_size(dst_mask
);
2302 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2304 if (mask_size
> 1) {
2305 shader_addline(arg
->buffer
, "vec%d(dot(T%u.xyz, %s)));\n", mask_size
, dstreg
, src0_param
.param_str
);
2307 shader_addline(arg
->buffer
, "dot(T%u.xyz, %s));\n", dstreg
, src0_param
.param_str
);
2311 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2312 * Calculate the depth as dst.x / dst.y */
2313 void pshader_glsl_texdepth(SHADER_OPCODE_ARG
* arg
) {
2314 glsl_dst_param_t dst_param
;
2316 shader_glsl_add_dst_param(arg
, arg
->dst
, 0, &dst_param
);
2318 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2319 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2320 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2321 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2324 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
);
2327 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2328 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2329 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2330 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2332 void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG
* arg
) {
2333 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2334 DWORD dstreg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2335 glsl_src_param_t src0_param
;
2337 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2339 shader_addline(arg
->buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg
, src0_param
.param_str
);
2340 shader_addline(arg
->buffer
, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2343 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2344 * Calculate the 1st of a 2-row matrix multiplication. */
2345 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG
* arg
) {
2346 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2347 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2348 SHADER_BUFFER
* buffer
= arg
->buffer
;
2349 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.x = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2355 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2356 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2357 void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG
* arg
) {
2359 IWineD3DPixelShaderImpl
* shader
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2360 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2361 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2362 SHADER_BUFFER
* buffer
= arg
->buffer
;
2363 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
2364 glsl_src_param_t src0_param
;
2366 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2367 shader_addline(buffer
, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state
->current_row
, reg
, src0_param
.param_str
);
2368 current_state
->texcoord_w
[current_state
->current_row
++] = reg
;
2371 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG
* arg
) {
2372 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2373 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2374 SHADER_BUFFER
* buffer
= arg
->buffer
;
2375 glsl_src_param_t src0_param
;
2378 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2379 shader_addline(buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2381 shader_glsl_append_dst(buffer
, arg
);
2382 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2384 /* Sample the texture using the calculated coordinates */
2385 shader_addline(buffer
, "texture2D(Psampler%u, tmp0.xy)%s);\n", reg
, dst_mask
);
2388 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2389 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2390 void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG
* arg
) {
2391 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2392 glsl_src_param_t src0_param
;
2394 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2395 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2396 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
2397 DWORD sampler_type
= arg
->reg_maps
->samplers
[reg
] & WINED3DSP_TEXTURETYPE_MASK
;
2398 glsl_sample_function_t sample_function
;
2400 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2401 shader_addline(arg
->buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2403 shader_glsl_append_dst(arg
->buffer
, arg
);
2404 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2405 /* Dependent read, not valid with conditional NP2 */
2406 shader_glsl_get_sample_function(sampler_type
, FALSE
, FALSE
, &sample_function
);
2408 /* Sample the texture using the calculated coordinates */
2409 shader_addline(arg
->buffer
, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function
.name
, reg
, dst_mask
);
2411 current_state
->current_row
= 0;
2414 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2415 * Perform the 3rd row of a 3x3 matrix multiply */
2416 void pshader_glsl_texm3x3(SHADER_OPCODE_ARG
* arg
) {
2417 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2418 glsl_src_param_t src0_param
;
2420 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2421 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2422 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
2424 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2426 shader_glsl_append_dst(arg
->buffer
, arg
);
2427 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2428 shader_addline(arg
->buffer
, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg
, src0_param
.param_str
, dst_mask
);
2430 current_state
->current_row
= 0;
2433 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
2434 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2435 void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG
* arg
) {
2437 IWineD3DPixelShaderImpl
* shader
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2438 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2439 glsl_src_param_t src0_param
;
2440 glsl_src_param_t src1_param
;
2442 SHADER_BUFFER
* buffer
= arg
->buffer
;
2443 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
2444 DWORD stype
= arg
->reg_maps
->samplers
[reg
] & WINED3DSP_TEXTURETYPE_MASK
;
2445 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2446 glsl_sample_function_t sample_function
;
2448 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2449 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], src_mask
, &src1_param
);
2451 /* Perform the last matrix multiply operation */
2452 shader_addline(buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2453 /* Reflection calculation */
2454 shader_addline(buffer
, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param
.param_str
);
2456 shader_glsl_append_dst(buffer
, arg
);
2457 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2458 /* Dependent read, not valid with conditional NP2 */
2459 shader_glsl_get_sample_function(stype
, FALSE
, FALSE
, &sample_function
);
2461 /* Sample the texture */
2462 shader_addline(buffer
, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function
.name
, reg
, dst_mask
);
2464 current_state
->current_row
= 0;
2467 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
2468 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2469 void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG
* arg
) {
2471 IWineD3DPixelShaderImpl
* shader
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2472 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2473 SHADER_BUFFER
* buffer
= arg
->buffer
;
2474 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
2475 glsl_src_param_t src0_param
;
2477 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2478 DWORD sampler_type
= arg
->reg_maps
->samplers
[reg
] & WINED3DSP_TEXTURETYPE_MASK
;
2479 glsl_sample_function_t sample_function
;
2481 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2483 /* Perform the last matrix multiply operation */
2484 shader_addline(buffer
, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg
, src0_param
.param_str
);
2486 /* Construct the eye-ray vector from w coordinates */
2487 shader_addline(buffer
, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
2488 current_state
->texcoord_w
[0], current_state
->texcoord_w
[1], reg
);
2489 shader_addline(buffer
, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
2491 shader_glsl_append_dst(buffer
, arg
);
2492 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2493 /* Dependent read, not valid with conditional NP2 */
2494 shader_glsl_get_sample_function(sampler_type
, FALSE
, FALSE
, &sample_function
);
2496 /* Sample the texture using the calculated coordinates */
2497 shader_addline(buffer
, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function
.name
, reg
, dst_mask
);
2499 current_state
->current_row
= 0;
2502 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
2503 * Apply a fake bump map transform.
2504 * texbem is pshader <= 1.3 only, this saves a few version checks
2506 void pshader_glsl_texbem(SHADER_OPCODE_ARG
* arg
) {
2507 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2508 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
2509 char dst_swizzle
[6];
2510 glsl_sample_function_t sample_function
;
2511 glsl_src_param_t coord_param
;
2518 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2519 flags
= deviceImpl
->stateBlock
->textureState
[sampler_idx
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
2521 sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
2522 /* Dependent read, not valid with conditional NP2 */
2523 shader_glsl_get_sample_function(sampler_type
, FALSE
, FALSE
, &sample_function
);
2524 mask
= sample_function
.coord_mask
;
2526 shader_glsl_get_write_mask(arg
->dst
, dst_swizzle
);
2528 shader_glsl_get_write_mask(mask
, coord_mask
);
2530 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2531 * so we can't let the GL handle this.
2533 if (flags
& WINED3DTTFF_PROJECTED
) {
2535 char coord_div_mask
[3];
2536 switch (flags
& ~WINED3DTTFF_PROJECTED
) {
2537 case WINED3DTTFF_COUNT1
: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2538 case WINED3DTTFF_COUNT2
: div_mask
= WINED3DSP_WRITEMASK_1
; break;
2539 case WINED3DTTFF_COUNT3
: div_mask
= WINED3DSP_WRITEMASK_2
; break;
2540 case WINED3DTTFF_COUNT4
:
2541 case WINED3DTTFF_DISABLE
: div_mask
= WINED3DSP_WRITEMASK_3
; break;
2543 shader_glsl_get_write_mask(div_mask
, coord_div_mask
);
2544 shader_addline(arg
->buffer
, "T%u%s /= T%u%s;\n", sampler_idx
, coord_mask
, sampler_idx
, coord_div_mask
);
2547 shader_glsl_append_dst(arg
->buffer
, arg
);
2548 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
|WINED3DSP_WRITEMASK_1
, &coord_param
);
2549 if(arg
->opcode
->opcode
== WINED3DSIO_TEXBEML
) {
2550 glsl_src_param_t luminance_param
;
2551 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_2
, &luminance_param
);
2552 shader_addline(arg
->buffer
, "(%s(Psampler%u, T%u%s + vec4(bumpenvmat * %s, 0.0, 0.0)%s )*(%s * luminancescale + luminanceoffset))%s);\n",
2553 sample_function
.name
, sampler_idx
, sampler_idx
, coord_mask
, coord_param
.param_str
, coord_mask
,
2554 luminance_param
.param_str
, dst_swizzle
);
2556 shader_addline(arg
->buffer
, "%s(Psampler%u, T%u%s + vec4(bumpenvmat * %s, 0.0, 0.0)%s )%s);\n",
2557 sample_function
.name
, sampler_idx
, sampler_idx
, coord_mask
, coord_param
.param_str
, coord_mask
, dst_swizzle
);
2561 void pshader_glsl_bem(SHADER_OPCODE_ARG
* arg
) {
2562 glsl_src_param_t src0_param
, src1_param
;
2564 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
|WINED3DSP_WRITEMASK_1
, &src0_param
);
2565 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
|WINED3DSP_WRITEMASK_1
, &src1_param
);
2567 shader_glsl_append_dst(arg
->buffer
, arg
);
2568 shader_addline(arg
->buffer
, "%s + bumpenvmat * %s);\n",
2569 src0_param
.param_str
, src1_param
.param_str
);
2572 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
2573 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
2574 void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG
* arg
) {
2576 glsl_src_param_t src0_param
;
2577 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2580 shader_glsl_append_dst(arg
->buffer
, arg
);
2581 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2582 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
2584 shader_addline(arg
->buffer
, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx
, src0_param
.reg_name
, dst_mask
);
2587 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
2588 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
2589 void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG
* arg
) {
2590 glsl_src_param_t src0_param
;
2591 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2594 shader_glsl_append_dst(arg
->buffer
, arg
);
2595 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2596 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
2598 shader_addline(arg
->buffer
, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx
, src0_param
.reg_name
, dst_mask
);
2601 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
2602 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
2603 void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG
* arg
) {
2604 glsl_src_param_t src0_param
;
2606 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2607 DWORD sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
2608 glsl_sample_function_t sample_function
;
2610 shader_glsl_append_dst(arg
->buffer
, arg
);
2611 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2612 /* Dependent read, not valid with conditional NP2 */
2613 shader_glsl_get_sample_function(sampler_type
, FALSE
, FALSE
, &sample_function
);
2614 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], sample_function
.coord_mask
, &src0_param
);
2616 shader_addline(arg
->buffer
, "%s(Psampler%u, %s)%s);\n", sample_function
.name
, sampler_idx
, src0_param
.param_str
, dst_mask
);
2619 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
2620 * If any of the first 3 components are < 0, discard this pixel */
2621 void pshader_glsl_texkill(SHADER_OPCODE_ARG
* arg
) {
2622 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2623 DWORD hex_version
= This
->baseShader
.hex_version
;
2624 glsl_dst_param_t dst_param
;
2626 /* The argument is a destination parameter, and no writemasks are allowed */
2627 shader_glsl_add_dst_param(arg
, arg
->dst
, 0, &dst_param
);
2628 if((hex_version
>= WINED3DPS_VERSION(2,0))) {
2629 /* 2.0 shaders compare all 4 components in texkill */
2630 shader_addline(arg
->buffer
, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param
.reg_name
);
2632 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
2633 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
2634 * 4 components are defined, only the first 3 are used
2636 shader_addline(arg
->buffer
, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param
.reg_name
);
2640 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
2641 * dst = dot2(src0, src1) + src2 */
2642 void pshader_glsl_dp2add(SHADER_OPCODE_ARG
* arg
) {
2643 glsl_src_param_t src0_param
;
2644 glsl_src_param_t src1_param
;
2645 glsl_src_param_t src2_param
;
2647 unsigned int mask_size
;
2649 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
2650 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2652 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
2653 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
2654 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], WINED3DSP_WRITEMASK_0
, &src2_param
);
2656 shader_addline(arg
->buffer
, "dot(%s, %s) + %s);\n", src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2659 void pshader_glsl_input_pack(
2660 SHADER_BUFFER
* buffer
,
2661 semantic
* semantics_in
,
2662 IWineD3DPixelShader
*iface
) {
2665 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*) iface
;
2667 for (i
= 0; i
< MAX_REG_INPUT
; i
++) {
2669 DWORD usage_token
= semantics_in
[i
].usage
;
2670 DWORD register_token
= semantics_in
[i
].reg
;
2671 DWORD usage
, usage_idx
;
2675 if (!usage_token
) continue;
2676 usage
= (usage_token
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
2677 usage_idx
= (usage_token
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
2678 shader_glsl_get_write_mask(register_token
, reg_mask
);
2682 case WINED3DDECLUSAGE_TEXCOORD
:
2683 if(usage_idx
< 8 && This
->vertexprocessing
== pretransformed
) {
2684 shader_addline(buffer
, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
2685 This
->input_reg_map
[i
], reg_mask
, usage_idx
, reg_mask
);
2687 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2688 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
2692 case WINED3DDECLUSAGE_COLOR
:
2694 shader_addline(buffer
, "IN[%u]%s = vec4(gl_Color)%s;\n",
2695 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
2696 else if (usage_idx
== 1)
2697 shader_addline(buffer
, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
2698 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
2700 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2701 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
2705 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2706 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
2711 /*********************************************
2712 * Vertex Shader Specific Code begins here
2713 ********************************************/
2715 static void add_glsl_program_entry(IWineD3DDeviceImpl
*device
, struct glsl_shader_prog_link
*entry
) {
2716 glsl_program_key_t
*key
;
2718 key
= HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t
));
2719 key
->vshader
= entry
->vshader
;
2720 key
->pshader
= entry
->pshader
;
2722 hash_table_put(device
->glsl_program_lookup
, key
, entry
);
2725 static struct glsl_shader_prog_link
*get_glsl_program_entry(IWineD3DDeviceImpl
*device
,
2726 GLhandleARB vshader
, GLhandleARB pshader
) {
2727 glsl_program_key_t key
;
2729 key
.vshader
= vshader
;
2730 key
.pshader
= pshader
;
2732 return (struct glsl_shader_prog_link
*)hash_table_get(device
->glsl_program_lookup
, &key
);
2735 void delete_glsl_program_entry(IWineD3DDevice
*iface
, struct glsl_shader_prog_link
*entry
) {
2736 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
2737 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
2738 glsl_program_key_t
*key
;
2740 key
= HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t
));
2741 key
->vshader
= entry
->vshader
;
2742 key
->pshader
= entry
->pshader
;
2743 hash_table_remove(This
->glsl_program_lookup
, key
);
2745 GL_EXTCALL(glDeleteObjectARB(entry
->programId
));
2746 if (entry
->vshader
) list_remove(&entry
->vshader_entry
);
2747 if (entry
->pshader
) list_remove(&entry
->pshader_entry
);
2748 HeapFree(GetProcessHeap(), 0, entry
->vuniformF_locations
);
2749 HeapFree(GetProcessHeap(), 0, entry
->puniformF_locations
);
2750 HeapFree(GetProcessHeap(), 0, entry
);
2753 static void handle_ps3_input(SHADER_BUFFER
*buffer
, semantic
*semantics_in
, semantic
*semantics_out
, WineD3D_GL_Info
*gl_info
, DWORD
*map
) {
2755 DWORD usage_token
, usage_token_out
;
2756 DWORD register_token
, register_token_out
;
2757 DWORD usage
, usage_idx
, usage_out
, usage_idx_out
;
2759 char reg_mask
[6], reg_mask_out
[6];
2761 set
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*set
) * (GL_LIMITS(glsl_varyings
) / 4));
2763 for(i
= 0; i
< MAX_REG_INPUT
; i
++) {
2764 usage_token
= semantics_in
[i
].usage
;
2765 if (!usage_token
) continue;
2766 if(map
[i
] >= (GL_LIMITS(glsl_varyings
) / 4)) {
2767 FIXME("More input varyings declared than supported, expect issues\n");
2769 } else if(map
[i
] == -1) {
2770 /* Declared, but not read register */
2773 register_token
= semantics_in
[i
].reg
;
2775 usage
= (usage_token
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
2776 usage_idx
= (usage_token
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
2777 set
[map
[i
]] = shader_glsl_get_write_mask(register_token
, reg_mask
);
2779 if(!semantics_out
) {
2781 case WINED3DDECLUSAGE_COLOR
:
2783 shader_addline(buffer
, "IN[%u]%s = gl_FrontColor%s;\n",
2784 map
[i
], reg_mask
, reg_mask
);
2785 else if (usage_idx
== 1)
2786 shader_addline(buffer
, "IN[%u]%s = gl_FrontSecondaryColor%s;\n",
2787 map
[i
], reg_mask
, reg_mask
);
2789 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2790 map
[i
], reg_mask
, reg_mask
);
2793 case WINED3DDECLUSAGE_TEXCOORD
:
2794 if (usage_idx
< 8) {
2795 shader_addline(buffer
, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
2796 map
[i
], reg_mask
, usage_idx
, reg_mask
);
2798 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2799 map
[i
], reg_mask
, reg_mask
);
2803 case WINED3DDECLUSAGE_FOG
:
2804 shader_addline(buffer
, "IN[%u]%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
2805 map
[i
], reg_mask
, reg_mask
);
2809 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2810 map
[i
], reg_mask
, reg_mask
);
2814 for(j
= 0; j
< MAX_REG_OUTPUT
; j
++) {
2815 usage_token_out
= semantics_out
[j
].usage
;
2816 if (!usage_token_out
) continue;
2817 register_token_out
= semantics_out
[j
].reg
;
2819 usage_out
= (usage_token_out
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
2820 usage_idx_out
= (usage_token_out
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
2821 shader_glsl_get_write_mask(register_token_out
, reg_mask_out
);
2823 if(usage
== usage_out
&&
2824 usage_idx
== usage_idx_out
) {
2825 shader_addline(buffer
, "IN[%u]%s = OUT[%u]%s;\n",
2826 map
[i
], reg_mask
, j
, reg_mask
);
2831 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2832 map
[i
], reg_mask
, reg_mask
);
2837 /* This is solely to make the compiler / linker happy and avoid warning about undefined
2838 * varyings. It shouldn't result in any real code executed on the GPU, since all read
2839 * input varyings are assigned above, if the optimizer works properly.
2841 for(i
= 0; i
< GL_LIMITS(glsl_varyings
) / 4; i
++) {
2842 if(set
[i
] != WINED3DSP_WRITEMASK_ALL
) {
2843 unsigned int size
= 0;
2844 memset(reg_mask
, 0, sizeof(reg_mask
));
2845 if(!(set
[i
] & WINED3DSP_WRITEMASK_0
)) {
2846 reg_mask
[size
] = 'x';
2849 if(!(set
[i
] & WINED3DSP_WRITEMASK_1
)) {
2850 reg_mask
[size
] = 'y';
2853 if(!(set
[i
] & WINED3DSP_WRITEMASK_2
)) {
2854 reg_mask
[size
] = 'z';
2857 if(!(set
[i
] & WINED3DSP_WRITEMASK_3
)) {
2858 reg_mask
[size
] = 'w';
2863 shader_addline(buffer
, "IN[%u].%s = 0.0;\n", i
, reg_mask
);
2866 shader_addline(buffer
, "IN[%u].%s = vec2(0.0, 0.0);\n", i
, reg_mask
);
2869 shader_addline(buffer
, "IN[%u].%s = vec3(0.0, 0.0, 0.0);\n", i
, reg_mask
);
2872 shader_addline(buffer
, "IN[%u].%s = vec4(0.0, 0.0, 0.0, 0.0);\n", i
, reg_mask
);
2878 HeapFree(GetProcessHeap(), 0, set
);
2881 static GLhandleARB
generate_param_reorder_function(IWineD3DVertexShader
*vertexshader
,
2882 IWineD3DPixelShader
*pixelshader
,
2883 WineD3D_GL_Info
*gl_info
) {
2884 GLhandleARB ret
= 0;
2885 IWineD3DVertexShaderImpl
*vs
= (IWineD3DVertexShaderImpl
*) vertexshader
;
2886 IWineD3DPixelShaderImpl
*ps
= (IWineD3DPixelShaderImpl
*) pixelshader
;
2887 DWORD vs_major
= vs
? WINED3DSHADER_VERSION_MAJOR(vs
->baseShader
.hex_version
) : 0;
2888 DWORD ps_major
= ps
? WINED3DSHADER_VERSION_MAJOR(ps
->baseShader
.hex_version
) : 0;
2890 SHADER_BUFFER buffer
;
2892 DWORD register_token
;
2893 DWORD usage
, usage_idx
, writemask
;
2895 semantic
*semantics_out
, *semantics_in
;
2897 buffer
.buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, SHADER_PGMSIZE
);
2900 buffer
.newline
= TRUE
;
2902 if(vs_major
< 3 && ps_major
< 3) {
2903 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
2904 * Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
2906 if((GLINFO_LOCATION
).set_texcoord_w
&& ps_major
== 0 && vs_major
> 0) {
2907 shader_addline(&buffer
, "void order_ps_input() {\n");
2908 for(i
= 0; i
< min(8, MAX_REG_TEXCRD
); i
++) {
2909 if(vs
->baseShader
.reg_maps
.texcoord_mask
[i
] != 0 &&
2910 vs
->baseShader
.reg_maps
.texcoord_mask
[i
] != WINED3DSP_WRITEMASK_ALL
) {
2911 shader_addline(&buffer
, "gl_TexCoord[%u].w = 1.0;\n", i
);
2914 shader_addline(&buffer
, "}\n");
2916 shader_addline(&buffer
, "void order_ps_input() { /* do nothing */ }\n");
2918 } else if(ps_major
< 3 && vs_major
>= 3) {
2919 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
2920 semantics_out
= vs
->semantics_out
;
2922 shader_addline(&buffer
, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT
);
2923 for(i
= 0; i
< MAX_REG_OUTPUT
; i
++) {
2924 usage_token
= semantics_out
[i
].usage
;
2925 if (!usage_token
) continue;
2926 register_token
= semantics_out
[i
].reg
;
2928 usage
= (usage_token
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
2929 usage_idx
= (usage_token
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
2930 writemask
= shader_glsl_get_write_mask(register_token
, reg_mask
);
2933 case WINED3DDECLUSAGE_COLOR
:
2935 shader_addline(&buffer
, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
2936 else if (usage_idx
== 1)
2937 shader_addline(&buffer
, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
2940 case WINED3DDECLUSAGE_POSITION
:
2941 shader_addline(&buffer
, "gl_Position%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
2944 case WINED3DDECLUSAGE_TEXCOORD
:
2945 if (usage_idx
< 8) {
2946 if(!(GLINFO_LOCATION
).set_texcoord_w
|| ps_major
> 0) writemask
|= WINED3DSP_WRITEMASK_3
;
2948 shader_addline(&buffer
, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
2949 usage_idx
, reg_mask
, i
, reg_mask
);
2950 if(!(writemask
& WINED3DSP_WRITEMASK_3
)) {
2951 shader_addline(&buffer
, "gl_TexCoord[%u].w = 1.0;\n", usage_idx
);
2956 case WINED3DDECLUSAGE_PSIZE
:
2957 shader_addline(&buffer
, "gl_PointSize = OUT[%u].x;\n", i
);
2960 case WINED3DDECLUSAGE_FOG
:
2961 shader_addline(&buffer
, "gl_FogFragCoord = OUT[%u].%c;\n", i
, reg_mask
[1]);
2968 shader_addline(&buffer
, "}\n");
2970 } else if(ps_major
>= 3 && vs_major
>= 3) {
2971 semantics_out
= vs
->semantics_out
;
2972 semantics_in
= ps
->semantics_in
;
2974 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
2975 shader_addline(&buffer
, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings
) / 4);
2976 shader_addline(&buffer
, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT
);
2978 /* First, sort out position and point size. Those are not passed to the pixel shader */
2979 for(i
= 0; i
< MAX_REG_OUTPUT
; i
++) {
2980 usage_token
= semantics_out
[i
].usage
;
2981 if (!usage_token
) continue;
2982 register_token
= semantics_out
[i
].reg
;
2984 usage
= (usage_token
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
2985 usage_idx
= (usage_token
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
2986 shader_glsl_get_write_mask(register_token
, reg_mask
);
2989 case WINED3DDECLUSAGE_POSITION
:
2990 shader_addline(&buffer
, "gl_Position%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
2993 case WINED3DDECLUSAGE_PSIZE
:
2994 shader_addline(&buffer
, "gl_PointSize = OUT[%u].x;\n", i
);
3002 /* Then, fix the pixel shader input */
3003 handle_ps3_input(&buffer
, semantics_in
, semantics_out
, gl_info
, ps
->input_reg_map
);
3005 shader_addline(&buffer
, "}\n");
3006 } else if(ps_major
>= 3 && vs_major
< 3) {
3007 semantics_in
= ps
->semantics_in
;
3009 shader_addline(&buffer
, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings
) / 4);
3010 shader_addline(&buffer
, "void order_ps_input() {\n");
3011 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
3012 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
3013 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
3015 handle_ps3_input(&buffer
, semantics_in
, NULL
, gl_info
, ps
->input_reg_map
);
3016 shader_addline(&buffer
, "}\n");
3018 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major
, ps_major
);
3021 ret
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
3022 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3023 GL_EXTCALL(glShaderSourceARB(ret
, 1, (const char**)&buffer
.buffer
, NULL
));
3024 checkGLcall("glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL)");
3025 GL_EXTCALL(glCompileShaderARB(ret
));
3026 checkGLcall("glCompileShaderARB(ret)");
3028 HeapFree(GetProcessHeap(), 0, buffer
.buffer
);
3032 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
3033 * It sets the programId on the current StateBlock (because it should be called
3034 * inside of the DrawPrimitive() part of the render loop).
3036 * If a program for the given combination does not exist, create one, and store
3037 * the program in the hash table. If it creates a program, it will link the
3038 * given objects, too.
3040 static void set_glsl_shader_program(IWineD3DDevice
*iface
, BOOL use_ps
, BOOL use_vs
) {
3041 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3042 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3043 IWineD3DPixelShader
*pshader
= This
->stateBlock
->pixelShader
;
3044 IWineD3DVertexShader
*vshader
= This
->stateBlock
->vertexShader
;
3045 struct glsl_shader_prog_link
*entry
= NULL
;
3046 GLhandleARB programId
= 0;
3047 GLhandleARB reorder_shader_id
= 0;
3051 GLhandleARB vshader_id
= use_vs
? ((IWineD3DBaseShaderImpl
*)vshader
)->baseShader
.prgId
: 0;
3052 GLhandleARB pshader_id
= use_ps
? ((IWineD3DBaseShaderImpl
*)pshader
)->baseShader
.prgId
: 0;
3053 entry
= get_glsl_program_entry(This
, vshader_id
, pshader_id
);
3055 This
->stateBlock
->glsl_program
= entry
;
3059 /* If we get to this point, then no matching program exists, so we create one */
3060 programId
= GL_EXTCALL(glCreateProgramObjectARB());
3061 TRACE("Created new GLSL shader program %u\n", programId
);
3063 /* Create the entry */
3064 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link
));
3065 entry
->programId
= programId
;
3066 entry
->vshader
= vshader_id
;
3067 entry
->pshader
= pshader_id
;
3068 /* Add the hash table entry */
3069 add_glsl_program_entry(This
, entry
);
3071 /* Set the current program */
3072 This
->stateBlock
->glsl_program
= entry
;
3074 /* Attach GLSL vshader */
3076 int max_attribs
= 16; /* TODO: Will this always be the case? It is at the moment... */
3079 reorder_shader_id
= generate_param_reorder_function(vshader
, pshader
, gl_info
);
3080 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id
, programId
);
3081 GL_EXTCALL(glAttachObjectARB(programId
, reorder_shader_id
));
3082 checkGLcall("glAttachObjectARB");
3083 /* Flag the reorder function for deletion, then it will be freed automatically when the program
3086 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id
));
3088 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id
, programId
);
3089 GL_EXTCALL(glAttachObjectARB(programId
, vshader_id
));
3090 checkGLcall("glAttachObjectARB");
3092 /* Bind vertex attributes to a corresponding index number to match
3093 * the same index numbers as ARB_vertex_programs (makes loading
3094 * vertex attributes simpler). With this method, we can use the
3095 * exact same code to load the attributes later for both ARB and
3098 * We have to do this here because we need to know the Program ID
3099 * in order to make the bindings work, and it has to be done prior
3100 * to linking the GLSL program. */
3101 for (i
= 0; i
< max_attribs
; ++i
) {
3102 if (((IWineD3DBaseShaderImpl
*)vshader
)->baseShader
.reg_maps
.attributes
[i
]) {
3103 snprintf(tmp_name
, sizeof(tmp_name
), "attrib%i", i
);
3104 GL_EXTCALL(glBindAttribLocationARB(programId
, i
, tmp_name
));
3107 checkGLcall("glBindAttribLocationARB");
3109 list_add_head(&((IWineD3DBaseShaderImpl
*)vshader
)->baseShader
.linked_programs
, &entry
->vshader_entry
);
3112 /* Attach GLSL pshader */
3114 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id
, programId
);
3115 GL_EXTCALL(glAttachObjectARB(programId
, pshader_id
));
3116 checkGLcall("glAttachObjectARB");
3118 list_add_head(&((IWineD3DBaseShaderImpl
*)pshader
)->baseShader
.linked_programs
, &entry
->pshader_entry
);
3121 /* Link the program */
3122 TRACE("Linking GLSL shader program %u\n", programId
);
3123 GL_EXTCALL(glLinkProgramARB(programId
));
3124 print_glsl_info_log(&GLINFO_LOCATION
, programId
);
3126 entry
->vuniformF_locations
= HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB
) * GL_LIMITS(vshader_constantsF
));
3127 for (i
= 0; i
< GL_LIMITS(vshader_constantsF
); ++i
) {
3128 snprintf(glsl_name
, sizeof(glsl_name
), "VC[%i]", i
);
3129 entry
->vuniformF_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
3131 for (i
= 0; i
< MAX_CONST_I
; ++i
) {
3132 snprintf(glsl_name
, sizeof(glsl_name
), "VI[%i]", i
);
3133 entry
->vuniformI_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
3135 entry
->puniformF_locations
= HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB
) * GL_LIMITS(pshader_constantsF
));
3136 for (i
= 0; i
< GL_LIMITS(pshader_constantsF
); ++i
) {
3137 snprintf(glsl_name
, sizeof(glsl_name
), "PC[%i]", i
);
3138 entry
->puniformF_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
3140 for (i
= 0; i
< MAX_CONST_I
; ++i
) {
3141 snprintf(glsl_name
, sizeof(glsl_name
), "PI[%i]", i
);
3142 entry
->puniformI_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
3145 entry
->posFixup_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "posFixup"));
3146 entry
->bumpenvmat_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "bumpenvmat"));
3147 entry
->luminancescale_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "luminancescale"));
3148 entry
->luminanceoffset_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "luminanceoffset"));
3149 entry
->srgb_comparison_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "srgb_comparison"));
3150 entry
->srgb_mul_low_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "srgb_mul_low"));
3151 entry
->ycorrection_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "ycorrection"));
3152 checkGLcall("Find glsl program uniform locations");
3154 /* Set the shader to allow uniform loading on it */
3155 GL_EXTCALL(glUseProgramObjectARB(programId
));
3156 checkGLcall("glUseProgramObjectARB(programId)");
3158 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
3159 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
3160 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
3161 * vertex shader with fixed function pixel processing is used we make sure that the card
3162 * supports enough samplers to allow the max number of vertex samplers with all possible
3163 * fixed function fragment processing setups. So once the program is linked these samplers
3167 /* Load vertex shader samplers */
3168 shader_glsl_load_vsamplers(gl_info
, (IWineD3DStateBlock
*)This
->stateBlock
, programId
);
3171 /* Load pixel shader samplers */
3172 shader_glsl_load_psamplers(gl_info
, (IWineD3DStateBlock
*)This
->stateBlock
, programId
);
3176 static GLhandleARB
create_glsl_blt_shader(WineD3D_GL_Info
*gl_info
) {
3177 GLhandleARB program_id
;
3178 GLhandleARB vshader_id
, pshader_id
;
3179 const char *blt_vshader
[] = {
3182 " gl_Position = gl_Vertex;\n"
3183 " gl_FrontColor = vec4(1.0);\n"
3184 " gl_TexCoord[0].x = (gl_Vertex.x * 0.5) + 0.5;\n"
3185 " gl_TexCoord[0].y = (-gl_Vertex.y * 0.5) + 0.5;\n"
3189 const char *blt_pshader
[] = {
3190 "uniform sampler2D sampler;\n"
3193 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
3197 vshader_id
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
3198 GL_EXTCALL(glShaderSourceARB(vshader_id
, 1, blt_vshader
, NULL
));
3199 GL_EXTCALL(glCompileShaderARB(vshader_id
));
3201 pshader_id
= GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB
));
3202 GL_EXTCALL(glShaderSourceARB(pshader_id
, 1, blt_pshader
, NULL
));
3203 GL_EXTCALL(glCompileShaderARB(pshader_id
));
3205 program_id
= GL_EXTCALL(glCreateProgramObjectARB());
3206 GL_EXTCALL(glAttachObjectARB(program_id
, vshader_id
));
3207 GL_EXTCALL(glAttachObjectARB(program_id
, pshader_id
));
3208 GL_EXTCALL(glLinkProgramARB(program_id
));
3210 print_glsl_info_log(&GLINFO_LOCATION
, program_id
);
3215 static void shader_glsl_select(IWineD3DDevice
*iface
, BOOL usePS
, BOOL useVS
) {
3216 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3217 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3218 GLhandleARB program_id
= 0;
3220 if (useVS
|| usePS
) set_glsl_shader_program(iface
, usePS
, useVS
);
3221 else This
->stateBlock
->glsl_program
= NULL
;
3223 program_id
= This
->stateBlock
->glsl_program
? This
->stateBlock
->glsl_program
->programId
: 0;
3224 if (program_id
) TRACE("Using GLSL program %u\n", program_id
);
3225 GL_EXTCALL(glUseProgramObjectARB(program_id
));
3226 checkGLcall("glUseProgramObjectARB");
3229 static void shader_glsl_select_depth_blt(IWineD3DDevice
*iface
) {
3230 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3231 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3232 static GLhandleARB loc
= -1;
3234 if (!This
->depth_blt_glsl_program_id
) {
3235 This
->depth_blt_glsl_program_id
= create_glsl_blt_shader(gl_info
);
3236 loc
= GL_EXTCALL(glGetUniformLocationARB(This
->depth_blt_glsl_program_id
, "sampler"));
3239 GL_EXTCALL(glUseProgramObjectARB(This
->depth_blt_glsl_program_id
));
3240 GL_EXTCALL(glUniform1iARB(loc
, 0));
3243 static void shader_glsl_destroy_depth_blt(IWineD3DDevice
*iface
) {
3244 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3245 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3247 if(This
->depth_blt_glsl_program_id
) {
3248 GL_EXTCALL(glDeleteObjectARB(This
->depth_blt_glsl_program_id
));
3249 This
->depth_blt_glsl_program_id
= 0;
3253 static void shader_glsl_cleanup(IWineD3DDevice
*iface
) {
3254 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3255 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3256 GL_EXTCALL(glUseProgramObjectARB(0));
3259 static void shader_glsl_destroy(IWineD3DBaseShader
*iface
) {
3260 struct list
*linked_programs
;
3261 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*) iface
;
3262 WineD3D_GL_Info
*gl_info
= &((IWineD3DDeviceImpl
*) This
->baseShader
.device
)->adapter
->gl_info
;
3264 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
3265 * can be called from IWineD3DBaseShader::Release
3267 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
3269 if(This
->baseShader
.prgId
== 0) return;
3270 linked_programs
= &This
->baseShader
.linked_programs
;
3272 TRACE("Deleting linked programs\n");
3273 if (linked_programs
->next
) {
3274 struct glsl_shader_prog_link
*entry
, *entry2
;
3277 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
, struct glsl_shader_prog_link
, pshader_entry
) {
3278 delete_glsl_program_entry(This
->baseShader
.device
, entry
);
3281 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
, struct glsl_shader_prog_link
, vshader_entry
) {
3282 delete_glsl_program_entry(This
->baseShader
.device
, entry
);
3287 TRACE("Deleting shader object %u\n", This
->baseShader
.prgId
);
3288 GL_EXTCALL(glDeleteObjectARB(This
->baseShader
.prgId
));
3289 checkGLcall("glDeleteObjectARB");
3290 This
->baseShader
.prgId
= 0;
3291 This
->baseShader
.is_compiled
= FALSE
;
3294 const shader_backend_t glsl_shader_backend
= {
3295 &shader_glsl_select
,
3296 &shader_glsl_select_depth_blt
,
3297 &shader_glsl_destroy_depth_blt
,
3298 &shader_glsl_load_constants
,
3299 &shader_glsl_cleanup
,
3300 &shader_glsl_color_correction
,
3301 &shader_glsl_destroy