wined3d: Move the ARB shader program constant handling to the backend where it belongs.
[wine/multimedia.git] / dlls / wined3d / glsl_shader.c
blob35a317c531dd63426602fc06de51483fd788e688
1 /*
2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
6 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
7 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
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.
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.
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
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.
32 #include "config.h"
33 #include <limits.h>
34 #include <stdio.h>
35 #include "wined3d_private.h"
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);
42 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x1
43 #define WINED3D_GLSL_SAMPLE_RECT 0x2
44 #define WINED3D_GLSL_SAMPLE_LOD 0x4
45 #define WINED3D_GLSL_SAMPLE_GRAD 0x8
47 struct glsl_dst_param
49 char reg_name[150];
50 char mask_str[6];
53 struct glsl_src_param
55 char reg_name[150];
56 char param_str[200];
59 struct glsl_sample_function
61 const char *name;
62 DWORD coord_mask;
65 enum heap_node_op
67 HEAP_NODE_TRAVERSE_LEFT,
68 HEAP_NODE_TRAVERSE_RIGHT,
69 HEAP_NODE_POP,
72 struct constant_entry
74 unsigned int idx;
75 unsigned int version;
78 struct constant_heap
80 struct constant_entry *entries;
81 unsigned int *positions;
82 unsigned int size;
85 /* GLSL shader private data */
86 struct shader_glsl_priv {
87 struct wined3d_shader_buffer shader_buffer;
88 struct wine_rb_tree program_lookup;
89 struct glsl_shader_prog_link *glsl_program;
90 struct constant_heap vconst_heap;
91 struct constant_heap pconst_heap;
92 unsigned char *stack;
93 GLhandleARB depth_blt_program_full[tex_type_count];
94 GLhandleARB depth_blt_program_masked[tex_type_count];
95 UINT next_constant_version;
98 /* Struct to maintain data about a linked GLSL program */
99 struct glsl_shader_prog_link {
100 struct wine_rb_entry program_lookup_entry;
101 struct list vshader_entry;
102 struct list pshader_entry;
103 GLhandleARB programId;
104 GLint *vuniformF_locations;
105 GLint *puniformF_locations;
106 GLint vuniformI_locations[MAX_CONST_I];
107 GLint puniformI_locations[MAX_CONST_I];
108 GLint posFixup_location;
109 GLint np2Fixup_location;
110 GLint bumpenvmat_location[MAX_TEXTURES];
111 GLint luminancescale_location[MAX_TEXTURES];
112 GLint luminanceoffset_location[MAX_TEXTURES];
113 GLint ycorrection_location;
114 GLenum vertex_color_clamp;
115 const struct wined3d_shader *vshader;
116 const struct wined3d_shader *pshader;
117 struct vs_compile_args vs_args;
118 struct ps_compile_args ps_args;
119 UINT constant_version;
120 const struct ps_np2fixup_info *np2Fixup_info;
123 struct glsl_program_key
125 const struct wined3d_shader *vshader;
126 const struct wined3d_shader *pshader;
127 struct ps_compile_args ps_args;
128 struct vs_compile_args vs_args;
131 struct shader_glsl_ctx_priv {
132 const struct vs_compile_args *cur_vs_args;
133 const struct ps_compile_args *cur_ps_args;
134 struct ps_np2fixup_info *cur_np2fixup_info;
137 struct glsl_ps_compiled_shader
139 struct ps_compile_args args;
140 struct ps_np2fixup_info np2fixup;
141 GLhandleARB prgId;
144 struct glsl_pshader_private
146 struct glsl_ps_compiled_shader *gl_shaders;
147 UINT num_gl_shaders, shader_array_size;
150 struct glsl_vs_compiled_shader
152 struct vs_compile_args args;
153 GLhandleARB prgId;
156 struct glsl_vshader_private
158 struct glsl_vs_compiled_shader *gl_shaders;
159 UINT num_gl_shaders, shader_array_size;
162 static const char *debug_gl_shader_type(GLenum type)
164 switch (type)
166 #define WINED3D_TO_STR(u) case u: return #u
167 WINED3D_TO_STR(GL_VERTEX_SHADER_ARB);
168 WINED3D_TO_STR(GL_GEOMETRY_SHADER_ARB);
169 WINED3D_TO_STR(GL_FRAGMENT_SHADER_ARB);
170 #undef WINED3D_TO_STR
171 default:
172 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
176 /* Extract a line from the info log.
177 * Note that this modifies the source string. */
178 static char *get_info_log_line(char **ptr)
180 char *p, *q;
182 p = *ptr;
183 if (!(q = strstr(p, "\n")))
185 if (!*p) return NULL;
186 *ptr += strlen(p);
187 return p;
189 *q = '\0';
190 *ptr = q + 1;
192 return p;
195 /** Prints the GLSL info log which will contain error messages if they exist */
196 /* GL locking is done by the caller */
197 static void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLhandleARB obj)
199 int infologLength = 0;
200 char *infoLog;
202 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
203 return;
205 GL_EXTCALL(glGetObjectParameterivARB(obj,
206 GL_OBJECT_INFO_LOG_LENGTH_ARB,
207 &infologLength));
209 /* A size of 1 is just a null-terminated string, so the log should be bigger than
210 * that if there are errors. */
211 if (infologLength > 1)
213 char *ptr, *line;
215 infoLog = HeapAlloc(GetProcessHeap(), 0, infologLength);
216 /* The info log is supposed to be zero-terminated, but at least some
217 * versions of fglrx don't terminate the string properly. The reported
218 * length does include the terminator, so explicitly set it to zero
219 * here. */
220 infoLog[infologLength - 1] = 0;
221 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
223 ptr = infoLog;
224 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
226 WARN("Info log received from GLSL shader #%u:\n", obj);
227 while ((line = get_info_log_line(&ptr))) WARN(" %s\n", line);
229 else
231 FIXME("Info log received from GLSL shader #%u:\n", obj);
232 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
234 HeapFree(GetProcessHeap(), 0, infoLog);
238 /* GL locking is done by the caller. */
239 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLhandleARB shader, const char *src)
241 TRACE("Compiling shader object %u.\n", shader);
242 GL_EXTCALL(glShaderSourceARB(shader, 1, &src, NULL));
243 checkGLcall("glShaderSourceARB");
244 GL_EXTCALL(glCompileShaderARB(shader));
245 checkGLcall("glCompileShaderARB");
246 print_glsl_info_log(gl_info, shader);
249 /* GL locking is done by the caller. */
250 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLhandleARB program)
252 GLint i, object_count, source_size = -1;
253 GLhandleARB *objects;
254 char *source = NULL;
256 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_ATTACHED_OBJECTS_ARB, &object_count));
257 objects = HeapAlloc(GetProcessHeap(), 0, object_count * sizeof(*objects));
258 if (!objects)
260 ERR("Failed to allocate object array memory.\n");
261 return;
264 GL_EXTCALL(glGetAttachedObjectsARB(program, object_count, NULL, objects));
265 for (i = 0; i < object_count; ++i)
267 char *ptr, *line;
268 GLint tmp;
270 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &tmp));
272 if (source_size < tmp)
274 HeapFree(GetProcessHeap(), 0, source);
276 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
277 if (!source)
279 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
280 HeapFree(GetProcessHeap(), 0, objects);
281 return;
283 source_size = tmp;
286 FIXME("Object %u:\n", objects[i]);
287 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SUBTYPE_ARB, &tmp));
288 FIXME(" GL_OBJECT_SUBTYPE_ARB: %s.\n", debug_gl_shader_type(tmp));
289 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_COMPILE_STATUS_ARB, &tmp));
290 FIXME(" GL_OBJECT_COMPILE_STATUS_ARB: %d.\n", tmp);
291 FIXME("\n");
293 ptr = source;
294 GL_EXTCALL(glGetShaderSourceARB(objects[i], source_size, NULL, source));
295 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
296 FIXME("\n");
299 HeapFree(GetProcessHeap(), 0, source);
300 HeapFree(GetProcessHeap(), 0, objects);
303 /* GL locking is done by the caller. */
304 static void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLhandleARB program)
306 GLint tmp;
308 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader)) return;
310 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_TYPE_ARB, &tmp));
311 if (tmp == GL_PROGRAM_OBJECT_ARB)
313 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &tmp));
314 if (!tmp)
316 FIXME("Program %u link status invalid.\n", program);
317 shader_glsl_dump_program_source(gl_info, program);
321 print_glsl_info_log(gl_info, program);
325 * Loads (pixel shader) samplers
327 /* GL locking is done by the caller */
328 static void shader_glsl_load_psamplers(const struct wined3d_gl_info *gl_info,
329 const DWORD *tex_unit_map, GLhandleARB programId)
331 GLint name_loc;
332 int i;
333 char sampler_name[20];
335 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
336 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
337 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
338 if (name_loc != -1) {
339 DWORD mapped_unit = tex_unit_map[i];
340 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.fragment_samplers)
342 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
343 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
344 checkGLcall("glUniform1iARB");
345 } else {
346 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
352 /* GL locking is done by the caller */
353 static void shader_glsl_load_vsamplers(const struct wined3d_gl_info *gl_info,
354 const DWORD *tex_unit_map, GLhandleARB programId)
356 GLint name_loc;
357 char sampler_name[20];
358 int i;
360 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
361 snprintf(sampler_name, sizeof(sampler_name), "Vsampler%d", i);
362 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
363 if (name_loc != -1) {
364 DWORD mapped_unit = tex_unit_map[MAX_FRAGMENT_SAMPLERS + i];
365 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.combined_samplers)
367 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
368 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
369 checkGLcall("glUniform1iARB");
370 } else {
371 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
377 /* GL locking is done by the caller */
378 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
379 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
381 int stack_idx = 0;
382 unsigned int heap_idx = 1;
383 unsigned int idx;
385 if (heap->entries[heap_idx].version <= version) return;
387 idx = heap->entries[heap_idx].idx;
388 if (constant_locations[idx] != -1) GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
389 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
391 while (stack_idx >= 0)
393 /* Note that we fall through to the next case statement. */
394 switch(stack[stack_idx])
396 case HEAP_NODE_TRAVERSE_LEFT:
398 unsigned int left_idx = heap_idx << 1;
399 if (left_idx < heap->size && heap->entries[left_idx].version > version)
401 heap_idx = left_idx;
402 idx = heap->entries[heap_idx].idx;
403 if (constant_locations[idx] != -1)
404 GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
406 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
407 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
408 break;
412 case HEAP_NODE_TRAVERSE_RIGHT:
414 unsigned int right_idx = (heap_idx << 1) + 1;
415 if (right_idx < heap->size && heap->entries[right_idx].version > version)
417 heap_idx = right_idx;
418 idx = heap->entries[heap_idx].idx;
419 if (constant_locations[idx] != -1)
420 GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
422 stack[stack_idx++] = HEAP_NODE_POP;
423 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
424 break;
428 case HEAP_NODE_POP:
430 heap_idx >>= 1;
431 --stack_idx;
432 break;
436 checkGLcall("walk_constant_heap()");
439 /* GL locking is done by the caller */
440 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
442 GLfloat clamped_constant[4];
444 if (location == -1) return;
446 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
447 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
448 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
449 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
451 GL_EXTCALL(glUniform4fvARB(location, 1, clamped_constant));
454 /* GL locking is done by the caller */
455 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
456 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
458 int stack_idx = 0;
459 unsigned int heap_idx = 1;
460 unsigned int idx;
462 if (heap->entries[heap_idx].version <= version) return;
464 idx = heap->entries[heap_idx].idx;
465 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
466 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
468 while (stack_idx >= 0)
470 /* Note that we fall through to the next case statement. */
471 switch(stack[stack_idx])
473 case HEAP_NODE_TRAVERSE_LEFT:
475 unsigned int left_idx = heap_idx << 1;
476 if (left_idx < heap->size && heap->entries[left_idx].version > version)
478 heap_idx = left_idx;
479 idx = heap->entries[heap_idx].idx;
480 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
482 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
483 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
484 break;
488 case HEAP_NODE_TRAVERSE_RIGHT:
490 unsigned int right_idx = (heap_idx << 1) + 1;
491 if (right_idx < heap->size && heap->entries[right_idx].version > version)
493 heap_idx = right_idx;
494 idx = heap->entries[heap_idx].idx;
495 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
497 stack[stack_idx++] = HEAP_NODE_POP;
498 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
499 break;
503 case HEAP_NODE_POP:
505 heap_idx >>= 1;
506 --stack_idx;
507 break;
511 checkGLcall("walk_constant_heap_clamped()");
514 /* Loads floating point constants (aka uniforms) into the currently set GLSL program. */
515 /* GL locking is done by the caller */
516 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
517 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
518 unsigned char *stack, UINT version)
520 const local_constant *lconst;
522 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
523 if (shader->reg_maps.shader_version.major == 1
524 && shader_is_pshader_version(shader->reg_maps.shader_version.type))
525 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
526 else
527 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
529 if (!shader->load_local_constsF)
531 TRACE("No need to load local float constants for this shader\n");
532 return;
535 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
536 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, local_constant, entry)
538 GLint location = constant_locations[lconst->idx];
539 /* We found this uniform name in the program - go ahead and send the data */
540 if (location != -1) GL_EXTCALL(glUniform4fvARB(location, 1, (const GLfloat *)lconst->value));
542 checkGLcall("glUniform4fvARB()");
545 /* Loads integer constants (aka uniforms) into the currently set GLSL program. */
546 /* GL locking is done by the caller */
547 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
548 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
550 unsigned int i;
551 struct list* ptr;
553 for (i = 0; constants_set; constants_set >>= 1, ++i)
555 if (!(constants_set & 1)) continue;
557 TRACE_(d3d_constants)("Loading constants %u: %i, %i, %i, %i\n",
558 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
560 /* We found this uniform name in the program - go ahead and send the data */
561 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
562 checkGLcall("glUniform4ivARB");
565 /* Load immediate constants */
566 ptr = list_head(&shader->constantsI);
567 while (ptr)
569 const struct local_constant *lconst = LIST_ENTRY(ptr, const struct local_constant, entry);
570 unsigned int idx = lconst->idx;
571 const GLint *values = (const GLint *)lconst->value;
573 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
574 values[0], values[1], values[2], values[3]);
576 /* We found this uniform name in the program - go ahead and send the data */
577 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
578 checkGLcall("glUniform4ivARB");
579 ptr = list_next(&shader->constantsI, ptr);
583 /* Loads boolean constants (aka uniforms) into the currently set GLSL program. */
584 /* GL locking is done by the caller */
585 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
586 GLhandleARB programId, const BOOL *constants, WORD constants_set)
588 GLint tmp_loc;
589 unsigned int i;
590 char tmp_name[8];
591 const char *prefix;
592 struct list* ptr;
594 switch (shader->reg_maps.shader_version.type)
596 case WINED3D_SHADER_TYPE_VERTEX:
597 prefix = "VB";
598 break;
600 case WINED3D_SHADER_TYPE_GEOMETRY:
601 prefix = "GB";
602 break;
604 case WINED3D_SHADER_TYPE_PIXEL:
605 prefix = "PB";
606 break;
608 default:
609 FIXME("Unknown shader type %#x.\n",
610 shader->reg_maps.shader_version.type);
611 prefix = "UB";
612 break;
615 /* TODO: Benchmark and see if it would be beneficial to store the
616 * locations of the constants to avoid looking up each time */
617 for (i = 0; constants_set; constants_set >>= 1, ++i)
619 if (!(constants_set & 1)) continue;
621 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i]);
623 /* TODO: Benchmark and see if it would be beneficial to store the
624 * locations of the constants to avoid looking up each time */
625 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
626 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
627 if (tmp_loc != -1)
629 /* We found this uniform name in the program - go ahead and send the data */
630 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
631 checkGLcall("glUniform1ivARB");
635 /* Load immediate constants */
636 ptr = list_head(&shader->constantsB);
637 while (ptr)
639 const struct local_constant *lconst = LIST_ENTRY(ptr, const struct local_constant, entry);
640 unsigned int idx = lconst->idx;
641 const GLint *values = (const GLint *)lconst->value;
643 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
645 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
646 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
647 if (tmp_loc != -1) {
648 /* We found this uniform name in the program - go ahead and send the data */
649 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
650 checkGLcall("glUniform1ivARB");
652 ptr = list_next(&shader->constantsB, ptr);
656 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
658 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
662 * Loads the texture dimensions for NP2 fixup into the currently set GLSL program.
664 /* GL locking is done by the caller (state handler) */
665 static void shader_glsl_load_np2fixup_constants(void *shader_priv,
666 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
668 struct shader_glsl_priv *glsl_priv = shader_priv;
669 const struct glsl_shader_prog_link *prog = glsl_priv->glsl_program;
671 /* No GLSL program set - nothing to do. */
672 if (!prog) return;
674 /* NP2 texcoord fixup is (currently) only done for pixelshaders. */
675 if (!use_ps(state)) return;
677 if (prog->ps_args.np2_fixup && prog->np2Fixup_location != -1)
679 UINT i;
680 UINT fixup = prog->ps_args.np2_fixup;
681 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
683 for (i = 0; fixup; fixup >>= 1, ++i)
685 const struct wined3d_texture *tex = state->textures[i];
686 const unsigned char idx = prog->np2Fixup_info->idx[i];
687 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
689 if (!tex)
691 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
692 continue;
695 if (idx % 2)
697 tex_dim[2] = tex->pow2_matrix[0];
698 tex_dim[3] = tex->pow2_matrix[5];
700 else
702 tex_dim[0] = tex->pow2_matrix[0];
703 tex_dim[1] = tex->pow2_matrix[5];
707 GL_EXTCALL(glUniform4fvARB(prog->np2Fixup_location, prog->np2Fixup_info->num_consts, np2fixup_constants));
712 * Loads the app-supplied constants into the currently set GLSL program.
714 /* GL locking is done by the caller (state handler) */
715 static void shader_glsl_load_constants(const struct wined3d_context *context,
716 char usePixelShader, char useVertexShader)
718 const struct wined3d_gl_info *gl_info = context->gl_info;
719 struct wined3d_device *device = context->swapchain->device;
720 struct wined3d_stateblock *stateBlock = device->stateBlock;
721 struct shader_glsl_priv *priv = device->shader_priv;
722 float position_fixup[4];
724 GLhandleARB programId;
725 struct glsl_shader_prog_link *prog = priv->glsl_program;
726 UINT constant_version;
727 int i;
729 if (!prog) {
730 /* No GLSL program set - nothing to do. */
731 return;
733 programId = prog->programId;
734 constant_version = prog->constant_version;
736 if (useVertexShader)
738 const struct wined3d_shader *vshader = stateBlock->state.vertex_shader;
740 /* Load DirectX 9 float constants/uniforms for vertex shader */
741 shader_glsl_load_constantsF(vshader, gl_info, stateBlock->state.vs_consts_f,
742 prog->vuniformF_locations, &priv->vconst_heap, priv->stack, constant_version);
744 /* Load DirectX 9 integer constants/uniforms for vertex shader */
745 shader_glsl_load_constantsI(vshader, gl_info, prog->vuniformI_locations, stateBlock->state.vs_consts_i,
746 stateBlock->changed.vertexShaderConstantsI & vshader->reg_maps.integer_constants);
748 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
749 shader_glsl_load_constantsB(vshader, gl_info, programId, stateBlock->state.vs_consts_b,
750 stateBlock->changed.vertexShaderConstantsB & vshader->reg_maps.boolean_constants);
752 /* Upload the position fixup params */
753 shader_get_position_fixup(context, &stateBlock->state, position_fixup);
754 GL_EXTCALL(glUniform4fvARB(prog->posFixup_location, 1, position_fixup));
755 checkGLcall("glUniform4fvARB");
758 if (usePixelShader)
760 const struct wined3d_shader *pshader = stateBlock->state.pixel_shader;
762 /* Load DirectX 9 float constants/uniforms for pixel shader */
763 shader_glsl_load_constantsF(pshader, gl_info, stateBlock->state.ps_consts_f,
764 prog->puniformF_locations, &priv->pconst_heap, priv->stack, constant_version);
766 /* Load DirectX 9 integer constants/uniforms for pixel shader */
767 shader_glsl_load_constantsI(pshader, gl_info, prog->puniformI_locations, stateBlock->state.ps_consts_i,
768 stateBlock->changed.pixelShaderConstantsI & pshader->reg_maps.integer_constants);
770 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
771 shader_glsl_load_constantsB(pshader, gl_info, programId, stateBlock->state.ps_consts_b,
772 stateBlock->changed.pixelShaderConstantsB & pshader->reg_maps.boolean_constants);
774 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
775 * It can't be 0 for a valid texbem instruction.
777 for(i = 0; i < MAX_TEXTURES; i++) {
778 const float *data;
780 if(prog->bumpenvmat_location[i] == -1) continue;
782 data = (const float *)&stateBlock->state.texture_states[i][WINED3DTSS_BUMPENVMAT00];
783 GL_EXTCALL(glUniformMatrix2fvARB(prog->bumpenvmat_location[i], 1, 0, data));
784 checkGLcall("glUniformMatrix2fvARB");
786 /* texbeml needs the luminance scale and offset too. If texbeml
787 * is used, needsbumpmat is set too, so we can check that in the
788 * needsbumpmat check. */
789 if (prog->luminancescale_location[i] != -1)
791 const GLfloat *scale = (const GLfloat *)&stateBlock->state.texture_states[i][WINED3DTSS_BUMPENVLSCALE];
792 const GLfloat *offset = (const GLfloat *)&stateBlock->state.texture_states[i][WINED3DTSS_BUMPENVLOFFSET];
794 GL_EXTCALL(glUniform1fvARB(prog->luminancescale_location[i], 1, scale));
795 checkGLcall("glUniform1fvARB");
796 GL_EXTCALL(glUniform1fvARB(prog->luminanceoffset_location[i], 1, offset));
797 checkGLcall("glUniform1fvARB");
801 if (prog->ycorrection_location != -1)
803 float correction_params[4];
805 if (context->render_offscreen)
807 correction_params[0] = 0.0f;
808 correction_params[1] = 1.0f;
809 } else {
810 /* position is window relative, not viewport relative */
811 correction_params[0] = (float) context->current_rt->resource.height;
812 correction_params[1] = -1.0f;
814 GL_EXTCALL(glUniform4fvARB(prog->ycorrection_location, 1, correction_params));
818 if (priv->next_constant_version == UINT_MAX)
820 TRACE("Max constant version reached, resetting to 0.\n");
821 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
822 priv->next_constant_version = 1;
824 else
826 prog->constant_version = priv->next_constant_version++;
830 static void update_heap_entry(const struct constant_heap *heap, unsigned int idx,
831 unsigned int heap_idx, DWORD new_version)
833 struct constant_entry *entries = heap->entries;
834 unsigned int *positions = heap->positions;
835 unsigned int parent_idx;
837 while (heap_idx > 1)
839 parent_idx = heap_idx >> 1;
841 if (new_version <= entries[parent_idx].version) break;
843 entries[heap_idx] = entries[parent_idx];
844 positions[entries[parent_idx].idx] = heap_idx;
845 heap_idx = parent_idx;
848 entries[heap_idx].version = new_version;
849 entries[heap_idx].idx = idx;
850 positions[idx] = heap_idx;
853 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
855 struct shader_glsl_priv *priv = device->shader_priv;
856 struct constant_heap *heap = &priv->vconst_heap;
857 UINT i;
859 for (i = start; i < count + start; ++i)
861 if (!device->stateBlock->changed.vertexShaderConstantsF[i])
862 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
863 else
864 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
868 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
870 struct shader_glsl_priv *priv = device->shader_priv;
871 struct constant_heap *heap = &priv->pconst_heap;
872 UINT i;
874 for (i = start; i < count + start; ++i)
876 if (!device->stateBlock->changed.pixelShaderConstantsF[i])
877 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
878 else
879 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
883 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
885 unsigned int ret = gl_info->limits.glsl_varyings / 4;
886 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
887 if(shader_major > 3) return ret;
889 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
890 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
891 return ret;
894 /** Generate the variable & register declarations for the GLSL output target */
895 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
896 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
897 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
899 const struct wined3d_state *state = &shader->device->stateBlock->state;
900 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
901 const struct wined3d_gl_info *gl_info = context->gl_info;
902 const struct wined3d_fb_state *fb = &shader->device->fb;
903 unsigned int i, extra_constants_needed = 0;
904 const local_constant *lconst;
905 DWORD map;
907 /* There are some minor differences between pixel and vertex shaders */
908 char pshader = shader_is_pshader_version(reg_maps->shader_version.type);
909 char prefix = pshader ? 'P' : 'V';
911 /* Prototype the subroutines */
912 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
914 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
917 /* Declare the constants (aka uniforms) */
918 if (shader->limits.constant_float > 0)
920 unsigned max_constantsF;
921 /* Unless the shader uses indirect addressing, always declare the maximum array size and ignore that we need some
922 * uniforms privately. E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup and immediate values, still
923 * declare VC[256]. If the shader needs more uniforms than we have it won't work in any case. If it uses less, the
924 * compiler will figure out which uniforms are really used and strip them out. This allows a shader to use c255 on
925 * a dx9 card, as long as it doesn't also use all the other constants.
927 * If the shader uses indirect addressing the compiler must assume that all declared uniforms are used. In this case,
928 * declare only the amount that we're assured to have.
930 * Thus we run into problems in these two cases:
931 * 1) The shader really uses more uniforms than supported
932 * 2) The shader uses indirect addressing, less constants than supported, but uses a constant index > #supported consts
934 if (pshader)
936 /* No indirect addressing here. */
937 max_constantsF = gl_info->limits.glsl_ps_float_constants;
939 else
941 if (reg_maps->usesrelconstF)
943 /* Subtract the other potential uniforms from the max
944 * available (bools, ints, and 1 row of projection matrix).
945 * Subtract another uniform for immediate values, which have
946 * to be loaded via uniform by the driver as well. The shader
947 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
948 * shader code, so one vec4 should be enough. (Unfortunately
949 * the Nvidia driver doesn't store 128 and -128 in one float).
951 * Writing gl_ClipVertex requires one uniform for each
952 * clipplane as well. */
953 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
954 if(ctx_priv->cur_vs_args->clip_enabled)
956 max_constantsF -= gl_info->limits.clipplanes;
958 max_constantsF -= count_bits(reg_maps->integer_constants);
959 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
960 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
961 * for now take this into account when calculating the number of available constants
963 max_constantsF -= count_bits(reg_maps->boolean_constants);
964 /* Set by driver quirks in directx.c */
965 max_constantsF -= gl_info->reserved_glsl_constants;
967 else
969 max_constantsF = gl_info->limits.glsl_vs_float_constants;
972 max_constantsF = min(shader->limits.constant_float, max_constantsF);
973 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
976 /* Always declare the full set of constants, the compiler can remove the
977 * unused ones because d3d doesn't (yet) support indirect int and bool
978 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
979 if (shader->limits.constant_int > 0 && reg_maps->integer_constants)
980 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, shader->limits.constant_int);
982 if (shader->limits.constant_bool > 0 && reg_maps->boolean_constants)
983 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, shader->limits.constant_bool);
985 if (!pshader)
987 shader_addline(buffer, "uniform vec4 posFixup;\n");
988 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", MAX_REG_OUTPUT);
990 else
992 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
994 if (!(map & 1)) continue;
996 shader_addline(buffer, "uniform mat2 bumpenvmat%d;\n", i);
998 if (reg_maps->luminanceparams & (1 << i))
1000 shader_addline(buffer, "uniform float luminancescale%d;\n", i);
1001 shader_addline(buffer, "uniform float luminanceoffset%d;\n", i);
1002 extra_constants_needed++;
1005 extra_constants_needed++;
1008 if (ps_args->srgb_correction)
1010 shader_addline(buffer, "const vec4 srgb_const0 = vec4(%.8e, %.8e, %.8e, %.8e);\n",
1011 srgb_pow, srgb_mul_high, srgb_sub_high, srgb_mul_low);
1012 shader_addline(buffer, "const vec4 srgb_const1 = vec4(%.8e, 0.0, 0.0, 0.0);\n",
1013 srgb_cmp);
1015 if (reg_maps->vpos || reg_maps->usesdsy)
1017 if (shader->limits.constant_float + extra_constants_needed
1018 + 1 < gl_info->limits.glsl_ps_float_constants)
1020 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1021 extra_constants_needed++;
1023 else
1025 /* This happens because we do not have proper tracking of the constant registers that are
1026 * actually used, only the max limit of the shader version
1028 FIXME("Cannot find a free uniform for vpos correction params\n");
1029 shader_addline(buffer, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
1030 context->render_offscreen ? 0.0f : fb->render_targets[0]->resource.height,
1031 context->render_offscreen ? 1.0f : -1.0f);
1033 shader_addline(buffer, "vec4 vpos;\n");
1037 /* Declare texture samplers */
1038 for (i = 0; i < shader->limits.sampler; ++i)
1040 if (reg_maps->sampler_type[i])
1042 const struct wined3d_texture *texture;
1044 switch (reg_maps->sampler_type[i])
1046 case WINED3DSTT_1D:
1047 if (pshader && ps_args->shadow & (1 << i))
1048 shader_addline(buffer, "uniform sampler1DShadow %csampler%u;\n", prefix, i);
1049 else
1050 shader_addline(buffer, "uniform sampler1D %csampler%u;\n", prefix, i);
1051 break;
1052 case WINED3DSTT_2D:
1053 texture = state->textures[i];
1054 if (pshader && ps_args->shadow & (1 << i))
1056 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
1057 shader_addline(buffer, "uniform sampler2DRectShadow %csampler%u;\n", prefix, i);
1058 else
1059 shader_addline(buffer, "uniform sampler2DShadow %csampler%u;\n", prefix, i);
1061 else
1063 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
1064 shader_addline(buffer, "uniform sampler2DRect %csampler%u;\n", prefix, i);
1065 else
1066 shader_addline(buffer, "uniform sampler2D %csampler%u;\n", prefix, i);
1068 break;
1069 case WINED3DSTT_CUBE:
1070 if (pshader && ps_args->shadow & (1 << i)) FIXME("Unsupported Cube shadow sampler.\n");
1071 shader_addline(buffer, "uniform samplerCube %csampler%u;\n", prefix, i);
1072 break;
1073 case WINED3DSTT_VOLUME:
1074 if (pshader && ps_args->shadow & (1 << i)) FIXME("Unsupported 3D shadow sampler.\n");
1075 shader_addline(buffer, "uniform sampler3D %csampler%u;\n", prefix, i);
1076 break;
1077 default:
1078 shader_addline(buffer, "uniform unsupported_sampler %csampler%u;\n", prefix, i);
1079 FIXME("Unrecognized sampler type: %#x\n", reg_maps->sampler_type[i]);
1080 break;
1085 /* Declare uniforms for NP2 texcoord fixup:
1086 * This is NOT done inside the loop that declares the texture samplers since the NP2 fixup code
1087 * is currently only used for the GeforceFX series and when forcing the ARB_npot extension off.
1088 * Modern cards just skip the code anyway, so put it inside a separate loop. */
1089 if (pshader && ps_args->np2_fixup) {
1091 struct ps_np2fixup_info* const fixup = ctx_priv->cur_np2fixup_info;
1092 UINT cur = 0;
1094 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1095 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1096 * samplerNP2Fixup stores texture dimensions and is updated through
1097 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1099 for (i = 0; i < shader->limits.sampler; ++i)
1101 if (reg_maps->sampler_type[i])
1103 if (!(ps_args->np2_fixup & (1 << i))) continue;
1105 if (WINED3DSTT_2D != reg_maps->sampler_type[i]) {
1106 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1107 continue;
1110 fixup->idx[i] = cur++;
1114 fixup->num_consts = (cur + 1) >> 1;
1115 shader_addline(buffer, "uniform vec4 %csamplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1118 /* Declare address variables */
1119 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1121 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1124 /* Declare texture coordinate temporaries and initialize them */
1125 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1127 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1130 /* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
1131 * helper function shader that is linked in at link time
1133 if (pshader && reg_maps->shader_version.major >= 3)
1135 if (use_vs(state))
1137 shader_addline(buffer, "varying vec4 IN[%u];\n", vec4_varyings(reg_maps->shader_version.major, gl_info));
1138 } else {
1139 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
1140 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
1141 * pixel shader that reads the fixed function color into the packed input registers.
1143 shader_addline(buffer, "vec4 IN[%u];\n", vec4_varyings(reg_maps->shader_version.major, gl_info));
1147 /* Declare output register temporaries */
1148 if (shader->limits.packed_output)
1149 shader_addline(buffer, "vec4 OUT[%u];\n", shader->limits.packed_output);
1151 /* Declare temporary variables */
1152 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1154 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1157 /* Declare attributes */
1158 if (reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX)
1160 for (i = 0, map = reg_maps->input_registers; map; map >>= 1, ++i)
1162 if (map & 1) shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
1166 /* Declare loop registers aLx */
1167 for (i = 0; i < reg_maps->loop_depth; i++) {
1168 shader_addline(buffer, "int aL%u;\n", i);
1169 shader_addline(buffer, "int tmpInt%u;\n", i);
1172 /* Temporary variables for matrix operations */
1173 shader_addline(buffer, "vec4 tmp0;\n");
1174 shader_addline(buffer, "vec4 tmp1;\n");
1176 /* Local constants use a different name so they can be loaded once at shader link time
1177 * They can't be hardcoded into the shader text via LC = {x, y, z, w}; because the
1178 * float -> string conversion can cause precision loss.
1180 if (!shader->load_local_constsF)
1182 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, local_constant, entry)
1184 shader_addline(buffer, "uniform vec4 %cLC%u;\n", prefix, lconst->idx);
1188 /* Start the main program */
1189 shader_addline(buffer, "void main() {\n");
1190 if(pshader && reg_maps->vpos) {
1191 /* DirectX apps expect integer values, while OpenGL drivers add approximately 0.5. This causes
1192 * off-by-one problems as spotted by the vPos d3d9 visual test. Unfortunately the ATI cards do
1193 * not add exactly 0.5, but rather something like 0.49999999 or 0.50000001, which still causes
1194 * precision troubles when we just subtract 0.5.
1196 * To deal with that just floor() the position. This will eliminate the fraction on all cards.
1198 * TODO: Test how that behaves with multisampling once we can enable multisampling in winex11.
1200 * An advantage of floor is that it works even if the driver doesn't add 1/2. It is somewhat
1201 * questionable if 1.5, 2.5, ... are the proper values to return in gl_FragCoord, even though
1202 * coordinates specify the pixel centers instead of the pixel corners. This code will behave
1203 * correctly on drivers that returns integer values.
1205 shader_addline(buffer, "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1209 /*****************************************************************************
1210 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1212 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1213 ****************************************************************************/
1215 /* Prototypes */
1216 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1217 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1219 /** Used for opcode modifiers - They multiply the result by the specified amount */
1220 static const char * const shift_glsl_tab[] = {
1221 "", /* 0 (none) */
1222 "2.0 * ", /* 1 (x2) */
1223 "4.0 * ", /* 2 (x4) */
1224 "8.0 * ", /* 3 (x8) */
1225 "16.0 * ", /* 4 (x16) */
1226 "32.0 * ", /* 5 (x32) */
1227 "", /* 6 (x64) */
1228 "", /* 7 (x128) */
1229 "", /* 8 (d256) */
1230 "", /* 9 (d128) */
1231 "", /* 10 (d64) */
1232 "", /* 11 (d32) */
1233 "0.0625 * ", /* 12 (d16) */
1234 "0.125 * ", /* 13 (d8) */
1235 "0.25 * ", /* 14 (d4) */
1236 "0.5 * " /* 15 (d2) */
1239 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1240 static void shader_glsl_gen_modifier(DWORD src_modifier, const char *in_reg, const char *in_regswizzle, char *out_str)
1242 out_str[0] = 0;
1244 switch (src_modifier)
1246 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1247 case WINED3DSPSM_DW:
1248 case WINED3DSPSM_NONE:
1249 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1250 break;
1251 case WINED3DSPSM_NEG:
1252 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1253 break;
1254 case WINED3DSPSM_NOT:
1255 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1256 break;
1257 case WINED3DSPSM_BIAS:
1258 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1259 break;
1260 case WINED3DSPSM_BIASNEG:
1261 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1262 break;
1263 case WINED3DSPSM_SIGN:
1264 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1265 break;
1266 case WINED3DSPSM_SIGNNEG:
1267 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1268 break;
1269 case WINED3DSPSM_COMP:
1270 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1271 break;
1272 case WINED3DSPSM_X2:
1273 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1274 break;
1275 case WINED3DSPSM_X2NEG:
1276 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1277 break;
1278 case WINED3DSPSM_ABS:
1279 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1280 break;
1281 case WINED3DSPSM_ABSNEG:
1282 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1283 break;
1284 default:
1285 FIXME("Unhandled modifier %u\n", src_modifier);
1286 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1290 /** Writes the GLSL variable name that corresponds to the register that the
1291 * DX opcode parameter is trying to access */
1292 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1293 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1295 /* oPos, oFog and oPts in D3D */
1296 static const char * const hwrastout_reg_names[] = {"OUT[10]", "OUT[11].x", "OUT[11].y"};
1298 const struct wined3d_shader *shader = ins->ctx->shader;
1299 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1300 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1301 char pshader = shader_is_pshader_version(reg_maps->shader_version.type);
1303 *is_color = FALSE;
1305 switch (reg->type)
1307 case WINED3DSPR_TEMP:
1308 sprintf(register_name, "R%u", reg->idx);
1309 break;
1311 case WINED3DSPR_INPUT:
1312 /* vertex shaders */
1313 if (!pshader)
1315 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1316 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx)) *is_color = TRUE;
1317 sprintf(register_name, "attrib%u", reg->idx);
1318 break;
1321 /* pixel shaders >= 3.0 */
1322 if (reg_maps->shader_version.major >= 3)
1324 DWORD idx = shader->u.ps.input_reg_map[reg->idx];
1325 unsigned int in_count = vec4_varyings(reg_maps->shader_version.major, gl_info);
1327 if (reg->rel_addr)
1329 struct glsl_src_param rel_param;
1331 shader_glsl_add_src_param(ins, reg->rel_addr, WINED3DSP_WRITEMASK_0, &rel_param);
1333 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
1334 * operation there */
1335 if (idx)
1337 if (shader->u.ps.declared_in_count > in_count)
1339 sprintf(register_name,
1340 "((%s + %u) > %d ? (%s + %u) > %d ? gl_SecondaryColor : gl_Color : IN[%s + %u])",
1341 rel_param.param_str, idx, in_count - 1, rel_param.param_str, idx, in_count,
1342 rel_param.param_str, idx);
1344 else
1346 sprintf(register_name, "IN[%s + %u]", rel_param.param_str, idx);
1349 else
1351 if (shader->u.ps.declared_in_count > in_count)
1353 sprintf(register_name, "((%s) > %d ? (%s) > %d ? gl_SecondaryColor : gl_Color : IN[%s])",
1354 rel_param.param_str, in_count - 1, rel_param.param_str, in_count,
1355 rel_param.param_str);
1357 else
1359 sprintf(register_name, "IN[%s]", rel_param.param_str);
1363 else
1365 if (idx == in_count) sprintf(register_name, "gl_Color");
1366 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
1367 else sprintf(register_name, "IN[%u]", idx);
1370 else
1372 if (!reg->idx) strcpy(register_name, "gl_Color");
1373 else strcpy(register_name, "gl_SecondaryColor");
1374 break;
1376 break;
1378 case WINED3DSPR_CONST:
1380 const char prefix = pshader ? 'P' : 'V';
1382 /* Relative addressing */
1383 if (reg->rel_addr)
1385 struct glsl_src_param rel_param;
1386 shader_glsl_add_src_param(ins, reg->rel_addr, WINED3DSP_WRITEMASK_0, &rel_param);
1387 if (reg->idx) sprintf(register_name, "%cC[%s + %u]", prefix, rel_param.param_str, reg->idx);
1388 else sprintf(register_name, "%cC[%s]", prefix, rel_param.param_str);
1390 else
1392 if (shader_constant_is_local(shader, reg->idx))
1393 sprintf(register_name, "%cLC%u", prefix, reg->idx);
1394 else
1395 sprintf(register_name, "%cC[%u]", prefix, reg->idx);
1398 break;
1400 case WINED3DSPR_CONSTINT:
1401 if (pshader) sprintf(register_name, "PI[%u]", reg->idx);
1402 else sprintf(register_name, "VI[%u]", reg->idx);
1403 break;
1405 case WINED3DSPR_CONSTBOOL:
1406 if (pshader) sprintf(register_name, "PB[%u]", reg->idx);
1407 else sprintf(register_name, "VB[%u]", reg->idx);
1408 break;
1410 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1411 if (pshader) sprintf(register_name, "T%u", reg->idx);
1412 else sprintf(register_name, "A%u", reg->idx);
1413 break;
1415 case WINED3DSPR_LOOP:
1416 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
1417 break;
1419 case WINED3DSPR_SAMPLER:
1420 if (pshader) sprintf(register_name, "Psampler%u", reg->idx);
1421 else sprintf(register_name, "Vsampler%u", reg->idx);
1422 break;
1424 case WINED3DSPR_COLOROUT:
1425 if (reg->idx >= gl_info->limits.buffers)
1426 WARN("Write to render target %u, only %d supported.\n", reg->idx, gl_info->limits.buffers);
1428 sprintf(register_name, "gl_FragData[%u]", reg->idx);
1429 break;
1431 case WINED3DSPR_RASTOUT:
1432 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx]);
1433 break;
1435 case WINED3DSPR_DEPTHOUT:
1436 sprintf(register_name, "gl_FragDepth");
1437 break;
1439 case WINED3DSPR_ATTROUT:
1440 if (!reg->idx) sprintf(register_name, "OUT[8]");
1441 else sprintf(register_name, "OUT[9]");
1442 break;
1444 case WINED3DSPR_TEXCRDOUT:
1445 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1446 sprintf(register_name, "OUT[%u]", reg->idx);
1447 break;
1449 case WINED3DSPR_MISCTYPE:
1450 if (!reg->idx)
1452 /* vPos */
1453 sprintf(register_name, "vpos");
1455 else if (reg->idx == 1)
1457 /* Note that gl_FrontFacing is a bool, while vFace is
1458 * a float for which the sign determines front/back */
1459 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1461 else
1463 FIXME("Unhandled misctype register %d\n", reg->idx);
1464 sprintf(register_name, "unrecognized_register");
1466 break;
1468 case WINED3DSPR_IMMCONST:
1469 switch (reg->immconst_type)
1471 case WINED3D_IMMCONST_SCALAR:
1472 sprintf(register_name, "%.8e", *(const float *)reg->immconst_data);
1473 break;
1475 case WINED3D_IMMCONST_VEC4:
1476 sprintf(register_name, "vec4(%.8e, %.8e, %.8e, %.8e)",
1477 *(const float *)&reg->immconst_data[0], *(const float *)&reg->immconst_data[1],
1478 *(const float *)&reg->immconst_data[2], *(const float *)&reg->immconst_data[3]);
1479 break;
1481 default:
1482 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
1483 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
1485 break;
1487 default:
1488 FIXME("Unhandled register name Type(%d)\n", reg->type);
1489 sprintf(register_name, "unrecognized_register");
1490 break;
1494 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1496 *str++ = '.';
1497 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1498 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1499 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1500 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1501 *str = '\0';
1504 /* Get the GLSL write mask for the destination register */
1505 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1507 DWORD mask = param->write_mask;
1509 if (shader_is_scalar(&param->reg))
1511 mask = WINED3DSP_WRITEMASK_0;
1512 *write_mask = '\0';
1514 else
1516 shader_glsl_write_mask_to_str(mask, write_mask);
1519 return mask;
1522 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1523 unsigned int size = 0;
1525 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1526 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1527 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1528 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1530 return size;
1533 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
1535 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1536 * but addressed as "rgba". To fix this we need to swap the register's x
1537 * and z components. */
1538 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1540 *str++ = '.';
1541 /* swizzle bits fields: wwzzyyxx */
1542 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
1543 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
1544 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
1545 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
1546 *str = '\0';
1549 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
1550 BOOL fixup, DWORD mask, char *swizzle_str)
1552 if (shader_is_scalar(&param->reg))
1553 *swizzle_str = '\0';
1554 else
1555 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
1558 /* From a given parameter token, generate the corresponding GLSL string.
1559 * Also, return the actual register name and swizzle in case the
1560 * caller needs this information as well. */
1561 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1562 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
1564 BOOL is_color = FALSE;
1565 char swizzle_str[6];
1567 glsl_src->reg_name[0] = '\0';
1568 glsl_src->param_str[0] = '\0';
1569 swizzle_str[0] = '\0';
1571 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
1572 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
1573 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
1576 /* From a given parameter token, generate the corresponding GLSL string.
1577 * Also, return the actual register name and swizzle in case the
1578 * caller needs this information as well. */
1579 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1580 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
1582 BOOL is_color = FALSE;
1584 glsl_dst->mask_str[0] = '\0';
1585 glsl_dst->reg_name[0] = '\0';
1587 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
1588 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1591 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1592 static DWORD shader_glsl_append_dst_ext(struct wined3d_shader_buffer *buffer,
1593 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst)
1595 struct glsl_dst_param glsl_dst;
1596 DWORD mask;
1598 mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst);
1599 if (mask) shader_addline(buffer, "%s%s = %s(", glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1601 return mask;
1604 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1605 static DWORD shader_glsl_append_dst(struct wined3d_shader_buffer *buffer, const struct wined3d_shader_instruction *ins)
1607 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
1610 /** Process GLSL instruction modifiers */
1611 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1613 struct glsl_dst_param dst_param;
1614 DWORD modifiers;
1616 if (!ins->dst_count) return;
1618 modifiers = ins->dst[0].modifiers;
1619 if (!modifiers) return;
1621 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
1623 if (modifiers & WINED3DSPDM_SATURATE)
1625 /* _SAT means to clamp the value of the register to between 0 and 1 */
1626 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1627 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1630 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
1632 FIXME("_centroid modifier not handled\n");
1635 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
1637 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1641 static inline const char *shader_get_comp_op(DWORD op)
1643 switch (op) {
1644 case COMPARISON_GT: return ">";
1645 case COMPARISON_EQ: return "==";
1646 case COMPARISON_GE: return ">=";
1647 case COMPARISON_LT: return "<";
1648 case COMPARISON_NE: return "!=";
1649 case COMPARISON_LE: return "<=";
1650 default:
1651 FIXME("Unrecognized comparison value: %u\n", op);
1652 return "(\?\?)";
1656 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
1657 DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
1659 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ctx->reg_maps->sampler_type[sampler_idx];
1660 const struct wined3d_gl_info *gl_info = ctx->gl_info;
1661 BOOL shadow = shader_is_pshader_version(ctx->reg_maps->shader_version.type)
1662 && (((const struct shader_glsl_ctx_priv *)ctx->backend_data)->cur_ps_args->shadow & (1 << sampler_idx));
1663 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
1664 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_RECT;
1665 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
1666 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
1668 /* Note that there's no such thing as a projected cube texture. */
1669 switch(sampler_type) {
1670 case WINED3DSTT_1D:
1671 if (shadow)
1673 if (lod)
1675 sample_function->name = projected ? "shadow1DProjLod" : "shadow1DLod";
1677 else if (grad)
1679 if (gl_info->supported[EXT_GPU_SHADER4])
1680 sample_function->name = projected ? "shadow1DProjGrad" : "shadow1DGrad";
1681 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1682 sample_function->name = projected ? "shadow1DProjGradARB" : "shadow1DGradARB";
1683 else
1685 FIXME("Unsupported 1D shadow grad function.\n");
1686 sample_function->name = "unsupported1DGrad";
1689 else
1691 sample_function->name = projected ? "shadow1DProj" : "shadow1D";
1693 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1695 else
1697 if (lod)
1699 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
1701 else if (grad)
1703 if (gl_info->supported[EXT_GPU_SHADER4])
1704 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
1705 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1706 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
1707 else
1709 FIXME("Unsupported 1D grad function.\n");
1710 sample_function->name = "unsupported1DGrad";
1713 else
1715 sample_function->name = projected ? "texture1DProj" : "texture1D";
1717 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1719 break;
1721 case WINED3DSTT_2D:
1722 if (shadow)
1724 if (texrect)
1726 if (lod)
1728 sample_function->name = projected ? "shadow2DRectProjLod" : "shadow2DRectLod";
1730 else if (grad)
1732 if (gl_info->supported[EXT_GPU_SHADER4])
1733 sample_function->name = projected ? "shadow2DRectProjGrad" : "shadow2DRectGrad";
1734 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1735 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
1736 else
1738 FIXME("Unsupported RECT shadow grad function.\n");
1739 sample_function->name = "unsupported2DRectGrad";
1742 else
1744 sample_function->name = projected ? "shadow2DRectProj" : "shadow2DRect";
1747 else
1749 if (lod)
1751 sample_function->name = projected ? "shadow2DProjLod" : "shadow2DLod";
1753 else if (grad)
1755 if (gl_info->supported[EXT_GPU_SHADER4])
1756 sample_function->name = projected ? "shadow2DProjGrad" : "shadow2DGrad";
1757 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1758 sample_function->name = projected ? "shadow2DProjGradARB" : "shadow2DGradARB";
1759 else
1761 FIXME("Unsupported 2D shadow grad function.\n");
1762 sample_function->name = "unsupported2DGrad";
1765 else
1767 sample_function->name = projected ? "shadow2DProj" : "shadow2D";
1770 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1772 else
1774 if (texrect)
1776 if (lod)
1778 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
1780 else if (grad)
1782 if (gl_info->supported[EXT_GPU_SHADER4])
1783 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
1784 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1785 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
1786 else
1788 FIXME("Unsupported RECT grad function.\n");
1789 sample_function->name = "unsupported2DRectGrad";
1792 else
1794 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
1797 else
1799 if (lod)
1801 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
1803 else if (grad)
1805 if (gl_info->supported[EXT_GPU_SHADER4])
1806 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
1807 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1808 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
1809 else
1811 FIXME("Unsupported 2D grad function.\n");
1812 sample_function->name = "unsupported2DGrad";
1815 else
1817 sample_function->name = projected ? "texture2DProj" : "texture2D";
1820 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1822 break;
1824 case WINED3DSTT_CUBE:
1825 if (shadow)
1827 FIXME("Unsupported Cube shadow function.\n");
1828 sample_function->name = "unsupportedCubeShadow";
1829 sample_function->coord_mask = 0;
1831 else
1833 if (lod)
1835 sample_function->name = "textureCubeLod";
1837 else if (grad)
1839 if (gl_info->supported[EXT_GPU_SHADER4])
1840 sample_function->name = "textureCubeGrad";
1841 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1842 sample_function->name = "textureCubeGradARB";
1843 else
1845 FIXME("Unsupported Cube grad function.\n");
1846 sample_function->name = "unsupportedCubeGrad";
1849 else
1851 sample_function->name = "textureCube";
1853 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1855 break;
1857 case WINED3DSTT_VOLUME:
1858 if (shadow)
1860 FIXME("Unsupported 3D shadow function.\n");
1861 sample_function->name = "unsupported3DShadow";
1862 sample_function->coord_mask = 0;
1864 else
1866 if (lod)
1868 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
1870 else if (grad)
1872 if (gl_info->supported[EXT_GPU_SHADER4])
1873 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
1874 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1875 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
1876 else
1878 FIXME("Unsupported 3D grad function.\n");
1879 sample_function->name = "unsupported3DGrad";
1882 else
1884 sample_function->name = projected ? "texture3DProj" : "texture3D";
1886 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1888 break;
1890 default:
1891 sample_function->name = "";
1892 sample_function->coord_mask = 0;
1893 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
1894 break;
1898 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
1899 BOOL sign_fixup, enum fixup_channel_source channel_source)
1901 switch(channel_source)
1903 case CHANNEL_SOURCE_ZERO:
1904 strcat(arguments, "0.0");
1905 break;
1907 case CHANNEL_SOURCE_ONE:
1908 strcat(arguments, "1.0");
1909 break;
1911 case CHANNEL_SOURCE_X:
1912 strcat(arguments, reg_name);
1913 strcat(arguments, ".x");
1914 break;
1916 case CHANNEL_SOURCE_Y:
1917 strcat(arguments, reg_name);
1918 strcat(arguments, ".y");
1919 break;
1921 case CHANNEL_SOURCE_Z:
1922 strcat(arguments, reg_name);
1923 strcat(arguments, ".z");
1924 break;
1926 case CHANNEL_SOURCE_W:
1927 strcat(arguments, reg_name);
1928 strcat(arguments, ".w");
1929 break;
1931 default:
1932 FIXME("Unhandled channel source %#x\n", channel_source);
1933 strcat(arguments, "undefined");
1934 break;
1937 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
1940 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
1942 struct wined3d_shader_dst_param dst;
1943 unsigned int mask_size, remaining;
1944 struct glsl_dst_param dst_param;
1945 char arguments[256];
1946 DWORD mask;
1948 mask = 0;
1949 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) mask |= WINED3DSP_WRITEMASK_0;
1950 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) mask |= WINED3DSP_WRITEMASK_1;
1951 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) mask |= WINED3DSP_WRITEMASK_2;
1952 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) mask |= WINED3DSP_WRITEMASK_3;
1953 mask &= ins->dst[0].write_mask;
1955 if (!mask) return; /* Nothing to do */
1957 if (is_complex_fixup(fixup))
1959 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
1960 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
1961 return;
1964 mask_size = shader_glsl_get_write_mask_size(mask);
1966 dst = ins->dst[0];
1967 dst.write_mask = mask;
1968 shader_glsl_add_dst_param(ins, &dst, &dst_param);
1970 arguments[0] = '\0';
1971 remaining = mask_size;
1972 if (mask & WINED3DSP_WRITEMASK_0)
1974 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.x_sign_fixup, fixup.x_source);
1975 if (--remaining) strcat(arguments, ", ");
1977 if (mask & WINED3DSP_WRITEMASK_1)
1979 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.y_sign_fixup, fixup.y_source);
1980 if (--remaining) strcat(arguments, ", ");
1982 if (mask & WINED3DSP_WRITEMASK_2)
1984 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.z_sign_fixup, fixup.z_source);
1985 if (--remaining) strcat(arguments, ", ");
1987 if (mask & WINED3DSP_WRITEMASK_3)
1989 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.w_sign_fixup, fixup.w_source);
1990 if (--remaining) strcat(arguments, ", ");
1993 if (mask_size > 1)
1995 shader_addline(ins->ctx->buffer, "%s%s = vec%u(%s);\n",
1996 dst_param.reg_name, dst_param.mask_str, mask_size, arguments);
1998 else
2000 shader_addline(ins->ctx->buffer, "%s%s = %s;\n", dst_param.reg_name, dst_param.mask_str, arguments);
2004 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2005 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2006 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2008 const char *sampler_base;
2009 char dst_swizzle[6];
2010 struct color_fixup_desc fixup;
2011 BOOL np2_fixup = FALSE;
2012 va_list args;
2014 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2016 if (shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type))
2018 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2019 fixup = priv->cur_ps_args->color_fixup[sampler];
2020 sampler_base = "Psampler";
2022 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2023 if(bias) {
2024 FIXME("Biased sampling from NP2 textures is unsupported\n");
2025 } else {
2026 np2_fixup = TRUE;
2029 } else {
2030 sampler_base = "Vsampler";
2031 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
2034 shader_glsl_append_dst(ins->ctx->buffer, ins);
2036 shader_addline(ins->ctx->buffer, "%s(%s%u, ", sample_function->name, sampler_base, sampler);
2038 va_start(args, coord_reg_fmt);
2039 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2040 va_end(args);
2042 if(bias) {
2043 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2044 } else {
2045 if (np2_fixup) {
2046 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2047 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2049 shader_addline(ins->ctx->buffer, " * PsamplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2050 (idx % 2) ? "zw" : "xy", dst_swizzle);
2051 } else if(dx && dy) {
2052 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2053 } else {
2054 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2058 if(!is_identity_fixup(fixup)) {
2059 shader_glsl_color_correction(ins, fixup);
2063 /*****************************************************************************
2064 * Begin processing individual instruction opcodes
2065 ****************************************************************************/
2067 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
2068 static void shader_glsl_arith(const struct wined3d_shader_instruction *ins)
2070 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2071 struct glsl_src_param src0_param;
2072 struct glsl_src_param src1_param;
2073 DWORD write_mask;
2074 char op;
2076 /* Determine the GLSL operator to use based on the opcode */
2077 switch (ins->handler_idx)
2079 case WINED3DSIH_MUL: op = '*'; break;
2080 case WINED3DSIH_ADD: op = '+'; break;
2081 case WINED3DSIH_SUB: op = '-'; break;
2082 default:
2083 op = ' ';
2084 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2085 break;
2088 write_mask = shader_glsl_append_dst(buffer, ins);
2089 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2090 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2091 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
2094 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2095 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2097 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2098 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2099 struct glsl_src_param src0_param;
2100 DWORD write_mask;
2102 write_mask = shader_glsl_append_dst(buffer, ins);
2103 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2105 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2106 * shader versions WINED3DSIO_MOVA is used for this. */
2107 if (ins->ctx->reg_maps->shader_version.major == 1
2108 && !shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type)
2109 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2111 /* This is a simple floor() */
2112 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2113 if (mask_size > 1) {
2114 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2115 } else {
2116 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2119 else if(ins->handler_idx == WINED3DSIH_MOVA)
2121 /* We need to *round* to the nearest int here. */
2122 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2124 if (gl_info->supported[EXT_GPU_SHADER4])
2126 if (mask_size > 1)
2127 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2128 else
2129 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2131 else
2133 if (mask_size > 1)
2134 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2135 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2136 else
2137 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2138 src0_param.param_str, src0_param.param_str);
2141 else
2143 shader_addline(buffer, "%s);\n", src0_param.param_str);
2147 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2148 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2150 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2151 struct glsl_src_param src0_param;
2152 struct glsl_src_param src1_param;
2153 DWORD dst_write_mask, src_write_mask;
2154 unsigned int dst_size = 0;
2156 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2157 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2159 /* dp3 works on vec3, dp4 on vec4 */
2160 if (ins->handler_idx == WINED3DSIH_DP4)
2162 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2163 } else {
2164 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2167 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2168 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2170 if (dst_size > 1) {
2171 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2172 } else {
2173 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2177 /* Note that this instruction has some restrictions. The destination write mask
2178 * can't contain the w component, and the source swizzles have to be .xyzw */
2179 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2181 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2182 struct glsl_src_param src0_param;
2183 struct glsl_src_param src1_param;
2184 char dst_mask[6];
2186 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2187 shader_glsl_append_dst(ins->ctx->buffer, ins);
2188 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2189 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2190 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2193 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2194 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2195 * GLSL uses the value as-is. */
2196 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2198 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2199 struct glsl_src_param src0_param;
2200 struct glsl_src_param src1_param;
2201 DWORD dst_write_mask;
2202 unsigned int dst_size;
2204 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2205 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2207 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2208 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2210 if (dst_size > 1)
2212 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2213 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
2215 else
2217 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
2218 src1_param.param_str, src0_param.param_str, src1_param.param_str);
2222 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
2223 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
2224 * GLSL uses the value as-is. */
2225 static void shader_glsl_log(const struct wined3d_shader_instruction *ins)
2227 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2228 struct glsl_src_param src0_param;
2229 DWORD dst_write_mask;
2230 unsigned int dst_size;
2232 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2233 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2235 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2237 if (dst_size > 1)
2239 shader_addline(buffer, "vec%u(log2(abs(%s))));\n",
2240 dst_size, src0_param.param_str);
2242 else
2244 shader_addline(buffer, "log2(abs(%s)));\n",
2245 src0_param.param_str);
2249 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2250 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
2252 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2253 struct glsl_src_param src_param;
2254 const char *instruction;
2255 DWORD write_mask;
2256 unsigned i;
2258 /* Determine the GLSL function to use based on the opcode */
2259 /* TODO: Possibly make this a table for faster lookups */
2260 switch (ins->handler_idx)
2262 case WINED3DSIH_MIN: instruction = "min"; break;
2263 case WINED3DSIH_MAX: instruction = "max"; break;
2264 case WINED3DSIH_ABS: instruction = "abs"; break;
2265 case WINED3DSIH_FRC: instruction = "fract"; break;
2266 case WINED3DSIH_EXP: instruction = "exp2"; break;
2267 case WINED3DSIH_DSX: instruction = "dFdx"; break;
2268 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
2269 default: instruction = "";
2270 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2271 break;
2274 write_mask = shader_glsl_append_dst(buffer, ins);
2276 shader_addline(buffer, "%s(", instruction);
2278 if (ins->src_count)
2280 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2281 shader_addline(buffer, "%s", src_param.param_str);
2282 for (i = 1; i < ins->src_count; ++i)
2284 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
2285 shader_addline(buffer, ", %s", src_param.param_str);
2289 shader_addline(buffer, "));\n");
2292 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
2294 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2295 struct glsl_src_param src_param;
2296 unsigned int mask_size;
2297 DWORD write_mask;
2298 char dst_mask[6];
2300 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
2301 mask_size = shader_glsl_get_write_mask_size(write_mask);
2302 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2304 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
2305 src_param.param_str, src_param.param_str);
2306 shader_glsl_append_dst(buffer, ins);
2308 if (mask_size > 1)
2310 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
2311 mask_size, src_param.param_str);
2313 else
2315 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
2316 src_param.param_str);
2320 /** Process the WINED3DSIO_EXPP instruction in GLSL:
2321 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2322 * dst.x = 2^(floor(src))
2323 * dst.y = src - floor(src)
2324 * dst.z = 2^src (partial precision is allowed, but optional)
2325 * dst.w = 1.0;
2326 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2327 * dst = 2^src; (partial precision is allowed, but optional)
2329 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
2331 struct glsl_src_param src_param;
2333 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
2335 if (ins->ctx->reg_maps->shader_version.major < 2)
2337 char dst_mask[6];
2339 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
2340 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
2341 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
2342 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
2344 shader_glsl_append_dst(ins->ctx->buffer, ins);
2345 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2346 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
2347 } else {
2348 DWORD write_mask;
2349 unsigned int mask_size;
2351 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2352 mask_size = shader_glsl_get_write_mask_size(write_mask);
2354 if (mask_size > 1) {
2355 shader_addline(ins->ctx->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
2356 } else {
2357 shader_addline(ins->ctx->buffer, "exp2(%s));\n", src_param.param_str);
2362 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
2363 static void shader_glsl_rcp(const struct wined3d_shader_instruction *ins)
2365 struct glsl_src_param src_param;
2366 DWORD write_mask;
2367 unsigned int mask_size;
2369 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2370 mask_size = shader_glsl_get_write_mask_size(write_mask);
2371 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2373 if (mask_size > 1)
2375 shader_addline(ins->ctx->buffer, "vec%u(1.0 / %s));\n",
2376 mask_size, src_param.param_str);
2378 else
2380 shader_addline(ins->ctx->buffer, "1.0 / %s);\n",
2381 src_param.param_str);
2385 static void shader_glsl_rsq(const struct wined3d_shader_instruction *ins)
2387 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2388 struct glsl_src_param src_param;
2389 DWORD write_mask;
2390 unsigned int mask_size;
2392 write_mask = shader_glsl_append_dst(buffer, ins);
2393 mask_size = shader_glsl_get_write_mask_size(write_mask);
2395 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2397 if (mask_size > 1)
2399 shader_addline(buffer, "vec%u(inversesqrt(abs(%s))));\n",
2400 mask_size, src_param.param_str);
2402 else
2404 shader_addline(buffer, "inversesqrt(abs(%s)));\n",
2405 src_param.param_str);
2409 /** Process signed comparison opcodes in GLSL. */
2410 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
2412 struct glsl_src_param src0_param;
2413 struct glsl_src_param src1_param;
2414 DWORD write_mask;
2415 unsigned int mask_size;
2417 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2418 mask_size = shader_glsl_get_write_mask_size(write_mask);
2419 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2420 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2422 if (mask_size > 1) {
2423 const char *compare;
2425 switch(ins->handler_idx)
2427 case WINED3DSIH_SLT: compare = "lessThan"; break;
2428 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
2429 default: compare = "";
2430 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2433 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
2434 src0_param.param_str, src1_param.param_str);
2435 } else {
2436 switch(ins->handler_idx)
2438 case WINED3DSIH_SLT:
2439 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2440 * to return 0.0 but step returns 1.0 because step is not < x
2441 * An alternative is a bvec compare padded with an unused second component.
2442 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2443 * issue. Playing with not() is not possible either because not() does not accept
2444 * a scalar.
2446 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
2447 src0_param.param_str, src1_param.param_str);
2448 break;
2449 case WINED3DSIH_SGE:
2450 /* Here we can use the step() function and safe a conditional */
2451 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
2452 break;
2453 default:
2454 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2460 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
2461 static void shader_glsl_cmp(const struct wined3d_shader_instruction *ins)
2463 struct glsl_src_param src0_param;
2464 struct glsl_src_param src1_param;
2465 struct glsl_src_param src2_param;
2466 DWORD write_mask, cmp_channel = 0;
2467 unsigned int i, j;
2468 char mask_char[6];
2469 BOOL temp_destination = FALSE;
2471 if (shader_is_scalar(&ins->src[0].reg))
2473 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2475 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2476 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2477 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2479 shader_addline(ins->ctx->buffer, "%s >= 0.0 ? %s : %s);\n",
2480 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2481 } else {
2482 DWORD dst_mask = ins->dst[0].write_mask;
2483 struct wined3d_shader_dst_param dst = ins->dst[0];
2485 /* Cycle through all source0 channels */
2486 for (i=0; i<4; i++) {
2487 write_mask = 0;
2488 /* Find the destination channels which use the current source0 channel */
2489 for (j=0; j<4; j++) {
2490 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2492 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2493 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2496 dst.write_mask = dst_mask & write_mask;
2498 /* Splitting the cmp instruction up in multiple lines imposes a problem:
2499 * The first lines may overwrite source parameters of the following lines.
2500 * Deal with that by using a temporary destination register if needed
2502 if ((ins->src[0].reg.idx == ins->dst[0].reg.idx
2503 && ins->src[0].reg.type == ins->dst[0].reg.type)
2504 || (ins->src[1].reg.idx == ins->dst[0].reg.idx
2505 && ins->src[1].reg.type == ins->dst[0].reg.type)
2506 || (ins->src[2].reg.idx == ins->dst[0].reg.idx
2507 && ins->src[2].reg.type == ins->dst[0].reg.type))
2509 write_mask = shader_glsl_get_write_mask(&dst, mask_char);
2510 if (!write_mask) continue;
2511 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
2512 temp_destination = TRUE;
2513 } else {
2514 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst);
2515 if (!write_mask) continue;
2518 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2519 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2520 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2522 shader_addline(ins->ctx->buffer, "%s >= 0.0 ? %s : %s);\n",
2523 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2526 if(temp_destination) {
2527 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
2528 shader_glsl_append_dst(ins->ctx->buffer, ins);
2529 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
2535 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2536 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2537 * the compare is done per component of src0. */
2538 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
2540 struct wined3d_shader_dst_param dst;
2541 struct glsl_src_param src0_param;
2542 struct glsl_src_param src1_param;
2543 struct glsl_src_param src2_param;
2544 DWORD write_mask, cmp_channel = 0;
2545 unsigned int i, j;
2546 DWORD dst_mask;
2547 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2548 ins->ctx->reg_maps->shader_version.minor);
2550 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
2552 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2553 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2554 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2555 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2557 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
2558 if (ins->coissue)
2560 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
2561 } else {
2562 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2563 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2565 return;
2567 /* Cycle through all source0 channels */
2568 dst_mask = ins->dst[0].write_mask;
2569 dst = ins->dst[0];
2570 for (i=0; i<4; i++) {
2571 write_mask = 0;
2572 /* Find the destination channels which use the current source0 channel */
2573 for (j=0; j<4; j++) {
2574 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2576 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2577 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2581 dst.write_mask = dst_mask & write_mask;
2582 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst);
2583 if (!write_mask) continue;
2585 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2586 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2587 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2589 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2590 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2594 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2595 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
2597 struct glsl_src_param src0_param;
2598 struct glsl_src_param src1_param;
2599 struct glsl_src_param src2_param;
2600 DWORD write_mask;
2602 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2603 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2604 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2605 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2606 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
2607 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2610 /* Handles transforming all WINED3DSIO_M?x? opcodes for
2611 Vertex shaders to GLSL codes */
2612 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
2614 int i;
2615 int nComponents = 0;
2616 struct wined3d_shader_dst_param tmp_dst = {{0}};
2617 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
2618 struct wined3d_shader_instruction tmp_ins;
2620 memset(&tmp_ins, 0, sizeof(tmp_ins));
2622 /* Set constants for the temporary argument */
2623 tmp_ins.ctx = ins->ctx;
2624 tmp_ins.dst_count = 1;
2625 tmp_ins.dst = &tmp_dst;
2626 tmp_ins.src_count = 2;
2627 tmp_ins.src = tmp_src;
2629 switch(ins->handler_idx)
2631 case WINED3DSIH_M4x4:
2632 nComponents = 4;
2633 tmp_ins.handler_idx = WINED3DSIH_DP4;
2634 break;
2635 case WINED3DSIH_M4x3:
2636 nComponents = 3;
2637 tmp_ins.handler_idx = WINED3DSIH_DP4;
2638 break;
2639 case WINED3DSIH_M3x4:
2640 nComponents = 4;
2641 tmp_ins.handler_idx = WINED3DSIH_DP3;
2642 break;
2643 case WINED3DSIH_M3x3:
2644 nComponents = 3;
2645 tmp_ins.handler_idx = WINED3DSIH_DP3;
2646 break;
2647 case WINED3DSIH_M3x2:
2648 nComponents = 2;
2649 tmp_ins.handler_idx = WINED3DSIH_DP3;
2650 break;
2651 default:
2652 break;
2655 tmp_dst = ins->dst[0];
2656 tmp_src[0] = ins->src[0];
2657 tmp_src[1] = ins->src[1];
2658 for (i = 0; i < nComponents; ++i)
2660 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
2661 shader_glsl_dot(&tmp_ins);
2662 ++tmp_src[1].reg.idx;
2667 The LRP instruction performs a component-wise linear interpolation
2668 between the second and third operands using the first operand as the
2669 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
2670 This is equivalent to mix(src2, src1, src0);
2672 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
2674 struct glsl_src_param src0_param;
2675 struct glsl_src_param src1_param;
2676 struct glsl_src_param src2_param;
2677 DWORD write_mask;
2679 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2681 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2682 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2683 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2685 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
2686 src2_param.param_str, src1_param.param_str, src0_param.param_str);
2689 /** Process the WINED3DSIO_LIT instruction in GLSL:
2690 * dst.x = dst.w = 1.0
2691 * dst.y = (src0.x > 0) ? src0.x
2692 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
2693 * where src.w is clamped at +- 128
2695 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
2697 struct glsl_src_param src0_param;
2698 struct glsl_src_param src1_param;
2699 struct glsl_src_param src3_param;
2700 char dst_mask[6];
2702 shader_glsl_append_dst(ins->ctx->buffer, ins);
2703 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2705 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2706 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
2707 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
2709 /* The sdk specifies the instruction like this
2710 * dst.x = 1.0;
2711 * if(src.x > 0.0) dst.y = src.x
2712 * else dst.y = 0.0.
2713 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
2714 * else dst.z = 0.0;
2715 * dst.w = 1.0;
2716 * (where power = src.w clamped between -128 and 128)
2718 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
2719 * dst.x = 1.0 ... No further explanation needed
2720 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
2721 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
2722 * dst.w = 1.0. ... Nothing fancy.
2724 * So we still have one conditional in there. So do this:
2725 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
2727 * 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),
2728 * 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.
2729 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
2731 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
2732 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
2733 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
2735 shader_addline(ins->ctx->buffer,
2736 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
2737 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
2738 src0_param.param_str, src3_param.param_str, src1_param.param_str,
2739 src0_param.param_str, src3_param.param_str, dst_mask);
2742 /** Process the WINED3DSIO_DST instruction in GLSL:
2743 * dst.x = 1.0
2744 * dst.y = src0.x * src0.y
2745 * dst.z = src0.z
2746 * dst.w = src1.w
2748 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
2750 struct glsl_src_param src0y_param;
2751 struct glsl_src_param src0z_param;
2752 struct glsl_src_param src1y_param;
2753 struct glsl_src_param src1w_param;
2754 char dst_mask[6];
2756 shader_glsl_append_dst(ins->ctx->buffer, ins);
2757 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2759 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
2760 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
2761 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
2762 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
2764 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
2765 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
2768 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
2769 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
2770 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
2772 * dst.x = cos(src0.?)
2773 * dst.y = sin(src0.?)
2774 * dst.z = dst.z
2775 * dst.w = dst.w
2777 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
2779 struct glsl_src_param src0_param;
2780 DWORD write_mask;
2782 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2783 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2785 switch (write_mask) {
2786 case WINED3DSP_WRITEMASK_0:
2787 shader_addline(ins->ctx->buffer, "cos(%s));\n", src0_param.param_str);
2788 break;
2790 case WINED3DSP_WRITEMASK_1:
2791 shader_addline(ins->ctx->buffer, "sin(%s));\n", src0_param.param_str);
2792 break;
2794 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
2795 shader_addline(ins->ctx->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
2796 break;
2798 default:
2799 ERR("Write mask should be .x, .y or .xy\n");
2800 break;
2804 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
2805 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
2806 * generate invalid code
2808 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
2810 struct glsl_src_param src0_param;
2811 DWORD write_mask;
2813 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2814 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2816 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
2819 /** Process the WINED3DSIO_LOOP instruction in GLSL:
2820 * Start a for() loop where src1.y is the initial value of aL,
2821 * increment aL by src1.z for a total of src1.x iterations.
2822 * Need to use a temporary variable for this operation.
2824 /* FIXME: I don't think nested loops will work correctly this way. */
2825 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
2827 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
2828 const struct wined3d_shader *shader = ins->ctx->shader;
2829 struct glsl_src_param src1_param;
2830 const DWORD *control_values = NULL;
2831 const local_constant *constant;
2833 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
2835 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
2836 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
2837 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
2838 * addressing.
2840 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
2842 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, local_constant, entry)
2844 if (constant->idx == ins->src[1].reg.idx)
2846 control_values = constant->value;
2847 break;
2852 if (control_values)
2854 struct wined3d_shader_loop_control loop_control;
2855 loop_control.count = control_values[0];
2856 loop_control.start = control_values[1];
2857 loop_control.step = (int)control_values[2];
2859 if (loop_control.step > 0)
2861 shader_addline(ins->ctx->buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d) {\n",
2862 loop_state->current_depth, loop_control.start,
2863 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
2864 loop_state->current_depth, loop_control.step);
2866 else if (loop_control.step < 0)
2868 shader_addline(ins->ctx->buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d) {\n",
2869 loop_state->current_depth, loop_control.start,
2870 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
2871 loop_state->current_depth, loop_control.step);
2873 else
2875 shader_addline(ins->ctx->buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++) {\n",
2876 loop_state->current_depth, loop_control.start, loop_state->current_depth,
2877 loop_state->current_depth, loop_control.count,
2878 loop_state->current_depth);
2880 } else {
2881 shader_addline(ins->ctx->buffer,
2882 "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
2883 loop_state->current_depth, loop_state->current_reg,
2884 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
2885 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
2888 ++loop_state->current_depth;
2889 ++loop_state->current_reg;
2892 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
2894 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
2896 shader_addline(ins->ctx->buffer, "}\n");
2898 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
2900 --loop_state->current_depth;
2901 --loop_state->current_reg;
2904 if (ins->handler_idx == WINED3DSIH_ENDREP)
2906 --loop_state->current_depth;
2910 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
2912 const struct wined3d_shader *shader = ins->ctx->shader;
2913 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
2914 struct glsl_src_param src0_param;
2915 const DWORD *control_values = NULL;
2916 const local_constant *constant;
2918 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
2919 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
2921 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, local_constant, entry)
2923 if (constant->idx == ins->src[0].reg.idx)
2925 control_values = constant->value;
2926 break;
2931 if (control_values)
2933 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
2934 loop_state->current_depth, loop_state->current_depth,
2935 control_values[0], loop_state->current_depth);
2937 else
2939 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2940 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2941 loop_state->current_depth, loop_state->current_depth,
2942 src0_param.param_str, loop_state->current_depth);
2945 ++loop_state->current_depth;
2948 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
2950 struct glsl_src_param src0_param;
2952 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2953 shader_addline(ins->ctx->buffer, "if (%s) {\n", src0_param.param_str);
2956 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
2958 struct glsl_src_param src0_param;
2959 struct glsl_src_param src1_param;
2961 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2962 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2964 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
2965 src0_param.param_str, shader_get_comp_op(ins->flags), src1_param.param_str);
2968 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
2970 shader_addline(ins->ctx->buffer, "} else {\n");
2973 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
2975 shader_addline(ins->ctx->buffer, "break;\n");
2978 /* FIXME: According to MSDN the compare is done per component. */
2979 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
2981 struct glsl_src_param src0_param;
2982 struct glsl_src_param src1_param;
2984 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2985 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2987 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
2988 src0_param.param_str, shader_get_comp_op(ins->flags), src1_param.param_str);
2991 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
2993 shader_addline(ins->ctx->buffer, "}\n");
2994 shader_addline(ins->ctx->buffer, "void subroutine%u () {\n", ins->src[0].reg.idx);
2997 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
2999 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx);
3002 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3004 struct glsl_src_param src1_param;
3006 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3007 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n", src1_param.param_str, ins->src[0].reg.idx);
3010 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3012 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3013 * function only suppresses the unhandled instruction warning
3017 /*********************************************
3018 * Pixel Shader Specific Code begins here
3019 ********************************************/
3020 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3022 const struct wined3d_shader *shader = ins->ctx->shader;
3023 struct wined3d_device *device = shader->device;
3024 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3025 ins->ctx->reg_maps->shader_version.minor);
3026 struct glsl_sample_function sample_function;
3027 const struct wined3d_texture *texture;
3028 DWORD sample_flags = 0;
3029 DWORD sampler_idx;
3030 DWORD mask = 0, swizzle;
3032 /* 1.0-1.4: Use destination register as sampler source.
3033 * 2.0+: Use provided sampler source. */
3034 if (shader_version < WINED3D_SHADER_VERSION(2,0)) sampler_idx = ins->dst[0].reg.idx;
3035 else sampler_idx = ins->src[1].reg.idx;
3036 texture = device->stateBlock->state.textures[sampler_idx];
3038 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3040 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3041 DWORD flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3042 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3043 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3045 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3046 if (flags & WINED3D_PSARGS_PROJECTED && sampler_type != WINED3DSTT_CUBE) {
3047 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3048 switch (flags & ~WINED3D_PSARGS_PROJECTED) {
3049 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
3050 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
3051 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
3052 case WINED3DTTFF_COUNT4:
3053 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
3057 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3059 DWORD src_mod = ins->src[0].modifiers;
3061 if (src_mod == WINED3DSPSM_DZ) {
3062 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3063 mask = WINED3DSP_WRITEMASK_2;
3064 } else if (src_mod == WINED3DSPSM_DW) {
3065 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3066 mask = WINED3DSP_WRITEMASK_3;
3068 } else {
3069 if (ins->flags & WINED3DSI_TEXLD_PROJECT)
3071 /* ps 2.0 texldp instruction always divides by the fourth component. */
3072 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3073 mask = WINED3DSP_WRITEMASK_3;
3077 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
3078 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3080 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3081 mask |= sample_function.coord_mask;
3083 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3084 else swizzle = ins->src[1].swizzle;
3086 /* 1.0-1.3: Use destination register as coordinate source.
3087 1.4+: Use provided coordinate source register. */
3088 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3090 char coord_mask[6];
3091 shader_glsl_write_mask_to_str(mask, coord_mask);
3092 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3093 "T%u%s", sampler_idx, coord_mask);
3095 else
3097 struct glsl_src_param coord_param;
3098 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3099 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3101 struct glsl_src_param bias;
3102 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3103 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3104 "%s", coord_param.param_str);
3105 } else {
3106 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3107 "%s", coord_param.param_str);
3112 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3114 const struct wined3d_shader *shader = ins->ctx->shader;
3115 struct wined3d_device *device = shader->device;
3116 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3117 struct glsl_src_param coord_param, dx_param, dy_param;
3118 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3119 struct glsl_sample_function sample_function;
3120 DWORD sampler_idx;
3121 DWORD swizzle = ins->src[1].swizzle;
3122 const struct wined3d_texture *texture;
3124 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3126 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3127 shader_glsl_tex(ins);
3128 return;
3131 sampler_idx = ins->src[1].reg.idx;
3132 texture = device->stateBlock->state.textures[sampler_idx];
3133 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
3134 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3136 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3137 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3138 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3139 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3141 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3142 "%s", coord_param.param_str);
3145 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3147 const struct wined3d_shader *shader = ins->ctx->shader;
3148 struct wined3d_device *device = shader->device;
3149 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3150 struct glsl_src_param coord_param, lod_param;
3151 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3152 struct glsl_sample_function sample_function;
3153 DWORD sampler_idx;
3154 DWORD swizzle = ins->src[1].swizzle;
3155 const struct wined3d_texture *texture;
3157 sampler_idx = ins->src[1].reg.idx;
3158 texture = device->stateBlock->state.textures[sampler_idx];
3159 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
3160 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3162 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3163 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3165 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3167 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3168 && shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type))
3170 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
3171 * However, the NVIDIA drivers allow them in fragment shaders as well,
3172 * even without the appropriate extension. */
3173 WARN("Using %s in fragment shader.\n", sample_function.name);
3175 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3176 "%s", coord_param.param_str);
3179 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3181 /* FIXME: Make this work for more than just 2D textures */
3182 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3183 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3185 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3187 char dst_mask[6];
3189 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3190 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3191 ins->dst[0].reg.idx, dst_mask);
3192 } else {
3193 DWORD reg = ins->src[0].reg.idx;
3194 DWORD src_mod = ins->src[0].modifiers;
3195 char dst_swizzle[6];
3197 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3199 if (src_mod == WINED3DSPSM_DZ)
3201 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3202 struct glsl_src_param div_param;
3204 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3206 if (mask_size > 1) {
3207 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3208 } else {
3209 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3212 else if (src_mod == WINED3DSPSM_DW)
3214 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3215 struct glsl_src_param div_param;
3217 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3219 if (mask_size > 1) {
3220 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3221 } else {
3222 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3224 } else {
3225 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3230 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3231 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3232 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3233 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3235 DWORD sampler_idx = ins->dst[0].reg.idx;
3236 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3237 struct glsl_sample_function sample_function;
3238 struct glsl_src_param src0_param;
3239 UINT mask_size;
3241 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3243 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3244 * scalar, and projected sampling would require 4.
3246 * It is a dependent read - not valid with conditional NP2 textures
3248 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3249 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3251 switch(mask_size)
3253 case 1:
3254 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3255 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3256 break;
3258 case 2:
3259 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3260 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3261 break;
3263 case 3:
3264 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3265 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3266 break;
3268 default:
3269 FIXME("Unexpected mask size %u\n", mask_size);
3270 break;
3274 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3275 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3276 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3278 DWORD dstreg = ins->dst[0].reg.idx;
3279 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3280 struct glsl_src_param src0_param;
3281 DWORD dst_mask;
3282 unsigned int mask_size;
3284 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3285 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3286 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3288 if (mask_size > 1) {
3289 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3290 } else {
3291 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3295 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3296 * Calculate the depth as dst.x / dst.y */
3297 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3299 struct glsl_dst_param dst_param;
3301 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3303 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3304 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3305 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3306 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3307 * >= 1.0 or < 0.0
3309 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3310 dst_param.reg_name, dst_param.reg_name);
3313 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3314 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3315 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3316 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3318 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3320 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3321 DWORD dstreg = ins->dst[0].reg.idx;
3322 struct glsl_src_param src0_param;
3324 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3326 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3327 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3330 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3331 * Calculate the 1st of a 2-row matrix multiplication. */
3332 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3334 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3335 DWORD reg = ins->dst[0].reg.idx;
3336 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3337 struct glsl_src_param src0_param;
3339 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3340 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3343 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3344 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3345 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3347 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3348 DWORD reg = ins->dst[0].reg.idx;
3349 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3350 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3351 struct glsl_src_param src0_param;
3353 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3354 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
3355 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
3358 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3360 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3361 DWORD reg = ins->dst[0].reg.idx;
3362 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3363 struct glsl_sample_function sample_function;
3364 struct glsl_src_param src0_param;
3366 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3367 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3369 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3371 /* Sample the texture using the calculated coordinates */
3372 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
3375 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3376 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3377 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3379 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3380 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3381 struct glsl_sample_function sample_function;
3382 struct glsl_src_param src0_param;
3383 DWORD reg = ins->dst[0].reg.idx;
3385 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3386 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3388 /* Dependent read, not valid with conditional NP2 */
3389 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3391 /* Sample the texture using the calculated coordinates */
3392 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3394 tex_mx->current_row = 0;
3397 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3398 * Perform the 3rd row of a 3x3 matrix multiply */
3399 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
3401 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3402 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3403 struct glsl_src_param src0_param;
3404 char dst_mask[6];
3405 DWORD reg = ins->dst[0].reg.idx;
3407 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3409 shader_glsl_append_dst(ins->ctx->buffer, ins);
3410 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3411 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
3413 tex_mx->current_row = 0;
3416 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3417 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3418 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
3420 struct glsl_src_param src0_param;
3421 struct glsl_src_param src1_param;
3422 DWORD reg = ins->dst[0].reg.idx;
3423 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3424 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3425 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3426 struct glsl_sample_function sample_function;
3428 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3429 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3431 /* Perform the last matrix multiply operation */
3432 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3433 /* Reflection calculation */
3434 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
3436 /* Dependent read, not valid with conditional NP2 */
3437 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3439 /* Sample the texture */
3440 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3442 tex_mx->current_row = 0;
3445 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3446 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3447 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
3449 DWORD reg = ins->dst[0].reg.idx;
3450 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3451 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3452 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3453 struct glsl_sample_function sample_function;
3454 struct glsl_src_param src0_param;
3456 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3458 /* Perform the last matrix multiply operation */
3459 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
3461 /* Construct the eye-ray vector from w coordinates */
3462 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3463 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
3464 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3466 /* Dependent read, not valid with conditional NP2 */
3467 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3469 /* Sample the texture using the calculated coordinates */
3470 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3472 tex_mx->current_row = 0;
3475 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3476 * Apply a fake bump map transform.
3477 * texbem is pshader <= 1.3 only, this saves a few version checks
3479 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
3481 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3482 struct glsl_sample_function sample_function;
3483 struct glsl_src_param coord_param;
3484 DWORD sampler_idx;
3485 DWORD mask;
3486 DWORD flags;
3487 char coord_mask[6];
3489 sampler_idx = ins->dst[0].reg.idx;
3490 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3491 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3493 /* Dependent read, not valid with conditional NP2 */
3494 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3495 mask = sample_function.coord_mask;
3497 shader_glsl_write_mask_to_str(mask, coord_mask);
3499 /* with projective textures, texbem only divides the static texture coord, not the displacement,
3500 * so we can't let the GL handle this.
3502 if (flags & WINED3D_PSARGS_PROJECTED) {
3503 DWORD div_mask=0;
3504 char coord_div_mask[3];
3505 switch (flags & ~WINED3D_PSARGS_PROJECTED) {
3506 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
3507 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
3508 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
3509 case WINED3DTTFF_COUNT4:
3510 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
3512 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
3513 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
3516 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
3518 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3519 "T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
3520 coord_param.param_str, coord_mask);
3522 if (ins->handler_idx == WINED3DSIH_TEXBEML)
3524 struct glsl_src_param luminance_param;
3525 struct glsl_dst_param dst_param;
3527 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
3528 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3530 shader_addline(ins->ctx->buffer, "%s%s *= (%s * luminancescale%d + luminanceoffset%d);\n",
3531 dst_param.reg_name, dst_param.mask_str,
3532 luminance_param.param_str, sampler_idx, sampler_idx);
3536 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
3538 struct glsl_src_param src0_param, src1_param;
3539 DWORD sampler_idx = ins->dst[0].reg.idx;
3541 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
3542 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
3544 shader_glsl_append_dst(ins->ctx->buffer, ins);
3545 shader_addline(ins->ctx->buffer, "%s + bumpenvmat%d * %s);\n",
3546 src0_param.param_str, sampler_idx, src1_param.param_str);
3549 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
3550 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
3551 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
3553 struct glsl_sample_function sample_function;
3554 struct glsl_src_param src0_param;
3555 DWORD sampler_idx = ins->dst[0].reg.idx;
3557 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
3559 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3560 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3561 "%s.wx", src0_param.reg_name);
3564 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
3565 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
3566 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
3568 struct glsl_sample_function sample_function;
3569 struct glsl_src_param src0_param;
3570 DWORD sampler_idx = ins->dst[0].reg.idx;
3572 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
3574 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3575 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3576 "%s.yz", src0_param.reg_name);
3579 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
3580 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
3581 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
3583 struct glsl_sample_function sample_function;
3584 struct glsl_src_param src0_param;
3585 DWORD sampler_idx = ins->dst[0].reg.idx;
3587 /* Dependent read, not valid with conditional NP2 */
3588 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3589 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
3591 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3592 "%s", src0_param.param_str);
3595 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
3596 * If any of the first 3 components are < 0, discard this pixel */
3597 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
3599 struct glsl_dst_param dst_param;
3601 /* The argument is a destination parameter, and no writemasks are allowed */
3602 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3603 if (ins->ctx->reg_maps->shader_version.major >= 2)
3605 /* 2.0 shaders compare all 4 components in texkill */
3606 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
3607 } else {
3608 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
3609 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
3610 * 4 components are defined, only the first 3 are used
3612 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
3616 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
3617 * dst = dot2(src0, src1) + src2 */
3618 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
3620 struct glsl_src_param src0_param;
3621 struct glsl_src_param src1_param;
3622 struct glsl_src_param src2_param;
3623 DWORD write_mask;
3624 unsigned int mask_size;
3626 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3627 mask_size = shader_glsl_get_write_mask_size(write_mask);
3629 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
3630 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
3631 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
3633 if (mask_size > 1) {
3634 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
3635 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
3636 } else {
3637 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
3638 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3642 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
3643 const struct wined3d_shader_signature_element *input_signature,
3644 const struct wined3d_shader_reg_maps *reg_maps,
3645 enum vertexprocessing_mode vertexprocessing)
3647 WORD map = reg_maps->input_registers;
3648 unsigned int i;
3650 for (i = 0; map; map >>= 1, ++i)
3652 const char *semantic_name;
3653 UINT semantic_idx;
3654 char reg_mask[6];
3656 /* Unused */
3657 if (!(map & 1)) continue;
3659 semantic_name = input_signature[i].semantic_name;
3660 semantic_idx = input_signature[i].semantic_idx;
3661 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
3663 if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
3665 if (semantic_idx < 8 && vertexprocessing == pretransformed)
3666 shader_addline(buffer, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
3667 shader->u.ps.input_reg_map[i], reg_mask, semantic_idx, reg_mask);
3668 else
3669 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3670 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
3672 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
3674 if (!semantic_idx)
3675 shader_addline(buffer, "IN[%u]%s = vec4(gl_Color)%s;\n",
3676 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
3677 else if (semantic_idx == 1)
3678 shader_addline(buffer, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
3679 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
3680 else
3681 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3682 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
3684 else
3686 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3687 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
3692 /*********************************************
3693 * Vertex Shader Specific Code begins here
3694 ********************************************/
3696 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
3698 struct glsl_program_key key;
3700 key.vshader = entry->vshader;
3701 key.pshader = entry->pshader;
3702 key.vs_args = entry->vs_args;
3703 key.ps_args = entry->ps_args;
3705 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
3707 ERR("Failed to insert program entry.\n");
3711 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
3712 const struct wined3d_shader *vshader, const struct wined3d_shader *pshader,
3713 const struct vs_compile_args *vs_args, const struct ps_compile_args *ps_args)
3715 struct wine_rb_entry *entry;
3716 struct glsl_program_key key;
3718 key.vshader = vshader;
3719 key.pshader = pshader;
3720 key.vs_args = *vs_args;
3721 key.ps_args = *ps_args;
3723 entry = wine_rb_get(&priv->program_lookup, &key);
3724 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
3727 /* GL locking is done by the caller */
3728 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
3729 struct glsl_shader_prog_link *entry)
3731 struct glsl_program_key key;
3733 key.vshader = entry->vshader;
3734 key.pshader = entry->pshader;
3735 key.vs_args = entry->vs_args;
3736 key.ps_args = entry->ps_args;
3737 wine_rb_remove(&priv->program_lookup, &key);
3739 GL_EXTCALL(glDeleteObjectARB(entry->programId));
3740 if (entry->vshader) list_remove(&entry->vshader_entry);
3741 if (entry->pshader) list_remove(&entry->pshader_entry);
3742 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
3743 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
3744 HeapFree(GetProcessHeap(), 0, entry);
3747 static void handle_ps3_input(struct wined3d_shader_buffer *buffer,
3748 const struct wined3d_gl_info *gl_info, const DWORD *map,
3749 const struct wined3d_shader_signature_element *input_signature,
3750 const struct wined3d_shader_reg_maps *reg_maps_in,
3751 const struct wined3d_shader_signature_element *output_signature,
3752 const struct wined3d_shader_reg_maps *reg_maps_out)
3754 unsigned int i, j;
3755 const char *semantic_name_in;
3756 UINT semantic_idx_in;
3757 DWORD *set;
3758 DWORD in_idx;
3759 unsigned int in_count = vec4_varyings(3, gl_info);
3760 char reg_mask[6];
3761 char destination[50];
3762 WORD input_map, output_map;
3764 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
3766 input_map = reg_maps_in->input_registers;
3767 for (i = 0; input_map; input_map >>= 1, ++i)
3769 if (!(input_map & 1)) continue;
3771 in_idx = map[i];
3772 /* Declared, but not read register */
3773 if (in_idx == ~0U) continue;
3774 if (in_idx >= (in_count + 2))
3776 FIXME("More input varyings declared than supported, expect issues.\n");
3777 continue;
3780 if (in_idx == in_count) {
3781 sprintf(destination, "gl_FrontColor");
3782 } else if (in_idx == in_count + 1) {
3783 sprintf(destination, "gl_FrontSecondaryColor");
3784 } else {
3785 sprintf(destination, "IN[%u]", in_idx);
3788 semantic_name_in = input_signature[i].semantic_name;
3789 semantic_idx_in = input_signature[i].semantic_idx;
3790 set[in_idx] = ~0U;
3792 output_map = reg_maps_out->output_registers;
3793 for (j = 0; output_map; output_map >>= 1, ++j)
3795 DWORD mask;
3797 if (!(output_map & 1)
3798 || semantic_idx_in != output_signature[j].semantic_idx
3799 || strcmp(semantic_name_in, output_signature[j].semantic_name)
3800 || !(mask = input_signature[i].mask & output_signature[j].mask))
3801 continue;
3803 set[in_idx] = mask;
3804 shader_glsl_write_mask_to_str(mask, reg_mask);
3806 shader_addline(buffer, "%s%s = OUT[%u]%s;\n",
3807 destination, reg_mask, j, reg_mask);
3811 for (i = 0; i < in_count + 2; ++i)
3813 unsigned int size;
3815 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
3816 continue;
3818 if (set[i] == ~0U) set[i] = 0;
3820 size = 0;
3821 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
3822 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
3823 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
3824 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
3825 reg_mask[size] = '\0';
3827 if (i == in_count) sprintf(destination, "gl_FrontColor");
3828 else if (i == in_count + 1) sprintf(destination, "gl_FrontSecondaryColor");
3829 else sprintf(destination, "IN[%u]", i);
3831 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
3832 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
3835 HeapFree(GetProcessHeap(), 0, set);
3838 /* GL locking is done by the caller */
3839 static GLhandleARB generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
3840 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
3841 const struct wined3d_gl_info *gl_info)
3843 GLhandleARB ret = 0;
3844 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
3845 unsigned int i;
3846 const char *semantic_name;
3847 UINT semantic_idx;
3848 char reg_mask[6];
3849 const struct wined3d_shader_signature_element *output_signature = vs->output_signature;
3850 WORD map = vs->reg_maps.output_registers;
3852 shader_buffer_clear(buffer);
3854 shader_addline(buffer, "#version 120\n");
3856 if (ps_major < 3)
3858 shader_addline(buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3860 for (i = 0; map; map >>= 1, ++i)
3862 DWORD write_mask;
3864 if (!(map & 1)) continue;
3866 semantic_name = output_signature[i].semantic_name;
3867 semantic_idx = output_signature[i].semantic_idx;
3868 write_mask = output_signature[i].mask;
3869 shader_glsl_write_mask_to_str(write_mask, reg_mask);
3871 if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
3873 if (!semantic_idx)
3874 shader_addline(buffer, "gl_FrontColor%s = OUT[%u]%s;\n",
3875 reg_mask, i, reg_mask);
3876 else if (semantic_idx == 1)
3877 shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n",
3878 reg_mask, i, reg_mask);
3880 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_POSITION))
3882 shader_addline(buffer, "gl_Position%s = OUT[%u]%s;\n",
3883 reg_mask, i, reg_mask);
3885 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
3887 if (semantic_idx < 8)
3889 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
3890 write_mask |= WINED3DSP_WRITEMASK_3;
3892 shader_addline(buffer, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3893 semantic_idx, reg_mask, i, reg_mask);
3894 if (!(write_mask & WINED3DSP_WRITEMASK_3))
3895 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
3898 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_PSIZE))
3900 shader_addline(buffer, "gl_PointSize = OUT[%u].%c;\n", i, reg_mask[1]);
3902 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_FOG))
3904 shader_addline(buffer, "gl_FogFragCoord = OUT[%u].%c;\n", i, reg_mask[1]);
3907 shader_addline(buffer, "}\n");
3910 else
3912 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3913 shader_addline(buffer, "varying vec4 IN[%u];\n", vec4_varyings(3, gl_info));
3914 shader_addline(buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3916 /* First, sort out position and point size. Those are not passed to the pixel shader */
3917 for (i = 0; map; map >>= 1, ++i)
3919 if (!(map & 1)) continue;
3921 semantic_name = output_signature[i].semantic_name;
3922 shader_glsl_write_mask_to_str(output_signature[i].mask, reg_mask);
3924 if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_POSITION))
3926 shader_addline(buffer, "gl_Position%s = OUT[%u]%s;\n",
3927 reg_mask, i, reg_mask);
3929 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_PSIZE))
3931 shader_addline(buffer, "gl_PointSize = OUT[%u].%c;\n", i, reg_mask[1]);
3935 /* Then, fix the pixel shader input */
3936 handle_ps3_input(buffer, gl_info, ps->u.ps.input_reg_map, ps->input_signature,
3937 &ps->reg_maps, output_signature, &vs->reg_maps);
3939 shader_addline(buffer, "}\n");
3942 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3943 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3944 shader_glsl_compile(gl_info, ret, buffer->buffer);
3946 return ret;
3949 /* GL locking is done by the caller */
3950 static void hardcode_local_constants(const struct wined3d_shader *shader,
3951 const struct wined3d_gl_info *gl_info, GLhandleARB programId, char prefix)
3953 const local_constant *lconst;
3954 GLint tmp_loc;
3955 const float *value;
3956 char glsl_name[8];
3958 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, local_constant, entry)
3960 value = (const float *)lconst->value;
3961 snprintf(glsl_name, sizeof(glsl_name), "%cLC%u", prefix, lconst->idx);
3962 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3963 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, value));
3965 checkGLcall("Hardcoding local constants");
3968 /* GL locking is done by the caller */
3969 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
3970 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
3971 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
3973 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
3974 const struct wined3d_gl_info *gl_info = context->gl_info;
3975 const DWORD *function = shader->function;
3976 struct shader_glsl_ctx_priv priv_ctx;
3978 /* Create the hw GLSL shader object and assign it as the shader->prgId */
3979 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3981 memset(&priv_ctx, 0, sizeof(priv_ctx));
3982 priv_ctx.cur_ps_args = args;
3983 priv_ctx.cur_np2fixup_info = np2fixup_info;
3985 shader_addline(buffer, "#version 120\n");
3987 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3989 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
3991 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
3993 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
3994 * drivers write a warning if we don't do so
3996 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
3998 if (gl_info->supported[EXT_GPU_SHADER4])
4000 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4003 /* Base Declarations */
4004 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4006 /* Pack 3.0 inputs */
4007 if (reg_maps->shader_version.major >= 3 && args->vp_mode != vertexshader)
4008 shader_glsl_input_pack(shader, buffer, shader->input_signature, reg_maps, args->vp_mode);
4010 /* Base Shader Body */
4011 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4013 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4014 if (reg_maps->shader_version.major < 2)
4016 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4017 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4020 if (args->srgb_correction)
4022 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4023 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4024 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4025 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4026 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4027 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4029 /* Pixel shader < 3.0 do not replace the fog stage.
4030 * This implements linear fog computation and blending.
4031 * TODO: non linear fog
4032 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
4033 * -1/(e-s) and e/(e-s) respectively.
4035 if (reg_maps->shader_version.major < 3)
4037 switch(args->fog) {
4038 case FOG_OFF: break;
4039 case FOG_LINEAR:
4040 shader_addline(buffer, "float fogstart = -1.0 / (gl_Fog.end - gl_Fog.start);\n");
4041 shader_addline(buffer, "float fogend = gl_Fog.end * -fogstart;\n");
4042 shader_addline(buffer, "float Fog = clamp(gl_FogFragCoord * fogstart + fogend, 0.0, 1.0);\n");
4043 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4044 break;
4045 case FOG_EXP:
4046 /* Fog = e^(-gl_Fog.density * gl_FogFragCoord) */
4047 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4048 shader_addline(buffer, "Fog = clamp(Fog, 0.0, 1.0);\n");
4049 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4050 break;
4051 case FOG_EXP2:
4052 /* Fog = e^(-(gl_Fog.density * gl_FogFragCoord)^2) */
4053 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4054 shader_addline(buffer, "Fog = clamp(Fog, 0.0, 1.0);\n");
4055 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4056 break;
4060 shader_addline(buffer, "}\n");
4062 TRACE("Compiling shader object %u\n", shader_obj);
4063 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4065 /* Store the shader object */
4066 return shader_obj;
4069 /* GL locking is done by the caller */
4070 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4071 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4072 const struct vs_compile_args *args)
4074 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4075 const struct wined3d_gl_info *gl_info = context->gl_info;
4076 const DWORD *function = shader->function;
4077 struct shader_glsl_ctx_priv priv_ctx;
4079 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4080 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4082 shader_addline(buffer, "#version 120\n");
4084 if (gl_info->supported[EXT_GPU_SHADER4])
4085 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4087 memset(&priv_ctx, 0, sizeof(priv_ctx));
4088 priv_ctx.cur_vs_args = args;
4090 /* Base Declarations */
4091 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4093 /* Base Shader Body */
4094 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4096 /* Unpack outputs */
4097 shader_addline(buffer, "order_ps_input(OUT);\n");
4099 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4100 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4101 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4102 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4104 if (args->fog_src == VS_FOG_Z)
4105 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4106 else if (!reg_maps->fog)
4107 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4109 /* We always store the clipplanes without y inversion */
4110 if (args->clip_enabled)
4111 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4113 /* Write the final position.
4115 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4116 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4117 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4118 * contains 1.0 to allow a mad.
4120 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4121 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4123 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4125 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4126 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4127 * which is the same as z = z * 2 - w.
4129 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4131 shader_addline(buffer, "}\n");
4133 TRACE("Compiling shader object %u\n", shader_obj);
4134 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4136 return shader_obj;
4139 static GLhandleARB find_glsl_pshader(const struct wined3d_context *context,
4140 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4141 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
4143 struct wined3d_state *state = &shader->device->stateBlock->state;
4144 UINT i;
4145 DWORD new_size;
4146 struct glsl_ps_compiled_shader *new_array;
4147 struct glsl_pshader_private *shader_data;
4148 struct ps_np2fixup_info *np2fixup = NULL;
4149 GLhandleARB ret;
4151 if (!shader->backend_data)
4153 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4154 if (!shader->backend_data)
4156 ERR("Failed to allocate backend data.\n");
4157 return 0;
4160 shader_data = shader->backend_data;
4162 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4163 * so a linear search is more performant than a hashmap or a binary search
4164 * (cache coherency etc)
4166 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4168 if (!memcmp(&shader_data->gl_shaders[i].args, args, sizeof(*args)))
4170 if (args->np2_fixup) *np2fixup_info = &shader_data->gl_shaders[i].np2fixup;
4171 return shader_data->gl_shaders[i].prgId;
4175 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4176 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4177 if (shader_data->num_gl_shaders)
4179 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4180 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders,
4181 new_size * sizeof(*shader_data->gl_shaders));
4182 } else {
4183 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader_data->gl_shaders));
4184 new_size = 1;
4187 if(!new_array) {
4188 ERR("Out of memory\n");
4189 return 0;
4191 shader_data->gl_shaders = new_array;
4192 shader_data->shader_array_size = new_size;
4195 shader_data->gl_shaders[shader_data->num_gl_shaders].args = *args;
4197 memset(&shader_data->gl_shaders[shader_data->num_gl_shaders].np2fixup, 0, sizeof(struct ps_np2fixup_info));
4198 if (args->np2_fixup) np2fixup = &shader_data->gl_shaders[shader_data->num_gl_shaders].np2fixup;
4200 pixelshader_update_samplers(&shader->reg_maps, state->textures);
4202 shader_buffer_clear(buffer);
4203 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4204 shader_data->gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4205 *np2fixup_info = np2fixup;
4207 return ret;
4210 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4211 const DWORD use_map) {
4212 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4213 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4214 return stored->fog_src == new->fog_src;
4217 static GLhandleARB find_glsl_vshader(const struct wined3d_context *context,
4218 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4219 const struct vs_compile_args *args)
4221 UINT i;
4222 DWORD new_size;
4223 struct glsl_vs_compiled_shader *new_array;
4224 DWORD use_map = shader->device->strided_streams.use_map;
4225 struct glsl_vshader_private *shader_data;
4226 GLhandleARB ret;
4228 if (!shader->backend_data)
4230 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4231 if (!shader->backend_data)
4233 ERR("Failed to allocate backend data.\n");
4234 return 0;
4237 shader_data = shader->backend_data;
4239 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4240 * so a linear search is more performant than a hashmap or a binary search
4241 * (cache coherency etc)
4243 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4244 if(vs_args_equal(&shader_data->gl_shaders[i].args, args, use_map)) {
4245 return shader_data->gl_shaders[i].prgId;
4249 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4251 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4252 if (shader_data->num_gl_shaders)
4254 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4255 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders,
4256 new_size * sizeof(*shader_data->gl_shaders));
4257 } else {
4258 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader_data->gl_shaders));
4259 new_size = 1;
4262 if(!new_array) {
4263 ERR("Out of memory\n");
4264 return 0;
4266 shader_data->gl_shaders = new_array;
4267 shader_data->shader_array_size = new_size;
4270 shader_data->gl_shaders[shader_data->num_gl_shaders].args = *args;
4272 shader_buffer_clear(buffer);
4273 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4274 shader_data->gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4276 return ret;
4279 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
4280 * It sets the programId on the current StateBlock (because it should be called
4281 * inside of the DrawPrimitive() part of the render loop).
4283 * If a program for the given combination does not exist, create one, and store
4284 * the program in the hash table. If it creates a program, it will link the
4285 * given objects, too.
4288 /* GL locking is done by the caller */
4289 static void set_glsl_shader_program(const struct wined3d_context *context,
4290 struct wined3d_device *device, BOOL use_ps, BOOL use_vs)
4292 const struct wined3d_state *state = &device->stateBlock->state;
4293 struct wined3d_shader *vshader = use_vs ? state->vertex_shader : NULL;
4294 struct wined3d_shader *pshader = use_ps ? state->pixel_shader : NULL;
4295 const struct wined3d_gl_info *gl_info = context->gl_info;
4296 struct shader_glsl_priv *priv = device->shader_priv;
4297 struct glsl_shader_prog_link *entry = NULL;
4298 GLhandleARB programId = 0;
4299 GLhandleARB reorder_shader_id = 0;
4300 unsigned int i;
4301 char glsl_name[8];
4302 struct ps_compile_args ps_compile_args;
4303 struct vs_compile_args vs_compile_args;
4305 if (vshader) find_vs_compile_args(state, vshader, &vs_compile_args);
4306 if (pshader) find_ps_compile_args(state, pshader, &ps_compile_args);
4308 entry = get_glsl_program_entry(priv, vshader, pshader, &vs_compile_args, &ps_compile_args);
4309 if (entry)
4311 priv->glsl_program = entry;
4312 return;
4315 /* If we get to this point, then no matching program exists, so we create one */
4316 programId = GL_EXTCALL(glCreateProgramObjectARB());
4317 TRACE("Created new GLSL shader program %u\n", programId);
4319 /* Create the entry */
4320 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
4321 entry->programId = programId;
4322 entry->vshader = vshader;
4323 entry->pshader = pshader;
4324 entry->vs_args = vs_compile_args;
4325 entry->ps_args = ps_compile_args;
4326 entry->constant_version = 0;
4327 entry->np2Fixup_info = NULL;
4328 /* Add the hash table entry */
4329 add_glsl_program_entry(priv, entry);
4331 /* Set the current program */
4332 priv->glsl_program = entry;
4334 /* Attach GLSL vshader */
4335 if (vshader)
4337 GLhandleARB vshader_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
4338 WORD map = vshader->reg_maps.input_registers;
4339 char tmp_name[10];
4341 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
4342 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
4343 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
4344 checkGLcall("glAttachObjectARB");
4345 /* Flag the reorder function for deletion, then it will be freed automatically when the program
4346 * is destroyed
4348 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
4350 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
4351 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
4352 checkGLcall("glAttachObjectARB");
4354 /* Bind vertex attributes to a corresponding index number to match
4355 * the same index numbers as ARB_vertex_programs (makes loading
4356 * vertex attributes simpler). With this method, we can use the
4357 * exact same code to load the attributes later for both ARB and
4358 * GLSL shaders.
4360 * We have to do this here because we need to know the Program ID
4361 * in order to make the bindings work, and it has to be done prior
4362 * to linking the GLSL program. */
4363 for (i = 0; map; map >>= 1, ++i)
4365 if (!(map & 1)) continue;
4367 snprintf(tmp_name, sizeof(tmp_name), "attrib%u", i);
4368 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
4370 checkGLcall("glBindAttribLocationARB");
4372 list_add_head(&vshader->linked_programs, &entry->vshader_entry);
4375 /* Attach GLSL pshader */
4376 if (pshader)
4378 GLhandleARB pshader_id = find_glsl_pshader(context, &priv->shader_buffer,
4379 pshader, &ps_compile_args, &entry->np2Fixup_info);
4380 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
4381 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
4382 checkGLcall("glAttachObjectARB");
4384 list_add_head(&pshader->linked_programs, &entry->pshader_entry);
4387 /* Link the program */
4388 TRACE("Linking GLSL shader program %u\n", programId);
4389 GL_EXTCALL(glLinkProgramARB(programId));
4390 shader_glsl_validate_link(gl_info, programId);
4392 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0,
4393 sizeof(GLhandleARB) * gl_info->limits.glsl_vs_float_constants);
4394 for (i = 0; i < gl_info->limits.glsl_vs_float_constants; ++i)
4396 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
4397 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4399 for (i = 0; i < MAX_CONST_I; ++i)
4401 snprintf(glsl_name, sizeof(glsl_name), "VI[%i]", i);
4402 entry->vuniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4404 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0,
4405 sizeof(GLhandleARB) * gl_info->limits.glsl_ps_float_constants);
4406 for (i = 0; i < gl_info->limits.glsl_ps_float_constants; ++i)
4408 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
4409 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4411 for (i = 0; i < MAX_CONST_I; ++i)
4413 snprintf(glsl_name, sizeof(glsl_name), "PI[%i]", i);
4414 entry->puniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4417 if(pshader) {
4418 char name[32];
4420 for(i = 0; i < MAX_TEXTURES; i++) {
4421 sprintf(name, "bumpenvmat%u", i);
4422 entry->bumpenvmat_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4423 sprintf(name, "luminancescale%u", i);
4424 entry->luminancescale_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4425 sprintf(name, "luminanceoffset%u", i);
4426 entry->luminanceoffset_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4429 if (ps_compile_args.np2_fixup) {
4430 if (entry->np2Fixup_info) {
4431 entry->np2Fixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "PsamplerNP2Fixup"));
4432 } else {
4433 FIXME("NP2 texcoord fixup needed for this pixelshader, but no fixup uniform found.\n");
4438 entry->posFixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
4439 entry->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
4440 checkGLcall("Find glsl program uniform locations");
4442 if (pshader && pshader->reg_maps.shader_version.major >= 3
4443 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
4445 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
4446 entry->vertex_color_clamp = GL_FALSE;
4447 } else {
4448 entry->vertex_color_clamp = GL_FIXED_ONLY_ARB;
4451 /* Set the shader to allow uniform loading on it */
4452 GL_EXTCALL(glUseProgramObjectARB(programId));
4453 checkGLcall("glUseProgramObjectARB(programId)");
4455 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
4456 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
4457 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
4458 * vertex shader with fixed function pixel processing is used we make sure that the card
4459 * supports enough samplers to allow the max number of vertex samplers with all possible
4460 * fixed function fragment processing setups. So once the program is linked these samplers
4461 * won't change.
4463 if (vshader) shader_glsl_load_vsamplers(gl_info, device->texUnitMap, programId);
4464 if (pshader) shader_glsl_load_psamplers(gl_info, device->texUnitMap, programId);
4466 /* If the local constants do not have to be loaded with the environment constants,
4467 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
4468 * later
4470 if (pshader && !pshader->load_local_constsF)
4471 hardcode_local_constants(pshader, gl_info, programId, 'P');
4472 if (vshader && !vshader->load_local_constsF)
4473 hardcode_local_constants(vshader, gl_info, programId, 'V');
4476 /* GL locking is done by the caller */
4477 static GLhandleARB create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum tex_types tex_type, BOOL masked)
4479 GLhandleARB program_id;
4480 GLhandleARB vshader_id, pshader_id;
4481 const char *blt_pshader;
4483 static const char *blt_vshader =
4484 "#version 120\n"
4485 "void main(void)\n"
4486 "{\n"
4487 " gl_Position = gl_Vertex;\n"
4488 " gl_FrontColor = vec4(1.0);\n"
4489 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
4490 "}\n";
4492 static const char * const blt_pshaders_full[tex_type_count] =
4494 /* tex_1d */
4495 NULL,
4496 /* tex_2d */
4497 "#version 120\n"
4498 "uniform sampler2D sampler;\n"
4499 "void main(void)\n"
4500 "{\n"
4501 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
4502 "}\n",
4503 /* tex_3d */
4504 NULL,
4505 /* tex_cube */
4506 "#version 120\n"
4507 "uniform samplerCube sampler;\n"
4508 "void main(void)\n"
4509 "{\n"
4510 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
4511 "}\n",
4512 /* tex_rect */
4513 "#version 120\n"
4514 "#extension GL_ARB_texture_rectangle : enable\n"
4515 "uniform sampler2DRect sampler;\n"
4516 "void main(void)\n"
4517 "{\n"
4518 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
4519 "}\n",
4522 static const char * const blt_pshaders_masked[tex_type_count] =
4524 /* tex_1d */
4525 NULL,
4526 /* tex_2d */
4527 "#version 120\n"
4528 "uniform sampler2D sampler;\n"
4529 "uniform vec4 mask;\n"
4530 "void main(void)\n"
4531 "{\n"
4532 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
4533 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
4534 "}\n",
4535 /* tex_3d */
4536 NULL,
4537 /* tex_cube */
4538 "#version 120\n"
4539 "uniform samplerCube sampler;\n"
4540 "uniform vec4 mask;\n"
4541 "void main(void)\n"
4542 "{\n"
4543 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
4544 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
4545 "}\n",
4546 /* tex_rect */
4547 "#version 120\n"
4548 "#extension GL_ARB_texture_rectangle : enable\n"
4549 "uniform sampler2DRect sampler;\n"
4550 "uniform vec4 mask;\n"
4551 "void main(void)\n"
4552 "{\n"
4553 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
4554 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
4555 "}\n",
4558 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
4559 if (!blt_pshader)
4561 FIXME("tex_type %#x not supported\n", tex_type);
4562 return 0;
4565 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4566 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
4568 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4569 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
4571 program_id = GL_EXTCALL(glCreateProgramObjectARB());
4572 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
4573 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
4574 GL_EXTCALL(glLinkProgramARB(program_id));
4576 shader_glsl_validate_link(gl_info, program_id);
4578 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
4579 * is destroyed
4581 GL_EXTCALL(glDeleteObjectARB(vshader_id));
4582 GL_EXTCALL(glDeleteObjectARB(pshader_id));
4583 return program_id;
4586 /* GL locking is done by the caller */
4587 static void shader_glsl_select(const struct wined3d_context *context, BOOL usePS, BOOL useVS)
4589 const struct wined3d_gl_info *gl_info = context->gl_info;
4590 struct wined3d_device *device = context->swapchain->device;
4591 struct shader_glsl_priv *priv = device->shader_priv;
4592 GLhandleARB program_id = 0;
4593 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
4595 old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
4597 if (useVS || usePS) set_glsl_shader_program(context, device, usePS, useVS);
4598 else priv->glsl_program = NULL;
4600 current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
4602 if (old_vertex_color_clamp != current_vertex_color_clamp)
4604 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
4606 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
4607 checkGLcall("glClampColorARB");
4609 else
4611 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
4615 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
4616 if (program_id) TRACE("Using GLSL program %u\n", program_id);
4617 GL_EXTCALL(glUseProgramObjectARB(program_id));
4618 checkGLcall("glUseProgramObjectARB");
4620 /* In case that NP2 texcoord fixup data is found for the selected program, trigger a reload of the
4621 * constants. This has to be done because it can't be guaranteed that sampler() (from state.c) is
4622 * called between selecting the shader and using it, which results in wrong fixup for some frames. */
4623 if (priv->glsl_program && priv->glsl_program->np2Fixup_info)
4625 shader_glsl_load_np2fixup_constants(priv, gl_info, &device->stateBlock->state);
4629 /* GL locking is done by the caller */
4630 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
4631 enum tex_types tex_type, const SIZE *ds_mask_size)
4633 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
4634 struct shader_glsl_priv *priv = shader_priv;
4635 GLhandleARB *blt_program;
4636 GLint loc;
4638 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
4639 if (!*blt_program)
4641 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
4642 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
4643 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
4644 GL_EXTCALL(glUniform1iARB(loc, 0));
4646 else
4648 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
4651 if (masked)
4653 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "mask"));
4654 GL_EXTCALL(glUniform4fARB(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
4658 /* GL locking is done by the caller */
4659 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
4661 struct shader_glsl_priv *priv = shader_priv;
4662 GLhandleARB program_id;
4664 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
4665 if (program_id) TRACE("Using GLSL program %u\n", program_id);
4667 GL_EXTCALL(glUseProgramObjectARB(program_id));
4668 checkGLcall("glUseProgramObjectARB");
4671 static void shader_glsl_destroy(struct wined3d_shader *shader)
4673 struct wined3d_device *device = shader->device;
4674 struct shader_glsl_priv *priv = device->shader_priv;
4675 const struct wined3d_gl_info *gl_info;
4676 const struct list *linked_programs;
4677 struct wined3d_context *context;
4679 char pshader = shader_is_pshader_version(shader->reg_maps.shader_version.type);
4681 if (pshader)
4683 struct glsl_pshader_private *shader_data = shader->backend_data;
4685 if (!shader_data || !shader_data->num_gl_shaders)
4687 HeapFree(GetProcessHeap(), 0, shader_data);
4688 shader->backend_data = NULL;
4689 return;
4692 context = context_acquire(device, NULL);
4693 gl_info = context->gl_info;
4695 if (priv->glsl_program && priv->glsl_program->pshader == shader)
4697 ENTER_GL();
4698 shader_glsl_select(context, FALSE, FALSE);
4699 LEAVE_GL();
4702 else
4704 struct glsl_vshader_private *shader_data = shader->backend_data;
4706 if (!shader_data || !shader_data->num_gl_shaders)
4708 HeapFree(GetProcessHeap(), 0, shader_data);
4709 shader->backend_data = NULL;
4710 return;
4713 context = context_acquire(device, NULL);
4714 gl_info = context->gl_info;
4716 if (priv->glsl_program && priv->glsl_program->vshader == shader)
4718 ENTER_GL();
4719 shader_glsl_select(context, FALSE, FALSE);
4720 LEAVE_GL();
4724 linked_programs = &shader->linked_programs;
4726 TRACE("Deleting linked programs\n");
4727 if (linked_programs->next) {
4728 struct glsl_shader_prog_link *entry, *entry2;
4730 ENTER_GL();
4731 if(pshader) {
4732 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
4733 delete_glsl_program_entry(priv, gl_info, entry);
4735 } else {
4736 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, vshader_entry) {
4737 delete_glsl_program_entry(priv, gl_info, entry);
4740 LEAVE_GL();
4743 if (pshader)
4745 struct glsl_pshader_private *shader_data = shader->backend_data;
4746 UINT i;
4748 ENTER_GL();
4749 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4750 TRACE("deleting pshader %u\n", shader_data->gl_shaders[i].prgId);
4751 GL_EXTCALL(glDeleteObjectARB(shader_data->gl_shaders[i].prgId));
4752 checkGLcall("glDeleteObjectARB");
4754 LEAVE_GL();
4755 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders);
4757 else
4759 struct glsl_vshader_private *shader_data = shader->backend_data;
4760 UINT i;
4762 ENTER_GL();
4763 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4764 TRACE("deleting vshader %u\n", shader_data->gl_shaders[i].prgId);
4765 GL_EXTCALL(glDeleteObjectARB(shader_data->gl_shaders[i].prgId));
4766 checkGLcall("glDeleteObjectARB");
4768 LEAVE_GL();
4769 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders);
4772 HeapFree(GetProcessHeap(), 0, shader->backend_data);
4773 shader->backend_data = NULL;
4775 context_release(context);
4778 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
4780 const struct glsl_program_key *k = key;
4781 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
4782 const struct glsl_shader_prog_link, program_lookup_entry);
4783 int cmp;
4785 if (k->vshader > prog->vshader) return 1;
4786 else if (k->vshader < prog->vshader) return -1;
4788 if (k->pshader > prog->pshader) return 1;
4789 else if (k->pshader < prog->pshader) return -1;
4791 if (k->vshader && (cmp = memcmp(&k->vs_args, &prog->vs_args, sizeof(prog->vs_args)))) return cmp;
4792 if (k->pshader && (cmp = memcmp(&k->ps_args, &prog->ps_args, sizeof(prog->ps_args)))) return cmp;
4794 return 0;
4797 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
4799 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries) + constant_count * sizeof(*heap->positions);
4800 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
4802 if (!mem)
4804 ERR("Failed to allocate memory\n");
4805 return FALSE;
4808 heap->entries = mem;
4809 heap->entries[1].version = 0;
4810 heap->positions = (unsigned int *)(heap->entries + constant_count + 1);
4811 heap->size = 1;
4813 return TRUE;
4816 static void constant_heap_free(struct constant_heap *heap)
4818 HeapFree(GetProcessHeap(), 0, heap->entries);
4821 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
4823 wined3d_rb_alloc,
4824 wined3d_rb_realloc,
4825 wined3d_rb_free,
4826 glsl_program_key_compare,
4829 static HRESULT shader_glsl_alloc(struct wined3d_device *device)
4831 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4832 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
4833 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
4834 gl_info->limits.glsl_ps_float_constants)) + 1;
4836 if (!shader_buffer_init(&priv->shader_buffer))
4838 ERR("Failed to initialize shader buffer.\n");
4839 goto fail;
4842 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
4843 if (!priv->stack)
4845 ERR("Failed to allocate memory.\n");
4846 goto fail;
4849 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
4851 ERR("Failed to initialize vertex shader constant heap\n");
4852 goto fail;
4855 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
4857 ERR("Failed to initialize pixel shader constant heap\n");
4858 goto fail;
4861 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
4863 ERR("Failed to initialize rbtree.\n");
4864 goto fail;
4867 priv->next_constant_version = 1;
4869 device->shader_priv = priv;
4870 return WINED3D_OK;
4872 fail:
4873 constant_heap_free(&priv->pconst_heap);
4874 constant_heap_free(&priv->vconst_heap);
4875 HeapFree(GetProcessHeap(), 0, priv->stack);
4876 shader_buffer_free(&priv->shader_buffer);
4877 HeapFree(GetProcessHeap(), 0, priv);
4878 return E_OUTOFMEMORY;
4881 /* Context activation is done by the caller. */
4882 static void shader_glsl_free(struct wined3d_device *device)
4884 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4885 struct shader_glsl_priv *priv = device->shader_priv;
4886 int i;
4888 ENTER_GL();
4889 for (i = 0; i < tex_type_count; ++i)
4891 if (priv->depth_blt_program_full[i])
4893 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_full[i]));
4895 if (priv->depth_blt_program_masked[i])
4897 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_masked[i]));
4900 LEAVE_GL();
4902 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
4903 constant_heap_free(&priv->pconst_heap);
4904 constant_heap_free(&priv->vconst_heap);
4905 HeapFree(GetProcessHeap(), 0, priv->stack);
4906 shader_buffer_free(&priv->shader_buffer);
4908 HeapFree(GetProcessHeap(), 0, device->shader_priv);
4909 device->shader_priv = NULL;
4912 static void shader_glsl_context_destroyed(void *shader_priv, const struct wined3d_context *context) {}
4914 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
4916 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
4917 * texldd and texldl instructions. */
4918 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
4920 caps->VertexShaderVersion = WINED3DVS_VERSION(3,0);
4921 caps->PixelShaderVersion = WINED3DPS_VERSION(3,0);
4923 else
4925 caps->VertexShaderVersion = WINED3DVS_VERSION(2,0);
4926 caps->PixelShaderVersion = WINED3DPS_VERSION(2,0);
4929 caps->MaxVertexShaderConst = gl_info->limits.glsl_vs_float_constants;
4930 caps->MaxPixelShaderConst = gl_info->limits.glsl_ps_float_constants;
4932 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
4933 * Direct3D minimum requirement.
4935 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
4936 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
4938 * The problem is that the refrast clamps temporary results in the shader to
4939 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
4940 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
4941 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
4942 * offer a way to query this.
4944 caps->PixelShader1xMaxValue = 8.0;
4946 caps->VSClipping = TRUE;
4948 TRACE_(d3d_caps)("Hardware vertex shader version %u.%u enabled (GLSL).\n",
4949 (caps->VertexShaderVersion >> 8) & 0xff, caps->VertexShaderVersion & 0xff);
4950 TRACE_(d3d_caps)("Hardware pixel shader version %u.%u enabled (GLSL).\n",
4951 (caps->PixelShaderVersion >> 8) & 0xff, caps->PixelShaderVersion & 0xff);
4954 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
4956 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
4958 TRACE("Checking support for fixup:\n");
4959 dump_color_fixup_desc(fixup);
4962 /* We support everything except YUV conversions. */
4963 if (!is_complex_fixup(fixup))
4965 TRACE("[OK]\n");
4966 return TRUE;
4969 TRACE("[FAILED]\n");
4970 return FALSE;
4973 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
4975 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
4976 /* WINED3DSIH_ADD */ shader_glsl_arith,
4977 /* WINED3DSIH_AND */ NULL,
4978 /* WINED3DSIH_BEM */ shader_glsl_bem,
4979 /* WINED3DSIH_BREAK */ shader_glsl_break,
4980 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
4981 /* WINED3DSIH_BREAKP */ NULL,
4982 /* WINED3DSIH_CALL */ shader_glsl_call,
4983 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
4984 /* WINED3DSIH_CMP */ shader_glsl_cmp,
4985 /* WINED3DSIH_CND */ shader_glsl_cnd,
4986 /* WINED3DSIH_CRS */ shader_glsl_cross,
4987 /* WINED3DSIH_CUT */ NULL,
4988 /* WINED3DSIH_DCL */ NULL,
4989 /* WINED3DSIH_DEF */ NULL,
4990 /* WINED3DSIH_DEFB */ NULL,
4991 /* WINED3DSIH_DEFI */ NULL,
4992 /* WINED3DSIH_DIV */ NULL,
4993 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
4994 /* WINED3DSIH_DP3 */ shader_glsl_dot,
4995 /* WINED3DSIH_DP4 */ shader_glsl_dot,
4996 /* WINED3DSIH_DST */ shader_glsl_dst,
4997 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
4998 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
4999 /* WINED3DSIH_ELSE */ shader_glsl_else,
5000 /* WINED3DSIH_EMIT */ NULL,
5001 /* WINED3DSIH_ENDIF */ shader_glsl_end,
5002 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
5003 /* WINED3DSIH_ENDREP */ shader_glsl_end,
5004 /* WINED3DSIH_EXP */ shader_glsl_map2gl,
5005 /* WINED3DSIH_EXPP */ shader_glsl_expp,
5006 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
5007 /* WINED3DSIH_FTOI */ NULL,
5008 /* WINED3DSIH_IADD */ NULL,
5009 /* WINED3DSIH_IEQ */ NULL,
5010 /* WINED3DSIH_IF */ shader_glsl_if,
5011 /* WINED3DSIH_IFC */ shader_glsl_ifc,
5012 /* WINED3DSIH_IGE */ NULL,
5013 /* WINED3DSIH_IMUL */ NULL,
5014 /* WINED3DSIH_ITOF */ NULL,
5015 /* WINED3DSIH_LABEL */ shader_glsl_label,
5016 /* WINED3DSIH_LD */ NULL,
5017 /* WINED3DSIH_LIT */ shader_glsl_lit,
5018 /* WINED3DSIH_LOG */ shader_glsl_log,
5019 /* WINED3DSIH_LOGP */ shader_glsl_log,
5020 /* WINED3DSIH_LOOP */ shader_glsl_loop,
5021 /* WINED3DSIH_LRP */ shader_glsl_lrp,
5022 /* WINED3DSIH_LT */ NULL,
5023 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
5024 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
5025 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
5026 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
5027 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
5028 /* WINED3DSIH_MAD */ shader_glsl_mad,
5029 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
5030 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
5031 /* WINED3DSIH_MOV */ shader_glsl_mov,
5032 /* WINED3DSIH_MOVA */ shader_glsl_mov,
5033 /* WINED3DSIH_MOVC */ NULL,
5034 /* WINED3DSIH_MUL */ shader_glsl_arith,
5035 /* WINED3DSIH_NOP */ NULL,
5036 /* WINED3DSIH_NRM */ shader_glsl_nrm,
5037 /* WINED3DSIH_PHASE */ NULL,
5038 /* WINED3DSIH_POW */ shader_glsl_pow,
5039 /* WINED3DSIH_RCP */ shader_glsl_rcp,
5040 /* WINED3DSIH_REP */ shader_glsl_rep,
5041 /* WINED3DSIH_RET */ shader_glsl_ret,
5042 /* WINED3DSIH_RSQ */ shader_glsl_rsq,
5043 /* WINED3DSIH_SAMPLE */ NULL,
5044 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
5045 /* WINED3DSIH_SAMPLE_LOD */ NULL,
5046 /* WINED3DSIH_SETP */ NULL,
5047 /* WINED3DSIH_SGE */ shader_glsl_compare,
5048 /* WINED3DSIH_SGN */ shader_glsl_sgn,
5049 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
5050 /* WINED3DSIH_SLT */ shader_glsl_compare,
5051 /* WINED3DSIH_SQRT */ NULL,
5052 /* WINED3DSIH_SUB */ shader_glsl_arith,
5053 /* WINED3DSIH_TEX */ shader_glsl_tex,
5054 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
5055 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
5056 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
5057 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
5058 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
5059 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
5060 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
5061 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
5062 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
5063 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
5064 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
5065 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
5066 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
5067 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
5068 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
5069 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
5070 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
5071 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
5072 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
5073 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
5074 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
5075 /* WINED3DSIH_UTOF */ NULL,
5078 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
5079 SHADER_HANDLER hw_fct;
5081 /* Select handler */
5082 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
5084 /* Unhandled opcode */
5085 if (!hw_fct)
5087 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
5088 return;
5090 hw_fct(ins);
5092 shader_glsl_add_instruction_modifiers(ins);
5095 const struct wined3d_shader_backend_ops glsl_shader_backend =
5097 shader_glsl_handle_instruction,
5098 shader_glsl_select,
5099 shader_glsl_select_depth_blt,
5100 shader_glsl_deselect_depth_blt,
5101 shader_glsl_update_float_vertex_constants,
5102 shader_glsl_update_float_pixel_constants,
5103 shader_glsl_load_constants,
5104 shader_glsl_load_np2fixup_constants,
5105 shader_glsl_destroy,
5106 shader_glsl_alloc,
5107 shader_glsl_free,
5108 shader_glsl_context_destroyed,
5109 shader_glsl_get_caps,
5110 shader_glsl_color_fixup_supported,