2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
6 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * D3D shader asm has swizzles on source parameters, and write masks for
25 * destination parameters. GLSL uses swizzles for both. The result of this is
26 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
27 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
28 * mask for the destination parameter into account.
33 #include "wined3d_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader
);
36 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants
);
37 WINE_DECLARE_DEBUG_CHANNEL(d3d_caps
);
39 #define GLINFO_LOCATION (*gl_info)
54 } glsl_sample_function_t
;
56 /* GLSL shader private data */
57 struct shader_glsl_priv
{
58 struct hash_table_t
*glsl_program_lookup
;
59 struct glsl_shader_prog_link
*glsl_program
;
60 GLhandleARB depth_blt_program
[tex_type_count
];
63 /* Struct to maintain data about a linked GLSL program */
64 struct glsl_shader_prog_link
{
65 struct list vshader_entry
;
66 struct list pshader_entry
;
67 GLhandleARB programId
;
68 GLhandleARB
*vuniformF_locations
;
69 GLhandleARB
*puniformF_locations
;
70 GLhandleARB vuniformI_locations
[MAX_CONST_I
];
71 GLhandleARB puniformI_locations
[MAX_CONST_I
];
72 GLhandleARB posFixup_location
;
73 GLhandleARB bumpenvmat_location
[MAX_TEXTURES
];
74 GLhandleARB luminancescale_location
[MAX_TEXTURES
];
75 GLhandleARB luminanceoffset_location
[MAX_TEXTURES
];
76 GLhandleARB srgb_comparison_location
;
77 GLhandleARB srgb_mul_low_location
;
78 GLhandleARB ycorrection_location
;
79 GLenum vertex_color_clamp
;
90 /** Prints the GLSL info log which will contain error messages if they exist */
91 static void print_glsl_info_log(WineD3D_GL_Info
*gl_info
, GLhandleARB obj
) {
92 int infologLength
= 0;
97 const char *spam
[] = {
98 "Vertex shader was successfully compiled to run on hardware.\n", /* fglrx */
99 "Fragment shader was successfully compiled to run on hardware.\n", /* fglrx */
100 "Fragment shader(s) linked, vertex shader(s) linked. \n ", /* fglrx, with \n */
101 "Fragment shader(s) linked, vertex shader(s) linked.", /* fglrx, no \n */
102 "Vertex shader(s) linked, no fragment shader(s) defined. \n ", /* fglrx, with \n */
103 "Vertex shader(s) linked, no fragment shader(s) defined.", /* fglrx, no \n */
104 "Fragment shader was successfully compiled to run on hardware.\nWARNING: 0:1: extension 'GL_ARB_draw_buffers' is not supported",
105 "Fragment shader(s) linked, no vertex shader(s) defined.", /* fglrx, no \n */
106 "Fragment shader(s) linked, no vertex shader(s) defined. \n ", /* fglrx, with \n */
107 "WARNING: 0:2: extension 'GL_ARB_draw_buffers' is not supported\n" /* MacOS ati */
110 GL_EXTCALL(glGetObjectParameterivARB(obj
,
111 GL_OBJECT_INFO_LOG_LENGTH_ARB
,
114 /* A size of 1 is just a null-terminated string, so the log should be bigger than
115 * that if there are errors. */
116 if (infologLength
> 1)
118 /* Fglrx doesn't terminate the string properly, but it tells us the proper length.
119 * So use HEAP_ZERO_MEMORY to avoid uninitialized bytes
121 infoLog
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, infologLength
);
122 GL_EXTCALL(glGetInfoLogARB(obj
, infologLength
, NULL
, infoLog
));
125 for(i
= 0; i
< sizeof(spam
) / sizeof(spam
[0]); i
++) {
126 if(strcmp(infoLog
, spam
[i
]) == 0) {
132 TRACE("Spam received from GLSL shader #%u: %s\n", obj
, debugstr_a(infoLog
));
134 FIXME("Error received from GLSL shader #%u: %s\n", obj
, debugstr_a(infoLog
));
136 HeapFree(GetProcessHeap(), 0, infoLog
);
141 * Loads (pixel shader) samplers
143 static void shader_glsl_load_psamplers(
144 WineD3D_GL_Info
*gl_info
,
145 IWineD3DStateBlock
* iface
,
146 GLhandleARB programId
) {
148 IWineD3DStateBlockImpl
* stateBlock
= (IWineD3DStateBlockImpl
*) iface
;
149 GLhandleARB name_loc
;
151 char sampler_name
[20];
153 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
) {
154 snprintf(sampler_name
, sizeof(sampler_name
), "Psampler%d", i
);
155 name_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, sampler_name
));
156 if (name_loc
!= -1) {
157 int mapped_unit
= stateBlock
->wineD3DDevice
->texUnitMap
[i
];
158 if (mapped_unit
!= -1 && mapped_unit
< GL_LIMITS(fragment_samplers
)) {
159 TRACE("Loading %s for texture %d\n", sampler_name
, mapped_unit
);
160 GL_EXTCALL(glUniform1iARB(name_loc
, mapped_unit
));
161 checkGLcall("glUniform1iARB");
163 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name
, mapped_unit
);
169 static void shader_glsl_load_vsamplers(WineD3D_GL_Info
*gl_info
, IWineD3DStateBlock
* iface
, GLhandleARB programId
) {
170 IWineD3DStateBlockImpl
* stateBlock
= (IWineD3DStateBlockImpl
*) iface
;
171 GLhandleARB name_loc
;
172 char sampler_name
[20];
175 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
) {
176 snprintf(sampler_name
, sizeof(sampler_name
), "Vsampler%d", i
);
177 name_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, sampler_name
));
178 if (name_loc
!= -1) {
179 int mapped_unit
= stateBlock
->wineD3DDevice
->texUnitMap
[MAX_FRAGMENT_SAMPLERS
+ i
];
180 if (mapped_unit
!= -1 && mapped_unit
< GL_LIMITS(combined_samplers
)) {
181 TRACE("Loading %s for texture %d\n", sampler_name
, mapped_unit
);
182 GL_EXTCALL(glUniform1iARB(name_loc
, mapped_unit
));
183 checkGLcall("glUniform1iARB");
185 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name
, mapped_unit
);
192 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
193 * When constant_list == NULL, it will load all the constants.
195 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl
* This
, WineD3D_GL_Info
*gl_info
,
196 unsigned int max_constants
, float* constants
, GLhandleARB
*constant_locations
,
197 struct list
*constant_list
) {
198 constants_entry
*constant
;
199 local_constant
* lconst
;
204 if (TRACE_ON(d3d_shader
)) {
205 LIST_FOR_EACH_ENTRY(constant
, constant_list
, constants_entry
, entry
) {
210 tmp_loc
= constant_locations
[i
];
212 TRACE_(d3d_constants
)("Loading constants %i: %f, %f, %f, %f\n", i
,
213 constants
[i
* 4 + 0], constants
[i
* 4 + 1],
214 constants
[i
* 4 + 2], constants
[i
* 4 + 3]);
220 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
221 if(WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) == 1 &&
222 shader_is_pshader_version(This
->baseShader
.hex_version
)) {
225 LIST_FOR_EACH_ENTRY(constant
, constant_list
, constants_entry
, entry
) {
230 tmp_loc
= constant_locations
[i
];
232 /* We found this uniform name in the program - go ahead and send the data */
234 if(constants
[k
+ 0] < -1.0) lcl_const
[0] = -1.0;
235 else if(constants
[k
+ 0] > 1.0) lcl_const
[0] = 1.0;
236 else lcl_const
[0] = constants
[k
+ 0];
237 if(constants
[k
+ 1] < -1.0) lcl_const
[1] = -1.0;
238 else if(constants
[k
+ 1] > 1.0) lcl_const
[1] = 1.0;
239 else lcl_const
[1] = constants
[k
+ 1];
240 if(constants
[k
+ 2] < -1.0) lcl_const
[2] = -1.0;
241 else if(constants
[k
+ 2] > 1.0) lcl_const
[2] = 1.0;
242 else lcl_const
[2] = constants
[k
+ 2];
243 if(constants
[k
+ 3] < -1.0) lcl_const
[3] = -1.0;
244 else if(constants
[k
+ 3] > 1.0) lcl_const
[3] = 1.0;
245 else lcl_const
[3] = constants
[k
+ 3];
247 GL_EXTCALL(glUniform4fvARB(tmp_loc
, 1, lcl_const
));
252 LIST_FOR_EACH_ENTRY(constant
, constant_list
, constants_entry
, entry
) {
257 tmp_loc
= constant_locations
[i
];
259 /* We found this uniform name in the program - go ahead and send the data */
260 GL_EXTCALL(glUniform4fvARB(tmp_loc
, 1, constants
+ (i
* 4)));
265 checkGLcall("glUniform4fvARB()");
267 if(!This
->baseShader
.load_local_constsF
) {
268 TRACE("No need to load local float constants for this shader\n");
272 /* Load immediate constants */
273 if (TRACE_ON(d3d_shader
)) {
274 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
275 tmp_loc
= constant_locations
[lconst
->idx
];
277 GLfloat
* values
= (GLfloat
*)lconst
->value
;
278 TRACE_(d3d_constants
)("Loading local constants %i: %f, %f, %f, %f\n", lconst
->idx
,
279 values
[0], values
[1], values
[2], values
[3]);
283 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
284 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
285 tmp_loc
= constant_locations
[lconst
->idx
];
287 /* We found this uniform name in the program - go ahead and send the data */
288 GL_EXTCALL(glUniform4fvARB(tmp_loc
, 1, (GLfloat
*)lconst
->value
));
291 checkGLcall("glUniform4fvARB()");
295 * Loads integer 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_constantsI(
299 IWineD3DBaseShaderImpl
* This
,
300 WineD3D_GL_Info
*gl_info
,
301 GLhandleARB programId
,
302 GLhandleARB locations
[MAX_CONST_I
],
303 unsigned max_constants
,
305 BOOL
* constants_set
) {
310 for (i
=0; i
<max_constants
; ++i
) {
311 if (NULL
== constants_set
|| constants_set
[i
]) {
313 TRACE_(d3d_constants
)("Loading constants %i: %i, %i, %i, %i\n",
314 i
, constants
[i
*4], constants
[i
*4+1], constants
[i
*4+2], constants
[i
*4+3]);
316 /* We found this uniform name in the program - go ahead and send the data */
317 GL_EXTCALL(glUniform4ivARB(locations
[i
], 1, &constants
[i
*4]));
318 checkGLcall("glUniform4ivARB");
322 /* Load immediate constants */
323 ptr
= list_head(&This
->baseShader
.constantsI
);
325 local_constant
* lconst
= LIST_ENTRY(ptr
, struct local_constant
, entry
);
326 unsigned int idx
= lconst
->idx
;
327 GLint
* values
= (GLint
*) lconst
->value
;
329 TRACE_(d3d_constants
)("Loading local constants %i: %i, %i, %i, %i\n", idx
,
330 values
[0], values
[1], values
[2], values
[3]);
332 /* We found this uniform name in the program - go ahead and send the data */
333 GL_EXTCALL(glUniform4ivARB(locations
[idx
], 1, values
));
334 checkGLcall("glUniform4ivARB");
335 ptr
= list_next(&This
->baseShader
.constantsI
, ptr
);
340 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
341 * When @constants_set == NULL, it will load all the constants.
343 static void shader_glsl_load_constantsB(
344 IWineD3DBaseShaderImpl
* This
,
345 WineD3D_GL_Info
*gl_info
,
346 GLhandleARB programId
,
347 unsigned max_constants
,
349 BOOL
* constants_set
) {
354 char is_pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
355 const char* prefix
= is_pshader
? "PB":"VB";
358 for (i
=0; i
<max_constants
; ++i
) {
359 if (NULL
== constants_set
|| constants_set
[i
]) {
361 TRACE_(d3d_constants
)("Loading constants %i: %i;\n", i
, constants
[i
]);
363 /* TODO: Benchmark and see if it would be beneficial to store the
364 * locations of the constants to avoid looking up each time */
365 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, i
);
366 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
368 /* We found this uniform name in the program - go ahead and send the data */
369 GL_EXTCALL(glUniform1ivARB(tmp_loc
, 1, &constants
[i
]));
370 checkGLcall("glUniform1ivARB");
375 /* Load immediate constants */
376 ptr
= list_head(&This
->baseShader
.constantsB
);
378 local_constant
* lconst
= LIST_ENTRY(ptr
, struct local_constant
, entry
);
379 unsigned int idx
= lconst
->idx
;
380 GLint
* values
= (GLint
*) lconst
->value
;
382 TRACE_(d3d_constants
)("Loading local constants %i: %i\n", idx
, values
[0]);
384 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, idx
);
385 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
387 /* We found this uniform name in the program - go ahead and send the data */
388 GL_EXTCALL(glUniform1ivARB(tmp_loc
, 1, values
));
389 checkGLcall("glUniform1ivARB");
391 ptr
= list_next(&This
->baseShader
.constantsB
, ptr
);
398 * Loads the app-supplied constants into the currently set GLSL program.
400 static void shader_glsl_load_constants(
401 IWineD3DDevice
* device
,
403 char useVertexShader
) {
405 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) device
;
406 struct shader_glsl_priv
*priv
= (struct shader_glsl_priv
*)deviceImpl
->shader_priv
;
407 IWineD3DStateBlockImpl
* stateBlock
= deviceImpl
->stateBlock
;
408 WineD3D_GL_Info
*gl_info
= &deviceImpl
->adapter
->gl_info
;
410 GLhandleARB
*constant_locations
;
411 struct list
*constant_list
;
412 GLhandleARB programId
;
413 struct glsl_shader_prog_link
*prog
= priv
->glsl_program
;
417 /* No GLSL program set - nothing to do. */
420 programId
= prog
->programId
;
422 if (useVertexShader
) {
423 IWineD3DBaseShaderImpl
* vshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->vertexShader
;
425 constant_locations
= prog
->vuniformF_locations
;
426 constant_list
= &stateBlock
->set_vconstantsF
;
428 /* Load DirectX 9 float constants/uniforms for vertex shader */
429 shader_glsl_load_constantsF(vshader
, gl_info
, GL_LIMITS(vshader_constantsF
),
430 stateBlock
->vertexShaderConstantF
, constant_locations
, constant_list
);
432 /* Load DirectX 9 integer constants/uniforms for vertex shader */
433 shader_glsl_load_constantsI(vshader
, gl_info
, programId
,
434 prog
->vuniformI_locations
, MAX_CONST_I
,
435 stateBlock
->vertexShaderConstantI
,
436 stateBlock
->changed
.vertexShaderConstantsI
);
438 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
439 shader_glsl_load_constantsB(vshader
, gl_info
, programId
, MAX_CONST_B
,
440 stateBlock
->vertexShaderConstantB
,
441 stateBlock
->changed
.vertexShaderConstantsB
);
443 /* Upload the position fixup params */
444 GL_EXTCALL(glUniform4fvARB(prog
->posFixup_location
, 1, &deviceImpl
->posFixup
[0]));
445 checkGLcall("glUniform4fvARB");
448 if (usePixelShader
) {
450 IWineD3DBaseShaderImpl
* pshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->pixelShader
;
452 constant_locations
= prog
->puniformF_locations
;
453 constant_list
= &stateBlock
->set_pconstantsF
;
455 /* Load DirectX 9 float constants/uniforms for pixel shader */
456 shader_glsl_load_constantsF(pshader
, gl_info
, GL_LIMITS(pshader_constantsF
),
457 stateBlock
->pixelShaderConstantF
, constant_locations
, constant_list
);
459 /* Load DirectX 9 integer constants/uniforms for pixel shader */
460 shader_glsl_load_constantsI(pshader
, gl_info
, programId
,
461 prog
->puniformI_locations
, MAX_CONST_I
,
462 stateBlock
->pixelShaderConstantI
,
463 stateBlock
->changed
.pixelShaderConstantsI
);
465 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
466 shader_glsl_load_constantsB(pshader
, gl_info
, programId
, MAX_CONST_B
,
467 stateBlock
->pixelShaderConstantB
,
468 stateBlock
->changed
.pixelShaderConstantsB
);
470 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
471 * It can't be 0 for a valid texbem instruction.
473 for(i
= 0; i
< ((IWineD3DPixelShaderImpl
*) pshader
)->numbumpenvmatconsts
; i
++) {
474 IWineD3DPixelShaderImpl
*ps
= (IWineD3DPixelShaderImpl
*) pshader
;
475 int stage
= ps
->luminanceconst
[i
].texunit
;
477 float *data
= (float *) &stateBlock
->textureState
[(int) ps
->bumpenvmatconst
[i
].texunit
][WINED3DTSS_BUMPENVMAT00
];
478 GL_EXTCALL(glUniformMatrix2fvARB(prog
->bumpenvmat_location
[i
], 1, 0, data
));
479 checkGLcall("glUniformMatrix2fvARB");
481 /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
482 * is set too, so we can check that in the needsbumpmat check
484 if(ps
->baseShader
.reg_maps
.luminanceparams
[stage
]) {
485 GLfloat
*scale
= (GLfloat
*) &stateBlock
->textureState
[stage
][WINED3DTSS_BUMPENVLSCALE
];
486 GLfloat
*offset
= (GLfloat
*) &stateBlock
->textureState
[stage
][WINED3DTSS_BUMPENVLOFFSET
];
488 GL_EXTCALL(glUniform1fvARB(prog
->luminancescale_location
[i
], 1, scale
));
489 checkGLcall("glUniform1fvARB");
490 GL_EXTCALL(glUniform1fvARB(prog
->luminanceoffset_location
[i
], 1, offset
));
491 checkGLcall("glUniform1fvARB");
495 if(((IWineD3DPixelShaderImpl
*) pshader
)->srgb_enabled
&&
496 !((IWineD3DPixelShaderImpl
*) pshader
)->srgb_mode_hardcoded
) {
500 if(stateBlock
->renderState
[WINED3DRS_SRGBWRITEENABLE
]) {
501 comparison
[0] = srgb_cmp
; comparison
[1] = srgb_cmp
;
502 comparison
[2] = srgb_cmp
; comparison
[3] = srgb_cmp
;
504 mul_low
[0] = srgb_mul_low
; mul_low
[1] = srgb_mul_low
;
505 mul_low
[2] = srgb_mul_low
; mul_low
[3] = srgb_mul_low
;
507 comparison
[0] = 1.0 / 0.0; comparison
[1] = 1.0 / 0.0;
508 comparison
[2] = 1.0 / 0.0; comparison
[3] = 1.0 / 0.0;
510 mul_low
[0] = 1.0; mul_low
[1] = 1.0;
511 mul_low
[2] = 1.0; mul_low
[3] = 1.0;
514 GL_EXTCALL(glUniform4fvARB(prog
->srgb_comparison_location
, 1, comparison
));
515 GL_EXTCALL(glUniform4fvARB(prog
->srgb_mul_low_location
, 1, mul_low
));
517 if(((IWineD3DPixelShaderImpl
*) pshader
)->vpos_uniform
) {
518 float correction_params
[4];
519 if(deviceImpl
->render_offscreen
) {
520 correction_params
[0] = 0.0;
521 correction_params
[1] = 1.0;
523 /* position is window relative, not viewport relative */
524 correction_params
[0] = ((IWineD3DSurfaceImpl
*) deviceImpl
->render_targets
[0])->currentDesc
.Height
;
525 correction_params
[1] = -1.0;
527 GL_EXTCALL(glUniform4fvARB(prog
->ycorrection_location
, 1, correction_params
));
532 /** Generate the variable & register declarations for the GLSL output target */
533 static void shader_generate_glsl_declarations(
534 IWineD3DBaseShader
*iface
,
535 shader_reg_maps
* reg_maps
,
536 SHADER_BUFFER
* buffer
,
537 WineD3D_GL_Info
* gl_info
) {
539 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
540 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
542 unsigned int extra_constants_needed
= 0;
543 local_constant
* lconst
;
545 /* There are some minor differences between pixel and vertex shaders */
546 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
547 char prefix
= pshader
? 'P' : 'V';
549 /* Prototype the subroutines */
550 for (i
= 0; i
< This
->baseShader
.limits
.label
; i
++) {
551 if (reg_maps
->labels
[i
])
552 shader_addline(buffer
, "void subroutine%u();\n", i
);
555 /* Declare the constants (aka uniforms) */
556 if (This
->baseShader
.limits
.constant_float
> 0) {
557 unsigned max_constantsF
= min(This
->baseShader
.limits
.constant_float
,
558 (pshader
? GL_LIMITS(pshader_constantsF
) : GL_LIMITS(vshader_constantsF
)));
559 shader_addline(buffer
, "uniform vec4 %cC[%u];\n", prefix
, max_constantsF
);
562 if (This
->baseShader
.limits
.constant_int
> 0)
563 shader_addline(buffer
, "uniform ivec4 %cI[%u];\n", prefix
, This
->baseShader
.limits
.constant_int
);
565 if (This
->baseShader
.limits
.constant_bool
> 0)
566 shader_addline(buffer
, "uniform bool %cB[%u];\n", prefix
, This
->baseShader
.limits
.constant_bool
);
569 shader_addline(buffer
, "uniform vec4 posFixup;\n");
570 /* Predeclaration; This function is added at link time based on the pixel shader.
571 * VS 3.0 shaders have an array OUT[] the shader writes to, earlier versions don't have
572 * that. We know the input to the reorder function at vertex shader compile time, so
573 * we can deal with that. The reorder function for a 1.x and 2.x vertex shader can just
574 * read gl_FrontColor. The output depends on the pixel shader. The reorder function for a
575 * 1.x and 2.x pshader or for fixed function will write gl_FrontColor, and for a 3.0 shader
576 * it will write to the varying array. Here we depend on the shader optimizer on sorting that
577 * out. The nvidia driver only does that if the parameter is inout instead of out, hence the
580 if(This
->baseShader
.hex_version
>= WINED3DVS_VERSION(3, 0)) {
581 shader_addline(buffer
, "void order_ps_input(in vec4[%u]);\n", MAX_REG_OUTPUT
);
583 shader_addline(buffer
, "void order_ps_input();\n");
586 IWineD3DPixelShaderImpl
*ps_impl
= (IWineD3DPixelShaderImpl
*) This
;
588 ps_impl
->numbumpenvmatconsts
= 0;
589 for(i
= 0; i
< (sizeof(reg_maps
->bumpmat
) / sizeof(reg_maps
->bumpmat
[0])); i
++) {
590 if(!reg_maps
->bumpmat
[i
]) {
594 ps_impl
->bumpenvmatconst
[(int) ps_impl
->numbumpenvmatconsts
].texunit
= i
;
595 shader_addline(buffer
, "uniform mat2 bumpenvmat%d;\n", i
);
597 if(reg_maps
->luminanceparams
) {
598 ps_impl
->luminanceconst
[(int) ps_impl
->numbumpenvmatconsts
].texunit
= i
;
599 shader_addline(buffer
, "uniform float luminancescale%d;\n", i
);
600 shader_addline(buffer
, "uniform float luminanceoffset%d;\n", i
);
601 extra_constants_needed
++;
603 ps_impl
->luminanceconst
[(int) ps_impl
->numbumpenvmatconsts
].texunit
= -1;
606 extra_constants_needed
++;
607 ps_impl
->numbumpenvmatconsts
++;
610 if(device
->stateBlock
->renderState
[WINED3DRS_SRGBWRITEENABLE
]) {
611 ps_impl
->srgb_enabled
= 1;
612 if(This
->baseShader
.limits
.constant_float
+ extra_constants_needed
+ 1 < GL_LIMITS(pshader_constantsF
)) {
613 shader_addline(buffer
, "uniform vec4 srgb_mul_low;\n");
614 shader_addline(buffer
, "uniform vec4 srgb_comparison;\n");
615 ps_impl
->srgb_mode_hardcoded
= 0;
616 extra_constants_needed
++;
618 ps_impl
->srgb_mode_hardcoded
= 1;
619 shader_addline(buffer
, "const vec4 srgb_mul_low = vec4(%f, %f, %f, %f);\n",
620 srgb_mul_low
, srgb_mul_low
, srgb_mul_low
, srgb_mul_low
);
621 shader_addline(buffer
, "const vec4 srgb_comparison = vec4(%f, %f, %f, %f);\n",
622 srgb_cmp
, srgb_cmp
, srgb_cmp
, srgb_cmp
);
625 IWineD3DPixelShaderImpl
*ps_impl
= (IWineD3DPixelShaderImpl
*) This
;
627 /* Do not write any srgb fixup into the shader to save shader size and processing time.
628 * As a consequence, we can't toggle srgb write on without recompilation
630 ps_impl
->srgb_enabled
= 0;
631 ps_impl
->srgb_mode_hardcoded
= 1;
633 if(reg_maps
->vpos
|| reg_maps
->usesdsy
) {
634 if(This
->baseShader
.limits
.constant_float
+ extra_constants_needed
+ 1 < GL_LIMITS(pshader_constantsF
)) {
635 shader_addline(buffer
, "uniform vec4 ycorrection;\n");
636 ((IWineD3DPixelShaderImpl
*) This
)->vpos_uniform
= 1;
637 extra_constants_needed
++;
639 /* This happens because we do not have proper tracking of the constant registers that are
640 * actually used, only the max limit of the shader version
642 FIXME("Cannot find a free uniform for vpos correction params\n");
643 shader_addline(buffer
, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
644 device
->render_offscreen
? 0.0 : ((IWineD3DSurfaceImpl
*) device
->render_targets
[0])->currentDesc
.Height
,
645 device
->render_offscreen
? 1.0 : -1.0);
647 shader_addline(buffer
, "vec4 vpos;\n");
651 /* Declare texture samplers */
652 for (i
= 0; i
< This
->baseShader
.limits
.sampler
; i
++) {
653 if (reg_maps
->samplers
[i
]) {
655 DWORD stype
= reg_maps
->samplers
[i
] & WINED3DSP_TEXTURETYPE_MASK
;
659 shader_addline(buffer
, "uniform sampler1D %csampler%u;\n", prefix
, i
);
662 if(device
->stateBlock
->textures
[i
] &&
663 IWineD3DBaseTexture_GetTextureDimensions(device
->stateBlock
->textures
[i
]) == GL_TEXTURE_RECTANGLE_ARB
) {
664 shader_addline(buffer
, "uniform sampler2DRect %csampler%u;\n", prefix
, i
);
666 shader_addline(buffer
, "uniform sampler2D %csampler%u;\n", prefix
, i
);
669 case WINED3DSTT_CUBE
:
670 shader_addline(buffer
, "uniform samplerCube %csampler%u;\n", prefix
, i
);
672 case WINED3DSTT_VOLUME
:
673 shader_addline(buffer
, "uniform sampler3D %csampler%u;\n", prefix
, i
);
676 shader_addline(buffer
, "uniform unsupported_sampler %csampler%u;\n", prefix
, i
);
677 FIXME("Unrecognized sampler type: %#x\n", stype
);
683 /* Declare address variables */
684 for (i
= 0; i
< This
->baseShader
.limits
.address
; i
++) {
685 if (reg_maps
->address
[i
])
686 shader_addline(buffer
, "ivec4 A%d;\n", i
);
689 /* Declare texture coordinate temporaries and initialize them */
690 for (i
= 0; i
< This
->baseShader
.limits
.texcoord
; i
++) {
691 if (reg_maps
->texcoord
[i
])
692 shader_addline(buffer
, "vec4 T%u = gl_TexCoord[%u];\n", i
, i
);
695 /* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
696 * helper function shader that is linked in at link time
698 if(pshader
&& This
->baseShader
.hex_version
>= WINED3DPS_VERSION(3, 0)) {
700 shader_addline(buffer
, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings
) / 4);
702 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
703 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
704 * pixel shader that reads the fixed function color into the packed input registers.
706 shader_addline(buffer
, "vec4 IN[%u];\n", GL_LIMITS(glsl_varyings
) / 4);
710 /* Declare output register temporaries */
711 if(This
->baseShader
.limits
.packed_output
) {
712 shader_addline(buffer
, "vec4 OUT[%u];\n", This
->baseShader
.limits
.packed_output
);
715 /* Declare temporary variables */
716 for(i
= 0; i
< This
->baseShader
.limits
.temporary
; i
++) {
717 if (reg_maps
->temporary
[i
])
718 shader_addline(buffer
, "vec4 R%u;\n", i
);
721 /* Declare attributes */
722 for (i
= 0; i
< This
->baseShader
.limits
.attributes
; i
++) {
723 if (reg_maps
->attributes
[i
])
724 shader_addline(buffer
, "attribute vec4 attrib%i;\n", i
);
727 /* Declare loop registers aLx */
728 for (i
= 0; i
< reg_maps
->loop_depth
; i
++) {
729 shader_addline(buffer
, "int aL%u;\n", i
);
730 shader_addline(buffer
, "int tmpInt%u;\n", i
);
733 /* Temporary variables for matrix operations */
734 shader_addline(buffer
, "vec4 tmp0;\n");
735 shader_addline(buffer
, "vec4 tmp1;\n");
737 /* Local constants use a different name so they can be loaded once at shader link time
738 * They can't be hardcoded into the shader text via LC = {x, y, z, w}; because the
739 * float -> string conversion can cause precision loss.
741 if(!This
->baseShader
.load_local_constsF
) {
742 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
743 shader_addline(buffer
, "uniform vec4 %cLC%u;\n", prefix
, lconst
->idx
);
747 /* Start the main program */
748 shader_addline(buffer
, "void main() {\n");
749 if(pshader
&& reg_maps
->vpos
) {
750 /* DirectX apps expect integer values, while OpenGL drivers add approximately 0.5. This causes
751 * off-by-one problems as spotted by the vPos d3d9 visual test. Unfortunately the ATI cards do
752 * not add exactly 0.5, but rather something like 0.49999999 or 0.50000001, which still causes
753 * precision troubles when we just substract 0.5.
755 * To deal with that just floor() the position. This will eliminate the fraction on all cards.
757 * TODO: Test how that behaves with multisampling once we can enable multisampling in winex11.
759 * An advantage of floor is that it works even if the driver doesn't add 1/2. It is somewhat
760 * questionable if 1.5, 2.5, ... are the proper values to return in gl_FragCoord, even though
761 * coordinates specify the pixel centers instead of the pixel corners. This code will behave
762 * correctly on drivers that returns integer values.
764 shader_addline(buffer
, "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
768 /*****************************************************************************
769 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
771 * For more information, see http://wiki.winehq.org/DirectX-Shaders
772 ****************************************************************************/
775 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG
* arg
, const DWORD param
,
776 const DWORD addr_token
, DWORD mask
, glsl_src_param_t
*src_param
);
778 /** Used for opcode modifiers - They multiply the result by the specified amount */
779 static const char * const shift_glsl_tab
[] = {
781 "2.0 * ", /* 1 (x2) */
782 "4.0 * ", /* 2 (x4) */
783 "8.0 * ", /* 3 (x8) */
784 "16.0 * ", /* 4 (x16) */
785 "32.0 * ", /* 5 (x32) */
792 "0.0625 * ", /* 12 (d16) */
793 "0.125 * ", /* 13 (d8) */
794 "0.25 * ", /* 14 (d4) */
795 "0.5 * " /* 15 (d2) */
798 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
799 static void shader_glsl_gen_modifier (
802 const char *in_regswizzle
,
807 if (instr
== WINED3DSIO_TEXKILL
)
810 switch (instr
& WINED3DSP_SRCMOD_MASK
) {
811 case WINED3DSPSM_DZ
: /* Need to handle this in the instructions itself (texld & texcrd). */
813 case WINED3DSPSM_NONE
:
814 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
816 case WINED3DSPSM_NEG
:
817 sprintf(out_str
, "-%s%s", in_reg
, in_regswizzle
);
819 case WINED3DSPSM_NOT
:
820 sprintf(out_str
, "!%s%s", in_reg
, in_regswizzle
);
822 case WINED3DSPSM_BIAS
:
823 sprintf(out_str
, "(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
825 case WINED3DSPSM_BIASNEG
:
826 sprintf(out_str
, "-(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
828 case WINED3DSPSM_SIGN
:
829 sprintf(out_str
, "(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
831 case WINED3DSPSM_SIGNNEG
:
832 sprintf(out_str
, "-(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
834 case WINED3DSPSM_COMP
:
835 sprintf(out_str
, "(1.0 - %s%s)", in_reg
, in_regswizzle
);
838 sprintf(out_str
, "(2.0 * %s%s)", in_reg
, in_regswizzle
);
840 case WINED3DSPSM_X2NEG
:
841 sprintf(out_str
, "-(2.0 * %s%s)", in_reg
, in_regswizzle
);
843 case WINED3DSPSM_ABS
:
844 sprintf(out_str
, "abs(%s%s)", in_reg
, in_regswizzle
);
846 case WINED3DSPSM_ABSNEG
:
847 sprintf(out_str
, "-abs(%s%s)", in_reg
, in_regswizzle
);
850 FIXME("Unhandled modifier %u\n", (instr
& WINED3DSP_SRCMOD_MASK
));
851 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
855 /** Writes the GLSL variable name that corresponds to the register that the
856 * DX opcode parameter is trying to access */
857 static void shader_glsl_get_register_name(
859 const DWORD addr_token
,
862 SHADER_OPCODE_ARG
* arg
) {
864 /* oPos, oFog and oPts in D3D */
865 static const char * const hwrastout_reg_names
[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
867 DWORD reg
= param
& WINED3DSP_REGNUM_MASK
;
868 DWORD regtype
= shader_get_regtype(param
);
869 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
870 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
871 WineD3D_GL_Info
* gl_info
= &deviceImpl
->adapter
->gl_info
;
873 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
879 case WINED3DSPR_TEMP
:
880 sprintf(tmpStr
, "R%u", reg
);
882 case WINED3DSPR_INPUT
:
884 /* Pixel shaders >= 3.0 */
885 if (WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 3) {
886 DWORD in_count
= GL_LIMITS(glsl_varyings
) / 4;
888 if (param
& WINED3DSHADER_ADDRMODE_RELATIVE
) {
889 glsl_src_param_t rel_param
;
890 shader_glsl_add_src_param(arg
, addr_token
, 0, WINED3DSP_WRITEMASK_0
, &rel_param
);
892 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
895 if(((IWineD3DPixelShaderImpl
*) This
)->input_reg_map
[reg
]) {
896 if (((IWineD3DPixelShaderImpl
*)This
)->declared_in_count
> in_count
) {
897 sprintf(tmpStr
, "((%s + %u) > %d ? (%s + %u) > %d ? gl_SecondaryColor : gl_Color : IN[%s + %u])",
898 rel_param
.param_str
, ((IWineD3DPixelShaderImpl
*)This
)->input_reg_map
[reg
], in_count
- 1,
899 rel_param
.param_str
, ((IWineD3DPixelShaderImpl
*)This
)->input_reg_map
[reg
], in_count
,
900 rel_param
.param_str
, ((IWineD3DPixelShaderImpl
*)This
)->input_reg_map
[reg
]);
902 sprintf(tmpStr
, "IN[%s + %u]", rel_param
.param_str
, ((IWineD3DPixelShaderImpl
*)This
)->input_reg_map
[reg
]);
905 if (((IWineD3DPixelShaderImpl
*)This
)->declared_in_count
> in_count
) {
906 sprintf(tmpStr
, "((%s) > %d ? (%s) > %d ? gl_SecondaryColor : gl_Color : IN[%s])",
907 rel_param
.param_str
, in_count
- 1,
908 rel_param
.param_str
, in_count
,
909 rel_param
.param_str
);
911 sprintf(tmpStr
, "IN[%s]", rel_param
.param_str
);
915 DWORD idx
= ((IWineD3DPixelShaderImpl
*) This
)->input_reg_map
[reg
];
916 if (idx
== in_count
) {
917 sprintf(tmpStr
, "gl_Color");
918 } else if (idx
== in_count
+ 1) {
919 sprintf(tmpStr
, "gl_SecondaryColor");
921 sprintf(tmpStr
, "IN[%u]", idx
);
926 strcpy(tmpStr
, "gl_Color");
928 strcpy(tmpStr
, "gl_SecondaryColor");
931 if (vshader_input_is_color((IWineD3DVertexShader
*) This
, reg
))
933 sprintf(tmpStr
, "attrib%u", reg
);
936 case WINED3DSPR_CONST
:
938 const char prefix
= pshader
? 'P':'V';
940 /* Relative addressing */
941 if (param
& WINED3DSHADER_ADDRMODE_RELATIVE
) {
943 /* Relative addressing on shaders 2.0+ have a relative address token,
944 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
945 if (WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 2) {
946 glsl_src_param_t rel_param
;
947 shader_glsl_add_src_param(arg
, addr_token
, 0, WINED3DSP_WRITEMASK_0
, &rel_param
);
949 sprintf(tmpStr
, "%cC[%s + %u]", prefix
, rel_param
.param_str
, reg
);
951 sprintf(tmpStr
, "%cC[%s]", prefix
, rel_param
.param_str
);
955 sprintf(tmpStr
, "%cC[A0.x + %u]", prefix
, reg
);
957 sprintf(tmpStr
, "%cC[A0.x]", prefix
);
962 if(shader_constant_is_local(This
, reg
)) {
963 sprintf(tmpStr
, "%cLC%u", prefix
, reg
);
965 sprintf(tmpStr
, "%cC[%u]", prefix
, reg
);
971 case WINED3DSPR_CONSTINT
:
973 sprintf(tmpStr
, "PI[%u]", reg
);
975 sprintf(tmpStr
, "VI[%u]", reg
);
977 case WINED3DSPR_CONSTBOOL
:
979 sprintf(tmpStr
, "PB[%u]", reg
);
981 sprintf(tmpStr
, "VB[%u]", reg
);
983 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
985 sprintf(tmpStr
, "T%u", reg
);
987 sprintf(tmpStr
, "A%u", reg
);
990 case WINED3DSPR_LOOP
:
991 sprintf(tmpStr
, "aL%u", This
->baseShader
.cur_loop_regno
- 1);
993 case WINED3DSPR_SAMPLER
:
995 sprintf(tmpStr
, "Psampler%u", reg
);
997 sprintf(tmpStr
, "Vsampler%u", reg
);
999 case WINED3DSPR_COLOROUT
:
1000 if (reg
>= GL_LIMITS(buffers
)) {
1001 WARN("Write to render target %u, only %d supported\n", reg
, 4);
1003 if (GL_SUPPORT(ARB_DRAW_BUFFERS
)) {
1004 sprintf(tmpStr
, "gl_FragData[%u]", reg
);
1005 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
1006 sprintf(tmpStr
, "gl_FragColor");
1009 case WINED3DSPR_RASTOUT
:
1010 sprintf(tmpStr
, "%s", hwrastout_reg_names
[reg
]);
1012 case WINED3DSPR_DEPTHOUT
:
1013 sprintf(tmpStr
, "gl_FragDepth");
1015 case WINED3DSPR_ATTROUT
:
1017 sprintf(tmpStr
, "gl_FrontColor");
1019 sprintf(tmpStr
, "gl_FrontSecondaryColor");
1022 case WINED3DSPR_TEXCRDOUT
:
1023 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1024 if (WINED3DSHADER_VERSION_MAJOR(This
->baseShader
.hex_version
) >= 3)
1025 sprintf(tmpStr
, "OUT[%u]", reg
);
1027 sprintf(tmpStr
, "gl_TexCoord[%u]", reg
);
1029 case WINED3DSPR_MISCTYPE
:
1032 sprintf(tmpStr
, "vpos");
1033 } else if (reg
== 1){
1034 /* Note that gl_FrontFacing is a bool, while vFace is
1035 * a float for which the sign determines front/back
1037 sprintf(tmpStr
, "(gl_FrontFacing ? 1.0 : -1.0)");
1039 FIXME("Unhandled misctype register %d\n", reg
);
1040 sprintf(tmpStr
, "unrecognized_register");
1044 FIXME("Unhandled register name Type(%d)\n", regtype
);
1045 sprintf(tmpStr
, "unrecognized_register");
1049 strcat(regstr
, tmpStr
);
1052 /* Get the GLSL write mask for the destination register */
1053 static DWORD
shader_glsl_get_write_mask(const DWORD param
, char *write_mask
) {
1054 char *ptr
= write_mask
;
1055 DWORD mask
= param
& WINED3DSP_WRITEMASK_ALL
;
1057 if (shader_is_scalar(param
)) {
1058 mask
= WINED3DSP_WRITEMASK_0
;
1061 if (param
& WINED3DSP_WRITEMASK_0
) *ptr
++ = 'x';
1062 if (param
& WINED3DSP_WRITEMASK_1
) *ptr
++ = 'y';
1063 if (param
& WINED3DSP_WRITEMASK_2
) *ptr
++ = 'z';
1064 if (param
& WINED3DSP_WRITEMASK_3
) *ptr
++ = 'w';
1072 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask
) {
1073 unsigned int size
= 0;
1075 if (write_mask
& WINED3DSP_WRITEMASK_0
) ++size
;
1076 if (write_mask
& WINED3DSP_WRITEMASK_1
) ++size
;
1077 if (write_mask
& WINED3DSP_WRITEMASK_2
) ++size
;
1078 if (write_mask
& WINED3DSP_WRITEMASK_3
) ++size
;
1083 static void shader_glsl_get_swizzle(const DWORD param
, BOOL fixup
, DWORD mask
, char *swizzle_str
) {
1084 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1085 * but addressed as "rgba". To fix this we need to swap the register's x
1086 * and z components. */
1087 DWORD swizzle
= (param
& WINED3DSP_SWIZZLE_MASK
) >> WINED3DSP_SWIZZLE_SHIFT
;
1088 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
1089 char *ptr
= swizzle_str
;
1091 if (!shader_is_scalar(param
)) {
1093 /* swizzle bits fields: wwzzyyxx */
1094 if (mask
& WINED3DSP_WRITEMASK_0
) *ptr
++ = swizzle_chars
[swizzle
& 0x03];
1095 if (mask
& WINED3DSP_WRITEMASK_1
) *ptr
++ = swizzle_chars
[(swizzle
>> 2) & 0x03];
1096 if (mask
& WINED3DSP_WRITEMASK_2
) *ptr
++ = swizzle_chars
[(swizzle
>> 4) & 0x03];
1097 if (mask
& WINED3DSP_WRITEMASK_3
) *ptr
++ = swizzle_chars
[(swizzle
>> 6) & 0x03];
1103 /* From a given parameter token, generate the corresponding GLSL string.
1104 * Also, return the actual register name and swizzle in case the
1105 * caller needs this information as well. */
1106 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG
* arg
, const DWORD param
,
1107 const DWORD addr_token
, DWORD mask
, glsl_src_param_t
*src_param
) {
1108 BOOL is_color
= FALSE
;
1109 char swizzle_str
[6];
1111 src_param
->reg_name
[0] = '\0';
1112 src_param
->param_str
[0] = '\0';
1113 swizzle_str
[0] = '\0';
1115 shader_glsl_get_register_name(param
, addr_token
, src_param
->reg_name
, &is_color
, arg
);
1117 shader_glsl_get_swizzle(param
, is_color
, mask
, swizzle_str
);
1118 shader_glsl_gen_modifier(param
, src_param
->reg_name
, swizzle_str
, src_param
->param_str
);
1121 /* From a given parameter token, generate the corresponding GLSL string.
1122 * Also, return the actual register name and swizzle in case the
1123 * caller needs this information as well. */
1124 static DWORD
shader_glsl_add_dst_param(SHADER_OPCODE_ARG
* arg
, const DWORD param
,
1125 const DWORD addr_token
, glsl_dst_param_t
*dst_param
) {
1126 BOOL is_color
= FALSE
;
1128 dst_param
->mask_str
[0] = '\0';
1129 dst_param
->reg_name
[0] = '\0';
1131 shader_glsl_get_register_name(param
, addr_token
, dst_param
->reg_name
, &is_color
, arg
);
1132 return shader_glsl_get_write_mask(param
, dst_param
->mask_str
);
1135 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1136 static DWORD
shader_glsl_append_dst_ext(SHADER_BUFFER
*buffer
, SHADER_OPCODE_ARG
*arg
, const DWORD param
) {
1137 glsl_dst_param_t dst_param
;
1141 mask
= shader_glsl_add_dst_param(arg
, param
, arg
->dst_addr
, &dst_param
);
1144 shift
= (param
& WINED3DSP_DSTSHIFT_MASK
) >> WINED3DSP_DSTSHIFT_SHIFT
;
1145 shader_addline(buffer
, "%s%s = %s(", dst_param
.reg_name
, dst_param
.mask_str
, shift_glsl_tab
[shift
]);
1151 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1152 static DWORD
shader_glsl_append_dst(SHADER_BUFFER
*buffer
, SHADER_OPCODE_ARG
*arg
) {
1153 return shader_glsl_append_dst_ext(buffer
, arg
, arg
->dst
);
1156 /** Process GLSL instruction modifiers */
1157 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG
* arg
) {
1159 DWORD mask
= arg
->dst
& WINED3DSP_DSTMOD_MASK
;
1161 if (arg
->opcode
->dst_token
&& mask
!= 0) {
1162 glsl_dst_param_t dst_param
;
1164 shader_glsl_add_dst_param(arg
, arg
->dst
, 0, &dst_param
);
1166 if (mask
& WINED3DSPDM_SATURATE
) {
1167 /* _SAT means to clamp the value of the register to between 0 and 1 */
1168 shader_addline(arg
->buffer
, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param
.reg_name
,
1169 dst_param
.mask_str
, dst_param
.reg_name
, dst_param
.mask_str
);
1171 if (mask
& WINED3DSPDM_MSAMPCENTROID
) {
1172 FIXME("_centroid modifier not handled\n");
1174 if (mask
& WINED3DSPDM_PARTIALPRECISION
) {
1175 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1180 static inline const char* shader_get_comp_op(
1181 const DWORD opcode
) {
1183 DWORD op
= (opcode
& INST_CONTROLS_MASK
) >> INST_CONTROLS_SHIFT
;
1185 case COMPARISON_GT
: return ">";
1186 case COMPARISON_EQ
: return "==";
1187 case COMPARISON_GE
: return ">=";
1188 case COMPARISON_LT
: return "<";
1189 case COMPARISON_NE
: return "!=";
1190 case COMPARISON_LE
: return "<=";
1192 FIXME("Unrecognized comparison value: %u\n", op
);
1197 static void shader_glsl_get_sample_function(DWORD sampler_type
, BOOL projected
, BOOL texrect
, glsl_sample_function_t
*sample_function
) {
1198 /* Note that there's no such thing as a projected cube texture. */
1199 switch(sampler_type
) {
1201 sample_function
->name
= projected
? "texture1DProj" : "texture1D";
1202 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
;
1206 sample_function
->name
= projected
? "texture2DRectProj" : "texture2DRect";
1208 sample_function
->name
= projected
? "texture2DProj" : "texture2D";
1210 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
;
1212 case WINED3DSTT_CUBE
:
1213 sample_function
->name
= "textureCube";
1214 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1216 case WINED3DSTT_VOLUME
:
1217 sample_function
->name
= projected
? "texture3DProj" : "texture3D";
1218 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1221 sample_function
->name
= "";
1222 sample_function
->coord_mask
= 0;
1223 FIXME("Unrecognized sampler type: %#x;\n", sampler_type
);
1228 static void shader_glsl_color_correction(SHADER_OPCODE_ARG
* arg
) {
1229 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
1230 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) shader
->baseShader
.device
;
1231 WineD3D_GL_Info
*gl_info
= &deviceImpl
->adapter
->gl_info
;
1232 glsl_dst_param_t dst_param
;
1233 glsl_dst_param_t dst_param2
;
1235 WINED3DFORMAT conversion_group
;
1236 IWineD3DBaseTextureImpl
*texture
;
1237 DWORD mask
, mask_size
;
1239 BOOL recorded
= FALSE
;
1241 DWORD hex_version
= shader
->baseShader
.hex_version
;
1243 switch(arg
->opcode
->opcode
) {
1244 case WINED3DSIO_TEX
:
1245 if (hex_version
< WINED3DPS_VERSION(2,0)) {
1246 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1248 sampler_idx
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
1252 case WINED3DSIO_TEXLDL
:
1253 FIXME("Add color fixup for vertex texture WINED3DSIO_TEXLDL\n");
1256 case WINED3DSIO_TEXDP3TEX
:
1257 case WINED3DSIO_TEXM3x3TEX
:
1258 case WINED3DSIO_TEXM3x3SPEC
:
1259 case WINED3DSIO_TEXM3x3VSPEC
:
1260 case WINED3DSIO_TEXBEM
:
1261 case WINED3DSIO_TEXREG2AR
:
1262 case WINED3DSIO_TEXREG2GB
:
1263 case WINED3DSIO_TEXREG2RGB
:
1264 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1268 /* Not a texture sampling instruction, nothing to do */
1272 texture
= (IWineD3DBaseTextureImpl
*) deviceImpl
->stateBlock
->textures
[sampler_idx
];
1274 fmt
= texture
->resource
.format
;
1275 conversion_group
= texture
->baseTexture
.shader_conversion_group
;
1277 fmt
= WINED3DFMT_UNKNOWN
;
1278 conversion_group
= WINED3DFMT_UNKNOWN
;
1281 /* before doing anything, record the sampler with the format in the format conversion list,
1282 * but check if it's not there already
1284 for(i
= 0; i
< shader
->baseShader
.num_sampled_samplers
; i
++) {
1285 if(shader
->baseShader
.sampled_samplers
[i
] == sampler_idx
) {
1291 shader
->baseShader
.sampled_samplers
[shader
->baseShader
.num_sampled_samplers
] = sampler_idx
;
1292 shader
->baseShader
.num_sampled_samplers
++;
1293 shader
->baseShader
.sampled_format
[sampler_idx
] = conversion_group
;
1297 case WINED3DFMT_V8U8
:
1298 case WINED3DFMT_V16U16
:
1299 if(GL_SUPPORT(NV_TEXTURE_SHADER
) && fmt
== WINED3DFMT_V8U8
) {
1300 /* The 3rd channel returns 1.0 in d3d, but 0.0 in gl. Fix this while we're at it :-) */
1301 mask
= shader_glsl_add_dst_param(arg
, arg
->dst
, WINED3DSP_WRITEMASK_2
, &dst_param
);
1302 mask_size
= shader_glsl_get_write_mask_size(mask
);
1303 if(mask_size
>= 3) {
1304 shader_addline(arg
->buffer
, "%s.%c = 1.0;\n", dst_param
.reg_name
, dst_param
.mask_str
[3]);
1307 /* Correct the sign, but leave the blue as it is - it was loaded correctly already */
1308 mask
= shader_glsl_add_dst_param(arg
, arg
->dst
,
1309 WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
,
1311 mask_size
= shader_glsl_get_write_mask_size(mask
);
1312 if(mask_size
>= 2) {
1313 shader_addline(arg
->buffer
, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1314 dst_param
.reg_name
, dst_param
.mask_str
[1], dst_param
.mask_str
[2],
1315 dst_param
.reg_name
, dst_param
.mask_str
[1], dst_param
.mask_str
[2]);
1316 } else if(mask_size
== 1) {
1317 shader_addline(arg
->buffer
, "%s.%c = %s.%c * 2.0 - 1.0;\n", dst_param
.reg_name
, dst_param
.mask_str
[1],
1318 dst_param
.reg_name
, dst_param
.mask_str
[1]);
1323 case WINED3DFMT_X8L8V8U8
:
1324 if(!GL_SUPPORT(NV_TEXTURE_SHADER
)) {
1325 /* Red and blue are the signed channels, fix them up; Blue(=L) is correct already,
1326 * and a(X) is always 1.0
1328 mask
= shader_glsl_add_dst_param(arg
, arg
->dst
, WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &dst_param
);
1329 mask_size
= shader_glsl_get_write_mask_size(mask
);
1330 if(mask_size
>= 2) {
1331 shader_addline(arg
->buffer
, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1332 dst_param
.reg_name
, dst_param
.mask_str
[1], dst_param
.mask_str
[2],
1333 dst_param
.reg_name
, dst_param
.mask_str
[1], dst_param
.mask_str
[2]);
1334 } else if(mask_size
== 1) {
1335 shader_addline(arg
->buffer
, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1336 dst_param
.reg_name
, dst_param
.mask_str
[1],
1337 dst_param
.reg_name
, dst_param
.mask_str
[1]);
1342 case WINED3DFMT_L6V5U5
:
1343 if(!GL_SUPPORT(NV_TEXTURE_SHADER
)) {
1344 mask
= shader_glsl_add_dst_param(arg
, arg
->dst
, WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &dst_param
);
1345 mask_size
= shader_glsl_get_write_mask_size(mask
);
1346 shader_glsl_add_dst_param(arg
, arg
->dst
, WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_2
, &dst_param2
);
1347 if(mask_size
>= 3) {
1348 /* Swap y and z (U and L), and do a sign conversion on x and the new y(V and U) */
1349 shader_addline(arg
->buffer
, "tmp0.g = %s.%c;\n",
1350 dst_param
.reg_name
, dst_param
.mask_str
[2]);
1351 shader_addline(arg
->buffer
, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1352 dst_param
.reg_name
, dst_param
.mask_str
[2], dst_param
.mask_str
[1],
1353 dst_param2
.reg_name
, dst_param
.mask_str
[1], dst_param
.mask_str
[3]);
1354 shader_addline(arg
->buffer
, "%s.%c = tmp0.g;\n", dst_param
.reg_name
,
1355 dst_param
.mask_str
[3]);
1356 } else if(mask_size
== 2) {
1357 /* This is bad: We have VL, but we need VU */
1358 FIXME("2 components sampled from a converted L6V5U5 texture\n");
1360 shader_addline(arg
->buffer
, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1361 dst_param
.reg_name
, dst_param
.mask_str
[1],
1362 dst_param2
.reg_name
, dst_param
.mask_str
[1]);
1367 case WINED3DFMT_Q8W8V8U8
:
1368 if(!GL_SUPPORT(NV_TEXTURE_SHADER
)) {
1369 /* Correct the sign in all channels. The writemask just applies as-is, no
1370 * need for checking the mask size
1372 shader_glsl_add_dst_param(arg
, arg
->dst
,
1373 WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
|
1374 WINED3DSP_WRITEMASK_2
| WINED3DSP_WRITEMASK_3
,
1376 shader_addline(arg
->buffer
, "%s%s = %s%s * 2.0 - 1.0;\n", dst_param
.reg_name
, dst_param
.mask_str
,
1377 dst_param
.reg_name
, dst_param
.mask_str
);
1381 case WINED3DFMT_ATI2N
:
1382 /* GL_ATI_texture_compression_3dc returns the two channels as luminance-alpha,
1383 * which means the first one is replicated across .rgb, and the 2nd one is in
1384 * .a. We need the 2nd in .g
1386 * GL_EXT_texture_compression_rgtc returns the values in .rg, however, they
1387 * are swapped compared to d3d. So swap red and green.
1389 mask
= shader_glsl_add_dst_param(arg
, arg
->dst
, WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &dst_param
);
1390 mask_size
= shader_glsl_get_write_mask_size(mask
);
1391 if(GL_SUPPORT(EXT_TEXTURE_COMPRESSION_RGTC
)) {
1392 if(mask_size
>= 2) {
1393 shader_addline(arg
->buffer
, "%s.%c%c = %s.%c%c;\n",
1394 dst_param
.reg_name
, dst_param
.mask_str
[1],
1395 dst_param
.mask_str
[2],
1396 dst_param
.reg_name
, dst_param
.mask_str
[2],
1397 dst_param
.mask_str
[1]);
1399 FIXME("%u components sampled from a converted ATI2N texture\n", mask_size
);
1402 if(mask_size
== 4) {
1403 /* Swap y and z (U and L), and do a sign conversion on x and the new y(V and U) */
1404 shader_addline(arg
->buffer
, "%s.%c = %s.%c;\n",
1405 dst_param
.reg_name
, dst_param
.mask_str
[2],
1406 dst_param
.reg_name
, dst_param
.mask_str
[4]);
1407 } else if(mask_size
== 1) {
1410 FIXME("%u components sampled from a converted ATI2N texture\n", mask_size
);
1411 /* This is bad: We have .r[gb], but we need .ra */
1416 /* stupid compiler */
1422 /*****************************************************************************
1424 * Begin processing individual instruction opcodes
1426 ****************************************************************************/
1428 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
1429 static void shader_glsl_arith(SHADER_OPCODE_ARG
* arg
) {
1430 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
1431 SHADER_BUFFER
* buffer
= arg
->buffer
;
1432 glsl_src_param_t src0_param
;
1433 glsl_src_param_t src1_param
;
1437 /* Determine the GLSL operator to use based on the opcode */
1438 switch (curOpcode
->opcode
) {
1439 case WINED3DSIO_MUL
: op
= '*'; break;
1440 case WINED3DSIO_ADD
: op
= '+'; break;
1441 case WINED3DSIO_SUB
: op
= '-'; break;
1444 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode
->name
);
1448 write_mask
= shader_glsl_append_dst(buffer
, arg
);
1449 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1450 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1451 shader_addline(buffer
, "%s %c %s);\n", src0_param
.param_str
, op
, src1_param
.param_str
);
1454 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1455 static void shader_glsl_mov(SHADER_OPCODE_ARG
* arg
) {
1456 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
1457 SHADER_BUFFER
* buffer
= arg
->buffer
;
1458 glsl_src_param_t src0_param
;
1461 write_mask
= shader_glsl_append_dst(buffer
, arg
);
1462 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1464 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1465 * shader versions WINED3DSIO_MOVA is used for this. */
1466 if ((WINED3DSHADER_VERSION_MAJOR(shader
->baseShader
.hex_version
) == 1 &&
1467 !shader_is_pshader_version(shader
->baseShader
.hex_version
) &&
1468 shader_get_regtype(arg
->dst
) == WINED3DSPR_ADDR
)) {
1469 /* This is a simple floor() */
1470 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1471 if (mask_size
> 1) {
1472 shader_addline(buffer
, "ivec%d(floor(%s)));\n", mask_size
, src0_param
.param_str
);
1474 shader_addline(buffer
, "int(floor(%s)));\n", src0_param
.param_str
);
1476 } else if(arg
->opcode
->opcode
== WINED3DSIO_MOVA
) {
1477 /* We need to *round* to the nearest int here. */
1478 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1479 if (mask_size
> 1) {
1480 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
);
1482 shader_addline(buffer
, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param
.param_str
, src0_param
.param_str
);
1485 shader_addline(buffer
, "%s);\n", src0_param
.param_str
);
1489 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
1490 static void shader_glsl_dot(SHADER_OPCODE_ARG
* arg
) {
1491 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
1492 SHADER_BUFFER
* buffer
= arg
->buffer
;
1493 glsl_src_param_t src0_param
;
1494 glsl_src_param_t src1_param
;
1495 DWORD dst_write_mask
, src_write_mask
;
1496 unsigned int dst_size
= 0;
1498 dst_write_mask
= shader_glsl_append_dst(buffer
, arg
);
1499 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
1501 /* dp3 works on vec3, dp4 on vec4 */
1502 if (curOpcode
->opcode
== WINED3DSIO_DP4
) {
1503 src_write_mask
= WINED3DSP_WRITEMASK_ALL
;
1505 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1508 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_write_mask
, &src0_param
);
1509 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], src_write_mask
, &src1_param
);
1512 shader_addline(buffer
, "vec%d(dot(%s, %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
1514 shader_addline(buffer
, "dot(%s, %s));\n", src0_param
.param_str
, src1_param
.param_str
);
1518 /* Note that this instruction has some restrictions. The destination write mask
1519 * can't contain the w component, and the source swizzles have to be .xyzw */
1520 static void shader_glsl_cross(SHADER_OPCODE_ARG
*arg
) {
1521 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1522 glsl_src_param_t src0_param
;
1523 glsl_src_param_t src1_param
;
1526 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1527 shader_glsl_append_dst(arg
->buffer
, arg
);
1528 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
1529 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], src_mask
, &src1_param
);
1530 shader_addline(arg
->buffer
, "cross(%s, %s)%s);\n", src0_param
.param_str
, src1_param
.param_str
, dst_mask
);
1533 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1534 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1535 * GLSL uses the value as-is. */
1536 static void shader_glsl_pow(SHADER_OPCODE_ARG
*arg
) {
1537 SHADER_BUFFER
*buffer
= arg
->buffer
;
1538 glsl_src_param_t src0_param
;
1539 glsl_src_param_t src1_param
;
1540 DWORD dst_write_mask
;
1541 unsigned int dst_size
;
1543 dst_write_mask
= shader_glsl_append_dst(buffer
, arg
);
1544 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
1546 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1547 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
1550 shader_addline(buffer
, "vec%d(pow(abs(%s), %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
1552 shader_addline(buffer
, "pow(abs(%s), %s));\n", src0_param
.param_str
, src1_param
.param_str
);
1556 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
1557 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
1558 * GLSL uses the value as-is. */
1559 static void shader_glsl_log(SHADER_OPCODE_ARG
*arg
) {
1560 SHADER_BUFFER
*buffer
= arg
->buffer
;
1561 glsl_src_param_t src0_param
;
1562 DWORD dst_write_mask
;
1563 unsigned int dst_size
;
1565 dst_write_mask
= shader_glsl_append_dst(buffer
, arg
);
1566 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
1568 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1571 shader_addline(buffer
, "vec%d(log2(abs(%s))));\n", dst_size
, src0_param
.param_str
);
1573 shader_addline(buffer
, "log2(abs(%s)));\n", src0_param
.param_str
);
1577 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1578 static void shader_glsl_map2gl(SHADER_OPCODE_ARG
* arg
) {
1579 CONST SHADER_OPCODE
* curOpcode
= arg
->opcode
;
1580 SHADER_BUFFER
* buffer
= arg
->buffer
;
1581 glsl_src_param_t src_param
;
1582 const char *instruction
;
1583 char arguments
[256];
1587 /* Determine the GLSL function to use based on the opcode */
1588 /* TODO: Possibly make this a table for faster lookups */
1589 switch (curOpcode
->opcode
) {
1590 case WINED3DSIO_MIN
: instruction
= "min"; break;
1591 case WINED3DSIO_MAX
: instruction
= "max"; break;
1592 case WINED3DSIO_ABS
: instruction
= "abs"; break;
1593 case WINED3DSIO_FRC
: instruction
= "fract"; break;
1594 case WINED3DSIO_NRM
: instruction
= "normalize"; break;
1595 case WINED3DSIO_EXP
: instruction
= "exp2"; break;
1596 case WINED3DSIO_SGN
: instruction
= "sign"; break;
1597 case WINED3DSIO_DSX
: instruction
= "dFdx"; break;
1598 case WINED3DSIO_DSY
: instruction
= "ycorrection.y * dFdy"; break;
1599 default: instruction
= "";
1600 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode
->name
);
1604 write_mask
= shader_glsl_append_dst(buffer
, arg
);
1606 arguments
[0] = '\0';
1607 if (curOpcode
->num_params
> 0) {
1608 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src_param
);
1609 strcat(arguments
, src_param
.param_str
);
1610 for (i
= 2; i
< curOpcode
->num_params
; ++i
) {
1611 strcat(arguments
, ", ");
1612 shader_glsl_add_src_param(arg
, arg
->src
[i
-1], arg
->src_addr
[i
-1], write_mask
, &src_param
);
1613 strcat(arguments
, src_param
.param_str
);
1617 shader_addline(buffer
, "%s(%s));\n", instruction
, arguments
);
1620 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1621 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1622 * dst.x = 2^(floor(src))
1623 * dst.y = src - floor(src)
1624 * dst.z = 2^src (partial precision is allowed, but optional)
1626 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1627 * dst = 2^src; (partial precision is allowed, but optional)
1629 static void shader_glsl_expp(SHADER_OPCODE_ARG
* arg
) {
1630 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)arg
->shader
;
1631 glsl_src_param_t src_param
;
1633 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
1635 if (shader
->baseShader
.hex_version
< WINED3DPS_VERSION(2,0)) {
1638 shader_addline(arg
->buffer
, "tmp0.x = exp2(floor(%s));\n", src_param
.param_str
);
1639 shader_addline(arg
->buffer
, "tmp0.y = %s - floor(%s);\n", src_param
.param_str
, src_param
.param_str
);
1640 shader_addline(arg
->buffer
, "tmp0.z = exp2(%s);\n", src_param
.param_str
);
1641 shader_addline(arg
->buffer
, "tmp0.w = 1.0;\n");
1643 shader_glsl_append_dst(arg
->buffer
, arg
);
1644 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1645 shader_addline(arg
->buffer
, "tmp0%s);\n", dst_mask
);
1648 unsigned int mask_size
;
1650 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1651 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1653 if (mask_size
> 1) {
1654 shader_addline(arg
->buffer
, "vec%d(exp2(%s)));\n", mask_size
, src_param
.param_str
);
1656 shader_addline(arg
->buffer
, "exp2(%s));\n", src_param
.param_str
);
1661 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1662 static void shader_glsl_rcp(SHADER_OPCODE_ARG
* arg
) {
1663 glsl_src_param_t src_param
;
1665 unsigned int mask_size
;
1667 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1668 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1669 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
1671 if (mask_size
> 1) {
1672 shader_addline(arg
->buffer
, "vec%d(1.0 / %s));\n", mask_size
, src_param
.param_str
);
1674 shader_addline(arg
->buffer
, "1.0 / %s);\n", src_param
.param_str
);
1678 static void shader_glsl_rsq(SHADER_OPCODE_ARG
* arg
) {
1679 SHADER_BUFFER
* buffer
= arg
->buffer
;
1680 glsl_src_param_t src_param
;
1682 unsigned int mask_size
;
1684 write_mask
= shader_glsl_append_dst(buffer
, arg
);
1685 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1687 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
1689 if (mask_size
> 1) {
1690 shader_addline(buffer
, "vec%d(inversesqrt(%s)));\n", mask_size
, src_param
.param_str
);
1692 shader_addline(buffer
, "inversesqrt(%s));\n", src_param
.param_str
);
1696 /** Process signed comparison opcodes in GLSL. */
1697 static void shader_glsl_compare(SHADER_OPCODE_ARG
* arg
) {
1698 glsl_src_param_t src0_param
;
1699 glsl_src_param_t src1_param
;
1701 unsigned int mask_size
;
1703 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1704 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1705 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1706 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1708 if (mask_size
> 1) {
1709 const char *compare
;
1711 switch(arg
->opcode
->opcode
) {
1712 case WINED3DSIO_SLT
: compare
= "lessThan"; break;
1713 case WINED3DSIO_SGE
: compare
= "greaterThanEqual"; break;
1714 default: compare
= "";
1715 FIXME("Can't handle opcode %s\n", arg
->opcode
->name
);
1718 shader_addline(arg
->buffer
, "vec%d(%s(%s, %s)));\n", mask_size
, compare
,
1719 src0_param
.param_str
, src1_param
.param_str
);
1721 switch(arg
->opcode
->opcode
) {
1722 case WINED3DSIO_SLT
:
1723 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
1724 * to return 0.0 but step returns 1.0 because step is not < x
1725 * An alternative is a bvec compare padded with an unused second component.
1726 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
1727 * issue. Playing with not() is not possible either because not() does not accept
1730 shader_addline(arg
->buffer
, "(%s < %s) ? 1.0 : 0.0);\n", src0_param
.param_str
, src1_param
.param_str
);
1732 case WINED3DSIO_SGE
:
1733 /* Here we can use the step() function and safe a conditional */
1734 shader_addline(arg
->buffer
, "step(%s, %s));\n", src1_param
.param_str
, src0_param
.param_str
);
1737 FIXME("Can't handle opcode %s\n", arg
->opcode
->name
);
1743 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1744 static void shader_glsl_cmp(SHADER_OPCODE_ARG
* arg
) {
1745 glsl_src_param_t src0_param
;
1746 glsl_src_param_t src1_param
;
1747 glsl_src_param_t src2_param
;
1748 DWORD write_mask
, cmp_channel
= 0;
1751 BOOL temp_destination
= FALSE
;
1753 if(shader_is_scalar(arg
->src
[0])) {
1754 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1756 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
1757 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1758 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1760 shader_addline(arg
->buffer
, "%s >= 0.0 ? %s : %s);\n",
1761 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1763 DWORD src0reg
= arg
->src
[0] & WINED3DSP_REGNUM_MASK
;
1764 DWORD src1reg
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
1765 DWORD src2reg
= arg
->src
[2] & WINED3DSP_REGNUM_MASK
;
1766 DWORD src0regtype
= shader_get_regtype(arg
->src
[0]);
1767 DWORD src1regtype
= shader_get_regtype(arg
->src
[1]);
1768 DWORD src2regtype
= shader_get_regtype(arg
->src
[2]);
1769 DWORD dstreg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
1770 DWORD dstregtype
= shader_get_regtype(arg
->dst
);
1772 /* Cycle through all source0 channels */
1773 for (i
=0; i
<4; i
++) {
1775 /* Find the destination channels which use the current source0 channel */
1776 for (j
=0; j
<4; j
++) {
1777 if ( ((arg
->src
[0] >> (WINED3DSP_SWIZZLE_SHIFT
+ 2*j
)) & 0x3) == i
) {
1778 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
1779 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
1783 /* Splitting the cmp instruction up in multiple lines imposes a problem:
1784 * The first lines may overwrite source parameters of the following lines.
1785 * Deal with that by using a temporary destination register if needed
1787 if((src0reg
== dstreg
&& src0regtype
== dstregtype
) ||
1788 (src1reg
== dstreg
&& src1regtype
== dstregtype
) ||
1789 (src2reg
== dstreg
&& src2regtype
== dstregtype
)) {
1791 write_mask
= shader_glsl_get_write_mask(arg
->dst
& (~WINED3DSP_SWIZZLE_MASK
| write_mask
), mask_char
);
1792 if (!write_mask
) continue;
1793 shader_addline(arg
->buffer
, "tmp0%s = (", mask_char
);
1794 temp_destination
= TRUE
;
1796 write_mask
= shader_glsl_append_dst_ext(arg
->buffer
, arg
, arg
->dst
& (~WINED3DSP_SWIZZLE_MASK
| write_mask
));
1797 if (!write_mask
) continue;
1800 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], cmp_channel
, &src0_param
);
1801 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1802 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1804 shader_addline(arg
->buffer
, "%s >= 0.0 ? %s : %s);\n",
1805 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1808 if(temp_destination
) {
1809 shader_glsl_get_write_mask(arg
->dst
, mask_char
);
1810 shader_glsl_append_dst_ext(arg
->buffer
, arg
, arg
->dst
);
1811 shader_addline(arg
->buffer
, "tmp0%s);\n", mask_char
);
1817 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
1818 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1819 * the compare is done per component of src0. */
1820 static void shader_glsl_cnd(SHADER_OPCODE_ARG
* arg
) {
1821 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
1822 glsl_src_param_t src0_param
;
1823 glsl_src_param_t src1_param
;
1824 glsl_src_param_t src2_param
;
1825 DWORD write_mask
, cmp_channel
= 0;
1828 if (shader
->baseShader
.hex_version
< WINED3DPS_VERSION(1, 4)) {
1829 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1830 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1831 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1832 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1834 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
1835 if(arg
->opcode_token
& WINED3DSI_COISSUE
) {
1836 shader_addline(arg
->buffer
, "%s /* COISSUE! */);\n", src1_param
.param_str
);
1838 shader_addline(arg
->buffer
, "%s > 0.5 ? %s : %s);\n",
1839 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1843 /* Cycle through all source0 channels */
1844 for (i
=0; i
<4; i
++) {
1846 /* Find the destination channels which use the current source0 channel */
1847 for (j
=0; j
<4; j
++) {
1848 if ( ((arg
->src
[0] >> (WINED3DSP_SWIZZLE_SHIFT
+ 2*j
)) & 0x3) == i
) {
1849 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
1850 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
1853 write_mask
= shader_glsl_append_dst_ext(arg
->buffer
, arg
, arg
->dst
& (~WINED3DSP_SWIZZLE_MASK
| write_mask
));
1854 if (!write_mask
) continue;
1856 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], cmp_channel
, &src0_param
);
1857 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1858 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1860 shader_addline(arg
->buffer
, "%s > 0.5 ? %s : %s);\n",
1861 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1865 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1866 static void shader_glsl_mad(SHADER_OPCODE_ARG
* arg
) {
1867 glsl_src_param_t src0_param
;
1868 glsl_src_param_t src1_param
;
1869 glsl_src_param_t src2_param
;
1872 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1873 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1874 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1875 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1876 shader_addline(arg
->buffer
, "(%s * %s) + %s);\n",
1877 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
1880 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1881 Vertex shaders to GLSL codes */
1882 static void shader_glsl_mnxn(SHADER_OPCODE_ARG
* arg
) {
1884 int nComponents
= 0;
1885 SHADER_OPCODE_ARG tmpArg
;
1887 memset(&tmpArg
, 0, sizeof(SHADER_OPCODE_ARG
));
1889 /* Set constants for the temporary argument */
1890 tmpArg
.shader
= arg
->shader
;
1891 tmpArg
.buffer
= arg
->buffer
;
1892 tmpArg
.src
[0] = arg
->src
[0];
1893 tmpArg
.src_addr
[0] = arg
->src_addr
[0];
1894 tmpArg
.src_addr
[1] = arg
->src_addr
[1];
1895 tmpArg
.reg_maps
= arg
->reg_maps
;
1897 switch(arg
->opcode
->opcode
) {
1898 case WINED3DSIO_M4x4
:
1900 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP4
);
1902 case WINED3DSIO_M4x3
:
1904 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP4
);
1906 case WINED3DSIO_M3x4
:
1908 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1910 case WINED3DSIO_M3x3
:
1912 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1914 case WINED3DSIO_M3x2
:
1916 tmpArg
.opcode
= shader_get_opcode(arg
->shader
, WINED3DSIO_DP3
);
1922 for (i
= 0; i
< nComponents
; i
++) {
1923 tmpArg
.dst
= ((arg
->dst
) & ~WINED3DSP_WRITEMASK_ALL
)|(WINED3DSP_WRITEMASK_0
<<i
);
1924 tmpArg
.src
[1] = arg
->src
[1]+i
;
1925 shader_glsl_dot(&tmpArg
);
1930 The LRP instruction performs a component-wise linear interpolation
1931 between the second and third operands using the first operand as the
1932 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
1933 This is equivalent to mix(src2, src1, src0);
1935 static void shader_glsl_lrp(SHADER_OPCODE_ARG
* arg
) {
1936 glsl_src_param_t src0_param
;
1937 glsl_src_param_t src1_param
;
1938 glsl_src_param_t src2_param
;
1941 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
1943 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], write_mask
, &src0_param
);
1944 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], write_mask
, &src1_param
);
1945 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], write_mask
, &src2_param
);
1947 shader_addline(arg
->buffer
, "mix(%s, %s, %s));\n",
1948 src2_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
);
1951 /** Process the WINED3DSIO_LIT instruction in GLSL:
1952 * dst.x = dst.w = 1.0
1953 * dst.y = (src0.x > 0) ? src0.x
1954 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1955 * where src.w is clamped at +- 128
1957 static void shader_glsl_lit(SHADER_OPCODE_ARG
* arg
) {
1958 glsl_src_param_t src0_param
;
1959 glsl_src_param_t src1_param
;
1960 glsl_src_param_t src3_param
;
1963 shader_glsl_append_dst(arg
->buffer
, arg
);
1964 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
1966 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1967 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_1
, &src1_param
);
1968 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &src3_param
);
1970 /* The sdk specifies the instruction like this
1972 * if(src.x > 0.0) dst.y = src.x
1974 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
1978 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
1979 * dst.x = 1.0 ... No further explanation needed
1980 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
1981 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
1982 * dst.w = 1.0. ... Nothing fancy.
1984 * So we still have one conditional in there. So do this:
1985 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
1987 * 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),
1988 * 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.
1989 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
1991 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",
1992 src0_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
, src3_param
.param_str
, dst_mask
);
1995 /** Process the WINED3DSIO_DST instruction in GLSL:
1997 * dst.y = src0.x * src0.y
2001 static void shader_glsl_dst(SHADER_OPCODE_ARG
* arg
) {
2002 glsl_src_param_t src0y_param
;
2003 glsl_src_param_t src0z_param
;
2004 glsl_src_param_t src1y_param
;
2005 glsl_src_param_t src1w_param
;
2008 shader_glsl_append_dst(arg
->buffer
, arg
);
2009 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2011 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_1
, &src0y_param
);
2012 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_2
, &src0z_param
);
2013 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_1
, &src1y_param
);
2014 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_3
, &src1w_param
);
2016 shader_addline(arg
->buffer
, "vec4(1.0, %s * %s, %s, %s))%s;\n",
2017 src0y_param
.param_str
, src1y_param
.param_str
, src0z_param
.param_str
, src1w_param
.param_str
, dst_mask
);
2020 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
2021 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
2022 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
2024 * dst.x = cos(src0.?)
2025 * dst.y = sin(src0.?)
2029 static void shader_glsl_sincos(SHADER_OPCODE_ARG
* arg
) {
2030 glsl_src_param_t src0_param
;
2033 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
2034 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2036 switch (write_mask
) {
2037 case WINED3DSP_WRITEMASK_0
:
2038 shader_addline(arg
->buffer
, "cos(%s));\n", src0_param
.param_str
);
2041 case WINED3DSP_WRITEMASK_1
:
2042 shader_addline(arg
->buffer
, "sin(%s));\n", src0_param
.param_str
);
2045 case (WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
):
2046 shader_addline(arg
->buffer
, "vec2(cos(%s), sin(%s)));\n", src0_param
.param_str
, src0_param
.param_str
);
2050 ERR("Write mask should be .x, .y or .xy\n");
2055 /** Process the WINED3DSIO_LOOP instruction in GLSL:
2056 * Start a for() loop where src1.y is the initial value of aL,
2057 * increment aL by src1.z for a total of src1.x iterations.
2058 * Need to use a temporary variable for this operation.
2060 /* FIXME: I don't think nested loops will work correctly this way. */
2061 static void shader_glsl_loop(SHADER_OPCODE_ARG
* arg
) {
2062 glsl_src_param_t src1_param
;
2063 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
2064 DWORD regtype
= shader_get_regtype(arg
->src
[1]);
2065 DWORD reg
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
2066 const DWORD
*control_values
= NULL
;
2067 local_constant
*constant
;
2069 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_ALL
, &src1_param
);
2071 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
2072 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
2073 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
2076 if(regtype
== WINED3DSPR_CONSTINT
) {
2077 LIST_FOR_EACH_ENTRY(constant
, &shader
->baseShader
.constantsI
, local_constant
, entry
) {
2078 if(constant
->idx
== reg
) {
2079 control_values
= constant
->value
;
2085 if(control_values
) {
2086 if(control_values
[2] > 0) {
2087 shader_addline(arg
->buffer
, "for (aL%u = %d; aL%u < (%d * %d + %d); aL%u += %d) {\n",
2088 shader
->baseShader
.cur_loop_depth
, control_values
[1],
2089 shader
->baseShader
.cur_loop_depth
, control_values
[0], control_values
[2], control_values
[1],
2090 shader
->baseShader
.cur_loop_depth
, control_values
[2]);
2091 } else if(control_values
[2] == 0) {
2092 shader_addline(arg
->buffer
, "for (aL%u = %d, tmpInt%u = 0; tmpInt%u < %d; tmpInt%u++) {\n",
2093 shader
->baseShader
.cur_loop_depth
, control_values
[1], shader
->baseShader
.cur_loop_depth
,
2094 shader
->baseShader
.cur_loop_depth
, control_values
[0],
2095 shader
->baseShader
.cur_loop_depth
);
2097 shader_addline(arg
->buffer
, "for (aL%u = %d; aL%u > (%d * %d + %d); aL%u += %d) {\n",
2098 shader
->baseShader
.cur_loop_depth
, control_values
[1],
2099 shader
->baseShader
.cur_loop_depth
, control_values
[0], control_values
[2], control_values
[1],
2100 shader
->baseShader
.cur_loop_depth
, control_values
[2]);
2103 shader_addline(arg
->buffer
, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
2104 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_regno
,
2105 src1_param
.reg_name
, shader
->baseShader
.cur_loop_depth
, src1_param
.reg_name
,
2106 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_regno
, src1_param
.reg_name
);
2109 shader
->baseShader
.cur_loop_depth
++;
2110 shader
->baseShader
.cur_loop_regno
++;
2113 static void shader_glsl_end(SHADER_OPCODE_ARG
* arg
) {
2114 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
2116 shader_addline(arg
->buffer
, "}\n");
2118 if(arg
->opcode
->opcode
== WINED3DSIO_ENDLOOP
) {
2119 shader
->baseShader
.cur_loop_depth
--;
2120 shader
->baseShader
.cur_loop_regno
--;
2122 if(arg
->opcode
->opcode
== WINED3DSIO_ENDREP
) {
2123 shader
->baseShader
.cur_loop_depth
--;
2127 static void shader_glsl_rep(SHADER_OPCODE_ARG
* arg
) {
2128 IWineD3DBaseShaderImpl
* shader
= (IWineD3DBaseShaderImpl
*) arg
->shader
;
2129 glsl_src_param_t src0_param
;
2131 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2132 shader_addline(arg
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2133 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_depth
,
2134 src0_param
.param_str
, shader
->baseShader
.cur_loop_depth
);
2135 shader
->baseShader
.cur_loop_depth
++;
2138 static void shader_glsl_if(SHADER_OPCODE_ARG
* arg
) {
2139 glsl_src_param_t src0_param
;
2141 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2142 shader_addline(arg
->buffer
, "if (%s) {\n", src0_param
.param_str
);
2145 static void shader_glsl_ifc(SHADER_OPCODE_ARG
* arg
) {
2146 glsl_src_param_t src0_param
;
2147 glsl_src_param_t src1_param
;
2149 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2150 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2152 shader_addline(arg
->buffer
, "if (%s %s %s) {\n",
2153 src0_param
.param_str
, shader_get_comp_op(arg
->opcode_token
), src1_param
.param_str
);
2156 static void shader_glsl_else(SHADER_OPCODE_ARG
* arg
) {
2157 shader_addline(arg
->buffer
, "} else {\n");
2160 static void shader_glsl_break(SHADER_OPCODE_ARG
* arg
) {
2161 shader_addline(arg
->buffer
, "break;\n");
2164 /* FIXME: According to MSDN the compare is done per component. */
2165 static void shader_glsl_breakc(SHADER_OPCODE_ARG
* arg
) {
2166 glsl_src_param_t src0_param
;
2167 glsl_src_param_t src1_param
;
2169 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2170 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2172 shader_addline(arg
->buffer
, "if (%s %s %s) break;\n",
2173 src0_param
.param_str
, shader_get_comp_op(arg
->opcode_token
), src1_param
.param_str
);
2176 static void shader_glsl_label(SHADER_OPCODE_ARG
* arg
) {
2178 DWORD snum
= (arg
->src
[0]) & WINED3DSP_REGNUM_MASK
;
2179 shader_addline(arg
->buffer
, "}\n");
2180 shader_addline(arg
->buffer
, "void subroutine%u () {\n", snum
);
2183 static void shader_glsl_call(SHADER_OPCODE_ARG
* arg
) {
2184 DWORD snum
= (arg
->src
[0]) & WINED3DSP_REGNUM_MASK
;
2185 shader_addline(arg
->buffer
, "subroutine%u();\n", snum
);
2188 static void shader_glsl_callnz(SHADER_OPCODE_ARG
* arg
) {
2189 glsl_src_param_t src1_param
;
2191 DWORD snum
= (arg
->src
[0]) & WINED3DSP_REGNUM_MASK
;
2192 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2193 shader_addline(arg
->buffer
, "if (%s) subroutine%u();\n", src1_param
.param_str
, snum
);
2196 /*********************************************
2197 * Pixel Shader Specific Code begins here
2198 ********************************************/
2199 static void pshader_glsl_tex(SHADER_OPCODE_ARG
* arg
) {
2200 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2201 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
2202 DWORD hex_version
= This
->baseShader
.hex_version
;
2203 char dst_swizzle
[6];
2204 glsl_sample_function_t sample_function
;
2207 BOOL projected
, texrect
= FALSE
;
2210 /* All versions have a destination register */
2211 shader_glsl_append_dst(arg
->buffer
, arg
);
2213 /* 1.0-1.4: Use destination register as sampler source.
2214 * 2.0+: Use provided sampler source. */
2215 if (hex_version
< WINED3DPS_VERSION(2,0)) {
2216 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2218 sampler_idx
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
2220 sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
2222 if (hex_version
< WINED3DPS_VERSION(1,4)) {
2223 DWORD flags
= deviceImpl
->stateBlock
->textureState
[sampler_idx
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
2225 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
2226 if (flags
& WINED3DTTFF_PROJECTED
&& sampler_type
!= WINED3DSTT_CUBE
) {
2228 switch (flags
& ~WINED3DTTFF_PROJECTED
) {
2229 case WINED3DTTFF_COUNT1
: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2230 case WINED3DTTFF_COUNT2
: mask
= WINED3DSP_WRITEMASK_1
; break;
2231 case WINED3DTTFF_COUNT3
: mask
= WINED3DSP_WRITEMASK_2
; break;
2232 case WINED3DTTFF_COUNT4
:
2233 case WINED3DTTFF_DISABLE
: mask
= WINED3DSP_WRITEMASK_3
; break;
2238 } else if (hex_version
< WINED3DPS_VERSION(2,0)) {
2239 DWORD src_mod
= arg
->src
[0] & WINED3DSP_SRCMOD_MASK
;
2241 if (src_mod
== WINED3DSPSM_DZ
) {
2243 mask
= WINED3DSP_WRITEMASK_2
;
2244 } else if (src_mod
== WINED3DSPSM_DW
) {
2246 mask
= WINED3DSP_WRITEMASK_3
;
2251 if(arg
->opcode_token
& WINED3DSI_TEXLD_PROJECT
) {
2252 /* ps 2.0 texldp instruction always divides by the fourth component. */
2254 mask
= WINED3DSP_WRITEMASK_3
;
2260 if(deviceImpl
->stateBlock
->textures
[sampler_idx
] &&
2261 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl
->stateBlock
->textures
[sampler_idx
]) == GL_TEXTURE_RECTANGLE_ARB
) {
2265 shader_glsl_get_sample_function(sampler_type
, projected
, texrect
, &sample_function
);
2266 mask
|= sample_function
.coord_mask
;
2268 if (hex_version
< WINED3DPS_VERSION(2,0)) {
2269 shader_glsl_get_write_mask(arg
->dst
, dst_swizzle
);
2271 shader_glsl_get_swizzle(arg
->src
[1], FALSE
, arg
->dst
, dst_swizzle
);
2274 /* 1.0-1.3: Use destination register as coordinate source.
2275 1.4+: Use provided coordinate source register. */
2276 if (hex_version
< WINED3DPS_VERSION(1,4)) {
2278 shader_glsl_get_write_mask(mask
, coord_mask
);
2279 shader_addline(arg
->buffer
, "%s(Psampler%u, T%u%s)%s);\n",
2280 sample_function
.name
, sampler_idx
, sampler_idx
, coord_mask
, dst_swizzle
);
2282 glsl_src_param_t coord_param
;
2283 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], mask
, &coord_param
);
2284 if(arg
->opcode_token
& WINED3DSI_TEXLD_BIAS
) {
2285 glsl_src_param_t bias
;
2286 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &bias
);
2288 shader_addline(arg
->buffer
, "%s(Psampler%u, %s, %s)%s);\n",
2289 sample_function
.name
, sampler_idx
, coord_param
.param_str
,
2290 bias
.param_str
, dst_swizzle
);
2292 shader_addline(arg
->buffer
, "%s(Psampler%u, %s)%s);\n",
2293 sample_function
.name
, sampler_idx
, coord_param
.param_str
, dst_swizzle
);
2298 static void shader_glsl_texldl(SHADER_OPCODE_ARG
* arg
) {
2299 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*)arg
->shader
;
2300 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
2301 glsl_sample_function_t sample_function
;
2302 glsl_src_param_t coord_param
, lod_param
;
2303 char dst_swizzle
[6];
2306 BOOL texrect
= FALSE
;
2308 shader_glsl_append_dst(arg
->buffer
, arg
);
2309 shader_glsl_get_swizzle(arg
->src
[1], FALSE
, arg
->dst
, dst_swizzle
);
2311 sampler_idx
= arg
->src
[1] & WINED3DSP_REGNUM_MASK
;
2312 sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
2313 if(deviceImpl
->stateBlock
->textures
[sampler_idx
] &&
2314 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl
->stateBlock
->textures
[sampler_idx
]) == GL_TEXTURE_RECTANGLE_ARB
) {
2317 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
);
2319 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
2321 if (shader_is_pshader_version(This
->baseShader
.hex_version
)) {
2322 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
2323 * However, they seem to work just fine in fragment shaders as well. */
2324 WARN("Using %sLod in fragment shader.\n", sample_function
.name
);
2325 shader_addline(arg
->buffer
, "%sLod(Psampler%u, %s, %s)%s);\n",
2326 sample_function
.name
, sampler_idx
, coord_param
.param_str
, lod_param
.param_str
, dst_swizzle
);
2328 shader_addline(arg
->buffer
, "%sLod(Vsampler%u, %s, %s)%s);\n",
2329 sample_function
.name
, sampler_idx
, coord_param
.param_str
, lod_param
.param_str
, dst_swizzle
);
2333 static void pshader_glsl_texcoord(SHADER_OPCODE_ARG
* arg
) {
2335 /* FIXME: Make this work for more than just 2D textures */
2337 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2338 SHADER_BUFFER
* buffer
= arg
->buffer
;
2339 DWORD hex_version
= This
->baseShader
.hex_version
;
2343 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
2344 shader_glsl_get_write_mask(write_mask
, dst_mask
);
2346 if (hex_version
!= WINED3DPS_VERSION(1,4)) {
2347 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2348 shader_addline(buffer
, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n", reg
, dst_mask
);
2350 DWORD reg
= arg
->src
[0] & WINED3DSP_REGNUM_MASK
;
2351 DWORD src_mod
= arg
->src
[0] & WINED3DSP_SRCMOD_MASK
;
2352 char dst_swizzle
[6];
2354 shader_glsl_get_swizzle(arg
->src
[0], FALSE
, write_mask
, dst_swizzle
);
2356 if (src_mod
== WINED3DSPSM_DZ
) {
2357 glsl_src_param_t div_param
;
2358 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2359 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_2
, &div_param
);
2361 if (mask_size
> 1) {
2362 shader_addline(buffer
, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
2364 shader_addline(buffer
, "gl_TexCoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
2366 } else if (src_mod
== WINED3DSPSM_DW
) {
2367 glsl_src_param_t div_param
;
2368 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2369 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_3
, &div_param
);
2371 if (mask_size
> 1) {
2372 shader_addline(buffer
, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
2374 shader_addline(buffer
, "gl_TexCoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
2377 shader_addline(buffer
, "gl_TexCoord[%u]%s);\n", reg
, dst_swizzle
);
2382 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
2383 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2384 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2385 static void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG
* arg
) {
2386 glsl_src_param_t src0_param
;
2388 glsl_sample_function_t sample_function
;
2389 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2390 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2391 DWORD sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
2393 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2395 shader_glsl_append_dst(arg
->buffer
, arg
);
2396 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2398 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
2399 * scalar, and projected sampling would require 4.
2401 * It is a dependent read - not valid with conditional NP2 textures
2403 shader_glsl_get_sample_function(sampler_type
, FALSE
, FALSE
, &sample_function
);
2405 switch(count_bits(sample_function
.coord_mask
)) {
2407 shader_addline(arg
->buffer
, "%s(Psampler%u, dot(gl_TexCoord[%u].xyz, %s))%s);\n",
2408 sample_function
.name
, sampler_idx
, sampler_idx
, src0_param
.param_str
, dst_mask
);
2412 shader_addline(arg
->buffer
, "%s(Psampler%u, vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0))%s);\n",
2413 sample_function
.name
, sampler_idx
, sampler_idx
, src0_param
.param_str
, dst_mask
);
2417 shader_addline(arg
->buffer
, "%s(Psampler%u, vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0))%s);\n",
2418 sample_function
.name
, sampler_idx
, sampler_idx
, src0_param
.param_str
, dst_mask
);
2421 FIXME("Unexpected mask bitcount %d\n", count_bits(sample_function
.coord_mask
));
2425 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2426 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2427 static void pshader_glsl_texdp3(SHADER_OPCODE_ARG
* arg
) {
2428 glsl_src_param_t src0_param
;
2429 DWORD dstreg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2430 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2432 unsigned int mask_size
;
2434 dst_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
2435 mask_size
= shader_glsl_get_write_mask_size(dst_mask
);
2436 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2438 if (mask_size
> 1) {
2439 shader_addline(arg
->buffer
, "vec%d(dot(T%u.xyz, %s)));\n", mask_size
, dstreg
, src0_param
.param_str
);
2441 shader_addline(arg
->buffer
, "dot(T%u.xyz, %s));\n", dstreg
, src0_param
.param_str
);
2445 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2446 * Calculate the depth as dst.x / dst.y */
2447 static void pshader_glsl_texdepth(SHADER_OPCODE_ARG
* arg
) {
2448 glsl_dst_param_t dst_param
;
2450 shader_glsl_add_dst_param(arg
, arg
->dst
, 0, &dst_param
);
2452 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2453 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2454 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2455 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2458 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
);
2461 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2462 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2463 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2464 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2466 static void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG
* arg
) {
2467 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2468 DWORD dstreg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2469 glsl_src_param_t src0_param
;
2471 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2473 shader_addline(arg
->buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg
, src0_param
.param_str
);
2474 shader_addline(arg
->buffer
, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2477 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2478 * Calculate the 1st of a 2-row matrix multiplication. */
2479 static void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG
* arg
) {
2480 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2481 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2482 SHADER_BUFFER
* buffer
= arg
->buffer
;
2483 glsl_src_param_t src0_param
;
2485 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2486 shader_addline(buffer
, "tmp0.x = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2489 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2490 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2491 static void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG
* arg
) {
2493 IWineD3DPixelShaderImpl
* shader
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2494 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2495 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2496 SHADER_BUFFER
* buffer
= arg
->buffer
;
2497 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
2498 glsl_src_param_t src0_param
;
2500 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2501 shader_addline(buffer
, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state
->current_row
, reg
, src0_param
.param_str
);
2502 current_state
->texcoord_w
[current_state
->current_row
++] = reg
;
2505 static void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG
* arg
) {
2506 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2507 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2508 SHADER_BUFFER
* buffer
= arg
->buffer
;
2509 glsl_src_param_t src0_param
;
2512 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2513 shader_addline(buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2515 shader_glsl_append_dst(buffer
, arg
);
2516 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2518 /* Sample the texture using the calculated coordinates */
2519 shader_addline(buffer
, "texture2D(Psampler%u, tmp0.xy)%s);\n", reg
, dst_mask
);
2522 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2523 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2524 static void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG
* arg
) {
2525 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2526 glsl_src_param_t src0_param
;
2528 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2529 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2530 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
2531 DWORD sampler_type
= arg
->reg_maps
->samplers
[reg
] & WINED3DSP_TEXTURETYPE_MASK
;
2532 glsl_sample_function_t sample_function
;
2534 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2535 shader_addline(arg
->buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2537 shader_glsl_append_dst(arg
->buffer
, arg
);
2538 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2539 /* Dependent read, not valid with conditional NP2 */
2540 shader_glsl_get_sample_function(sampler_type
, FALSE
, FALSE
, &sample_function
);
2542 /* Sample the texture using the calculated coordinates */
2543 shader_addline(arg
->buffer
, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function
.name
, reg
, dst_mask
);
2545 current_state
->current_row
= 0;
2548 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2549 * Perform the 3rd row of a 3x3 matrix multiply */
2550 static void pshader_glsl_texm3x3(SHADER_OPCODE_ARG
* arg
) {
2551 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2552 glsl_src_param_t src0_param
;
2554 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2555 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2556 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
2558 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2560 shader_glsl_append_dst(arg
->buffer
, arg
);
2561 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2562 shader_addline(arg
->buffer
, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg
, src0_param
.param_str
, dst_mask
);
2564 current_state
->current_row
= 0;
2567 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
2568 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2569 static void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG
* arg
) {
2571 IWineD3DPixelShaderImpl
* shader
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2572 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2573 glsl_src_param_t src0_param
;
2574 glsl_src_param_t src1_param
;
2576 SHADER_BUFFER
* buffer
= arg
->buffer
;
2577 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
2578 DWORD stype
= arg
->reg_maps
->samplers
[reg
] & WINED3DSP_TEXTURETYPE_MASK
;
2579 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2580 glsl_sample_function_t sample_function
;
2582 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2583 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], src_mask
, &src1_param
);
2585 /* Perform the last matrix multiply operation */
2586 shader_addline(buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2587 /* Reflection calculation */
2588 shader_addline(buffer
, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param
.param_str
);
2590 shader_glsl_append_dst(buffer
, arg
);
2591 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2592 /* Dependent read, not valid with conditional NP2 */
2593 shader_glsl_get_sample_function(stype
, FALSE
, FALSE
, &sample_function
);
2595 /* Sample the texture */
2596 shader_addline(buffer
, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function
.name
, reg
, dst_mask
);
2598 current_state
->current_row
= 0;
2601 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
2602 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2603 static void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG
* arg
) {
2605 IWineD3DPixelShaderImpl
* shader
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2606 DWORD reg
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2607 SHADER_BUFFER
* buffer
= arg
->buffer
;
2608 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
2609 glsl_src_param_t src0_param
;
2611 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2612 DWORD sampler_type
= arg
->reg_maps
->samplers
[reg
] & WINED3DSP_TEXTURETYPE_MASK
;
2613 glsl_sample_function_t sample_function
;
2615 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], src_mask
, &src0_param
);
2617 /* Perform the last matrix multiply operation */
2618 shader_addline(buffer
, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg
, src0_param
.param_str
);
2620 /* Construct the eye-ray vector from w coordinates */
2621 shader_addline(buffer
, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
2622 current_state
->texcoord_w
[0], current_state
->texcoord_w
[1], reg
);
2623 shader_addline(buffer
, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
2625 shader_glsl_append_dst(buffer
, arg
);
2626 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2627 /* Dependent read, not valid with conditional NP2 */
2628 shader_glsl_get_sample_function(sampler_type
, FALSE
, FALSE
, &sample_function
);
2630 /* Sample the texture using the calculated coordinates */
2631 shader_addline(buffer
, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function
.name
, reg
, dst_mask
);
2633 current_state
->current_row
= 0;
2636 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
2637 * Apply a fake bump map transform.
2638 * texbem is pshader <= 1.3 only, this saves a few version checks
2640 static void pshader_glsl_texbem(SHADER_OPCODE_ARG
* arg
) {
2641 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2642 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
2643 char dst_swizzle
[6];
2644 glsl_sample_function_t sample_function
;
2645 glsl_src_param_t coord_param
;
2652 sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2653 flags
= deviceImpl
->stateBlock
->textureState
[sampler_idx
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
2655 sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
2656 /* Dependent read, not valid with conditional NP2 */
2657 shader_glsl_get_sample_function(sampler_type
, FALSE
, FALSE
, &sample_function
);
2658 mask
= sample_function
.coord_mask
;
2660 shader_glsl_get_write_mask(arg
->dst
, dst_swizzle
);
2662 shader_glsl_get_write_mask(mask
, coord_mask
);
2664 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2665 * so we can't let the GL handle this.
2667 if (flags
& WINED3DTTFF_PROJECTED
) {
2669 char coord_div_mask
[3];
2670 switch (flags
& ~WINED3DTTFF_PROJECTED
) {
2671 case WINED3DTTFF_COUNT1
: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2672 case WINED3DTTFF_COUNT2
: div_mask
= WINED3DSP_WRITEMASK_1
; break;
2673 case WINED3DTTFF_COUNT3
: div_mask
= WINED3DSP_WRITEMASK_2
; break;
2674 case WINED3DTTFF_COUNT4
:
2675 case WINED3DTTFF_DISABLE
: div_mask
= WINED3DSP_WRITEMASK_3
; break;
2677 shader_glsl_get_write_mask(div_mask
, coord_div_mask
);
2678 shader_addline(arg
->buffer
, "T%u%s /= T%u%s;\n", sampler_idx
, coord_mask
, sampler_idx
, coord_div_mask
);
2681 shader_glsl_append_dst(arg
->buffer
, arg
);
2682 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
|WINED3DSP_WRITEMASK_1
, &coord_param
);
2683 if(arg
->opcode
->opcode
== WINED3DSIO_TEXBEML
) {
2684 glsl_src_param_t luminance_param
;
2685 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_2
, &luminance_param
);
2686 shader_addline(arg
->buffer
, "(%s(Psampler%u, T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s )*(%s * luminancescale%d + luminanceoffset%d))%s);\n",
2687 sample_function
.name
, sampler_idx
, sampler_idx
, coord_mask
, sampler_idx
, coord_param
.param_str
, coord_mask
,
2688 luminance_param
.param_str
, sampler_idx
, sampler_idx
, dst_swizzle
);
2690 shader_addline(arg
->buffer
, "%s(Psampler%u, T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s )%s);\n",
2691 sample_function
.name
, sampler_idx
, sampler_idx
, coord_mask
, sampler_idx
, coord_param
.param_str
, coord_mask
, dst_swizzle
);
2695 static void pshader_glsl_bem(SHADER_OPCODE_ARG
* arg
) {
2696 glsl_src_param_t src0_param
, src1_param
;
2697 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2699 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
|WINED3DSP_WRITEMASK_1
, &src0_param
);
2700 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
|WINED3DSP_WRITEMASK_1
, &src1_param
);
2702 shader_glsl_append_dst(arg
->buffer
, arg
);
2703 shader_addline(arg
->buffer
, "%s + bumpenvmat%d * %s);\n",
2704 src0_param
.param_str
, sampler_idx
, src1_param
.param_str
);
2707 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
2708 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
2709 static void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG
* arg
) {
2711 glsl_src_param_t src0_param
;
2712 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2715 shader_glsl_append_dst(arg
->buffer
, arg
);
2716 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2717 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
2719 shader_addline(arg
->buffer
, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx
, src0_param
.reg_name
, dst_mask
);
2722 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
2723 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
2724 static void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG
* arg
) {
2725 glsl_src_param_t src0_param
;
2726 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2729 shader_glsl_append_dst(arg
->buffer
, arg
);
2730 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2731 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
2733 shader_addline(arg
->buffer
, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx
, src0_param
.reg_name
, dst_mask
);
2736 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
2737 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
2738 static void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG
* arg
) {
2739 glsl_src_param_t src0_param
;
2741 DWORD sampler_idx
= arg
->dst
& WINED3DSP_REGNUM_MASK
;
2742 DWORD sampler_type
= arg
->reg_maps
->samplers
[sampler_idx
] & WINED3DSP_TEXTURETYPE_MASK
;
2743 glsl_sample_function_t sample_function
;
2745 shader_glsl_append_dst(arg
->buffer
, arg
);
2746 shader_glsl_get_write_mask(arg
->dst
, dst_mask
);
2747 /* Dependent read, not valid with conditional NP2 */
2748 shader_glsl_get_sample_function(sampler_type
, FALSE
, FALSE
, &sample_function
);
2749 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], sample_function
.coord_mask
, &src0_param
);
2751 shader_addline(arg
->buffer
, "%s(Psampler%u, %s)%s);\n", sample_function
.name
, sampler_idx
, src0_param
.param_str
, dst_mask
);
2754 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
2755 * If any of the first 3 components are < 0, discard this pixel */
2756 static void pshader_glsl_texkill(SHADER_OPCODE_ARG
* arg
) {
2757 IWineD3DPixelShaderImpl
* This
= (IWineD3DPixelShaderImpl
*) arg
->shader
;
2758 DWORD hex_version
= This
->baseShader
.hex_version
;
2759 glsl_dst_param_t dst_param
;
2761 /* The argument is a destination parameter, and no writemasks are allowed */
2762 shader_glsl_add_dst_param(arg
, arg
->dst
, 0, &dst_param
);
2763 if((hex_version
>= WINED3DPS_VERSION(2,0))) {
2764 /* 2.0 shaders compare all 4 components in texkill */
2765 shader_addline(arg
->buffer
, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param
.reg_name
);
2767 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
2768 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
2769 * 4 components are defined, only the first 3 are used
2771 shader_addline(arg
->buffer
, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param
.reg_name
);
2775 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
2776 * dst = dot2(src0, src1) + src2 */
2777 static void pshader_glsl_dp2add(SHADER_OPCODE_ARG
* arg
) {
2778 glsl_src_param_t src0_param
;
2779 glsl_src_param_t src1_param
;
2780 glsl_src_param_t src2_param
;
2782 unsigned int mask_size
;
2784 write_mask
= shader_glsl_append_dst(arg
->buffer
, arg
);
2785 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2787 shader_glsl_add_src_param(arg
, arg
->src
[0], arg
->src_addr
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
2788 shader_glsl_add_src_param(arg
, arg
->src
[1], arg
->src_addr
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
2789 shader_glsl_add_src_param(arg
, arg
->src
[2], arg
->src_addr
[2], WINED3DSP_WRITEMASK_0
, &src2_param
);
2791 if (mask_size
> 1) {
2792 shader_addline(arg
->buffer
, "vec%d(dot(%s, %s) + %s));\n", mask_size
, src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2794 shader_addline(arg
->buffer
, "dot(%s, %s) + %s);\n", src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2798 static void pshader_glsl_input_pack(
2799 SHADER_BUFFER
* buffer
,
2800 semantic
* semantics_in
,
2801 IWineD3DPixelShader
*iface
) {
2804 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*) iface
;
2806 for (i
= 0; i
< MAX_REG_INPUT
; i
++) {
2808 DWORD usage_token
= semantics_in
[i
].usage
;
2809 DWORD register_token
= semantics_in
[i
].reg
;
2810 DWORD usage
, usage_idx
;
2814 if (!usage_token
) continue;
2815 usage
= (usage_token
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
2816 usage_idx
= (usage_token
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
2817 shader_glsl_get_write_mask(register_token
, reg_mask
);
2821 case WINED3DDECLUSAGE_TEXCOORD
:
2822 if(usage_idx
< 8 && This
->vertexprocessing
== pretransformed
) {
2823 shader_addline(buffer
, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
2824 This
->input_reg_map
[i
], reg_mask
, usage_idx
, reg_mask
);
2826 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2827 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
2831 case WINED3DDECLUSAGE_COLOR
:
2833 shader_addline(buffer
, "IN[%u]%s = vec4(gl_Color)%s;\n",
2834 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
2835 else if (usage_idx
== 1)
2836 shader_addline(buffer
, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
2837 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
2839 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2840 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
2844 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2845 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
2850 /*********************************************
2851 * Vertex Shader Specific Code begins here
2852 ********************************************/
2854 static void add_glsl_program_entry(struct shader_glsl_priv
*priv
, struct glsl_shader_prog_link
*entry
) {
2855 glsl_program_key_t
*key
;
2857 key
= HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t
));
2858 key
->vshader
= entry
->vshader
;
2859 key
->pshader
= entry
->pshader
;
2861 hash_table_put(priv
->glsl_program_lookup
, key
, entry
);
2864 static struct glsl_shader_prog_link
*get_glsl_program_entry(struct shader_glsl_priv
*priv
,
2865 GLhandleARB vshader
, GLhandleARB pshader
) {
2866 glsl_program_key_t key
;
2868 key
.vshader
= vshader
;
2869 key
.pshader
= pshader
;
2871 return (struct glsl_shader_prog_link
*)hash_table_get(priv
->glsl_program_lookup
, &key
);
2874 static void delete_glsl_program_entry(struct shader_glsl_priv
*priv
, WineD3D_GL_Info
*gl_info
, struct glsl_shader_prog_link
*entry
) {
2875 glsl_program_key_t
*key
;
2877 key
= HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t
));
2878 key
->vshader
= entry
->vshader
;
2879 key
->pshader
= entry
->pshader
;
2880 hash_table_remove(priv
->glsl_program_lookup
, key
);
2882 GL_EXTCALL(glDeleteObjectARB(entry
->programId
));
2883 if (entry
->vshader
) list_remove(&entry
->vshader_entry
);
2884 if (entry
->pshader
) list_remove(&entry
->pshader_entry
);
2885 HeapFree(GetProcessHeap(), 0, entry
->vuniformF_locations
);
2886 HeapFree(GetProcessHeap(), 0, entry
->puniformF_locations
);
2887 HeapFree(GetProcessHeap(), 0, entry
);
2890 static void handle_ps3_input(SHADER_BUFFER
*buffer
, semantic
*semantics_in
, semantic
*semantics_out
, WineD3D_GL_Info
*gl_info
, DWORD
*map
) {
2892 DWORD usage_token
, usage_token_out
;
2893 DWORD register_token
, register_token_out
;
2894 DWORD usage
, usage_idx
, usage_out
, usage_idx_out
;
2897 DWORD in_count
= GL_LIMITS(glsl_varyings
) / 4;
2898 char reg_mask
[6], reg_mask_out
[6];
2899 char destination
[50];
2901 set
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*set
) * (in_count
+ 2));
2903 if (!semantics_out
) {
2904 /* Save gl_FrontColor & gl_FrontSecondaryColor before overwriting them. */
2905 shader_addline(buffer
, "vec4 front_color = gl_FrontColor;\n");
2906 shader_addline(buffer
, "vec4 front_secondary_color = gl_FrontSecondaryColor;\n");
2909 for(i
= 0; i
< MAX_REG_INPUT
; i
++) {
2910 usage_token
= semantics_in
[i
].usage
;
2911 if (!usage_token
) continue;
2914 if (in_idx
>= (in_count
+ 2)) {
2915 FIXME("More input varyings declared than supported, expect issues\n");
2917 } else if(map
[i
] == -1) {
2918 /* Declared, but not read register */
2922 if (in_idx
== in_count
) {
2923 sprintf(destination
, "gl_FrontColor");
2924 } else if (in_idx
== in_count
+ 1) {
2925 sprintf(destination
, "gl_FrontSecondaryColor");
2927 sprintf(destination
, "IN[%u]", in_idx
);
2930 register_token
= semantics_in
[i
].reg
;
2932 usage
= (usage_token
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
2933 usage_idx
= (usage_token
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
2934 set
[map
[i
]] = shader_glsl_get_write_mask(register_token
, reg_mask
);
2936 if(!semantics_out
) {
2938 case WINED3DDECLUSAGE_COLOR
:
2940 shader_addline(buffer
, "%s%s = front_color%s;\n",
2941 destination
, reg_mask
, reg_mask
);
2942 else if (usage_idx
== 1)
2943 shader_addline(buffer
, "%s%s = front_secondary_color%s;\n",
2944 destination
, reg_mask
, reg_mask
);
2946 shader_addline(buffer
, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2947 destination
, reg_mask
, reg_mask
);
2950 case WINED3DDECLUSAGE_TEXCOORD
:
2951 if (usage_idx
< 8) {
2952 shader_addline(buffer
, "%s%s = gl_TexCoord[%u]%s;\n",
2953 destination
, reg_mask
, usage_idx
, reg_mask
);
2955 shader_addline(buffer
, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2956 destination
, reg_mask
, reg_mask
);
2960 case WINED3DDECLUSAGE_FOG
:
2961 shader_addline(buffer
, "%s%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
2962 destination
, reg_mask
, reg_mask
);
2966 shader_addline(buffer
, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2967 destination
, reg_mask
, reg_mask
);
2971 for(j
= 0; j
< MAX_REG_OUTPUT
; j
++) {
2972 usage_token_out
= semantics_out
[j
].usage
;
2973 if (!usage_token_out
) continue;
2974 register_token_out
= semantics_out
[j
].reg
;
2976 usage_out
= (usage_token_out
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
2977 usage_idx_out
= (usage_token_out
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
2978 shader_glsl_get_write_mask(register_token_out
, reg_mask_out
);
2980 if(usage
== usage_out
&&
2981 usage_idx
== usage_idx_out
) {
2982 shader_addline(buffer
, "%s%s = OUT[%u]%s;\n",
2983 destination
, reg_mask
, j
, reg_mask
);
2988 shader_addline(buffer
, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2989 destination
, reg_mask
, reg_mask
);
2994 /* This is solely to make the compiler / linker happy and avoid warning about undefined
2995 * varyings. It shouldn't result in any real code executed on the GPU, since all read
2996 * input varyings are assigned above, if the optimizer works properly.
2998 for(i
= 0; i
< in_count
+ 2; i
++) {
2999 if(set
[i
] != WINED3DSP_WRITEMASK_ALL
) {
3000 unsigned int size
= 0;
3001 memset(reg_mask
, 0, sizeof(reg_mask
));
3002 if(!(set
[i
] & WINED3DSP_WRITEMASK_0
)) {
3003 reg_mask
[size
] = 'x';
3006 if(!(set
[i
] & WINED3DSP_WRITEMASK_1
)) {
3007 reg_mask
[size
] = 'y';
3010 if(!(set
[i
] & WINED3DSP_WRITEMASK_2
)) {
3011 reg_mask
[size
] = 'z';
3014 if(!(set
[i
] & WINED3DSP_WRITEMASK_3
)) {
3015 reg_mask
[size
] = 'w';
3019 if (i
== in_count
) {
3020 sprintf(destination
, "gl_FrontColor");
3021 } else if (i
== in_count
+ 1) {
3022 sprintf(destination
, "gl_FrontSecondaryColor");
3024 sprintf(destination
, "IN[%u]", i
);
3028 shader_addline(buffer
, "%s.%s = 0.0;\n", destination
, reg_mask
);
3030 shader_addline(buffer
, "%s.%s = vec%u(0.0);\n", destination
, reg_mask
, size
);
3035 HeapFree(GetProcessHeap(), 0, set
);
3038 static GLhandleARB
generate_param_reorder_function(IWineD3DVertexShader
*vertexshader
,
3039 IWineD3DPixelShader
*pixelshader
,
3040 WineD3D_GL_Info
*gl_info
) {
3041 GLhandleARB ret
= 0;
3042 IWineD3DVertexShaderImpl
*vs
= (IWineD3DVertexShaderImpl
*) vertexshader
;
3043 IWineD3DPixelShaderImpl
*ps
= (IWineD3DPixelShaderImpl
*) pixelshader
;
3044 IWineD3DDeviceImpl
*device
;
3045 DWORD vs_major
= WINED3DSHADER_VERSION_MAJOR(vs
->baseShader
.hex_version
);
3046 DWORD ps_major
= ps
? WINED3DSHADER_VERSION_MAJOR(ps
->baseShader
.hex_version
) : 0;
3048 SHADER_BUFFER buffer
;
3050 DWORD register_token
;
3051 DWORD usage
, usage_idx
, writemask
;
3053 semantic
*semantics_out
, *semantics_in
;
3055 buffer
.buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, SHADER_PGMSIZE
);
3058 buffer
.newline
= TRUE
;
3060 shader_addline(&buffer
, "#version 120\n");
3062 if(vs_major
< 3 && ps_major
< 3) {
3063 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
3064 * Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
3066 device
= (IWineD3DDeviceImpl
*) vs
->baseShader
.device
;
3067 if((GLINFO_LOCATION
).set_texcoord_w
&& ps_major
== 0 && vs_major
> 0 &&
3068 !device
->frag_pipe
->ffp_proj_control
) {
3069 shader_addline(&buffer
, "void order_ps_input() {\n");
3070 for(i
= 0; i
< min(8, MAX_REG_TEXCRD
); i
++) {
3071 if(vs
->baseShader
.reg_maps
.texcoord_mask
[i
] != 0 &&
3072 vs
->baseShader
.reg_maps
.texcoord_mask
[i
] != WINED3DSP_WRITEMASK_ALL
) {
3073 shader_addline(&buffer
, "gl_TexCoord[%u].w = 1.0;\n", i
);
3076 shader_addline(&buffer
, "}\n");
3078 shader_addline(&buffer
, "void order_ps_input() { /* do nothing */ }\n");
3080 } else if(ps_major
< 3 && vs_major
>= 3) {
3081 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
3082 semantics_out
= vs
->semantics_out
;
3084 shader_addline(&buffer
, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT
);
3085 for(i
= 0; i
< MAX_REG_OUTPUT
; i
++) {
3086 usage_token
= semantics_out
[i
].usage
;
3087 if (!usage_token
) continue;
3088 register_token
= semantics_out
[i
].reg
;
3090 usage
= (usage_token
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
3091 usage_idx
= (usage_token
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
3092 writemask
= shader_glsl_get_write_mask(register_token
, reg_mask
);
3095 case WINED3DDECLUSAGE_COLOR
:
3097 shader_addline(&buffer
, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
3098 else if (usage_idx
== 1)
3099 shader_addline(&buffer
, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
3102 case WINED3DDECLUSAGE_POSITION
:
3103 shader_addline(&buffer
, "gl_Position%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
3106 case WINED3DDECLUSAGE_TEXCOORD
:
3107 if (usage_idx
< 8) {
3108 if(!(GLINFO_LOCATION
).set_texcoord_w
|| ps_major
> 0) writemask
|= WINED3DSP_WRITEMASK_3
;
3110 shader_addline(&buffer
, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3111 usage_idx
, reg_mask
, i
, reg_mask
);
3112 if(!(writemask
& WINED3DSP_WRITEMASK_3
)) {
3113 shader_addline(&buffer
, "gl_TexCoord[%u].w = 1.0;\n", usage_idx
);
3118 case WINED3DDECLUSAGE_PSIZE
:
3119 shader_addline(&buffer
, "gl_PointSize = OUT[%u].x;\n", i
);
3122 case WINED3DDECLUSAGE_FOG
:
3123 shader_addline(&buffer
, "gl_FogFragCoord = OUT[%u].%c;\n", i
, reg_mask
[1]);
3130 shader_addline(&buffer
, "}\n");
3132 } else if(ps_major
>= 3 && vs_major
>= 3) {
3133 semantics_out
= vs
->semantics_out
;
3134 semantics_in
= ps
->semantics_in
;
3136 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3137 shader_addline(&buffer
, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings
) / 4);
3138 shader_addline(&buffer
, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT
);
3140 /* First, sort out position and point size. Those are not passed to the pixel shader */
3141 for(i
= 0; i
< MAX_REG_OUTPUT
; i
++) {
3142 usage_token
= semantics_out
[i
].usage
;
3143 if (!usage_token
) continue;
3144 register_token
= semantics_out
[i
].reg
;
3146 usage
= (usage_token
& WINED3DSP_DCL_USAGE_MASK
) >> WINED3DSP_DCL_USAGE_SHIFT
;
3147 usage_idx
= (usage_token
& WINED3DSP_DCL_USAGEINDEX_MASK
) >> WINED3DSP_DCL_USAGEINDEX_SHIFT
;
3148 shader_glsl_get_write_mask(register_token
, reg_mask
);
3151 case WINED3DDECLUSAGE_POSITION
:
3152 shader_addline(&buffer
, "gl_Position%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
3155 case WINED3DDECLUSAGE_PSIZE
:
3156 shader_addline(&buffer
, "gl_PointSize = OUT[%u].x;\n", i
);
3164 /* Then, fix the pixel shader input */
3165 handle_ps3_input(&buffer
, semantics_in
, semantics_out
, gl_info
, ps
->input_reg_map
);
3167 shader_addline(&buffer
, "}\n");
3168 } else if(ps_major
>= 3 && vs_major
< 3) {
3169 semantics_in
= ps
->semantics_in
;
3171 shader_addline(&buffer
, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings
) / 4);
3172 shader_addline(&buffer
, "void order_ps_input() {\n");
3173 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
3174 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
3175 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
3177 handle_ps3_input(&buffer
, semantics_in
, NULL
, gl_info
, ps
->input_reg_map
);
3178 shader_addline(&buffer
, "}\n");
3180 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major
, ps_major
);
3183 ret
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
3184 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3185 GL_EXTCALL(glShaderSourceARB(ret
, 1, (const char**)&buffer
.buffer
, NULL
));
3186 checkGLcall("glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL)");
3187 GL_EXTCALL(glCompileShaderARB(ret
));
3188 checkGLcall("glCompileShaderARB(ret)");
3190 HeapFree(GetProcessHeap(), 0, buffer
.buffer
);
3194 static void hardcode_local_constants(IWineD3DBaseShaderImpl
*shader
, WineD3D_GL_Info
*gl_info
, GLhandleARB programId
, char prefix
) {
3195 local_constant
* lconst
;
3200 LIST_FOR_EACH_ENTRY(lconst
, &shader
->baseShader
.constantsF
, local_constant
, entry
) {
3201 value
= (float *) lconst
->value
;
3202 snprintf(glsl_name
, sizeof(glsl_name
), "%cLC%u", prefix
, lconst
->idx
);
3203 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
3204 GL_EXTCALL(glUniform4fvARB(tmp_loc
, 1, value
));
3206 checkGLcall("Hardcoding local constants\n");
3209 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
3210 * It sets the programId on the current StateBlock (because it should be called
3211 * inside of the DrawPrimitive() part of the render loop).
3213 * If a program for the given combination does not exist, create one, and store
3214 * the program in the hash table. If it creates a program, it will link the
3215 * given objects, too.
3217 static void set_glsl_shader_program(IWineD3DDevice
*iface
, BOOL use_ps
, BOOL use_vs
) {
3218 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3219 struct shader_glsl_priv
*priv
= (struct shader_glsl_priv
*)This
->shader_priv
;
3220 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3221 IWineD3DPixelShader
*pshader
= This
->stateBlock
->pixelShader
;
3222 IWineD3DVertexShader
*vshader
= This
->stateBlock
->vertexShader
;
3223 struct glsl_shader_prog_link
*entry
= NULL
;
3224 GLhandleARB programId
= 0;
3225 GLhandleARB reorder_shader_id
= 0;
3229 GLhandleARB vshader_id
= use_vs
? ((IWineD3DBaseShaderImpl
*)vshader
)->baseShader
.prgId
: 0;
3230 GLhandleARB pshader_id
= use_ps
? ((IWineD3DBaseShaderImpl
*)pshader
)->baseShader
.prgId
: 0;
3231 entry
= get_glsl_program_entry(priv
, vshader_id
, pshader_id
);
3233 priv
->glsl_program
= entry
;
3237 /* If we get to this point, then no matching program exists, so we create one */
3238 programId
= GL_EXTCALL(glCreateProgramObjectARB());
3239 TRACE("Created new GLSL shader program %u\n", programId
);
3241 /* Create the entry */
3242 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link
));
3243 entry
->programId
= programId
;
3244 entry
->vshader
= vshader_id
;
3245 entry
->pshader
= pshader_id
;
3246 /* Add the hash table entry */
3247 add_glsl_program_entry(priv
, entry
);
3249 /* Set the current program */
3250 priv
->glsl_program
= entry
;
3252 /* Attach GLSL vshader */
3254 int max_attribs
= 16; /* TODO: Will this always be the case? It is at the moment... */
3257 reorder_shader_id
= generate_param_reorder_function(vshader
, pshader
, gl_info
);
3258 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id
, programId
);
3259 GL_EXTCALL(glAttachObjectARB(programId
, reorder_shader_id
));
3260 checkGLcall("glAttachObjectARB");
3261 /* Flag the reorder function for deletion, then it will be freed automatically when the program
3264 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id
));
3266 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id
, programId
);
3267 GL_EXTCALL(glAttachObjectARB(programId
, vshader_id
));
3268 checkGLcall("glAttachObjectARB");
3270 /* Bind vertex attributes to a corresponding index number to match
3271 * the same index numbers as ARB_vertex_programs (makes loading
3272 * vertex attributes simpler). With this method, we can use the
3273 * exact same code to load the attributes later for both ARB and
3276 * We have to do this here because we need to know the Program ID
3277 * in order to make the bindings work, and it has to be done prior
3278 * to linking the GLSL program. */
3279 for (i
= 0; i
< max_attribs
; ++i
) {
3280 if (((IWineD3DBaseShaderImpl
*)vshader
)->baseShader
.reg_maps
.attributes
[i
]) {
3281 snprintf(tmp_name
, sizeof(tmp_name
), "attrib%i", i
);
3282 GL_EXTCALL(glBindAttribLocationARB(programId
, i
, tmp_name
));
3285 checkGLcall("glBindAttribLocationARB");
3287 list_add_head(&((IWineD3DBaseShaderImpl
*)vshader
)->baseShader
.linked_programs
, &entry
->vshader_entry
);
3290 /* Attach GLSL pshader */
3292 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id
, programId
);
3293 GL_EXTCALL(glAttachObjectARB(programId
, pshader_id
));
3294 checkGLcall("glAttachObjectARB");
3296 list_add_head(&((IWineD3DBaseShaderImpl
*)pshader
)->baseShader
.linked_programs
, &entry
->pshader_entry
);
3299 /* Link the program */
3300 TRACE("Linking GLSL shader program %u\n", programId
);
3301 GL_EXTCALL(glLinkProgramARB(programId
));
3302 print_glsl_info_log(&GLINFO_LOCATION
, programId
);
3304 entry
->vuniformF_locations
= HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB
) * GL_LIMITS(vshader_constantsF
));
3305 for (i
= 0; i
< GL_LIMITS(vshader_constantsF
); ++i
) {
3306 snprintf(glsl_name
, sizeof(glsl_name
), "VC[%i]", i
);
3307 entry
->vuniformF_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
3309 for (i
= 0; i
< MAX_CONST_I
; ++i
) {
3310 snprintf(glsl_name
, sizeof(glsl_name
), "VI[%i]", i
);
3311 entry
->vuniformI_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
3313 entry
->puniformF_locations
= HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB
) * GL_LIMITS(pshader_constantsF
));
3314 for (i
= 0; i
< GL_LIMITS(pshader_constantsF
); ++i
) {
3315 snprintf(glsl_name
, sizeof(glsl_name
), "PC[%i]", i
);
3316 entry
->puniformF_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
3318 for (i
= 0; i
< MAX_CONST_I
; ++i
) {
3319 snprintf(glsl_name
, sizeof(glsl_name
), "PI[%i]", i
);
3320 entry
->puniformI_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
3324 for(i
= 0; i
< ((IWineD3DPixelShaderImpl
*)pshader
)->numbumpenvmatconsts
; i
++) {
3326 sprintf(name
, "bumpenvmat%d", ((IWineD3DPixelShaderImpl
*)pshader
)->bumpenvmatconst
[i
].texunit
);
3327 entry
->bumpenvmat_location
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, name
));
3328 sprintf(name
, "luminancescale%d", ((IWineD3DPixelShaderImpl
*)pshader
)->luminanceconst
[i
].texunit
);
3329 entry
->luminancescale_location
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, name
));
3330 sprintf(name
, "luminanceoffset%d", ((IWineD3DPixelShaderImpl
*)pshader
)->luminanceconst
[i
].texunit
);
3331 entry
->luminanceoffset_location
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, name
));
3336 entry
->posFixup_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "posFixup"));
3337 entry
->srgb_comparison_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "srgb_comparison"));
3338 entry
->srgb_mul_low_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "srgb_mul_low"));
3339 entry
->ycorrection_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "ycorrection"));
3340 checkGLcall("Find glsl program uniform locations");
3342 if (pshader
&& WINED3DSHADER_VERSION_MAJOR(((IWineD3DPixelShaderImpl
*)pshader
)->baseShader
.hex_version
) >= 3
3343 && ((IWineD3DPixelShaderImpl
*)pshader
)->declared_in_count
> GL_LIMITS(glsl_varyings
) / 4) {
3344 TRACE("Shader %d needs vertex color clamping disabled\n", programId
);
3345 entry
->vertex_color_clamp
= GL_FALSE
;
3347 entry
->vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
3350 /* Set the shader to allow uniform loading on it */
3351 GL_EXTCALL(glUseProgramObjectARB(programId
));
3352 checkGLcall("glUseProgramObjectARB(programId)");
3354 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
3355 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
3356 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
3357 * vertex shader with fixed function pixel processing is used we make sure that the card
3358 * supports enough samplers to allow the max number of vertex samplers with all possible
3359 * fixed function fragment processing setups. So once the program is linked these samplers
3363 /* Load vertex shader samplers */
3364 shader_glsl_load_vsamplers(gl_info
, (IWineD3DStateBlock
*)This
->stateBlock
, programId
);
3367 /* Load pixel shader samplers */
3368 shader_glsl_load_psamplers(gl_info
, (IWineD3DStateBlock
*)This
->stateBlock
, programId
);
3371 /* If the local constants do not have to be loaded with the environment constants,
3372 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
3375 if(pshader
&& !((IWineD3DPixelShaderImpl
*)pshader
)->baseShader
.load_local_constsF
) {
3376 hardcode_local_constants((IWineD3DBaseShaderImpl
*) pshader
, gl_info
, programId
, 'P');
3378 if(vshader
&& !((IWineD3DVertexShaderImpl
*)vshader
)->baseShader
.load_local_constsF
) {
3379 hardcode_local_constants((IWineD3DBaseShaderImpl
*) vshader
, gl_info
, programId
, 'V');
3383 static GLhandleARB
create_glsl_blt_shader(WineD3D_GL_Info
*gl_info
, enum tex_types tex_type
) {
3384 GLhandleARB program_id
;
3385 GLhandleARB vshader_id
, pshader_id
;
3386 const char *blt_vshader
[] = {
3390 " gl_Position = gl_Vertex;\n"
3391 " gl_FrontColor = vec4(1.0);\n"
3392 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
3396 const char *blt_pshaders
[tex_type_count
] = {
3401 "uniform sampler2D sampler;\n"
3404 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
3410 "uniform samplerCube sampler;\n"
3413 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
3417 "#extension GL_ARB_texture_rectangle : enable\n"
3418 "uniform sampler2DRect sampler;\n"
3421 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
3425 if (!blt_pshaders
[tex_type
])
3427 FIXME("tex_type %#x not supported\n", tex_type
);
3431 vshader_id
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
3432 GL_EXTCALL(glShaderSourceARB(vshader_id
, 1, blt_vshader
, NULL
));
3433 GL_EXTCALL(glCompileShaderARB(vshader_id
));
3435 pshader_id
= GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB
));
3436 GL_EXTCALL(glShaderSourceARB(pshader_id
, 1, &blt_pshaders
[tex_type
], NULL
));
3437 GL_EXTCALL(glCompileShaderARB(pshader_id
));
3439 program_id
= GL_EXTCALL(glCreateProgramObjectARB());
3440 GL_EXTCALL(glAttachObjectARB(program_id
, vshader_id
));
3441 GL_EXTCALL(glAttachObjectARB(program_id
, pshader_id
));
3442 GL_EXTCALL(glLinkProgramARB(program_id
));
3444 print_glsl_info_log(&GLINFO_LOCATION
, program_id
);
3446 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
3449 GL_EXTCALL(glDeleteObjectARB(vshader_id
));
3450 GL_EXTCALL(glDeleteObjectARB(pshader_id
));
3454 static void shader_glsl_select(IWineD3DDevice
*iface
, BOOL usePS
, BOOL useVS
) {
3455 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3456 struct shader_glsl_priv
*priv
= (struct shader_glsl_priv
*)This
->shader_priv
;
3457 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3458 GLhandleARB program_id
= 0;
3459 GLenum old_vertex_color_clamp
, current_vertex_color_clamp
;
3461 old_vertex_color_clamp
= priv
->glsl_program
? priv
->glsl_program
->vertex_color_clamp
: GL_FIXED_ONLY_ARB
;
3463 if (useVS
|| usePS
) set_glsl_shader_program(iface
, usePS
, useVS
);
3464 else priv
->glsl_program
= NULL
;
3466 current_vertex_color_clamp
= priv
->glsl_program
? priv
->glsl_program
->vertex_color_clamp
: GL_FIXED_ONLY_ARB
;
3468 if (old_vertex_color_clamp
!= current_vertex_color_clamp
) {
3469 if (GL_SUPPORT(ARB_COLOR_BUFFER_FLOAT
)) {
3470 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, current_vertex_color_clamp
));
3471 checkGLcall("glClampColorARB");
3473 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
3477 program_id
= priv
->glsl_program
? priv
->glsl_program
->programId
: 0;
3478 if (program_id
) TRACE("Using GLSL program %u\n", program_id
);
3479 GL_EXTCALL(glUseProgramObjectARB(program_id
));
3480 checkGLcall("glUseProgramObjectARB");
3483 static void shader_glsl_select_depth_blt(IWineD3DDevice
*iface
, enum tex_types tex_type
) {
3484 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3485 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3486 struct shader_glsl_priv
*priv
= (struct shader_glsl_priv
*) This
->shader_priv
;
3487 GLhandleARB
*blt_program
= &priv
->depth_blt_program
[tex_type
];
3489 if (!*blt_program
) {
3491 *blt_program
= create_glsl_blt_shader(gl_info
, tex_type
);
3492 loc
= GL_EXTCALL(glGetUniformLocationARB(*blt_program
, "sampler"));
3493 GL_EXTCALL(glUseProgramObjectARB(*blt_program
));
3494 GL_EXTCALL(glUniform1iARB(loc
, 0));
3496 GL_EXTCALL(glUseProgramObjectARB(*blt_program
));
3500 static void shader_glsl_deselect_depth_blt(IWineD3DDevice
*iface
) {
3501 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3502 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3503 struct shader_glsl_priv
*priv
= (struct shader_glsl_priv
*) This
->shader_priv
;
3504 GLhandleARB program_id
;
3506 program_id
= priv
->glsl_program
? priv
->glsl_program
->programId
: 0;
3507 if (program_id
) TRACE("Using GLSL program %u\n", program_id
);
3509 GL_EXTCALL(glUseProgramObjectARB(program_id
));
3510 checkGLcall("glUseProgramObjectARB");
3513 static void shader_glsl_cleanup(IWineD3DDevice
*iface
) {
3514 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3515 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3516 GL_EXTCALL(glUseProgramObjectARB(0));
3519 static void shader_glsl_destroy(IWineD3DBaseShader
*iface
) {
3520 struct list
*linked_programs
;
3521 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*) iface
;
3522 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*)This
->baseShader
.device
;
3523 struct shader_glsl_priv
*priv
= (struct shader_glsl_priv
*)device
->shader_priv
;
3524 WineD3D_GL_Info
*gl_info
= &device
->adapter
->gl_info
;
3526 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
3527 * can be called from IWineD3DBaseShader::Release
3529 char pshader
= shader_is_pshader_version(This
->baseShader
.hex_version
);
3531 if(This
->baseShader
.prgId
== 0) return;
3532 linked_programs
= &This
->baseShader
.linked_programs
;
3534 TRACE("Deleting linked programs\n");
3535 if (linked_programs
->next
) {
3536 struct glsl_shader_prog_link
*entry
, *entry2
;
3539 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
, struct glsl_shader_prog_link
, pshader_entry
) {
3540 delete_glsl_program_entry(priv
, gl_info
, entry
);
3543 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
, struct glsl_shader_prog_link
, vshader_entry
) {
3544 delete_glsl_program_entry(priv
, gl_info
, entry
);
3549 TRACE("Deleting shader object %u\n", This
->baseShader
.prgId
);
3550 GL_EXTCALL(glDeleteObjectARB(This
->baseShader
.prgId
));
3551 checkGLcall("glDeleteObjectARB");
3552 This
->baseShader
.prgId
= 0;
3553 This
->baseShader
.is_compiled
= FALSE
;
3556 static unsigned int glsl_program_key_hash(void *key
) {
3557 glsl_program_key_t
*k
= (glsl_program_key_t
*)key
;
3559 unsigned int hash
= k
->vshader
| k
->pshader
<< 16;
3560 hash
+= ~(hash
<< 15);
3561 hash
^= (hash
>> 10);
3562 hash
+= (hash
<< 3);
3563 hash
^= (hash
>> 6);
3564 hash
+= ~(hash
<< 11);
3565 hash
^= (hash
>> 16);
3570 static BOOL
glsl_program_key_compare(void *keya
, void *keyb
) {
3571 glsl_program_key_t
*ka
= (glsl_program_key_t
*)keya
;
3572 glsl_program_key_t
*kb
= (glsl_program_key_t
*)keyb
;
3574 return ka
->vshader
== kb
->vshader
&& ka
->pshader
== kb
->pshader
;
3577 static HRESULT
shader_glsl_alloc(IWineD3DDevice
*iface
) {
3578 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3579 struct shader_glsl_priv
*priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct shader_glsl_priv
));
3580 priv
->glsl_program_lookup
= hash_table_create(glsl_program_key_hash
, glsl_program_key_compare
);
3581 This
->shader_priv
= priv
;
3585 static void shader_glsl_free(IWineD3DDevice
*iface
) {
3586 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3587 WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3588 struct shader_glsl_priv
*priv
= (struct shader_glsl_priv
*)This
->shader_priv
;
3591 for (i
= 0; i
< tex_type_count
; ++i
)
3593 if (priv
->depth_blt_program
[i
])
3595 GL_EXTCALL(glDeleteObjectARB(priv
->depth_blt_program
[i
]));
3599 hash_table_destroy(priv
->glsl_program_lookup
, NULL
, NULL
);
3601 HeapFree(GetProcessHeap(), 0, This
->shader_priv
);
3602 This
->shader_priv
= NULL
;
3605 static BOOL
shader_glsl_dirty_const(IWineD3DDevice
*iface
) {
3606 /* TODO: GL_EXT_bindable_uniform can be used to share constants across shaders */
3610 static void shader_glsl_generate_pshader(IWineD3DPixelShader
*iface
, SHADER_BUFFER
*buffer
) {
3611 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)iface
;
3612 shader_reg_maps
* reg_maps
= &This
->baseShader
.reg_maps
;
3613 CONST DWORD
*function
= This
->baseShader
.function
;
3614 const char *fragcolor
;
3615 WineD3D_GL_Info
*gl_info
= &((IWineD3DDeviceImpl
*)This
->baseShader
.device
)->adapter
->gl_info
;
3617 /* Create the hw GLSL shader object and assign it as the baseShader.prgId */
3618 GLhandleARB shader_obj
= GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB
));
3620 shader_addline(buffer
, "#version 120\n");
3622 if (GL_SUPPORT(ARB_DRAW_BUFFERS
)) {
3623 shader_addline(buffer
, "#extension GL_ARB_draw_buffers : enable\n");
3625 if (GL_SUPPORT(ARB_TEXTURE_RECTANGLE
)) {
3626 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
3627 * drivers write a warning if we don't do so
3629 shader_addline(buffer
, "#extension GL_ARB_texture_rectangle : enable\n");
3632 /* Base Declarations */
3633 shader_generate_glsl_declarations( (IWineD3DBaseShader
*) This
, reg_maps
, buffer
, &GLINFO_LOCATION
);
3635 /* Pack 3.0 inputs */
3636 if (This
->baseShader
.hex_version
>= WINED3DPS_VERSION(3,0)) {
3638 if(((IWineD3DDeviceImpl
*) This
->baseShader
.device
)->strided_streams
.u
.s
.position_transformed
) {
3639 This
->vertexprocessing
= pretransformed
;
3640 pshader_glsl_input_pack(buffer
, This
->semantics_in
, iface
);
3641 } else if(!use_vs((IWineD3DDeviceImpl
*) This
->baseShader
.device
)) {
3642 This
->vertexprocessing
= fixedfunction
;
3643 pshader_glsl_input_pack(buffer
, This
->semantics_in
, iface
);
3645 This
->vertexprocessing
= vertexshader
;
3649 /* Base Shader Body */
3650 shader_generate_main( (IWineD3DBaseShader
*) This
, buffer
, reg_maps
, function
);
3652 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
3653 if (This
->baseShader
.hex_version
< WINED3DPS_VERSION(2,0)) {
3654 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
3655 if(GL_SUPPORT(ARB_DRAW_BUFFERS
))
3656 shader_addline(buffer
, "gl_FragData[0] = R0;\n");
3658 shader_addline(buffer
, "gl_FragColor = R0;\n");
3661 if(GL_SUPPORT(ARB_DRAW_BUFFERS
)) {
3662 fragcolor
= "gl_FragData[0]";
3664 fragcolor
= "gl_FragColor";
3666 if(This
->srgb_enabled
) {
3667 shader_addline(buffer
, "tmp0.xyz = pow(%s.xyz, vec3(%f, %f, %f)) * vec3(%f, %f, %f) - vec3(%f, %f, %f);\n",
3668 fragcolor
, srgb_pow
, srgb_pow
, srgb_pow
, srgb_mul_high
, srgb_mul_high
, srgb_mul_high
,
3669 srgb_sub_high
, srgb_sub_high
, srgb_sub_high
);
3670 shader_addline(buffer
, "tmp1.xyz = %s.xyz * srgb_mul_low.xyz;\n", fragcolor
);
3671 shader_addline(buffer
, "%s.x = %s.x < srgb_comparison.x ? tmp1.x : tmp0.x;\n", fragcolor
, fragcolor
);
3672 shader_addline(buffer
, "%s.y = %s.y < srgb_comparison.y ? tmp1.y : tmp0.y;\n", fragcolor
, fragcolor
);
3673 shader_addline(buffer
, "%s.z = %s.z < srgb_comparison.z ? tmp1.z : tmp0.z;\n", fragcolor
, fragcolor
);
3674 shader_addline(buffer
, "%s = clamp(%s, 0.0, 1.0);\n", fragcolor
, fragcolor
);
3676 /* Pixel shader < 3.0 do not replace the fog stage.
3677 * This implements linear fog computation and blending.
3678 * TODO: non linear fog
3679 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
3680 * -1/(e-s) and e/(e-s) respectively.
3682 if(This
->baseShader
.hex_version
< WINED3DPS_VERSION(3,0)) {
3683 shader_addline(buffer
, "float Fog = clamp(gl_FogFragCoord * gl_Fog.start + gl_Fog.end, 0.0, 1.0);\n");
3684 shader_addline(buffer
, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor
, fragcolor
);
3687 shader_addline(buffer
, "}\n");
3689 TRACE("Compiling shader object %u\n", shader_obj
);
3690 GL_EXTCALL(glShaderSourceARB(shader_obj
, 1, (const char**)&buffer
->buffer
, NULL
));
3691 GL_EXTCALL(glCompileShaderARB(shader_obj
));
3692 print_glsl_info_log(&GLINFO_LOCATION
, shader_obj
);
3694 /* Store the shader object */
3695 This
->baseShader
.prgId
= shader_obj
;
3698 static void shader_glsl_generate_vshader(IWineD3DVertexShader
*iface
, SHADER_BUFFER
*buffer
) {
3699 IWineD3DVertexShaderImpl
*This
= (IWineD3DVertexShaderImpl
*)iface
;
3700 shader_reg_maps
* reg_maps
= &This
->baseShader
.reg_maps
;
3701 CONST DWORD
*function
= This
->baseShader
.function
;
3702 WineD3D_GL_Info
*gl_info
= &((IWineD3DDeviceImpl
*)This
->baseShader
.device
)->adapter
->gl_info
;
3704 /* Create the hw GLSL shader program and assign it as the baseShader.prgId */
3705 GLhandleARB shader_obj
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
3707 shader_addline(buffer
, "#version 120\n");
3709 /* Base Declarations */
3710 shader_generate_glsl_declarations( (IWineD3DBaseShader
*) This
, reg_maps
, buffer
, &GLINFO_LOCATION
);
3712 /* Base Shader Body */
3713 shader_generate_main( (IWineD3DBaseShader
*) This
, buffer
, reg_maps
, function
);
3715 /* Unpack 3.0 outputs */
3716 if (This
->baseShader
.hex_version
>= WINED3DVS_VERSION(3,0)) {
3717 shader_addline(buffer
, "order_ps_input(OUT);\n");
3719 shader_addline(buffer
, "order_ps_input();\n");
3722 /* If this shader doesn't use fog copy the z coord to the fog coord so that we can use table fog */
3724 shader_addline(buffer
, "gl_FogFragCoord = gl_Position.z;\n");
3726 /* Write the final position.
3728 * OpenGL coordinates specify the center of the pixel while d3d coords specify
3729 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
3730 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
3731 * contains 1.0 to allow a mad.
3733 shader_addline(buffer
, "gl_Position.y = gl_Position.y * posFixup.y;\n");
3734 shader_addline(buffer
, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
3736 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
3738 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
3739 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
3740 * which is the same as z = z * 2 - w.
3742 shader_addline(buffer
, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3744 shader_addline(buffer
, "}\n");
3746 TRACE("Compiling shader object %u\n", shader_obj
);
3747 GL_EXTCALL(glShaderSourceARB(shader_obj
, 1, (const char**)&buffer
->buffer
, NULL
));
3748 GL_EXTCALL(glCompileShaderARB(shader_obj
));
3749 print_glsl_info_log(&GLINFO_LOCATION
, shader_obj
);
3751 /* Store the shader object */
3752 This
->baseShader
.prgId
= shader_obj
;
3755 static void shader_glsl_get_caps(WINED3DDEVTYPE devtype
, WineD3D_GL_Info
*gl_info
, struct shader_caps
*pCaps
) {
3756 /* Nvidia Geforce6/7 or Ati R4xx/R5xx cards with GLSL support, support VS 3.0 but older Nvidia/Ati
3757 * models with GLSL support only support 2.0. In case of nvidia we can detect VS 2.0 support using
3758 * vs_nv_version which is based on NV_vertex_program.
3759 * For Ati cards there's no way using glsl (it abstracts the lowlevel info away) and also not
3760 * using ARB_vertex_program. It is safe to assume that when a card supports pixel shader 2.0 it
3761 * supports vertex shader 2.0 too and the way around. We can detect ps2.0 using the maximum number
3762 * of native instructions, so use that here. For more info see the pixel shader versioning code below.
3764 if((GLINFO_LOCATION
.vs_nv_version
== VS_VERSION_20
) || (GLINFO_LOCATION
.ps_arb_max_instructions
<= 512))
3765 pCaps
->VertexShaderVersion
= WINED3DVS_VERSION(2,0);
3767 pCaps
->VertexShaderVersion
= WINED3DVS_VERSION(3,0);
3768 TRACE_(d3d_caps
)("Hardware vertex shader version %d.%d enabled (GLSL)\n", (pCaps
->VertexShaderVersion
>> 8) & 0xff, pCaps
->VertexShaderVersion
& 0xff);
3769 pCaps
->MaxVertexShaderConst
= GL_LIMITS(vshader_constantsF
);
3771 /* Older DX9-class videocards (GeforceFX / Radeon >9500/X*00) only support pixel shader 2.0/2.0a/2.0b.
3772 * In OpenGL the extensions related to GLSL abstract lowlevel GL info away which is needed
3773 * to distinguish between 2.0 and 3.0 (and 2.0a/2.0b). In case of Nvidia we use their fragment
3774 * program extensions. On other hardware including ATI GL_ARB_fragment_program offers the info
3775 * in max native instructions. Intel and others also offer the info in this extension but they
3776 * don't support GLSL (at least on Windows).
3778 * PS2.0 requires at least 96 instructions, 2.0a/2.0b go up to 512. Assume that if the number
3779 * of instructions is 512 or less we have to do with ps2.0 hardware.
3780 * NOTE: ps3.0 hardware requires 512 or more instructions but ati and nvidia offer 'enough' (1024 vs 4096) on their most basic ps3.0 hardware.
3782 if((GLINFO_LOCATION
.ps_nv_version
== PS_VERSION_20
) || (GLINFO_LOCATION
.ps_arb_max_instructions
<= 512))
3783 pCaps
->PixelShaderVersion
= WINED3DPS_VERSION(2,0);
3785 pCaps
->PixelShaderVersion
= WINED3DPS_VERSION(3,0);
3787 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
3788 * Direct3D minimum requirement.
3790 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
3791 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
3793 * The problem is that the refrast clamps temporary results in the shader to
3794 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
3795 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
3796 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
3797 * offer a way to query this.
3799 pCaps
->PixelShader1xMaxValue
= 8.0;
3800 TRACE_(d3d_caps
)("Hardware pixel shader version %d.%d enabled (GLSL)\n", (pCaps
->PixelShaderVersion
>> 8) & 0xff, pCaps
->PixelShaderVersion
& 0xff);
3803 static BOOL
shader_glsl_conv_supported(WINED3DFORMAT fmt
) {
3804 TRACE("Checking shader format support for format %s:", debug_d3dformat(fmt
));
3806 case WINED3DFMT_V8U8
:
3807 case WINED3DFMT_V16U16
:
3808 case WINED3DFMT_X8L8V8U8
:
3809 case WINED3DFMT_L6V5U5
:
3810 case WINED3DFMT_Q8W8V8U8
:
3811 case WINED3DFMT_ATI2N
:
3820 static const SHADER_HANDLER shader_glsl_instruction_handler_table
[WINED3DSIH_TABLE_SIZE
] =
3822 /* WINED3DSIH_ABS */ shader_glsl_map2gl
,
3823 /* WINED3DSIH_ADD */ shader_glsl_arith
,
3824 /* WINED3DSIH_BEM */ pshader_glsl_bem
,
3825 /* WINED3DSIH_BREAK */ shader_glsl_break
,
3826 /* WINED3DSIH_BREAKC */ shader_glsl_breakc
,
3827 /* WINED3DSIH_BREAKP */ NULL
,
3828 /* WINED3DSIH_CALL */ shader_glsl_call
,
3829 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz
,
3830 /* WINED3DSIH_CMP */ shader_glsl_cmp
,
3831 /* WINED3DSIH_CND */ shader_glsl_cnd
,
3832 /* WINED3DSIH_CRS */ shader_glsl_cross
,
3833 /* WINED3DSIH_DCL */ NULL
,
3834 /* WINED3DSIH_DEF */ NULL
,
3835 /* WINED3DSIH_DEFB */ NULL
,
3836 /* WINED3DSIH_DEFI */ NULL
,
3837 /* WINED3DSIH_DP2ADD */ pshader_glsl_dp2add
,
3838 /* WINED3DSIH_DP3 */ shader_glsl_dot
,
3839 /* WINED3DSIH_DP4 */ shader_glsl_dot
,
3840 /* WINED3DSIH_DST */ shader_glsl_dst
,
3841 /* WINED3DSIH_DSX */ shader_glsl_map2gl
,
3842 /* WINED3DSIH_DSY */ shader_glsl_map2gl
,
3843 /* WINED3DSIH_ELSE */ shader_glsl_else
,
3844 /* WINED3DSIH_ENDIF */ shader_glsl_end
,
3845 /* WINED3DSIH_ENDLOOP */ shader_glsl_end
,
3846 /* WINED3DSIH_ENDREP */ shader_glsl_end
,
3847 /* WINED3DSIH_EXP */ shader_glsl_map2gl
,
3848 /* WINED3DSIH_EXPP */ shader_glsl_expp
,
3849 /* WINED3DSIH_FRC */ shader_glsl_map2gl
,
3850 /* WINED3DSIH_IF */ shader_glsl_if
,
3851 /* WINED3DSIH_IFC */ shader_glsl_ifc
,
3852 /* WINED3DSIH_LABEL */ shader_glsl_label
,
3853 /* WINED3DSIH_LIT */ shader_glsl_lit
,
3854 /* WINED3DSIH_LOG */ shader_glsl_log
,
3855 /* WINED3DSIH_LOGP */ shader_glsl_log
,
3856 /* WINED3DSIH_LOOP */ shader_glsl_loop
,
3857 /* WINED3DSIH_LRP */ shader_glsl_lrp
,
3858 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn
,
3859 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn
,
3860 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn
,
3861 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn
,
3862 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn
,
3863 /* WINED3DSIH_MAD */ shader_glsl_mad
,
3864 /* WINED3DSIH_MAX */ shader_glsl_map2gl
,
3865 /* WINED3DSIH_MIN */ shader_glsl_map2gl
,
3866 /* WINED3DSIH_MOV */ shader_glsl_mov
,
3867 /* WINED3DSIH_MOVA */ shader_glsl_mov
,
3868 /* WINED3DSIH_MUL */ shader_glsl_arith
,
3869 /* WINED3DSIH_NOP */ NULL
,
3870 /* WINED3DSIH_NRM */ shader_glsl_map2gl
,
3871 /* WINED3DSIH_PHASE */ NULL
,
3872 /* WINED3DSIH_POW */ shader_glsl_pow
,
3873 /* WINED3DSIH_RCP */ shader_glsl_rcp
,
3874 /* WINED3DSIH_REP */ shader_glsl_rep
,
3875 /* WINED3DSIH_RET */ NULL
,
3876 /* WINED3DSIH_RSQ */ shader_glsl_rsq
,
3877 /* WINED3DSIH_SETP */ NULL
,
3878 /* WINED3DSIH_SGE */ shader_glsl_compare
,
3879 /* WINED3DSIH_SGN */ shader_glsl_map2gl
,
3880 /* WINED3DSIH_SINCOS */ shader_glsl_sincos
,
3881 /* WINED3DSIH_SLT */ shader_glsl_compare
,
3882 /* WINED3DSIH_SUB */ shader_glsl_arith
,
3883 /* WINED3DSIH_TEX */ pshader_glsl_tex
,
3884 /* WINED3DSIH_TEXBEM */ pshader_glsl_texbem
,
3885 /* WINED3DSIH_TEXBEML */ pshader_glsl_texbem
,
3886 /* WINED3DSIH_TEXCOORD */ pshader_glsl_texcoord
,
3887 /* WINED3DSIH_TEXDEPTH */ pshader_glsl_texdepth
,
3888 /* WINED3DSIH_TEXDP3 */ pshader_glsl_texdp3
,
3889 /* WINED3DSIH_TEXDP3TEX */ pshader_glsl_texdp3tex
,
3890 /* WINED3DSIH_TEXKILL */ pshader_glsl_texkill
,
3891 /* WINED3DSIH_TEXLDD */ NULL
,
3892 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl
,
3893 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_glsl_texm3x2depth
,
3894 /* WINED3DSIH_TEXM3x2PAD */ pshader_glsl_texm3x2pad
,
3895 /* WINED3DSIH_TEXM3x2TEX */ pshader_glsl_texm3x2tex
,
3896 /* WINED3DSIH_TEXM3x3 */ pshader_glsl_texm3x3
,
3897 /* WINED3DSIH_TEXM3x3DIFF */ NULL
,
3898 /* WINED3DSIH_TEXM3x3PAD */ pshader_glsl_texm3x3pad
,
3899 /* WINED3DSIH_TEXM3x3SPEC */ pshader_glsl_texm3x3spec
,
3900 /* WINED3DSIH_TEXM3x3TEX */ pshader_glsl_texm3x3tex
,
3901 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_glsl_texm3x3vspec
,
3902 /* WINED3DSIH_TEXREG2AR */ pshader_glsl_texreg2ar
,
3903 /* WINED3DSIH_TEXREG2GB */ pshader_glsl_texreg2gb
,
3904 /* WINED3DSIH_TEXREG2RGB */ pshader_glsl_texreg2rgb
,
3907 const shader_backend_t glsl_shader_backend
= {
3908 shader_glsl_instruction_handler_table
,
3910 shader_glsl_select_depth_blt
,
3911 shader_glsl_deselect_depth_blt
,
3912 shader_glsl_load_constants
,
3913 shader_glsl_cleanup
,
3914 shader_glsl_color_correction
,
3915 shader_glsl_destroy
,
3918 shader_glsl_dirty_const
,
3919 shader_glsl_generate_pshader
,
3920 shader_glsl_generate_vshader
,
3921 shader_glsl_get_caps
,
3922 shader_glsl_conv_supported
,