wintrust: Add a helper function to initialize chain creation parameters.
[wine] / dlls / wined3d / glsl_shader.c
1 /*
2  * GLSL pixel and vertex shader implementation
3  *
4  * Copyright 2006 Jason Green 
5  * Copyright 2006-2007 Henri Verbeet
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 /*
23  * D3D shader asm has swizzles on source parameters, and write masks for
24  * destination parameters. GLSL uses swizzles for both. The result of this is
25  * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
26  * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
27  * mask for the destination parameter into account.
28  */
29
30 #include "config.h"
31 #include <stdio.h>
32 #include "wined3d_private.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
35 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
36
37 #define GLINFO_LOCATION      (*gl_info)
38
39 typedef struct {
40     char reg_name[50];
41     char mask_str[6];
42 } glsl_dst_param_t;
43
44 typedef struct {
45     char reg_name[50];
46     char param_str[100];
47 } glsl_src_param_t;
48
49 typedef struct {
50     const char *name;
51     DWORD coord_mask;
52 } glsl_sample_function_t;
53
54 /** Prints the GLSL info log which will contain error messages if they exist */
55 void print_glsl_info_log(WineD3D_GL_Info *gl_info, GLhandleARB obj) {
56     
57     int infologLength = 0;
58     char *infoLog;
59
60     GL_EXTCALL(glGetObjectParameterivARB(obj,
61                GL_OBJECT_INFO_LOG_LENGTH_ARB,
62                &infologLength));
63
64     /* A size of 1 is just a null-terminated string, so the log should be bigger than
65      * that if there are errors. */
66     if (infologLength > 1)
67     {
68         infoLog = HeapAlloc(GetProcessHeap(), 0, infologLength);
69         GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
70         FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
71         HeapFree(GetProcessHeap(), 0, infoLog);
72     }
73 }
74
75 /**
76  * Loads (pixel shader) samplers
77  */
78 static void shader_glsl_load_psamplers(
79     WineD3D_GL_Info *gl_info,
80     IWineD3DStateBlock* iface) {
81
82     IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
83     GLhandleARB programId = stateBlock->glsl_program->programId;
84     GLhandleARB name_loc;
85     int i;
86     char sampler_name[20];
87
88     for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
89         snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
90         name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
91         if (name_loc != -1) {
92             int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[i];
93             if (mapped_unit != -1 && mapped_unit < GL_LIMITS(fragment_samplers)) {
94                 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
95                 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
96                 checkGLcall("glUniform1iARB");
97             } else {
98                 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
99             }
100         }
101     }
102 }
103
104 static void shader_glsl_load_vsamplers(WineD3D_GL_Info *gl_info, IWineD3DStateBlock* iface) {
105     IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
106     GLhandleARB programId = stateBlock->glsl_program->programId;
107     GLhandleARB name_loc;
108     char sampler_name[20];
109     int i;
110
111     for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
112         snprintf(sampler_name, sizeof(sampler_name), "Vsampler%d", i);
113         name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
114         if (name_loc != -1) {
115             int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[MAX_FRAGMENT_SAMPLERS + i];
116             if (mapped_unit != -1 && mapped_unit < GL_LIMITS(combined_samplers)) {
117                 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
118                 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
119                 checkGLcall("glUniform1iARB");
120             } else {
121                 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
122             }
123         }
124     }
125 }
126
127 /** 
128  * Loads floating point constants (aka uniforms) into the currently set GLSL program.
129  * When constant_list == NULL, it will load all the constants.
130  */
131 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl* This, WineD3D_GL_Info *gl_info,
132         unsigned int max_constants, float* constants, GLhandleARB *constant_locations,
133         struct list *constant_list) {
134     constants_entry *constant;
135     local_constant* lconst;
136     GLhandleARB tmp_loc;
137     DWORD i, j, k;
138     DWORD *idx;
139
140     if (TRACE_ON(d3d_shader)) {
141         LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
142             idx = constant->idx;
143             j = constant->count;
144             while (j--) {
145                 i = *idx++;
146                 tmp_loc = constant_locations[i];
147                 if (tmp_loc != -1) {
148                     TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
149                             constants[i * 4 + 0], constants[i * 4 + 1],
150                             constants[i * 4 + 2], constants[i * 4 + 3]);
151                 }
152             }
153         }
154     }
155
156     /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
157     if(WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1 &&
158        shader_is_pshader_version(This->baseShader.hex_version)) {
159         float lcl_const[4];
160
161         LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
162             idx = constant->idx;
163             j = constant->count;
164             while (j--) {
165                 i = *idx++;
166                 tmp_loc = constant_locations[i];
167                 if (tmp_loc != -1) {
168                     /* We found this uniform name in the program - go ahead and send the data */
169                     k = i * 4;
170                     if(constants[k + 0] < -1.0) lcl_const[0] = -1.0;
171                     else if(constants[k + 0] > 1.0) lcl_const[0] = 1.0;
172                     else lcl_const[0] = constants[k + 0];
173                     if(constants[k + 1] < -1.0) lcl_const[1] = -1.0;
174                     else if(constants[k + 1] > 1.0) lcl_const[1] = 1.0;
175                     else lcl_const[1] = constants[k + 1];
176                     if(constants[k + 2] < -1.0) lcl_const[2] = -1.0;
177                     else if(constants[k + 2] > 1.0) lcl_const[2] = 1.0;
178                     else lcl_const[2] = constants[k + 2];
179                     if(constants[k + 3] < -1.0) lcl_const[3] = -1.0;
180                     else if(constants[k + 3] > 1.0) lcl_const[3] = 1.0;
181                     else lcl_const[3] = constants[k + 3];
182
183                     GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, lcl_const));
184                 }
185             }
186         }
187     } else {
188         LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
189             idx = constant->idx;
190             j = constant->count;
191             while (j--) {
192                 i = *idx++;
193                 tmp_loc = constant_locations[i];
194                 if (tmp_loc != -1) {
195                     /* We found this uniform name in the program - go ahead and send the data */
196                     GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
197                 }
198             }
199         }
200     }
201     checkGLcall("glUniform4fvARB()");
202
203     /* Load immediate constants */
204     if (TRACE_ON(d3d_shader)) {
205         LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
206             tmp_loc = constant_locations[lconst->idx];
207             if (tmp_loc != -1) {
208                 GLfloat* values = (GLfloat*)lconst->value;
209                 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
210                         values[0], values[1], values[2], values[3]);
211             }
212         }
213     }
214     /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
215     LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
216         tmp_loc = constant_locations[lconst->idx];
217         if (tmp_loc != -1) {
218             /* We found this uniform name in the program - go ahead and send the data */
219             GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, (GLfloat*)lconst->value));
220         }
221     }
222     checkGLcall("glUniform4fvARB()");
223 }
224
225 /** 
226  * Loads integer constants (aka uniforms) into the currently set GLSL program.
227  * When @constants_set == NULL, it will load all the constants.
228  */
229 static void shader_glsl_load_constantsI(
230     IWineD3DBaseShaderImpl* This,
231     WineD3D_GL_Info *gl_info,
232     GLhandleARB programId,
233     unsigned max_constants,
234     int* constants,
235     BOOL* constants_set) {
236     
237     GLhandleARB tmp_loc;
238     int i;
239     char tmp_name[8];
240     char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
241     const char* prefix = is_pshader? "PI":"VI";
242     struct list* ptr;
243
244     for (i=0; i<max_constants; ++i) {
245         if (NULL == constants_set || constants_set[i]) {
246
247             TRACE_(d3d_constants)("Loading constants %i: %i, %i, %i, %i\n",
248                   i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
249
250             /* TODO: Benchmark and see if it would be beneficial to store the 
251              * locations of the constants to avoid looking up each time */
252             snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
253             tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
254             if (tmp_loc != -1) {
255                 /* We found this uniform name in the program - go ahead and send the data */
256                 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, &constants[i*4]));
257                 checkGLcall("glUniform4ivARB");
258             }
259         }
260     }
261
262     /* Load immediate constants */
263     ptr = list_head(&This->baseShader.constantsI);
264     while (ptr) {
265         local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
266         unsigned int idx = lconst->idx;
267         GLint* values = (GLint*) lconst->value;
268
269         TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
270             values[0], values[1], values[2], values[3]);
271
272         snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
273         tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
274         if (tmp_loc != -1) {
275             /* We found this uniform name in the program - go ahead and send the data */
276             GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, values));
277             checkGLcall("glUniform4ivARB");
278         }
279         ptr = list_next(&This->baseShader.constantsI, ptr);
280     }
281 }
282
283 /** 
284  * Loads boolean constants (aka uniforms) into the currently set GLSL program.
285  * When @constants_set == NULL, it will load all the constants.
286  */
287 static void shader_glsl_load_constantsB(
288     IWineD3DBaseShaderImpl* This,
289     WineD3D_GL_Info *gl_info,
290     GLhandleARB programId,
291     unsigned max_constants,
292     BOOL* constants,
293     BOOL* constants_set) {
294     
295     GLhandleARB tmp_loc;
296     int i;
297     char tmp_name[8];
298     char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
299     const char* prefix = is_pshader? "PB":"VB";
300     struct list* ptr;
301
302     for (i=0; i<max_constants; ++i) {
303         if (NULL == constants_set || constants_set[i]) {
304
305             TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i*4]);
306
307             /* TODO: Benchmark and see if it would be beneficial to store the 
308              * locations of the constants to avoid looking up each time */
309             snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
310             tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
311             if (tmp_loc != -1) {
312                 /* We found this uniform name in the program - go ahead and send the data */
313                 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i*4]));
314                 checkGLcall("glUniform1ivARB");
315             }
316         }
317     }
318
319     /* Load immediate constants */
320     ptr = list_head(&This->baseShader.constantsB);
321     while (ptr) {
322         local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
323         unsigned int idx = lconst->idx;
324         GLint* values = (GLint*) lconst->value;
325
326         TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
327
328         snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
329         tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
330         if (tmp_loc != -1) {
331             /* We found this uniform name in the program - go ahead and send the data */
332             GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
333             checkGLcall("glUniform1ivARB");
334         }
335         ptr = list_next(&This->baseShader.constantsB, ptr);
336     }
337 }
338
339
340
341 /**
342  * Loads the app-supplied constants into the currently set GLSL program.
343  */
344 void shader_glsl_load_constants(
345     IWineD3DDevice* device,
346     char usePixelShader,
347     char useVertexShader) {
348    
349     IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
350     IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
351     WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
352
353     GLhandleARB *constant_locations;
354     struct list *constant_list;
355     GLhandleARB programId;
356     GLint pos;
357
358     if (!stateBlock->glsl_program) {
359         /* No GLSL program set - nothing to do. */
360         return;
361     }
362     programId = stateBlock->glsl_program->programId;
363
364     if (useVertexShader) {
365         IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
366         GLint pos;
367
368         constant_locations = stateBlock->glsl_program->vuniformF_locations;
369         constant_list = &stateBlock->set_vconstantsF;
370
371         /* Load vertex shader samplers */
372         shader_glsl_load_vsamplers(gl_info, (IWineD3DStateBlock*)stateBlock);
373
374         /* Load DirectX 9 float constants/uniforms for vertex shader */
375         shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
376                 stateBlock->vertexShaderConstantF, constant_locations, constant_list);
377
378         /* Load DirectX 9 integer constants/uniforms for vertex shader */
379         shader_glsl_load_constantsI(vshader, gl_info, programId, MAX_CONST_I,
380                                     stateBlock->vertexShaderConstantI,
381                                     stateBlock->changed.vertexShaderConstantsI);
382
383         /* Load DirectX 9 boolean constants/uniforms for vertex shader */
384         shader_glsl_load_constantsB(vshader, gl_info, programId, MAX_CONST_B,
385                                     stateBlock->vertexShaderConstantB,
386                                     stateBlock->changed.vertexShaderConstantsB);
387
388         /* Upload the position fixup params */
389         pos = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
390         checkGLcall("glGetUniformLocationARB");
391         GL_EXTCALL(glUniform4fvARB(pos, 1, &deviceImpl->posFixup[0]));
392         checkGLcall("glUniform4fvARB");
393     }
394
395     if (usePixelShader) {
396
397         IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
398
399         constant_locations = stateBlock->glsl_program->puniformF_locations;
400         constant_list = &stateBlock->set_pconstantsF;
401
402         /* Load pixel shader samplers */
403         shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*) stateBlock);
404
405         /* Load DirectX 9 float constants/uniforms for pixel shader */
406         shader_glsl_load_constantsF(pshader, gl_info, GL_LIMITS(pshader_constantsF),
407                 stateBlock->pixelShaderConstantF, constant_locations, constant_list);
408
409         /* Load DirectX 9 integer constants/uniforms for pixel shader */
410         shader_glsl_load_constantsI(pshader, gl_info, programId, MAX_CONST_I,
411                                     stateBlock->pixelShaderConstantI, 
412                                     stateBlock->changed.pixelShaderConstantsI);
413
414         /* Load DirectX 9 boolean constants/uniforms for pixel shader */
415         shader_glsl_load_constantsB(pshader, gl_info, programId, MAX_CONST_B,
416                                     stateBlock->pixelShaderConstantB, 
417                                     stateBlock->changed.pixelShaderConstantsB);
418
419         /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
420          * It can't be 0 for a valid texbem instruction.
421          */
422         if(((IWineD3DPixelShaderImpl *) pshader)->needsbumpmat != -1) {
423             float *data = (float *) &stateBlock->textureState[(int) ((IWineD3DPixelShaderImpl *) pshader)->needsbumpmat][WINED3DTSS_BUMPENVMAT00];
424             pos = GL_EXTCALL(glGetUniformLocationARB(programId, "bumpenvmat"));
425             checkGLcall("glGetUniformLocationARB");
426             GL_EXTCALL(glUniformMatrix2fvARB(pos, 1, 0, data));
427             checkGLcall("glUniformMatrix2fvARB");
428
429             /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
430              * is set too, so we can check that in the needsbumpmat check
431              */
432             if(((IWineD3DPixelShaderImpl *) pshader)->baseShader.reg_maps.luminanceparams != -1) {
433                 int stage = ((IWineD3DPixelShaderImpl *) pshader)->baseShader.reg_maps.luminanceparams;
434                 GLfloat *scale = (GLfloat *) &stateBlock->textureState[stage][WINED3DTSS_BUMPENVLSCALE];
435                 GLfloat *offset = (GLfloat *) &stateBlock->textureState[stage][WINED3DTSS_BUMPENVLOFFSET];
436
437                 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "luminancescale"));
438                 checkGLcall("glGetUniformLocationARB");
439                 GL_EXTCALL(glUniform1fvARB(pos, 1, scale));
440                 checkGLcall("glUniform1fvARB");
441                 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "luminanceoffset"));
442                 checkGLcall("glGetUniformLocationARB");
443                 GL_EXTCALL(glUniform1fvARB(pos, 1, offset));
444                 checkGLcall("glUniform1fvARB");
445             }
446         } else if(((IWineD3DPixelShaderImpl *) pshader)->srgb_enabled &&
447                   !((IWineD3DPixelShaderImpl *) pshader)->srgb_mode_hardcoded) {
448             float comparison[4];
449             float mul_low[4];
450
451             if(stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
452                 comparison[0] = srgb_cmp; comparison[1] = srgb_cmp;
453                 comparison[2] = srgb_cmp; comparison[3] = srgb_cmp;
454
455                 mul_low[0] = srgb_mul_low; mul_low[1] = srgb_mul_low;
456                 mul_low[2] = srgb_mul_low; mul_low[3] = srgb_mul_low;
457             } else {
458                 comparison[0] = 1.0 / 0.0; comparison[1] = 1.0 / 0.0;
459                 comparison[2] = 1.0 / 0.0; comparison[3] = 1.0 / 0.0;
460
461                 mul_low[0] = 1.0; mul_low[1] = 1.0;
462                 mul_low[2] = 1.0; mul_low[3] = 1.0;
463             }
464
465             pos = GL_EXTCALL(glGetUniformLocationARB(programId, "srgb_comparison"));
466             checkGLcall("glGetUniformLocationARB");
467             GL_EXTCALL(glUniform4fvARB(pos, 1, comparison));
468             pos = GL_EXTCALL(glGetUniformLocationARB(programId, "srgb_mul_low"));
469             checkGLcall("glGetUniformLocationARB");
470             GL_EXTCALL(glUniform4fvARB(pos, 1, mul_low));
471         }
472         if(((IWineD3DPixelShaderImpl *) pshader)->vpos_uniform) {
473             float correction_params[4];
474             pos = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
475             checkGLcall("glGetUniformLocationARB");
476             if(deviceImpl->render_offscreen) {
477                 correction_params[0] = 0.0;
478                 correction_params[1] = 1.0;
479             } else {
480                 /* position is window relative, not viewport relative */
481                 correction_params[0] = ((IWineD3DSurfaceImpl *) deviceImpl->render_targets[0])->currentDesc.Height;
482                 correction_params[1] = -1.0;
483             }
484             GL_EXTCALL(glUniform4fvARB(pos, 1, correction_params));
485         }
486     }
487 }
488
489 /** Generate the variable & register declarations for the GLSL output target */
490 void shader_generate_glsl_declarations(
491     IWineD3DBaseShader *iface,
492     shader_reg_maps* reg_maps,
493     SHADER_BUFFER* buffer,
494     WineD3D_GL_Info* gl_info) {
495
496     IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
497     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
498     int i;
499     unsigned int extra_constants_needed = 0;
500
501     /* There are some minor differences between pixel and vertex shaders */
502     char pshader = shader_is_pshader_version(This->baseShader.hex_version);
503     char prefix = pshader ? 'P' : 'V';
504
505     /* Prototype the subroutines */
506     for (i = 0; i < This->baseShader.limits.label; i++) {
507         if (reg_maps->labels[i])
508             shader_addline(buffer, "void subroutine%lu();\n", i);
509     }
510
511     /* Declare the constants (aka uniforms) */
512     if (This->baseShader.limits.constant_float > 0) {
513         unsigned max_constantsF = min(This->baseShader.limits.constant_float, 
514                 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
515         shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
516     }
517
518     if (This->baseShader.limits.constant_int > 0)
519         shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
520
521     if (This->baseShader.limits.constant_bool > 0)
522         shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
523
524     if(!pshader) {
525         shader_addline(buffer, "uniform vec4 posFixup;\n");
526     } else {
527         IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
528
529         if(reg_maps->bumpmat != -1) {
530             shader_addline(buffer, "uniform mat2 bumpenvmat;\n");
531             if(reg_maps->luminanceparams) {
532                 shader_addline(buffer, "uniform float luminancescale;\n");
533                 shader_addline(buffer, "uniform float luminanceoffset;\n");
534                 extra_constants_needed++;
535             }
536             extra_constants_needed++;
537         }
538
539         if(device->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE]) {
540             ps_impl->srgb_enabled = 1;
541             if(This->baseShader.limits.constant_float + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF)) {
542                 shader_addline(buffer, "uniform vec4 srgb_mul_low;\n");
543                 shader_addline(buffer, "uniform vec4 srgb_comparison;\n");
544                 ps_impl->srgb_mode_hardcoded = 0;
545                 extra_constants_needed++;
546             } else {
547                 ps_impl->srgb_mode_hardcoded = 1;
548                 shader_addline(buffer, "const vec4 srgb_mul_low = {%f, %f, %f, %f};\n",
549                                srgb_mul_low, srgb_mul_low, srgb_mul_low, srgb_mul_low);
550                 shader_addline(buffer, "const vec4 srgb_comparison = {%f, %f, %f, %f};\n",
551                                srgb_cmp, srgb_cmp, srgb_cmp, srgb_cmp);
552             }
553         } else {
554             IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
555
556             /* Do not write any srgb fixup into the shader to save shader size and processing time.
557              * As a consequence, we can't toggle srgb write on without recompilation
558              */
559             ps_impl->srgb_enabled = 0;
560             ps_impl->srgb_mode_hardcoded = 1;
561         }
562         if(reg_maps->vpos) {
563             if(This->baseShader.limits.constant_float + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF)) {
564                 shader_addline(buffer, "uniform vec4 ycorrection;\n");
565                 ((IWineD3DPixelShaderImpl *) This)->vpos_uniform = 1;
566                 extra_constants_needed++;
567             } else {
568                 /* This happens because we do not have proper tracking of the constant registers that are
569                  * actually used, only the max limit of the shader version
570                  */
571                 FIXME("Cannot find a free uniform for vpos correction params\n");
572                 shader_addline(buffer, "const vec4 ycorrection = {%f, %f, 0.0, 0.0};\n",
573                                device->render_offscreen ? 0.0 : ((IWineD3DSurfaceImpl *) device->render_targets[0])->currentDesc.Height,
574                                device->render_offscreen ? 1.0 : -1.0);
575             }
576             shader_addline(buffer, "vec4 vpos;\n");
577         }
578     }
579
580     /* Declare texture samplers */ 
581     for (i = 0; i < This->baseShader.limits.sampler; i++) {
582         if (reg_maps->samplers[i]) {
583
584             DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
585             switch (stype) {
586
587                 case WINED3DSTT_1D:
588                     shader_addline(buffer, "uniform sampler1D %csampler%lu;\n", prefix, i);
589                     break;
590                 case WINED3DSTT_2D:
591                     shader_addline(buffer, "uniform sampler2D %csampler%lu;\n", prefix, i);
592                     break;
593                 case WINED3DSTT_CUBE:
594                     shader_addline(buffer, "uniform samplerCube %csampler%lu;\n", prefix, i);
595                     break;
596                 case WINED3DSTT_VOLUME:
597                     shader_addline(buffer, "uniform sampler3D %csampler%lu;\n", prefix, i);
598                     break;
599                 default:
600                     shader_addline(buffer, "uniform unsupported_sampler %csampler%lu;\n", prefix, i);
601                     FIXME("Unrecognized sampler type: %#x\n", stype);
602                     break;
603             }
604         }
605     }
606     
607     /* Declare address variables */
608     for (i = 0; i < This->baseShader.limits.address; i++) {
609         if (reg_maps->address[i])
610             shader_addline(buffer, "ivec4 A%d;\n", i);
611     }
612
613     /* Declare texture coordinate temporaries and initialize them */
614     for (i = 0; i < This->baseShader.limits.texcoord; i++) {
615         if (reg_maps->texcoord[i]) 
616             shader_addline(buffer, "vec4 T%lu = gl_TexCoord[%lu];\n", i, i);
617     }
618
619     /* Declare input register temporaries */
620     for (i=0; i < This->baseShader.limits.packed_input; i++) {
621         if (reg_maps->packed_input[i])
622             shader_addline(buffer, "vec4 IN%lu;\n", i);
623     }
624
625     /* Declare output register temporaries */
626     for (i = 0; i < This->baseShader.limits.packed_output; i++) {
627         if (reg_maps->packed_output[i])
628             shader_addline(buffer, "vec4 OUT%lu;\n", i);
629     }
630
631     /* Declare temporary variables */
632     for(i = 0; i < This->baseShader.limits.temporary; i++) {
633         if (reg_maps->temporary[i])
634             shader_addline(buffer, "vec4 R%lu;\n", i);
635     }
636
637     /* Declare attributes */
638     for (i = 0; i < This->baseShader.limits.attributes; i++) {
639         if (reg_maps->attributes[i])
640             shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
641     }
642
643     /* Declare loop registers aLx */
644     for (i = 0; i < reg_maps->loop_depth; i++) {
645         shader_addline(buffer, "int aL%u;\n", i);
646         shader_addline(buffer, "int tmpInt%u;\n", i);
647     }
648
649     /* Temporary variables for matrix operations */
650     shader_addline(buffer, "vec4 tmp0;\n");
651     shader_addline(buffer, "vec4 tmp1;\n");
652
653     /* Start the main program */
654     shader_addline(buffer, "void main() {\n");
655     if(pshader && reg_maps->vpos) {
656         shader_addline(buffer, "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1) - 0.5;\n");
657     }
658 }
659
660 /*****************************************************************************
661  * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
662  *
663  * For more information, see http://wiki.winehq.org/DirectX-Shaders
664  ****************************************************************************/
665
666 /* Prototypes */
667 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
668         const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param);
669
670 /** Used for opcode modifiers - They multiply the result by the specified amount */
671 static const char * const shift_glsl_tab[] = {
672     "",           /*  0 (none) */ 
673     "2.0 * ",     /*  1 (x2)   */ 
674     "4.0 * ",     /*  2 (x4)   */ 
675     "8.0 * ",     /*  3 (x8)   */ 
676     "16.0 * ",    /*  4 (x16)  */ 
677     "32.0 * ",    /*  5 (x32)  */ 
678     "",           /*  6 (x64)  */ 
679     "",           /*  7 (x128) */ 
680     "",           /*  8 (d256) */ 
681     "",           /*  9 (d128) */ 
682     "",           /* 10 (d64)  */ 
683     "",           /* 11 (d32)  */ 
684     "0.0625 * ",  /* 12 (d16)  */ 
685     "0.125 * ",   /* 13 (d8)   */ 
686     "0.25 * ",    /* 14 (d4)   */ 
687     "0.5 * "      /* 15 (d2)   */ 
688 };
689
690 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
691 static void shader_glsl_gen_modifier (
692     const DWORD instr,
693     const char *in_reg,
694     const char *in_regswizzle,
695     char *out_str) {
696
697     out_str[0] = 0;
698     
699     if (instr == WINED3DSIO_TEXKILL)
700         return;
701
702     switch (instr & WINED3DSP_SRCMOD_MASK) {
703     case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
704     case WINED3DSPSM_DW:
705     case WINED3DSPSM_NONE:
706         sprintf(out_str, "%s%s", in_reg, in_regswizzle);
707         break;
708     case WINED3DSPSM_NEG:
709         sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
710         break;
711     case WINED3DSPSM_NOT:
712         sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
713         break;
714     case WINED3DSPSM_BIAS:
715         sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
716         break;
717     case WINED3DSPSM_BIASNEG:
718         sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
719         break;
720     case WINED3DSPSM_SIGN:
721         sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
722         break;
723     case WINED3DSPSM_SIGNNEG:
724         sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
725         break;
726     case WINED3DSPSM_COMP:
727         sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
728         break;
729     case WINED3DSPSM_X2:
730         sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
731         break;
732     case WINED3DSPSM_X2NEG:
733         sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
734         break;
735     case WINED3DSPSM_ABS:
736         sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
737         break;
738     case WINED3DSPSM_ABSNEG:
739         sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
740         break;
741     default:
742         FIXME("Unhandled modifier %u\n", (instr & WINED3DSP_SRCMOD_MASK));
743         sprintf(out_str, "%s%s", in_reg, in_regswizzle);
744     }
745 }
746
747 /** Writes the GLSL variable name that corresponds to the register that the
748  * DX opcode parameter is trying to access */
749 static void shader_glsl_get_register_name(
750     const DWORD param,
751     const DWORD addr_token,
752     char* regstr,
753     BOOL* is_color,
754     SHADER_OPCODE_ARG* arg) {
755
756     /* oPos, oFog and oPts in D3D */
757     static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
758
759     DWORD reg = param & WINED3DSP_REGNUM_MASK;
760     DWORD regtype = shader_get_regtype(param);
761     IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
762     IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
763     WineD3D_GL_Info* gl_info = &deviceImpl->adapter->gl_info;
764
765     char pshader = shader_is_pshader_version(This->baseShader.hex_version);
766     char tmpStr[50];
767
768     *is_color = FALSE;   
769  
770     switch (regtype) {
771     case WINED3DSPR_TEMP:
772         sprintf(tmpStr, "R%u", reg);
773     break;
774     case WINED3DSPR_INPUT:
775         if (pshader) {
776             /* Pixel shaders >= 3.0 */
777             if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
778                 sprintf(tmpStr, "IN%u", reg);
779              else {
780                 if (reg==0)
781                     strcpy(tmpStr, "gl_Color");
782                 else
783                     strcpy(tmpStr, "gl_SecondaryColor");
784             }
785         } else {
786             if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
787                *is_color = TRUE;
788             sprintf(tmpStr, "attrib%u", reg);
789         } 
790         break;
791     case WINED3DSPR_CONST:
792     {
793         const char* prefix = pshader? "PC":"VC";
794
795         /* Relative addressing */
796         if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
797
798            /* Relative addressing on shaders 2.0+ have a relative address token, 
799             * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
800            if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2)  {
801                glsl_src_param_t rel_param;
802                shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
803                sprintf(tmpStr, "%s[%s + %u]", prefix, rel_param.param_str, reg);
804            } else
805                sprintf(tmpStr, "%s[A0.x + %u]", prefix, reg);
806
807         } else
808              sprintf(tmpStr, "%s[%u]", prefix, reg);
809
810         break;
811     }
812     case WINED3DSPR_CONSTINT:
813         if (pshader)
814             sprintf(tmpStr, "PI[%u]", reg);
815         else
816             sprintf(tmpStr, "VI[%u]", reg);
817         break;
818     case WINED3DSPR_CONSTBOOL:
819         if (pshader)
820             sprintf(tmpStr, "PB[%u]", reg);
821         else
822             sprintf(tmpStr, "VB[%u]", reg);
823         break;
824     case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
825         if (pshader) {
826             sprintf(tmpStr, "T%u", reg);
827         } else {
828             sprintf(tmpStr, "A%u", reg);
829         }
830     break;
831     case WINED3DSPR_LOOP:
832         sprintf(tmpStr, "aL%u", This->baseShader.cur_loop_regno - 1);
833     break;
834     case WINED3DSPR_SAMPLER:
835         if (pshader)
836             sprintf(tmpStr, "Psampler%u", reg);
837         else
838             sprintf(tmpStr, "Vsampler%u", reg);
839     break;
840     case WINED3DSPR_COLOROUT:
841         if (reg >= GL_LIMITS(buffers)) {
842             WARN("Write to render target %u, only %d supported\n", reg, 4);
843         }
844         if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
845             sprintf(tmpStr, "gl_FragData[%u]", reg);
846         } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
847             sprintf(tmpStr, "gl_FragColor");
848         }
849     break;
850     case WINED3DSPR_RASTOUT:
851         sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
852     break;
853     case WINED3DSPR_DEPTHOUT:
854         sprintf(tmpStr, "gl_FragDepth");
855     break;
856     case WINED3DSPR_ATTROUT:
857         if (reg == 0) {
858             sprintf(tmpStr, "gl_FrontColor");
859         } else {
860             sprintf(tmpStr, "gl_FrontSecondaryColor");
861         }
862     break;
863     case WINED3DSPR_TEXCRDOUT:
864         /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
865         if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
866             sprintf(tmpStr, "OUT%u", reg);
867         else
868             sprintf(tmpStr, "gl_TexCoord[%u]", reg);
869     break;
870     case WINED3DSPR_MISCTYPE:
871         if (reg == 0) {
872             /* vPos */
873             sprintf(tmpStr, "vpos");
874         } else if (reg == 1){
875             /* Note that gl_FrontFacing is a bool, while vFace is
876              * a float for which the sign determines front/back
877              */
878             sprintf(tmpStr, "(gl_FrontFacing ? 1.0 : -1.0)");
879         } else {
880             FIXME("Unhandled misctype register %d\n", reg);
881             sprintf(tmpStr, "unrecognized_register");
882         }
883         break;
884     default:
885         FIXME("Unhandled register name Type(%d)\n", regtype);
886         sprintf(tmpStr, "unrecognized_register");
887     break;
888     }
889
890     strcat(regstr, tmpStr);
891 }
892
893 /* Get the GLSL write mask for the destination register */
894 static DWORD shader_glsl_get_write_mask(const DWORD param, char *write_mask) {
895     char *ptr = write_mask;
896     DWORD mask = param & WINED3DSP_WRITEMASK_ALL;
897
898     if (shader_is_scalar(param)) {
899         mask = WINED3DSP_WRITEMASK_0;
900     } else {
901         *ptr++ = '.';
902         if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
903         if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
904         if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
905         if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
906     }
907
908     *ptr = '\0';
909
910     return mask;
911 }
912
913 static size_t shader_glsl_get_write_mask_size(DWORD write_mask) {
914     size_t size = 0;
915
916     if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
917     if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
918     if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
919     if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
920
921     return size;
922 }
923
924 static void shader_glsl_get_swizzle(const DWORD param, BOOL fixup, DWORD mask, char *swizzle_str) {
925     /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
926      * but addressed as "rgba". To fix this we need to swap the register's x
927      * and z components. */
928     DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
929     const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
930     char *ptr = swizzle_str;
931
932     if (!shader_is_scalar(param)) {
933         *ptr++ = '.';
934         /* swizzle bits fields: wwzzyyxx */
935         if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = swizzle_chars[swizzle & 0x03];
936         if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = swizzle_chars[(swizzle >> 2) & 0x03];
937         if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = swizzle_chars[(swizzle >> 4) & 0x03];
938         if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = swizzle_chars[(swizzle >> 6) & 0x03];
939     }
940
941     *ptr = '\0';
942 }
943
944 /* From a given parameter token, generate the corresponding GLSL string.
945  * Also, return the actual register name and swizzle in case the
946  * caller needs this information as well. */
947 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
948         const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param) {
949     BOOL is_color = FALSE;
950     char swizzle_str[6];
951
952     src_param->reg_name[0] = '\0';
953     src_param->param_str[0] = '\0';
954     swizzle_str[0] = '\0';
955
956     shader_glsl_get_register_name(param, addr_token, src_param->reg_name, &is_color, arg);
957
958     shader_glsl_get_swizzle(param, is_color, mask, swizzle_str);
959     shader_glsl_gen_modifier(param, src_param->reg_name, swizzle_str, src_param->param_str);
960 }
961
962 /* From a given parameter token, generate the corresponding GLSL string.
963  * Also, return the actual register name and swizzle in case the
964  * caller needs this information as well. */
965 static DWORD shader_glsl_add_dst_param(SHADER_OPCODE_ARG* arg, const DWORD param,
966         const DWORD addr_token, glsl_dst_param_t *dst_param) {
967     BOOL is_color = FALSE;
968
969     dst_param->mask_str[0] = '\0';
970     dst_param->reg_name[0] = '\0';
971
972     shader_glsl_get_register_name(param, addr_token, dst_param->reg_name, &is_color, arg);
973     return shader_glsl_get_write_mask(param, dst_param->mask_str);
974 }
975
976 /* Append the destination part of the instruction to the buffer, return the effective write mask */
977 static DWORD shader_glsl_append_dst_ext(SHADER_BUFFER *buffer, SHADER_OPCODE_ARG *arg, const DWORD param) {
978     glsl_dst_param_t dst_param;
979     DWORD mask;
980     int shift;
981
982     mask = shader_glsl_add_dst_param(arg, param, arg->dst_addr, &dst_param);
983
984     if(mask) {
985         shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
986         shader_addline(buffer, "%s%s = %s(", dst_param.reg_name, dst_param.mask_str, shift_glsl_tab[shift]);
987     }
988
989     return mask;
990 }
991
992 /* Append the destination part of the instruction to the buffer, return the effective write mask */
993 static DWORD shader_glsl_append_dst(SHADER_BUFFER *buffer, SHADER_OPCODE_ARG *arg) {
994     return shader_glsl_append_dst_ext(buffer, arg, arg->dst);
995 }
996
997 /** Process GLSL instruction modifiers */
998 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
999     
1000     DWORD mask = arg->dst & WINED3DSP_DSTMOD_MASK;
1001  
1002     if (arg->opcode->dst_token && mask != 0) {
1003         glsl_dst_param_t dst_param;
1004
1005         shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
1006
1007         if (mask & WINED3DSPDM_SATURATE) {
1008             /* _SAT means to clamp the value of the register to between 0 and 1 */
1009             shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1010                     dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1011         }
1012         if (mask & WINED3DSPDM_MSAMPCENTROID) {
1013             FIXME("_centroid modifier not handled\n");
1014         }
1015         if (mask & WINED3DSPDM_PARTIALPRECISION) {
1016             /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1017         }
1018     }
1019 }
1020
1021 static inline const char* shader_get_comp_op(
1022     const DWORD opcode) {
1023
1024     DWORD op = (opcode & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1025     switch (op) {
1026         case COMPARISON_GT: return ">";
1027         case COMPARISON_EQ: return "==";
1028         case COMPARISON_GE: return ">=";
1029         case COMPARISON_LT: return "<";
1030         case COMPARISON_NE: return "!=";
1031         case COMPARISON_LE: return "<=";
1032         default:
1033             FIXME("Unrecognized comparison value: %u\n", op);
1034             return "(\?\?)";
1035     }
1036 }
1037
1038 static void shader_glsl_get_sample_function(DWORD sampler_type, BOOL projected, glsl_sample_function_t *sample_function) {
1039     /* Note that there's no such thing as a projected cube texture. */
1040     switch(sampler_type) {
1041         case WINED3DSTT_1D:
1042             sample_function->name = projected ? "texture1DProj" : "texture1D";
1043             sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1044             break;
1045         case WINED3DSTT_2D:
1046             sample_function->name = projected ? "texture2DProj" : "texture2D";
1047             sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1048             break;
1049         case WINED3DSTT_CUBE:
1050             sample_function->name = "textureCube";
1051             sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1052             break;
1053         case WINED3DSTT_VOLUME:
1054             sample_function->name = projected ? "texture3DProj" : "texture3D";
1055             sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1056             break;
1057         default:
1058             sample_function->name = "";
1059             FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
1060             break;
1061     }
1062 }
1063
1064 static void shader_glsl_color_correction(SHADER_OPCODE_ARG* arg) {
1065     IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1066     IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) shader->baseShader.device;
1067     WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
1068     glsl_dst_param_t dst_param;
1069     glsl_dst_param_t dst_param2;
1070     WINED3DFORMAT fmt;
1071     WINED3DFORMAT conversion_group;
1072     IWineD3DBaseTextureImpl *texture;
1073     DWORD mask, mask_size;
1074     UINT i;
1075     BOOL recorded = FALSE;
1076     DWORD sampler_idx;
1077     DWORD hex_version = shader->baseShader.hex_version;
1078
1079     switch(arg->opcode->opcode) {
1080         case WINED3DSIO_TEX:
1081             if (hex_version < WINED3DPS_VERSION(2,0)) {
1082                 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1083             } else {
1084                 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
1085             }
1086             break;
1087
1088         case WINED3DSIO_TEXLDL:
1089             FIXME("Add color fixup for vertex texture WINED3DSIO_TEXLDL\n");
1090             return;
1091
1092         case WINED3DSIO_TEXDP3TEX:
1093         case WINED3DSIO_TEXM3x3TEX:
1094         case WINED3DSIO_TEXM3x3SPEC:
1095         case WINED3DSIO_TEXM3x3VSPEC:
1096         case WINED3DSIO_TEXBEM:
1097         case WINED3DSIO_TEXREG2AR:
1098         case WINED3DSIO_TEXREG2GB:
1099         case WINED3DSIO_TEXREG2RGB:
1100             sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1101             break;
1102
1103         default:
1104             /* Not a texture sampling instruction, nothing to do */
1105             return;
1106     };
1107
1108     texture = (IWineD3DBaseTextureImpl *) deviceImpl->stateBlock->textures[sampler_idx];
1109     if(texture) {
1110         fmt = texture->resource.format;
1111         conversion_group = texture->baseTexture.shader_conversion_group;
1112     } else {
1113         fmt = WINED3DFMT_UNKNOWN;
1114         conversion_group = WINED3DFMT_UNKNOWN;
1115     }
1116
1117     /* before doing anything, record the sampler with the format in the format conversion list,
1118      * but check if it's not there already
1119      */
1120     for(i = 0; i < shader->baseShader.num_sampled_samplers; i++) {
1121         if(shader->baseShader.sampled_samplers[i] == sampler_idx) {
1122             recorded = TRUE;
1123             break;
1124         }
1125     }
1126     if(!recorded) {
1127         shader->baseShader.sampled_samplers[shader->baseShader.num_sampled_samplers] = sampler_idx;
1128         shader->baseShader.num_sampled_samplers++;
1129         shader->baseShader.sampled_format[sampler_idx] = conversion_group;
1130     }
1131
1132     switch(fmt) {
1133         case WINED3DFMT_V8U8:
1134         case WINED3DFMT_V16U16:
1135             if(GL_SUPPORT(NV_TEXTURE_SHADER) ||
1136               (GL_SUPPORT(ATI_ENVMAP_BUMPMAP) && fmt == WINED3DFMT_V8U8)) {
1137                 /* The 3rd channel returns 1.0 in d3d, but 0.0 in gl. Fix this while we're at it :-) */
1138                 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_2, &dst_param);
1139                 mask_size = shader_glsl_get_write_mask_size(mask);
1140                 if(mask_size >= 3) {
1141                     shader_addline(arg->buffer, "%s.%c = 1.0;\n", dst_param.reg_name, dst_param.mask_str[3]);
1142                 }
1143             } else {
1144                 /* Correct the sign, but leave the blue as it is - it was loaded correctly already */
1145                 mask = shader_glsl_add_dst_param(arg, arg->dst,
1146                                           WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1,
1147                                           &dst_param);
1148                 mask_size = shader_glsl_get_write_mask_size(mask);
1149                 if(mask_size >= 2) {
1150                     shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1151                     dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2],
1152                     dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2]);
1153                 } else if(mask_size == 1) {
1154                     shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n", dst_param.reg_name, dst_param.mask_str[1],
1155                     dst_param.reg_name, dst_param.mask_str[1]);
1156                 }
1157             }
1158             break;
1159
1160         case WINED3DFMT_X8L8V8U8:
1161             if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1162                 /* Red and blue are the signed channels, fix them up; Blue(=L) is correct already,
1163                  * and a(X) is always 1.0
1164                  */
1165                 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &dst_param);
1166                 mask_size = shader_glsl_get_write_mask_size(mask);
1167                 if(mask_size >= 2) {
1168                     shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1169                                    dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2],
1170                                    dst_param.reg_name, dst_param.mask_str[1], dst_param.mask_str[2]);
1171                 } else if(mask_size == 1) {
1172                     shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1173                                    dst_param.reg_name, dst_param.mask_str[1],
1174                                    dst_param.reg_name, dst_param.mask_str[1]);
1175                 }
1176             }
1177             break;
1178
1179         case WINED3DFMT_L6V5U5:
1180             if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1181                 mask = shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &dst_param);
1182                 mask_size = shader_glsl_get_write_mask_size(mask);
1183                 shader_glsl_add_dst_param(arg, arg->dst, WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_2, &dst_param2);
1184                 if(mask_size >= 3) {
1185                     /* Swap y and z (U and L), and do a sign conversion on x and the new y(V and U) */
1186                     shader_addline(arg->buffer, "tmp0.g = %s.%c;\n",
1187                                    dst_param.reg_name, dst_param.mask_str[2]);
1188                     shader_addline(arg->buffer, "%s.%c%c = %s.%c%c * 2.0 - 1.0;\n",
1189                                    dst_param.reg_name, dst_param.mask_str[2], dst_param.mask_str[1],
1190                                    dst_param2.reg_name, dst_param.mask_str[1], dst_param.mask_str[3]);
1191                     shader_addline(arg->buffer, "%s.%c = tmp0.g;\n", dst_param.reg_name,
1192                                    dst_param.mask_str[3]);
1193                 } else if(mask_size == 2) {
1194                     /* This is bad: We have VL, but we need VU */
1195                     FIXME("2 components sampled from a converted L6V5U5 texture\n");
1196                 } else {
1197                     shader_addline(arg->buffer, "%s.%c = %s.%c * 2.0 - 1.0;\n",
1198                                    dst_param.reg_name, dst_param.mask_str[1],
1199                                    dst_param2.reg_name, dst_param.mask_str[1]);
1200                 }
1201             }
1202             break;
1203
1204         case WINED3DFMT_Q8W8V8U8:
1205             if(!GL_SUPPORT(NV_TEXTURE_SHADER)) {
1206                 /* Correct the sign in all channels. The writemask just applies as-is, no
1207                  * need for checking the mask size
1208                  */
1209                 shader_glsl_add_dst_param(arg, arg->dst,
1210                                           WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 |
1211                                           WINED3DSP_WRITEMASK_2 | WINED3DSP_WRITEMASK_3,
1212                                           &dst_param);
1213                 shader_addline(arg->buffer, "%s%s = %s%s * 2.0 - 1.0;\n", dst_param.reg_name, dst_param.mask_str,
1214                                dst_param.reg_name, dst_param.mask_str);
1215             }
1216             break;
1217
1218             /* stupid compiler */
1219         default:
1220             break;
1221     }
1222 }
1223
1224 /*****************************************************************************
1225  * 
1226  * Begin processing individual instruction opcodes
1227  * 
1228  ****************************************************************************/
1229
1230 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
1231 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
1232     CONST SHADER_OPCODE* curOpcode = arg->opcode;
1233     SHADER_BUFFER* buffer = arg->buffer;
1234     glsl_src_param_t src0_param;
1235     glsl_src_param_t src1_param;
1236     DWORD write_mask;
1237     char op;
1238
1239     /* Determine the GLSL operator to use based on the opcode */
1240     switch (curOpcode->opcode) {
1241         case WINED3DSIO_MUL: op = '*'; break;
1242         case WINED3DSIO_ADD: op = '+'; break;
1243         case WINED3DSIO_SUB: op = '-'; break;
1244         default:
1245             op = ' ';
1246             FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1247             break;
1248     }
1249
1250     write_mask = shader_glsl_append_dst(buffer, arg);
1251     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1252     shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1253     shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
1254 }
1255
1256 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1257 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
1258     IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1259     SHADER_BUFFER* buffer = arg->buffer;
1260     glsl_src_param_t src0_param;
1261     DWORD write_mask;
1262
1263     write_mask = shader_glsl_append_dst(buffer, arg);
1264     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1265
1266     /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1267      * shader versions WINED3DSIO_MOVA is used for this. */
1268     if ((WINED3DSHADER_VERSION_MAJOR(shader->baseShader.hex_version) == 1 &&
1269             !shader_is_pshader_version(shader->baseShader.hex_version) &&
1270             shader_get_regtype(arg->dst) == WINED3DSPR_ADDR) ||
1271             arg->opcode->opcode == WINED3DSIO_MOVA) {
1272         /* We need to *round* to the nearest int here. */
1273         size_t mask_size = shader_glsl_get_write_mask_size(write_mask);
1274         if (mask_size > 1) {
1275             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);
1276         } else {
1277             shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param.param_str, src0_param.param_str);
1278         }
1279     } else {
1280         shader_addline(buffer, "%s);\n", src0_param.param_str);
1281     }
1282 }
1283
1284 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
1285 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
1286     CONST SHADER_OPCODE* curOpcode = arg->opcode;
1287     SHADER_BUFFER* buffer = arg->buffer;
1288     glsl_src_param_t src0_param;
1289     glsl_src_param_t src1_param;
1290     DWORD dst_write_mask, src_write_mask;
1291     size_t dst_size = 0;
1292
1293     dst_write_mask = shader_glsl_append_dst(buffer, arg);
1294     dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1295
1296     /* dp3 works on vec3, dp4 on vec4 */
1297     if (curOpcode->opcode == WINED3DSIO_DP4) {
1298         src_write_mask = WINED3DSP_WRITEMASK_ALL;
1299     } else {
1300         src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1301     }
1302
1303     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_write_mask, &src0_param);
1304     shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_write_mask, &src1_param);
1305
1306     if (dst_size > 1) {
1307         shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1308     } else {
1309         shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
1310     }
1311 }
1312
1313 /* Note that this instruction has some restrictions. The destination write mask
1314  * can't contain the w component, and the source swizzles have to be .xyzw */
1315 void shader_glsl_cross(SHADER_OPCODE_ARG *arg) {
1316     DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1317     glsl_src_param_t src0_param;
1318     glsl_src_param_t src1_param;
1319     char dst_mask[6];
1320
1321     shader_glsl_get_write_mask(arg->dst, dst_mask);
1322     shader_glsl_append_dst(arg->buffer, arg);
1323     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1324     shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
1325     shader_addline(arg->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
1326 }
1327
1328 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1329  * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1330  * GLSL uses the value as-is. */
1331 void shader_glsl_pow(SHADER_OPCODE_ARG *arg) {
1332     SHADER_BUFFER *buffer = arg->buffer;
1333     glsl_src_param_t src0_param;
1334     glsl_src_param_t src1_param;
1335     DWORD dst_write_mask;
1336     size_t dst_size;
1337
1338     dst_write_mask = shader_glsl_append_dst(buffer, arg);
1339     dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1340
1341     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1342     shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1343
1344     if (dst_size > 1) {
1345         shader_addline(buffer, "vec%d(pow(abs(%s), %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1346     } else {
1347         shader_addline(buffer, "pow(abs(%s), %s));\n", src0_param.param_str, src1_param.param_str);
1348     }
1349 }
1350
1351 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
1352  * Src0 is a scalar. Note that D3D uses the absolute of src0, while
1353  * GLSL uses the value as-is. */
1354 void shader_glsl_log(SHADER_OPCODE_ARG *arg) {
1355     SHADER_BUFFER *buffer = arg->buffer;
1356     glsl_src_param_t src0_param;
1357     DWORD dst_write_mask;
1358     size_t dst_size;
1359
1360     dst_write_mask = shader_glsl_append_dst(buffer, arg);
1361     dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1362
1363     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1364
1365     if (dst_size > 1) {
1366         shader_addline(buffer, "vec%d(log2(abs(%s))));\n", dst_size, src0_param.param_str);
1367     } else {
1368         shader_addline(buffer, "log2(abs(%s)));\n", src0_param.param_str);
1369     }
1370 }
1371
1372 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1373 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
1374     CONST SHADER_OPCODE* curOpcode = arg->opcode;
1375     SHADER_BUFFER* buffer = arg->buffer;
1376     glsl_src_param_t src_param;
1377     const char *instruction;
1378     char arguments[256];
1379     DWORD write_mask;
1380     unsigned i;
1381
1382     /* Determine the GLSL function to use based on the opcode */
1383     /* TODO: Possibly make this a table for faster lookups */
1384     switch (curOpcode->opcode) {
1385         case WINED3DSIO_MIN: instruction = "min"; break;
1386         case WINED3DSIO_MAX: instruction = "max"; break;
1387         case WINED3DSIO_ABS: instruction = "abs"; break;
1388         case WINED3DSIO_FRC: instruction = "fract"; break;
1389         case WINED3DSIO_NRM: instruction = "normalize"; break;
1390         case WINED3DSIO_LOGP:
1391         case WINED3DSIO_LOG: instruction = "log2"; break;
1392         case WINED3DSIO_EXP: instruction = "exp2"; break;
1393         case WINED3DSIO_SGN: instruction = "sign"; break;
1394         case WINED3DSIO_DSX: instruction = "dFdx"; break;
1395         case WINED3DSIO_DSY: instruction = "dFdy"; break;
1396         default: instruction = "";
1397             FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1398             break;
1399     }
1400
1401     write_mask = shader_glsl_append_dst(buffer, arg);
1402
1403     arguments[0] = '\0';
1404     if (curOpcode->num_params > 0) {
1405         shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src_param);
1406         strcat(arguments, src_param.param_str);
1407         for (i = 2; i < curOpcode->num_params; ++i) {
1408             strcat(arguments, ", ");
1409             shader_glsl_add_src_param(arg, arg->src[i-1], arg->src_addr[i-1], write_mask, &src_param);
1410             strcat(arguments, src_param.param_str);
1411         }
1412     }
1413
1414     shader_addline(buffer, "%s(%s));\n", instruction, arguments);
1415 }
1416
1417 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1418  * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1419  *   dst.x = 2^(floor(src))
1420  *   dst.y = src - floor(src)
1421  *   dst.z = 2^src   (partial precision is allowed, but optional)
1422  *   dst.w = 1.0;
1423  * For 2.0 shaders, just do this (honoring writemask and swizzle):
1424  *   dst = 2^src;    (partial precision is allowed, but optional)
1425  */
1426 void shader_glsl_expp(SHADER_OPCODE_ARG* arg) {
1427     IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)arg->shader;
1428     glsl_src_param_t src_param;
1429
1430     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src_param);
1431
1432     if (shader->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
1433         char dst_mask[6];
1434
1435         shader_addline(arg->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
1436         shader_addline(arg->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
1437         shader_addline(arg->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
1438         shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1439
1440         shader_glsl_append_dst(arg->buffer, arg);
1441         shader_glsl_get_write_mask(arg->dst, dst_mask);
1442         shader_addline(arg->buffer, "tmp0%s);\n", dst_mask);
1443     } else {
1444         DWORD write_mask;
1445         size_t mask_size;
1446
1447         write_mask = shader_glsl_append_dst(arg->buffer, arg);
1448         mask_size = shader_glsl_get_write_mask_size(write_mask);
1449
1450         if (mask_size > 1) {
1451             shader_addline(arg->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
1452         } else {
1453             shader_addline(arg->buffer, "exp2(%s));\n", src_param.param_str);
1454         }
1455     }
1456 }
1457
1458 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1459 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
1460     glsl_src_param_t src_param;
1461     DWORD write_mask;
1462     size_t mask_size;
1463
1464     write_mask = shader_glsl_append_dst(arg->buffer, arg);
1465     mask_size = shader_glsl_get_write_mask_size(write_mask);
1466     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1467
1468     if (mask_size > 1) {
1469         shader_addline(arg->buffer, "vec%d(1.0 / %s));\n", mask_size, src_param.param_str);
1470     } else {
1471         shader_addline(arg->buffer, "1.0 / %s);\n", src_param.param_str);
1472     }
1473 }
1474
1475 void shader_glsl_rsq(SHADER_OPCODE_ARG* arg) {
1476     SHADER_BUFFER* buffer = arg->buffer;
1477     glsl_src_param_t src_param;
1478     DWORD write_mask;
1479     size_t mask_size;
1480
1481     write_mask = shader_glsl_append_dst(buffer, arg);
1482     mask_size = shader_glsl_get_write_mask_size(write_mask);
1483
1484     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1485
1486     if (mask_size > 1) {
1487         shader_addline(buffer, "vec%d(inversesqrt(%s)));\n", mask_size, src_param.param_str);
1488     } else {
1489         shader_addline(buffer, "inversesqrt(%s));\n", src_param.param_str);
1490     }
1491 }
1492
1493 /** Process signed comparison opcodes in GLSL. */
1494 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
1495     glsl_src_param_t src0_param;
1496     glsl_src_param_t src1_param;
1497     DWORD write_mask;
1498     size_t mask_size;
1499
1500     write_mask = shader_glsl_append_dst(arg->buffer, arg);
1501     mask_size = shader_glsl_get_write_mask_size(write_mask);
1502     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1503     shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1504
1505     if (mask_size > 1) {
1506         const char *compare;
1507
1508         switch(arg->opcode->opcode) {
1509             case WINED3DSIO_SLT: compare = "lessThan"; break;
1510             case WINED3DSIO_SGE: compare = "greaterThanEqual"; break;
1511             default: compare = "";
1512                 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1513         }
1514
1515         shader_addline(arg->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
1516                 src0_param.param_str, src1_param.param_str);
1517     } else {
1518         const char *compare;
1519
1520         switch(arg->opcode->opcode) {
1521             case WINED3DSIO_SLT: compare = "<"; break;
1522             case WINED3DSIO_SGE: compare = ">="; break;
1523             default: compare = "";
1524                 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1525         }
1526
1527         shader_addline(arg->buffer, "(%s %s %s) ? 1.0 : 0.0);\n",
1528                 src0_param.param_str, compare, src1_param.param_str);
1529     }
1530 }
1531
1532 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1533 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
1534     glsl_src_param_t src0_param;
1535     glsl_src_param_t src1_param;
1536     glsl_src_param_t src2_param;
1537     DWORD write_mask, cmp_channel = 0;
1538     unsigned int i, j;
1539     char mask_char[6];
1540     BOOL temp_destination = FALSE;
1541
1542     DWORD src0reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
1543     DWORD src1reg = arg->src[1] & WINED3DSP_REGNUM_MASK;
1544     DWORD src2reg = arg->src[2] & WINED3DSP_REGNUM_MASK;
1545     DWORD src0regtype = shader_get_regtype(arg->src[0]);
1546     DWORD src1regtype = shader_get_regtype(arg->src[1]);
1547     DWORD src2regtype = shader_get_regtype(arg->src[2]);
1548     DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1549     DWORD dstregtype = shader_get_regtype(arg->dst);
1550
1551     /* Cycle through all source0 channels */
1552     for (i=0; i<4; i++) {
1553         write_mask = 0;
1554         /* Find the destination channels which use the current source0 channel */
1555         for (j=0; j<4; j++) {
1556             if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1557                 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1558                 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1559             }
1560         }
1561
1562         /* Splitting the cmp instruction up in multiple lines imposes a problem:
1563          * The first lines may overwrite source parameters of the following lines.
1564          * Deal with that by using a temporary destination register if needed
1565          */
1566         if((src0reg == dstreg && src0regtype == dstregtype) ||
1567            (src1reg == dstreg && src1regtype == dstregtype) ||
1568            (src2reg == dstreg && src2regtype == dstregtype)) {
1569
1570             write_mask = shader_glsl_get_write_mask(arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask), mask_char);
1571             if (!write_mask) continue;
1572             shader_addline(arg->buffer, "tmp0%s = (", mask_char);
1573             temp_destination = TRUE;
1574         } else {
1575             write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1576             if (!write_mask) continue;
1577         }
1578
1579         shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1580         shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1581         shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1582
1583             shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1584                            src0_param.param_str, src1_param.param_str, src2_param.param_str);
1585     }
1586
1587     if(temp_destination) {
1588         shader_glsl_get_write_mask(arg->dst, mask_char);
1589         shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst);
1590         shader_addline(arg->buffer, "tmp0%s);\n", mask_char);
1591     }
1592
1593 }
1594
1595 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
1596 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1597  * the compare is done per component of src0. */
1598 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
1599     IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1600     glsl_src_param_t src0_param;
1601     glsl_src_param_t src1_param;
1602     glsl_src_param_t src2_param;
1603     DWORD write_mask, cmp_channel = 0;
1604     unsigned int i, j;
1605
1606     if (shader->baseShader.hex_version < WINED3DPS_VERSION(1, 4)) {
1607         write_mask = shader_glsl_append_dst(arg->buffer, arg);
1608         shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1609         shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1610         shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1611
1612         /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
1613         if(arg->opcode_token & WINED3DSI_COISSUE) {
1614             shader_addline(arg->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
1615         } else {
1616             shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1617                     src0_param.param_str, src1_param.param_str, src2_param.param_str);
1618         }
1619         return;
1620     }
1621     /* Cycle through all source0 channels */
1622     for (i=0; i<4; i++) {
1623         write_mask = 0;
1624         /* Find the destination channels which use the current source0 channel */
1625         for (j=0; j<4; j++) {
1626             if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1627                 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1628                 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1629             }
1630         }
1631         write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1632         if (!write_mask) continue;
1633
1634         shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1635         shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1636         shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1637
1638         shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1639                 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1640     }
1641 }
1642
1643 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1644 void shader_glsl_mad(SHADER_OPCODE_ARG* arg) {
1645     glsl_src_param_t src0_param;
1646     glsl_src_param_t src1_param;
1647     glsl_src_param_t src2_param;
1648     DWORD write_mask;
1649
1650     write_mask = shader_glsl_append_dst(arg->buffer, arg);
1651     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1652     shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1653     shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1654     shader_addline(arg->buffer, "(%s * %s) + %s);\n",
1655             src0_param.param_str, src1_param.param_str, src2_param.param_str);
1656 }
1657
1658 /** Handles transforming all WINED3DSIO_M?x? opcodes for 
1659     Vertex shaders to GLSL codes */
1660 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
1661     int i;
1662     int nComponents = 0;
1663     SHADER_OPCODE_ARG tmpArg;
1664    
1665     memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1666
1667     /* Set constants for the temporary argument */
1668     tmpArg.shader      = arg->shader;
1669     tmpArg.buffer      = arg->buffer;
1670     tmpArg.src[0]      = arg->src[0];
1671     tmpArg.src_addr[0] = arg->src_addr[0];
1672     tmpArg.src_addr[1] = arg->src_addr[1];
1673     tmpArg.reg_maps = arg->reg_maps; 
1674     
1675     switch(arg->opcode->opcode) {
1676         case WINED3DSIO_M4x4:
1677             nComponents = 4;
1678             tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1679             break;
1680         case WINED3DSIO_M4x3:
1681             nComponents = 3;
1682             tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1683             break;
1684         case WINED3DSIO_M3x4:
1685             nComponents = 4;
1686             tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1687             break;
1688         case WINED3DSIO_M3x3:
1689             nComponents = 3;
1690             tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1691             break;
1692         case WINED3DSIO_M3x2:
1693             nComponents = 2;
1694             tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1695             break;
1696         default:
1697             break;
1698     }
1699
1700     for (i = 0; i < nComponents; i++) {
1701         tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1702         tmpArg.src[1]      = arg->src[1]+i;
1703         shader_glsl_dot(&tmpArg);
1704     }
1705 }
1706
1707 /**
1708     The LRP instruction performs a component-wise linear interpolation 
1709     between the second and third operands using the first operand as the
1710     blend factor.  Equation:  (dst = src2 + src0 * (src1 - src2))
1711     This is equivalent to mix(src2, src1, src0);
1712 */
1713 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
1714     glsl_src_param_t src0_param;
1715     glsl_src_param_t src1_param;
1716     glsl_src_param_t src2_param;
1717     DWORD write_mask;
1718
1719     write_mask = shader_glsl_append_dst(arg->buffer, arg);
1720
1721     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &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);
1724
1725     shader_addline(arg->buffer, "mix(%s, %s, %s));\n",
1726             src2_param.param_str, src1_param.param_str, src0_param.param_str);
1727 }
1728
1729 /** Process the WINED3DSIO_LIT instruction in GLSL:
1730  * dst.x = dst.w = 1.0
1731  * dst.y = (src0.x > 0) ? src0.x
1732  * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1733  *                                        where src.w is clamped at +- 128
1734  */
1735 void shader_glsl_lit(SHADER_OPCODE_ARG* arg) {
1736     glsl_src_param_t src0_param;
1737     glsl_src_param_t src1_param;
1738     glsl_src_param_t src3_param;
1739     char dst_mask[6];
1740
1741     shader_glsl_append_dst(arg->buffer, arg);
1742     shader_glsl_get_write_mask(arg->dst, dst_mask);
1743
1744     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1745     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src1_param);
1746     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src3_param);
1747
1748     shader_addline(arg->buffer, "vec4(1.0, (%s > 0.0 ? %s : 0.0), (%s > 0.0 ? ((%s > 0.0) ? pow(%s, clamp(%s, -128.0, 128.0)) : 0.0) : 0.0), 1.0)%s);\n",
1749         src0_param.param_str, src0_param.param_str, src0_param.param_str, src1_param.param_str, src1_param.param_str, src3_param.param_str, dst_mask);
1750 }
1751
1752 /** Process the WINED3DSIO_DST instruction in GLSL:
1753  * dst.x = 1.0
1754  * dst.y = src0.x * src0.y
1755  * dst.z = src0.z
1756  * dst.w = src1.w
1757  */
1758 void shader_glsl_dst(SHADER_OPCODE_ARG* arg) {
1759     glsl_src_param_t src0y_param;
1760     glsl_src_param_t src0z_param;
1761     glsl_src_param_t src1y_param;
1762     glsl_src_param_t src1w_param;
1763     char dst_mask[6];
1764
1765     shader_glsl_append_dst(arg->buffer, arg);
1766     shader_glsl_get_write_mask(arg->dst, dst_mask);
1767
1768     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src0y_param);
1769     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &src0z_param);
1770     shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_1, &src1y_param);
1771     shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_3, &src1w_param);
1772
1773     shader_addline(arg->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
1774             src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
1775 }
1776
1777 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1778  * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1779  * can handle it.  But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1780  * 
1781  * dst.x = cos(src0.?)
1782  * dst.y = sin(src0.?)
1783  * dst.z = dst.z
1784  * dst.w = dst.w
1785  */
1786 void shader_glsl_sincos(SHADER_OPCODE_ARG* arg) {
1787     glsl_src_param_t src0_param;
1788     DWORD write_mask;
1789
1790     write_mask = shader_glsl_append_dst(arg->buffer, arg);
1791     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1792
1793     switch (write_mask) {
1794         case WINED3DSP_WRITEMASK_0:
1795             shader_addline(arg->buffer, "cos(%s));\n", src0_param.param_str);
1796             break;
1797
1798         case WINED3DSP_WRITEMASK_1:
1799             shader_addline(arg->buffer, "sin(%s));\n", src0_param.param_str);
1800             break;
1801
1802         case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
1803             shader_addline(arg->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
1804             break;
1805
1806         default:
1807             ERR("Write mask should be .x, .y or .xy\n");
1808             break;
1809     }
1810 }
1811
1812 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1813  * Start a for() loop where src1.y is the initial value of aL,
1814  *  increment aL by src1.z for a total of src1.x iterations.
1815  *  Need to use a temporary variable for this operation.
1816  */
1817 /* FIXME: I don't think nested loops will work correctly this way. */
1818 void shader_glsl_loop(SHADER_OPCODE_ARG* arg) {
1819     glsl_src_param_t src1_param;
1820     IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1821
1822     shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
1823
1824     shader_addline(arg->buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
1825                    shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno,
1826                    src1_param.reg_name, shader->baseShader.cur_loop_depth, src1_param.reg_name,
1827                    shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno, src1_param.reg_name);
1828
1829     shader->baseShader.cur_loop_depth++;
1830     shader->baseShader.cur_loop_regno++;
1831 }
1832
1833 void shader_glsl_end(SHADER_OPCODE_ARG* arg) {
1834     IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1835
1836     shader_addline(arg->buffer, "}\n");
1837
1838     if(arg->opcode->opcode == WINED3DSIO_ENDLOOP) {
1839         shader->baseShader.cur_loop_depth--;
1840         shader->baseShader.cur_loop_regno--;
1841     }
1842     if(arg->opcode->opcode == WINED3DSIO_ENDREP) {
1843         shader->baseShader.cur_loop_depth--;
1844     }
1845 }
1846
1847 void shader_glsl_rep(SHADER_OPCODE_ARG* arg) {
1848     IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1849     glsl_src_param_t src0_param;
1850
1851     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1852     shader_addline(arg->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
1853                    shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_depth,
1854                    src0_param.param_str, shader->baseShader.cur_loop_depth);
1855     shader->baseShader.cur_loop_depth++;
1856 }
1857
1858 void shader_glsl_if(SHADER_OPCODE_ARG* arg) {
1859     glsl_src_param_t src0_param;
1860
1861     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1862     shader_addline(arg->buffer, "if (%s) {\n", src0_param.param_str);
1863 }
1864
1865 void shader_glsl_ifc(SHADER_OPCODE_ARG* arg) {
1866     glsl_src_param_t src0_param;
1867     glsl_src_param_t src1_param;
1868
1869     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1870     shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1871
1872     shader_addline(arg->buffer, "if (%s %s %s) {\n",
1873             src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
1874 }
1875
1876 void shader_glsl_else(SHADER_OPCODE_ARG* arg) {
1877     shader_addline(arg->buffer, "} else {\n");
1878 }
1879
1880 void shader_glsl_break(SHADER_OPCODE_ARG* arg) {
1881     shader_addline(arg->buffer, "break;\n");
1882 }
1883
1884 /* FIXME: According to MSDN the compare is done per component. */
1885 void shader_glsl_breakc(SHADER_OPCODE_ARG* arg) {
1886     glsl_src_param_t src0_param;
1887     glsl_src_param_t src1_param;
1888
1889     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1890     shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1891
1892     shader_addline(arg->buffer, "if (%s %s %s) break;\n",
1893             src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
1894 }
1895
1896 void shader_glsl_label(SHADER_OPCODE_ARG* arg) {
1897
1898     DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1899     shader_addline(arg->buffer, "}\n");
1900     shader_addline(arg->buffer, "void subroutine%lu () {\n",  snum);
1901 }
1902
1903 void shader_glsl_call(SHADER_OPCODE_ARG* arg) {
1904     DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1905     shader_addline(arg->buffer, "subroutine%lu();\n", snum);
1906 }
1907
1908 void shader_glsl_callnz(SHADER_OPCODE_ARG* arg) {
1909     glsl_src_param_t src1_param;
1910
1911     DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1912     shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1913     shader_addline(arg->buffer, "if (%s) subroutine%lu();\n", src1_param.param_str, snum);
1914 }
1915
1916 /*********************************************
1917  * Pixel Shader Specific Code begins here
1918  ********************************************/
1919 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
1920     IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1921     IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1922     DWORD hex_version = This->baseShader.hex_version;
1923     char dst_swizzle[6];
1924     glsl_sample_function_t sample_function;
1925     DWORD sampler_type;
1926     DWORD sampler_idx;
1927     BOOL projected;
1928     DWORD mask = 0;
1929
1930     /* All versions have a destination register */
1931     shader_glsl_append_dst(arg->buffer, arg);
1932
1933     /* 1.0-1.4: Use destination register as sampler source.
1934      * 2.0+: Use provided sampler source. */
1935     if (hex_version < WINED3DPS_VERSION(1,4)) {
1936         DWORD flags;
1937
1938         sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1939         flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1940
1941         if (flags & WINED3DTTFF_PROJECTED) {
1942             projected = TRUE;
1943             switch (flags & ~WINED3DTTFF_PROJECTED) {
1944                 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
1945                 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
1946                 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
1947                 case WINED3DTTFF_COUNT4:
1948                 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
1949             }
1950         } else {
1951             projected = FALSE;
1952         }
1953     } else if (hex_version < WINED3DPS_VERSION(2,0)) {
1954         DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
1955         sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1956
1957         if (src_mod == WINED3DSPSM_DZ) {
1958             projected = TRUE;
1959             mask = WINED3DSP_WRITEMASK_2;
1960         } else if (src_mod == WINED3DSPSM_DW) {
1961             projected = TRUE;
1962             mask = WINED3DSP_WRITEMASK_3;
1963         } else {
1964             projected = FALSE;
1965         }
1966     } else {
1967         sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
1968         if(arg->opcode_token & WINED3DSI_TEXLD_PROJECT) {
1969                 /* ps 2.0 texldp instruction always divides by the fourth component. */
1970                 projected = TRUE;
1971                 mask = WINED3DSP_WRITEMASK_3;
1972         } else {
1973             projected = FALSE;
1974         }
1975     }
1976
1977     sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
1978     shader_glsl_get_sample_function(sampler_type, projected, &sample_function);
1979     mask |= sample_function.coord_mask;
1980
1981     if (hex_version < WINED3DPS_VERSION(2,0)) {
1982         shader_glsl_get_write_mask(arg->dst, dst_swizzle);
1983     } else {
1984         shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
1985     }
1986
1987     /* 1.0-1.3: Use destination register as coordinate source.
1988        1.4+: Use provided coordinate source register. */
1989     if (hex_version < WINED3DPS_VERSION(1,4)) {
1990         char coord_mask[6];
1991         shader_glsl_get_write_mask(mask, coord_mask);
1992         shader_addline(arg->buffer, "%s(Psampler%u, T%u%s)%s);\n",
1993                 sample_function.name, sampler_idx, sampler_idx, coord_mask, dst_swizzle);
1994     } else {
1995         glsl_src_param_t coord_param;
1996         shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], mask, &coord_param);
1997         if(arg->opcode_token & WINED3DSI_TEXLD_BIAS) {
1998             glsl_src_param_t bias;
1999             shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &bias);
2000
2001             shader_addline(arg->buffer, "%s(Psampler%u, %s, %s)%s);\n",
2002                     sample_function.name, sampler_idx, coord_param.param_str,
2003                     bias.param_str, dst_swizzle);
2004         } else {
2005             shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n",
2006                     sample_function.name, sampler_idx, coord_param.param_str, dst_swizzle);
2007         }
2008     }
2009 }
2010
2011 void shader_glsl_texldl(SHADER_OPCODE_ARG* arg) {
2012     IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*)arg->shader;
2013     glsl_sample_function_t sample_function;
2014     glsl_src_param_t coord_param, lod_param;
2015     char dst_swizzle[6];
2016     DWORD sampler_type;
2017     DWORD sampler_idx;
2018
2019     shader_glsl_append_dst(arg->buffer, arg);
2020     shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
2021
2022     sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
2023     sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2024     shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
2025     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &coord_param);
2026
2027     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &lod_param);
2028
2029     if (shader_is_pshader_version(This->baseShader.hex_version)) {
2030         /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
2031          * However, they seem to work just fine in fragment shaders as well. */
2032         WARN("Using %sLod in fragment shader.\n", sample_function.name);
2033         shader_addline(arg->buffer, "%sLod(Psampler%u, %s, %s)%s);\n",
2034                 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2035     } else {
2036         shader_addline(arg->buffer, "%sLod(Vsampler%u, %s, %s)%s);\n",
2037                 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2038     }
2039 }
2040
2041 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
2042
2043     /* FIXME: Make this work for more than just 2D textures */
2044     
2045     IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2046     SHADER_BUFFER* buffer = arg->buffer;
2047     DWORD hex_version = This->baseShader.hex_version;
2048     DWORD write_mask;
2049     char dst_mask[6];
2050
2051     write_mask = shader_glsl_append_dst(arg->buffer, arg);
2052     shader_glsl_get_write_mask(write_mask, dst_mask);
2053
2054     if (hex_version != WINED3DPS_VERSION(1,4)) {
2055         DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2056         shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n", reg, dst_mask);
2057     } else {
2058         DWORD reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
2059         DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2060         char dst_swizzle[6];
2061
2062         shader_glsl_get_swizzle(arg->src[0], FALSE, write_mask, dst_swizzle);
2063
2064         if (src_mod == WINED3DSPSM_DZ) {
2065             glsl_src_param_t div_param;
2066             size_t mask_size = shader_glsl_get_write_mask_size(write_mask);
2067             shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &div_param);
2068
2069             if (mask_size > 1) {
2070                 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2071             } else {
2072                 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2073             }
2074         } else if (src_mod == WINED3DSPSM_DW) {
2075             glsl_src_param_t div_param;
2076             size_t mask_size = shader_glsl_get_write_mask_size(write_mask);
2077             shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &div_param);
2078
2079             if (mask_size > 1) {
2080                 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2081             } else {
2082                 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2083             }
2084         } else {
2085             shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
2086         }
2087     }
2088 }
2089
2090 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
2091  * Take a 3-component dot product of the TexCoord[dstreg] and src,
2092  * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2093 void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG* arg) {
2094     glsl_src_param_t src0_param;
2095     char dst_mask[6];
2096     glsl_sample_function_t sample_function;
2097     DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2098     DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2099     DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2100
2101     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2102
2103     shader_glsl_append_dst(arg->buffer, arg);
2104     shader_glsl_get_write_mask(arg->dst, dst_mask);
2105
2106     /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
2107      * scalar, and projected sampling would require 4
2108      */
2109     shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
2110
2111     switch(count_bits(sample_function.coord_mask)) {
2112         case 1:
2113             shader_addline(arg->buffer, "%s(Psampler%u, dot(gl_TexCoord[%u].xyz, %s))%s);\n",
2114                            sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2115             break;
2116
2117         case 2:
2118             shader_addline(arg->buffer, "%s(Psampler%u, vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0))%s);\n",
2119                           sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2120             break;
2121
2122         case 3:
2123             shader_addline(arg->buffer, "%s(Psampler%u, vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0))%s);\n",
2124                            sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2125             break;
2126         default:
2127             FIXME("Unexpected mask bitcount %d\n", count_bits(sample_function.coord_mask));
2128     }
2129 }
2130
2131 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2132  * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2133 void pshader_glsl_texdp3(SHADER_OPCODE_ARG* arg) {
2134     glsl_src_param_t src0_param;
2135     DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2136     DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2137     DWORD dst_mask;
2138     size_t mask_size;
2139
2140     dst_mask = shader_glsl_append_dst(arg->buffer, arg);
2141     mask_size = shader_glsl_get_write_mask_size(dst_mask);
2142     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2143
2144     if (mask_size > 1) {
2145         shader_addline(arg->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
2146     } else {
2147         shader_addline(arg->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
2148     }
2149 }
2150
2151 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2152  * Calculate the depth as dst.x / dst.y   */
2153 void pshader_glsl_texdepth(SHADER_OPCODE_ARG* arg) {
2154     glsl_dst_param_t dst_param;
2155
2156     shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2157
2158     /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2159      * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2160      * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2161      * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2162      * >= 1.0 or < 0.0
2163      */
2164     shader_addline(arg->buffer, "gl_FragDepth = (%s.y == 0.0) ? 1.0 : clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n", dst_param.reg_name, dst_param.reg_name, dst_param.reg_name);
2165 }
2166
2167 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2168  * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2169  * Calculate tmp0.y = TexCoord[dstreg] . src.xyz;  (tmp0.x has already been calculated)
2170  * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2171  */
2172 void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG* arg) {
2173     DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2174     DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2175     glsl_src_param_t src0_param;
2176
2177     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2178
2179     shader_addline(arg->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
2180     shader_addline(arg->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2181 }
2182
2183 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2184  * Calculate the 1st of a 2-row matrix multiplication. */
2185 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
2186     DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2187     DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2188     SHADER_BUFFER* buffer = arg->buffer;
2189     glsl_src_param_t src0_param;
2190
2191     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2192     shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2193 }
2194
2195 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2196  * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2197 void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG* arg) {
2198
2199     IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2200     DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2201     DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2202     SHADER_BUFFER* buffer = arg->buffer;
2203     SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2204     glsl_src_param_t src0_param;
2205
2206     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2207     shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
2208     current_state->texcoord_w[current_state->current_row++] = reg;
2209 }
2210
2211 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
2212     DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2213     DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2214     SHADER_BUFFER* buffer = arg->buffer;
2215     glsl_src_param_t src0_param;
2216     char dst_mask[6];
2217
2218     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2219     shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2220
2221     shader_glsl_append_dst(buffer, arg);
2222     shader_glsl_get_write_mask(arg->dst, dst_mask);
2223
2224     /* Sample the texture using the calculated coordinates */
2225     shader_addline(buffer, "texture2D(Psampler%u, tmp0.xy)%s);\n", reg, dst_mask);
2226 }
2227
2228 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2229  * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2230 void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG* arg) {
2231     DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2232     glsl_src_param_t src0_param;
2233     char dst_mask[6];
2234     DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2235     IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2236     SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2237     DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2238     glsl_sample_function_t sample_function;
2239
2240     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2241     shader_addline(arg->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2242
2243     shader_glsl_append_dst(arg->buffer, arg);
2244     shader_glsl_get_write_mask(arg->dst, dst_mask);
2245     shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
2246
2247     /* Sample the texture using the calculated coordinates */
2248     shader_addline(arg->buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2249
2250     current_state->current_row = 0;
2251 }
2252
2253 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2254  * Perform the 3rd row of a 3x3 matrix multiply */
2255 void pshader_glsl_texm3x3(SHADER_OPCODE_ARG* arg) {
2256     DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2257     glsl_src_param_t src0_param;
2258     char dst_mask[6];
2259     DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2260     IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2261     SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2262
2263     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2264
2265     shader_glsl_append_dst(arg->buffer, arg);
2266     shader_glsl_get_write_mask(arg->dst, dst_mask);
2267     shader_addline(arg->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
2268
2269     current_state->current_row = 0;
2270 }
2271
2272 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL 
2273  * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2274 void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG* arg) {
2275
2276     IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2277     DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2278     glsl_src_param_t src0_param;
2279     glsl_src_param_t src1_param;
2280     char dst_mask[6];
2281     SHADER_BUFFER* buffer = arg->buffer;
2282     SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2283     DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2284     DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2285     glsl_sample_function_t sample_function;
2286
2287     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2288     shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
2289
2290     /* Perform the last matrix multiply operation */
2291     shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2292     /* Reflection calculation */
2293     shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
2294
2295     shader_glsl_append_dst(buffer, arg);
2296     shader_glsl_get_write_mask(arg->dst, dst_mask);
2297     shader_glsl_get_sample_function(stype, FALSE, &sample_function);
2298
2299     /* Sample the texture */
2300     shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2301
2302     current_state->current_row = 0;
2303 }
2304
2305 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL 
2306  * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2307 void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
2308
2309     IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2310     DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2311     SHADER_BUFFER* buffer = arg->buffer;
2312     SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2313     glsl_src_param_t src0_param;
2314     char dst_mask[6];
2315     DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2316     DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2317     glsl_sample_function_t sample_function;
2318
2319     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2320
2321     /* Perform the last matrix multiply operation */
2322     shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
2323
2324     /* Construct the eye-ray vector from w coordinates */
2325     shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
2326             current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
2327     shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
2328
2329     shader_glsl_append_dst(buffer, arg);
2330     shader_glsl_get_write_mask(arg->dst, dst_mask);
2331     shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
2332
2333     /* Sample the texture using the calculated coordinates */
2334     shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2335
2336     current_state->current_row = 0;
2337 }
2338
2339 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
2340  * Apply a fake bump map transform.
2341  * texbem is pshader <= 1.3 only, this saves a few version checks
2342  */
2343 void pshader_glsl_texbem(SHADER_OPCODE_ARG* arg) {
2344     IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2345     IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2346     char dst_swizzle[6];
2347     glsl_sample_function_t sample_function;
2348     glsl_src_param_t coord_param;
2349     DWORD sampler_type;
2350     DWORD sampler_idx;
2351     DWORD mask;
2352     DWORD flags;
2353     char coord_mask[6];
2354
2355     sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2356     flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2357
2358     sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2359     shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
2360     mask = sample_function.coord_mask;
2361
2362     shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2363
2364     shader_glsl_get_write_mask(mask, coord_mask);
2365
2366     /* with projective textures, texbem only divides the static texture coord, not the displacement,
2367          * so we can't let the GL handle this.
2368          */
2369     if (flags & WINED3DTTFF_PROJECTED) {
2370         DWORD div_mask=0;
2371         char coord_div_mask[3];
2372         switch (flags & ~WINED3DTTFF_PROJECTED) {
2373             case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2374             case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
2375             case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
2376             case WINED3DTTFF_COUNT4:
2377             case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
2378         }
2379         shader_glsl_get_write_mask(div_mask, coord_div_mask);
2380         shader_addline(arg->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
2381     }
2382
2383     shader_glsl_append_dst(arg->buffer, arg);
2384     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &coord_param);
2385     if(arg->opcode->opcode == WINED3DSIO_TEXBEML) {
2386         glsl_src_param_t luminance_param;
2387         shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &luminance_param);
2388         shader_addline(arg->buffer, "(%s(Psampler%u, T%u%s + vec4(bumpenvmat * %s, 0.0, 0.0)%s )*(%s * luminancescale + luminanceoffset))%s);\n",
2389                        sample_function.name, sampler_idx, sampler_idx, coord_mask, coord_param.param_str, coord_mask,
2390                        luminance_param.param_str, dst_swizzle);
2391     } else {
2392         shader_addline(arg->buffer, "%s(Psampler%u, T%u%s + vec4(bumpenvmat * %s, 0.0, 0.0)%s )%s);\n",
2393                        sample_function.name, sampler_idx, sampler_idx, coord_mask, coord_param.param_str, coord_mask, dst_swizzle);
2394     }
2395 }
2396
2397 void pshader_glsl_bem(SHADER_OPCODE_ARG* arg) {
2398     glsl_src_param_t src0_param, src1_param;
2399
2400     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src0_param);
2401     shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src1_param);
2402
2403     shader_glsl_append_dst(arg->buffer, arg);
2404     shader_addline(arg->buffer, "%s + bumpenvmat * %s);\n",
2405                    src0_param.param_str, src1_param.param_str);
2406 }
2407
2408 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
2409  * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
2410 void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG* arg) {
2411
2412     glsl_src_param_t src0_param;
2413     DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2414     char dst_mask[6];
2415
2416     shader_glsl_append_dst(arg->buffer, arg);
2417     shader_glsl_get_write_mask(arg->dst, dst_mask);
2418     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2419
2420     shader_addline(arg->buffer, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2421 }
2422
2423 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
2424  * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
2425 void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG* arg) {
2426     glsl_src_param_t src0_param;
2427     DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2428     char dst_mask[6];
2429
2430     shader_glsl_append_dst(arg->buffer, arg);
2431     shader_glsl_get_write_mask(arg->dst, dst_mask);
2432     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2433
2434     shader_addline(arg->buffer, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2435 }
2436
2437 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
2438  * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
2439 void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG* arg) {
2440     glsl_src_param_t src0_param;
2441     char dst_mask[6];
2442     DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2443     DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2444     glsl_sample_function_t sample_function;
2445
2446     shader_glsl_append_dst(arg->buffer, arg);
2447     shader_glsl_get_write_mask(arg->dst, dst_mask);
2448     shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
2449     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &src0_param);
2450
2451     shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n", sample_function.name, sampler_idx, src0_param.param_str, dst_mask);
2452 }
2453
2454 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
2455  * If any of the first 3 components are < 0, discard this pixel */
2456 void pshader_glsl_texkill(SHADER_OPCODE_ARG* arg) {
2457     IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2458     DWORD hex_version = This->baseShader.hex_version;
2459     glsl_dst_param_t dst_param;
2460
2461     /* The argument is a destination parameter, and no writemasks are allowed */
2462     shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2463     if((hex_version >= WINED3DPS_VERSION(2,0))) {
2464         /* 2.0 shaders compare all 4 components in texkill */
2465         shader_addline(arg->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
2466     } else {
2467         /* 1.X shaders only compare the first 3 components, propably due to the nature of the texkill
2468          * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
2469          * 4 components are defined, only the first 3 are used
2470          */
2471         shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
2472     }
2473 }
2474
2475 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
2476  * dst = dot2(src0, src1) + src2 */
2477 void pshader_glsl_dp2add(SHADER_OPCODE_ARG* arg) {
2478     glsl_src_param_t src0_param;
2479     glsl_src_param_t src1_param;
2480     glsl_src_param_t src2_param;
2481     DWORD write_mask;
2482     size_t mask_size;
2483
2484     write_mask = shader_glsl_append_dst(arg->buffer, arg);
2485     mask_size = shader_glsl_get_write_mask_size(write_mask);
2486
2487     shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
2488     shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
2489     shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], WINED3DSP_WRITEMASK_0, &src2_param);
2490
2491     shader_addline(arg->buffer, "dot(%s, %s) + %s);\n", src0_param.param_str, src1_param.param_str, src2_param.param_str);
2492 }
2493
2494 void pshader_glsl_input_pack(
2495    SHADER_BUFFER* buffer,
2496    semantic* semantics_in) {
2497
2498    unsigned int i;
2499
2500    for (i = 0; i < MAX_REG_INPUT; i++) {
2501
2502        DWORD usage_token = semantics_in[i].usage;
2503        DWORD register_token = semantics_in[i].reg;
2504        DWORD usage, usage_idx;
2505        char reg_mask[6];
2506
2507        /* Uninitialized */
2508        if (!usage_token) continue;
2509        usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2510        usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2511        shader_glsl_get_write_mask(register_token, reg_mask);
2512
2513        switch(usage) {
2514
2515            case WINED3DDECLUSAGE_COLOR:
2516                if (usage_idx == 0)
2517                    shader_addline(buffer, "IN%u%s = vec4(gl_Color)%s;\n",
2518                        i, reg_mask, reg_mask);
2519                else if (usage_idx == 1)
2520                    shader_addline(buffer, "IN%u%s = vec4(gl_SecondaryColor)%s;\n",
2521                        i, reg_mask, reg_mask);
2522                else
2523                    shader_addline(buffer, "IN%u%s = vec4(unsupported_color_input)%s;\n",
2524                        i, reg_mask, reg_mask);
2525                break;
2526
2527            case WINED3DDECLUSAGE_TEXCOORD:
2528                shader_addline(buffer, "IN%u%s = vec4(gl_TexCoord[%u])%s;\n",
2529                    i, reg_mask, usage_idx, reg_mask );
2530                break;
2531
2532            case WINED3DDECLUSAGE_FOG:
2533                shader_addline(buffer, "IN%u%s = vec4(gl_FogFragCoord)%s;\n",
2534                    i, reg_mask, reg_mask);
2535                break;
2536
2537            default:
2538                shader_addline(buffer, "IN%u%s = vec4(unsupported_input)%s;\n",
2539                    i, reg_mask, reg_mask);
2540         }
2541     }
2542 }
2543
2544 /*********************************************
2545  * Vertex Shader Specific Code begins here
2546  ********************************************/
2547
2548 void vshader_glsl_output_unpack(
2549    SHADER_BUFFER* buffer,
2550    semantic* semantics_out) {
2551
2552    unsigned int i;
2553
2554    for (i = 0; i < MAX_REG_OUTPUT; i++) {
2555
2556        DWORD usage_token = semantics_out[i].usage;
2557        DWORD register_token = semantics_out[i].reg;
2558        DWORD usage, usage_idx;
2559        char reg_mask[6];
2560
2561        /* Uninitialized */
2562        if (!usage_token) continue;
2563
2564        usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2565        usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2566        shader_glsl_get_write_mask(register_token, reg_mask);
2567
2568        switch(usage) {
2569
2570            case WINED3DDECLUSAGE_COLOR:
2571                if (usage_idx == 0)
2572                    shader_addline(buffer, "gl_FrontColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2573                else if (usage_idx == 1)
2574                    shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2575                else
2576                    shader_addline(buffer, "unsupported_color_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2577                break;
2578
2579            case WINED3DDECLUSAGE_POSITION:
2580                shader_addline(buffer, "gl_Position%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2581                break;
2582
2583            case WINED3DDECLUSAGE_TEXCOORD:
2584                shader_addline(buffer, "gl_TexCoord[%u]%s = OUT%u%s;\n",
2585                    usage_idx, reg_mask, i, reg_mask);
2586                break;
2587
2588            case WINED3DDECLUSAGE_PSIZE:
2589                shader_addline(buffer, "gl_PointSize = OUT%u.x;\n", i);
2590                break;
2591
2592            case WINED3DDECLUSAGE_FOG:
2593                shader_addline(buffer, "gl_FogFragCoord = OUT%u%s;\n", i, reg_mask);
2594                break;
2595
2596            default:
2597                shader_addline(buffer, "unsupported_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2598        }
2599     }
2600 }
2601
2602 static void add_glsl_program_entry(IWineD3DDeviceImpl *device, struct glsl_shader_prog_link *entry) {
2603     glsl_program_key_t *key;
2604
2605     key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2606     key->vshader = entry->vshader;
2607     key->pshader = entry->pshader;
2608
2609     hash_table_put(device->glsl_program_lookup, key, entry);
2610 }
2611
2612 static struct glsl_shader_prog_link *get_glsl_program_entry(IWineD3DDeviceImpl *device,
2613         GLhandleARB vshader, GLhandleARB pshader) {
2614     glsl_program_key_t key;
2615
2616     key.vshader = vshader;
2617     key.pshader = pshader;
2618
2619     return (struct glsl_shader_prog_link *)hash_table_get(device->glsl_program_lookup, &key);
2620 }
2621
2622 void delete_glsl_program_entry(IWineD3DDevice *iface, struct glsl_shader_prog_link *entry) {
2623     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2624     WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2625     glsl_program_key_t *key;
2626
2627     key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2628     key->vshader = entry->vshader;
2629     key->pshader = entry->pshader;
2630     hash_table_remove(This->glsl_program_lookup, key);
2631
2632     GL_EXTCALL(glDeleteObjectARB(entry->programId));
2633     if (entry->vshader) list_remove(&entry->vshader_entry);
2634     if (entry->pshader) list_remove(&entry->pshader_entry);
2635     HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
2636     HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
2637     HeapFree(GetProcessHeap(), 0, entry);
2638 }
2639
2640 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
2641  * It sets the programId on the current StateBlock (because it should be called
2642  * inside of the DrawPrimitive() part of the render loop).
2643  *
2644  * If a program for the given combination does not exist, create one, and store
2645  * the program in the hash table.  If it creates a program, it will link the
2646  * given objects, too.
2647  */
2648 static void set_glsl_shader_program(IWineD3DDevice *iface, BOOL use_ps, BOOL use_vs) {
2649     IWineD3DDeviceImpl *This               = (IWineD3DDeviceImpl *)iface;
2650     WineD3D_GL_Info *gl_info               = &This->adapter->gl_info;
2651     IWineD3DPixelShader  *pshader          = This->stateBlock->pixelShader;
2652     IWineD3DVertexShader *vshader          = This->stateBlock->vertexShader;
2653     struct glsl_shader_prog_link *entry    = NULL;
2654     GLhandleARB programId                  = 0;
2655     int i;
2656     char glsl_name[8];
2657
2658     GLhandleARB vshader_id = use_vs ? ((IWineD3DBaseShaderImpl*)vshader)->baseShader.prgId : 0;
2659     GLhandleARB pshader_id = use_ps ? ((IWineD3DBaseShaderImpl*)pshader)->baseShader.prgId : 0;
2660     entry = get_glsl_program_entry(This, vshader_id, pshader_id);
2661     if (entry) {
2662         This->stateBlock->glsl_program = entry;
2663         return;
2664     }
2665
2666     /* If we get to this point, then no matching program exists, so we create one */
2667     programId = GL_EXTCALL(glCreateProgramObjectARB());
2668     TRACE("Created new GLSL shader program %u\n", programId);
2669
2670     /* Create the entry */
2671     entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
2672     entry->programId = programId;
2673     entry->vshader = vshader_id;
2674     entry->pshader = pshader_id;
2675     /* Add the hash table entry */
2676     add_glsl_program_entry(This, entry);
2677
2678     /* Set the current program */
2679     This->stateBlock->glsl_program = entry;
2680
2681     /* Attach GLSL vshader */
2682     if (vshader_id) {
2683         int max_attribs = 16;   /* TODO: Will this always be the case? It is at the moment... */
2684         char tmp_name[10];
2685
2686         TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
2687         GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
2688         checkGLcall("glAttachObjectARB");
2689
2690         /* Bind vertex attributes to a corresponding index number to match
2691          * the same index numbers as ARB_vertex_programs (makes loading
2692          * vertex attributes simpler).  With this method, we can use the
2693          * exact same code to load the attributes later for both ARB and
2694          * GLSL shaders.
2695          *
2696          * We have to do this here because we need to know the Program ID
2697          * in order to make the bindings work, and it has to be done prior
2698          * to linking the GLSL program. */
2699         for (i = 0; i < max_attribs; ++i) {
2700              snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
2701              GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
2702         }
2703         checkGLcall("glBindAttribLocationARB");
2704
2705         list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
2706     }
2707
2708     /* Attach GLSL pshader */
2709     if (pshader_id) {
2710         TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
2711         GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
2712         checkGLcall("glAttachObjectARB");
2713
2714         list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
2715     }
2716
2717     /* Link the program */
2718     TRACE("Linking GLSL shader program %u\n", programId);
2719     GL_EXTCALL(glLinkProgramARB(programId));
2720     print_glsl_info_log(&GLINFO_LOCATION, programId);
2721
2722     entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
2723     for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
2724         snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
2725         entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
2726     }
2727     entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
2728     for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
2729         snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
2730         entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
2731     }
2732 }
2733
2734 static GLhandleARB create_glsl_blt_shader(WineD3D_GL_Info *gl_info) {
2735     GLhandleARB program_id;
2736     GLhandleARB vshader_id, pshader_id;
2737     const char *blt_vshader[] = {
2738         "void main(void)\n"
2739         "{\n"
2740         "    gl_Position = gl_Vertex;\n"
2741         "    gl_FrontColor = vec4(1.0);\n"
2742         "    gl_TexCoord[0].x = (gl_Vertex.x * 0.5) + 0.5;\n"
2743         "    gl_TexCoord[0].y = (-gl_Vertex.y * 0.5) + 0.5;\n"
2744         "}\n"
2745     };
2746
2747     const char *blt_pshader[] = {
2748         "uniform sampler2D sampler;\n"
2749         "void main(void)\n"
2750         "{\n"
2751         "    gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
2752         "}\n"
2753     };
2754
2755     vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
2756     GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
2757     GL_EXTCALL(glCompileShaderARB(vshader_id));
2758
2759     pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
2760     GL_EXTCALL(glShaderSourceARB(pshader_id, 1, blt_pshader, NULL));
2761     GL_EXTCALL(glCompileShaderARB(pshader_id));
2762
2763     program_id = GL_EXTCALL(glCreateProgramObjectARB());
2764     GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
2765     GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
2766     GL_EXTCALL(glLinkProgramARB(program_id));
2767
2768     print_glsl_info_log(&GLINFO_LOCATION, program_id);
2769
2770     return program_id;
2771 }
2772
2773 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
2774     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2775     WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2776     GLhandleARB program_id = 0;
2777
2778     if (useVS || usePS) set_glsl_shader_program(iface, usePS, useVS);
2779     else This->stateBlock->glsl_program = NULL;
2780
2781     program_id = This->stateBlock->glsl_program ? This->stateBlock->glsl_program->programId : 0;
2782     if (program_id) TRACE("Using GLSL program %u\n", program_id);
2783     GL_EXTCALL(glUseProgramObjectARB(program_id));
2784     checkGLcall("glUseProgramObjectARB");
2785 }
2786
2787 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface) {
2788     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2789     WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2790     static GLhandleARB program_id = 0;
2791     static GLhandleARB loc = -1;
2792
2793     if (!program_id) {
2794         program_id = create_glsl_blt_shader(gl_info);
2795         loc = GL_EXTCALL(glGetUniformLocationARB(program_id, "sampler"));
2796     }
2797
2798     GL_EXTCALL(glUseProgramObjectARB(program_id));
2799     GL_EXTCALL(glUniform1iARB(loc, 0));
2800 }
2801
2802 static void shader_glsl_cleanup(IWineD3DDevice *iface) {
2803     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2804     WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2805     GL_EXTCALL(glUseProgramObjectARB(0));
2806 }
2807
2808 const shader_backend_t glsl_shader_backend = {
2809     &shader_glsl_select,
2810     &shader_glsl_select_depth_blt,
2811     &shader_glsl_load_constants,
2812     &shader_glsl_cleanup,
2813     &shader_glsl_color_correction
2814 };