wined3d: Track shader constants in the shader backend.
[wine/wine64.git] / dlls / wined3d / glsl_shader.c
blob205f111128447e6fae856250ea32ac04a219274f
1 /*
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.
31 #include "config.h"
32 #include <stdio.h>
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);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d);
40 #define GLINFO_LOCATION (*gl_info)
42 typedef struct {
43 char reg_name[150];
44 char mask_str[6];
45 } glsl_dst_param_t;
47 typedef struct {
48 char reg_name[150];
49 char param_str[100];
50 } glsl_src_param_t;
52 typedef struct {
53 const char *name;
54 DWORD coord_mask;
55 } glsl_sample_function_t;
57 /* GLSL shader private data */
58 struct shader_glsl_priv {
59 struct hash_table_t *glsl_program_lookup;
60 const struct glsl_shader_prog_link *glsl_program;
61 GLhandleARB depth_blt_program[tex_type_count];
64 /* Struct to maintain data about a linked GLSL program */
65 struct glsl_shader_prog_link {
66 struct list vshader_entry;
67 struct list pshader_entry;
68 GLhandleARB programId;
69 GLhandleARB *vuniformF_locations;
70 GLhandleARB *puniformF_locations;
71 GLhandleARB vuniformI_locations[MAX_CONST_I];
72 GLhandleARB puniformI_locations[MAX_CONST_I];
73 GLhandleARB posFixup_location;
74 GLhandleARB bumpenvmat_location[MAX_TEXTURES];
75 GLhandleARB luminancescale_location[MAX_TEXTURES];
76 GLhandleARB luminanceoffset_location[MAX_TEXTURES];
77 GLhandleARB ycorrection_location;
78 GLenum vertex_color_clamp;
79 GLhandleARB vshader;
80 IWineD3DPixelShader *pshader;
81 struct ps_compile_args ps_args;
84 typedef struct {
85 GLhandleARB vshader;
86 IWineD3DPixelShader *pshader;
87 struct ps_compile_args ps_args;
88 } glsl_program_key_t;
91 /** Prints the GLSL info log which will contain error messages if they exist */
92 static void print_glsl_info_log(const WineD3D_GL_Info *gl_info, GLhandleARB obj)
94 int infologLength = 0;
95 char *infoLog;
96 unsigned int i;
97 BOOL is_spam;
99 static const char * const spam[] =
101 "Vertex shader was successfully compiled to run on hardware.\n", /* fglrx */
102 "Fragment shader was successfully compiled to run on hardware.\n", /* fglrx */
103 "Fragment shader(s) linked, vertex shader(s) linked. \n ", /* fglrx, with \n */
104 "Fragment shader(s) linked, vertex shader(s) linked.", /* fglrx, no \n */
105 "Vertex shader(s) linked, no fragment shader(s) defined. \n ", /* fglrx, with \n */
106 "Vertex shader(s) linked, no fragment shader(s) defined.", /* fglrx, no \n */
107 "Fragment shader was successfully compiled to run on hardware.\nWARNING: 0:1: extension 'GL_ARB_draw_buffers' is not supported",
108 "Fragment shader(s) linked, no vertex shader(s) defined.", /* fglrx, no \n */
109 "Fragment shader(s) linked, no vertex shader(s) defined. \n ", /* fglrx, with \n */
110 "WARNING: 0:2: extension 'GL_ARB_draw_buffers' is not supported\n" /* MacOS ati */
113 GL_EXTCALL(glGetObjectParameterivARB(obj,
114 GL_OBJECT_INFO_LOG_LENGTH_ARB,
115 &infologLength));
117 /* A size of 1 is just a null-terminated string, so the log should be bigger than
118 * that if there are errors. */
119 if (infologLength > 1)
121 /* Fglrx doesn't terminate the string properly, but it tells us the proper length.
122 * So use HEAP_ZERO_MEMORY to avoid uninitialized bytes
124 infoLog = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, infologLength);
125 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
126 is_spam = FALSE;
128 for(i = 0; i < sizeof(spam) / sizeof(spam[0]); i++) {
129 if(strcmp(infoLog, spam[i]) == 0) {
130 is_spam = TRUE;
131 break;
134 if(is_spam) {
135 TRACE("Spam received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
136 } else {
137 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
139 HeapFree(GetProcessHeap(), 0, infoLog);
144 * Loads (pixel shader) samplers
146 static void shader_glsl_load_psamplers(const WineD3D_GL_Info *gl_info, IWineD3DStateBlock *iface, GLhandleARB programId)
148 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
149 GLhandleARB name_loc;
150 int i;
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");
162 } else {
163 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
169 static void shader_glsl_load_vsamplers(const WineD3D_GL_Info *gl_info, IWineD3DStateBlock *iface, GLhandleARB programId)
171 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
172 GLhandleARB name_loc;
173 char sampler_name[20];
174 int i;
176 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
177 snprintf(sampler_name, sizeof(sampler_name), "Vsampler%d", i);
178 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
179 if (name_loc != -1) {
180 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[MAX_FRAGMENT_SAMPLERS + i];
181 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(combined_samplers)) {
182 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
183 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
184 checkGLcall("glUniform1iARB");
185 } else {
186 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
192 /**
193 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
194 * When constant_list == NULL, it will load all the constants.
196 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
197 unsigned int max_constants, const float *constants, const GLhandleARB *constant_locations,
198 const struct list *constant_list)
200 const constants_entry *constant;
201 const local_constant *lconst;
202 GLhandleARB tmp_loc;
203 DWORD i, j, k;
204 const DWORD *idx;
206 if (TRACE_ON(d3d_shader)) {
207 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
208 idx = constant->idx;
209 j = constant->count;
210 while (j--) {
211 i = *idx++;
212 tmp_loc = constant_locations[i];
213 if (tmp_loc != -1) {
214 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
215 constants[i * 4 + 0], constants[i * 4 + 1],
216 constants[i * 4 + 2], constants[i * 4 + 3]);
222 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
223 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.reg_maps.shader_version) == 1
224 && shader_is_pshader_version(This->baseShader.reg_maps.shader_version))
226 float lcl_const[4];
228 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
229 idx = constant->idx;
230 j = constant->count;
231 while (j--) {
232 i = *idx++;
233 tmp_loc = constant_locations[i];
234 if (tmp_loc != -1) {
235 /* We found this uniform name in the program - go ahead and send the data */
236 k = i * 4;
237 if(constants[k + 0] < -1.0) lcl_const[0] = -1.0;
238 else if(constants[k + 0] > 1.0) lcl_const[0] = 1.0;
239 else lcl_const[0] = constants[k + 0];
240 if(constants[k + 1] < -1.0) lcl_const[1] = -1.0;
241 else if(constants[k + 1] > 1.0) lcl_const[1] = 1.0;
242 else lcl_const[1] = constants[k + 1];
243 if(constants[k + 2] < -1.0) lcl_const[2] = -1.0;
244 else if(constants[k + 2] > 1.0) lcl_const[2] = 1.0;
245 else lcl_const[2] = constants[k + 2];
246 if(constants[k + 3] < -1.0) lcl_const[3] = -1.0;
247 else if(constants[k + 3] > 1.0) lcl_const[3] = 1.0;
248 else lcl_const[3] = constants[k + 3];
250 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, lcl_const));
254 } else {
255 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
256 idx = constant->idx;
257 j = constant->count;
258 while (j--) {
259 i = *idx++;
260 tmp_loc = constant_locations[i];
261 if (tmp_loc != -1) {
262 /* We found this uniform name in the program - go ahead and send the data */
263 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
268 checkGLcall("glUniform4fvARB()");
270 if(!This->baseShader.load_local_constsF) {
271 TRACE("No need to load local float constants for this shader\n");
272 return;
275 /* Load immediate constants */
276 if (TRACE_ON(d3d_shader)) {
277 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
278 tmp_loc = constant_locations[lconst->idx];
279 if (tmp_loc != -1) {
280 const GLfloat *values = (const GLfloat *)lconst->value;
281 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
282 values[0], values[1], values[2], values[3]);
286 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
287 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
288 tmp_loc = constant_locations[lconst->idx];
289 if (tmp_loc != -1) {
290 /* We found this uniform name in the program - go ahead and send the data */
291 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, (const GLfloat *)lconst->value));
294 checkGLcall("glUniform4fvARB()");
297 /* Loads integer constants (aka uniforms) into the currently set GLSL program. */
298 static void shader_glsl_load_constantsI(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
299 const GLhandleARB locations[MAX_CONST_I], const int *constants, WORD constants_set)
301 unsigned int i;
302 struct list* ptr;
304 for (i = 0; constants_set; constants_set >>= 1, ++i)
306 if (!(constants_set & 1)) continue;
308 TRACE_(d3d_constants)("Loading constants %u: %i, %i, %i, %i\n",
309 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
311 /* We found this uniform name in the program - go ahead and send the data */
312 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
313 checkGLcall("glUniform4ivARB");
316 /* Load immediate constants */
317 ptr = list_head(&This->baseShader.constantsI);
318 while (ptr) {
319 const struct local_constant *lconst = LIST_ENTRY(ptr, const struct local_constant, entry);
320 unsigned int idx = lconst->idx;
321 const GLint *values = (const GLint *)lconst->value;
323 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
324 values[0], values[1], values[2], values[3]);
326 /* We found this uniform name in the program - go ahead and send the data */
327 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
328 checkGLcall("glUniform4ivARB");
329 ptr = list_next(&This->baseShader.constantsI, ptr);
333 /* Loads boolean constants (aka uniforms) into the currently set GLSL program. */
334 static void shader_glsl_load_constantsB(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
335 GLhandleARB programId, const BOOL *constants, WORD constants_set)
337 GLhandleARB tmp_loc;
338 unsigned int i;
339 char tmp_name[8];
340 char is_pshader = shader_is_pshader_version(This->baseShader.reg_maps.shader_version);
341 const char* prefix = is_pshader? "PB":"VB";
342 struct list* ptr;
344 /* TODO: Benchmark and see if it would be beneficial to store the
345 * locations of the constants to avoid looking up each time */
346 for (i = 0; constants_set; constants_set >>= 1, ++i)
348 if (!(constants_set & 1)) continue;
350 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i]);
352 /* TODO: Benchmark and see if it would be beneficial to store the
353 * locations of the constants to avoid looking up each time */
354 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
355 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
356 if (tmp_loc != -1)
358 /* We found this uniform name in the program - go ahead and send the data */
359 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
360 checkGLcall("glUniform1ivARB");
364 /* Load immediate constants */
365 ptr = list_head(&This->baseShader.constantsB);
366 while (ptr) {
367 const struct local_constant *lconst = LIST_ENTRY(ptr, const struct local_constant, entry);
368 unsigned int idx = lconst->idx;
369 const GLint *values = (const GLint *)lconst->value;
371 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
373 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
374 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
375 if (tmp_loc != -1) {
376 /* We found this uniform name in the program - go ahead and send the data */
377 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
378 checkGLcall("glUniform1ivARB");
380 ptr = list_next(&This->baseShader.constantsB, ptr);
387 * Loads the app-supplied constants into the currently set GLSL program.
389 static void shader_glsl_load_constants(
390 IWineD3DDevice* device,
391 char usePixelShader,
392 char useVertexShader) {
394 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
395 const struct shader_glsl_priv *priv = (struct shader_glsl_priv *)deviceImpl->shader_priv;
396 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
397 const WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
399 const GLhandleARB *constant_locations;
400 const struct list *constant_list;
401 GLhandleARB programId;
402 const struct glsl_shader_prog_link *prog = priv->glsl_program;
403 int i;
405 if (!prog) {
406 /* No GLSL program set - nothing to do. */
407 return;
409 programId = prog->programId;
411 if (useVertexShader) {
412 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
414 constant_locations = prog->vuniformF_locations;
415 constant_list = &stateBlock->set_vconstantsF;
417 /* Load DirectX 9 float constants/uniforms for vertex shader */
418 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
419 stateBlock->vertexShaderConstantF, constant_locations, constant_list);
421 /* Load DirectX 9 integer constants/uniforms for vertex shader */
422 if(vshader->baseShader.uses_int_consts) {
423 shader_glsl_load_constantsI(vshader, gl_info, prog->vuniformI_locations,
424 stateBlock->vertexShaderConstantI, stateBlock->changed.vertexShaderConstantsI);
427 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
428 if(vshader->baseShader.uses_bool_consts) {
429 shader_glsl_load_constantsB(vshader, gl_info, programId,
430 stateBlock->vertexShaderConstantB, stateBlock->changed.vertexShaderConstantsB);
433 /* Upload the position fixup params */
434 GL_EXTCALL(glUniform4fvARB(prog->posFixup_location, 1, &deviceImpl->posFixup[0]));
435 checkGLcall("glUniform4fvARB");
438 if (usePixelShader) {
440 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
442 constant_locations = prog->puniformF_locations;
443 constant_list = &stateBlock->set_pconstantsF;
445 /* Load DirectX 9 float constants/uniforms for pixel shader */
446 shader_glsl_load_constantsF(pshader, gl_info, GL_LIMITS(pshader_constantsF),
447 stateBlock->pixelShaderConstantF, constant_locations, constant_list);
449 /* Load DirectX 9 integer constants/uniforms for pixel shader */
450 if(pshader->baseShader.uses_int_consts) {
451 shader_glsl_load_constantsI(pshader, gl_info, prog->puniformI_locations,
452 stateBlock->pixelShaderConstantI, stateBlock->changed.pixelShaderConstantsI);
455 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
456 if(pshader->baseShader.uses_bool_consts) {
457 shader_glsl_load_constantsB(pshader, gl_info, programId,
458 stateBlock->pixelShaderConstantB, stateBlock->changed.pixelShaderConstantsB);
461 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
462 * It can't be 0 for a valid texbem instruction.
464 for(i = 0; i < ((IWineD3DPixelShaderImpl *) pshader)->numbumpenvmatconsts; i++) {
465 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pshader;
466 int stage = ps->luminanceconst[i].texunit;
468 const float *data = (const float *)&stateBlock->textureState[(int)ps->bumpenvmatconst[i].texunit][WINED3DTSS_BUMPENVMAT00];
469 GL_EXTCALL(glUniformMatrix2fvARB(prog->bumpenvmat_location[i], 1, 0, data));
470 checkGLcall("glUniformMatrix2fvARB");
472 /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
473 * is set too, so we can check that in the needsbumpmat check
475 if(ps->baseShader.reg_maps.luminanceparams[stage]) {
476 const GLfloat *scale = (const GLfloat *)&stateBlock->textureState[stage][WINED3DTSS_BUMPENVLSCALE];
477 const GLfloat *offset = (const GLfloat *)&stateBlock->textureState[stage][WINED3DTSS_BUMPENVLOFFSET];
479 GL_EXTCALL(glUniform1fvARB(prog->luminancescale_location[i], 1, scale));
480 checkGLcall("glUniform1fvARB");
481 GL_EXTCALL(glUniform1fvARB(prog->luminanceoffset_location[i], 1, offset));
482 checkGLcall("glUniform1fvARB");
486 if(((IWineD3DPixelShaderImpl *) pshader)->vpos_uniform) {
487 float correction_params[4];
488 if(deviceImpl->render_offscreen) {
489 correction_params[0] = 0.0;
490 correction_params[1] = 1.0;
491 } else {
492 /* position is window relative, not viewport relative */
493 correction_params[0] = ((IWineD3DSurfaceImpl *) deviceImpl->render_targets[0])->currentDesc.Height;
494 correction_params[1] = -1.0;
496 GL_EXTCALL(glUniform4fvARB(prog->ycorrection_location, 1, correction_params));
501 static void shader_glsl_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count)
503 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
504 UINT i;
506 for (i = start; i < count + start; ++i)
508 if (!This->stateBlock->changed.vertexShaderConstantsF[i])
510 constants_entry *ptr = LIST_ENTRY(list_head(&This->stateBlock->set_vconstantsF),
511 constants_entry, entry);
513 if (!ptr || ptr->count >= sizeof(ptr->idx) / sizeof(*ptr->idx))
515 ptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(constants_entry));
516 list_add_head(&This->stateBlock->set_vconstantsF, &ptr->entry);
518 ptr->idx[ptr->count++] = i;
523 static void shader_glsl_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count)
525 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
526 UINT i;
528 for (i = start; i < count + start; ++i)
530 if (!This->stateBlock->changed.pixelShaderConstantsF[i])
532 constants_entry *ptr = LIST_ENTRY(list_head(&This->stateBlock->set_pconstantsF),
533 constants_entry, entry);
535 if (!ptr || ptr->count >= sizeof(ptr->idx) / sizeof(*ptr->idx))
537 ptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(constants_entry));
538 list_add_head(&This->stateBlock->set_pconstantsF, &ptr->entry);
540 ptr->idx[ptr->count++] = i;
545 /** Generate the variable & register declarations for the GLSL output target */
546 static void shader_generate_glsl_declarations(IWineD3DBaseShader *iface, const shader_reg_maps *reg_maps,
547 SHADER_BUFFER *buffer, const WineD3D_GL_Info *gl_info)
549 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
550 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
551 DWORD shader_version = reg_maps->shader_version;
552 unsigned int i, extra_constants_needed = 0;
553 const local_constant *lconst;
555 /* There are some minor differences between pixel and vertex shaders */
556 char pshader = shader_is_pshader_version(shader_version);
557 char prefix = pshader ? 'P' : 'V';
559 /* Prototype the subroutines */
560 for (i = 0; i < This->baseShader.limits.label; i++) {
561 if (reg_maps->labels[i])
562 shader_addline(buffer, "void subroutine%u();\n", i);
565 /* Declare the constants (aka uniforms) */
566 if (This->baseShader.limits.constant_float > 0) {
567 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
568 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
569 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
572 if (This->baseShader.limits.constant_int > 0)
573 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
575 if (This->baseShader.limits.constant_bool > 0)
576 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
578 if(!pshader) {
579 shader_addline(buffer, "uniform vec4 posFixup;\n");
580 /* Predeclaration; This function is added at link time based on the pixel shader.
581 * VS 3.0 shaders have an array OUT[] the shader writes to, earlier versions don't have
582 * that. We know the input to the reorder function at vertex shader compile time, so
583 * we can deal with that. The reorder function for a 1.x and 2.x vertex shader can just
584 * read gl_FrontColor. The output depends on the pixel shader. The reorder function for a
585 * 1.x and 2.x pshader or for fixed function will write gl_FrontColor, and for a 3.0 shader
586 * it will write to the varying array. Here we depend on the shader optimizer on sorting that
587 * out. The nvidia driver only does that if the parameter is inout instead of out, hence the
588 * inout.
590 if (shader_version >= WINED3DVS_VERSION(3, 0))
592 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", MAX_REG_OUTPUT);
593 } else {
594 shader_addline(buffer, "void order_ps_input();\n");
596 } else {
597 IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
599 ps_impl->numbumpenvmatconsts = 0;
600 for(i = 0; i < (sizeof(reg_maps->bumpmat) / sizeof(reg_maps->bumpmat[0])); i++) {
601 if(!reg_maps->bumpmat[i]) {
602 continue;
605 ps_impl->bumpenvmatconst[(int) ps_impl->numbumpenvmatconsts].texunit = i;
606 shader_addline(buffer, "uniform mat2 bumpenvmat%d;\n", i);
608 if(reg_maps->luminanceparams) {
609 ps_impl->luminanceconst[(int) ps_impl->numbumpenvmatconsts].texunit = i;
610 shader_addline(buffer, "uniform float luminancescale%d;\n", i);
611 shader_addline(buffer, "uniform float luminanceoffset%d;\n", i);
612 extra_constants_needed++;
613 } else {
614 ps_impl->luminanceconst[(int) ps_impl->numbumpenvmatconsts].texunit = -1;
617 extra_constants_needed++;
618 ps_impl->numbumpenvmatconsts++;
621 if(device->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
622 shader_addline(buffer, "const vec4 srgb_mul_low = vec4(%f, %f, %f, %f);\n",
623 srgb_mul_low, srgb_mul_low, srgb_mul_low, srgb_mul_low);
624 shader_addline(buffer, "const vec4 srgb_comparison = vec4(%f, %f, %f, %f);\n",
625 srgb_cmp, srgb_cmp, srgb_cmp, srgb_cmp);
627 if(reg_maps->vpos || reg_maps->usesdsy) {
628 if(This->baseShader.limits.constant_float + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF)) {
629 shader_addline(buffer, "uniform vec4 ycorrection;\n");
630 ((IWineD3DPixelShaderImpl *) This)->vpos_uniform = 1;
631 extra_constants_needed++;
632 } else {
633 /* This happens because we do not have proper tracking of the constant registers that are
634 * actually used, only the max limit of the shader version
636 FIXME("Cannot find a free uniform for vpos correction params\n");
637 shader_addline(buffer, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
638 device->render_offscreen ? 0.0 : ((IWineD3DSurfaceImpl *) device->render_targets[0])->currentDesc.Height,
639 device->render_offscreen ? 1.0 : -1.0);
641 shader_addline(buffer, "vec4 vpos;\n");
645 /* Declare texture samplers */
646 for (i = 0; i < This->baseShader.limits.sampler; i++) {
647 if (reg_maps->samplers[i]) {
649 DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
650 switch (stype) {
652 case WINED3DSTT_1D:
653 shader_addline(buffer, "uniform sampler1D %csampler%u;\n", prefix, i);
654 break;
655 case WINED3DSTT_2D:
656 if(device->stateBlock->textures[i] &&
657 IWineD3DBaseTexture_GetTextureDimensions(device->stateBlock->textures[i]) == GL_TEXTURE_RECTANGLE_ARB) {
658 shader_addline(buffer, "uniform sampler2DRect %csampler%u;\n", prefix, i);
659 } else {
660 shader_addline(buffer, "uniform sampler2D %csampler%u;\n", prefix, i);
662 break;
663 case WINED3DSTT_CUBE:
664 shader_addline(buffer, "uniform samplerCube %csampler%u;\n", prefix, i);
665 break;
666 case WINED3DSTT_VOLUME:
667 shader_addline(buffer, "uniform sampler3D %csampler%u;\n", prefix, i);
668 break;
669 default:
670 shader_addline(buffer, "uniform unsupported_sampler %csampler%u;\n", prefix, i);
671 FIXME("Unrecognized sampler type: %#x\n", stype);
672 break;
677 /* Declare address variables */
678 for (i = 0; i < This->baseShader.limits.address; i++) {
679 if (reg_maps->address[i])
680 shader_addline(buffer, "ivec4 A%d;\n", i);
683 /* Declare texture coordinate temporaries and initialize them */
684 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
685 if (reg_maps->texcoord[i])
686 shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
689 /* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
690 * helper function shader that is linked in at link time
692 if (pshader && shader_version >= WINED3DPS_VERSION(3, 0))
694 if(use_vs(device)) {
695 shader_addline(buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
696 } else {
697 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
698 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
699 * pixel shader that reads the fixed function color into the packed input registers.
701 shader_addline(buffer, "vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
705 /* Declare output register temporaries */
706 if(This->baseShader.limits.packed_output) {
707 shader_addline(buffer, "vec4 OUT[%u];\n", This->baseShader.limits.packed_output);
710 /* Declare temporary variables */
711 for(i = 0; i < This->baseShader.limits.temporary; i++) {
712 if (reg_maps->temporary[i])
713 shader_addline(buffer, "vec4 R%u;\n", i);
716 /* Declare attributes */
717 for (i = 0; i < This->baseShader.limits.attributes; i++) {
718 if (reg_maps->attributes[i])
719 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
722 /* Declare loop registers aLx */
723 for (i = 0; i < reg_maps->loop_depth; i++) {
724 shader_addline(buffer, "int aL%u;\n", i);
725 shader_addline(buffer, "int tmpInt%u;\n", i);
728 /* Temporary variables for matrix operations */
729 shader_addline(buffer, "vec4 tmp0;\n");
730 shader_addline(buffer, "vec4 tmp1;\n");
732 /* Local constants use a different name so they can be loaded once at shader link time
733 * They can't be hardcoded into the shader text via LC = {x, y, z, w}; because the
734 * float -> string conversion can cause precision loss.
736 if(!This->baseShader.load_local_constsF) {
737 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
738 shader_addline(buffer, "uniform vec4 %cLC%u;\n", prefix, lconst->idx);
742 /* Start the main program */
743 shader_addline(buffer, "void main() {\n");
744 if(pshader && reg_maps->vpos) {
745 /* DirectX apps expect integer values, while OpenGL drivers add approximately 0.5. This causes
746 * off-by-one problems as spotted by the vPos d3d9 visual test. Unfortunately the ATI cards do
747 * not add exactly 0.5, but rather something like 0.49999999 or 0.50000001, which still causes
748 * precision troubles when we just substract 0.5.
750 * To deal with that just floor() the position. This will eliminate the fraction on all cards.
752 * TODO: Test how that behaves with multisampling once we can enable multisampling in winex11.
754 * An advantage of floor is that it works even if the driver doesn't add 1/2. It is somewhat
755 * questionable if 1.5, 2.5, ... are the proper values to return in gl_FragCoord, even though
756 * coordinates specify the pixel centers instead of the pixel corners. This code will behave
757 * correctly on drivers that returns integer values.
759 shader_addline(buffer, "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
763 /*****************************************************************************
764 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
766 * For more information, see http://wiki.winehq.org/DirectX-Shaders
767 ****************************************************************************/
769 /* Prototypes */
770 static void shader_glsl_add_src_param(const SHADER_OPCODE_ARG *arg, const DWORD param,
771 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param);
773 /** Used for opcode modifiers - They multiply the result by the specified amount */
774 static const char * const shift_glsl_tab[] = {
775 "", /* 0 (none) */
776 "2.0 * ", /* 1 (x2) */
777 "4.0 * ", /* 2 (x4) */
778 "8.0 * ", /* 3 (x8) */
779 "16.0 * ", /* 4 (x16) */
780 "32.0 * ", /* 5 (x32) */
781 "", /* 6 (x64) */
782 "", /* 7 (x128) */
783 "", /* 8 (d256) */
784 "", /* 9 (d128) */
785 "", /* 10 (d64) */
786 "", /* 11 (d32) */
787 "0.0625 * ", /* 12 (d16) */
788 "0.125 * ", /* 13 (d8) */
789 "0.25 * ", /* 14 (d4) */
790 "0.5 * " /* 15 (d2) */
793 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
794 static void shader_glsl_gen_modifier (
795 const DWORD instr,
796 const char *in_reg,
797 const char *in_regswizzle,
798 char *out_str) {
800 out_str[0] = 0;
802 if (instr == WINED3DSIO_TEXKILL)
803 return;
805 switch (instr & WINED3DSP_SRCMOD_MASK) {
806 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
807 case WINED3DSPSM_DW:
808 case WINED3DSPSM_NONE:
809 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
810 break;
811 case WINED3DSPSM_NEG:
812 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
813 break;
814 case WINED3DSPSM_NOT:
815 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
816 break;
817 case WINED3DSPSM_BIAS:
818 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
819 break;
820 case WINED3DSPSM_BIASNEG:
821 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
822 break;
823 case WINED3DSPSM_SIGN:
824 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
825 break;
826 case WINED3DSPSM_SIGNNEG:
827 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
828 break;
829 case WINED3DSPSM_COMP:
830 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
831 break;
832 case WINED3DSPSM_X2:
833 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
834 break;
835 case WINED3DSPSM_X2NEG:
836 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
837 break;
838 case WINED3DSPSM_ABS:
839 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
840 break;
841 case WINED3DSPSM_ABSNEG:
842 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
843 break;
844 default:
845 FIXME("Unhandled modifier %u\n", (instr & WINED3DSP_SRCMOD_MASK));
846 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
850 /** Writes the GLSL variable name that corresponds to the register that the
851 * DX opcode parameter is trying to access */
852 static void shader_glsl_get_register_name(const DWORD param, const DWORD addr_token,
853 char *regstr, BOOL *is_color, const SHADER_OPCODE_ARG *arg)
855 /* oPos, oFog and oPts in D3D */
856 static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
858 DWORD reg = param & WINED3DSP_REGNUM_MASK;
859 DWORD regtype = shader_get_regtype(param);
860 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
861 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
862 const WineD3D_GL_Info* gl_info = &deviceImpl->adapter->gl_info;
863 DWORD shader_version = This->baseShader.reg_maps.shader_version;
864 char pshader = shader_is_pshader_version(shader_version);
865 char tmpStr[150];
867 *is_color = FALSE;
869 switch (regtype) {
870 case WINED3DSPR_TEMP:
871 sprintf(tmpStr, "R%u", reg);
872 break;
873 case WINED3DSPR_INPUT:
874 if (pshader) {
875 /* Pixel shaders >= 3.0 */
876 if (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 3)
878 DWORD in_count = GL_LIMITS(glsl_varyings) / 4;
880 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
881 glsl_src_param_t rel_param;
882 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
884 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
885 * operation there
887 if(((IWineD3DPixelShaderImpl *) This)->input_reg_map[reg]) {
888 if (((IWineD3DPixelShaderImpl *)This)->declared_in_count > in_count) {
889 sprintf(tmpStr, "((%s + %u) > %d ? (%s + %u) > %d ? gl_SecondaryColor : gl_Color : IN[%s + %u])",
890 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg], in_count - 1,
891 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg], in_count,
892 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg]);
893 } else {
894 sprintf(tmpStr, "IN[%s + %u]", rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg]);
896 } else {
897 if (((IWineD3DPixelShaderImpl *)This)->declared_in_count > in_count) {
898 sprintf(tmpStr, "((%s) > %d ? (%s) > %d ? gl_SecondaryColor : gl_Color : IN[%s])",
899 rel_param.param_str, in_count - 1,
900 rel_param.param_str, in_count,
901 rel_param.param_str);
902 } else {
903 sprintf(tmpStr, "IN[%s]", rel_param.param_str);
906 } else {
907 DWORD idx = ((IWineD3DPixelShaderImpl *) This)->input_reg_map[reg];
908 if (idx == in_count) {
909 sprintf(tmpStr, "gl_Color");
910 } else if (idx == in_count + 1) {
911 sprintf(tmpStr, "gl_SecondaryColor");
912 } else {
913 sprintf(tmpStr, "IN[%u]", idx);
916 } else {
917 if (reg==0)
918 strcpy(tmpStr, "gl_Color");
919 else
920 strcpy(tmpStr, "gl_SecondaryColor");
922 } else {
923 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
924 *is_color = TRUE;
925 sprintf(tmpStr, "attrib%u", reg);
927 break;
928 case WINED3DSPR_CONST:
930 const char prefix = pshader? 'P':'V';
932 /* Relative addressing */
933 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
935 /* Relative addressing on shaders 2.0+ have a relative address token,
936 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
937 if (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 2)
939 glsl_src_param_t rel_param;
940 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
941 if(reg) {
942 sprintf(tmpStr, "%cC[%s + %u]", prefix, rel_param.param_str, reg);
943 } else {
944 sprintf(tmpStr, "%cC[%s]", prefix, rel_param.param_str);
946 } else {
947 if(reg) {
948 sprintf(tmpStr, "%cC[A0.x + %u]", prefix, reg);
949 } else {
950 sprintf(tmpStr, "%cC[A0.x]", prefix);
954 } else {
955 if(shader_constant_is_local(This, reg)) {
956 sprintf(tmpStr, "%cLC%u", prefix, reg);
957 } else {
958 sprintf(tmpStr, "%cC[%u]", prefix, reg);
962 break;
964 case WINED3DSPR_CONSTINT:
965 if (pshader)
966 sprintf(tmpStr, "PI[%u]", reg);
967 else
968 sprintf(tmpStr, "VI[%u]", reg);
969 break;
970 case WINED3DSPR_CONSTBOOL:
971 if (pshader)
972 sprintf(tmpStr, "PB[%u]", reg);
973 else
974 sprintf(tmpStr, "VB[%u]", reg);
975 break;
976 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
977 if (pshader) {
978 sprintf(tmpStr, "T%u", reg);
979 } else {
980 sprintf(tmpStr, "A%u", reg);
982 break;
983 case WINED3DSPR_LOOP:
984 sprintf(tmpStr, "aL%u", This->baseShader.cur_loop_regno - 1);
985 break;
986 case WINED3DSPR_SAMPLER:
987 if (pshader)
988 sprintf(tmpStr, "Psampler%u", reg);
989 else
990 sprintf(tmpStr, "Vsampler%u", reg);
991 break;
992 case WINED3DSPR_COLOROUT:
993 if (reg >= GL_LIMITS(buffers)) {
994 WARN("Write to render target %u, only %d supported\n", reg, 4);
996 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
997 sprintf(tmpStr, "gl_FragData[%u]", reg);
998 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
999 sprintf(tmpStr, "gl_FragColor");
1001 break;
1002 case WINED3DSPR_RASTOUT:
1003 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
1004 break;
1005 case WINED3DSPR_DEPTHOUT:
1006 sprintf(tmpStr, "gl_FragDepth");
1007 break;
1008 case WINED3DSPR_ATTROUT:
1009 if (reg == 0) {
1010 sprintf(tmpStr, "gl_FrontColor");
1011 } else {
1012 sprintf(tmpStr, "gl_FrontSecondaryColor");
1014 break;
1015 case WINED3DSPR_TEXCRDOUT:
1016 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1017 if (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 3) sprintf(tmpStr, "OUT[%u]", reg);
1018 else sprintf(tmpStr, "gl_TexCoord[%u]", reg);
1019 break;
1020 case WINED3DSPR_MISCTYPE:
1021 if (reg == 0) {
1022 /* vPos */
1023 sprintf(tmpStr, "vpos");
1024 } else if (reg == 1){
1025 /* Note that gl_FrontFacing is a bool, while vFace is
1026 * a float for which the sign determines front/back
1028 sprintf(tmpStr, "(gl_FrontFacing ? 1.0 : -1.0)");
1029 } else {
1030 FIXME("Unhandled misctype register %d\n", reg);
1031 sprintf(tmpStr, "unrecognized_register");
1033 break;
1034 default:
1035 FIXME("Unhandled register name Type(%d)\n", regtype);
1036 sprintf(tmpStr, "unrecognized_register");
1037 break;
1040 strcat(regstr, tmpStr);
1043 /* Get the GLSL write mask for the destination register */
1044 static DWORD shader_glsl_get_write_mask(const DWORD param, char *write_mask) {
1045 char *ptr = write_mask;
1046 DWORD mask = param & WINED3DSP_WRITEMASK_ALL;
1048 if (shader_is_scalar(param)) {
1049 mask = WINED3DSP_WRITEMASK_0;
1050 } else {
1051 *ptr++ = '.';
1052 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
1053 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
1054 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
1055 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
1058 *ptr = '\0';
1060 return mask;
1063 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1064 unsigned int size = 0;
1066 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1067 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1068 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1069 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1071 return size;
1074 static void shader_glsl_get_swizzle(const DWORD param, BOOL fixup, DWORD mask, char *swizzle_str) {
1075 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1076 * but addressed as "rgba". To fix this we need to swap the register's x
1077 * and z components. */
1078 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
1079 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1080 char *ptr = swizzle_str;
1082 if (!shader_is_scalar(param)) {
1083 *ptr++ = '.';
1084 /* swizzle bits fields: wwzzyyxx */
1085 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = swizzle_chars[swizzle & 0x03];
1086 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = swizzle_chars[(swizzle >> 2) & 0x03];
1087 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = swizzle_chars[(swizzle >> 4) & 0x03];
1088 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = swizzle_chars[(swizzle >> 6) & 0x03];
1091 *ptr = '\0';
1094 /* From a given parameter token, generate the corresponding GLSL string.
1095 * Also, return the actual register name and swizzle in case the
1096 * caller needs this information as well. */
1097 static void shader_glsl_add_src_param(const SHADER_OPCODE_ARG *arg, const DWORD param,
1098 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param)
1100 BOOL is_color = FALSE;
1101 char swizzle_str[6];
1103 src_param->reg_name[0] = '\0';
1104 src_param->param_str[0] = '\0';
1105 swizzle_str[0] = '\0';
1107 shader_glsl_get_register_name(param, addr_token, src_param->reg_name, &is_color, arg);
1109 shader_glsl_get_swizzle(param, is_color, mask, swizzle_str);
1110 shader_glsl_gen_modifier(param, src_param->reg_name, swizzle_str, src_param->param_str);
1113 /* From a given parameter token, generate the corresponding GLSL string.
1114 * Also, return the actual register name and swizzle in case the
1115 * caller needs this information as well. */
1116 static DWORD shader_glsl_add_dst_param(const SHADER_OPCODE_ARG* arg, const DWORD param,
1117 const DWORD addr_token, glsl_dst_param_t *dst_param)
1119 BOOL is_color = FALSE;
1121 dst_param->mask_str[0] = '\0';
1122 dst_param->reg_name[0] = '\0';
1124 shader_glsl_get_register_name(param, addr_token, dst_param->reg_name, &is_color, arg);
1125 return shader_glsl_get_write_mask(param, dst_param->mask_str);
1128 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1129 static DWORD shader_glsl_append_dst_ext(SHADER_BUFFER *buffer, const SHADER_OPCODE_ARG *arg, const DWORD param)
1131 glsl_dst_param_t dst_param;
1132 DWORD mask;
1133 int shift;
1135 mask = shader_glsl_add_dst_param(arg, param, arg->dst_addr, &dst_param);
1137 if(mask) {
1138 shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
1139 shader_addline(buffer, "%s%s = %s(", dst_param.reg_name, dst_param.mask_str, shift_glsl_tab[shift]);
1142 return mask;
1145 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1146 static DWORD shader_glsl_append_dst(SHADER_BUFFER *buffer, const SHADER_OPCODE_ARG *arg)
1148 return shader_glsl_append_dst_ext(buffer, arg, arg->dst);
1151 /** Process GLSL instruction modifiers */
1152 void shader_glsl_add_instruction_modifiers(const SHADER_OPCODE_ARG* arg)
1154 DWORD mask = arg->dst & WINED3DSP_DSTMOD_MASK;
1156 if (arg->opcode->dst_token && mask != 0) {
1157 glsl_dst_param_t dst_param;
1159 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
1161 if (mask & WINED3DSPDM_SATURATE) {
1162 /* _SAT means to clamp the value of the register to between 0 and 1 */
1163 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1164 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1166 if (mask & WINED3DSPDM_MSAMPCENTROID) {
1167 FIXME("_centroid modifier not handled\n");
1169 if (mask & WINED3DSPDM_PARTIALPRECISION) {
1170 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1175 static inline const char* shader_get_comp_op(
1176 const DWORD opcode) {
1178 DWORD op = (opcode & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1179 switch (op) {
1180 case COMPARISON_GT: return ">";
1181 case COMPARISON_EQ: return "==";
1182 case COMPARISON_GE: return ">=";
1183 case COMPARISON_LT: return "<";
1184 case COMPARISON_NE: return "!=";
1185 case COMPARISON_LE: return "<=";
1186 default:
1187 FIXME("Unrecognized comparison value: %u\n", op);
1188 return "(\?\?)";
1192 static void shader_glsl_get_sample_function(DWORD sampler_type, BOOL projected, BOOL texrect, glsl_sample_function_t *sample_function) {
1193 /* Note that there's no such thing as a projected cube texture. */
1194 switch(sampler_type) {
1195 case WINED3DSTT_1D:
1196 sample_function->name = projected ? "texture1DProj" : "texture1D";
1197 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1198 break;
1199 case WINED3DSTT_2D:
1200 if(texrect) {
1201 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
1202 } else {
1203 sample_function->name = projected ? "texture2DProj" : "texture2D";
1205 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1206 break;
1207 case WINED3DSTT_CUBE:
1208 sample_function->name = "textureCube";
1209 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1210 break;
1211 case WINED3DSTT_VOLUME:
1212 sample_function->name = projected ? "texture3DProj" : "texture3D";
1213 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1214 break;
1215 default:
1216 sample_function->name = "";
1217 sample_function->coord_mask = 0;
1218 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
1219 break;
1223 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
1224 BOOL sign_fixup, enum fixup_channel_source channel_source)
1226 switch(channel_source)
1228 case CHANNEL_SOURCE_ZERO:
1229 strcat(arguments, "0.0");
1230 break;
1232 case CHANNEL_SOURCE_ONE:
1233 strcat(arguments, "1.0");
1234 break;
1236 case CHANNEL_SOURCE_X:
1237 strcat(arguments, reg_name);
1238 strcat(arguments, ".x");
1239 break;
1241 case CHANNEL_SOURCE_Y:
1242 strcat(arguments, reg_name);
1243 strcat(arguments, ".y");
1244 break;
1246 case CHANNEL_SOURCE_Z:
1247 strcat(arguments, reg_name);
1248 strcat(arguments, ".z");
1249 break;
1251 case CHANNEL_SOURCE_W:
1252 strcat(arguments, reg_name);
1253 strcat(arguments, ".w");
1254 break;
1256 default:
1257 FIXME("Unhandled channel source %#x\n", channel_source);
1258 strcat(arguments, "undefined");
1259 break;
1262 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
1265 static void shader_glsl_color_correction(const struct SHADER_OPCODE_ARG *arg, struct color_fixup_desc fixup)
1267 unsigned int mask_size, remaining;
1268 glsl_dst_param_t dst_param;
1269 char arguments[256];
1270 DWORD mask;
1271 BOOL dummy;
1273 mask = 0;
1274 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) mask |= WINED3DSP_WRITEMASK_0;
1275 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) mask |= WINED3DSP_WRITEMASK_1;
1276 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) mask |= WINED3DSP_WRITEMASK_2;
1277 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) mask |= WINED3DSP_WRITEMASK_3;
1278 mask &= arg->dst;
1280 if (!mask) return; /* Nothing to do */
1282 if (is_yuv_fixup(fixup))
1284 enum yuv_fixup yuv_fixup = get_yuv_fixup(fixup);
1285 FIXME("YUV fixup (%#x) not supported\n", yuv_fixup);
1286 return;
1289 mask_size = shader_glsl_get_write_mask_size(mask);
1291 dst_param.mask_str[0] = '\0';
1292 shader_glsl_get_write_mask(mask, dst_param.mask_str);
1294 dst_param.reg_name[0] = '\0';
1295 shader_glsl_get_register_name(arg->dst, arg->dst_addr, dst_param.reg_name, &dummy, arg);
1297 arguments[0] = '\0';
1298 remaining = mask_size;
1299 if (mask & WINED3DSP_WRITEMASK_0)
1301 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.x_sign_fixup, fixup.x_source);
1302 if (--remaining) strcat(arguments, ", ");
1304 if (mask & WINED3DSP_WRITEMASK_1)
1306 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.y_sign_fixup, fixup.y_source);
1307 if (--remaining) strcat(arguments, ", ");
1309 if (mask & WINED3DSP_WRITEMASK_2)
1311 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.z_sign_fixup, fixup.z_source);
1312 if (--remaining) strcat(arguments, ", ");
1314 if (mask & WINED3DSP_WRITEMASK_3)
1316 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.w_sign_fixup, fixup.w_source);
1317 if (--remaining) strcat(arguments, ", ");
1320 if (mask_size > 1)
1322 shader_addline(arg->buffer, "%s%s = vec%u(%s);\n",
1323 dst_param.reg_name, dst_param.mask_str, mask_size, arguments);
1325 else
1327 shader_addline(arg->buffer, "%s%s = %s;\n", dst_param.reg_name, dst_param.mask_str, arguments);
1331 /*****************************************************************************
1333 * Begin processing individual instruction opcodes
1335 ****************************************************************************/
1337 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
1338 static void shader_glsl_arith(const SHADER_OPCODE_ARG *arg)
1340 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1341 SHADER_BUFFER* buffer = arg->buffer;
1342 glsl_src_param_t src0_param;
1343 glsl_src_param_t src1_param;
1344 DWORD write_mask;
1345 char op;
1347 /* Determine the GLSL operator to use based on the opcode */
1348 switch (curOpcode->opcode) {
1349 case WINED3DSIO_MUL: op = '*'; break;
1350 case WINED3DSIO_ADD: op = '+'; break;
1351 case WINED3DSIO_SUB: op = '-'; break;
1352 default:
1353 op = ' ';
1354 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1355 break;
1358 write_mask = shader_glsl_append_dst(buffer, arg);
1359 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1360 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1361 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
1364 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1365 static void shader_glsl_mov(const SHADER_OPCODE_ARG *arg)
1367 SHADER_BUFFER* buffer = arg->buffer;
1368 glsl_src_param_t src0_param;
1369 DWORD write_mask;
1371 write_mask = shader_glsl_append_dst(buffer, arg);
1372 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1374 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1375 * shader versions WINED3DSIO_MOVA is used for this. */
1376 if ((WINED3DSHADER_VERSION_MAJOR(arg->reg_maps->shader_version) == 1
1377 && !shader_is_pshader_version(arg->reg_maps->shader_version)
1378 && shader_get_regtype(arg->dst) == WINED3DSPR_ADDR))
1380 /* This is a simple floor() */
1381 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1382 if (mask_size > 1) {
1383 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
1384 } else {
1385 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
1387 } else if(arg->opcode->opcode == WINED3DSIO_MOVA) {
1388 /* We need to *round* to the nearest int here. */
1389 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1390 if (mask_size > 1) {
1391 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);
1392 } else {
1393 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param.param_str, src0_param.param_str);
1395 } else {
1396 shader_addline(buffer, "%s);\n", src0_param.param_str);
1400 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
1401 static void shader_glsl_dot(const SHADER_OPCODE_ARG *arg)
1403 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1404 SHADER_BUFFER* buffer = arg->buffer;
1405 glsl_src_param_t src0_param;
1406 glsl_src_param_t src1_param;
1407 DWORD dst_write_mask, src_write_mask;
1408 unsigned int dst_size = 0;
1410 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1411 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1413 /* dp3 works on vec3, dp4 on vec4 */
1414 if (curOpcode->opcode == WINED3DSIO_DP4) {
1415 src_write_mask = WINED3DSP_WRITEMASK_ALL;
1416 } else {
1417 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1420 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_write_mask, &src0_param);
1421 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_write_mask, &src1_param);
1423 if (dst_size > 1) {
1424 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1425 } else {
1426 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
1430 /* Note that this instruction has some restrictions. The destination write mask
1431 * can't contain the w component, and the source swizzles have to be .xyzw */
1432 static void shader_glsl_cross(const SHADER_OPCODE_ARG *arg)
1434 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1435 glsl_src_param_t src0_param;
1436 glsl_src_param_t src1_param;
1437 char dst_mask[6];
1439 shader_glsl_get_write_mask(arg->dst, dst_mask);
1440 shader_glsl_append_dst(arg->buffer, arg);
1441 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1442 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
1443 shader_addline(arg->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
1446 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1447 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1448 * GLSL uses the value as-is. */
1449 static void shader_glsl_pow(const SHADER_OPCODE_ARG *arg)
1451 SHADER_BUFFER *buffer = arg->buffer;
1452 glsl_src_param_t src0_param;
1453 glsl_src_param_t src1_param;
1454 DWORD dst_write_mask;
1455 unsigned int dst_size;
1457 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1458 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1460 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1461 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1463 if (dst_size > 1) {
1464 shader_addline(buffer, "vec%d(pow(abs(%s), %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1465 } else {
1466 shader_addline(buffer, "pow(abs(%s), %s));\n", src0_param.param_str, src1_param.param_str);
1470 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
1471 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
1472 * GLSL uses the value as-is. */
1473 static void shader_glsl_log(const SHADER_OPCODE_ARG *arg)
1475 SHADER_BUFFER *buffer = arg->buffer;
1476 glsl_src_param_t src0_param;
1477 DWORD dst_write_mask;
1478 unsigned int dst_size;
1480 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1481 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1483 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1485 if (dst_size > 1) {
1486 shader_addline(buffer, "vec%d(log2(abs(%s))));\n", dst_size, src0_param.param_str);
1487 } else {
1488 shader_addline(buffer, "log2(abs(%s)));\n", src0_param.param_str);
1492 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1493 static void shader_glsl_map2gl(const SHADER_OPCODE_ARG *arg)
1495 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1496 SHADER_BUFFER* buffer = arg->buffer;
1497 glsl_src_param_t src_param;
1498 const char *instruction;
1499 char arguments[256];
1500 DWORD write_mask;
1501 unsigned i;
1503 /* Determine the GLSL function to use based on the opcode */
1504 /* TODO: Possibly make this a table for faster lookups */
1505 switch (curOpcode->opcode) {
1506 case WINED3DSIO_MIN: instruction = "min"; break;
1507 case WINED3DSIO_MAX: instruction = "max"; break;
1508 case WINED3DSIO_ABS: instruction = "abs"; break;
1509 case WINED3DSIO_FRC: instruction = "fract"; break;
1510 case WINED3DSIO_NRM: instruction = "normalize"; break;
1511 case WINED3DSIO_EXP: instruction = "exp2"; break;
1512 case WINED3DSIO_SGN: instruction = "sign"; break;
1513 case WINED3DSIO_DSX: instruction = "dFdx"; break;
1514 case WINED3DSIO_DSY: instruction = "ycorrection.y * dFdy"; break;
1515 default: instruction = "";
1516 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1517 break;
1520 write_mask = shader_glsl_append_dst(buffer, arg);
1522 arguments[0] = '\0';
1523 if (curOpcode->num_params > 0) {
1524 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src_param);
1525 strcat(arguments, src_param.param_str);
1526 for (i = 2; i < curOpcode->num_params; ++i) {
1527 strcat(arguments, ", ");
1528 shader_glsl_add_src_param(arg, arg->src[i-1], arg->src_addr[i-1], write_mask, &src_param);
1529 strcat(arguments, src_param.param_str);
1533 shader_addline(buffer, "%s(%s));\n", instruction, arguments);
1536 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1537 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1538 * dst.x = 2^(floor(src))
1539 * dst.y = src - floor(src)
1540 * dst.z = 2^src (partial precision is allowed, but optional)
1541 * dst.w = 1.0;
1542 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1543 * dst = 2^src; (partial precision is allowed, but optional)
1545 static void shader_glsl_expp(const SHADER_OPCODE_ARG *arg)
1547 glsl_src_param_t src_param;
1549 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src_param);
1551 if (arg->reg_maps->shader_version < WINED3DPS_VERSION(2,0))
1553 char dst_mask[6];
1555 shader_addline(arg->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
1556 shader_addline(arg->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
1557 shader_addline(arg->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
1558 shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1560 shader_glsl_append_dst(arg->buffer, arg);
1561 shader_glsl_get_write_mask(arg->dst, dst_mask);
1562 shader_addline(arg->buffer, "tmp0%s);\n", dst_mask);
1563 } else {
1564 DWORD write_mask;
1565 unsigned int mask_size;
1567 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1568 mask_size = shader_glsl_get_write_mask_size(write_mask);
1570 if (mask_size > 1) {
1571 shader_addline(arg->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
1572 } else {
1573 shader_addline(arg->buffer, "exp2(%s));\n", src_param.param_str);
1578 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1579 static void shader_glsl_rcp(const SHADER_OPCODE_ARG *arg)
1581 glsl_src_param_t src_param;
1582 DWORD write_mask;
1583 unsigned int mask_size;
1585 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1586 mask_size = shader_glsl_get_write_mask_size(write_mask);
1587 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1589 if (mask_size > 1) {
1590 shader_addline(arg->buffer, "vec%d(1.0 / %s));\n", mask_size, src_param.param_str);
1591 } else {
1592 shader_addline(arg->buffer, "1.0 / %s);\n", src_param.param_str);
1596 static void shader_glsl_rsq(const SHADER_OPCODE_ARG *arg)
1598 SHADER_BUFFER* buffer = arg->buffer;
1599 glsl_src_param_t src_param;
1600 DWORD write_mask;
1601 unsigned int mask_size;
1603 write_mask = shader_glsl_append_dst(buffer, arg);
1604 mask_size = shader_glsl_get_write_mask_size(write_mask);
1606 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1608 if (mask_size > 1) {
1609 shader_addline(buffer, "vec%d(inversesqrt(%s)));\n", mask_size, src_param.param_str);
1610 } else {
1611 shader_addline(buffer, "inversesqrt(%s));\n", src_param.param_str);
1615 /** Process signed comparison opcodes in GLSL. */
1616 static void shader_glsl_compare(const SHADER_OPCODE_ARG *arg)
1618 glsl_src_param_t src0_param;
1619 glsl_src_param_t src1_param;
1620 DWORD write_mask;
1621 unsigned int mask_size;
1623 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1624 mask_size = shader_glsl_get_write_mask_size(write_mask);
1625 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1626 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1628 if (mask_size > 1) {
1629 const char *compare;
1631 switch(arg->opcode->opcode) {
1632 case WINED3DSIO_SLT: compare = "lessThan"; break;
1633 case WINED3DSIO_SGE: compare = "greaterThanEqual"; break;
1634 default: compare = "";
1635 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1638 shader_addline(arg->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
1639 src0_param.param_str, src1_param.param_str);
1640 } else {
1641 switch(arg->opcode->opcode) {
1642 case WINED3DSIO_SLT:
1643 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
1644 * to return 0.0 but step returns 1.0 because step is not < x
1645 * An alternative is a bvec compare padded with an unused second component.
1646 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
1647 * issue. Playing with not() is not possible either because not() does not accept
1648 * a scalar.
1650 shader_addline(arg->buffer, "(%s < %s) ? 1.0 : 0.0);\n", src0_param.param_str, src1_param.param_str);
1651 break;
1652 case WINED3DSIO_SGE:
1653 /* Here we can use the step() function and safe a conditional */
1654 shader_addline(arg->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
1655 break;
1656 default:
1657 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1663 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1664 static void shader_glsl_cmp(const SHADER_OPCODE_ARG *arg)
1666 glsl_src_param_t src0_param;
1667 glsl_src_param_t src1_param;
1668 glsl_src_param_t src2_param;
1669 DWORD write_mask, cmp_channel = 0;
1670 unsigned int i, j;
1671 char mask_char[6];
1672 BOOL temp_destination = FALSE;
1674 if(shader_is_scalar(arg->src[0])) {
1675 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1677 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
1678 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1679 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1681 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1682 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1683 } else {
1684 DWORD src0reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
1685 DWORD src1reg = arg->src[1] & WINED3DSP_REGNUM_MASK;
1686 DWORD src2reg = arg->src[2] & WINED3DSP_REGNUM_MASK;
1687 DWORD src0regtype = shader_get_regtype(arg->src[0]);
1688 DWORD src1regtype = shader_get_regtype(arg->src[1]);
1689 DWORD src2regtype = shader_get_regtype(arg->src[2]);
1690 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1691 DWORD dstregtype = shader_get_regtype(arg->dst);
1693 /* Cycle through all source0 channels */
1694 for (i=0; i<4; i++) {
1695 write_mask = 0;
1696 /* Find the destination channels which use the current source0 channel */
1697 for (j=0; j<4; j++) {
1698 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1699 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1700 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1704 /* Splitting the cmp instruction up in multiple lines imposes a problem:
1705 * The first lines may overwrite source parameters of the following lines.
1706 * Deal with that by using a temporary destination register if needed
1708 if((src0reg == dstreg && src0regtype == dstregtype) ||
1709 (src1reg == dstreg && src1regtype == dstregtype) ||
1710 (src2reg == dstreg && src2regtype == dstregtype)) {
1712 write_mask = shader_glsl_get_write_mask(arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask), mask_char);
1713 if (!write_mask) continue;
1714 shader_addline(arg->buffer, "tmp0%s = (", mask_char);
1715 temp_destination = TRUE;
1716 } else {
1717 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1718 if (!write_mask) continue;
1721 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1722 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1723 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1725 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1726 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1729 if(temp_destination) {
1730 shader_glsl_get_write_mask(arg->dst, mask_char);
1731 shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst);
1732 shader_addline(arg->buffer, "tmp0%s);\n", mask_char);
1738 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
1739 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1740 * the compare is done per component of src0. */
1741 static void shader_glsl_cnd(const SHADER_OPCODE_ARG *arg)
1743 glsl_src_param_t src0_param;
1744 glsl_src_param_t src1_param;
1745 glsl_src_param_t src2_param;
1746 DWORD write_mask, cmp_channel = 0;
1747 unsigned int i, j;
1749 if (arg->reg_maps->shader_version < WINED3DPS_VERSION(1, 4))
1751 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1752 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1753 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1754 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1756 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
1757 if(arg->opcode_token & WINED3DSI_COISSUE) {
1758 shader_addline(arg->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
1759 } else {
1760 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1761 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1763 return;
1765 /* Cycle through all source0 channels */
1766 for (i=0; i<4; i++) {
1767 write_mask = 0;
1768 /* Find the destination channels which use the current source0 channel */
1769 for (j=0; j<4; j++) {
1770 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1771 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1772 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1775 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1776 if (!write_mask) continue;
1778 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1779 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1780 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1782 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1783 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1787 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1788 static void shader_glsl_mad(const SHADER_OPCODE_ARG *arg)
1790 glsl_src_param_t src0_param;
1791 glsl_src_param_t src1_param;
1792 glsl_src_param_t src2_param;
1793 DWORD write_mask;
1795 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1796 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1797 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1798 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1799 shader_addline(arg->buffer, "(%s * %s) + %s);\n",
1800 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1803 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1804 Vertex shaders to GLSL codes */
1805 static void shader_glsl_mnxn(const SHADER_OPCODE_ARG *arg)
1807 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)arg->shader;
1808 const SHADER_OPCODE *opcode_table = shader->baseShader.shader_ins;
1809 DWORD shader_version = arg->reg_maps->shader_version;
1810 int i;
1811 int nComponents = 0;
1812 SHADER_OPCODE_ARG tmpArg;
1814 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1816 /* Set constants for the temporary argument */
1817 tmpArg.shader = arg->shader;
1818 tmpArg.buffer = arg->buffer;
1819 tmpArg.src[0] = arg->src[0];
1820 tmpArg.src_addr[0] = arg->src_addr[0];
1821 tmpArg.src_addr[1] = arg->src_addr[1];
1822 tmpArg.reg_maps = arg->reg_maps;
1824 switch(arg->opcode->opcode) {
1825 case WINED3DSIO_M4x4:
1826 nComponents = 4;
1827 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP4);
1828 break;
1829 case WINED3DSIO_M4x3:
1830 nComponents = 3;
1831 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP4);
1832 break;
1833 case WINED3DSIO_M3x4:
1834 nComponents = 4;
1835 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP3);
1836 break;
1837 case WINED3DSIO_M3x3:
1838 nComponents = 3;
1839 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP3);
1840 break;
1841 case WINED3DSIO_M3x2:
1842 nComponents = 2;
1843 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP3);
1844 break;
1845 default:
1846 break;
1849 for (i = 0; i < nComponents; i++) {
1850 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1851 tmpArg.src[1] = arg->src[1]+i;
1852 shader_glsl_dot(&tmpArg);
1857 The LRP instruction performs a component-wise linear interpolation
1858 between the second and third operands using the first operand as the
1859 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
1860 This is equivalent to mix(src2, src1, src0);
1862 static void shader_glsl_lrp(const SHADER_OPCODE_ARG *arg)
1864 glsl_src_param_t src0_param;
1865 glsl_src_param_t src1_param;
1866 glsl_src_param_t src2_param;
1867 DWORD write_mask;
1869 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1871 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1872 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1873 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1875 shader_addline(arg->buffer, "mix(%s, %s, %s));\n",
1876 src2_param.param_str, src1_param.param_str, src0_param.param_str);
1879 /** Process the WINED3DSIO_LIT instruction in GLSL:
1880 * dst.x = dst.w = 1.0
1881 * dst.y = (src0.x > 0) ? src0.x
1882 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1883 * where src.w is clamped at +- 128
1885 static void shader_glsl_lit(const SHADER_OPCODE_ARG *arg)
1887 glsl_src_param_t src0_param;
1888 glsl_src_param_t src1_param;
1889 glsl_src_param_t src3_param;
1890 char dst_mask[6];
1892 shader_glsl_append_dst(arg->buffer, arg);
1893 shader_glsl_get_write_mask(arg->dst, dst_mask);
1895 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1896 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src1_param);
1897 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src3_param);
1899 /* The sdk specifies the instruction like this
1900 * dst.x = 1.0;
1901 * if(src.x > 0.0) dst.y = src.x
1902 * else dst.y = 0.0.
1903 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
1904 * else dst.z = 0.0;
1905 * dst.w = 1.0;
1907 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
1908 * dst.x = 1.0 ... No further explanation needed
1909 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
1910 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
1911 * dst.w = 1.0. ... Nothing fancy.
1913 * So we still have one conditional in there. So do this:
1914 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
1916 * 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),
1917 * 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.
1918 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
1920 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",
1921 src0_param.param_str, src1_param.param_str, src0_param.param_str, src3_param.param_str, dst_mask);
1924 /** Process the WINED3DSIO_DST instruction in GLSL:
1925 * dst.x = 1.0
1926 * dst.y = src0.x * src0.y
1927 * dst.z = src0.z
1928 * dst.w = src1.w
1930 static void shader_glsl_dst(const SHADER_OPCODE_ARG *arg)
1932 glsl_src_param_t src0y_param;
1933 glsl_src_param_t src0z_param;
1934 glsl_src_param_t src1y_param;
1935 glsl_src_param_t src1w_param;
1936 char dst_mask[6];
1938 shader_glsl_append_dst(arg->buffer, arg);
1939 shader_glsl_get_write_mask(arg->dst, dst_mask);
1941 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src0y_param);
1942 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &src0z_param);
1943 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_1, &src1y_param);
1944 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_3, &src1w_param);
1946 shader_addline(arg->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
1947 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
1950 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1951 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1952 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1954 * dst.x = cos(src0.?)
1955 * dst.y = sin(src0.?)
1956 * dst.z = dst.z
1957 * dst.w = dst.w
1959 static void shader_glsl_sincos(const SHADER_OPCODE_ARG *arg)
1961 glsl_src_param_t src0_param;
1962 DWORD write_mask;
1964 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1965 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1967 switch (write_mask) {
1968 case WINED3DSP_WRITEMASK_0:
1969 shader_addline(arg->buffer, "cos(%s));\n", src0_param.param_str);
1970 break;
1972 case WINED3DSP_WRITEMASK_1:
1973 shader_addline(arg->buffer, "sin(%s));\n", src0_param.param_str);
1974 break;
1976 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
1977 shader_addline(arg->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
1978 break;
1980 default:
1981 ERR("Write mask should be .x, .y or .xy\n");
1982 break;
1986 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1987 * Start a for() loop where src1.y is the initial value of aL,
1988 * increment aL by src1.z for a total of src1.x iterations.
1989 * Need to use a temporary variable for this operation.
1991 /* FIXME: I don't think nested loops will work correctly this way. */
1992 static void shader_glsl_loop(const SHADER_OPCODE_ARG *arg)
1994 glsl_src_param_t src1_param;
1995 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1996 DWORD regtype = shader_get_regtype(arg->src[1]);
1997 DWORD reg = arg->src[1] & WINED3DSP_REGNUM_MASK;
1998 const DWORD *control_values = NULL;
1999 const local_constant *constant;
2001 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
2003 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
2004 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
2005 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
2006 * addressing.
2008 if(regtype == WINED3DSPR_CONSTINT) {
2009 LIST_FOR_EACH_ENTRY(constant, &shader->baseShader.constantsI, local_constant, entry) {
2010 if(constant->idx == reg) {
2011 control_values = constant->value;
2012 break;
2017 if(control_values) {
2018 if(control_values[2] > 0) {
2019 shader_addline(arg->buffer, "for (aL%u = %d; aL%u < (%d * %d + %d); aL%u += %d) {\n",
2020 shader->baseShader.cur_loop_depth, control_values[1],
2021 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
2022 shader->baseShader.cur_loop_depth, control_values[2]);
2023 } else if(control_values[2] == 0) {
2024 shader_addline(arg->buffer, "for (aL%u = %d, tmpInt%u = 0; tmpInt%u < %d; tmpInt%u++) {\n",
2025 shader->baseShader.cur_loop_depth, control_values[1], shader->baseShader.cur_loop_depth,
2026 shader->baseShader.cur_loop_depth, control_values[0],
2027 shader->baseShader.cur_loop_depth);
2028 } else {
2029 shader_addline(arg->buffer, "for (aL%u = %d; aL%u > (%d * %d + %d); aL%u += %d) {\n",
2030 shader->baseShader.cur_loop_depth, control_values[1],
2031 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
2032 shader->baseShader.cur_loop_depth, control_values[2]);
2034 } else {
2035 shader_addline(arg->buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
2036 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno,
2037 src1_param.reg_name, shader->baseShader.cur_loop_depth, src1_param.reg_name,
2038 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno, src1_param.reg_name);
2041 shader->baseShader.cur_loop_depth++;
2042 shader->baseShader.cur_loop_regno++;
2045 static void shader_glsl_end(const SHADER_OPCODE_ARG *arg)
2047 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
2049 shader_addline(arg->buffer, "}\n");
2051 if(arg->opcode->opcode == WINED3DSIO_ENDLOOP) {
2052 shader->baseShader.cur_loop_depth--;
2053 shader->baseShader.cur_loop_regno--;
2055 if(arg->opcode->opcode == WINED3DSIO_ENDREP) {
2056 shader->baseShader.cur_loop_depth--;
2060 static void shader_glsl_rep(const SHADER_OPCODE_ARG *arg)
2062 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
2063 glsl_src_param_t src0_param;
2065 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2066 shader_addline(arg->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2067 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_depth,
2068 src0_param.param_str, shader->baseShader.cur_loop_depth);
2069 shader->baseShader.cur_loop_depth++;
2072 static void shader_glsl_if(const SHADER_OPCODE_ARG *arg)
2074 glsl_src_param_t src0_param;
2076 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2077 shader_addline(arg->buffer, "if (%s) {\n", src0_param.param_str);
2080 static void shader_glsl_ifc(const SHADER_OPCODE_ARG *arg)
2082 glsl_src_param_t src0_param;
2083 glsl_src_param_t src1_param;
2085 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2086 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2088 shader_addline(arg->buffer, "if (%s %s %s) {\n",
2089 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
2092 static void shader_glsl_else(const SHADER_OPCODE_ARG *arg)
2094 shader_addline(arg->buffer, "} else {\n");
2097 static void shader_glsl_break(const SHADER_OPCODE_ARG *arg)
2099 shader_addline(arg->buffer, "break;\n");
2102 /* FIXME: According to MSDN the compare is done per component. */
2103 static void shader_glsl_breakc(const SHADER_OPCODE_ARG *arg)
2105 glsl_src_param_t src0_param;
2106 glsl_src_param_t src1_param;
2108 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2109 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2111 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
2112 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
2115 static void shader_glsl_label(const SHADER_OPCODE_ARG *arg)
2118 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2119 shader_addline(arg->buffer, "}\n");
2120 shader_addline(arg->buffer, "void subroutine%u () {\n", snum);
2123 static void shader_glsl_call(const SHADER_OPCODE_ARG *arg)
2125 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2126 shader_addline(arg->buffer, "subroutine%u();\n", snum);
2129 static void shader_glsl_callnz(const SHADER_OPCODE_ARG *arg)
2131 glsl_src_param_t src1_param;
2133 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2134 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2135 shader_addline(arg->buffer, "if (%s) subroutine%u();\n", src1_param.param_str, snum);
2138 /*********************************************
2139 * Pixel Shader Specific Code begins here
2140 ********************************************/
2141 static void pshader_glsl_tex(const SHADER_OPCODE_ARG *arg)
2143 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2144 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2145 DWORD shader_version = arg->reg_maps->shader_version;
2146 char dst_swizzle[6];
2147 glsl_sample_function_t sample_function;
2148 DWORD sampler_type;
2149 DWORD sampler_idx;
2150 BOOL projected, texrect = FALSE;
2151 DWORD mask = 0;
2153 /* All versions have a destination register */
2154 shader_glsl_append_dst(arg->buffer, arg);
2156 /* 1.0-1.4: Use destination register as sampler source.
2157 * 2.0+: Use provided sampler source. */
2158 if (shader_version < WINED3DPS_VERSION(2,0)) sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2159 else sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
2160 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2162 if (shader_version < WINED3DPS_VERSION(1,4))
2164 DWORD flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2166 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
2167 if (flags & WINED3DTTFF_PROJECTED && sampler_type != WINED3DSTT_CUBE) {
2168 projected = TRUE;
2169 switch (flags & ~WINED3DTTFF_PROJECTED) {
2170 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2171 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
2172 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
2173 case WINED3DTTFF_COUNT4:
2174 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
2176 } else {
2177 projected = FALSE;
2180 else if (shader_version < WINED3DPS_VERSION(2,0))
2182 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2184 if (src_mod == WINED3DSPSM_DZ) {
2185 projected = TRUE;
2186 mask = WINED3DSP_WRITEMASK_2;
2187 } else if (src_mod == WINED3DSPSM_DW) {
2188 projected = TRUE;
2189 mask = WINED3DSP_WRITEMASK_3;
2190 } else {
2191 projected = FALSE;
2193 } else {
2194 if(arg->opcode_token & WINED3DSI_TEXLD_PROJECT) {
2195 /* ps 2.0 texldp instruction always divides by the fourth component. */
2196 projected = TRUE;
2197 mask = WINED3DSP_WRITEMASK_3;
2198 } else {
2199 projected = FALSE;
2203 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2204 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2205 texrect = TRUE;
2208 shader_glsl_get_sample_function(sampler_type, projected, texrect, &sample_function);
2209 mask |= sample_function.coord_mask;
2211 if (shader_version < WINED3DPS_VERSION(2,0)) shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2212 else shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
2214 /* 1.0-1.3: Use destination register as coordinate source.
2215 1.4+: Use provided coordinate source register. */
2216 if (shader_version < WINED3DPS_VERSION(1,4))
2218 char coord_mask[6];
2219 shader_glsl_get_write_mask(mask, coord_mask);
2220 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s)%s);\n",
2221 sample_function.name, sampler_idx, sampler_idx, coord_mask, dst_swizzle);
2222 } else {
2223 glsl_src_param_t coord_param;
2224 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], mask, &coord_param);
2225 if(arg->opcode_token & WINED3DSI_TEXLD_BIAS) {
2226 glsl_src_param_t bias;
2227 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &bias);
2229 shader_addline(arg->buffer, "%s(Psampler%u, %s, %s)%s);\n",
2230 sample_function.name, sampler_idx, coord_param.param_str,
2231 bias.param_str, dst_swizzle);
2232 } else {
2233 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n",
2234 sample_function.name, sampler_idx, coord_param.param_str, dst_swizzle);
2239 static void shader_glsl_texldl(const SHADER_OPCODE_ARG *arg)
2241 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*)arg->shader;
2242 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2243 glsl_sample_function_t sample_function;
2244 glsl_src_param_t coord_param, lod_param;
2245 char dst_swizzle[6];
2246 DWORD sampler_type;
2247 DWORD sampler_idx;
2248 BOOL texrect = FALSE;
2250 shader_glsl_append_dst(arg->buffer, arg);
2251 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
2253 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
2254 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2255 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2256 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2257 texrect = TRUE;
2259 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);
2261 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &lod_param);
2263 if (shader_is_pshader_version(arg->reg_maps->shader_version))
2265 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
2266 * However, they seem to work just fine in fragment shaders as well. */
2267 WARN("Using %sLod in fragment shader.\n", sample_function.name);
2268 shader_addline(arg->buffer, "%sLod(Psampler%u, %s, %s)%s);\n",
2269 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2270 } else {
2271 shader_addline(arg->buffer, "%sLod(Vsampler%u, %s, %s)%s);\n",
2272 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2276 static void pshader_glsl_texcoord(const SHADER_OPCODE_ARG *arg)
2278 /* FIXME: Make this work for more than just 2D textures */
2279 SHADER_BUFFER* buffer = arg->buffer;
2280 DWORD write_mask;
2281 char dst_mask[6];
2283 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2284 shader_glsl_get_write_mask(write_mask, dst_mask);
2286 if (arg->reg_maps->shader_version != WINED3DPS_VERSION(1,4))
2288 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2289 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n", reg, dst_mask);
2290 } else {
2291 DWORD reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
2292 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2293 char dst_swizzle[6];
2295 shader_glsl_get_swizzle(arg->src[0], FALSE, write_mask, dst_swizzle);
2297 if (src_mod == WINED3DSPSM_DZ) {
2298 glsl_src_param_t div_param;
2299 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2300 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &div_param);
2302 if (mask_size > 1) {
2303 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2304 } else {
2305 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2307 } else if (src_mod == WINED3DSPSM_DW) {
2308 glsl_src_param_t div_param;
2309 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2310 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &div_param);
2312 if (mask_size > 1) {
2313 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2314 } else {
2315 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2317 } else {
2318 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
2323 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
2324 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2325 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2326 static void pshader_glsl_texdp3tex(const SHADER_OPCODE_ARG *arg)
2328 glsl_src_param_t src0_param;
2329 char dst_mask[6];
2330 glsl_sample_function_t sample_function;
2331 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2332 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2333 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2335 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2337 shader_glsl_append_dst(arg->buffer, arg);
2338 shader_glsl_get_write_mask(arg->dst, dst_mask);
2340 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
2341 * scalar, and projected sampling would require 4.
2343 * It is a dependent read - not valid with conditional NP2 textures
2345 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2347 switch(count_bits(sample_function.coord_mask)) {
2348 case 1:
2349 shader_addline(arg->buffer, "%s(Psampler%u, dot(gl_TexCoord[%u].xyz, %s))%s);\n",
2350 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2351 break;
2353 case 2:
2354 shader_addline(arg->buffer, "%s(Psampler%u, vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0))%s);\n",
2355 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2356 break;
2358 case 3:
2359 shader_addline(arg->buffer, "%s(Psampler%u, vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0))%s);\n",
2360 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2361 break;
2362 default:
2363 FIXME("Unexpected mask bitcount %d\n", count_bits(sample_function.coord_mask));
2367 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2368 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2369 static void pshader_glsl_texdp3(const SHADER_OPCODE_ARG *arg)
2371 glsl_src_param_t src0_param;
2372 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2373 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2374 DWORD dst_mask;
2375 unsigned int mask_size;
2377 dst_mask = shader_glsl_append_dst(arg->buffer, arg);
2378 mask_size = shader_glsl_get_write_mask_size(dst_mask);
2379 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2381 if (mask_size > 1) {
2382 shader_addline(arg->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
2383 } else {
2384 shader_addline(arg->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
2388 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2389 * Calculate the depth as dst.x / dst.y */
2390 static void pshader_glsl_texdepth(const SHADER_OPCODE_ARG *arg)
2392 glsl_dst_param_t dst_param;
2394 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2396 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2397 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2398 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2399 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2400 * >= 1.0 or < 0.0
2402 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);
2405 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2406 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2407 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2408 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2410 static void pshader_glsl_texm3x2depth(const SHADER_OPCODE_ARG *arg)
2412 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2413 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2414 glsl_src_param_t src0_param;
2416 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2418 shader_addline(arg->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
2419 shader_addline(arg->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2422 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2423 * Calculate the 1st of a 2-row matrix multiplication. */
2424 static void pshader_glsl_texm3x2pad(const SHADER_OPCODE_ARG *arg)
2426 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2427 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2428 SHADER_BUFFER* buffer = arg->buffer;
2429 glsl_src_param_t src0_param;
2431 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2432 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2435 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2436 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2437 static void pshader_glsl_texm3x3pad(const SHADER_OPCODE_ARG* arg)
2439 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2440 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2441 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2442 SHADER_BUFFER* buffer = arg->buffer;
2443 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2444 glsl_src_param_t src0_param;
2446 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2447 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
2448 current_state->texcoord_w[current_state->current_row++] = reg;
2451 static void pshader_glsl_texm3x2tex(const SHADER_OPCODE_ARG *arg)
2453 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2454 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2455 SHADER_BUFFER* buffer = arg->buffer;
2456 glsl_src_param_t src0_param;
2457 char dst_mask[6];
2459 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2460 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2462 shader_glsl_append_dst(buffer, arg);
2463 shader_glsl_get_write_mask(arg->dst, dst_mask);
2465 /* Sample the texture using the calculated coordinates */
2466 shader_addline(buffer, "texture2D(Psampler%u, tmp0.xy)%s);\n", reg, dst_mask);
2469 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2470 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2471 static void pshader_glsl_texm3x3tex(const SHADER_OPCODE_ARG *arg)
2473 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2474 glsl_src_param_t src0_param;
2475 char dst_mask[6];
2476 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2477 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2478 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2479 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2480 glsl_sample_function_t sample_function;
2482 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2483 shader_addline(arg->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2485 shader_glsl_append_dst(arg->buffer, arg);
2486 shader_glsl_get_write_mask(arg->dst, dst_mask);
2487 /* Dependent read, not valid with conditional NP2 */
2488 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2490 /* Sample the texture using the calculated coordinates */
2491 shader_addline(arg->buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2493 current_state->current_row = 0;
2496 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2497 * Perform the 3rd row of a 3x3 matrix multiply */
2498 static void pshader_glsl_texm3x3(const SHADER_OPCODE_ARG *arg)
2500 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2501 glsl_src_param_t src0_param;
2502 char dst_mask[6];
2503 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2504 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2505 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2507 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2509 shader_glsl_append_dst(arg->buffer, arg);
2510 shader_glsl_get_write_mask(arg->dst, dst_mask);
2511 shader_addline(arg->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
2513 current_state->current_row = 0;
2516 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
2517 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2518 static void pshader_glsl_texm3x3spec(const SHADER_OPCODE_ARG *arg)
2520 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2521 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2522 glsl_src_param_t src0_param;
2523 glsl_src_param_t src1_param;
2524 char dst_mask[6];
2525 SHADER_BUFFER* buffer = arg->buffer;
2526 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2527 DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2528 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2529 glsl_sample_function_t sample_function;
2531 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2532 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
2534 /* Perform the last matrix multiply operation */
2535 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2536 /* Reflection calculation */
2537 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
2539 shader_glsl_append_dst(buffer, arg);
2540 shader_glsl_get_write_mask(arg->dst, dst_mask);
2541 /* Dependent read, not valid with conditional NP2 */
2542 shader_glsl_get_sample_function(stype, FALSE, FALSE, &sample_function);
2544 /* Sample the texture */
2545 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2547 current_state->current_row = 0;
2550 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
2551 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2552 static void pshader_glsl_texm3x3vspec(const SHADER_OPCODE_ARG *arg)
2554 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2555 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2556 SHADER_BUFFER* buffer = arg->buffer;
2557 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2558 glsl_src_param_t src0_param;
2559 char dst_mask[6];
2560 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2561 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2562 glsl_sample_function_t sample_function;
2564 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2566 /* Perform the last matrix multiply operation */
2567 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
2569 /* Construct the eye-ray vector from w coordinates */
2570 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
2571 current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
2572 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
2574 shader_glsl_append_dst(buffer, arg);
2575 shader_glsl_get_write_mask(arg->dst, dst_mask);
2576 /* Dependent read, not valid with conditional NP2 */
2577 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2579 /* Sample the texture using the calculated coordinates */
2580 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2582 current_state->current_row = 0;
2585 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
2586 * Apply a fake bump map transform.
2587 * texbem is pshader <= 1.3 only, this saves a few version checks
2589 static void pshader_glsl_texbem(const SHADER_OPCODE_ARG *arg)
2591 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2592 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2593 char dst_swizzle[6];
2594 glsl_sample_function_t sample_function;
2595 glsl_src_param_t coord_param;
2596 DWORD sampler_type;
2597 DWORD sampler_idx;
2598 DWORD mask;
2599 DWORD flags;
2600 char coord_mask[6];
2602 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2603 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2605 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2606 /* Dependent read, not valid with conditional NP2 */
2607 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2608 mask = sample_function.coord_mask;
2610 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2612 shader_glsl_get_write_mask(mask, coord_mask);
2614 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2615 * so we can't let the GL handle this.
2617 if (flags & WINED3DTTFF_PROJECTED) {
2618 DWORD div_mask=0;
2619 char coord_div_mask[3];
2620 switch (flags & ~WINED3DTTFF_PROJECTED) {
2621 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2622 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
2623 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
2624 case WINED3DTTFF_COUNT4:
2625 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
2627 shader_glsl_get_write_mask(div_mask, coord_div_mask);
2628 shader_addline(arg->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
2631 shader_glsl_append_dst(arg->buffer, arg);
2632 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &coord_param);
2633 if(arg->opcode->opcode == WINED3DSIO_TEXBEML) {
2634 glsl_src_param_t luminance_param;
2635 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &luminance_param);
2636 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",
2637 sample_function.name, sampler_idx, sampler_idx, coord_mask, sampler_idx, coord_param.param_str, coord_mask,
2638 luminance_param.param_str, sampler_idx, sampler_idx, dst_swizzle);
2639 } else {
2640 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s )%s);\n",
2641 sample_function.name, sampler_idx, sampler_idx, coord_mask, sampler_idx, coord_param.param_str, coord_mask, dst_swizzle);
2645 static void pshader_glsl_bem(const SHADER_OPCODE_ARG *arg)
2647 glsl_src_param_t src0_param, src1_param;
2648 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2650 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src0_param);
2651 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src1_param);
2653 shader_glsl_append_dst(arg->buffer, arg);
2654 shader_addline(arg->buffer, "%s + bumpenvmat%d * %s);\n",
2655 src0_param.param_str, sampler_idx, src1_param.param_str);
2658 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
2659 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
2660 static void pshader_glsl_texreg2ar(const SHADER_OPCODE_ARG *arg)
2662 glsl_src_param_t src0_param;
2663 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2664 char dst_mask[6];
2666 shader_glsl_append_dst(arg->buffer, arg);
2667 shader_glsl_get_write_mask(arg->dst, dst_mask);
2668 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2670 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2673 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
2674 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
2675 static void pshader_glsl_texreg2gb(const SHADER_OPCODE_ARG *arg)
2677 glsl_src_param_t src0_param;
2678 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2679 char dst_mask[6];
2681 shader_glsl_append_dst(arg->buffer, arg);
2682 shader_glsl_get_write_mask(arg->dst, dst_mask);
2683 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2685 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2688 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
2689 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
2690 static void pshader_glsl_texreg2rgb(const SHADER_OPCODE_ARG *arg)
2692 glsl_src_param_t src0_param;
2693 char dst_mask[6];
2694 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2695 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2696 glsl_sample_function_t sample_function;
2698 shader_glsl_append_dst(arg->buffer, arg);
2699 shader_glsl_get_write_mask(arg->dst, dst_mask);
2700 /* Dependent read, not valid with conditional NP2 */
2701 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2702 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &src0_param);
2704 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n", sample_function.name, sampler_idx, src0_param.param_str, dst_mask);
2707 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
2708 * If any of the first 3 components are < 0, discard this pixel */
2709 static void pshader_glsl_texkill(const SHADER_OPCODE_ARG *arg)
2711 glsl_dst_param_t dst_param;
2713 /* The argument is a destination parameter, and no writemasks are allowed */
2714 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2715 if ((arg->reg_maps->shader_version >= WINED3DPS_VERSION(2,0)))
2717 /* 2.0 shaders compare all 4 components in texkill */
2718 shader_addline(arg->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
2719 } else {
2720 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
2721 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
2722 * 4 components are defined, only the first 3 are used
2724 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
2728 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
2729 * dst = dot2(src0, src1) + src2 */
2730 static void pshader_glsl_dp2add(const SHADER_OPCODE_ARG *arg)
2732 glsl_src_param_t src0_param;
2733 glsl_src_param_t src1_param;
2734 glsl_src_param_t src2_param;
2735 DWORD write_mask;
2736 unsigned int mask_size;
2738 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2739 mask_size = shader_glsl_get_write_mask_size(write_mask);
2741 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
2742 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
2743 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], WINED3DSP_WRITEMASK_0, &src2_param);
2745 if (mask_size > 1) {
2746 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);
2747 } else {
2748 shader_addline(arg->buffer, "dot(%s, %s) + %s);\n", src0_param.param_str, src1_param.param_str, src2_param.param_str);
2752 static void pshader_glsl_input_pack(SHADER_BUFFER* buffer, const struct semantic* semantics_in,
2753 IWineD3DPixelShader *iface, enum vertexprocessing_mode vertexprocessing)
2755 unsigned int i;
2756 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *) iface;
2758 for (i = 0; i < MAX_REG_INPUT; i++) {
2760 DWORD usage_token = semantics_in[i].usage;
2761 DWORD register_token = semantics_in[i].reg;
2762 DWORD usage, usage_idx;
2763 char reg_mask[6];
2765 /* Uninitialized */
2766 if (!usage_token) continue;
2767 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2768 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2769 shader_glsl_get_write_mask(register_token, reg_mask);
2771 switch(usage) {
2773 case WINED3DDECLUSAGE_TEXCOORD:
2774 if(usage_idx < 8 && vertexprocessing == pretransformed) {
2775 shader_addline(buffer, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
2776 This->input_reg_map[i], reg_mask, usage_idx, reg_mask);
2777 } else {
2778 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2779 This->input_reg_map[i], reg_mask, reg_mask);
2781 break;
2783 case WINED3DDECLUSAGE_COLOR:
2784 if (usage_idx == 0)
2785 shader_addline(buffer, "IN[%u]%s = vec4(gl_Color)%s;\n",
2786 This->input_reg_map[i], reg_mask, reg_mask);
2787 else if (usage_idx == 1)
2788 shader_addline(buffer, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
2789 This->input_reg_map[i], reg_mask, reg_mask);
2790 else
2791 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2792 This->input_reg_map[i], reg_mask, reg_mask);
2793 break;
2795 default:
2796 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2797 This->input_reg_map[i], reg_mask, reg_mask);
2802 /*********************************************
2803 * Vertex Shader Specific Code begins here
2804 ********************************************/
2806 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry) {
2807 glsl_program_key_t *key;
2809 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2810 key->vshader = entry->vshader;
2811 key->pshader = entry->pshader;
2812 key->ps_args = entry->ps_args;
2814 hash_table_put(priv->glsl_program_lookup, key, entry);
2817 static struct glsl_shader_prog_link *get_glsl_program_entry(struct shader_glsl_priv *priv,
2818 GLhandleARB vshader, IWineD3DPixelShader *pshader, struct ps_compile_args *ps_args) {
2819 glsl_program_key_t key;
2821 key.vshader = vshader;
2822 key.pshader = pshader;
2823 key.ps_args = *ps_args;
2825 return (struct glsl_shader_prog_link *)hash_table_get(priv->glsl_program_lookup, &key);
2828 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const WineD3D_GL_Info *gl_info,
2829 struct glsl_shader_prog_link *entry)
2831 glsl_program_key_t *key;
2833 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2834 key->vshader = entry->vshader;
2835 key->pshader = entry->pshader;
2836 key->ps_args = entry->ps_args;
2837 hash_table_remove(priv->glsl_program_lookup, key);
2839 GL_EXTCALL(glDeleteObjectARB(entry->programId));
2840 if (entry->vshader) list_remove(&entry->vshader_entry);
2841 if (entry->pshader) list_remove(&entry->pshader_entry);
2842 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
2843 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
2844 HeapFree(GetProcessHeap(), 0, entry);
2847 static void handle_ps3_input(SHADER_BUFFER *buffer, const struct semantic *semantics_in,
2848 const struct semantic *semantics_out, const WineD3D_GL_Info *gl_info, const DWORD *map)
2850 unsigned int i, j;
2851 DWORD usage_token, usage_token_out;
2852 DWORD register_token, register_token_out;
2853 DWORD usage, usage_idx, usage_out, usage_idx_out;
2854 DWORD *set;
2855 DWORD in_idx;
2856 DWORD in_count = GL_LIMITS(glsl_varyings) / 4;
2857 char reg_mask[6], reg_mask_out[6];
2858 char destination[50];
2860 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
2862 if (!semantics_out) {
2863 /* Save gl_FrontColor & gl_FrontSecondaryColor before overwriting them. */
2864 shader_addline(buffer, "vec4 front_color = gl_FrontColor;\n");
2865 shader_addline(buffer, "vec4 front_secondary_color = gl_FrontSecondaryColor;\n");
2868 for(i = 0; i < MAX_REG_INPUT; i++) {
2869 usage_token = semantics_in[i].usage;
2870 if (!usage_token) continue;
2872 in_idx = map[i];
2873 if (in_idx >= (in_count + 2)) {
2874 FIXME("More input varyings declared than supported, expect issues\n");
2875 continue;
2876 } else if(map[i] == -1) {
2877 /* Declared, but not read register */
2878 continue;
2881 if (in_idx == in_count) {
2882 sprintf(destination, "gl_FrontColor");
2883 } else if (in_idx == in_count + 1) {
2884 sprintf(destination, "gl_FrontSecondaryColor");
2885 } else {
2886 sprintf(destination, "IN[%u]", in_idx);
2889 register_token = semantics_in[i].reg;
2891 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2892 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2893 set[map[i]] = shader_glsl_get_write_mask(register_token, reg_mask);
2895 if(!semantics_out) {
2896 switch(usage) {
2897 case WINED3DDECLUSAGE_COLOR:
2898 if (usage_idx == 0)
2899 shader_addline(buffer, "%s%s = front_color%s;\n",
2900 destination, reg_mask, reg_mask);
2901 else if (usage_idx == 1)
2902 shader_addline(buffer, "%s%s = front_secondary_color%s;\n",
2903 destination, reg_mask, reg_mask);
2904 else
2905 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2906 destination, reg_mask, reg_mask);
2907 break;
2909 case WINED3DDECLUSAGE_TEXCOORD:
2910 if (usage_idx < 8) {
2911 shader_addline(buffer, "%s%s = gl_TexCoord[%u]%s;\n",
2912 destination, reg_mask, usage_idx, reg_mask);
2913 } else {
2914 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2915 destination, reg_mask, reg_mask);
2917 break;
2919 case WINED3DDECLUSAGE_FOG:
2920 shader_addline(buffer, "%s%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
2921 destination, reg_mask, reg_mask);
2922 break;
2924 default:
2925 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2926 destination, reg_mask, reg_mask);
2928 } else {
2929 BOOL found = FALSE;
2930 for(j = 0; j < MAX_REG_OUTPUT; j++) {
2931 usage_token_out = semantics_out[j].usage;
2932 if (!usage_token_out) continue;
2933 register_token_out = semantics_out[j].reg;
2935 usage_out = (usage_token_out & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2936 usage_idx_out = (usage_token_out & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2937 shader_glsl_get_write_mask(register_token_out, reg_mask_out);
2939 if(usage == usage_out &&
2940 usage_idx == usage_idx_out) {
2941 shader_addline(buffer, "%s%s = OUT[%u]%s;\n",
2942 destination, reg_mask, j, reg_mask);
2943 found = TRUE;
2946 if(!found) {
2947 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2948 destination, reg_mask, reg_mask);
2953 /* This is solely to make the compiler / linker happy and avoid warning about undefined
2954 * varyings. It shouldn't result in any real code executed on the GPU, since all read
2955 * input varyings are assigned above, if the optimizer works properly.
2957 for(i = 0; i < in_count + 2; i++) {
2958 if(set[i] != WINED3DSP_WRITEMASK_ALL) {
2959 unsigned int size = 0;
2960 memset(reg_mask, 0, sizeof(reg_mask));
2961 if(!(set[i] & WINED3DSP_WRITEMASK_0)) {
2962 reg_mask[size] = 'x';
2963 size++;
2965 if(!(set[i] & WINED3DSP_WRITEMASK_1)) {
2966 reg_mask[size] = 'y';
2967 size++;
2969 if(!(set[i] & WINED3DSP_WRITEMASK_2)) {
2970 reg_mask[size] = 'z';
2971 size++;
2973 if(!(set[i] & WINED3DSP_WRITEMASK_3)) {
2974 reg_mask[size] = 'w';
2975 size++;
2978 if (i == in_count) {
2979 sprintf(destination, "gl_FrontColor");
2980 } else if (i == in_count + 1) {
2981 sprintf(destination, "gl_FrontSecondaryColor");
2982 } else {
2983 sprintf(destination, "IN[%u]", i);
2986 if (size == 1) {
2987 shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
2988 } else {
2989 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
2994 HeapFree(GetProcessHeap(), 0, set);
2997 static GLhandleARB generate_param_reorder_function(IWineD3DVertexShader *vertexshader,
2998 IWineD3DPixelShader *pixelshader, const WineD3D_GL_Info *gl_info)
3000 GLhandleARB ret = 0;
3001 IWineD3DVertexShaderImpl *vs = (IWineD3DVertexShaderImpl *) vertexshader;
3002 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pixelshader;
3003 IWineD3DDeviceImpl *device;
3004 DWORD vs_major = WINED3DSHADER_VERSION_MAJOR(vs->baseShader.reg_maps.shader_version);
3005 DWORD ps_major = ps ? WINED3DSHADER_VERSION_MAJOR(ps->baseShader.reg_maps.shader_version) : 0;
3006 unsigned int i;
3007 SHADER_BUFFER buffer;
3008 DWORD usage_token;
3009 DWORD register_token;
3010 DWORD usage, usage_idx, writemask;
3011 char reg_mask[6];
3012 const struct semantic *semantics_out, *semantics_in;
3014 shader_buffer_init(&buffer);
3016 shader_addline(&buffer, "#version 120\n");
3018 if(vs_major < 3 && ps_major < 3) {
3019 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
3020 * Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
3022 device = (IWineD3DDeviceImpl *) vs->baseShader.device;
3023 if((GLINFO_LOCATION).set_texcoord_w && ps_major == 0 && vs_major > 0 &&
3024 !device->frag_pipe->ffp_proj_control) {
3025 shader_addline(&buffer, "void order_ps_input() {\n");
3026 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
3027 if(vs->baseShader.reg_maps.texcoord_mask[i] != 0 &&
3028 vs->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
3029 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", i);
3032 shader_addline(&buffer, "}\n");
3033 } else {
3034 shader_addline(&buffer, "void order_ps_input() { /* do nothing */ }\n");
3036 } else if(ps_major < 3 && vs_major >= 3) {
3037 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
3038 semantics_out = vs->semantics_out;
3040 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3041 for(i = 0; i < MAX_REG_OUTPUT; i++) {
3042 usage_token = semantics_out[i].usage;
3043 if (!usage_token) continue;
3044 register_token = semantics_out[i].reg;
3046 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
3047 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
3048 writemask = shader_glsl_get_write_mask(register_token, reg_mask);
3050 switch(usage) {
3051 case WINED3DDECLUSAGE_COLOR:
3052 if (usage_idx == 0)
3053 shader_addline(&buffer, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3054 else if (usage_idx == 1)
3055 shader_addline(&buffer, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3056 break;
3058 case WINED3DDECLUSAGE_POSITION:
3059 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3060 break;
3062 case WINED3DDECLUSAGE_TEXCOORD:
3063 if (usage_idx < 8) {
3064 if(!(GLINFO_LOCATION).set_texcoord_w || ps_major > 0) writemask |= WINED3DSP_WRITEMASK_3;
3066 shader_addline(&buffer, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3067 usage_idx, reg_mask, i, reg_mask);
3068 if(!(writemask & WINED3DSP_WRITEMASK_3)) {
3069 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", usage_idx);
3072 break;
3074 case WINED3DDECLUSAGE_PSIZE:
3075 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3076 break;
3078 case WINED3DDECLUSAGE_FOG:
3079 shader_addline(&buffer, "gl_FogFragCoord = OUT[%u].%c;\n", i, reg_mask[1]);
3080 break;
3082 default:
3083 break;
3086 shader_addline(&buffer, "}\n");
3088 } else if(ps_major >= 3 && vs_major >= 3) {
3089 semantics_out = vs->semantics_out;
3090 semantics_in = ps->semantics_in;
3092 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3093 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3094 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3096 /* First, sort out position and point size. Those are not passed to the pixel shader */
3097 for(i = 0; i < MAX_REG_OUTPUT; i++) {
3098 usage_token = semantics_out[i].usage;
3099 if (!usage_token) continue;
3100 register_token = semantics_out[i].reg;
3102 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
3103 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
3104 shader_glsl_get_write_mask(register_token, reg_mask);
3106 switch(usage) {
3107 case WINED3DDECLUSAGE_POSITION:
3108 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3109 break;
3111 case WINED3DDECLUSAGE_PSIZE:
3112 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3113 break;
3115 default:
3116 break;
3120 /* Then, fix the pixel shader input */
3121 handle_ps3_input(&buffer, semantics_in, semantics_out, gl_info, ps->input_reg_map);
3123 shader_addline(&buffer, "}\n");
3124 } else if(ps_major >= 3 && vs_major < 3) {
3125 semantics_in = ps->semantics_in;
3127 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3128 shader_addline(&buffer, "void order_ps_input() {\n");
3129 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
3130 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
3131 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
3133 handle_ps3_input(&buffer, semantics_in, NULL, gl_info, ps->input_reg_map);
3134 shader_addline(&buffer, "}\n");
3135 } else {
3136 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major, ps_major);
3139 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3140 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3141 GL_EXTCALL(glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL));
3142 checkGLcall("glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL)");
3143 GL_EXTCALL(glCompileShaderARB(ret));
3144 checkGLcall("glCompileShaderARB(ret)");
3146 shader_buffer_free(&buffer);
3147 return ret;
3150 static void hardcode_local_constants(IWineD3DBaseShaderImpl *shader, const WineD3D_GL_Info *gl_info,
3151 GLhandleARB programId, char prefix)
3153 const local_constant *lconst;
3154 GLuint tmp_loc;
3155 const float *value;
3156 char glsl_name[8];
3158 LIST_FOR_EACH_ENTRY(lconst, &shader->baseShader.constantsF, local_constant, entry) {
3159 value = (const float *)lconst->value;
3160 snprintf(glsl_name, sizeof(glsl_name), "%cLC%u", prefix, lconst->idx);
3161 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3162 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, value));
3164 checkGLcall("Hardcoding local constants\n");
3167 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
3168 * It sets the programId on the current StateBlock (because it should be called
3169 * inside of the DrawPrimitive() part of the render loop).
3171 * If a program for the given combination does not exist, create one, and store
3172 * the program in the hash table. If it creates a program, it will link the
3173 * given objects, too.
3175 static void set_glsl_shader_program(IWineD3DDevice *iface, BOOL use_ps, BOOL use_vs) {
3176 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3177 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
3178 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3179 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
3180 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
3181 struct glsl_shader_prog_link *entry = NULL;
3182 GLhandleARB programId = 0;
3183 GLhandleARB reorder_shader_id = 0;
3184 int i;
3185 char glsl_name[8];
3186 GLhandleARB vshader_id, pshader_id;
3187 struct ps_compile_args compile_args;
3189 if(use_vs) {
3190 IWineD3DVertexShaderImpl_CompileShader(vshader);
3191 vshader_id = ((IWineD3DVertexShaderImpl*)vshader)->prgId;
3192 } else {
3193 vshader_id = 0;
3195 if(use_ps) {
3196 find_ps_compile_args((IWineD3DPixelShaderImpl*)This->stateBlock->pixelShader, This->stateBlock, &compile_args);
3197 } else {
3198 /* FIXME: Do we really have to spend CPU cycles to generate a few zeroed bytes? */
3199 memset(&compile_args, 0, sizeof(compile_args));
3201 entry = get_glsl_program_entry(priv, vshader_id, pshader, &compile_args);
3202 if (entry) {
3203 priv->glsl_program = entry;
3204 return;
3207 /* If we get to this point, then no matching program exists, so we create one */
3208 programId = GL_EXTCALL(glCreateProgramObjectARB());
3209 TRACE("Created new GLSL shader program %u\n", programId);
3211 /* Create the entry */
3212 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
3213 entry->programId = programId;
3214 entry->vshader = vshader_id;
3215 entry->pshader = pshader;
3216 entry->ps_args = compile_args;
3217 /* Add the hash table entry */
3218 add_glsl_program_entry(priv, entry);
3220 /* Set the current program */
3221 priv->glsl_program = entry;
3223 /* Attach GLSL vshader */
3224 if (vshader_id) {
3225 int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
3226 char tmp_name[10];
3228 reorder_shader_id = generate_param_reorder_function(vshader, pshader, gl_info);
3229 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
3230 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
3231 checkGLcall("glAttachObjectARB");
3232 /* Flag the reorder function for deletion, then it will be freed automatically when the program
3233 * is destroyed
3235 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
3237 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
3238 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
3239 checkGLcall("glAttachObjectARB");
3241 /* Bind vertex attributes to a corresponding index number to match
3242 * the same index numbers as ARB_vertex_programs (makes loading
3243 * vertex attributes simpler). With this method, we can use the
3244 * exact same code to load the attributes later for both ARB and
3245 * GLSL shaders.
3247 * We have to do this here because we need to know the Program ID
3248 * in order to make the bindings work, and it has to be done prior
3249 * to linking the GLSL program. */
3250 for (i = 0; i < max_attribs; ++i) {
3251 if (((IWineD3DBaseShaderImpl*)vshader)->baseShader.reg_maps.attributes[i]) {
3252 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
3253 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
3256 checkGLcall("glBindAttribLocationARB");
3258 list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
3261 if(use_ps) {
3262 pshader_id = find_gl_pshader((IWineD3DPixelShaderImpl *) pshader, &compile_args);
3263 } else {
3264 pshader_id = 0;
3267 /* Attach GLSL pshader */
3268 if (pshader_id) {
3269 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
3270 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
3271 checkGLcall("glAttachObjectARB");
3273 list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
3276 /* Link the program */
3277 TRACE("Linking GLSL shader program %u\n", programId);
3278 GL_EXTCALL(glLinkProgramARB(programId));
3279 print_glsl_info_log(&GLINFO_LOCATION, programId);
3281 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
3282 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
3283 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
3284 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3286 for (i = 0; i < MAX_CONST_I; ++i) {
3287 snprintf(glsl_name, sizeof(glsl_name), "VI[%i]", i);
3288 entry->vuniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3290 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
3291 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
3292 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
3293 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3295 for (i = 0; i < MAX_CONST_I; ++i) {
3296 snprintf(glsl_name, sizeof(glsl_name), "PI[%i]", i);
3297 entry->puniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3300 if(pshader) {
3301 for(i = 0; i < ((IWineD3DPixelShaderImpl*)pshader)->numbumpenvmatconsts; i++) {
3302 char name[32];
3303 sprintf(name, "bumpenvmat%d", ((IWineD3DPixelShaderImpl*)pshader)->bumpenvmatconst[i].texunit);
3304 entry->bumpenvmat_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3305 sprintf(name, "luminancescale%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3306 entry->luminancescale_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3307 sprintf(name, "luminanceoffset%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3308 entry->luminanceoffset_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3313 entry->posFixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
3314 entry->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
3315 checkGLcall("Find glsl program uniform locations");
3317 if (pshader
3318 && WINED3DSHADER_VERSION_MAJOR(((IWineD3DPixelShaderImpl *)pshader)->baseShader.reg_maps.shader_version) >= 3
3319 && ((IWineD3DPixelShaderImpl *)pshader)->declared_in_count > GL_LIMITS(glsl_varyings) / 4)
3321 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
3322 entry->vertex_color_clamp = GL_FALSE;
3323 } else {
3324 entry->vertex_color_clamp = GL_FIXED_ONLY_ARB;
3327 /* Set the shader to allow uniform loading on it */
3328 GL_EXTCALL(glUseProgramObjectARB(programId));
3329 checkGLcall("glUseProgramObjectARB(programId)");
3331 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
3332 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
3333 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
3334 * vertex shader with fixed function pixel processing is used we make sure that the card
3335 * supports enough samplers to allow the max number of vertex samplers with all possible
3336 * fixed function fragment processing setups. So once the program is linked these samplers
3337 * won't change.
3339 if(vshader_id) {
3340 /* Load vertex shader samplers */
3341 shader_glsl_load_vsamplers(gl_info, (IWineD3DStateBlock*)This->stateBlock, programId);
3343 if(pshader_id) {
3344 /* Load pixel shader samplers */
3345 shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*)This->stateBlock, programId);
3348 /* If the local constants do not have to be loaded with the environment constants,
3349 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
3350 * later
3352 if(pshader && !((IWineD3DPixelShaderImpl*)pshader)->baseShader.load_local_constsF) {
3353 hardcode_local_constants((IWineD3DBaseShaderImpl *) pshader, gl_info, programId, 'P');
3355 if(vshader && !((IWineD3DVertexShaderImpl*)vshader)->baseShader.load_local_constsF) {
3356 hardcode_local_constants((IWineD3DBaseShaderImpl *) vshader, gl_info, programId, 'V');
3360 static GLhandleARB create_glsl_blt_shader(const WineD3D_GL_Info *gl_info, enum tex_types tex_type)
3362 GLhandleARB program_id;
3363 GLhandleARB vshader_id, pshader_id;
3364 static const char *blt_vshader[] =
3366 "#version 120\n"
3367 "void main(void)\n"
3368 "{\n"
3369 " gl_Position = gl_Vertex;\n"
3370 " gl_FrontColor = vec4(1.0);\n"
3371 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
3372 "}\n"
3375 static const char *blt_pshaders[tex_type_count] =
3377 /* tex_1d */
3378 NULL,
3379 /* tex_2d */
3380 "#version 120\n"
3381 "uniform sampler2D sampler;\n"
3382 "void main(void)\n"
3383 "{\n"
3384 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
3385 "}\n",
3386 /* tex_3d */
3387 NULL,
3388 /* tex_cube */
3389 "#version 120\n"
3390 "uniform samplerCube sampler;\n"
3391 "void main(void)\n"
3392 "{\n"
3393 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
3394 "}\n",
3395 /* tex_rect */
3396 "#version 120\n"
3397 "#extension GL_ARB_texture_rectangle : enable\n"
3398 "uniform sampler2DRect sampler;\n"
3399 "void main(void)\n"
3400 "{\n"
3401 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
3402 "}\n",
3405 if (!blt_pshaders[tex_type])
3407 FIXME("tex_type %#x not supported\n", tex_type);
3408 tex_type = tex_2d;
3411 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3412 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
3413 GL_EXTCALL(glCompileShaderARB(vshader_id));
3415 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3416 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, &blt_pshaders[tex_type], NULL));
3417 GL_EXTCALL(glCompileShaderARB(pshader_id));
3419 program_id = GL_EXTCALL(glCreateProgramObjectARB());
3420 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
3421 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
3422 GL_EXTCALL(glLinkProgramARB(program_id));
3424 print_glsl_info_log(&GLINFO_LOCATION, program_id);
3426 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
3427 * is destroyed
3429 GL_EXTCALL(glDeleteObjectARB(vshader_id));
3430 GL_EXTCALL(glDeleteObjectARB(pshader_id));
3431 return program_id;
3434 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
3435 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3436 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
3437 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3438 GLhandleARB program_id = 0;
3439 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
3441 old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3443 if (useVS || usePS) set_glsl_shader_program(iface, usePS, useVS);
3444 else priv->glsl_program = NULL;
3446 current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3448 if (old_vertex_color_clamp != current_vertex_color_clamp) {
3449 if (GL_SUPPORT(ARB_COLOR_BUFFER_FLOAT)) {
3450 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
3451 checkGLcall("glClampColorARB");
3452 } else {
3453 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
3457 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
3458 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3459 GL_EXTCALL(glUseProgramObjectARB(program_id));
3460 checkGLcall("glUseProgramObjectARB");
3463 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {
3464 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3465 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3466 struct shader_glsl_priv *priv = (struct shader_glsl_priv *) This->shader_priv;
3467 GLhandleARB *blt_program = &priv->depth_blt_program[tex_type];
3469 if (!*blt_program) {
3470 GLhandleARB loc;
3471 *blt_program = create_glsl_blt_shader(gl_info, tex_type);
3472 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
3473 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
3474 GL_EXTCALL(glUniform1iARB(loc, 0));
3475 } else {
3476 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
3480 static void shader_glsl_deselect_depth_blt(IWineD3DDevice *iface) {
3481 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3482 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3483 struct shader_glsl_priv *priv = (struct shader_glsl_priv *) This->shader_priv;
3484 GLhandleARB program_id;
3486 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
3487 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3489 GL_EXTCALL(glUseProgramObjectARB(program_id));
3490 checkGLcall("glUseProgramObjectARB");
3493 static void shader_glsl_destroy(IWineD3DBaseShader *iface) {
3494 const struct list *linked_programs;
3495 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) iface;
3496 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
3497 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)device->shader_priv;
3498 const WineD3D_GL_Info *gl_info = &device->adapter->gl_info;
3499 IWineD3DPixelShaderImpl *ps = NULL;
3500 IWineD3DVertexShaderImpl *vs = NULL;
3502 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
3503 * can be called from IWineD3DBaseShader::Release
3505 char pshader = shader_is_pshader_version(This->baseShader.reg_maps.shader_version);
3507 if(pshader) {
3508 ps = (IWineD3DPixelShaderImpl *) This;
3509 if(ps->num_gl_shaders == 0) return;
3510 } else {
3511 vs = (IWineD3DVertexShaderImpl *) This;
3512 if(vs->prgId == 0) return;
3515 linked_programs = &This->baseShader.linked_programs;
3517 TRACE("Deleting linked programs\n");
3518 if (linked_programs->next) {
3519 struct glsl_shader_prog_link *entry, *entry2;
3521 if(pshader) {
3522 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
3523 delete_glsl_program_entry(priv, gl_info, entry);
3525 } else {
3526 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, vshader_entry) {
3527 delete_glsl_program_entry(priv, gl_info, entry);
3532 if(pshader) {
3533 UINT i;
3535 ENTER_GL();
3536 for(i = 0; i < ps->num_gl_shaders; i++) {
3537 TRACE("deleting pshader %u\n", ps->gl_shaders[i].prgId);
3538 GL_EXTCALL(glDeleteObjectARB(ps->gl_shaders[i].prgId));
3539 checkGLcall("glDeleteObjectARB");
3541 LEAVE_GL();
3542 HeapFree(GetProcessHeap(), 0, ps->gl_shaders);
3543 ps->gl_shaders = NULL;
3544 ps->num_gl_shaders = 0;
3545 } else {
3546 TRACE("Deleting shader object %u\n", vs->prgId);
3547 ENTER_GL();
3548 GL_EXTCALL(glDeleteObjectARB(vs->prgId));
3549 checkGLcall("glDeleteObjectARB");
3550 LEAVE_GL();
3551 vs->prgId = 0;
3552 vs->baseShader.is_compiled = FALSE;
3556 static unsigned int glsl_program_key_hash(const void *key)
3558 const glsl_program_key_t *k = (const glsl_program_key_t *)key;
3560 unsigned int hash = k->vshader | ((DWORD_PTR) k->pshader) << 16;
3561 hash += ~(hash << 15);
3562 hash ^= (hash >> 10);
3563 hash += (hash << 3);
3564 hash ^= (hash >> 6);
3565 hash += ~(hash << 11);
3566 hash ^= (hash >> 16);
3568 return hash;
3571 static BOOL glsl_program_key_compare(const void *keya, const void *keyb)
3573 const glsl_program_key_t *ka = (const glsl_program_key_t *)keya;
3574 const glsl_program_key_t *kb = (const glsl_program_key_t *)keyb;
3576 return ka->vshader == kb->vshader && ka->pshader == kb->pshader &&
3577 (memcmp(&ka->ps_args, &kb->ps_args, sizeof(kb->ps_args)) == 0);
3580 static HRESULT shader_glsl_alloc(IWineD3DDevice *iface) {
3581 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3582 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
3583 priv->glsl_program_lookup = hash_table_create(glsl_program_key_hash, glsl_program_key_compare);
3584 This->shader_priv = priv;
3585 return WINED3D_OK;
3588 static void shader_glsl_free(IWineD3DDevice *iface) {
3589 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3590 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3591 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
3592 int i;
3594 for (i = 0; i < tex_type_count; ++i)
3596 if (priv->depth_blt_program[i])
3598 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program[i]));
3602 hash_table_destroy(priv->glsl_program_lookup, NULL, NULL);
3604 HeapFree(GetProcessHeap(), 0, This->shader_priv);
3605 This->shader_priv = NULL;
3608 static BOOL shader_glsl_dirty_const(IWineD3DDevice *iface) {
3609 /* TODO: GL_EXT_bindable_uniform can be used to share constants across shaders */
3610 return FALSE;
3613 static GLuint shader_glsl_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer) {
3614 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
3615 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
3616 CONST DWORD *function = This->baseShader.function;
3617 const char *fragcolor;
3618 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3620 /* Create the hw GLSL shader object and assign it as the shader->prgId */
3621 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3623 shader_addline(buffer, "#version 120\n");
3625 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3626 shader_addline(buffer, "#extension GL_ARB_draw_buffers : enable\n");
3628 if (GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
3629 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
3630 * drivers write a warning if we don't do so
3632 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
3635 /* Base Declarations */
3636 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
3638 /* Pack 3.0 inputs */
3639 if (reg_maps->shader_version >= WINED3DPS_VERSION(3,0))
3641 if(((IWineD3DDeviceImpl *) This->baseShader.device)->strided_streams.u.s.position_transformed) {
3642 pshader_glsl_input_pack(buffer, This->semantics_in, iface, pretransformed);
3643 } else if(!use_vs((IWineD3DDeviceImpl *) This->baseShader.device)) {
3644 pshader_glsl_input_pack(buffer, This->semantics_in, iface, fixedfunction);
3648 /* Base Shader Body */
3649 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
3651 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
3652 if (reg_maps->shader_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");
3657 else
3658 shader_addline(buffer, "gl_FragColor = R0;\n");
3661 if(GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3662 fragcolor = "gl_FragData[0]";
3663 } else {
3664 fragcolor = "gl_FragColor";
3666 if(((IWineD3DDeviceImpl *)This->baseShader.device)->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
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 (reg_maps->shader_version < WINED3DPS_VERSION(3,0))
3684 shader_addline(buffer, "float Fog = clamp(gl_FogFragCoord * gl_Fog.start + gl_Fog.end, 0.0, 1.0);\n");
3685 shader_addline(buffer, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor, fragcolor);
3688 shader_addline(buffer, "}\n");
3690 TRACE("Compiling shader object %u\n", shader_obj);
3691 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
3692 GL_EXTCALL(glCompileShaderARB(shader_obj));
3693 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
3695 /* Store the shader object */
3696 return shader_obj;
3699 static void shader_glsl_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer) {
3700 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
3701 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
3702 CONST DWORD *function = This->baseShader.function;
3703 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3705 /* Create the hw GLSL shader program and assign it as the shader->prgId */
3706 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3708 shader_addline(buffer, "#version 120\n");
3710 /* Base Declarations */
3711 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION);
3713 /* Base Shader Body */
3714 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
3716 /* Unpack 3.0 outputs */
3717 if (reg_maps->shader_version >= WINED3DVS_VERSION(3,0)) shader_addline(buffer, "order_ps_input(OUT);\n");
3718 else shader_addline(buffer, "order_ps_input();\n");
3720 /* If this shader doesn't use fog copy the z coord to the fog coord so that we can use table fog */
3721 if (!reg_maps->fog)
3722 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
3724 /* Write the final position.
3726 * OpenGL coordinates specify the center of the pixel while d3d coords specify
3727 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
3728 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
3729 * contains 1.0 to allow a mad.
3731 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
3732 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
3734 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
3736 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
3737 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
3738 * which is the same as z = z * 2 - w.
3740 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3742 shader_addline(buffer, "}\n");
3744 TRACE("Compiling shader object %u\n", shader_obj);
3745 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
3746 GL_EXTCALL(glCompileShaderARB(shader_obj));
3747 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
3749 /* Store the shader object */
3750 This->prgId = shader_obj;
3753 static void shader_glsl_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
3755 /* Nvidia Geforce6/7 or Ati R4xx/R5xx cards with GLSL support, support VS 3.0 but older Nvidia/Ati
3756 * models with GLSL support only support 2.0. In case of nvidia we can detect VS 2.0 support using
3757 * vs_nv_version which is based on NV_vertex_program.
3758 * For Ati cards there's no way using glsl (it abstracts the lowlevel info away) and also not
3759 * using ARB_vertex_program. It is safe to assume that when a card supports pixel shader 2.0 it
3760 * supports vertex shader 2.0 too and the way around. We can detect ps2.0 using the maximum number
3761 * of native instructions, so use that here. For more info see the pixel shader versioning code below.
3763 if((GLINFO_LOCATION.vs_nv_version == VS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
3764 pCaps->VertexShaderVersion = WINED3DVS_VERSION(2,0);
3765 else
3766 pCaps->VertexShaderVersion = WINED3DVS_VERSION(3,0);
3767 TRACE_(d3d_caps)("Hardware vertex shader version %d.%d enabled (GLSL)\n", (pCaps->VertexShaderVersion >> 8) & 0xff, pCaps->VertexShaderVersion & 0xff);
3768 pCaps->MaxVertexShaderConst = GL_LIMITS(vshader_constantsF);
3770 /* Older DX9-class videocards (GeforceFX / Radeon >9500/X*00) only support pixel shader 2.0/2.0a/2.0b.
3771 * In OpenGL the extensions related to GLSL abstract lowlevel GL info away which is needed
3772 * to distinguish between 2.0 and 3.0 (and 2.0a/2.0b). In case of Nvidia we use their fragment
3773 * program extensions. On other hardware including ATI GL_ARB_fragment_program offers the info
3774 * in max native instructions. Intel and others also offer the info in this extension but they
3775 * don't support GLSL (at least on Windows).
3777 * PS2.0 requires at least 96 instructions, 2.0a/2.0b go up to 512. Assume that if the number
3778 * of instructions is 512 or less we have to do with ps2.0 hardware.
3779 * 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.
3781 if((GLINFO_LOCATION.ps_nv_version == PS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
3782 pCaps->PixelShaderVersion = WINED3DPS_VERSION(2,0);
3783 else
3784 pCaps->PixelShaderVersion = WINED3DPS_VERSION(3,0);
3786 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
3787 * Direct3D minimum requirement.
3789 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
3790 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
3792 * The problem is that the refrast clamps temporary results in the shader to
3793 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
3794 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
3795 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
3796 * offer a way to query this.
3798 pCaps->PixelShader1xMaxValue = 8.0;
3799 TRACE_(d3d_caps)("Hardware pixel shader version %d.%d enabled (GLSL)\n", (pCaps->PixelShaderVersion >> 8) & 0xff, pCaps->PixelShaderVersion & 0xff);
3802 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
3804 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
3806 TRACE("Checking support for fixup:\n");
3807 dump_color_fixup_desc(fixup);
3810 /* We support everything except YUV conversions. */
3811 if (!is_yuv_fixup(fixup))
3813 TRACE("[OK]\n");
3814 return TRUE;
3817 TRACE("[FAILED]\n");
3818 return FALSE;
3821 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
3823 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
3824 /* WINED3DSIH_ADD */ shader_glsl_arith,
3825 /* WINED3DSIH_BEM */ pshader_glsl_bem,
3826 /* WINED3DSIH_BREAK */ shader_glsl_break,
3827 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
3828 /* WINED3DSIH_BREAKP */ NULL,
3829 /* WINED3DSIH_CALL */ shader_glsl_call,
3830 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
3831 /* WINED3DSIH_CMP */ shader_glsl_cmp,
3832 /* WINED3DSIH_CND */ shader_glsl_cnd,
3833 /* WINED3DSIH_CRS */ shader_glsl_cross,
3834 /* WINED3DSIH_DCL */ NULL,
3835 /* WINED3DSIH_DEF */ NULL,
3836 /* WINED3DSIH_DEFB */ NULL,
3837 /* WINED3DSIH_DEFI */ NULL,
3838 /* WINED3DSIH_DP2ADD */ pshader_glsl_dp2add,
3839 /* WINED3DSIH_DP3 */ shader_glsl_dot,
3840 /* WINED3DSIH_DP4 */ shader_glsl_dot,
3841 /* WINED3DSIH_DST */ shader_glsl_dst,
3842 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
3843 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
3844 /* WINED3DSIH_ELSE */ shader_glsl_else,
3845 /* WINED3DSIH_ENDIF */ shader_glsl_end,
3846 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
3847 /* WINED3DSIH_ENDREP */ shader_glsl_end,
3848 /* WINED3DSIH_EXP */ shader_glsl_map2gl,
3849 /* WINED3DSIH_EXPP */ shader_glsl_expp,
3850 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
3851 /* WINED3DSIH_IF */ shader_glsl_if,
3852 /* WINED3DSIH_IFC */ shader_glsl_ifc,
3853 /* WINED3DSIH_LABEL */ shader_glsl_label,
3854 /* WINED3DSIH_LIT */ shader_glsl_lit,
3855 /* WINED3DSIH_LOG */ shader_glsl_log,
3856 /* WINED3DSIH_LOGP */ shader_glsl_log,
3857 /* WINED3DSIH_LOOP */ shader_glsl_loop,
3858 /* WINED3DSIH_LRP */ shader_glsl_lrp,
3859 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
3860 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
3861 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
3862 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
3863 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
3864 /* WINED3DSIH_MAD */ shader_glsl_mad,
3865 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
3866 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
3867 /* WINED3DSIH_MOV */ shader_glsl_mov,
3868 /* WINED3DSIH_MOVA */ shader_glsl_mov,
3869 /* WINED3DSIH_MUL */ shader_glsl_arith,
3870 /* WINED3DSIH_NOP */ NULL,
3871 /* WINED3DSIH_NRM */ shader_glsl_map2gl,
3872 /* WINED3DSIH_PHASE */ NULL,
3873 /* WINED3DSIH_POW */ shader_glsl_pow,
3874 /* WINED3DSIH_RCP */ shader_glsl_rcp,
3875 /* WINED3DSIH_REP */ shader_glsl_rep,
3876 /* WINED3DSIH_RET */ NULL,
3877 /* WINED3DSIH_RSQ */ shader_glsl_rsq,
3878 /* WINED3DSIH_SETP */ NULL,
3879 /* WINED3DSIH_SGE */ shader_glsl_compare,
3880 /* WINED3DSIH_SGN */ shader_glsl_map2gl,
3881 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
3882 /* WINED3DSIH_SLT */ shader_glsl_compare,
3883 /* WINED3DSIH_SUB */ shader_glsl_arith,
3884 /* WINED3DSIH_TEX */ pshader_glsl_tex,
3885 /* WINED3DSIH_TEXBEM */ pshader_glsl_texbem,
3886 /* WINED3DSIH_TEXBEML */ pshader_glsl_texbem,
3887 /* WINED3DSIH_TEXCOORD */ pshader_glsl_texcoord,
3888 /* WINED3DSIH_TEXDEPTH */ pshader_glsl_texdepth,
3889 /* WINED3DSIH_TEXDP3 */ pshader_glsl_texdp3,
3890 /* WINED3DSIH_TEXDP3TEX */ pshader_glsl_texdp3tex,
3891 /* WINED3DSIH_TEXKILL */ pshader_glsl_texkill,
3892 /* WINED3DSIH_TEXLDD */ NULL,
3893 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
3894 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_glsl_texm3x2depth,
3895 /* WINED3DSIH_TEXM3x2PAD */ pshader_glsl_texm3x2pad,
3896 /* WINED3DSIH_TEXM3x2TEX */ pshader_glsl_texm3x2tex,
3897 /* WINED3DSIH_TEXM3x3 */ pshader_glsl_texm3x3,
3898 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
3899 /* WINED3DSIH_TEXM3x3PAD */ pshader_glsl_texm3x3pad,
3900 /* WINED3DSIH_TEXM3x3SPEC */ pshader_glsl_texm3x3spec,
3901 /* WINED3DSIH_TEXM3x3TEX */ pshader_glsl_texm3x3tex,
3902 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_glsl_texm3x3vspec,
3903 /* WINED3DSIH_TEXREG2AR */ pshader_glsl_texreg2ar,
3904 /* WINED3DSIH_TEXREG2GB */ pshader_glsl_texreg2gb,
3905 /* WINED3DSIH_TEXREG2RGB */ pshader_glsl_texreg2rgb,
3908 const shader_backend_t glsl_shader_backend = {
3909 shader_glsl_instruction_handler_table,
3910 shader_glsl_select,
3911 shader_glsl_select_depth_blt,
3912 shader_glsl_deselect_depth_blt,
3913 shader_glsl_update_float_vertex_constants,
3914 shader_glsl_update_float_pixel_constants,
3915 shader_glsl_load_constants,
3916 shader_glsl_color_correction,
3917 shader_glsl_destroy,
3918 shader_glsl_alloc,
3919 shader_glsl_free,
3920 shader_glsl_dirty_const,
3921 shader_glsl_generate_pshader,
3922 shader_glsl_generate_vshader,
3923 shader_glsl_get_caps,
3924 shader_glsl_color_fixup_supported,