OK, I give up. Pigglesworth, HAALP. What do I do wrong??!
[reactos.git] / reactos / dll / directx / wine / 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 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
7 * Copyright 2009 Henri Verbeet for CodeWeavers
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24 /*
25 * D3D shader asm has swizzles on source parameters, and write masks for
26 * destination parameters. GLSL uses swizzles for both. The result of this is
27 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
28 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
29 * mask for the destination parameter into account.
30 */
31
32 #include "config.h"
33 #include <limits.h>
34 #include <stdio.h>
35 #include "wined3d_private.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
39 WINE_DECLARE_DEBUG_CHANNEL(d3d_caps);
40 WINE_DECLARE_DEBUG_CHANNEL(d3d);
41
42 #define GLINFO_LOCATION (*gl_info)
43
44 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x1
45 #define WINED3D_GLSL_SAMPLE_RECT 0x2
46 #define WINED3D_GLSL_SAMPLE_LOD 0x4
47 #define WINED3D_GLSL_SAMPLE_GRAD 0x8
48
49 typedef struct {
50 char reg_name[150];
51 char mask_str[6];
52 } glsl_dst_param_t;
53
54 typedef struct {
55 char reg_name[150];
56 char param_str[200];
57 } glsl_src_param_t;
58
59 typedef struct {
60 const char *name;
61 DWORD coord_mask;
62 } glsl_sample_function_t;
63
64 enum heap_node_op
65 {
66 HEAP_NODE_TRAVERSE_LEFT,
67 HEAP_NODE_TRAVERSE_RIGHT,
68 HEAP_NODE_POP,
69 };
70
71 struct constant_entry
72 {
73 unsigned int idx;
74 unsigned int version;
75 };
76
77 struct constant_heap
78 {
79 struct constant_entry *entries;
80 unsigned int *positions;
81 unsigned int size;
82 };
83
84 /* GLSL shader private data */
85 struct shader_glsl_priv {
86 struct hash_table_t *glsl_program_lookup;
87 struct glsl_shader_prog_link *glsl_program;
88 struct constant_heap vconst_heap;
89 struct constant_heap pconst_heap;
90 unsigned char *stack;
91 GLhandleARB depth_blt_program[tex_type_count];
92 UINT next_constant_version;
93 };
94
95 /* Struct to maintain data about a linked GLSL program */
96 struct glsl_shader_prog_link {
97 struct list vshader_entry;
98 struct list pshader_entry;
99 GLhandleARB programId;
100 GLint *vuniformF_locations;
101 GLint *puniformF_locations;
102 GLint vuniformI_locations[MAX_CONST_I];
103 GLint puniformI_locations[MAX_CONST_I];
104 GLint posFixup_location;
105 GLint np2Fixup_location[MAX_FRAGMENT_SAMPLERS];
106 GLint bumpenvmat_location[MAX_TEXTURES];
107 GLint luminancescale_location[MAX_TEXTURES];
108 GLint luminanceoffset_location[MAX_TEXTURES];
109 GLint ycorrection_location;
110 GLenum vertex_color_clamp;
111 IWineD3DVertexShader *vshader;
112 IWineD3DPixelShader *pshader;
113 struct vs_compile_args vs_args;
114 struct ps_compile_args ps_args;
115 UINT constant_version;
116 };
117
118 typedef struct {
119 IWineD3DVertexShader *vshader;
120 IWineD3DPixelShader *pshader;
121 struct ps_compile_args ps_args;
122 struct vs_compile_args vs_args;
123 } glsl_program_key_t;
124
125
126 /** Prints the GLSL info log which will contain error messages if they exist */
127 static void print_glsl_info_log(const WineD3D_GL_Info *gl_info, GLhandleARB obj)
128 {
129 int infologLength = 0;
130 char *infoLog;
131 unsigned int i;
132 BOOL is_spam;
133
134 static const char * const spam[] =
135 {
136 "Vertex shader was successfully compiled to run on hardware.\n", /* fglrx */
137 "Fragment shader was successfully compiled to run on hardware.\n", /* fglrx */
138 "Fragment shader(s) linked, vertex shader(s) linked. \n ", /* fglrx, with \n */
139 "Fragment shader(s) linked, vertex shader(s) linked.", /* fglrx, no \n */
140 "Vertex shader(s) linked, no fragment shader(s) defined. \n ", /* fglrx, with \n */
141 "Vertex shader(s) linked, no fragment shader(s) defined.", /* fglrx, no \n */
142 "Fragment shader was successfully compiled to run on hardware.\n"
143 "WARNING: 0:2: extension 'GL_ARB_draw_buffers' is not supported",
144 "Fragment shader(s) linked, no vertex shader(s) defined.", /* fglrx, no \n */
145 "Fragment shader(s) linked, no vertex shader(s) defined. \n ", /* fglrx, with \n */
146 "WARNING: 0:2: extension 'GL_ARB_draw_buffers' is not supported\n" /* MacOS ati */
147 };
148
149 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader)) return;
150
151 GL_EXTCALL(glGetObjectParameterivARB(obj,
152 GL_OBJECT_INFO_LOG_LENGTH_ARB,
153 &infologLength));
154
155 /* A size of 1 is just a null-terminated string, so the log should be bigger than
156 * that if there are errors. */
157 if (infologLength > 1)
158 {
159 /* Fglrx doesn't terminate the string properly, but it tells us the proper length.
160 * So use HEAP_ZERO_MEMORY to avoid uninitialized bytes
161 */
162 infoLog = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, infologLength);
163 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
164 is_spam = FALSE;
165
166 for(i = 0; i < sizeof(spam) / sizeof(spam[0]); i++) {
167 if(strcmp(infoLog, spam[i]) == 0) {
168 is_spam = TRUE;
169 break;
170 }
171 }
172 if(is_spam) {
173 TRACE("Spam received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
174 } else {
175 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
176 }
177 HeapFree(GetProcessHeap(), 0, infoLog);
178 }
179 }
180
181 /**
182 * Loads (pixel shader) samplers
183 */
184 static void shader_glsl_load_psamplers(const WineD3D_GL_Info *gl_info, DWORD *tex_unit_map, GLhandleARB programId)
185 {
186 GLint name_loc;
187 int i;
188 char sampler_name[20];
189
190 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
191 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
192 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
193 if (name_loc != -1) {
194 DWORD mapped_unit = tex_unit_map[i];
195 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < GL_LIMITS(fragment_samplers))
196 {
197 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
198 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
199 checkGLcall("glUniform1iARB");
200 } else {
201 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
202 }
203 }
204 }
205 }
206
207 static void shader_glsl_load_vsamplers(const WineD3D_GL_Info *gl_info, DWORD *tex_unit_map, GLhandleARB programId)
208 {
209 GLint name_loc;
210 char sampler_name[20];
211 int i;
212
213 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
214 snprintf(sampler_name, sizeof(sampler_name), "Vsampler%d", i);
215 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
216 if (name_loc != -1) {
217 DWORD mapped_unit = tex_unit_map[MAX_FRAGMENT_SAMPLERS + i];
218 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < GL_LIMITS(combined_samplers))
219 {
220 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
221 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
222 checkGLcall("glUniform1iARB");
223 } else {
224 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
225 }
226 }
227 }
228 }
229
230 static inline void walk_constant_heap(const WineD3D_GL_Info *gl_info, const float *constants,
231 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
232 {
233 int stack_idx = 0;
234 unsigned int heap_idx = 1;
235 unsigned int idx;
236
237 if (heap->entries[heap_idx].version <= version) return;
238
239 idx = heap->entries[heap_idx].idx;
240 if (constant_locations[idx] != -1) GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
241 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
242
243 while (stack_idx >= 0)
244 {
245 /* Note that we fall through to the next case statement. */
246 switch(stack[stack_idx])
247 {
248 case HEAP_NODE_TRAVERSE_LEFT:
249 {
250 unsigned int left_idx = heap_idx << 1;
251 if (left_idx < heap->size && heap->entries[left_idx].version > version)
252 {
253 heap_idx = left_idx;
254 idx = heap->entries[heap_idx].idx;
255 if (constant_locations[idx] != -1)
256 GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
257
258 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
259 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
260 break;
261 }
262 }
263
264 case HEAP_NODE_TRAVERSE_RIGHT:
265 {
266 unsigned int right_idx = (heap_idx << 1) + 1;
267 if (right_idx < heap->size && heap->entries[right_idx].version > version)
268 {
269 heap_idx = right_idx;
270 idx = heap->entries[heap_idx].idx;
271 if (constant_locations[idx] != -1)
272 GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
273
274 stack[stack_idx++] = HEAP_NODE_POP;
275 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
276 break;
277 }
278 }
279
280 case HEAP_NODE_POP:
281 {
282 heap_idx >>= 1;
283 --stack_idx;
284 break;
285 }
286 }
287 }
288 checkGLcall("walk_constant_heap()");
289 }
290
291 static inline void apply_clamped_constant(const WineD3D_GL_Info *gl_info, GLint location, const GLfloat *data)
292 {
293 GLfloat clamped_constant[4];
294
295 if (location == -1) return;
296
297 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0 ? 1.0 : data[0];
298 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0 ? 1.0 : data[1];
299 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0 ? 1.0 : data[2];
300 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0 ? 1.0 : data[3];
301
302 GL_EXTCALL(glUniform4fvARB(location, 1, clamped_constant));
303 }
304
305 static inline void walk_constant_heap_clamped(const WineD3D_GL_Info *gl_info, const float *constants,
306 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
307 {
308 int stack_idx = 0;
309 unsigned int heap_idx = 1;
310 unsigned int idx;
311
312 if (heap->entries[heap_idx].version <= version) return;
313
314 idx = heap->entries[heap_idx].idx;
315 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
316 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
317
318 while (stack_idx >= 0)
319 {
320 /* Note that we fall through to the next case statement. */
321 switch(stack[stack_idx])
322 {
323 case HEAP_NODE_TRAVERSE_LEFT:
324 {
325 unsigned int left_idx = heap_idx << 1;
326 if (left_idx < heap->size && heap->entries[left_idx].version > version)
327 {
328 heap_idx = left_idx;
329 idx = heap->entries[heap_idx].idx;
330 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
331
332 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
333 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
334 break;
335 }
336 }
337
338 case HEAP_NODE_TRAVERSE_RIGHT:
339 {
340 unsigned int right_idx = (heap_idx << 1) + 1;
341 if (right_idx < heap->size && heap->entries[right_idx].version > version)
342 {
343 heap_idx = right_idx;
344 idx = heap->entries[heap_idx].idx;
345 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
346
347 stack[stack_idx++] = HEAP_NODE_POP;
348 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
349 break;
350 }
351 }
352
353 case HEAP_NODE_POP:
354 {
355 heap_idx >>= 1;
356 --stack_idx;
357 break;
358 }
359 }
360 }
361 checkGLcall("walk_constant_heap_clamped()");
362 }
363
364 /* Loads floating point constants (aka uniforms) into the currently set GLSL program. */
365 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
366 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
367 unsigned char *stack, UINT version)
368 {
369 const local_constant *lconst;
370
371 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
372 if (This->baseShader.reg_maps.shader_version.major == 1
373 && shader_is_pshader_version(This->baseShader.reg_maps.shader_version.type))
374 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
375 else
376 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
377
378 if (!This->baseShader.load_local_constsF)
379 {
380 TRACE("No need to load local float constants for this shader\n");
381 return;
382 }
383
384 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
385 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry)
386 {
387 GLint location = constant_locations[lconst->idx];
388 /* We found this uniform name in the program - go ahead and send the data */
389 if (location != -1) GL_EXTCALL(glUniform4fvARB(location, 1, (const GLfloat *)lconst->value));
390 }
391 checkGLcall("glUniform4fvARB()");
392 }
393
394 /* Loads integer constants (aka uniforms) into the currently set GLSL program. */
395 static void shader_glsl_load_constantsI(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
396 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
397 {
398 unsigned int i;
399 struct list* ptr;
400
401 for (i = 0; constants_set; constants_set >>= 1, ++i)
402 {
403 if (!(constants_set & 1)) continue;
404
405 TRACE_(d3d_constants)("Loading constants %u: %i, %i, %i, %i\n",
406 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
407
408 /* We found this uniform name in the program - go ahead and send the data */
409 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
410 checkGLcall("glUniform4ivARB");
411 }
412
413 /* Load immediate constants */
414 ptr = list_head(&This->baseShader.constantsI);
415 while (ptr) {
416 const struct local_constant *lconst = LIST_ENTRY(ptr, const struct local_constant, entry);
417 unsigned int idx = lconst->idx;
418 const GLint *values = (const GLint *)lconst->value;
419
420 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
421 values[0], values[1], values[2], values[3]);
422
423 /* We found this uniform name in the program - go ahead and send the data */
424 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
425 checkGLcall("glUniform4ivARB");
426 ptr = list_next(&This->baseShader.constantsI, ptr);
427 }
428 }
429
430 /* Loads boolean constants (aka uniforms) into the currently set GLSL program. */
431 static void shader_glsl_load_constantsB(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
432 GLhandleARB programId, const BOOL *constants, WORD constants_set)
433 {
434 GLint tmp_loc;
435 unsigned int i;
436 char tmp_name[8];
437 char is_pshader = shader_is_pshader_version(This->baseShader.reg_maps.shader_version.type);
438 const char* prefix = is_pshader? "PB":"VB";
439 struct list* ptr;
440
441 /* TODO: Benchmark and see if it would be beneficial to store the
442 * locations of the constants to avoid looking up each time */
443 for (i = 0; constants_set; constants_set >>= 1, ++i)
444 {
445 if (!(constants_set & 1)) continue;
446
447 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i]);
448
449 /* TODO: Benchmark and see if it would be beneficial to store the
450 * locations of the constants to avoid looking up each time */
451 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
452 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
453 if (tmp_loc != -1)
454 {
455 /* We found this uniform name in the program - go ahead and send the data */
456 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
457 checkGLcall("glUniform1ivARB");
458 }
459 }
460
461 /* Load immediate constants */
462 ptr = list_head(&This->baseShader.constantsB);
463 while (ptr) {
464 const struct local_constant *lconst = LIST_ENTRY(ptr, const struct local_constant, entry);
465 unsigned int idx = lconst->idx;
466 const GLint *values = (const GLint *)lconst->value;
467
468 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
469
470 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
471 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
472 if (tmp_loc != -1) {
473 /* We found this uniform name in the program - go ahead and send the data */
474 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
475 checkGLcall("glUniform1ivARB");
476 }
477 ptr = list_next(&This->baseShader.constantsB, ptr);
478 }
479 }
480
481 static void reset_program_constant_version(void *value, void *context)
482 {
483 struct glsl_shader_prog_link *entry = value;
484 entry->constant_version = 0;
485 }
486
487 /**
488 * Loads the texture dimensions for NP2 fixup into the currently set GLSL program.
489 */
490 static void shader_glsl_load_np2fixup_constants(
491 IWineD3DDevice* device,
492 char usePixelShader,
493 char useVertexShader) {
494
495 const IWineD3DDeviceImpl* deviceImpl = (const IWineD3DDeviceImpl*) device;
496 const struct glsl_shader_prog_link* prog = ((struct shader_glsl_priv *)(deviceImpl->shader_priv))->glsl_program;
497
498 if (!prog) {
499 /* No GLSL program set - nothing to do. */
500 return;
501 }
502
503 if (!usePixelShader) {
504 /* NP2 texcoord fixup is (currently) only done for pixelshaders. */
505 return;
506 }
507
508 if (prog->ps_args.np2_fixup) {
509 UINT i;
510 UINT fixup = prog->ps_args.np2_fixup;
511 const WineD3D_GL_Info* gl_info = &deviceImpl->adapter->gl_info;
512 const IWineD3DStateBlockImpl* stateBlock = (const IWineD3DStateBlockImpl*) deviceImpl->stateBlock;
513
514 for (i = 0; fixup; fixup >>= 1, ++i) {
515 if (-1 != prog->np2Fixup_location[i]) {
516 const IWineD3DBaseTextureImpl* const tex = (const IWineD3DBaseTextureImpl*) stateBlock->textures[i];
517 if (!tex) {
518 FIXME("Nonexistent texture is flagged for NP2 texcoord fixup\n");
519 continue;
520 } else {
521 const float tex_dim[2] = {tex->baseTexture.pow2Matrix[0], tex->baseTexture.pow2Matrix[5]};
522 GL_EXTCALL(glUniform2fvARB(prog->np2Fixup_location[i], 1, tex_dim));
523 }
524 }
525 }
526 }
527 }
528
529 /**
530 * Loads the app-supplied constants into the currently set GLSL program.
531 */
532 static void shader_glsl_load_constants(
533 IWineD3DDevice* device,
534 char usePixelShader,
535 char useVertexShader) {
536
537 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
538 struct shader_glsl_priv *priv = deviceImpl->shader_priv;
539 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
540 const WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
541
542 GLhandleARB programId;
543 struct glsl_shader_prog_link *prog = priv->glsl_program;
544 UINT constant_version;
545 int i;
546
547 if (!prog) {
548 /* No GLSL program set - nothing to do. */
549 return;
550 }
551 programId = prog->programId;
552 constant_version = prog->constant_version;
553
554 if (useVertexShader) {
555 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
556
557 /* Load DirectX 9 float constants/uniforms for vertex shader */
558 shader_glsl_load_constantsF(vshader, gl_info, stateBlock->vertexShaderConstantF,
559 prog->vuniformF_locations, &priv->vconst_heap, priv->stack, constant_version);
560
561 /* Load DirectX 9 integer constants/uniforms for vertex shader */
562 shader_glsl_load_constantsI(vshader, gl_info, prog->vuniformI_locations, stateBlock->vertexShaderConstantI,
563 stateBlock->changed.vertexShaderConstantsI & vshader->baseShader.reg_maps.integer_constants);
564
565 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
566 shader_glsl_load_constantsB(vshader, gl_info, programId, stateBlock->vertexShaderConstantB,
567 stateBlock->changed.vertexShaderConstantsB & vshader->baseShader.reg_maps.boolean_constants);
568
569 /* Upload the position fixup params */
570 GL_EXTCALL(glUniform4fvARB(prog->posFixup_location, 1, &deviceImpl->posFixup[0]));
571 checkGLcall("glUniform4fvARB");
572 }
573
574 if (usePixelShader) {
575
576 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
577
578 /* Load DirectX 9 float constants/uniforms for pixel shader */
579 shader_glsl_load_constantsF(pshader, gl_info, stateBlock->pixelShaderConstantF,
580 prog->puniformF_locations, &priv->pconst_heap, priv->stack, constant_version);
581
582 /* Load DirectX 9 integer constants/uniforms for pixel shader */
583 shader_glsl_load_constantsI(pshader, gl_info, prog->puniformI_locations, stateBlock->pixelShaderConstantI,
584 stateBlock->changed.pixelShaderConstantsI & pshader->baseShader.reg_maps.integer_constants);
585
586 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
587 shader_glsl_load_constantsB(pshader, gl_info, programId, stateBlock->pixelShaderConstantB,
588 stateBlock->changed.pixelShaderConstantsB & pshader->baseShader.reg_maps.boolean_constants);
589
590 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
591 * It can't be 0 for a valid texbem instruction.
592 */
593 for(i = 0; i < ((IWineD3DPixelShaderImpl *) pshader)->numbumpenvmatconsts; i++) {
594 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pshader;
595 int stage = ps->luminanceconst[i].texunit;
596
597 const float *data = (const float *)&stateBlock->textureState[(int)ps->bumpenvmatconst[i].texunit][WINED3DTSS_BUMPENVMAT00];
598 GL_EXTCALL(glUniformMatrix2fvARB(prog->bumpenvmat_location[i], 1, 0, data));
599 checkGLcall("glUniformMatrix2fvARB");
600
601 /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
602 * is set too, so we can check that in the needsbumpmat check
603 */
604 if(ps->baseShader.reg_maps.luminanceparams[stage]) {
605 const GLfloat *scale = (const GLfloat *)&stateBlock->textureState[stage][WINED3DTSS_BUMPENVLSCALE];
606 const GLfloat *offset = (const GLfloat *)&stateBlock->textureState[stage][WINED3DTSS_BUMPENVLOFFSET];
607
608 GL_EXTCALL(glUniform1fvARB(prog->luminancescale_location[i], 1, scale));
609 checkGLcall("glUniform1fvARB");
610 GL_EXTCALL(glUniform1fvARB(prog->luminanceoffset_location[i], 1, offset));
611 checkGLcall("glUniform1fvARB");
612 }
613 }
614
615 if(((IWineD3DPixelShaderImpl *) pshader)->vpos_uniform) {
616 float correction_params[4];
617 if(deviceImpl->render_offscreen) {
618 correction_params[0] = 0.0;
619 correction_params[1] = 1.0;
620 } else {
621 /* position is window relative, not viewport relative */
622 correction_params[0] = ((IWineD3DSurfaceImpl *) deviceImpl->render_targets[0])->currentDesc.Height;
623 correction_params[1] = -1.0;
624 }
625 GL_EXTCALL(glUniform4fvARB(prog->ycorrection_location, 1, correction_params));
626 }
627 }
628
629 if (priv->next_constant_version == UINT_MAX)
630 {
631 TRACE("Max constant version reached, resetting to 0.\n");
632 hash_table_for_each_entry(priv->glsl_program_lookup, reset_program_constant_version, NULL);
633 priv->next_constant_version = 1;
634 }
635 else
636 {
637 prog->constant_version = priv->next_constant_version++;
638 }
639 }
640
641 static inline void update_heap_entry(struct constant_heap *heap, unsigned int idx,
642 unsigned int heap_idx, DWORD new_version)
643 {
644 struct constant_entry *entries = heap->entries;
645 unsigned int *positions = heap->positions;
646 unsigned int parent_idx;
647
648 while (heap_idx > 1)
649 {
650 parent_idx = heap_idx >> 1;
651
652 if (new_version <= entries[parent_idx].version) break;
653
654 entries[heap_idx] = entries[parent_idx];
655 positions[entries[parent_idx].idx] = heap_idx;
656 heap_idx = parent_idx;
657 }
658
659 entries[heap_idx].version = new_version;
660 entries[heap_idx].idx = idx;
661 positions[idx] = heap_idx;
662 }
663
664 static void shader_glsl_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count)
665 {
666 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
667 struct shader_glsl_priv *priv = This->shader_priv;
668 struct constant_heap *heap = &priv->vconst_heap;
669 UINT i;
670
671 for (i = start; i < count + start; ++i)
672 {
673 if (!This->stateBlock->changed.vertexShaderConstantsF[i])
674 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
675 else
676 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
677 }
678 }
679
680 static void shader_glsl_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count)
681 {
682 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
683 struct shader_glsl_priv *priv = This->shader_priv;
684 struct constant_heap *heap = &priv->pconst_heap;
685 UINT i;
686
687 for (i = start; i < count + start; ++i)
688 {
689 if (!This->stateBlock->changed.pixelShaderConstantsF[i])
690 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
691 else
692 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
693 }
694 }
695
696 /** Generate the variable & register declarations for the GLSL output target */
697 static void shader_generate_glsl_declarations(IWineD3DBaseShader *iface, const shader_reg_maps *reg_maps,
698 SHADER_BUFFER *buffer, const WineD3D_GL_Info *gl_info,
699 const struct ps_compile_args *ps_args)
700 {
701 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
702 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
703 unsigned int i, extra_constants_needed = 0;
704 const local_constant *lconst;
705
706 /* There are some minor differences between pixel and vertex shaders */
707 char pshader = shader_is_pshader_version(reg_maps->shader_version.type);
708 char prefix = pshader ? 'P' : 'V';
709
710 /* Prototype the subroutines */
711 for (i = 0; i < This->baseShader.limits.label; i++) {
712 if (reg_maps->labels[i])
713 shader_addline(buffer, "void subroutine%u();\n", i);
714 }
715
716 /* Declare the constants (aka uniforms) */
717 if (This->baseShader.limits.constant_float > 0) {
718 unsigned max_constantsF;
719 /* Unless the shader uses indirect addressing, always declare the maximum array size and ignore that we need some
720 * uniforms privately. E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup and immediate values, still
721 * declare VC[256]. If the shader needs more uniforms than we have it won't work in any case. If it uses less, the
722 * compiler will figure out which uniforms are really used and strip them out. This allows a shader to use c255 on
723 * a dx9 card, as long as it doesn't also use all the other constants.
724 *
725 * If the shader uses indirect addressing the compiler must assume that all declared uniforms are used. In this case,
726 * declare only the amount that we're assured to have.
727 *
728 * Thus we run into problems in these two cases:
729 * 1) The shader really uses more uniforms than supported
730 * 2) The shader uses indirect addressing, less constants than supported, but uses a constant index > #supported consts
731 */
732 if(pshader) {
733 /* No indirect addressing here */
734 max_constantsF = GL_LIMITS(pshader_constantsF);
735 } else {
736 if(This->baseShader.reg_maps.usesrelconstF) {
737 /* Subtract the other potential uniforms from the max available (bools, ints, and 1 row of projection matrix).
738 * Subtract another uniform for immediate values, which have to be loaded via uniform by the driver as well.
739 * The shader code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex shader code, so one vec4 should be enough
740 * (Unfortunately the Nvidia driver doesn't store 128 and -128 in one float
741 */
742 max_constantsF = GL_LIMITS(vshader_constantsF) - 3;
743 max_constantsF -= count_bits(This->baseShader.reg_maps.integer_constants);
744 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
745 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
746 * for now take this into account when calculating the number of available constants
747 */
748 max_constantsF -= count_bits(This->baseShader.reg_maps.boolean_constants);
749 /* Set by driver quirks in directx.c */
750 max_constantsF -= GLINFO_LOCATION.reserved_glsl_constants;
751 } else {
752 max_constantsF = GL_LIMITS(vshader_constantsF);
753 }
754 }
755 max_constantsF = min(This->baseShader.limits.constant_float, max_constantsF);
756 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
757 }
758
759 /* Always declare the full set of constants, the compiler can remove the unused ones because d3d doesn't(yet)
760 * support indirect int and bool constant addressing. This avoids problems if the app uses e.g. i0 and i9.
761 */
762 if (This->baseShader.limits.constant_int > 0 && This->baseShader.reg_maps.integer_constants)
763 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
764
765 if (This->baseShader.limits.constant_bool > 0 && This->baseShader.reg_maps.boolean_constants)
766 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
767
768 if(!pshader) {
769 shader_addline(buffer, "uniform vec4 posFixup;\n");
770 /* Predeclaration; This function is added at link time based on the pixel shader.
771 * VS 3.0 shaders have an array OUT[] the shader writes to, earlier versions don't have
772 * that. We know the input to the reorder function at vertex shader compile time, so
773 * we can deal with that. The reorder function for a 1.x and 2.x vertex shader can just
774 * read gl_FrontColor. The output depends on the pixel shader. The reorder function for a
775 * 1.x and 2.x pshader or for fixed function will write gl_FrontColor, and for a 3.0 shader
776 * it will write to the varying array. Here we depend on the shader optimizer on sorting that
777 * out. The nvidia driver only does that if the parameter is inout instead of out, hence the
778 * inout.
779 */
780 if (reg_maps->shader_version.major >= 3)
781 {
782 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", MAX_REG_OUTPUT);
783 } else {
784 shader_addline(buffer, "void order_ps_input();\n");
785 }
786 } else {
787 IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
788
789 ps_impl->numbumpenvmatconsts = 0;
790 for(i = 0; i < (sizeof(reg_maps->bumpmat) / sizeof(reg_maps->bumpmat[0])); i++) {
791 if(!reg_maps->bumpmat[i]) {
792 continue;
793 }
794
795 ps_impl->bumpenvmatconst[(int) ps_impl->numbumpenvmatconsts].texunit = i;
796 shader_addline(buffer, "uniform mat2 bumpenvmat%d;\n", i);
797
798 if(reg_maps->luminanceparams) {
799 ps_impl->luminanceconst[(int) ps_impl->numbumpenvmatconsts].texunit = i;
800 shader_addline(buffer, "uniform float luminancescale%d;\n", i);
801 shader_addline(buffer, "uniform float luminanceoffset%d;\n", i);
802 extra_constants_needed++;
803 } else {
804 ps_impl->luminanceconst[(int) ps_impl->numbumpenvmatconsts].texunit = -1;
805 }
806
807 extra_constants_needed++;
808 ps_impl->numbumpenvmatconsts++;
809 }
810
811 if(ps_args->srgb_correction) {
812 shader_addline(buffer, "const vec4 srgb_mul_low = vec4(%f, %f, %f, %f);\n",
813 srgb_mul_low, srgb_mul_low, srgb_mul_low, srgb_mul_low);
814 shader_addline(buffer, "const vec4 srgb_comparison = vec4(%f, %f, %f, %f);\n",
815 srgb_cmp, srgb_cmp, srgb_cmp, srgb_cmp);
816 }
817 if(reg_maps->vpos || reg_maps->usesdsy) {
818 if(This->baseShader.limits.constant_float + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF)) {
819 shader_addline(buffer, "uniform vec4 ycorrection;\n");
820 ((IWineD3DPixelShaderImpl *) This)->vpos_uniform = 1;
821 extra_constants_needed++;
822 } else {
823 /* This happens because we do not have proper tracking of the constant registers that are
824 * actually used, only the max limit of the shader version
825 */
826 FIXME("Cannot find a free uniform for vpos correction params\n");
827 shader_addline(buffer, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
828 device->render_offscreen ? 0.0 : ((IWineD3DSurfaceImpl *) device->render_targets[0])->currentDesc.Height,
829 device->render_offscreen ? 1.0 : -1.0);
830 }
831 shader_addline(buffer, "vec4 vpos;\n");
832 }
833 }
834
835 /* Declare texture samplers */
836 for (i = 0; i < This->baseShader.limits.sampler; i++) {
837 if (reg_maps->sampler_type[i])
838 {
839 switch (reg_maps->sampler_type[i])
840 {
841 case WINED3DSTT_1D:
842 shader_addline(buffer, "uniform sampler1D %csampler%u;\n", prefix, i);
843 break;
844 case WINED3DSTT_2D:
845 if(device->stateBlock->textures[i] &&
846 IWineD3DBaseTexture_GetTextureDimensions(device->stateBlock->textures[i]) == GL_TEXTURE_RECTANGLE_ARB) {
847 shader_addline(buffer, "uniform sampler2DRect %csampler%u;\n", prefix, i);
848 } else {
849 shader_addline(buffer, "uniform sampler2D %csampler%u;\n", prefix, i);
850 }
851
852 if (pshader && ps_args->np2_fixup & (1 << i))
853 {
854 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
855 * while D3D has them in the (normalized) [0,1]x[0,1] range.
856 * samplerNP2Fixup stores texture dimensions and is updated through
857 * shader_glsl_load_np2fixup_constants when the sampler changes. */
858 shader_addline(buffer, "uniform vec2 %csamplerNP2Fixup%u;\n", prefix, i);
859 }
860 break;
861 case WINED3DSTT_CUBE:
862 shader_addline(buffer, "uniform samplerCube %csampler%u;\n", prefix, i);
863 break;
864 case WINED3DSTT_VOLUME:
865 shader_addline(buffer, "uniform sampler3D %csampler%u;\n", prefix, i);
866 break;
867 default:
868 shader_addline(buffer, "uniform unsupported_sampler %csampler%u;\n", prefix, i);
869 FIXME("Unrecognized sampler type: %#x\n", reg_maps->sampler_type[i]);
870 break;
871 }
872 }
873 }
874
875 /* Declare address variables */
876 for (i = 0; i < This->baseShader.limits.address; i++) {
877 if (reg_maps->address[i])
878 shader_addline(buffer, "ivec4 A%d;\n", i);
879 }
880
881 /* Declare texture coordinate temporaries and initialize them */
882 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
883 if (reg_maps->texcoord[i])
884 shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
885 }
886
887 /* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
888 * helper function shader that is linked in at link time
889 */
890 if (pshader && reg_maps->shader_version.major >= 3)
891 {
892 if (use_vs(device->stateBlock))
893 {
894 shader_addline(buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
895 } else {
896 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
897 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
898 * pixel shader that reads the fixed function color into the packed input registers.
899 */
900 shader_addline(buffer, "vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
901 }
902 }
903
904 /* Declare output register temporaries */
905 if(This->baseShader.limits.packed_output) {
906 shader_addline(buffer, "vec4 OUT[%u];\n", This->baseShader.limits.packed_output);
907 }
908
909 /* Declare temporary variables */
910 for(i = 0; i < This->baseShader.limits.temporary; i++) {
911 if (reg_maps->temporary[i])
912 shader_addline(buffer, "vec4 R%u;\n", i);
913 }
914
915 /* Declare attributes */
916 for (i = 0; i < This->baseShader.limits.attributes; i++) {
917 if (reg_maps->attributes[i])
918 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
919 }
920
921 /* Declare loop registers aLx */
922 for (i = 0; i < reg_maps->loop_depth; i++) {
923 shader_addline(buffer, "int aL%u;\n", i);
924 shader_addline(buffer, "int tmpInt%u;\n", i);
925 }
926
927 /* Temporary variables for matrix operations */
928 shader_addline(buffer, "vec4 tmp0;\n");
929 shader_addline(buffer, "vec4 tmp1;\n");
930
931 /* Local constants use a different name so they can be loaded once at shader link time
932 * They can't be hardcoded into the shader text via LC = {x, y, z, w}; because the
933 * float -> string conversion can cause precision loss.
934 */
935 if(!This->baseShader.load_local_constsF) {
936 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
937 shader_addline(buffer, "uniform vec4 %cLC%u;\n", prefix, lconst->idx);
938 }
939 }
940
941 /* Start the main program */
942 shader_addline(buffer, "void main() {\n");
943 if(pshader && reg_maps->vpos) {
944 /* DirectX apps expect integer values, while OpenGL drivers add approximately 0.5. This causes
945 * off-by-one problems as spotted by the vPos d3d9 visual test. Unfortunately the ATI cards do
946 * not add exactly 0.5, but rather something like 0.49999999 or 0.50000001, which still causes
947 * precision troubles when we just substract 0.5.
948 *
949 * To deal with that just floor() the position. This will eliminate the fraction on all cards.
950 *
951 * TODO: Test how that behaves with multisampling once we can enable multisampling in winex11.
952 *
953 * An advantage of floor is that it works even if the driver doesn't add 1/2. It is somewhat
954 * questionable if 1.5, 2.5, ... are the proper values to return in gl_FragCoord, even though
955 * coordinates specify the pixel centers instead of the pixel corners. This code will behave
956 * correctly on drivers that returns integer values.
957 */
958 shader_addline(buffer, "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
959 }
960 }
961
962 /*****************************************************************************
963 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
964 *
965 * For more information, see http://wiki.winehq.org/DirectX-Shaders
966 ****************************************************************************/
967
968 /* Prototypes */
969 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
970 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, glsl_src_param_t *glsl_src);
971
972 /** Used for opcode modifiers - They multiply the result by the specified amount */
973 static const char * const shift_glsl_tab[] = {
974 "", /* 0 (none) */
975 "2.0 * ", /* 1 (x2) */
976 "4.0 * ", /* 2 (x4) */
977 "8.0 * ", /* 3 (x8) */
978 "16.0 * ", /* 4 (x16) */
979 "32.0 * ", /* 5 (x32) */
980 "", /* 6 (x64) */
981 "", /* 7 (x128) */
982 "", /* 8 (d256) */
983 "", /* 9 (d128) */
984 "", /* 10 (d64) */
985 "", /* 11 (d32) */
986 "0.0625 * ", /* 12 (d16) */
987 "0.125 * ", /* 13 (d8) */
988 "0.25 * ", /* 14 (d4) */
989 "0.5 * " /* 15 (d2) */
990 };
991
992 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
993 static void shader_glsl_gen_modifier(DWORD src_modifier, const char *in_reg, const char *in_regswizzle, char *out_str)
994 {
995 out_str[0] = 0;
996
997 switch (src_modifier)
998 {
999 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1000 case WINED3DSPSM_DW:
1001 case WINED3DSPSM_NONE:
1002 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1003 break;
1004 case WINED3DSPSM_NEG:
1005 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1006 break;
1007 case WINED3DSPSM_NOT:
1008 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1009 break;
1010 case WINED3DSPSM_BIAS:
1011 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1012 break;
1013 case WINED3DSPSM_BIASNEG:
1014 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1015 break;
1016 case WINED3DSPSM_SIGN:
1017 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1018 break;
1019 case WINED3DSPSM_SIGNNEG:
1020 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1021 break;
1022 case WINED3DSPSM_COMP:
1023 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1024 break;
1025 case WINED3DSPSM_X2:
1026 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1027 break;
1028 case WINED3DSPSM_X2NEG:
1029 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1030 break;
1031 case WINED3DSPSM_ABS:
1032 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1033 break;
1034 case WINED3DSPSM_ABSNEG:
1035 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1036 break;
1037 default:
1038 FIXME("Unhandled modifier %u\n", src_modifier);
1039 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1040 }
1041 }
1042
1043 /** Writes the GLSL variable name that corresponds to the register that the
1044 * DX opcode parameter is trying to access */
1045 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1046 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1047 {
1048 /* oPos, oFog and oPts in D3D */
1049 static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
1050
1051 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
1052 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1053 const WineD3D_GL_Info* gl_info = &deviceImpl->adapter->gl_info;
1054 char pshader = shader_is_pshader_version(This->baseShader.reg_maps.shader_version.type);
1055
1056 *is_color = FALSE;
1057
1058 switch (reg->type)
1059 {
1060 case WINED3DSPR_TEMP:
1061 sprintf(register_name, "R%u", reg->idx);
1062 break;
1063
1064 case WINED3DSPR_INPUT:
1065 /* vertex shaders */
1066 if (!pshader)
1067 {
1068 if (((IWineD3DVertexShaderImpl *)This)->cur_args->swizzle_map & (1 << reg->idx)) *is_color = TRUE;
1069 sprintf(register_name, "attrib%u", reg->idx);
1070 break;
1071 }
1072
1073 /* pixel shaders >= 3.0 */
1074 if (This->baseShader.reg_maps.shader_version.major >= 3)
1075 {
1076 DWORD idx = ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg->idx];
1077 DWORD in_count = GL_LIMITS(glsl_varyings) / 4;
1078
1079 if (reg->rel_addr)
1080 {
1081 glsl_src_param_t rel_param;
1082
1083 shader_glsl_add_src_param(ins, reg->rel_addr, WINED3DSP_WRITEMASK_0, &rel_param);
1084
1085 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
1086 * operation there */
1087 if (idx)
1088 {
1089 if (((IWineD3DPixelShaderImpl *)This)->declared_in_count > in_count)
1090 {
1091 sprintf(register_name,
1092 "((%s + %u) > %d ? (%s + %u) > %d ? gl_SecondaryColor : gl_Color : IN[%s + %u])",
1093 rel_param.param_str, idx, in_count - 1, rel_param.param_str, idx, in_count,
1094 rel_param.param_str, idx);
1095 }
1096 else
1097 {
1098 sprintf(register_name, "IN[%s + %u]", rel_param.param_str, idx);
1099 }
1100 }
1101 else
1102 {
1103 if (((IWineD3DPixelShaderImpl *)This)->declared_in_count > in_count)
1104 {
1105 sprintf(register_name, "((%s) > %d ? (%s) > %d ? gl_SecondaryColor : gl_Color : IN[%s])",
1106 rel_param.param_str, in_count - 1, rel_param.param_str, in_count,
1107 rel_param.param_str);
1108 }
1109 else
1110 {
1111 sprintf(register_name, "IN[%s]", rel_param.param_str);
1112 }
1113 }
1114 }
1115 else
1116 {
1117 if (idx == in_count) sprintf(register_name, "gl_Color");
1118 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
1119 else sprintf(register_name, "IN[%u]", idx);
1120 }
1121 }
1122 else
1123 {
1124 if (reg->idx == 0) strcpy(register_name, "gl_Color");
1125 else strcpy(register_name, "gl_SecondaryColor");
1126 break;
1127 }
1128 break;
1129
1130 case WINED3DSPR_CONST:
1131 {
1132 const char prefix = pshader ? 'P' : 'V';
1133
1134 /* Relative addressing */
1135 if (reg->rel_addr)
1136 {
1137 glsl_src_param_t rel_param;
1138 shader_glsl_add_src_param(ins, reg->rel_addr, WINED3DSP_WRITEMASK_0, &rel_param);
1139 if (reg->idx) sprintf(register_name, "%cC[%s + %u]", prefix, rel_param.param_str, reg->idx);
1140 else sprintf(register_name, "%cC[%s]", prefix, rel_param.param_str);
1141 }
1142 else
1143 {
1144 if (shader_constant_is_local(This, reg->idx))
1145 sprintf(register_name, "%cLC%u", prefix, reg->idx);
1146 else
1147 sprintf(register_name, "%cC[%u]", prefix, reg->idx);
1148 }
1149 }
1150 break;
1151
1152 case WINED3DSPR_CONSTINT:
1153 if (pshader) sprintf(register_name, "PI[%u]", reg->idx);
1154 else sprintf(register_name, "VI[%u]", reg->idx);
1155 break;
1156
1157 case WINED3DSPR_CONSTBOOL:
1158 if (pshader) sprintf(register_name, "PB[%u]", reg->idx);
1159 else sprintf(register_name, "VB[%u]", reg->idx);
1160 break;
1161
1162 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1163 if (pshader) sprintf(register_name, "T%u", reg->idx);
1164 else sprintf(register_name, "A%u", reg->idx);
1165 break;
1166
1167 case WINED3DSPR_LOOP:
1168 sprintf(register_name, "aL%u", This->baseShader.cur_loop_regno - 1);
1169 break;
1170
1171 case WINED3DSPR_SAMPLER:
1172 if (pshader) sprintf(register_name, "Psampler%u", reg->idx);
1173 else sprintf(register_name, "Vsampler%u", reg->idx);
1174 break;
1175
1176 case WINED3DSPR_COLOROUT:
1177 if (reg->idx >= GL_LIMITS(buffers))
1178 WARN("Write to render target %u, only %d supported\n", reg->idx, 4);
1179
1180 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) sprintf(register_name, "gl_FragData[%u]", reg->idx);
1181 /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
1182 else sprintf(register_name, "gl_FragColor");
1183 break;
1184
1185 case WINED3DSPR_RASTOUT:
1186 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx]);
1187 break;
1188
1189 case WINED3DSPR_DEPTHOUT:
1190 sprintf(register_name, "gl_FragDepth");
1191 break;
1192
1193 case WINED3DSPR_ATTROUT:
1194 if (reg->idx == 0) sprintf(register_name, "gl_FrontColor");
1195 else sprintf(register_name, "gl_FrontSecondaryColor");
1196 break;
1197
1198 case WINED3DSPR_TEXCRDOUT:
1199 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1200 if (This->baseShader.reg_maps.shader_version.major >= 3) sprintf(register_name, "OUT[%u]", reg->idx);
1201 else sprintf(register_name, "gl_TexCoord[%u]", reg->idx);
1202 break;
1203
1204 case WINED3DSPR_MISCTYPE:
1205 if (reg->idx == 0)
1206 {
1207 /* vPos */
1208 sprintf(register_name, "vpos");
1209 }
1210 else if (reg->idx == 1)
1211 {
1212 /* Note that gl_FrontFacing is a bool, while vFace is
1213 * a float for which the sign determines front/back */
1214 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1215 }
1216 else
1217 {
1218 FIXME("Unhandled misctype register %d\n", reg->idx);
1219 sprintf(register_name, "unrecognized_register");
1220 }
1221 break;
1222
1223 case WINED3DSPR_IMMCONST:
1224 switch (reg->immconst_type)
1225 {
1226 case WINED3D_IMMCONST_FLOAT:
1227 sprintf(register_name, "%.8e", *(float *)reg->immconst_data);
1228 break;
1229
1230 case WINED3D_IMMCONST_FLOAT4:
1231 sprintf(register_name, "vec4(%.8e, %.8e, %.8e, %.8e)",
1232 *(float *)&reg->immconst_data[0], *(float *)&reg->immconst_data[1],
1233 *(float *)&reg->immconst_data[2], *(float *)&reg->immconst_data[3]);
1234 break;
1235
1236 default:
1237 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
1238 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
1239 }
1240 break;
1241
1242 default:
1243 FIXME("Unhandled register name Type(%d)\n", reg->type);
1244 sprintf(register_name, "unrecognized_register");
1245 break;
1246 }
1247 }
1248
1249 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1250 {
1251 *str++ = '.';
1252 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1253 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1254 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1255 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1256 *str = '\0';
1257 }
1258
1259 /* Get the GLSL write mask for the destination register */
1260 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1261 {
1262 DWORD mask = param->write_mask;
1263
1264 if (shader_is_scalar(&param->reg))
1265 {
1266 mask = WINED3DSP_WRITEMASK_0;
1267 *write_mask = '\0';
1268 }
1269 else
1270 {
1271 shader_glsl_write_mask_to_str(mask, write_mask);
1272 }
1273
1274 return mask;
1275 }
1276
1277 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1278 unsigned int size = 0;
1279
1280 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1281 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1282 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1283 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1284
1285 return size;
1286 }
1287
1288 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
1289 {
1290 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1291 * but addressed as "rgba". To fix this we need to swap the register's x
1292 * and z components. */
1293 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1294
1295 *str++ = '.';
1296 /* swizzle bits fields: wwzzyyxx */
1297 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
1298 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
1299 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
1300 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
1301 *str = '\0';
1302 }
1303
1304 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
1305 BOOL fixup, DWORD mask, char *swizzle_str)
1306 {
1307 if (shader_is_scalar(&param->reg))
1308 *swizzle_str = '\0';
1309 else
1310 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
1311 }
1312
1313 /* From a given parameter token, generate the corresponding GLSL string.
1314 * Also, return the actual register name and swizzle in case the
1315 * caller needs this information as well. */
1316 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1317 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, glsl_src_param_t *glsl_src)
1318 {
1319 BOOL is_color = FALSE;
1320 char swizzle_str[6];
1321
1322 glsl_src->reg_name[0] = '\0';
1323 glsl_src->param_str[0] = '\0';
1324 swizzle_str[0] = '\0';
1325
1326 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
1327 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
1328 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
1329 }
1330
1331 /* From a given parameter token, generate the corresponding GLSL string.
1332 * Also, return the actual register name and swizzle in case the
1333 * caller needs this information as well. */
1334 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1335 const struct wined3d_shader_dst_param *wined3d_dst, glsl_dst_param_t *glsl_dst)
1336 {
1337 BOOL is_color = FALSE;
1338
1339 glsl_dst->mask_str[0] = '\0';
1340 glsl_dst->reg_name[0] = '\0';
1341
1342 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
1343 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1344 }
1345
1346 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1347 static DWORD shader_glsl_append_dst_ext(SHADER_BUFFER *buffer,
1348 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst)
1349 {
1350 glsl_dst_param_t glsl_dst;
1351 DWORD mask;
1352
1353 mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst);
1354 if (mask) shader_addline(buffer, "%s%s = %s(", glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1355
1356 return mask;
1357 }
1358
1359 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1360 static DWORD shader_glsl_append_dst(SHADER_BUFFER *buffer, const struct wined3d_shader_instruction *ins)
1361 {
1362 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
1363 }
1364
1365 /** Process GLSL instruction modifiers */
1366 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1367 {
1368 glsl_dst_param_t dst_param;
1369 DWORD modifiers;
1370
1371 if (!ins->dst_count) return;
1372
1373 modifiers = ins->dst[0].modifiers;
1374 if (!modifiers) return;
1375
1376 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
1377
1378 if (modifiers & WINED3DSPDM_SATURATE)
1379 {
1380 /* _SAT means to clamp the value of the register to between 0 and 1 */
1381 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1382 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1383 }
1384
1385 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
1386 {
1387 FIXME("_centroid modifier not handled\n");
1388 }
1389
1390 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
1391 {
1392 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1393 }
1394 }
1395
1396 static inline const char *shader_get_comp_op(DWORD op)
1397 {
1398 switch (op) {
1399 case COMPARISON_GT: return ">";
1400 case COMPARISON_EQ: return "==";
1401 case COMPARISON_GE: return ">=";
1402 case COMPARISON_LT: return "<";
1403 case COMPARISON_NE: return "!=";
1404 case COMPARISON_LE: return "<=";
1405 default:
1406 FIXME("Unrecognized comparison value: %u\n", op);
1407 return "(\?\?)";
1408 }
1409 }
1410
1411 static void shader_glsl_get_sample_function(DWORD sampler_type, DWORD flags, glsl_sample_function_t *sample_function)
1412 {
1413 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
1414 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_RECT;
1415 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
1416 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
1417
1418 /* Note that there's no such thing as a projected cube texture. */
1419 switch(sampler_type) {
1420 case WINED3DSTT_1D:
1421 if(lod) {
1422 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
1423 } else if(grad) {
1424 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
1425 } else {
1426 sample_function->name = projected ? "texture1DProj" : "texture1D";
1427 }
1428 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1429 break;
1430 case WINED3DSTT_2D:
1431 if(texrect) {
1432 if(lod) {
1433 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
1434 } else if(grad) {
1435 /* What good are texrect grad functions? I don't know, but GL_EXT_gpu_shader4 defines them.
1436 * There is no GL_ARB_shader_texture_lod spec yet, so I don't know if they're defined there
1437 */
1438 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
1439 } else {
1440 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
1441 }
1442 } else {
1443 if(lod) {
1444 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
1445 } else if(grad) {
1446 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
1447 } else {
1448 sample_function->name = projected ? "texture2DProj" : "texture2D";
1449 }
1450 }
1451 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1452 break;
1453 case WINED3DSTT_CUBE:
1454 if(lod) {
1455 sample_function->name = "textureCubeLod";
1456 } else if(grad) {
1457 sample_function->name = "textureCubeGradARB";
1458 } else {
1459 sample_function->name = "textureCube";
1460 }
1461 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1462 break;
1463 case WINED3DSTT_VOLUME:
1464 if(lod) {
1465 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
1466 } else if(grad) {
1467 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
1468 } else {
1469 sample_function->name = projected ? "texture3DProj" : "texture3D";
1470 }
1471 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1472 break;
1473 default:
1474 sample_function->name = "";
1475 sample_function->coord_mask = 0;
1476 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
1477 break;
1478 }
1479 }
1480
1481 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
1482 BOOL sign_fixup, enum fixup_channel_source channel_source)
1483 {
1484 switch(channel_source)
1485 {
1486 case CHANNEL_SOURCE_ZERO:
1487 strcat(arguments, "0.0");
1488 break;
1489
1490 case CHANNEL_SOURCE_ONE:
1491 strcat(arguments, "1.0");
1492 break;
1493
1494 case CHANNEL_SOURCE_X:
1495 strcat(arguments, reg_name);
1496 strcat(arguments, ".x");
1497 break;
1498
1499 case CHANNEL_SOURCE_Y:
1500 strcat(arguments, reg_name);
1501 strcat(arguments, ".y");
1502 break;
1503
1504 case CHANNEL_SOURCE_Z:
1505 strcat(arguments, reg_name);
1506 strcat(arguments, ".z");
1507 break;
1508
1509 case CHANNEL_SOURCE_W:
1510 strcat(arguments, reg_name);
1511 strcat(arguments, ".w");
1512 break;
1513
1514 default:
1515 FIXME("Unhandled channel source %#x\n", channel_source);
1516 strcat(arguments, "undefined");
1517 break;
1518 }
1519
1520 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
1521 }
1522
1523 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
1524 {
1525 struct wined3d_shader_dst_param dst;
1526 unsigned int mask_size, remaining;
1527 glsl_dst_param_t dst_param;
1528 char arguments[256];
1529 DWORD mask;
1530
1531 mask = 0;
1532 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) mask |= WINED3DSP_WRITEMASK_0;
1533 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) mask |= WINED3DSP_WRITEMASK_1;
1534 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) mask |= WINED3DSP_WRITEMASK_2;
1535 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) mask |= WINED3DSP_WRITEMASK_3;
1536 mask &= ins->dst[0].write_mask;
1537
1538 if (!mask) return; /* Nothing to do */
1539
1540 if (is_yuv_fixup(fixup))
1541 {
1542 enum yuv_fixup yuv_fixup = get_yuv_fixup(fixup);
1543 FIXME("YUV fixup (%#x) not supported\n", yuv_fixup);
1544 return;
1545 }
1546
1547 mask_size = shader_glsl_get_write_mask_size(mask);
1548
1549 dst = ins->dst[0];
1550 dst.write_mask = mask;
1551 shader_glsl_add_dst_param(ins, &dst, &dst_param);
1552
1553 arguments[0] = '\0';
1554 remaining = mask_size;
1555 if (mask & WINED3DSP_WRITEMASK_0)
1556 {
1557 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.x_sign_fixup, fixup.x_source);
1558 if (--remaining) strcat(arguments, ", ");
1559 }
1560 if (mask & WINED3DSP_WRITEMASK_1)
1561 {
1562 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.y_sign_fixup, fixup.y_source);
1563 if (--remaining) strcat(arguments, ", ");
1564 }
1565 if (mask & WINED3DSP_WRITEMASK_2)
1566 {
1567 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.z_sign_fixup, fixup.z_source);
1568 if (--remaining) strcat(arguments, ", ");
1569 }
1570 if (mask & WINED3DSP_WRITEMASK_3)
1571 {
1572 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.w_sign_fixup, fixup.w_source);
1573 if (--remaining) strcat(arguments, ", ");
1574 }
1575
1576 if (mask_size > 1)
1577 {
1578 shader_addline(ins->ctx->buffer, "%s%s = vec%u(%s);\n",
1579 dst_param.reg_name, dst_param.mask_str, mask_size, arguments);
1580 }
1581 else
1582 {
1583 shader_addline(ins->ctx->buffer, "%s%s = %s;\n", dst_param.reg_name, dst_param.mask_str, arguments);
1584 }
1585 }
1586
1587 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
1588 DWORD sampler, const glsl_sample_function_t *sample_function, DWORD swizzle,
1589 const char *dx, const char *dy,
1590 const char *bias, const char *coord_reg_fmt, ...)
1591 {
1592 const char *sampler_base;
1593 char dst_swizzle[6];
1594 struct color_fixup_desc fixup;
1595 BOOL np2_fixup = FALSE;
1596 va_list args;
1597
1598 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
1599
1600 if (shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type))
1601 {
1602 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
1603 fixup = This->cur_args->color_fixup[sampler];
1604 sampler_base = "Psampler";
1605
1606 if(This->cur_args->np2_fixup & (1 << sampler)) {
1607 if(bias) {
1608 FIXME("Biased sampling from NP2 textures is unsupported\n");
1609 } else {
1610 np2_fixup = TRUE;
1611 }
1612 }
1613 } else {
1614 sampler_base = "Vsampler";
1615 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
1616 }
1617
1618 shader_glsl_append_dst(ins->ctx->buffer, ins);
1619
1620 shader_addline(ins->ctx->buffer, "%s(%s%u, ", sample_function->name, sampler_base, sampler);
1621
1622 va_start(args, coord_reg_fmt);
1623 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
1624 va_end(args);
1625
1626 if(bias) {
1627 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
1628 } else {
1629 if (np2_fixup) {
1630 shader_addline(ins->ctx->buffer, " * PsamplerNP2Fixup%u)%s);\n", sampler, dst_swizzle);
1631 } else if(dx && dy) {
1632 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
1633 } else {
1634 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
1635 }
1636 }
1637
1638 if(!is_identity_fixup(fixup)) {
1639 shader_glsl_color_correction(ins, fixup);
1640 }
1641 }
1642
1643 /*****************************************************************************
1644 *
1645 * Begin processing individual instruction opcodes
1646 *
1647 ****************************************************************************/
1648
1649 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
1650 static void shader_glsl_arith(const struct wined3d_shader_instruction *ins)
1651 {
1652 SHADER_BUFFER *buffer = ins->ctx->buffer;
1653 glsl_src_param_t src0_param;
1654 glsl_src_param_t src1_param;
1655 DWORD write_mask;
1656 char op;
1657
1658 /* Determine the GLSL operator to use based on the opcode */
1659 switch (ins->handler_idx)
1660 {
1661 case WINED3DSIH_MUL: op = '*'; break;
1662 case WINED3DSIH_ADD: op = '+'; break;
1663 case WINED3DSIH_SUB: op = '-'; break;
1664 default:
1665 op = ' ';
1666 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
1667 break;
1668 }
1669
1670 write_mask = shader_glsl_append_dst(buffer, ins);
1671 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
1672 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
1673 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
1674 }
1675
1676 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1677 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
1678 {
1679 SHADER_BUFFER *buffer = ins->ctx->buffer;
1680 glsl_src_param_t src0_param;
1681 DWORD write_mask;
1682
1683 write_mask = shader_glsl_append_dst(buffer, ins);
1684 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
1685
1686 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1687 * shader versions WINED3DSIO_MOVA is used for this. */
1688 if (ins->ctx->reg_maps->shader_version.major == 1
1689 && !shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type)
1690 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
1691 {
1692 /* This is a simple floor() */
1693 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1694 if (mask_size > 1) {
1695 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
1696 } else {
1697 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
1698 }
1699 }
1700 else if(ins->handler_idx == WINED3DSIH_MOVA)
1701 {
1702 /* We need to *round* to the nearest int here. */
1703 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1704 if (mask_size > 1) {
1705 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);
1706 } else {
1707 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param.param_str, src0_param.param_str);
1708 }
1709 } else {
1710 shader_addline(buffer, "%s);\n", src0_param.param_str);
1711 }
1712 }
1713
1714 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
1715 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
1716 {
1717 SHADER_BUFFER *buffer = ins->ctx->buffer;
1718 glsl_src_param_t src0_param;
1719 glsl_src_param_t src1_param;
1720 DWORD dst_write_mask, src_write_mask;
1721 unsigned int dst_size = 0;
1722
1723 dst_write_mask = shader_glsl_append_dst(buffer, ins);
1724 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1725
1726 /* dp3 works on vec3, dp4 on vec4 */
1727 if (ins->handler_idx == WINED3DSIH_DP4)
1728 {
1729 src_write_mask = WINED3DSP_WRITEMASK_ALL;
1730 } else {
1731 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1732 }
1733
1734 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
1735 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
1736
1737 if (dst_size > 1) {
1738 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1739 } else {
1740 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
1741 }
1742 }
1743
1744 /* Note that this instruction has some restrictions. The destination write mask
1745 * can't contain the w component, and the source swizzles have to be .xyzw */
1746 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
1747 {
1748 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1749 glsl_src_param_t src0_param;
1750 glsl_src_param_t src1_param;
1751 char dst_mask[6];
1752
1753 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
1754 shader_glsl_append_dst(ins->ctx->buffer, ins);
1755 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
1756 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
1757 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
1758 }
1759
1760 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1761 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1762 * GLSL uses the value as-is. */
1763 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
1764 {
1765 SHADER_BUFFER *buffer = ins->ctx->buffer;
1766 glsl_src_param_t src0_param;
1767 glsl_src_param_t src1_param;
1768 DWORD dst_write_mask;
1769 unsigned int dst_size;
1770
1771 dst_write_mask = shader_glsl_append_dst(buffer, ins);
1772 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1773
1774 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
1775 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
1776
1777 if (dst_size > 1) {
1778 shader_addline(buffer, "vec%d(pow(abs(%s), %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1779 } else {
1780 shader_addline(buffer, "pow(abs(%s), %s));\n", src0_param.param_str, src1_param.param_str);
1781 }
1782 }
1783
1784 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
1785 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
1786 * GLSL uses the value as-is. */
1787 static void shader_glsl_log(const struct wined3d_shader_instruction *ins)
1788 {
1789 SHADER_BUFFER *buffer = ins->ctx->buffer;
1790 glsl_src_param_t src0_param;
1791 DWORD dst_write_mask;
1792 unsigned int dst_size;
1793
1794 dst_write_mask = shader_glsl_append_dst(buffer, ins);
1795 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1796
1797 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
1798
1799 if (dst_size > 1) {
1800 shader_addline(buffer, "vec%d(log2(abs(%s))));\n", dst_size, src0_param.param_str);
1801 } else {
1802 shader_addline(buffer, "log2(abs(%s)));\n", src0_param.param_str);
1803 }
1804 }
1805
1806 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1807 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
1808 {
1809 SHADER_BUFFER *buffer = ins->ctx->buffer;
1810 glsl_src_param_t src_param;
1811 const char *instruction;
1812 DWORD write_mask;
1813 unsigned i;
1814
1815 /* Determine the GLSL function to use based on the opcode */
1816 /* TODO: Possibly make this a table for faster lookups */
1817 switch (ins->handler_idx)
1818 {
1819 case WINED3DSIH_MIN: instruction = "min"; break;
1820 case WINED3DSIH_MAX: instruction = "max"; break;
1821 case WINED3DSIH_ABS: instruction = "abs"; break;
1822 case WINED3DSIH_FRC: instruction = "fract"; break;
1823 case WINED3DSIH_NRM: instruction = "normalize"; break;
1824 case WINED3DSIH_EXP: instruction = "exp2"; break;
1825 case WINED3DSIH_SGN: instruction = "sign"; break;
1826 case WINED3DSIH_DSX: instruction = "dFdx"; break;
1827 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
1828 default: instruction = "";
1829 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
1830 break;
1831 }
1832
1833 write_mask = shader_glsl_append_dst(buffer, ins);
1834
1835 shader_addline(buffer, "%s(", instruction);
1836
1837 if (ins->src_count)
1838 {
1839 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
1840 shader_addline(buffer, "%s", src_param.param_str);
1841 for (i = 1; i < ins->src_count; ++i)
1842 {
1843 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
1844 shader_addline(buffer, ", %s", src_param.param_str);
1845 }
1846 }
1847
1848 shader_addline(buffer, "));\n");
1849 }
1850
1851 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1852 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1853 * dst.x = 2^(floor(src))
1854 * dst.y = src - floor(src)
1855 * dst.z = 2^src (partial precision is allowed, but optional)
1856 * dst.w = 1.0;
1857 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1858 * dst = 2^src; (partial precision is allowed, but optional)
1859 */
1860 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
1861 {
1862 glsl_src_param_t src_param;
1863
1864 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
1865
1866 if (ins->ctx->reg_maps->shader_version.major < 2)
1867 {
1868 char dst_mask[6];
1869
1870 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
1871 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
1872 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
1873 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
1874
1875 shader_glsl_append_dst(ins->ctx->buffer, ins);
1876 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
1877 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
1878 } else {
1879 DWORD write_mask;
1880 unsigned int mask_size;
1881
1882 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
1883 mask_size = shader_glsl_get_write_mask_size(write_mask);
1884
1885 if (mask_size > 1) {
1886 shader_addline(ins->ctx->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
1887 } else {
1888 shader_addline(ins->ctx->buffer, "exp2(%s));\n", src_param.param_str);
1889 }
1890 }
1891 }
1892
1893 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1894 static void shader_glsl_rcp(const struct wined3d_shader_instruction *ins)
1895 {
1896 glsl_src_param_t src_param;
1897 DWORD write_mask;
1898 unsigned int mask_size;
1899
1900 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
1901 mask_size = shader_glsl_get_write_mask_size(write_mask);
1902 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
1903
1904 if (mask_size > 1) {
1905 shader_addline(ins->ctx->buffer, "vec%d(1.0 / %s));\n", mask_size, src_param.param_str);
1906 } else {
1907 shader_addline(ins->ctx->buffer, "1.0 / %s);\n", src_param.param_str);
1908 }
1909 }
1910
1911 static void shader_glsl_rsq(const struct wined3d_shader_instruction *ins)
1912 {
1913 SHADER_BUFFER *buffer = ins->ctx->buffer;
1914 glsl_src_param_t src_param;
1915 DWORD write_mask;
1916 unsigned int mask_size;
1917
1918 write_mask = shader_glsl_append_dst(buffer, ins);
1919 mask_size = shader_glsl_get_write_mask_size(write_mask);
1920
1921 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
1922
1923 if (mask_size > 1) {
1924 shader_addline(buffer, "vec%d(inversesqrt(%s)));\n", mask_size, src_param.param_str);
1925 } else {
1926 shader_addline(buffer, "inversesqrt(%s));\n", src_param.param_str);
1927 }
1928 }
1929
1930 /** Process signed comparison opcodes in GLSL. */
1931 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
1932 {
1933 glsl_src_param_t src0_param;
1934 glsl_src_param_t src1_param;
1935 DWORD write_mask;
1936 unsigned int mask_size;
1937
1938 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
1939 mask_size = shader_glsl_get_write_mask_size(write_mask);
1940 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
1941 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
1942
1943 if (mask_size > 1) {
1944 const char *compare;
1945
1946 switch(ins->handler_idx)
1947 {
1948 case WINED3DSIH_SLT: compare = "lessThan"; break;
1949 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
1950 default: compare = "";
1951 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
1952 }
1953
1954 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
1955 src0_param.param_str, src1_param.param_str);
1956 } else {
1957 switch(ins->handler_idx)
1958 {
1959 case WINED3DSIH_SLT:
1960 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
1961 * to return 0.0 but step returns 1.0 because step is not < x
1962 * An alternative is a bvec compare padded with an unused second component.
1963 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
1964 * issue. Playing with not() is not possible either because not() does not accept
1965 * a scalar.
1966 */
1967 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
1968 src0_param.param_str, src1_param.param_str);
1969 break;
1970 case WINED3DSIH_SGE:
1971 /* Here we can use the step() function and safe a conditional */
1972 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
1973 break;
1974 default:
1975 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
1976 }
1977
1978 }
1979 }
1980
1981 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1982 static void shader_glsl_cmp(const struct wined3d_shader_instruction *ins)
1983 {
1984 glsl_src_param_t src0_param;
1985 glsl_src_param_t src1_param;
1986 glsl_src_param_t src2_param;
1987 DWORD write_mask, cmp_channel = 0;
1988 unsigned int i, j;
1989 char mask_char[6];
1990 BOOL temp_destination = FALSE;
1991
1992 if (shader_is_scalar(&ins->src[0].reg))
1993 {
1994 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
1995
1996 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
1997 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
1998 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
1999
2000 shader_addline(ins->ctx->buffer, "%s >= 0.0 ? %s : %s);\n",
2001 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2002 } else {
2003 DWORD dst_mask = ins->dst[0].write_mask;
2004 struct wined3d_shader_dst_param dst = ins->dst[0];
2005
2006 /* Cycle through all source0 channels */
2007 for (i=0; i<4; i++) {
2008 write_mask = 0;
2009 /* Find the destination channels which use the current source0 channel */
2010 for (j=0; j<4; j++) {
2011 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2012 {
2013 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2014 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2015 }
2016 }
2017 dst.write_mask = dst_mask & write_mask;
2018
2019 /* Splitting the cmp instruction up in multiple lines imposes a problem:
2020 * The first lines may overwrite source parameters of the following lines.
2021 * Deal with that by using a temporary destination register if needed
2022 */
2023 if ((ins->src[0].reg.idx == ins->dst[0].reg.idx
2024 && ins->src[0].reg.type == ins->dst[0].reg.type)
2025 || (ins->src[1].reg.idx == ins->dst[0].reg.idx
2026 && ins->src[1].reg.type == ins->dst[0].reg.type)
2027 || (ins->src[2].reg.idx == ins->dst[0].reg.idx
2028 && ins->src[2].reg.type == ins->dst[0].reg.type))
2029 {
2030 write_mask = shader_glsl_get_write_mask(&dst, mask_char);
2031 if (!write_mask) continue;
2032 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
2033 temp_destination = TRUE;
2034 } else {
2035 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst);
2036 if (!write_mask) continue;
2037 }
2038
2039 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2040 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2041 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2042
2043 shader_addline(ins->ctx->buffer, "%s >= 0.0 ? %s : %s);\n",
2044 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2045 }
2046
2047 if(temp_destination) {
2048 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
2049 shader_glsl_append_dst(ins->ctx->buffer, ins);
2050 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
2051 }
2052 }
2053
2054 }
2055
2056 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2057 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2058 * the compare is done per component of src0. */
2059 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
2060 {
2061 struct wined3d_shader_dst_param dst;
2062 glsl_src_param_t src0_param;
2063 glsl_src_param_t src1_param;
2064 glsl_src_param_t src2_param;
2065 DWORD write_mask, cmp_channel = 0;
2066 unsigned int i, j;
2067 DWORD dst_mask;
2068 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2069 ins->ctx->reg_maps->shader_version.minor);
2070
2071 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
2072 {
2073 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2074 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2075 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2076 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2077
2078 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
2079 if (ins->coissue)
2080 {
2081 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
2082 } else {
2083 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2084 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2085 }
2086 return;
2087 }
2088 /* Cycle through all source0 channels */
2089 dst_mask = ins->dst[0].write_mask;
2090 dst = ins->dst[0];
2091 for (i=0; i<4; i++) {
2092 write_mask = 0;
2093 /* Find the destination channels which use the current source0 channel */
2094 for (j=0; j<4; j++) {
2095 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2096 {
2097 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2098 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2099 }
2100 }
2101
2102 dst.write_mask = dst_mask & write_mask;
2103 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst);
2104 if (!write_mask) continue;
2105
2106 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2107 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2108 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2109
2110 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2111 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2112 }
2113 }
2114
2115 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2116 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
2117 {
2118 glsl_src_param_t src0_param;
2119 glsl_src_param_t src1_param;
2120 glsl_src_param_t src2_param;
2121 DWORD write_mask;
2122
2123 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2124 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2125 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2126 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2127 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
2128 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2129 }
2130
2131 /** Handles transforming all WINED3DSIO_M?x? opcodes for
2132 Vertex shaders to GLSL codes */
2133 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
2134 {
2135 int i;
2136 int nComponents = 0;
2137 struct wined3d_shader_dst_param tmp_dst = {{0}};
2138 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
2139 struct wined3d_shader_instruction tmp_ins;
2140
2141 memset(&tmp_ins, 0, sizeof(tmp_ins));
2142
2143 /* Set constants for the temporary argument */
2144 tmp_ins.ctx = ins->ctx;
2145 tmp_ins.dst_count = 1;
2146 tmp_ins.dst = &tmp_dst;
2147 tmp_ins.src_count = 2;
2148 tmp_ins.src = tmp_src;
2149
2150 switch(ins->handler_idx)
2151 {
2152 case WINED3DSIH_M4x4:
2153 nComponents = 4;
2154 tmp_ins.handler_idx = WINED3DSIH_DP4;
2155 break;
2156 case WINED3DSIH_M4x3:
2157 nComponents = 3;
2158 tmp_ins.handler_idx = WINED3DSIH_DP4;
2159 break;
2160 case WINED3DSIH_M3x4:
2161 nComponents = 4;
2162 tmp_ins.handler_idx = WINED3DSIH_DP3;
2163 break;
2164 case WINED3DSIH_M3x3:
2165 nComponents = 3;
2166 tmp_ins.handler_idx = WINED3DSIH_DP3;
2167 break;
2168 case WINED3DSIH_M3x2:
2169 nComponents = 2;
2170 tmp_ins.handler_idx = WINED3DSIH_DP3;
2171 break;
2172 default:
2173 break;
2174 }
2175
2176 tmp_dst = ins->dst[0];
2177 tmp_src[0] = ins->src[0];
2178 tmp_src[1] = ins->src[1];
2179 for (i = 0; i < nComponents; ++i)
2180 {
2181 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
2182 shader_glsl_dot(&tmp_ins);
2183 ++tmp_src[1].reg.idx;
2184 }
2185 }
2186
2187 /**
2188 The LRP instruction performs a component-wise linear interpolation
2189 between the second and third operands using the first operand as the
2190 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
2191 This is equivalent to mix(src2, src1, src0);
2192 */
2193 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
2194 {
2195 glsl_src_param_t src0_param;
2196 glsl_src_param_t src1_param;
2197 glsl_src_param_t src2_param;
2198 DWORD write_mask;
2199
2200 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2201
2202 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2203 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2204 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2205
2206 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
2207 src2_param.param_str, src1_param.param_str, src0_param.param_str);
2208 }
2209
2210 /** Process the WINED3DSIO_LIT instruction in GLSL:
2211 * dst.x = dst.w = 1.0
2212 * dst.y = (src0.x > 0) ? src0.x
2213 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
2214 * where src.w is clamped at +- 128
2215 */
2216 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
2217 {
2218 glsl_src_param_t src0_param;
2219 glsl_src_param_t src1_param;
2220 glsl_src_param_t src3_param;
2221 char dst_mask[6];
2222
2223 shader_glsl_append_dst(ins->ctx->buffer, ins);
2224 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2225
2226 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2227 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
2228 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
2229
2230 /* The sdk specifies the instruction like this
2231 * dst.x = 1.0;
2232 * if(src.x > 0.0) dst.y = src.x
2233 * else dst.y = 0.0.
2234 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
2235 * else dst.z = 0.0;
2236 * dst.w = 1.0;
2237 *
2238 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
2239 * dst.x = 1.0 ... No further explanation needed
2240 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
2241 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
2242 * dst.w = 1.0. ... Nothing fancy.
2243 *
2244 * So we still have one conditional in there. So do this:
2245 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
2246 *
2247 * step(0.0, x) will return 1 if src.x > 0.0, and 0 otherwise. So if y is 0 we get pow(0.0 * 1.0, power),
2248 * which sets dst.z to 0. If y > 0, but x = 0.0, we get pow(y * 0.0, power), which results in 0 too.
2249 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
2250 */
2251 shader_addline(ins->ctx->buffer,
2252 "vec4(1.0, max(%s, 0.0), pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
2253 src0_param.param_str, src1_param.param_str, src0_param.param_str, src3_param.param_str, dst_mask);
2254 }
2255
2256 /** Process the WINED3DSIO_DST instruction in GLSL:
2257 * dst.x = 1.0
2258 * dst.y = src0.x * src0.y
2259 * dst.z = src0.z
2260 * dst.w = src1.w
2261 */
2262 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
2263 {
2264 glsl_src_param_t src0y_param;
2265 glsl_src_param_t src0z_param;
2266 glsl_src_param_t src1y_param;
2267 glsl_src_param_t src1w_param;
2268 char dst_mask[6];
2269
2270 shader_glsl_append_dst(ins->ctx->buffer, ins);
2271 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2272
2273 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
2274 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
2275 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
2276 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
2277
2278 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
2279 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
2280 }
2281
2282 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
2283 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
2284 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
2285 *
2286 * dst.x = cos(src0.?)
2287 * dst.y = sin(src0.?)
2288 * dst.z = dst.z
2289 * dst.w = dst.w
2290 */
2291 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
2292 {
2293 glsl_src_param_t src0_param;
2294 DWORD write_mask;
2295
2296 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2297 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2298
2299 switch (write_mask) {
2300 case WINED3DSP_WRITEMASK_0:
2301 shader_addline(ins->ctx->buffer, "cos(%s));\n", src0_param.param_str);
2302 break;
2303
2304 case WINED3DSP_WRITEMASK_1:
2305 shader_addline(ins->ctx->buffer, "sin(%s));\n", src0_param.param_str);
2306 break;
2307
2308 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
2309 shader_addline(ins->ctx->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
2310 break;
2311
2312 default:
2313 ERR("Write mask should be .x, .y or .xy\n");
2314 break;
2315 }
2316 }
2317
2318 /** Process the WINED3DSIO_LOOP instruction in GLSL:
2319 * Start a for() loop where src1.y is the initial value of aL,
2320 * increment aL by src1.z for a total of src1.x iterations.
2321 * Need to use a temporary variable for this operation.
2322 */
2323 /* FIXME: I don't think nested loops will work correctly this way. */
2324 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
2325 {
2326 glsl_src_param_t src1_param;
2327 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
2328 const DWORD *control_values = NULL;
2329 const local_constant *constant;
2330
2331 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
2332
2333 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
2334 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
2335 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
2336 * addressing.
2337 */
2338 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
2339 {
2340 LIST_FOR_EACH_ENTRY(constant, &shader->baseShader.constantsI, local_constant, entry) {
2341 if (constant->idx == ins->src[1].reg.idx)
2342 {
2343 control_values = constant->value;
2344 break;
2345 }
2346 }
2347 }
2348
2349 if(control_values) {
2350 if(control_values[2] > 0) {
2351 shader_addline(ins->ctx->buffer, "for (aL%u = %d; aL%u < (%d * %d + %d); aL%u += %d) {\n",
2352 shader->baseShader.cur_loop_depth, control_values[1],
2353 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
2354 shader->baseShader.cur_loop_depth, control_values[2]);
2355 } else if(control_values[2] == 0) {
2356 shader_addline(ins->ctx->buffer, "for (aL%u = %d, tmpInt%u = 0; tmpInt%u < %d; tmpInt%u++) {\n",
2357 shader->baseShader.cur_loop_depth, control_values[1], shader->baseShader.cur_loop_depth,
2358 shader->baseShader.cur_loop_depth, control_values[0],
2359 shader->baseShader.cur_loop_depth);
2360 } else {
2361 shader_addline(ins->ctx->buffer, "for (aL%u = %d; aL%u > (%d * %d + %d); aL%u += %d) {\n",
2362 shader->baseShader.cur_loop_depth, control_values[1],
2363 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
2364 shader->baseShader.cur_loop_depth, control_values[2]);
2365 }
2366 } else {
2367 shader_addline(ins->ctx->buffer,
2368 "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
2369 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno,
2370 src1_param.reg_name, shader->baseShader.cur_loop_depth, src1_param.reg_name,
2371 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno, src1_param.reg_name);
2372 }
2373
2374 shader->baseShader.cur_loop_depth++;
2375 shader->baseShader.cur_loop_regno++;
2376 }
2377
2378 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
2379 {
2380 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
2381
2382 shader_addline(ins->ctx->buffer, "}\n");
2383
2384 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
2385 {
2386 shader->baseShader.cur_loop_depth--;
2387 shader->baseShader.cur_loop_regno--;
2388 }
2389
2390 if (ins->handler_idx == WINED3DSIH_ENDREP)
2391 {
2392 shader->baseShader.cur_loop_depth--;
2393 }
2394 }
2395
2396 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
2397 {
2398 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
2399 glsl_src_param_t src0_param;
2400 const DWORD *control_values = NULL;
2401 const local_constant *constant;
2402
2403 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
2404 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
2405 {
2406 LIST_FOR_EACH_ENTRY(constant, &shader->baseShader.constantsI, local_constant, entry)
2407 {
2408 if (constant->idx == ins->src[0].reg.idx)
2409 {
2410 control_values = constant->value;
2411 break;
2412 }
2413 }
2414 }
2415
2416 if(control_values) {
2417 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
2418 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_depth,
2419 control_values[0], shader->baseShader.cur_loop_depth);
2420 } else {
2421 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2422 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2423 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_depth,
2424 src0_param.param_str, shader->baseShader.cur_loop_depth);
2425 }
2426 shader->baseShader.cur_loop_depth++;
2427 }
2428
2429 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
2430 {
2431 glsl_src_param_t src0_param;
2432
2433 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2434 shader_addline(ins->ctx->buffer, "if (%s) {\n", src0_param.param_str);
2435 }
2436
2437 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
2438 {
2439 glsl_src_param_t src0_param;
2440 glsl_src_param_t src1_param;
2441
2442 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2443 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2444
2445 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
2446 src0_param.param_str, shader_get_comp_op(ins->flags), src1_param.param_str);
2447 }
2448
2449 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
2450 {
2451 shader_addline(ins->ctx->buffer, "} else {\n");
2452 }
2453
2454 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
2455 {
2456 shader_addline(ins->ctx->buffer, "break;\n");
2457 }
2458
2459 /* FIXME: According to MSDN the compare is done per component. */
2460 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
2461 {
2462 glsl_src_param_t src0_param;
2463 glsl_src_param_t src1_param;
2464
2465 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2466 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2467
2468 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
2469 src0_param.param_str, shader_get_comp_op(ins->flags), src1_param.param_str);
2470 }
2471
2472 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
2473 {
2474 shader_addline(ins->ctx->buffer, "}\n");
2475 shader_addline(ins->ctx->buffer, "void subroutine%u () {\n", ins->src[0].reg.idx);
2476 }
2477
2478 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
2479 {
2480 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx);
2481 }
2482
2483 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
2484 {
2485 glsl_src_param_t src1_param;
2486
2487 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2488 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n", src1_param.param_str, ins->src[0].reg.idx);
2489 }
2490
2491 /*********************************************
2492 * Pixel Shader Specific Code begins here
2493 ********************************************/
2494 static void pshader_glsl_tex(const struct wined3d_shader_instruction *ins)
2495 {
2496 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
2497 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2498 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2499 ins->ctx->reg_maps->shader_version.minor);
2500 glsl_sample_function_t sample_function;
2501 DWORD sample_flags = 0;
2502 WINED3DSAMPLER_TEXTURE_TYPE sampler_type;
2503 DWORD sampler_idx;
2504 DWORD mask = 0, swizzle;
2505
2506 /* 1.0-1.4: Use destination register as sampler source.
2507 * 2.0+: Use provided sampler source. */
2508 if (shader_version < WINED3D_SHADER_VERSION(2,0)) sampler_idx = ins->dst[0].reg.idx;
2509 else sampler_idx = ins->src[1].reg.idx;
2510 sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
2511
2512 if (shader_version < WINED3D_SHADER_VERSION(1,4))
2513 {
2514 DWORD flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2515
2516 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
2517 if (flags & WINED3DTTFF_PROJECTED && sampler_type != WINED3DSTT_CUBE) {
2518 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
2519 switch (flags & ~WINED3DTTFF_PROJECTED) {
2520 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2521 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
2522 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
2523 case WINED3DTTFF_COUNT4:
2524 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
2525 }
2526 }
2527 }
2528 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
2529 {
2530 DWORD src_mod = ins->src[0].modifiers;
2531
2532 if (src_mod == WINED3DSPSM_DZ) {
2533 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
2534 mask = WINED3DSP_WRITEMASK_2;
2535 } else if (src_mod == WINED3DSPSM_DW) {
2536 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
2537 mask = WINED3DSP_WRITEMASK_3;
2538 }
2539 } else {
2540 if (ins->flags & WINED3DSI_TEXLD_PROJECT)
2541 {
2542 /* ps 2.0 texldp instruction always divides by the fourth component. */
2543 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
2544 mask = WINED3DSP_WRITEMASK_3;
2545 }
2546 }
2547
2548 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2549 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2550 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
2551 }
2552
2553 shader_glsl_get_sample_function(sampler_type, sample_flags, &sample_function);
2554 mask |= sample_function.coord_mask;
2555
2556 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
2557 else swizzle = ins->src[1].swizzle;
2558
2559 /* 1.0-1.3: Use destination register as coordinate source.
2560 1.4+: Use provided coordinate source register. */
2561 if (shader_version < WINED3D_SHADER_VERSION(1,4))
2562 {
2563 char coord_mask[6];
2564 shader_glsl_write_mask_to_str(mask, coord_mask);
2565 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
2566 "T%u%s", sampler_idx, coord_mask);
2567 } else {
2568 glsl_src_param_t coord_param;
2569 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
2570 if (ins->flags & WINED3DSI_TEXLD_BIAS)
2571 {
2572 glsl_src_param_t bias;
2573 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
2574 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
2575 "%s", coord_param.param_str);
2576 } else {
2577 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
2578 "%s", coord_param.param_str);
2579 }
2580 }
2581 }
2582
2583 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
2584 {
2585 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
2586 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2587 const WineD3D_GL_Info* gl_info = &deviceImpl->adapter->gl_info;
2588 glsl_sample_function_t sample_function;
2589 glsl_src_param_t coord_param, dx_param, dy_param;
2590 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
2591 DWORD sampler_type;
2592 DWORD sampler_idx;
2593 DWORD swizzle = ins->src[1].swizzle;
2594
2595 if(!GL_SUPPORT(ARB_SHADER_TEXTURE_LOD)) {
2596 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
2597 return pshader_glsl_tex(ins);
2598 }
2599
2600 sampler_idx = ins->src[1].reg.idx;
2601 sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
2602 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2603 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2604 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
2605 }
2606
2607 shader_glsl_get_sample_function(sampler_type, sample_flags, &sample_function);
2608 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
2609 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
2610 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
2611
2612 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
2613 "%s", coord_param.param_str);
2614 }
2615
2616 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
2617 {
2618 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
2619 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2620 glsl_sample_function_t sample_function;
2621 glsl_src_param_t coord_param, lod_param;
2622 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
2623 DWORD sampler_type;
2624 DWORD sampler_idx;
2625 DWORD swizzle = ins->src[1].swizzle;
2626
2627 sampler_idx = ins->src[1].reg.idx;
2628 sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
2629 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2630 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2631 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
2632 }
2633 shader_glsl_get_sample_function(sampler_type, sample_flags, &sample_function);
2634 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
2635
2636 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
2637
2638 if (shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type))
2639 {
2640 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
2641 * However, they seem to work just fine in fragment shaders as well. */
2642 WARN("Using %s in fragment shader.\n", sample_function.name);
2643 }
2644 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
2645 "%s", coord_param.param_str);
2646 }
2647
2648 static void pshader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
2649 {
2650 /* FIXME: Make this work for more than just 2D textures */
2651 SHADER_BUFFER *buffer = ins->ctx->buffer;
2652 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2653
2654 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
2655 {
2656 char dst_mask[6];
2657
2658 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2659 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
2660 ins->dst[0].reg.idx, dst_mask);
2661 } else {
2662 DWORD reg = ins->src[0].reg.idx;
2663 DWORD src_mod = ins->src[0].modifiers;
2664 char dst_swizzle[6];
2665
2666 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
2667
2668 if (src_mod == WINED3DSPSM_DZ) {
2669 glsl_src_param_t div_param;
2670 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2671 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
2672
2673 if (mask_size > 1) {
2674 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2675 } else {
2676 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2677 }
2678 } else if (src_mod == WINED3DSPSM_DW) {
2679 glsl_src_param_t div_param;
2680 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2681 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
2682
2683 if (mask_size > 1) {
2684 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2685 } else {
2686 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2687 }
2688 } else {
2689 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
2690 }
2691 }
2692 }
2693
2694 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
2695 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2696 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2697 static void pshader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
2698 {
2699 glsl_src_param_t src0_param;
2700 glsl_sample_function_t sample_function;
2701 DWORD sampler_idx = ins->dst[0].reg.idx;
2702 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2703 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
2704 UINT mask_size;
2705
2706 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2707
2708 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
2709 * scalar, and projected sampling would require 4.
2710 *
2711 * It is a dependent read - not valid with conditional NP2 textures
2712 */
2713 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2714 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
2715
2716 switch(mask_size)
2717 {
2718 case 1:
2719 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
2720 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
2721 break;
2722
2723 case 2:
2724 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
2725 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
2726 break;
2727
2728 case 3:
2729 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
2730 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
2731 break;
2732
2733 default:
2734 FIXME("Unexpected mask size %u\n", mask_size);
2735 break;
2736 }
2737 }
2738
2739 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2740 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2741 static void pshader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
2742 {
2743 glsl_src_param_t src0_param;
2744 DWORD dstreg = ins->dst[0].reg.idx;
2745 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2746 DWORD dst_mask;
2747 unsigned int mask_size;
2748
2749 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2750 mask_size = shader_glsl_get_write_mask_size(dst_mask);
2751 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2752
2753 if (mask_size > 1) {
2754 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
2755 } else {
2756 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
2757 }
2758 }
2759
2760 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2761 * Calculate the depth as dst.x / dst.y */
2762 static void pshader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
2763 {
2764 glsl_dst_param_t dst_param;
2765
2766 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2767
2768 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2769 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2770 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2771 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2772 * >= 1.0 or < 0.0
2773 */
2774 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
2775 dst_param.reg_name, dst_param.reg_name);
2776 }
2777
2778 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2779 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2780 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2781 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2782 */
2783 static void pshader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
2784 {
2785 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2786 DWORD dstreg = ins->dst[0].reg.idx;
2787 glsl_src_param_t src0_param;
2788
2789 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2790
2791 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
2792 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2793 }
2794
2795 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2796 * Calculate the 1st of a 2-row matrix multiplication. */
2797 static void pshader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
2798 {
2799 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2800 DWORD reg = ins->dst[0].reg.idx;
2801 SHADER_BUFFER *buffer = ins->ctx->buffer;
2802 glsl_src_param_t src0_param;
2803
2804 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2805 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2806 }
2807
2808 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2809 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2810 static void pshader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
2811 {
2812 IWineD3DPixelShaderImpl *shader = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
2813 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2814 DWORD reg = ins->dst[0].reg.idx;
2815 SHADER_BUFFER *buffer = ins->ctx->buffer;
2816 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2817 glsl_src_param_t src0_param;
2818
2819 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2820 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
2821 current_state->texcoord_w[current_state->current_row++] = reg;
2822 }
2823
2824 static void pshader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
2825 {
2826 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2827 DWORD reg = ins->dst[0].reg.idx;
2828 SHADER_BUFFER *buffer = ins->ctx->buffer;
2829 glsl_src_param_t src0_param;
2830 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[reg];
2831 glsl_sample_function_t sample_function;
2832
2833 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2834 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2835
2836 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2837
2838 /* Sample the texture using the calculated coordinates */
2839 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
2840 }
2841
2842 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2843 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2844 static void pshader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
2845 {
2846 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2847 glsl_src_param_t src0_param;
2848 DWORD reg = ins->dst[0].reg.idx;
2849 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
2850 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2851 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[reg];
2852 glsl_sample_function_t sample_function;
2853
2854 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2855 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2856
2857 /* Dependent read, not valid with conditional NP2 */
2858 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2859
2860 /* Sample the texture using the calculated coordinates */
2861 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
2862
2863 current_state->current_row = 0;
2864 }
2865
2866 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2867 * Perform the 3rd row of a 3x3 matrix multiply */
2868 static void pshader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
2869 {
2870 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2871 glsl_src_param_t src0_param;
2872 char dst_mask[6];
2873 DWORD reg = ins->dst[0].reg.idx;
2874 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
2875 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2876
2877 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2878
2879 shader_glsl_append_dst(ins->ctx->buffer, ins);
2880 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2881 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
2882
2883 current_state->current_row = 0;
2884 }
2885
2886 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
2887 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2888 static void pshader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
2889 {
2890 IWineD3DPixelShaderImpl *shader = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
2891 DWORD reg = ins->dst[0].reg.idx;
2892 glsl_src_param_t src0_param;
2893 glsl_src_param_t src1_param;
2894 SHADER_BUFFER *buffer = ins->ctx->buffer;
2895 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2896 WINED3DSAMPLER_TEXTURE_TYPE stype = ins->ctx->reg_maps->sampler_type[reg];
2897 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2898 glsl_sample_function_t sample_function;
2899
2900 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2901 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2902
2903 /* Perform the last matrix multiply operation */
2904 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2905 /* Reflection calculation */
2906 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
2907
2908 /* Dependent read, not valid with conditional NP2 */
2909 shader_glsl_get_sample_function(stype, 0, &sample_function);
2910
2911 /* Sample the texture */
2912 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
2913
2914 current_state->current_row = 0;
2915 }
2916
2917 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
2918 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2919 static void pshader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
2920 {
2921 IWineD3DPixelShaderImpl *shader = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
2922 DWORD reg = ins->dst[0].reg.idx;
2923 SHADER_BUFFER *buffer = ins->ctx->buffer;
2924 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2925 glsl_src_param_t src0_param;
2926 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2927 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[reg];
2928 glsl_sample_function_t sample_function;
2929
2930 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2931
2932 /* Perform the last matrix multiply operation */
2933 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
2934
2935 /* Construct the eye-ray vector from w coordinates */
2936 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
2937 current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
2938 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
2939
2940 /* Dependent read, not valid with conditional NP2 */
2941 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2942
2943 /* Sample the texture using the calculated coordinates */
2944 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
2945
2946 current_state->current_row = 0;
2947 }
2948
2949 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
2950 * Apply a fake bump map transform.
2951 * texbem is pshader <= 1.3 only, this saves a few version checks
2952 */
2953 static void pshader_glsl_texbem(const struct wined3d_shader_instruction *ins)
2954 {
2955 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)ins->ctx->shader;
2956 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2957 glsl_sample_function_t sample_function;
2958 glsl_src_param_t coord_param;
2959 WINED3DSAMPLER_TEXTURE_TYPE sampler_type;
2960 DWORD sampler_idx;
2961 DWORD mask;
2962 DWORD flags;
2963 char coord_mask[6];
2964
2965 sampler_idx = ins->dst[0].reg.idx;
2966 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2967
2968 sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
2969 /* Dependent read, not valid with conditional NP2 */
2970 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
2971 mask = sample_function.coord_mask;
2972
2973 shader_glsl_write_mask_to_str(mask, coord_mask);
2974
2975 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2976 * so we can't let the GL handle this.
2977 */
2978 if (flags & WINED3DTTFF_PROJECTED) {
2979 DWORD div_mask=0;
2980 char coord_div_mask[3];
2981 switch (flags & ~WINED3DTTFF_PROJECTED) {
2982 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2983 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
2984 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
2985 case WINED3DTTFF_COUNT4:
2986 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
2987 }
2988 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
2989 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
2990 }
2991
2992 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
2993
2994 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
2995 "T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
2996 coord_param.param_str, coord_mask);
2997
2998 if (ins->handler_idx == WINED3DSIH_TEXBEML)
2999 {
3000 glsl_src_param_t luminance_param;
3001 glsl_dst_param_t dst_param;
3002
3003 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
3004 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3005
3006 shader_addline(ins->ctx->buffer, "%s%s *= (%s * luminancescale%d + luminanceoffset%d);\n",
3007 dst_param.reg_name, dst_param.mask_str,
3008 luminance_param.param_str, sampler_idx, sampler_idx);
3009 }
3010 }
3011
3012 static void pshader_glsl_bem(const struct wined3d_shader_instruction *ins)
3013 {
3014 glsl_src_param_t src0_param, src1_param;
3015 DWORD sampler_idx = ins->dst[0].reg.idx;
3016
3017 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
3018 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
3019
3020 shader_glsl_append_dst(ins->ctx->buffer, ins);
3021 shader_addline(ins->ctx->buffer, "%s + bumpenvmat%d * %s);\n",
3022 src0_param.param_str, sampler_idx, src1_param.param_str);
3023 }
3024
3025 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
3026 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
3027 static void pshader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
3028 {
3029 glsl_src_param_t src0_param;
3030 DWORD sampler_idx = ins->dst[0].reg.idx;
3031 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3032 glsl_sample_function_t sample_function;
3033
3034 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
3035
3036 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
3037 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3038 "%s.wx", src0_param.reg_name);
3039 }
3040
3041 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
3042 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
3043 static void pshader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
3044 {
3045 glsl_src_param_t src0_param;
3046 DWORD sampler_idx = ins->dst[0].reg.idx;
3047 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3048 glsl_sample_function_t sample_function;
3049
3050 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
3051
3052 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
3053 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3054 "%s.yz", src0_param.reg_name);
3055 }
3056
3057 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
3058 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
3059 static void pshader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
3060 {
3061 glsl_src_param_t src0_param;
3062 DWORD sampler_idx = ins->dst[0].reg.idx;
3063 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3064 glsl_sample_function_t sample_function;
3065
3066 /* Dependent read, not valid with conditional NP2 */
3067 shader_glsl_get_sample_function(sampler_type, 0, &sample_function);
3068 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
3069
3070 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3071 "%s", src0_param.param_str);
3072 }
3073
3074 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
3075 * If any of the first 3 components are < 0, discard this pixel */
3076 static void pshader_glsl_texkill(const struct wined3d_shader_instruction *ins)
3077 {
3078 glsl_dst_param_t dst_param;
3079
3080 /* The argument is a destination parameter, and no writemasks are allowed */
3081 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3082 if (ins->ctx->reg_maps->shader_version.major >= 2)
3083 {
3084 /* 2.0 shaders compare all 4 components in texkill */
3085 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
3086 } else {
3087 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
3088 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
3089 * 4 components are defined, only the first 3 are used
3090 */
3091 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
3092 }
3093 }
3094
3095 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
3096 * dst = dot2(src0, src1) + src2 */
3097 static void pshader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
3098 {
3099 glsl_src_param_t src0_param;
3100 glsl_src_param_t src1_param;
3101 glsl_src_param_t src2_param;
3102 DWORD write_mask;
3103 unsigned int mask_size;
3104
3105 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3106 mask_size = shader_glsl_get_write_mask_size(write_mask);
3107
3108 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
3109 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
3110 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
3111
3112 if (mask_size > 1) {
3113 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
3114 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
3115 } else {
3116 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
3117 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3118 }
3119 }
3120
3121 static void pshader_glsl_input_pack(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer,
3122 const struct wined3d_shader_semantic *semantics_in, const struct shader_reg_maps *reg_maps,
3123 enum vertexprocessing_mode vertexprocessing)
3124 {
3125 unsigned int i;
3126 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
3127
3128 for (i = 0; i < MAX_REG_INPUT; ++i)
3129 {
3130 DWORD usage, usage_idx;
3131 char reg_mask[6];
3132
3133 /* Unused */
3134 if (!reg_maps->packed_input[i]) continue;
3135
3136 usage = semantics_in[i].usage;
3137 usage_idx = semantics_in[i].usage_idx;
3138 shader_glsl_get_write_mask(&semantics_in[i].reg, reg_mask);
3139
3140 switch (usage)
3141 {
3142 case WINED3DDECLUSAGE_TEXCOORD:
3143 if (usage_idx < 8 && vertexprocessing == pretransformed)
3144 shader_addline(buffer, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
3145 This->input_reg_map[i], reg_mask, usage_idx, reg_mask);
3146 else
3147 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3148 This->input_reg_map[i], reg_mask, reg_mask);
3149 break;
3150
3151 case WINED3DDECLUSAGE_COLOR:
3152 if (usage_idx == 0)
3153 shader_addline(buffer, "IN[%u]%s = vec4(gl_Color)%s;\n",
3154 This->input_reg_map[i], reg_mask, reg_mask);
3155 else if (usage_idx == 1)
3156 shader_addline(buffer, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
3157 This->input_reg_map[i], reg_mask, reg_mask);
3158 else
3159 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3160 This->input_reg_map[i], reg_mask, reg_mask);
3161 break;
3162
3163 default:
3164 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3165 This->input_reg_map[i], reg_mask, reg_mask);
3166 break;
3167 }
3168 }
3169 }
3170
3171 /*********************************************
3172 * Vertex Shader Specific Code begins here
3173 ********************************************/
3174
3175 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry) {
3176 glsl_program_key_t *key;
3177
3178 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
3179 key->vshader = entry->vshader;
3180 key->pshader = entry->pshader;
3181 key->vs_args = entry->vs_args;
3182 key->ps_args = entry->ps_args;
3183
3184 hash_table_put(priv->glsl_program_lookup, key, entry);
3185 }
3186
3187 static struct glsl_shader_prog_link *get_glsl_program_entry(struct shader_glsl_priv *priv,
3188 IWineD3DVertexShader *vshader, IWineD3DPixelShader *pshader, struct vs_compile_args *vs_args,
3189 struct ps_compile_args *ps_args) {
3190 glsl_program_key_t key;
3191
3192 key.vshader = vshader;
3193 key.pshader = pshader;
3194 key.vs_args = *vs_args;
3195 key.ps_args = *ps_args;
3196
3197 return hash_table_get(priv->glsl_program_lookup, &key);
3198 }
3199
3200 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const WineD3D_GL_Info *gl_info,
3201 struct glsl_shader_prog_link *entry)
3202 {
3203 glsl_program_key_t *key;
3204
3205 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
3206 key->vshader = entry->vshader;
3207 key->pshader = entry->pshader;
3208 key->vs_args = entry->vs_args;
3209 key->ps_args = entry->ps_args;
3210 hash_table_remove(priv->glsl_program_lookup, key);
3211
3212 GL_EXTCALL(glDeleteObjectARB(entry->programId));
3213 if (entry->vshader) list_remove(&entry->vshader_entry);
3214 if (entry->pshader) list_remove(&entry->pshader_entry);
3215 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
3216 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
3217 HeapFree(GetProcessHeap(), 0, entry);
3218 }
3219
3220 static void handle_ps3_input(SHADER_BUFFER *buffer, const WineD3D_GL_Info *gl_info, const DWORD *map,
3221 const struct wined3d_shader_semantic *semantics_in, const struct shader_reg_maps *reg_maps_in,
3222 const struct wined3d_shader_semantic *semantics_out, const struct shader_reg_maps *reg_maps_out)
3223 {
3224 unsigned int i, j;
3225 DWORD usage, usage_idx, usage_out, usage_idx_out;
3226 DWORD *set;
3227 DWORD in_idx;
3228 DWORD in_count = GL_LIMITS(glsl_varyings) / 4;
3229 char reg_mask[6], reg_mask_out[6];
3230 char destination[50];
3231
3232 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
3233
3234 if (!semantics_out) {
3235 /* Save gl_FrontColor & gl_FrontSecondaryColor before overwriting them. */
3236 shader_addline(buffer, "vec4 front_color = gl_FrontColor;\n");
3237 shader_addline(buffer, "vec4 front_secondary_color = gl_FrontSecondaryColor;\n");
3238 }
3239
3240 for(i = 0; i < MAX_REG_INPUT; i++) {
3241 if (!reg_maps_in->packed_input[i]) continue;
3242
3243 in_idx = map[i];
3244 if (in_idx >= (in_count + 2)) {
3245 FIXME("More input varyings declared than supported, expect issues\n");
3246 continue;
3247 }
3248 else if (map[i] == ~0U)
3249 {
3250 /* Declared, but not read register */
3251 continue;
3252 }
3253
3254 if (in_idx == in_count) {
3255 sprintf(destination, "gl_FrontColor");
3256 } else if (in_idx == in_count + 1) {
3257 sprintf(destination, "gl_FrontSecondaryColor");
3258 } else {
3259 sprintf(destination, "IN[%u]", in_idx);
3260 }
3261
3262 usage = semantics_in[i].usage;
3263 usage_idx = semantics_in[i].usage_idx;
3264 set[map[i]] = shader_glsl_get_write_mask(&semantics_in[i].reg, reg_mask);
3265
3266 if(!semantics_out) {
3267 switch(usage) {
3268 case WINED3DDECLUSAGE_COLOR:
3269 if (usage_idx == 0)
3270 shader_addline(buffer, "%s%s = front_color%s;\n",
3271 destination, reg_mask, reg_mask);
3272 else if (usage_idx == 1)
3273 shader_addline(buffer, "%s%s = front_secondary_color%s;\n",
3274 destination, reg_mask, reg_mask);
3275 else
3276 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3277 destination, reg_mask, reg_mask);
3278 break;
3279
3280 case WINED3DDECLUSAGE_TEXCOORD:
3281 if (usage_idx < 8) {
3282 shader_addline(buffer, "%s%s = gl_TexCoord[%u]%s;\n",
3283 destination, reg_mask, usage_idx, reg_mask);
3284 } else {
3285 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3286 destination, reg_mask, reg_mask);
3287 }
3288 break;
3289
3290 case WINED3DDECLUSAGE_FOG:
3291 shader_addline(buffer, "%s%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
3292 destination, reg_mask, reg_mask);
3293 break;
3294
3295 default:
3296 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3297 destination, reg_mask, reg_mask);
3298 }
3299 } else {
3300 BOOL found = FALSE;
3301 for(j = 0; j < MAX_REG_OUTPUT; j++) {
3302 if (!reg_maps_out->packed_output[j]) continue;
3303
3304 usage_out = semantics_out[j].usage;
3305 usage_idx_out = semantics_out[j].usage_idx;
3306 shader_glsl_get_write_mask(&semantics_out[j].reg, reg_mask_out);
3307
3308 if(usage == usage_out &&
3309 usage_idx == usage_idx_out) {
3310 shader_addline(buffer, "%s%s = OUT[%u]%s;\n",
3311 destination, reg_mask, j, reg_mask);
3312 found = TRUE;
3313 }
3314 }
3315 if(!found) {
3316 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3317 destination, reg_mask, reg_mask);
3318 }
3319 }
3320 }
3321
3322 /* This is solely to make the compiler / linker happy and avoid warning about undefined
3323 * varyings. It shouldn't result in any real code executed on the GPU, since all read
3324 * input varyings are assigned above, if the optimizer works properly.
3325 */
3326 for(i = 0; i < in_count + 2; i++) {
3327 if(set[i] != WINED3DSP_WRITEMASK_ALL) {
3328 unsigned int size = 0;
3329 memset(reg_mask, 0, sizeof(reg_mask));
3330 if(!(set[i] & WINED3DSP_WRITEMASK_0)) {
3331 reg_mask[size] = 'x';
3332 size++;
3333 }
3334 if(!(set[i] & WINED3DSP_WRITEMASK_1)) {
3335 reg_mask[size] = 'y';
3336 size++;
3337 }
3338 if(!(set[i] & WINED3DSP_WRITEMASK_2)) {
3339 reg_mask[size] = 'z';
3340 size++;
3341 }
3342 if(!(set[i] & WINED3DSP_WRITEMASK_3)) {
3343 reg_mask[size] = 'w';
3344 size++;
3345 }
3346
3347 if (i == in_count) {
3348 sprintf(destination, "gl_FrontColor");
3349 } else if (i == in_count + 1) {
3350 sprintf(destination, "gl_FrontSecondaryColor");
3351 } else {
3352 sprintf(destination, "IN[%u]", i);
3353 }
3354
3355 if (size == 1) {
3356 shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
3357 } else {
3358 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
3359 }
3360 }
3361 }
3362
3363 HeapFree(GetProcessHeap(), 0, set);
3364 }
3365
3366 static GLhandleARB generate_param_reorder_function(IWineD3DVertexShader *vertexshader,
3367 IWineD3DPixelShader *pixelshader, const WineD3D_GL_Info *gl_info)
3368 {
3369 GLhandleARB ret = 0;
3370 IWineD3DVertexShaderImpl *vs = (IWineD3DVertexShaderImpl *) vertexshader;
3371 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pixelshader;
3372 IWineD3DDeviceImpl *device;
3373 DWORD vs_major = vs->baseShader.reg_maps.shader_version.major;
3374 DWORD ps_major = ps ? ps->baseShader.reg_maps.shader_version.major : 0;
3375 unsigned int i;
3376 SHADER_BUFFER buffer;
3377 DWORD usage, usage_idx, writemask;
3378 char reg_mask[6];
3379 const struct wined3d_shader_semantic *semantics_out;
3380
3381 shader_buffer_init(&buffer);
3382
3383 shader_addline(&buffer, "#version 120\n");
3384
3385 if(vs_major < 3 && ps_major < 3) {
3386 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
3387 * Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
3388 */
3389 device = (IWineD3DDeviceImpl *) vs->baseShader.device;
3390 if((GLINFO_LOCATION).set_texcoord_w && ps_major == 0 && vs_major > 0 &&
3391 !device->frag_pipe->ffp_proj_control) {
3392 shader_addline(&buffer, "void order_ps_input() {\n");
3393 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
3394 if(vs->baseShader.reg_maps.texcoord_mask[i] != 0 &&
3395 vs->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
3396 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", i);
3397 }
3398 }
3399 shader_addline(&buffer, "}\n");
3400 } else {
3401 shader_addline(&buffer, "void order_ps_input() { /* do nothing */ }\n");
3402 }
3403 } else if(ps_major < 3 && vs_major >= 3) {
3404 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
3405 semantics_out = vs->semantics_out;
3406
3407 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3408 for(i = 0; i < MAX_REG_OUTPUT; i++) {
3409 if (!vs->baseShader.reg_maps.packed_output[i]) continue;
3410
3411 usage = semantics_out[i].usage;
3412 usage_idx = semantics_out[i].usage_idx;
3413 writemask = shader_glsl_get_write_mask(&semantics_out[i].reg, reg_mask);
3414
3415 switch(usage) {
3416 case WINED3DDECLUSAGE_COLOR:
3417 if (usage_idx == 0)
3418 shader_addline(&buffer, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3419 else if (usage_idx == 1)
3420 shader_addline(&buffer, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3421 break;
3422
3423 case WINED3DDECLUSAGE_POSITION:
3424 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3425 break;
3426
3427 case WINED3DDECLUSAGE_TEXCOORD:
3428 if (usage_idx < 8) {
3429 if(!(GLINFO_LOCATION).set_texcoord_w || ps_major > 0) writemask |= WINED3DSP_WRITEMASK_3;
3430
3431 shader_addline(&buffer, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3432 usage_idx, reg_mask, i, reg_mask);
3433 if(!(writemask & WINED3DSP_WRITEMASK_3)) {
3434 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", usage_idx);
3435 }
3436 }
3437 break;
3438
3439 case WINED3DDECLUSAGE_PSIZE:
3440 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3441 break;
3442
3443 case WINED3DDECLUSAGE_FOG:
3444 shader_addline(&buffer, "gl_FogFragCoord = OUT[%u].%c;\n", i, reg_mask[1]);
3445 break;
3446
3447 default:
3448 break;
3449 }
3450 }
3451 shader_addline(&buffer, "}\n");
3452
3453 } else if(ps_major >= 3 && vs_major >= 3) {
3454 semantics_out = vs->semantics_out;
3455
3456 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3457 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3458 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3459
3460 /* First, sort out position and point size. Those are not passed to the pixel shader */
3461 for(i = 0; i < MAX_REG_OUTPUT; i++) {
3462 if (!vs->baseShader.reg_maps.packed_output[i]) continue;
3463
3464 usage = semantics_out[i].usage;
3465 usage_idx = semantics_out[i].usage_idx;
3466 shader_glsl_get_write_mask(&semantics_out[i].reg, reg_mask);
3467
3468 switch(usage) {
3469 case WINED3DDECLUSAGE_POSITION:
3470 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3471 break;
3472
3473 case WINED3DDECLUSAGE_PSIZE:
3474 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3475 break;
3476
3477 default:
3478 break;
3479 }
3480 }
3481
3482 /* Then, fix the pixel shader input */
3483 handle_ps3_input(&buffer, gl_info, ps->input_reg_map,
3484 ps->semantics_in, &ps->baseShader.reg_maps, semantics_out, &vs->baseShader.reg_maps);
3485
3486 shader_addline(&buffer, "}\n");
3487 } else if(ps_major >= 3 && vs_major < 3) {
3488 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3489 shader_addline(&buffer, "void order_ps_input() {\n");
3490 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
3491 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
3492 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
3493 */
3494 handle_ps3_input(&buffer, gl_info, ps->input_reg_map, ps->semantics_in, &ps->baseShader.reg_maps, NULL, NULL);
3495 shader_addline(&buffer, "}\n");
3496 } else {
3497 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major, ps_major);
3498 }
3499
3500 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3501 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3502 GL_EXTCALL(glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL));
3503 checkGLcall("glShaderSourceARB(ret, 1, &buffer.buffer, NULL)");
3504 GL_EXTCALL(glCompileShaderARB(ret));
3505 checkGLcall("glCompileShaderARB(ret)");
3506
3507 shader_buffer_free(&buffer);
3508 return ret;
3509 }
3510
3511 static void hardcode_local_constants(IWineD3DBaseShaderImpl *shader, const WineD3D_GL_Info *gl_info,
3512 GLhandleARB programId, char prefix)
3513 {
3514 const local_constant *lconst;
3515 GLint tmp_loc;
3516 const float *value;
3517 char glsl_name[8];
3518
3519 LIST_FOR_EACH_ENTRY(lconst, &shader->baseShader.constantsF, local_constant, entry) {
3520 value = (const float *)lconst->value;
3521 snprintf(glsl_name, sizeof(glsl_name), "%cLC%u", prefix, lconst->idx);
3522 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3523 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, value));
3524 }
3525 checkGLcall("Hardcoding local constants\n");
3526 }
3527
3528 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
3529 * It sets the programId on the current StateBlock (because it should be called
3530 * inside of the DrawPrimitive() part of the render loop).
3531 *
3532 * If a program for the given combination does not exist, create one, and store
3533 * the program in the hash table. If it creates a program, it will link the
3534 * given objects, too.
3535 */
3536 static void set_glsl_shader_program(IWineD3DDevice *iface, BOOL use_ps, BOOL use_vs) {
3537 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3538 struct shader_glsl_priv *priv = This->shader_priv;
3539 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3540 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
3541 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
3542 struct glsl_shader_prog_link *entry = NULL;
3543 GLhandleARB programId = 0;
3544 GLhandleARB reorder_shader_id = 0;
3545 unsigned int i;
3546 char glsl_name[8];
3547 GLhandleARB vshader_id, pshader_id;
3548 struct ps_compile_args ps_compile_args;
3549 struct vs_compile_args vs_compile_args;
3550
3551 if(use_vs) {
3552 find_vs_compile_args((IWineD3DVertexShaderImpl*)This->stateBlock->vertexShader, This->stateBlock, &vs_compile_args);
3553 } else {
3554 /* FIXME: Do we really have to spend CPU cycles to generate a few zeroed bytes? */
3555 memset(&vs_compile_args, 0, sizeof(vs_compile_args));
3556 }
3557 if(use_ps) {
3558 find_ps_compile_args((IWineD3DPixelShaderImpl*)This->stateBlock->pixelShader, This->stateBlock, &ps_compile_args);
3559 } else {
3560 /* FIXME: Do we really have to spend CPU cycles to generate a few zeroed bytes? */
3561 memset(&ps_compile_args, 0, sizeof(ps_compile_args));
3562 }
3563 entry = get_glsl_program_entry(priv, vshader, pshader, &vs_compile_args, &ps_compile_args);
3564 if (entry) {
3565 priv->glsl_program = entry;
3566 return;
3567 }
3568
3569 /* If we get to this point, then no matching program exists, so we create one */
3570 programId = GL_EXTCALL(glCreateProgramObjectARB());
3571 TRACE("Created new GLSL shader program %u\n", programId);
3572
3573 /* Create the entry */
3574 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
3575 entry->programId = programId;
3576 entry->vshader = vshader;
3577 entry->pshader = pshader;
3578 entry->vs_args = vs_compile_args;
3579 entry->ps_args = ps_compile_args;
3580 entry->constant_version = 0;
3581 /* Add the hash table entry */
3582 add_glsl_program_entry(priv, entry);
3583
3584 /* Set the current program */
3585 priv->glsl_program = entry;
3586
3587 if(use_vs) {
3588 vshader_id = find_gl_vshader((IWineD3DVertexShaderImpl *) vshader, &vs_compile_args);
3589 } else {
3590 vshader_id = 0;
3591 }
3592
3593 /* Attach GLSL vshader */
3594 if (vshader_id) {
3595 const unsigned int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
3596 char tmp_name[10];
3597
3598 reorder_shader_id = generate_param_reorder_function(vshader, pshader, gl_info);
3599 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
3600 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
3601 checkGLcall("glAttachObjectARB");
3602 /* Flag the reorder function for deletion, then it will be freed automatically when the program
3603 * is destroyed
3604 */
3605 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
3606
3607 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
3608 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
3609 checkGLcall("glAttachObjectARB");
3610
3611 /* Bind vertex attributes to a corresponding index number to match
3612 * the same index numbers as ARB_vertex_programs (makes loading
3613 * vertex attributes simpler). With this method, we can use the
3614 * exact same code to load the attributes later for both ARB and
3615 * GLSL shaders.
3616 *
3617 * We have to do this here because we need to know the Program ID
3618 * in order to make the bindings work, and it has to be done prior
3619 * to linking the GLSL program. */
3620 for (i = 0; i < max_attribs; ++i) {
3621 if (((IWineD3DBaseShaderImpl*)vshader)->baseShader.reg_maps.attributes[i]) {
3622 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
3623 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
3624 }
3625 }
3626 checkGLcall("glBindAttribLocationARB");
3627
3628 list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
3629 }
3630
3631 if(use_ps) {
3632 pshader_id = find_gl_pshader((IWineD3DPixelShaderImpl *) pshader, &ps_compile_args);
3633 } else {
3634 pshader_id = 0;
3635 }
3636
3637 /* Attach GLSL pshader */
3638 if (pshader_id) {
3639 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
3640 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
3641 checkGLcall("glAttachObjectARB");
3642
3643 list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
3644 }
3645
3646 /* Link the program */
3647 TRACE("Linking GLSL shader program %u\n", programId);
3648 GL_EXTCALL(glLinkProgramARB(programId));
3649 print_glsl_info_log(&GLINFO_LOCATION, programId);
3650
3651 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
3652 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
3653 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
3654 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3655 }
3656 for (i = 0; i < MAX_CONST_I; ++i) {
3657 snprintf(glsl_name, sizeof(glsl_name), "VI[%i]", i);
3658 entry->vuniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3659 }
3660 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
3661 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
3662 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
3663 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3664 }
3665 for (i = 0; i < MAX_CONST_I; ++i) {
3666 snprintf(glsl_name, sizeof(glsl_name), "PI[%i]", i);
3667 entry->puniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3668 }
3669
3670 if(pshader) {
3671 for(i = 0; i < ((IWineD3DPixelShaderImpl*)pshader)->numbumpenvmatconsts; i++) {
3672 char name[32];
3673 sprintf(name, "bumpenvmat%d", ((IWineD3DPixelShaderImpl*)pshader)->bumpenvmatconst[i].texunit);
3674 entry->bumpenvmat_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3675 sprintf(name, "luminancescale%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3676 entry->luminancescale_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3677 sprintf(name, "luminanceoffset%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3678 entry->luminanceoffset_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3679 }
3680 }
3681
3682 if (use_ps && ps_compile_args.np2_fixup) {
3683 char name[32];
3684 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
3685 if (ps_compile_args.np2_fixup & (1 << i)) {
3686 sprintf(name, "PsamplerNP2Fixup%u", i);
3687 entry->np2Fixup_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3688 } else {
3689 entry->np2Fixup_location[i] = -1;
3690 }
3691 }
3692 }
3693
3694 entry->posFixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
3695 entry->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
3696 checkGLcall("Find glsl program uniform locations");
3697
3698 if (pshader
3699 && ((IWineD3DPixelShaderImpl *)pshader)->baseShader.reg_maps.shader_version.major >= 3
3700 && ((IWineD3DPixelShaderImpl *)pshader)->declared_in_count > GL_LIMITS(glsl_varyings) / 4)
3701 {
3702 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
3703 entry->vertex_color_clamp = GL_FALSE;
3704 } else {
3705 entry->vertex_color_clamp = GL_FIXED_ONLY_ARB;
3706 }
3707
3708 /* Set the shader to allow uniform loading on it */
3709 GL_EXTCALL(glUseProgramObjectARB(programId));
3710 checkGLcall("glUseProgramObjectARB(programId)");
3711
3712 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
3713 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
3714 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
3715 * vertex shader with fixed function pixel processing is used we make sure that the card
3716 * supports enough samplers to allow the max number of vertex samplers with all possible
3717 * fixed function fragment processing setups. So once the program is linked these samplers
3718 * won't change.
3719 */
3720 if(vshader_id) {
3721 /* Load vertex shader samplers */
3722 shader_glsl_load_vsamplers(gl_info, This->texUnitMap, programId);
3723 }
3724 if(pshader_id) {
3725 /* Load pixel shader samplers */
3726 shader_glsl_load_psamplers(gl_info, This->texUnitMap, programId);
3727 }
3728
3729 /* If the local constants do not have to be loaded with the environment constants,
3730 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
3731 * later
3732 */
3733 if(pshader && !((IWineD3DPixelShaderImpl*)pshader)->baseShader.load_local_constsF) {
3734 hardcode_local_constants((IWineD3DBaseShaderImpl *) pshader, gl_info, programId, 'P');
3735 }
3736 if(vshader && !((IWineD3DVertexShaderImpl*)vshader)->baseShader.load_local_constsF) {
3737 hardcode_local_constants((IWineD3DBaseShaderImpl *) vshader, gl_info, programId, 'V');
3738 }
3739 }
3740
3741 static GLhandleARB create_glsl_blt_shader(const WineD3D_GL_Info *gl_info, enum tex_types tex_type)
3742 {
3743 GLhandleARB program_id;
3744 GLhandleARB vshader_id, pshader_id;
3745 static const char *blt_vshader[] =
3746 {
3747 "#version 120\n"
3748 "void main(void)\n"
3749 "{\n"
3750 " gl_Position = gl_Vertex;\n"
3751 " gl_FrontColor = vec4(1.0);\n"
3752 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
3753 "}\n"
3754 };
3755
3756 static const char *blt_pshaders[tex_type_count] =
3757 {
3758 /* tex_1d */
3759 NULL,
3760 /* tex_2d */
3761 "#version 120\n"
3762 "uniform sampler2D sampler;\n"
3763 "void main(void)\n"
3764 "{\n"
3765 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
3766 "}\n",
3767 /* tex_3d */
3768 NULL,
3769 /* tex_cube */
3770 "#version 120\n"
3771 "uniform samplerCube sampler;\n"
3772 "void main(void)\n"
3773 "{\n"
3774 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
3775 "}\n",
3776 /* tex_rect */
3777 "#version 120\n"
3778 "#extension GL_ARB_texture_rectangle : enable\n"
3779 "uniform sampler2DRect sampler;\n"
3780 "void main(void)\n"
3781 "{\n"
3782 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
3783 "}\n",
3784 };
3785
3786 if (!blt_pshaders[tex_type])
3787 {
3788 FIXME("tex_type %#x not supported\n", tex_type);
3789 tex_type = tex_2d;
3790 }
3791
3792 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3793 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
3794 GL_EXTCALL(glCompileShaderARB(vshader_id));
3795
3796 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3797 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, &blt_pshaders[tex_type], NULL));
3798 GL_EXTCALL(glCompileShaderARB(pshader_id));
3799
3800 program_id = GL_EXTCALL(glCreateProgramObjectARB());
3801 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
3802 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
3803 GL_EXTCALL(glLinkProgramARB(program_id));
3804
3805 print_glsl_info_log(&GLINFO_LOCATION, program_id);
3806
3807 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
3808 * is destroyed
3809 */
3810 GL_EXTCALL(glDeleteObjectARB(vshader_id));
3811 GL_EXTCALL(glDeleteObjectARB(pshader_id));
3812 return program_id;
3813 }
3814
3815 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
3816 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3817 struct shader_glsl_priv *priv = This->shader_priv;
3818 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3819 GLhandleARB program_id = 0;
3820 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
3821
3822 old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3823
3824 if (useVS || usePS) set_glsl_shader_program(iface, usePS, useVS);
3825 else priv->glsl_program = NULL;
3826
3827 current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3828
3829 if (old_vertex_color_clamp != current_vertex_color_clamp) {
3830 if (GL_SUPPORT(ARB_COLOR_BUFFER_FLOAT)) {
3831 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
3832 checkGLcall("glClampColorARB");
3833 } else {
3834 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
3835 }
3836 }
3837
3838 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
3839 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3840 GL_EXTCALL(glUseProgramObjectARB(program_id));
3841 checkGLcall("glUseProgramObjectARB");
3842 }
3843
3844 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {
3845 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3846 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3847 struct shader_glsl_priv *priv = This->shader_priv;
3848 GLhandleARB *blt_program = &priv->depth_blt_program[tex_type];
3849
3850 if (!*blt_program) {
3851 GLint loc;
3852 *blt_program = create_glsl_blt_shader(gl_info, tex_type);
3853 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
3854 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
3855 GL_EXTCALL(glUniform1iARB(loc, 0));
3856 } else {
3857 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
3858 }
3859 }
3860
3861 static void shader_glsl_deselect_depth_blt(IWineD3DDevice *iface) {
3862 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3863 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3864 struct shader_glsl_priv *priv = This->shader_priv;
3865 GLhandleARB program_id;
3866
3867 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
3868 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3869
3870 GL_EXTCALL(glUseProgramObjectARB(program_id));
3871 checkGLcall("glUseProgramObjectARB");
3872 }
3873
3874 static void shader_glsl_destroy(IWineD3DBaseShader *iface) {
3875 const struct list *linked_programs;
3876 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) iface;
3877 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
3878 struct shader_glsl_priv *priv = device->shader_priv;
3879 const WineD3D_GL_Info *gl_info = &device->adapter->gl_info;
3880 IWineD3DPixelShaderImpl *ps = NULL;
3881 IWineD3DVertexShaderImpl *vs = NULL;
3882
3883 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
3884 * can be called from IWineD3DBaseShader::Release
3885 */
3886 char pshader = shader_is_pshader_version(This->baseShader.reg_maps.shader_version.type);
3887
3888 if(pshader) {
3889 ps = (IWineD3DPixelShaderImpl *) This;
3890 if(ps->num_gl_shaders == 0) return;
3891 if (priv->glsl_program && (IWineD3DBaseShader *)priv->glsl_program->pshader == iface)
3892 shader_glsl_select(This->baseShader.device, FALSE, FALSE);
3893 } else {
3894 vs = (IWineD3DVertexShaderImpl *) This;
3895 if(vs->num_gl_shaders == 0) return;
3896 if (priv->glsl_program && (IWineD3DBaseShader *)priv->glsl_program->vshader == iface)
3897 shader_glsl_select(This->baseShader.device, FALSE, FALSE);
3898 }
3899
3900 linked_programs = &This->baseShader.linked_programs;
3901
3902 TRACE("Deleting linked programs\n");
3903 if (linked_programs->next) {
3904 struct glsl_shader_prog_link *entry, *entry2;
3905
3906 if(pshader) {
3907 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
3908 delete_glsl_program_entry(priv, gl_info, entry);
3909 }
3910 } else {
3911 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, vshader_entry) {
3912 delete_glsl_program_entry(priv, gl_info, entry);
3913 }
3914 }
3915 }
3916
3917 if(pshader) {
3918 UINT i;
3919
3920 ENTER_GL();
3921 for(i = 0; i < ps->num_gl_shaders; i++) {
3922 TRACE("deleting pshader %u\n", ps->gl_shaders[i].prgId);
3923 GL_EXTCALL(glDeleteObjectARB(ps->gl_shaders[i].prgId));
3924 checkGLcall("glDeleteObjectARB");
3925 }
3926 LEAVE_GL();
3927 HeapFree(GetProcessHeap(), 0, ps->gl_shaders);
3928 ps->gl_shaders = NULL;
3929 ps->num_gl_shaders = 0;
3930 ps->shader_array_size = 0;
3931 } else {
3932 UINT i;
3933
3934 ENTER_GL();
3935 for(i = 0; i < vs->num_gl_shaders; i++) {
3936 TRACE("deleting vshader %u\n", vs->gl_shaders[i].prgId);
3937 GL_EXTCALL(glDeleteObjectARB(vs->gl_shaders[i].prgId));
3938 checkGLcall("glDeleteObjectARB");
3939 }
3940 LEAVE_GL();
3941 HeapFree(GetProcessHeap(), 0, vs->gl_shaders);
3942 vs->gl_shaders = NULL;
3943 vs->num_gl_shaders = 0;
3944 vs->shader_array_size = 0;
3945 }
3946 }
3947
3948 static unsigned int glsl_program_key_hash(const void *key)
3949 {
3950 const glsl_program_key_t *k = key;
3951
3952 unsigned int hash = ((DWORD_PTR) k->vshader) | ((DWORD_PTR) k->pshader) << 16;
3953 hash += ~(hash << 15);
3954 hash ^= (hash >> 10);
3955 hash += (hash << 3);
3956 hash ^= (hash >> 6);
3957 hash += ~(hash << 11);
3958 hash ^= (hash >> 16);
3959
3960 return hash;
3961 }
3962
3963 static BOOL glsl_program_key_compare(const void *keya, const void *keyb)
3964 {
3965 const glsl_program_key_t *ka = keya;
3966 const glsl_program_key_t *kb = keyb;
3967
3968 return ka->vshader == kb->vshader && ka->pshader == kb->pshader &&
3969 (memcmp(&ka->ps_args, &kb->ps_args, sizeof(kb->ps_args)) == 0) &&
3970 (memcmp(&ka->vs_args, &kb->vs_args, sizeof(kb->vs_args)) == 0);
3971 }
3972
3973 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
3974 {
3975 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries) + constant_count * sizeof(*heap->positions);
3976 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
3977
3978 if (!mem)
3979 {
3980 ERR("Failed to allocate memory\n");
3981 return FALSE;
3982 }
3983
3984 heap->entries = mem;
3985 heap->entries[1].version = 0;
3986 heap->positions = (unsigned int *)(heap->entries + constant_count + 1);
3987 heap->size = 1;
3988
3989 return TRUE;
3990 }
3991
3992 static void constant_heap_free(struct constant_heap *heap)
3993 {
3994 HeapFree(GetProcessHeap(), 0, heap->entries);
3995 }
3996
3997 static HRESULT shader_glsl_alloc(IWineD3DDevice *iface) {
3998 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3999 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
4000 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
4001 SIZE_T stack_size = wined3d_log2i(max(GL_LIMITS(vshader_constantsF), GL_LIMITS(pshader_constantsF))) + 1;
4002
4003 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
4004 if (!priv->stack)
4005 {
4006 ERR("Failed to allocate memory.\n");
4007 HeapFree(GetProcessHeap(), 0, priv);
4008 return E_OUTOFMEMORY;
4009 }
4010
4011 if (!constant_heap_init(&priv->vconst_heap, GL_LIMITS(vshader_constantsF)))
4012 {
4013 ERR("Failed to initialize vertex shader constant heap\n");
4014 HeapFree(GetProcessHeap(), 0, priv->stack);
4015 HeapFree(GetProcessHeap(), 0, priv);
4016 return E_OUTOFMEMORY;
4017 }
4018
4019 if (!constant_heap_init(&priv->pconst_heap, GL_LIMITS(pshader_constantsF)))
4020 {
4021 ERR("Failed to initialize pixel shader constant heap\n");
4022 constant_heap_free(&priv->vconst_heap);
4023 HeapFree(GetProcessHeap(), 0, priv->stack);
4024 HeapFree(GetProcessHeap(), 0, priv);
4025 return E_OUTOFMEMORY;
4026 }
4027
4028 priv->glsl_program_lookup = hash_table_create(glsl_program_key_hash, glsl_program_key_compare);
4029 priv->next_constant_version = 1;
4030
4031 This->shader_priv = priv;
4032 return WINED3D_OK;
4033 }
4034
4035 static void shader_glsl_free(IWineD3DDevice *iface) {
4036 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4037 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
4038 struct shader_glsl_priv *priv = This->shader_priv;
4039 int i;
4040
4041 for (i = 0; i < tex_type_count; ++i)
4042 {
4043 if (priv->depth_blt_program[i])
4044 {
4045 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program[i]));
4046 }
4047 }
4048
4049 hash_table_destroy(priv->glsl_program_lookup, NULL, NULL);
4050 constant_heap_free(&priv->pconst_heap);
4051 constant_heap_free(&priv->vconst_heap);
4052
4053 HeapFree(GetProcessHeap(), 0, This->shader_priv);
4054 This->shader_priv = NULL;
4055 }
4056
4057 static BOOL shader_glsl_dirty_const(IWineD3DDevice *iface) {
4058 /* TODO: GL_EXT_bindable_uniform can be used to share constants across shaders */
4059 return FALSE;
4060 }
4061
4062 static GLuint shader_glsl_generate_pshader(IWineD3DPixelShader *iface,
4063 SHADER_BUFFER *buffer, const struct ps_compile_args *args)
4064 {
4065 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
4066 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
4067 CONST DWORD *function = This->baseShader.function;
4068 const char *fragcolor;
4069 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
4070
4071 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4072 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4073
4074 shader_addline(buffer, "#version 120\n");
4075
4076 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
4077 shader_addline(buffer, "#extension GL_ARB_draw_buffers : enable\n");
4078 }
4079 if(GL_SUPPORT(ARB_SHADER_TEXTURE_LOD) && reg_maps->usestexldd) {
4080 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
4081 }
4082 if (GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
4083 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
4084 * drivers write a warning if we don't do so
4085 */
4086 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4087 }
4088
4089 /* Base Declarations */
4090 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION, args);
4091
4092 /* Pack 3.0 inputs */
4093 if (reg_maps->shader_version.major >= 3 && args->vp_mode != vertexshader)
4094 {
4095 pshader_glsl_input_pack(iface, buffer, This->semantics_in, reg_maps, args->vp_mode);
4096 }
4097
4098 /* Base Shader Body */
4099 shader_generate_main((IWineD3DBaseShader *)This, buffer, reg_maps, function);
4100
4101 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4102 if (reg_maps->shader_version.major < 2)
4103 {
4104 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4105 if(GL_SUPPORT(ARB_DRAW_BUFFERS))
4106 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4107 else
4108 shader_addline(buffer, "gl_FragColor = R0;\n");
4109 }
4110
4111 if(GL_SUPPORT(ARB_DRAW_BUFFERS)) {
4112 fragcolor = "gl_FragData[0]";
4113 } else {
4114 fragcolor = "gl_FragColor";
4115 }
4116 if(args->srgb_correction) {
4117 shader_addline(buffer, "tmp0.xyz = pow(%s.xyz, vec3(%f, %f, %f)) * vec3(%f, %f, %f) - vec3(%f, %f, %f);\n",
4118 fragcolor, srgb_pow, srgb_pow, srgb_pow, srgb_mul_high, srgb_mul_high, srgb_mul_high,
4119 srgb_sub_high, srgb_sub_high, srgb_sub_high);
4120 shader_addline(buffer, "tmp1.xyz = %s.xyz * srgb_mul_low.xyz;\n", fragcolor);
4121 shader_addline(buffer, "%s.x = %s.x < srgb_comparison.x ? tmp1.x : tmp0.x;\n", fragcolor, fragcolor);
4122 shader_addline(buffer, "%s.y = %s.y < srgb_comparison.y ? tmp1.y : tmp0.y;\n", fragcolor, fragcolor);
4123 shader_addline(buffer, "%s.z = %s.z < srgb_comparison.z ? tmp1.z : tmp0.z;\n", fragcolor, fragcolor);
4124 shader_addline(buffer, "%s = clamp(%s, 0.0, 1.0);\n", fragcolor, fragcolor);
4125 }
4126 /* Pixel shader < 3.0 do not replace the fog stage.
4127 * This implements linear fog computation and blending.
4128 * TODO: non linear fog
4129 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
4130 * -1/(e-s) and e/(e-s) respectively.
4131 */
4132 if (reg_maps->shader_version.major < 3)
4133 {
4134 switch(args->fog) {
4135 case FOG_OFF: break;
4136 case FOG_LINEAR:
4137 shader_addline(buffer, "float fogstart = -1.0 / (gl_Fog.end - gl_Fog.start);\n");
4138 shader_addline(buffer, "float fogend = gl_Fog.end * -fogstart;\n");
4139 shader_addline(buffer, "float Fog = clamp(gl_FogFragCoord * fogstart + fogend, 0.0, 1.0);\n");
4140 shader_addline(buffer, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor, fragcolor);
4141 break;
4142 case FOG_EXP:
4143 /* Fog = e^(-gl_Fog.density * gl_FogFragCoord) */
4144 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4145 shader_addline(buffer, "Fog = clamp(Fog, 0.0, 1.0);\n");
4146 shader_addline(buffer, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor, fragcolor);
4147 break;
4148 case FOG_EXP2:
4149 /* Fog = e^(-(gl_Fog.density * gl_FogFragCoord)^2) */
4150 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4151 shader_addline(buffer, "Fog = clamp(Fog, 0.0, 1.0);\n");
4152 shader_addline(buffer, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor, fragcolor);
4153 break;
4154 }
4155 }
4156
4157 shader_addline(buffer, "}\n");
4158
4159 TRACE("Compiling shader object %u\n", shader_obj);
4160 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
4161 GL_EXTCALL(glCompileShaderARB(shader_obj));
4162 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
4163
4164 /* Store the shader object */
4165 return shader_obj;
4166 }
4167
4168 static GLuint shader_glsl_generate_vshader(IWineD3DVertexShader *iface,
4169 SHADER_BUFFER *buffer, const struct vs_compile_args *args)
4170 {
4171 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
4172 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
4173 CONST DWORD *function = This->baseShader.function;
4174 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
4175
4176 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4177 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4178
4179 shader_addline(buffer, "#version 120\n");
4180
4181 /* Base Declarations */
4182 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION, NULL);
4183
4184 /* Base Shader Body */
4185 shader_generate_main((IWineD3DBaseShader*)This, buffer, reg_maps, function);
4186
4187 /* Unpack 3.0 outputs */
4188 if (reg_maps->shader_version.major >= 3) shader_addline(buffer, "order_ps_input(OUT);\n");
4189 else shader_addline(buffer, "order_ps_input();\n");
4190
4191 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4192 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4193 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4194 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4195 */
4196 if(args->fog_src == VS_FOG_Z) {
4197 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4198 } else if (!reg_maps->fog) {
4199 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4200 }
4201
4202 /* Write the final position.
4203 *
4204 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4205 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4206 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4207 * contains 1.0 to allow a mad.
4208 */
4209 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4210 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4211
4212 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4213 *
4214 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4215 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4216 * which is the same as z = z * 2 - w.
4217 */
4218 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4219
4220 shader_addline(buffer, "}\n");
4221
4222 TRACE("Compiling shader object %u\n", shader_obj);
4223 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
4224 GL_EXTCALL(glCompileShaderARB(shader_obj));
4225 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
4226
4227 return shader_obj;
4228 }
4229
4230 static void shader_glsl_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
4231 {
4232 /* Nvidia Geforce6/7 or Ati R4xx/R5xx cards with GLSL support, support VS 3.0 but older Nvidia/Ati
4233 * models with GLSL support only support 2.0. In case of nvidia we can detect VS 2.0 support using
4234 * vs_nv_version which is based on NV_vertex_program.
4235 * For Ati cards there's no way using glsl (it abstracts the lowlevel info away) and also not
4236 * using ARB_vertex_program. It is safe to assume that when a card supports pixel shader 2.0 it
4237 * supports vertex shader 2.0 too and the way around. We can detect ps2.0 using the maximum number
4238 * of native instructions, so use that here. For more info see the pixel shader versioning code below.
4239 */
4240 if((GLINFO_LOCATION.vs_nv_version == VS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
4241 pCaps->VertexShaderVersion = WINED3DVS_VERSION(2,0);
4242 else
4243 pCaps->VertexShaderVersion = WINED3DVS_VERSION(3,0);
4244 TRACE_(d3d_caps)("Hardware vertex shader version %d.%d enabled (GLSL)\n", (pCaps->VertexShaderVersion >> 8) & 0xff, pCaps->VertexShaderVersion & 0xff);
4245 pCaps->MaxVertexShaderConst = GL_LIMITS(vshader_constantsF);
4246
4247 /* Older DX9-class videocards (GeforceFX / Radeon >9500/X*00) only support pixel shader 2.0/2.0a/2.0b.
4248 * In OpenGL the extensions related to GLSL abstract lowlevel GL info away which is needed
4249 * to distinguish between 2.0 and 3.0 (and 2.0a/2.0b). In case of Nvidia we use their fragment
4250 * program extensions. On other hardware including ATI GL_ARB_fragment_program offers the info
4251 * in max native instructions. Intel and others also offer the info in this extension but they
4252 * don't support GLSL (at least on Windows).
4253 *
4254 * PS2.0 requires at least 96 instructions, 2.0a/2.0b go up to 512. Assume that if the number
4255 * of instructions is 512 or less we have to do with ps2.0 hardware.
4256 * NOTE: ps3.0 hardware requires 512 or more instructions but ati and nvidia offer 'enough' (1024 vs 4096) on their most basic ps3.0 hardware.
4257 */
4258 if((GLINFO_LOCATION.ps_nv_version == PS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
4259 pCaps->PixelShaderVersion = WINED3DPS_VERSION(2,0);
4260 else
4261 pCaps->PixelShaderVersion = WINED3DPS_VERSION(3,0);
4262
4263 pCaps->MaxPixelShaderConst = GL_LIMITS(pshader_constantsF);
4264
4265 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
4266 * Direct3D minimum requirement.
4267 *
4268 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
4269 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
4270 *
4271 * The problem is that the refrast clamps temporary results in the shader to
4272 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
4273 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
4274 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
4275 * offer a way to query this.
4276 */
4277 pCaps->PixelShader1xMaxValue = 8.0;
4278 TRACE_(d3d_caps)("Hardware pixel shader version %d.%d enabled (GLSL)\n", (pCaps->PixelShaderVersion >> 8) & 0xff, pCaps->PixelShaderVersion & 0xff);
4279 }
4280
4281 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
4282 {
4283 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
4284 {
4285 TRACE("Checking support for fixup:\n");
4286 dump_color_fixup_desc(fixup);
4287 }
4288
4289 /* We support everything except YUV conversions. */
4290 if (!is_yuv_fixup(fixup))
4291 {
4292 TRACE("[OK]\n");
4293 return TRUE;
4294 }
4295
4296 TRACE("[FAILED]\n");
4297 return FALSE;
4298 }
4299
4300 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
4301 {
4302 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
4303 /* WINED3DSIH_ADD */ shader_glsl_arith,
4304 /* WINED3DSIH_BEM */ pshader_glsl_bem,
4305 /* WINED3DSIH_BREAK */ shader_glsl_break,
4306 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
4307 /* WINED3DSIH_BREAKP */ NULL,
4308 /* WINED3DSIH_CALL */ shader_glsl_call,
4309 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
4310 /* WINED3DSIH_CMP */ shader_glsl_cmp,
4311 /* WINED3DSIH_CND */ shader_glsl_cnd,
4312 /* WINED3DSIH_CRS */ shader_glsl_cross,
4313 /* WINED3DSIH_DCL */ NULL,
4314 /* WINED3DSIH_DEF */ NULL,
4315 /* WINED3DSIH_DEFB */ NULL,
4316 /* WINED3DSIH_DEFI */ NULL,
4317 /* WINED3DSIH_DP2ADD */ pshader_glsl_dp2add,
4318 /* WINED3DSIH_DP3 */ shader_glsl_dot,
4319 /* WINED3DSIH_DP4 */ shader_glsl_dot,
4320 /* WINED3DSIH_DST */ shader_glsl_dst,
4321 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
4322 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
4323 /* WINED3DSIH_ELSE */ shader_glsl_else,
4324 /* WINED3DSIH_ENDIF */ shader_glsl_end,
4325 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
4326 /* WINED3DSIH_ENDREP */ shader_glsl_end,
4327 /* WINED3DSIH_EXP */ shader_glsl_map2gl,
4328 /* WINED3DSIH_EXPP */ shader_glsl_expp,
4329 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
4330 /* WINED3DSIH_IF */ shader_glsl_if,
4331 /* WINED3DSIH_IFC */ shader_glsl_ifc,
4332 /* WINED3DSIH_LABEL */ shader_glsl_label,
4333 /* WINED3DSIH_LIT */ shader_glsl_lit,
4334 /* WINED3DSIH_LOG */ shader_glsl_log,
4335 /* WINED3DSIH_LOGP */ shader_glsl_log,
4336 /* WINED3DSIH_LOOP */ shader_glsl_loop,
4337 /* WINED3DSIH_LRP */ shader_glsl_lrp,
4338 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
4339 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
4340 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
4341 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
4342 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
4343 /* WINED3DSIH_MAD */ shader_glsl_mad,
4344 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
4345 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
4346 /* WINED3DSIH_MOV */ shader_glsl_mov,
4347 /* WINED3DSIH_MOVA */ shader_glsl_mov,
4348 /* WINED3DSIH_MUL */ shader_glsl_arith,
4349 /* WINED3DSIH_NOP */ NULL,
4350 /* WINED3DSIH_NRM */ shader_glsl_map2gl,
4351 /* WINED3DSIH_PHASE */ NULL,
4352 /* WINED3DSIH_POW */ shader_glsl_pow,
4353 /* WINED3DSIH_RCP */ shader_glsl_rcp,
4354 /* WINED3DSIH_REP */ shader_glsl_rep,
4355 /* WINED3DSIH_RET */ NULL,
4356 /* WINED3DSIH_RSQ */ shader_glsl_rsq,
4357 /* WINED3DSIH_SETP */ NULL,
4358 /* WINED3DSIH_SGE */ shader_glsl_compare,
4359 /* WINED3DSIH_SGN */ shader_glsl_map2gl,
4360 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
4361 /* WINED3DSIH_SLT */ shader_glsl_compare,
4362 /* WINED3DSIH_SUB */ shader_glsl_arith,
4363 /* WINED3DSIH_TEX */ pshader_glsl_tex,
4364 /* WINED3DSIH_TEXBEM */ pshader_glsl_texbem,
4365 /* WINED3DSIH_TEXBEML */ pshader_glsl_texbem,
4366 /* WINED3DSIH_TEXCOORD */ pshader_glsl_texcoord,
4367 /* WINED3DSIH_TEXDEPTH */ pshader_glsl_texdepth,
4368 /* WINED3DSIH_TEXDP3 */ pshader_glsl_texdp3,
4369 /* WINED3DSIH_TEXDP3TEX */ pshader_glsl_texdp3tex,
4370 /* WINED3DSIH_TEXKILL */ pshader_glsl_texkill,
4371 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
4372 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
4373 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_glsl_texm3x2depth,
4374 /* WINED3DSIH_TEXM3x2PAD */ pshader_glsl_texm3x2pad,
4375 /* WINED3DSIH_TEXM3x2TEX */ pshader_glsl_texm3x2tex,
4376 /* WINED3DSIH_TEXM3x3 */ pshader_glsl_texm3x3,
4377 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
4378 /* WINED3DSIH_TEXM3x3PAD */ pshader_glsl_texm3x3pad,
4379 /* WINED3DSIH_TEXM3x3SPEC */ pshader_glsl_texm3x3spec,
4380 /* WINED3DSIH_TEXM3x3TEX */ pshader_glsl_texm3x3tex,
4381 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_glsl_texm3x3vspec,
4382 /* WINED3DSIH_TEXREG2AR */ pshader_glsl_texreg2ar,
4383 /* WINED3DSIH_TEXREG2GB */ pshader_glsl_texreg2gb,
4384 /* WINED3DSIH_TEXREG2RGB */ pshader_glsl_texreg2rgb,
4385 };
4386
4387 const shader_backend_t glsl_shader_backend = {
4388 shader_glsl_instruction_handler_table,
4389 shader_glsl_select,
4390 shader_glsl_select_depth_blt,
4391 shader_glsl_deselect_depth_blt,
4392 shader_glsl_update_float_vertex_constants,
4393 shader_glsl_update_float_pixel_constants,
4394 shader_glsl_load_constants,
4395 shader_glsl_load_np2fixup_constants,
4396 shader_glsl_destroy,
4397 shader_glsl_alloc,
4398 shader_glsl_free,
4399 shader_glsl_dirty_const,
4400 shader_glsl_generate_pshader,
4401 shader_glsl_generate_vshader,
4402 shader_glsl_get_caps,
4403 shader_glsl_color_fixup_supported,
4404 shader_glsl_add_instruction_modifiers,
4405 };