msvcrt: Add validity checks for _mbstrlen_l.
[wine/multimedia.git] / dlls / wined3d / glsl_shader.c
blobb74613961be196c5f5c6c8c4b750507310869d7b
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:
429 heap_idx >>= 1;
430 --stack_idx;
431 break;
434 checkGLcall("walk_constant_heap()");
437 /* GL locking is done by the caller */
438 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
440 GLfloat clamped_constant[4];
442 if (location == -1) return;
444 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
445 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
446 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
447 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
449 GL_EXTCALL(glUniform4fvARB(location, 1, clamped_constant));
452 /* GL locking is done by the caller */
453 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
454 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
456 int stack_idx = 0;
457 unsigned int heap_idx = 1;
458 unsigned int idx;
460 if (heap->entries[heap_idx].version <= version) return;
462 idx = heap->entries[heap_idx].idx;
463 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
464 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
466 while (stack_idx >= 0)
468 /* Note that we fall through to the next case statement. */
469 switch(stack[stack_idx])
471 case HEAP_NODE_TRAVERSE_LEFT:
473 unsigned int left_idx = heap_idx << 1;
474 if (left_idx < heap->size && heap->entries[left_idx].version > version)
476 heap_idx = left_idx;
477 idx = heap->entries[heap_idx].idx;
478 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
480 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
481 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
482 break;
486 case HEAP_NODE_TRAVERSE_RIGHT:
488 unsigned int right_idx = (heap_idx << 1) + 1;
489 if (right_idx < heap->size && heap->entries[right_idx].version > version)
491 heap_idx = right_idx;
492 idx = heap->entries[heap_idx].idx;
493 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
495 stack[stack_idx++] = HEAP_NODE_POP;
496 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
497 break;
501 case HEAP_NODE_POP:
502 heap_idx >>= 1;
503 --stack_idx;
504 break;
507 checkGLcall("walk_constant_heap_clamped()");
510 /* Loads floating point constants (aka uniforms) into the currently set GLSL program. */
511 /* GL locking is done by the caller */
512 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
513 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
514 unsigned char *stack, UINT version)
516 const struct wined3d_shader_lconst *lconst;
518 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
519 if (shader->reg_maps.shader_version.major == 1
520 && shader_is_pshader_version(shader->reg_maps.shader_version.type))
521 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
522 else
523 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
525 if (!shader->load_local_constsF)
527 TRACE("No need to load local float constants for this shader\n");
528 return;
531 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
532 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
534 GLint location = constant_locations[lconst->idx];
535 /* We found this uniform name in the program - go ahead and send the data */
536 if (location != -1) GL_EXTCALL(glUniform4fvARB(location, 1, (const GLfloat *)lconst->value));
538 checkGLcall("glUniform4fvARB()");
541 /* Loads integer constants (aka uniforms) into the currently set GLSL program. */
542 /* GL locking is done by the caller */
543 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
544 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
546 unsigned int i;
547 struct list* ptr;
549 for (i = 0; constants_set; constants_set >>= 1, ++i)
551 if (!(constants_set & 1)) continue;
553 TRACE_(d3d_constants)("Loading constants %u: %i, %i, %i, %i\n",
554 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
556 /* We found this uniform name in the program - go ahead and send the data */
557 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
558 checkGLcall("glUniform4ivARB");
561 /* Load immediate constants */
562 ptr = list_head(&shader->constantsI);
563 while (ptr)
565 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
566 unsigned int idx = lconst->idx;
567 const GLint *values = (const GLint *)lconst->value;
569 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
570 values[0], values[1], values[2], values[3]);
572 /* We found this uniform name in the program - go ahead and send the data */
573 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
574 checkGLcall("glUniform4ivARB");
575 ptr = list_next(&shader->constantsI, ptr);
579 /* Loads boolean constants (aka uniforms) into the currently set GLSL program. */
580 /* GL locking is done by the caller */
581 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
582 GLhandleARB programId, const BOOL *constants, WORD constants_set)
584 GLint tmp_loc;
585 unsigned int i;
586 char tmp_name[8];
587 const char *prefix;
588 struct list* ptr;
590 switch (shader->reg_maps.shader_version.type)
592 case WINED3D_SHADER_TYPE_VERTEX:
593 prefix = "VB";
594 break;
596 case WINED3D_SHADER_TYPE_GEOMETRY:
597 prefix = "GB";
598 break;
600 case WINED3D_SHADER_TYPE_PIXEL:
601 prefix = "PB";
602 break;
604 default:
605 FIXME("Unknown shader type %#x.\n",
606 shader->reg_maps.shader_version.type);
607 prefix = "UB";
608 break;
611 /* TODO: Benchmark and see if it would be beneficial to store the
612 * locations of the constants to avoid looking up each time */
613 for (i = 0; constants_set; constants_set >>= 1, ++i)
615 if (!(constants_set & 1)) continue;
617 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i]);
619 /* TODO: Benchmark and see if it would be beneficial to store the
620 * locations of the constants to avoid looking up each time */
621 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
622 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
623 if (tmp_loc != -1)
625 /* We found this uniform name in the program - go ahead and send the data */
626 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
627 checkGLcall("glUniform1ivARB");
631 /* Load immediate constants */
632 ptr = list_head(&shader->constantsB);
633 while (ptr)
635 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
636 unsigned int idx = lconst->idx;
637 const GLint *values = (const GLint *)lconst->value;
639 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
641 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
642 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
643 if (tmp_loc != -1) {
644 /* We found this uniform name in the program - go ahead and send the data */
645 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
646 checkGLcall("glUniform1ivARB");
648 ptr = list_next(&shader->constantsB, ptr);
652 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
654 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
658 * Loads the texture dimensions for NP2 fixup into the currently set GLSL program.
660 /* GL locking is done by the caller (state handler) */
661 static void shader_glsl_load_np2fixup_constants(void *shader_priv,
662 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
664 struct shader_glsl_priv *glsl_priv = shader_priv;
665 const struct glsl_shader_prog_link *prog = glsl_priv->glsl_program;
667 /* No GLSL program set - nothing to do. */
668 if (!prog) return;
670 /* NP2 texcoord fixup is (currently) only done for pixelshaders. */
671 if (!use_ps(state)) return;
673 if (prog->ps_args.np2_fixup && prog->np2Fixup_location != -1)
675 UINT i;
676 UINT fixup = prog->ps_args.np2_fixup;
677 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
679 for (i = 0; fixup; fixup >>= 1, ++i)
681 const struct wined3d_texture *tex = state->textures[i];
682 const unsigned char idx = prog->np2Fixup_info->idx[i];
683 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
685 if (!tex)
687 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
688 continue;
691 if (idx % 2)
693 tex_dim[2] = tex->pow2_matrix[0];
694 tex_dim[3] = tex->pow2_matrix[5];
696 else
698 tex_dim[0] = tex->pow2_matrix[0];
699 tex_dim[1] = tex->pow2_matrix[5];
703 GL_EXTCALL(glUniform4fvARB(prog->np2Fixup_location, prog->np2Fixup_info->num_consts, np2fixup_constants));
708 * Loads the app-supplied constants into the currently set GLSL program.
710 /* GL locking is done by the caller (state handler) */
711 static void shader_glsl_load_constants(const struct wined3d_context *context,
712 char usePixelShader, char useVertexShader)
714 const struct wined3d_gl_info *gl_info = context->gl_info;
715 struct wined3d_device *device = context->swapchain->device;
716 struct wined3d_stateblock *stateBlock = device->stateBlock;
717 const struct wined3d_state *state = &stateBlock->state;
718 struct shader_glsl_priv *priv = device->shader_priv;
719 float position_fixup[4];
721 GLhandleARB programId;
722 struct glsl_shader_prog_link *prog = priv->glsl_program;
723 UINT constant_version;
724 int i;
726 if (!prog) {
727 /* No GLSL program set - nothing to do. */
728 return;
730 programId = prog->programId;
731 constant_version = prog->constant_version;
733 if (useVertexShader)
735 const struct wined3d_shader *vshader = state->vertex_shader;
737 /* Load DirectX 9 float constants/uniforms for vertex shader */
738 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
739 prog->vuniformF_locations, &priv->vconst_heap, priv->stack, constant_version);
741 /* Load DirectX 9 integer constants/uniforms for vertex shader */
742 shader_glsl_load_constantsI(vshader, gl_info, prog->vuniformI_locations, state->vs_consts_i,
743 stateBlock->changed.vertexShaderConstantsI & vshader->reg_maps.integer_constants);
745 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
746 shader_glsl_load_constantsB(vshader, gl_info, programId, state->vs_consts_b,
747 stateBlock->changed.vertexShaderConstantsB & vshader->reg_maps.boolean_constants);
749 /* Upload the position fixup params */
750 shader_get_position_fixup(context, state, position_fixup);
751 GL_EXTCALL(glUniform4fvARB(prog->posFixup_location, 1, position_fixup));
752 checkGLcall("glUniform4fvARB");
755 if (usePixelShader)
757 const struct wined3d_shader *pshader = state->pixel_shader;
759 /* Load DirectX 9 float constants/uniforms for pixel shader */
760 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
761 prog->puniformF_locations, &priv->pconst_heap, priv->stack, constant_version);
763 /* Load DirectX 9 integer constants/uniforms for pixel shader */
764 shader_glsl_load_constantsI(pshader, gl_info, prog->puniformI_locations, state->ps_consts_i,
765 stateBlock->changed.pixelShaderConstantsI & pshader->reg_maps.integer_constants);
767 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
768 shader_glsl_load_constantsB(pshader, gl_info, programId, state->ps_consts_b,
769 stateBlock->changed.pixelShaderConstantsB & pshader->reg_maps.boolean_constants);
771 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
772 * It can't be 0 for a valid texbem instruction.
774 for(i = 0; i < MAX_TEXTURES; i++) {
775 const float *data;
777 if(prog->bumpenvmat_location[i] == -1) continue;
779 data = (const float *)&state->texture_states[i][WINED3DTSS_BUMPENVMAT00];
780 GL_EXTCALL(glUniformMatrix2fvARB(prog->bumpenvmat_location[i], 1, 0, data));
781 checkGLcall("glUniformMatrix2fvARB");
783 /* texbeml needs the luminance scale and offset too. If texbeml
784 * is used, needsbumpmat is set too, so we can check that in the
785 * needsbumpmat check. */
786 if (prog->luminancescale_location[i] != -1)
788 const GLfloat *scale = (const GLfloat *)&state->texture_states[i][WINED3DTSS_BUMPENVLSCALE];
789 const GLfloat *offset = (const GLfloat *)&state->texture_states[i][WINED3DTSS_BUMPENVLOFFSET];
791 GL_EXTCALL(glUniform1fvARB(prog->luminancescale_location[i], 1, scale));
792 checkGLcall("glUniform1fvARB");
793 GL_EXTCALL(glUniform1fvARB(prog->luminanceoffset_location[i], 1, offset));
794 checkGLcall("glUniform1fvARB");
798 if (prog->ycorrection_location != -1)
800 float correction_params[4];
802 if (context->render_offscreen)
804 correction_params[0] = 0.0f;
805 correction_params[1] = 1.0f;
806 } else {
807 /* position is window relative, not viewport relative */
808 correction_params[0] = (float) context->current_rt->resource.height;
809 correction_params[1] = -1.0f;
811 GL_EXTCALL(glUniform4fvARB(prog->ycorrection_location, 1, correction_params));
815 if (priv->next_constant_version == UINT_MAX)
817 TRACE("Max constant version reached, resetting to 0.\n");
818 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
819 priv->next_constant_version = 1;
821 else
823 prog->constant_version = priv->next_constant_version++;
827 static void update_heap_entry(const struct constant_heap *heap, unsigned int idx,
828 unsigned int heap_idx, DWORD new_version)
830 struct constant_entry *entries = heap->entries;
831 unsigned int *positions = heap->positions;
832 unsigned int parent_idx;
834 while (heap_idx > 1)
836 parent_idx = heap_idx >> 1;
838 if (new_version <= entries[parent_idx].version) break;
840 entries[heap_idx] = entries[parent_idx];
841 positions[entries[parent_idx].idx] = heap_idx;
842 heap_idx = parent_idx;
845 entries[heap_idx].version = new_version;
846 entries[heap_idx].idx = idx;
847 positions[idx] = heap_idx;
850 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
852 struct shader_glsl_priv *priv = device->shader_priv;
853 struct constant_heap *heap = &priv->vconst_heap;
854 UINT i;
856 for (i = start; i < count + start; ++i)
858 if (!device->stateBlock->changed.vertexShaderConstantsF[i])
859 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
860 else
861 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
865 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
867 struct shader_glsl_priv *priv = device->shader_priv;
868 struct constant_heap *heap = &priv->pconst_heap;
869 UINT i;
871 for (i = start; i < count + start; ++i)
873 if (!device->stateBlock->changed.pixelShaderConstantsF[i])
874 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
875 else
876 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
880 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
882 unsigned int ret = gl_info->limits.glsl_varyings / 4;
883 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
884 if(shader_major > 3) return ret;
886 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
887 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
888 return ret;
891 /** Generate the variable & register declarations for the GLSL output target */
892 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
893 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
894 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
896 const struct wined3d_state *state = &shader->device->stateBlock->state;
897 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
898 const struct wined3d_gl_info *gl_info = context->gl_info;
899 const struct wined3d_fb_state *fb = &shader->device->fb;
900 unsigned int i, extra_constants_needed = 0;
901 const struct wined3d_shader_lconst *lconst;
902 DWORD map;
904 /* There are some minor differences between pixel and vertex shaders */
905 char pshader = shader_is_pshader_version(reg_maps->shader_version.type);
906 char prefix = pshader ? 'P' : 'V';
908 /* Prototype the subroutines */
909 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
911 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
914 /* Declare the constants (aka uniforms) */
915 if (shader->limits.constant_float > 0)
917 unsigned max_constantsF;
918 /* Unless the shader uses indirect addressing, always declare the maximum array size and ignore that we need some
919 * uniforms privately. E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup and immediate values, still
920 * declare VC[256]. If the shader needs more uniforms than we have it won't work in any case. If it uses less, the
921 * compiler will figure out which uniforms are really used and strip them out. This allows a shader to use c255 on
922 * a dx9 card, as long as it doesn't also use all the other constants.
924 * If the shader uses indirect addressing the compiler must assume that all declared uniforms are used. In this case,
925 * declare only the amount that we're assured to have.
927 * Thus we run into problems in these two cases:
928 * 1) The shader really uses more uniforms than supported
929 * 2) The shader uses indirect addressing, less constants than supported, but uses a constant index > #supported consts
931 if (pshader)
933 /* No indirect addressing here. */
934 max_constantsF = gl_info->limits.glsl_ps_float_constants;
936 else
938 if (reg_maps->usesrelconstF)
940 /* Subtract the other potential uniforms from the max
941 * available (bools, ints, and 1 row of projection matrix).
942 * Subtract another uniform for immediate values, which have
943 * to be loaded via uniform by the driver as well. The shader
944 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
945 * shader code, so one vec4 should be enough. (Unfortunately
946 * the Nvidia driver doesn't store 128 and -128 in one float).
948 * Writing gl_ClipVertex requires one uniform for each
949 * clipplane as well. */
950 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
951 if(ctx_priv->cur_vs_args->clip_enabled)
953 max_constantsF -= gl_info->limits.clipplanes;
955 max_constantsF -= count_bits(reg_maps->integer_constants);
956 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
957 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
958 * for now take this into account when calculating the number of available constants
960 max_constantsF -= count_bits(reg_maps->boolean_constants);
961 /* Set by driver quirks in directx.c */
962 max_constantsF -= gl_info->reserved_glsl_constants;
964 else
966 max_constantsF = gl_info->limits.glsl_vs_float_constants;
969 max_constantsF = min(shader->limits.constant_float, max_constantsF);
970 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
973 /* Always declare the full set of constants, the compiler can remove the
974 * unused ones because d3d doesn't (yet) support indirect int and bool
975 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
976 if (shader->limits.constant_int > 0 && reg_maps->integer_constants)
977 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, shader->limits.constant_int);
979 if (shader->limits.constant_bool > 0 && reg_maps->boolean_constants)
980 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, shader->limits.constant_bool);
982 if (!pshader)
984 shader_addline(buffer, "uniform vec4 posFixup;\n");
985 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", MAX_REG_OUTPUT);
987 else
989 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
991 if (!(map & 1)) continue;
993 shader_addline(buffer, "uniform mat2 bumpenvmat%d;\n", i);
995 if (reg_maps->luminanceparams & (1 << i))
997 shader_addline(buffer, "uniform float luminancescale%d;\n", i);
998 shader_addline(buffer, "uniform float luminanceoffset%d;\n", i);
999 extra_constants_needed++;
1002 extra_constants_needed++;
1005 if (ps_args->srgb_correction)
1007 shader_addline(buffer, "const vec4 srgb_const0 = vec4(%.8e, %.8e, %.8e, %.8e);\n",
1008 srgb_pow, srgb_mul_high, srgb_sub_high, srgb_mul_low);
1009 shader_addline(buffer, "const vec4 srgb_const1 = vec4(%.8e, 0.0, 0.0, 0.0);\n",
1010 srgb_cmp);
1012 if (reg_maps->vpos || reg_maps->usesdsy)
1014 if (shader->limits.constant_float + extra_constants_needed
1015 + 1 < gl_info->limits.glsl_ps_float_constants)
1017 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1018 extra_constants_needed++;
1020 else
1022 /* This happens because we do not have proper tracking of the constant registers that are
1023 * actually used, only the max limit of the shader version
1025 FIXME("Cannot find a free uniform for vpos correction params\n");
1026 shader_addline(buffer, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
1027 context->render_offscreen ? 0.0f : fb->render_targets[0]->resource.height,
1028 context->render_offscreen ? 1.0f : -1.0f);
1030 shader_addline(buffer, "vec4 vpos;\n");
1034 /* Declare texture samplers */
1035 for (i = 0; i < shader->limits.sampler; ++i)
1037 if (reg_maps->sampler_type[i])
1039 const struct wined3d_texture *texture;
1041 switch (reg_maps->sampler_type[i])
1043 case WINED3DSTT_1D:
1044 if (pshader && ps_args->shadow & (1 << i))
1045 shader_addline(buffer, "uniform sampler1DShadow %csampler%u;\n", prefix, i);
1046 else
1047 shader_addline(buffer, "uniform sampler1D %csampler%u;\n", prefix, i);
1048 break;
1049 case WINED3DSTT_2D:
1050 texture = state->textures[i];
1051 if (pshader && ps_args->shadow & (1 << i))
1053 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
1054 shader_addline(buffer, "uniform sampler2DRectShadow %csampler%u;\n", prefix, i);
1055 else
1056 shader_addline(buffer, "uniform sampler2DShadow %csampler%u;\n", prefix, i);
1058 else
1060 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
1061 shader_addline(buffer, "uniform sampler2DRect %csampler%u;\n", prefix, i);
1062 else
1063 shader_addline(buffer, "uniform sampler2D %csampler%u;\n", prefix, i);
1065 break;
1066 case WINED3DSTT_CUBE:
1067 if (pshader && ps_args->shadow & (1 << i)) FIXME("Unsupported Cube shadow sampler.\n");
1068 shader_addline(buffer, "uniform samplerCube %csampler%u;\n", prefix, i);
1069 break;
1070 case WINED3DSTT_VOLUME:
1071 if (pshader && ps_args->shadow & (1 << i)) FIXME("Unsupported 3D shadow sampler.\n");
1072 shader_addline(buffer, "uniform sampler3D %csampler%u;\n", prefix, i);
1073 break;
1074 default:
1075 shader_addline(buffer, "uniform unsupported_sampler %csampler%u;\n", prefix, i);
1076 FIXME("Unrecognized sampler type: %#x\n", reg_maps->sampler_type[i]);
1077 break;
1082 /* Declare uniforms for NP2 texcoord fixup:
1083 * This is NOT done inside the loop that declares the texture samplers since the NP2 fixup code
1084 * is currently only used for the GeforceFX series and when forcing the ARB_npot extension off.
1085 * Modern cards just skip the code anyway, so put it inside a separate loop. */
1086 if (pshader && ps_args->np2_fixup) {
1088 struct ps_np2fixup_info* const fixup = ctx_priv->cur_np2fixup_info;
1089 UINT cur = 0;
1091 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1092 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1093 * samplerNP2Fixup stores texture dimensions and is updated through
1094 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1096 for (i = 0; i < shader->limits.sampler; ++i)
1098 if (reg_maps->sampler_type[i])
1100 if (!(ps_args->np2_fixup & (1 << i))) continue;
1102 if (WINED3DSTT_2D != reg_maps->sampler_type[i]) {
1103 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1104 continue;
1107 fixup->idx[i] = cur++;
1111 fixup->num_consts = (cur + 1) >> 1;
1112 shader_addline(buffer, "uniform vec4 %csamplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1115 /* Declare address variables */
1116 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1118 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1121 /* Declare texture coordinate temporaries and initialize them */
1122 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1124 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1127 /* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
1128 * helper function shader that is linked in at link time
1130 if (pshader && reg_maps->shader_version.major >= 3)
1132 if (use_vs(state))
1134 shader_addline(buffer, "varying vec4 IN[%u];\n", vec4_varyings(reg_maps->shader_version.major, gl_info));
1135 } else {
1136 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
1137 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
1138 * pixel shader that reads the fixed function color into the packed input registers.
1140 shader_addline(buffer, "vec4 IN[%u];\n", vec4_varyings(reg_maps->shader_version.major, gl_info));
1144 /* Declare output register temporaries */
1145 if (shader->limits.packed_output)
1146 shader_addline(buffer, "vec4 OUT[%u];\n", shader->limits.packed_output);
1148 /* Declare temporary variables */
1149 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1151 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1154 /* Declare attributes */
1155 if (reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX)
1157 for (i = 0, map = reg_maps->input_registers; map; map >>= 1, ++i)
1159 if (map & 1) shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
1163 /* Declare loop registers aLx */
1164 for (i = 0; i < reg_maps->loop_depth; i++) {
1165 shader_addline(buffer, "int aL%u;\n", i);
1166 shader_addline(buffer, "int tmpInt%u;\n", i);
1169 /* Temporary variables for matrix operations */
1170 shader_addline(buffer, "vec4 tmp0;\n");
1171 shader_addline(buffer, "vec4 tmp1;\n");
1173 /* Local constants use a different name so they can be loaded once at shader link time
1174 * They can't be hardcoded into the shader text via LC = {x, y, z, w}; because the
1175 * float -> string conversion can cause precision loss.
1177 if (!shader->load_local_constsF)
1179 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1181 shader_addline(buffer, "uniform vec4 %cLC%u;\n", prefix, lconst->idx);
1185 /* Start the main program */
1186 shader_addline(buffer, "void main() {\n");
1187 if(pshader && reg_maps->vpos) {
1188 /* DirectX apps expect integer values, while OpenGL drivers add approximately 0.5. This causes
1189 * off-by-one problems as spotted by the vPos d3d9 visual test. Unfortunately the ATI cards do
1190 * not add exactly 0.5, but rather something like 0.49999999 or 0.50000001, which still causes
1191 * precision troubles when we just subtract 0.5.
1193 * To deal with that just floor() the position. This will eliminate the fraction on all cards.
1195 * TODO: Test how that behaves with multisampling once we can enable multisampling in winex11.
1197 * An advantage of floor is that it works even if the driver doesn't add 1/2. It is somewhat
1198 * questionable if 1.5, 2.5, ... are the proper values to return in gl_FragCoord, even though
1199 * coordinates specify the pixel centers instead of the pixel corners. This code will behave
1200 * correctly on drivers that returns integer values.
1202 shader_addline(buffer, "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1206 /*****************************************************************************
1207 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1209 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1210 ****************************************************************************/
1212 /* Prototypes */
1213 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1214 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1216 /** Used for opcode modifiers - They multiply the result by the specified amount */
1217 static const char * const shift_glsl_tab[] = {
1218 "", /* 0 (none) */
1219 "2.0 * ", /* 1 (x2) */
1220 "4.0 * ", /* 2 (x4) */
1221 "8.0 * ", /* 3 (x8) */
1222 "16.0 * ", /* 4 (x16) */
1223 "32.0 * ", /* 5 (x32) */
1224 "", /* 6 (x64) */
1225 "", /* 7 (x128) */
1226 "", /* 8 (d256) */
1227 "", /* 9 (d128) */
1228 "", /* 10 (d64) */
1229 "", /* 11 (d32) */
1230 "0.0625 * ", /* 12 (d16) */
1231 "0.125 * ", /* 13 (d8) */
1232 "0.25 * ", /* 14 (d4) */
1233 "0.5 * " /* 15 (d2) */
1236 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1237 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1238 const char *in_reg, const char *in_regswizzle, char *out_str)
1240 out_str[0] = 0;
1242 switch (src_modifier)
1244 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1245 case WINED3DSPSM_DW:
1246 case WINED3DSPSM_NONE:
1247 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1248 break;
1249 case WINED3DSPSM_NEG:
1250 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1251 break;
1252 case WINED3DSPSM_NOT:
1253 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1254 break;
1255 case WINED3DSPSM_BIAS:
1256 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1257 break;
1258 case WINED3DSPSM_BIASNEG:
1259 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1260 break;
1261 case WINED3DSPSM_SIGN:
1262 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1263 break;
1264 case WINED3DSPSM_SIGNNEG:
1265 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1266 break;
1267 case WINED3DSPSM_COMP:
1268 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1269 break;
1270 case WINED3DSPSM_X2:
1271 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1272 break;
1273 case WINED3DSPSM_X2NEG:
1274 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1275 break;
1276 case WINED3DSPSM_ABS:
1277 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1278 break;
1279 case WINED3DSPSM_ABSNEG:
1280 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1281 break;
1282 default:
1283 FIXME("Unhandled modifier %u\n", src_modifier);
1284 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1288 /** Writes the GLSL variable name that corresponds to the register that the
1289 * DX opcode parameter is trying to access */
1290 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1291 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1293 /* oPos, oFog and oPts in D3D */
1294 static const char * const hwrastout_reg_names[] = {"OUT[10]", "OUT[11].x", "OUT[11].y"};
1296 const struct wined3d_shader *shader = ins->ctx->shader;
1297 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1298 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1299 char pshader = shader_is_pshader_version(reg_maps->shader_version.type);
1301 *is_color = FALSE;
1303 switch (reg->type)
1305 case WINED3DSPR_TEMP:
1306 sprintf(register_name, "R%u", reg->idx);
1307 break;
1309 case WINED3DSPR_INPUT:
1310 /* vertex shaders */
1311 if (!pshader)
1313 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1314 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx)) *is_color = TRUE;
1315 sprintf(register_name, "attrib%u", reg->idx);
1316 break;
1319 /* pixel shaders >= 3.0 */
1320 if (reg_maps->shader_version.major >= 3)
1322 DWORD idx = shader->u.ps.input_reg_map[reg->idx];
1323 unsigned int in_count = vec4_varyings(reg_maps->shader_version.major, gl_info);
1325 if (reg->rel_addr)
1327 struct glsl_src_param rel_param;
1329 shader_glsl_add_src_param(ins, reg->rel_addr, WINED3DSP_WRITEMASK_0, &rel_param);
1331 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
1332 * operation there */
1333 if (idx)
1335 if (shader->u.ps.declared_in_count > in_count)
1337 sprintf(register_name,
1338 "((%s + %u) > %d ? (%s + %u) > %d ? gl_SecondaryColor : gl_Color : IN[%s + %u])",
1339 rel_param.param_str, idx, in_count - 1, rel_param.param_str, idx, in_count,
1340 rel_param.param_str, idx);
1342 else
1344 sprintf(register_name, "IN[%s + %u]", rel_param.param_str, idx);
1347 else
1349 if (shader->u.ps.declared_in_count > in_count)
1351 sprintf(register_name, "((%s) > %d ? (%s) > %d ? gl_SecondaryColor : gl_Color : IN[%s])",
1352 rel_param.param_str, in_count - 1, rel_param.param_str, in_count,
1353 rel_param.param_str);
1355 else
1357 sprintf(register_name, "IN[%s]", rel_param.param_str);
1361 else
1363 if (idx == in_count) sprintf(register_name, "gl_Color");
1364 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
1365 else sprintf(register_name, "IN[%u]", idx);
1368 else
1370 if (!reg->idx) strcpy(register_name, "gl_Color");
1371 else strcpy(register_name, "gl_SecondaryColor");
1372 break;
1374 break;
1376 case WINED3DSPR_CONST:
1378 const char prefix = pshader ? 'P' : 'V';
1380 /* Relative addressing */
1381 if (reg->rel_addr)
1383 struct glsl_src_param rel_param;
1384 shader_glsl_add_src_param(ins, reg->rel_addr, WINED3DSP_WRITEMASK_0, &rel_param);
1385 if (reg->idx) sprintf(register_name, "%cC[%s + %u]", prefix, rel_param.param_str, reg->idx);
1386 else sprintf(register_name, "%cC[%s]", prefix, rel_param.param_str);
1388 else
1390 if (shader_constant_is_local(shader, reg->idx))
1391 sprintf(register_name, "%cLC%u", prefix, reg->idx);
1392 else
1393 sprintf(register_name, "%cC[%u]", prefix, reg->idx);
1396 break;
1398 case WINED3DSPR_CONSTINT:
1399 if (pshader) sprintf(register_name, "PI[%u]", reg->idx);
1400 else sprintf(register_name, "VI[%u]", reg->idx);
1401 break;
1403 case WINED3DSPR_CONSTBOOL:
1404 if (pshader) sprintf(register_name, "PB[%u]", reg->idx);
1405 else sprintf(register_name, "VB[%u]", reg->idx);
1406 break;
1408 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1409 if (pshader) sprintf(register_name, "T%u", reg->idx);
1410 else sprintf(register_name, "A%u", reg->idx);
1411 break;
1413 case WINED3DSPR_LOOP:
1414 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
1415 break;
1417 case WINED3DSPR_SAMPLER:
1418 if (pshader) sprintf(register_name, "Psampler%u", reg->idx);
1419 else sprintf(register_name, "Vsampler%u", reg->idx);
1420 break;
1422 case WINED3DSPR_COLOROUT:
1423 if (reg->idx >= gl_info->limits.buffers)
1424 WARN("Write to render target %u, only %d supported.\n", reg->idx, gl_info->limits.buffers);
1426 sprintf(register_name, "gl_FragData[%u]", reg->idx);
1427 break;
1429 case WINED3DSPR_RASTOUT:
1430 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx]);
1431 break;
1433 case WINED3DSPR_DEPTHOUT:
1434 sprintf(register_name, "gl_FragDepth");
1435 break;
1437 case WINED3DSPR_ATTROUT:
1438 if (!reg->idx) sprintf(register_name, "OUT[8]");
1439 else sprintf(register_name, "OUT[9]");
1440 break;
1442 case WINED3DSPR_TEXCRDOUT:
1443 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1444 sprintf(register_name, "OUT[%u]", reg->idx);
1445 break;
1447 case WINED3DSPR_MISCTYPE:
1448 if (!reg->idx)
1450 /* vPos */
1451 sprintf(register_name, "vpos");
1453 else if (reg->idx == 1)
1455 /* Note that gl_FrontFacing is a bool, while vFace is
1456 * a float for which the sign determines front/back */
1457 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1459 else
1461 FIXME("Unhandled misctype register %d\n", reg->idx);
1462 sprintf(register_name, "unrecognized_register");
1464 break;
1466 case WINED3DSPR_IMMCONST:
1467 switch (reg->immconst_type)
1469 case WINED3D_IMMCONST_SCALAR:
1470 sprintf(register_name, "%.8e", *(const float *)reg->immconst_data);
1471 break;
1473 case WINED3D_IMMCONST_VEC4:
1474 sprintf(register_name, "vec4(%.8e, %.8e, %.8e, %.8e)",
1475 *(const float *)&reg->immconst_data[0], *(const float *)&reg->immconst_data[1],
1476 *(const float *)&reg->immconst_data[2], *(const float *)&reg->immconst_data[3]);
1477 break;
1479 default:
1480 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
1481 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
1483 break;
1485 default:
1486 FIXME("Unhandled register name Type(%d)\n", reg->type);
1487 sprintf(register_name, "unrecognized_register");
1488 break;
1492 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1494 *str++ = '.';
1495 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1496 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1497 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1498 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1499 *str = '\0';
1502 /* Get the GLSL write mask for the destination register */
1503 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1505 DWORD mask = param->write_mask;
1507 if (shader_is_scalar(&param->reg))
1509 mask = WINED3DSP_WRITEMASK_0;
1510 *write_mask = '\0';
1512 else
1514 shader_glsl_write_mask_to_str(mask, write_mask);
1517 return mask;
1520 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1521 unsigned int size = 0;
1523 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1524 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1525 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1526 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1528 return size;
1531 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
1533 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1534 * but addressed as "rgba". To fix this we need to swap the register's x
1535 * and z components. */
1536 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1538 *str++ = '.';
1539 /* swizzle bits fields: wwzzyyxx */
1540 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
1541 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
1542 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
1543 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
1544 *str = '\0';
1547 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
1548 BOOL fixup, DWORD mask, char *swizzle_str)
1550 if (shader_is_scalar(&param->reg))
1551 *swizzle_str = '\0';
1552 else
1553 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
1556 /* From a given parameter token, generate the corresponding GLSL string.
1557 * Also, return the actual register name and swizzle in case the
1558 * caller needs this information as well. */
1559 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1560 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
1562 BOOL is_color = FALSE;
1563 char swizzle_str[6];
1565 glsl_src->reg_name[0] = '\0';
1566 glsl_src->param_str[0] = '\0';
1567 swizzle_str[0] = '\0';
1569 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
1570 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
1571 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
1574 /* From a given parameter token, generate the corresponding GLSL string.
1575 * Also, return the actual register name and swizzle in case the
1576 * caller needs this information as well. */
1577 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1578 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
1580 BOOL is_color = FALSE;
1582 glsl_dst->mask_str[0] = '\0';
1583 glsl_dst->reg_name[0] = '\0';
1585 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
1586 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1589 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1590 static DWORD shader_glsl_append_dst_ext(struct wined3d_shader_buffer *buffer,
1591 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst)
1593 struct glsl_dst_param glsl_dst;
1594 DWORD mask;
1596 mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst);
1597 if (mask) shader_addline(buffer, "%s%s = %s(", glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1599 return mask;
1602 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1603 static DWORD shader_glsl_append_dst(struct wined3d_shader_buffer *buffer, const struct wined3d_shader_instruction *ins)
1605 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
1608 /** Process GLSL instruction modifiers */
1609 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1611 struct glsl_dst_param dst_param;
1612 DWORD modifiers;
1614 if (!ins->dst_count) return;
1616 modifiers = ins->dst[0].modifiers;
1617 if (!modifiers) return;
1619 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
1621 if (modifiers & WINED3DSPDM_SATURATE)
1623 /* _SAT means to clamp the value of the register to between 0 and 1 */
1624 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1625 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1628 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
1630 FIXME("_centroid modifier not handled\n");
1633 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
1635 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1639 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
1641 switch (op)
1643 case WINED3D_SHADER_REL_OP_GT: return ">";
1644 case WINED3D_SHADER_REL_OP_EQ: return "==";
1645 case WINED3D_SHADER_REL_OP_GE: return ">=";
1646 case WINED3D_SHADER_REL_OP_LT: return "<";
1647 case WINED3D_SHADER_REL_OP_NE: return "!=";
1648 case WINED3D_SHADER_REL_OP_LE: return "<=";
1649 default:
1650 FIXME("Unrecognized operator %#x.\n", op);
1651 return "(\?\?)";
1655 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
1656 DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
1658 enum wined3d_sampler_texture_type sampler_type = ctx->reg_maps->sampler_type[sampler_idx];
1659 const struct wined3d_gl_info *gl_info = ctx->gl_info;
1660 BOOL shadow = shader_is_pshader_version(ctx->reg_maps->shader_version.type)
1661 && (((const struct shader_glsl_ctx_priv *)ctx->backend_data)->cur_ps_args->shadow & (1 << sampler_idx));
1662 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
1663 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_RECT;
1664 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
1665 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
1667 /* Note that there's no such thing as a projected cube texture. */
1668 switch(sampler_type) {
1669 case WINED3DSTT_1D:
1670 if (shadow)
1672 if (lod)
1674 sample_function->name = projected ? "shadow1DProjLod" : "shadow1DLod";
1676 else if (grad)
1678 if (gl_info->supported[EXT_GPU_SHADER4])
1679 sample_function->name = projected ? "shadow1DProjGrad" : "shadow1DGrad";
1680 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1681 sample_function->name = projected ? "shadow1DProjGradARB" : "shadow1DGradARB";
1682 else
1684 FIXME("Unsupported 1D shadow grad function.\n");
1685 sample_function->name = "unsupported1DGrad";
1688 else
1690 sample_function->name = projected ? "shadow1DProj" : "shadow1D";
1692 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1694 else
1696 if (lod)
1698 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
1700 else if (grad)
1702 if (gl_info->supported[EXT_GPU_SHADER4])
1703 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
1704 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1705 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
1706 else
1708 FIXME("Unsupported 1D grad function.\n");
1709 sample_function->name = "unsupported1DGrad";
1712 else
1714 sample_function->name = projected ? "texture1DProj" : "texture1D";
1716 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1718 break;
1720 case WINED3DSTT_2D:
1721 if (shadow)
1723 if (texrect)
1725 if (lod)
1727 sample_function->name = projected ? "shadow2DRectProjLod" : "shadow2DRectLod";
1729 else if (grad)
1731 if (gl_info->supported[EXT_GPU_SHADER4])
1732 sample_function->name = projected ? "shadow2DRectProjGrad" : "shadow2DRectGrad";
1733 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1734 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
1735 else
1737 FIXME("Unsupported RECT shadow grad function.\n");
1738 sample_function->name = "unsupported2DRectGrad";
1741 else
1743 sample_function->name = projected ? "shadow2DRectProj" : "shadow2DRect";
1746 else
1748 if (lod)
1750 sample_function->name = projected ? "shadow2DProjLod" : "shadow2DLod";
1752 else if (grad)
1754 if (gl_info->supported[EXT_GPU_SHADER4])
1755 sample_function->name = projected ? "shadow2DProjGrad" : "shadow2DGrad";
1756 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1757 sample_function->name = projected ? "shadow2DProjGradARB" : "shadow2DGradARB";
1758 else
1760 FIXME("Unsupported 2D shadow grad function.\n");
1761 sample_function->name = "unsupported2DGrad";
1764 else
1766 sample_function->name = projected ? "shadow2DProj" : "shadow2D";
1769 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1771 else
1773 if (texrect)
1775 if (lod)
1777 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
1779 else if (grad)
1781 if (gl_info->supported[EXT_GPU_SHADER4])
1782 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
1783 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1784 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
1785 else
1787 FIXME("Unsupported RECT grad function.\n");
1788 sample_function->name = "unsupported2DRectGrad";
1791 else
1793 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
1796 else
1798 if (lod)
1800 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
1802 else if (grad)
1804 if (gl_info->supported[EXT_GPU_SHADER4])
1805 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
1806 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1807 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
1808 else
1810 FIXME("Unsupported 2D grad function.\n");
1811 sample_function->name = "unsupported2DGrad";
1814 else
1816 sample_function->name = projected ? "texture2DProj" : "texture2D";
1819 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1821 break;
1823 case WINED3DSTT_CUBE:
1824 if (shadow)
1826 FIXME("Unsupported Cube shadow function.\n");
1827 sample_function->name = "unsupportedCubeShadow";
1828 sample_function->coord_mask = 0;
1830 else
1832 if (lod)
1834 sample_function->name = "textureCubeLod";
1836 else if (grad)
1838 if (gl_info->supported[EXT_GPU_SHADER4])
1839 sample_function->name = "textureCubeGrad";
1840 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1841 sample_function->name = "textureCubeGradARB";
1842 else
1844 FIXME("Unsupported Cube grad function.\n");
1845 sample_function->name = "unsupportedCubeGrad";
1848 else
1850 sample_function->name = "textureCube";
1852 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1854 break;
1856 case WINED3DSTT_VOLUME:
1857 if (shadow)
1859 FIXME("Unsupported 3D shadow function.\n");
1860 sample_function->name = "unsupported3DShadow";
1861 sample_function->coord_mask = 0;
1863 else
1865 if (lod)
1867 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
1869 else if (grad)
1871 if (gl_info->supported[EXT_GPU_SHADER4])
1872 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
1873 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1874 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
1875 else
1877 FIXME("Unsupported 3D grad function.\n");
1878 sample_function->name = "unsupported3DGrad";
1881 else
1883 sample_function->name = projected ? "texture3DProj" : "texture3D";
1885 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1887 break;
1889 default:
1890 sample_function->name = "";
1891 sample_function->coord_mask = 0;
1892 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
1893 break;
1897 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
1898 BOOL sign_fixup, enum fixup_channel_source channel_source)
1900 switch(channel_source)
1902 case CHANNEL_SOURCE_ZERO:
1903 strcat(arguments, "0.0");
1904 break;
1906 case CHANNEL_SOURCE_ONE:
1907 strcat(arguments, "1.0");
1908 break;
1910 case CHANNEL_SOURCE_X:
1911 strcat(arguments, reg_name);
1912 strcat(arguments, ".x");
1913 break;
1915 case CHANNEL_SOURCE_Y:
1916 strcat(arguments, reg_name);
1917 strcat(arguments, ".y");
1918 break;
1920 case CHANNEL_SOURCE_Z:
1921 strcat(arguments, reg_name);
1922 strcat(arguments, ".z");
1923 break;
1925 case CHANNEL_SOURCE_W:
1926 strcat(arguments, reg_name);
1927 strcat(arguments, ".w");
1928 break;
1930 default:
1931 FIXME("Unhandled channel source %#x\n", channel_source);
1932 strcat(arguments, "undefined");
1933 break;
1936 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
1939 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
1941 struct wined3d_shader_dst_param dst;
1942 unsigned int mask_size, remaining;
1943 struct glsl_dst_param dst_param;
1944 char arguments[256];
1945 DWORD mask;
1947 mask = 0;
1948 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) mask |= WINED3DSP_WRITEMASK_0;
1949 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) mask |= WINED3DSP_WRITEMASK_1;
1950 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) mask |= WINED3DSP_WRITEMASK_2;
1951 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) mask |= WINED3DSP_WRITEMASK_3;
1952 mask &= ins->dst[0].write_mask;
1954 if (!mask) return; /* Nothing to do */
1956 if (is_complex_fixup(fixup))
1958 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
1959 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
1960 return;
1963 mask_size = shader_glsl_get_write_mask_size(mask);
1965 dst = ins->dst[0];
1966 dst.write_mask = mask;
1967 shader_glsl_add_dst_param(ins, &dst, &dst_param);
1969 arguments[0] = '\0';
1970 remaining = mask_size;
1971 if (mask & WINED3DSP_WRITEMASK_0)
1973 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.x_sign_fixup, fixup.x_source);
1974 if (--remaining) strcat(arguments, ", ");
1976 if (mask & WINED3DSP_WRITEMASK_1)
1978 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.y_sign_fixup, fixup.y_source);
1979 if (--remaining) strcat(arguments, ", ");
1981 if (mask & WINED3DSP_WRITEMASK_2)
1983 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.z_sign_fixup, fixup.z_source);
1984 if (--remaining) strcat(arguments, ", ");
1986 if (mask & WINED3DSP_WRITEMASK_3)
1988 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.w_sign_fixup, fixup.w_source);
1989 if (--remaining) strcat(arguments, ", ");
1992 if (mask_size > 1)
1994 shader_addline(ins->ctx->buffer, "%s%s = vec%u(%s);\n",
1995 dst_param.reg_name, dst_param.mask_str, mask_size, arguments);
1997 else
1999 shader_addline(ins->ctx->buffer, "%s%s = %s;\n", dst_param.reg_name, dst_param.mask_str, arguments);
2003 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2004 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2005 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2007 const char *sampler_base;
2008 char dst_swizzle[6];
2009 struct color_fixup_desc fixup;
2010 BOOL np2_fixup = FALSE;
2011 va_list args;
2013 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2015 if (shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type))
2017 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2018 fixup = priv->cur_ps_args->color_fixup[sampler];
2019 sampler_base = "Psampler";
2021 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2022 if(bias) {
2023 FIXME("Biased sampling from NP2 textures is unsupported\n");
2024 } else {
2025 np2_fixup = TRUE;
2028 } else {
2029 sampler_base = "Vsampler";
2030 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
2033 shader_glsl_append_dst(ins->ctx->buffer, ins);
2035 shader_addline(ins->ctx->buffer, "%s(%s%u, ", sample_function->name, sampler_base, sampler);
2037 va_start(args, coord_reg_fmt);
2038 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2039 va_end(args);
2041 if(bias) {
2042 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2043 } else {
2044 if (np2_fixup) {
2045 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2046 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2048 shader_addline(ins->ctx->buffer, " * PsamplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2049 (idx % 2) ? "zw" : "xy", dst_swizzle);
2050 } else if(dx && dy) {
2051 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2052 } else {
2053 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2057 if(!is_identity_fixup(fixup)) {
2058 shader_glsl_color_correction(ins, fixup);
2062 /*****************************************************************************
2063 * Begin processing individual instruction opcodes
2064 ****************************************************************************/
2066 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
2067 static void shader_glsl_arith(const struct wined3d_shader_instruction *ins)
2069 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2070 struct glsl_src_param src0_param;
2071 struct glsl_src_param src1_param;
2072 DWORD write_mask;
2073 char op;
2075 /* Determine the GLSL operator to use based on the opcode */
2076 switch (ins->handler_idx)
2078 case WINED3DSIH_MUL: op = '*'; break;
2079 case WINED3DSIH_ADD: op = '+'; break;
2080 case WINED3DSIH_SUB: op = '-'; break;
2081 default:
2082 op = ' ';
2083 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2084 break;
2087 write_mask = shader_glsl_append_dst(buffer, ins);
2088 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2089 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2090 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
2093 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2094 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2096 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2097 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2098 struct glsl_src_param src0_param;
2099 DWORD write_mask;
2101 write_mask = shader_glsl_append_dst(buffer, ins);
2102 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2104 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2105 * shader versions WINED3DSIO_MOVA is used for this. */
2106 if (ins->ctx->reg_maps->shader_version.major == 1
2107 && !shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type)
2108 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2110 /* This is a simple floor() */
2111 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2112 if (mask_size > 1) {
2113 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2114 } else {
2115 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2118 else if(ins->handler_idx == WINED3DSIH_MOVA)
2120 /* We need to *round* to the nearest int here. */
2121 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2123 if (gl_info->supported[EXT_GPU_SHADER4])
2125 if (mask_size > 1)
2126 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2127 else
2128 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2130 else
2132 if (mask_size > 1)
2133 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2134 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2135 else
2136 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2137 src0_param.param_str, src0_param.param_str);
2140 else
2142 shader_addline(buffer, "%s);\n", src0_param.param_str);
2146 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2147 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2149 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2150 struct glsl_src_param src0_param;
2151 struct glsl_src_param src1_param;
2152 DWORD dst_write_mask, src_write_mask;
2153 unsigned int dst_size = 0;
2155 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2156 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2158 /* dp3 works on vec3, dp4 on vec4 */
2159 if (ins->handler_idx == WINED3DSIH_DP4)
2161 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2162 } else {
2163 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2166 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2167 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2169 if (dst_size > 1) {
2170 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2171 } else {
2172 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2176 /* Note that this instruction has some restrictions. The destination write mask
2177 * can't contain the w component, and the source swizzles have to be .xyzw */
2178 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2180 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2181 struct glsl_src_param src0_param;
2182 struct glsl_src_param src1_param;
2183 char dst_mask[6];
2185 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2186 shader_glsl_append_dst(ins->ctx->buffer, ins);
2187 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2188 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2189 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2192 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2193 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2194 * GLSL uses the value as-is. */
2195 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2197 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2198 struct glsl_src_param src0_param;
2199 struct glsl_src_param src1_param;
2200 DWORD dst_write_mask;
2201 unsigned int dst_size;
2203 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2204 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2206 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2207 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2209 if (dst_size > 1)
2211 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2212 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
2214 else
2216 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
2217 src1_param.param_str, src0_param.param_str, src1_param.param_str);
2221 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
2222 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
2223 * GLSL uses the value as-is. */
2224 static void shader_glsl_log(const struct wined3d_shader_instruction *ins)
2226 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2227 struct glsl_src_param src0_param;
2228 DWORD dst_write_mask;
2229 unsigned int dst_size;
2231 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2232 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2234 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2236 if (dst_size > 1)
2238 shader_addline(buffer, "vec%u(log2(abs(%s))));\n",
2239 dst_size, src0_param.param_str);
2241 else
2243 shader_addline(buffer, "log2(abs(%s)));\n",
2244 src0_param.param_str);
2248 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2249 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
2251 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2252 struct glsl_src_param src_param;
2253 const char *instruction;
2254 DWORD write_mask;
2255 unsigned i;
2257 /* Determine the GLSL function to use based on the opcode */
2258 /* TODO: Possibly make this a table for faster lookups */
2259 switch (ins->handler_idx)
2261 case WINED3DSIH_MIN: instruction = "min"; break;
2262 case WINED3DSIH_MAX: instruction = "max"; break;
2263 case WINED3DSIH_ABS: instruction = "abs"; break;
2264 case WINED3DSIH_FRC: instruction = "fract"; break;
2265 case WINED3DSIH_EXP: instruction = "exp2"; break;
2266 case WINED3DSIH_DSX: instruction = "dFdx"; break;
2267 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
2268 default: instruction = "";
2269 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2270 break;
2273 write_mask = shader_glsl_append_dst(buffer, ins);
2275 shader_addline(buffer, "%s(", instruction);
2277 if (ins->src_count)
2279 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2280 shader_addline(buffer, "%s", src_param.param_str);
2281 for (i = 1; i < ins->src_count; ++i)
2283 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
2284 shader_addline(buffer, ", %s", src_param.param_str);
2288 shader_addline(buffer, "));\n");
2291 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
2293 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2294 struct glsl_src_param src_param;
2295 unsigned int mask_size;
2296 DWORD write_mask;
2297 char dst_mask[6];
2299 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
2300 mask_size = shader_glsl_get_write_mask_size(write_mask);
2301 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2303 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
2304 src_param.param_str, src_param.param_str);
2305 shader_glsl_append_dst(buffer, ins);
2307 if (mask_size > 1)
2309 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
2310 mask_size, src_param.param_str);
2312 else
2314 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
2315 src_param.param_str);
2319 /** Process the WINED3DSIO_EXPP instruction in GLSL:
2320 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2321 * dst.x = 2^(floor(src))
2322 * dst.y = src - floor(src)
2323 * dst.z = 2^src (partial precision is allowed, but optional)
2324 * dst.w = 1.0;
2325 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2326 * dst = 2^src; (partial precision is allowed, but optional)
2328 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
2330 struct glsl_src_param src_param;
2332 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
2334 if (ins->ctx->reg_maps->shader_version.major < 2)
2336 char dst_mask[6];
2338 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
2339 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
2340 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
2341 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
2343 shader_glsl_append_dst(ins->ctx->buffer, ins);
2344 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2345 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
2346 } else {
2347 DWORD write_mask;
2348 unsigned int mask_size;
2350 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2351 mask_size = shader_glsl_get_write_mask_size(write_mask);
2353 if (mask_size > 1) {
2354 shader_addline(ins->ctx->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
2355 } else {
2356 shader_addline(ins->ctx->buffer, "exp2(%s));\n", src_param.param_str);
2361 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
2362 static void shader_glsl_rcp(const struct wined3d_shader_instruction *ins)
2364 struct glsl_src_param src_param;
2365 DWORD write_mask;
2366 unsigned int mask_size;
2368 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2369 mask_size = shader_glsl_get_write_mask_size(write_mask);
2370 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2372 if (mask_size > 1)
2374 shader_addline(ins->ctx->buffer, "vec%u(1.0 / %s));\n",
2375 mask_size, src_param.param_str);
2377 else
2379 shader_addline(ins->ctx->buffer, "1.0 / %s);\n",
2380 src_param.param_str);
2384 static void shader_glsl_rsq(const struct wined3d_shader_instruction *ins)
2386 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2387 struct glsl_src_param src_param;
2388 DWORD write_mask;
2389 unsigned int mask_size;
2391 write_mask = shader_glsl_append_dst(buffer, ins);
2392 mask_size = shader_glsl_get_write_mask_size(write_mask);
2394 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2396 if (mask_size > 1)
2398 shader_addline(buffer, "vec%u(inversesqrt(abs(%s))));\n",
2399 mask_size, src_param.param_str);
2401 else
2403 shader_addline(buffer, "inversesqrt(abs(%s)));\n",
2404 src_param.param_str);
2408 /** Process signed comparison opcodes in GLSL. */
2409 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
2411 struct glsl_src_param src0_param;
2412 struct glsl_src_param src1_param;
2413 DWORD write_mask;
2414 unsigned int mask_size;
2416 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2417 mask_size = shader_glsl_get_write_mask_size(write_mask);
2418 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2419 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2421 if (mask_size > 1) {
2422 const char *compare;
2424 switch(ins->handler_idx)
2426 case WINED3DSIH_SLT: compare = "lessThan"; break;
2427 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
2428 default: compare = "";
2429 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2432 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
2433 src0_param.param_str, src1_param.param_str);
2434 } else {
2435 switch(ins->handler_idx)
2437 case WINED3DSIH_SLT:
2438 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2439 * to return 0.0 but step returns 1.0 because step is not < x
2440 * An alternative is a bvec compare padded with an unused second component.
2441 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2442 * issue. Playing with not() is not possible either because not() does not accept
2443 * a scalar.
2445 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
2446 src0_param.param_str, src1_param.param_str);
2447 break;
2448 case WINED3DSIH_SGE:
2449 /* Here we can use the step() function and safe a conditional */
2450 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
2451 break;
2452 default:
2453 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2459 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
2460 static void shader_glsl_cmp(const struct wined3d_shader_instruction *ins)
2462 struct glsl_src_param src0_param;
2463 struct glsl_src_param src1_param;
2464 struct glsl_src_param src2_param;
2465 DWORD write_mask, cmp_channel = 0;
2466 unsigned int i, j;
2467 char mask_char[6];
2468 BOOL temp_destination = FALSE;
2470 if (shader_is_scalar(&ins->src[0].reg))
2472 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2474 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2475 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2476 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2478 shader_addline(ins->ctx->buffer, "%s >= 0.0 ? %s : %s);\n",
2479 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2480 } else {
2481 DWORD dst_mask = ins->dst[0].write_mask;
2482 struct wined3d_shader_dst_param dst = ins->dst[0];
2484 /* Cycle through all source0 channels */
2485 for (i=0; i<4; i++) {
2486 write_mask = 0;
2487 /* Find the destination channels which use the current source0 channel */
2488 for (j=0; j<4; j++) {
2489 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2491 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2492 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2495 dst.write_mask = dst_mask & write_mask;
2497 /* Splitting the cmp instruction up in multiple lines imposes a problem:
2498 * The first lines may overwrite source parameters of the following lines.
2499 * Deal with that by using a temporary destination register if needed
2501 if ((ins->src[0].reg.idx == ins->dst[0].reg.idx
2502 && ins->src[0].reg.type == ins->dst[0].reg.type)
2503 || (ins->src[1].reg.idx == ins->dst[0].reg.idx
2504 && ins->src[1].reg.type == ins->dst[0].reg.type)
2505 || (ins->src[2].reg.idx == ins->dst[0].reg.idx
2506 && ins->src[2].reg.type == ins->dst[0].reg.type))
2508 write_mask = shader_glsl_get_write_mask(&dst, mask_char);
2509 if (!write_mask) continue;
2510 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
2511 temp_destination = TRUE;
2512 } else {
2513 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst);
2514 if (!write_mask) continue;
2517 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2518 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2519 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2521 shader_addline(ins->ctx->buffer, "%s >= 0.0 ? %s : %s);\n",
2522 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2525 if(temp_destination) {
2526 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
2527 shader_glsl_append_dst(ins->ctx->buffer, ins);
2528 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
2534 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2535 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2536 * the compare is done per component of src0. */
2537 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
2539 struct wined3d_shader_dst_param dst;
2540 struct glsl_src_param src0_param;
2541 struct glsl_src_param src1_param;
2542 struct glsl_src_param src2_param;
2543 DWORD write_mask, cmp_channel = 0;
2544 unsigned int i, j;
2545 DWORD dst_mask;
2546 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2547 ins->ctx->reg_maps->shader_version.minor);
2549 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
2551 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2552 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2553 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2554 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2556 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
2557 if (ins->coissue)
2559 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
2560 } else {
2561 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2562 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2564 return;
2566 /* Cycle through all source0 channels */
2567 dst_mask = ins->dst[0].write_mask;
2568 dst = ins->dst[0];
2569 for (i=0; i<4; i++) {
2570 write_mask = 0;
2571 /* Find the destination channels which use the current source0 channel */
2572 for (j=0; j<4; j++) {
2573 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2575 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2576 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2580 dst.write_mask = dst_mask & write_mask;
2581 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst);
2582 if (!write_mask) continue;
2584 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2585 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2586 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2588 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2589 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2593 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2594 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
2596 struct glsl_src_param src0_param;
2597 struct glsl_src_param src1_param;
2598 struct glsl_src_param src2_param;
2599 DWORD write_mask;
2601 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2602 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2603 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2604 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2605 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
2606 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2609 /* Handles transforming all WINED3DSIO_M?x? opcodes for
2610 Vertex shaders to GLSL codes */
2611 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
2613 int i;
2614 int nComponents = 0;
2615 struct wined3d_shader_dst_param tmp_dst = {{0}};
2616 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
2617 struct wined3d_shader_instruction tmp_ins;
2619 memset(&tmp_ins, 0, sizeof(tmp_ins));
2621 /* Set constants for the temporary argument */
2622 tmp_ins.ctx = ins->ctx;
2623 tmp_ins.dst_count = 1;
2624 tmp_ins.dst = &tmp_dst;
2625 tmp_ins.src_count = 2;
2626 tmp_ins.src = tmp_src;
2628 switch(ins->handler_idx)
2630 case WINED3DSIH_M4x4:
2631 nComponents = 4;
2632 tmp_ins.handler_idx = WINED3DSIH_DP4;
2633 break;
2634 case WINED3DSIH_M4x3:
2635 nComponents = 3;
2636 tmp_ins.handler_idx = WINED3DSIH_DP4;
2637 break;
2638 case WINED3DSIH_M3x4:
2639 nComponents = 4;
2640 tmp_ins.handler_idx = WINED3DSIH_DP3;
2641 break;
2642 case WINED3DSIH_M3x3:
2643 nComponents = 3;
2644 tmp_ins.handler_idx = WINED3DSIH_DP3;
2645 break;
2646 case WINED3DSIH_M3x2:
2647 nComponents = 2;
2648 tmp_ins.handler_idx = WINED3DSIH_DP3;
2649 break;
2650 default:
2651 break;
2654 tmp_dst = ins->dst[0];
2655 tmp_src[0] = ins->src[0];
2656 tmp_src[1] = ins->src[1];
2657 for (i = 0; i < nComponents; ++i)
2659 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
2660 shader_glsl_dot(&tmp_ins);
2661 ++tmp_src[1].reg.idx;
2666 The LRP instruction performs a component-wise linear interpolation
2667 between the second and third operands using the first operand as the
2668 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
2669 This is equivalent to mix(src2, src1, src0);
2671 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
2673 struct glsl_src_param src0_param;
2674 struct glsl_src_param src1_param;
2675 struct glsl_src_param src2_param;
2676 DWORD write_mask;
2678 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2680 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2681 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2682 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2684 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
2685 src2_param.param_str, src1_param.param_str, src0_param.param_str);
2688 /** Process the WINED3DSIO_LIT instruction in GLSL:
2689 * dst.x = dst.w = 1.0
2690 * dst.y = (src0.x > 0) ? src0.x
2691 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
2692 * where src.w is clamped at +- 128
2694 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
2696 struct glsl_src_param src0_param;
2697 struct glsl_src_param src1_param;
2698 struct glsl_src_param src3_param;
2699 char dst_mask[6];
2701 shader_glsl_append_dst(ins->ctx->buffer, ins);
2702 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2704 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2705 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
2706 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
2708 /* The sdk specifies the instruction like this
2709 * dst.x = 1.0;
2710 * if(src.x > 0.0) dst.y = src.x
2711 * else dst.y = 0.0.
2712 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
2713 * else dst.z = 0.0;
2714 * dst.w = 1.0;
2715 * (where power = src.w clamped between -128 and 128)
2717 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
2718 * dst.x = 1.0 ... No further explanation needed
2719 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
2720 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
2721 * dst.w = 1.0. ... Nothing fancy.
2723 * So we still have one conditional in there. So do this:
2724 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
2726 * 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),
2727 * 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.
2728 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
2730 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
2731 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
2732 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
2734 shader_addline(ins->ctx->buffer,
2735 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
2736 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
2737 src0_param.param_str, src3_param.param_str, src1_param.param_str,
2738 src0_param.param_str, src3_param.param_str, dst_mask);
2741 /** Process the WINED3DSIO_DST instruction in GLSL:
2742 * dst.x = 1.0
2743 * dst.y = src0.x * src0.y
2744 * dst.z = src0.z
2745 * dst.w = src1.w
2747 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
2749 struct glsl_src_param src0y_param;
2750 struct glsl_src_param src0z_param;
2751 struct glsl_src_param src1y_param;
2752 struct glsl_src_param src1w_param;
2753 char dst_mask[6];
2755 shader_glsl_append_dst(ins->ctx->buffer, ins);
2756 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2758 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
2759 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
2760 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
2761 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
2763 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
2764 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
2767 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
2768 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
2769 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
2771 * dst.x = cos(src0.?)
2772 * dst.y = sin(src0.?)
2773 * dst.z = dst.z
2774 * dst.w = dst.w
2776 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
2778 struct glsl_src_param src0_param;
2779 DWORD write_mask;
2781 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2782 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2784 switch (write_mask) {
2785 case WINED3DSP_WRITEMASK_0:
2786 shader_addline(ins->ctx->buffer, "cos(%s));\n", src0_param.param_str);
2787 break;
2789 case WINED3DSP_WRITEMASK_1:
2790 shader_addline(ins->ctx->buffer, "sin(%s));\n", src0_param.param_str);
2791 break;
2793 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
2794 shader_addline(ins->ctx->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
2795 break;
2797 default:
2798 ERR("Write mask should be .x, .y or .xy\n");
2799 break;
2803 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
2804 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
2805 * generate invalid code
2807 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
2809 struct glsl_src_param src0_param;
2810 DWORD write_mask;
2812 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2813 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2815 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
2818 /** Process the WINED3DSIO_LOOP instruction in GLSL:
2819 * Start a for() loop where src1.y is the initial value of aL,
2820 * increment aL by src1.z for a total of src1.x iterations.
2821 * Need to use a temporary variable for this operation.
2823 /* FIXME: I don't think nested loops will work correctly this way. */
2824 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
2826 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
2827 const struct wined3d_shader *shader = ins->ctx->shader;
2828 const struct wined3d_shader_lconst *constant;
2829 struct glsl_src_param src1_param;
2830 const DWORD *control_values = NULL;
2832 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
2834 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
2835 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
2836 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
2837 * addressing.
2839 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
2841 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
2843 if (constant->idx == ins->src[1].reg.idx)
2845 control_values = constant->value;
2846 break;
2851 if (control_values)
2853 struct wined3d_shader_loop_control loop_control;
2854 loop_control.count = control_values[0];
2855 loop_control.start = control_values[1];
2856 loop_control.step = (int)control_values[2];
2858 if (loop_control.step > 0)
2860 shader_addline(ins->ctx->buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d) {\n",
2861 loop_state->current_depth, loop_control.start,
2862 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
2863 loop_state->current_depth, loop_control.step);
2865 else if (loop_control.step < 0)
2867 shader_addline(ins->ctx->buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d) {\n",
2868 loop_state->current_depth, loop_control.start,
2869 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
2870 loop_state->current_depth, loop_control.step);
2872 else
2874 shader_addline(ins->ctx->buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++) {\n",
2875 loop_state->current_depth, loop_control.start, loop_state->current_depth,
2876 loop_state->current_depth, loop_control.count,
2877 loop_state->current_depth);
2879 } else {
2880 shader_addline(ins->ctx->buffer,
2881 "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
2882 loop_state->current_depth, loop_state->current_reg,
2883 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
2884 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
2887 ++loop_state->current_depth;
2888 ++loop_state->current_reg;
2891 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
2893 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
2895 shader_addline(ins->ctx->buffer, "}\n");
2897 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
2899 --loop_state->current_depth;
2900 --loop_state->current_reg;
2903 if (ins->handler_idx == WINED3DSIH_ENDREP)
2905 --loop_state->current_depth;
2909 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
2911 const struct wined3d_shader *shader = ins->ctx->shader;
2912 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
2913 const struct wined3d_shader_lconst *constant;
2914 struct glsl_src_param src0_param;
2915 const DWORD *control_values = NULL;
2917 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
2918 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
2920 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
2922 if (constant->idx == ins->src[0].reg.idx)
2924 control_values = constant->value;
2925 break;
2930 if (control_values)
2932 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
2933 loop_state->current_depth, loop_state->current_depth,
2934 control_values[0], loop_state->current_depth);
2936 else
2938 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2939 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2940 loop_state->current_depth, loop_state->current_depth,
2941 src0_param.param_str, loop_state->current_depth);
2944 ++loop_state->current_depth;
2947 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
2949 struct glsl_src_param src0_param;
2951 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2952 shader_addline(ins->ctx->buffer, "if (%s) {\n", src0_param.param_str);
2955 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
2957 struct glsl_src_param src0_param;
2958 struct glsl_src_param src1_param;
2960 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2961 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2963 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
2964 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
2967 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
2969 shader_addline(ins->ctx->buffer, "} else {\n");
2972 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
2974 shader_addline(ins->ctx->buffer, "break;\n");
2977 /* FIXME: According to MSDN the compare is done per component. */
2978 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
2980 struct glsl_src_param src0_param;
2981 struct glsl_src_param src1_param;
2983 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2984 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2986 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
2987 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
2990 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
2992 shader_addline(ins->ctx->buffer, "}\n");
2993 shader_addline(ins->ctx->buffer, "void subroutine%u () {\n", ins->src[0].reg.idx);
2996 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
2998 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx);
3001 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3003 struct glsl_src_param src1_param;
3005 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3006 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n", src1_param.param_str, ins->src[0].reg.idx);
3009 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3011 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3012 * function only suppresses the unhandled instruction warning
3016 /*********************************************
3017 * Pixel Shader Specific Code begins here
3018 ********************************************/
3019 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3021 const struct wined3d_shader *shader = ins->ctx->shader;
3022 struct wined3d_device *device = shader->device;
3023 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3024 ins->ctx->reg_maps->shader_version.minor);
3025 struct glsl_sample_function sample_function;
3026 const struct wined3d_texture *texture;
3027 DWORD sample_flags = 0;
3028 DWORD sampler_idx;
3029 DWORD mask = 0, swizzle;
3031 /* 1.0-1.4: Use destination register as sampler source.
3032 * 2.0+: Use provided sampler source. */
3033 if (shader_version < WINED3D_SHADER_VERSION(2,0)) sampler_idx = ins->dst[0].reg.idx;
3034 else sampler_idx = ins->src[1].reg.idx;
3035 texture = device->stateBlock->state.textures[sampler_idx];
3037 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3039 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3040 DWORD flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3041 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3042 enum wined3d_sampler_texture_type sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3044 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3045 if (flags & WINED3D_PSARGS_PROJECTED && sampler_type != WINED3DSTT_CUBE) {
3046 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3047 switch (flags & ~WINED3D_PSARGS_PROJECTED) {
3048 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
3049 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
3050 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
3051 case WINED3DTTFF_COUNT4:
3052 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
3056 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3058 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3060 if (src_mod == WINED3DSPSM_DZ) {
3061 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3062 mask = WINED3DSP_WRITEMASK_2;
3063 } else if (src_mod == WINED3DSPSM_DW) {
3064 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3065 mask = WINED3DSP_WRITEMASK_3;
3067 } else {
3068 if (ins->flags & WINED3DSI_TEXLD_PROJECT)
3070 /* ps 2.0 texldp instruction always divides by the fourth component. */
3071 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3072 mask = WINED3DSP_WRITEMASK_3;
3076 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
3077 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3079 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3080 mask |= sample_function.coord_mask;
3082 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3083 else swizzle = ins->src[1].swizzle;
3085 /* 1.0-1.3: Use destination register as coordinate source.
3086 1.4+: Use provided coordinate source register. */
3087 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3089 char coord_mask[6];
3090 shader_glsl_write_mask_to_str(mask, coord_mask);
3091 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3092 "T%u%s", sampler_idx, coord_mask);
3094 else
3096 struct glsl_src_param coord_param;
3097 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3098 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3100 struct glsl_src_param bias;
3101 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3102 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3103 "%s", coord_param.param_str);
3104 } else {
3105 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3106 "%s", coord_param.param_str);
3111 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3113 const struct wined3d_shader *shader = ins->ctx->shader;
3114 struct wined3d_device *device = shader->device;
3115 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3116 struct glsl_src_param coord_param, dx_param, dy_param;
3117 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3118 struct glsl_sample_function sample_function;
3119 DWORD sampler_idx;
3120 DWORD swizzle = ins->src[1].swizzle;
3121 const struct wined3d_texture *texture;
3123 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3125 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3126 shader_glsl_tex(ins);
3127 return;
3130 sampler_idx = ins->src[1].reg.idx;
3131 texture = device->stateBlock->state.textures[sampler_idx];
3132 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
3133 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3135 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3136 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3137 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3138 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3140 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3141 "%s", coord_param.param_str);
3144 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3146 const struct wined3d_shader *shader = ins->ctx->shader;
3147 struct wined3d_device *device = shader->device;
3148 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3149 struct glsl_src_param coord_param, lod_param;
3150 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3151 struct glsl_sample_function sample_function;
3152 DWORD sampler_idx;
3153 DWORD swizzle = ins->src[1].swizzle;
3154 const struct wined3d_texture *texture;
3156 sampler_idx = ins->src[1].reg.idx;
3157 texture = device->stateBlock->state.textures[sampler_idx];
3158 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
3159 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3161 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3162 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3164 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3166 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3167 && shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type))
3169 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
3170 * However, the NVIDIA drivers allow them in fragment shaders as well,
3171 * even without the appropriate extension. */
3172 WARN("Using %s in fragment shader.\n", sample_function.name);
3174 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3175 "%s", coord_param.param_str);
3178 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3180 /* FIXME: Make this work for more than just 2D textures */
3181 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3182 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3184 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3186 char dst_mask[6];
3188 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3189 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3190 ins->dst[0].reg.idx, dst_mask);
3192 else
3194 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3195 DWORD reg = ins->src[0].reg.idx;
3196 char dst_swizzle[6];
3198 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3200 if (src_mod == WINED3DSPSM_DZ)
3202 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3203 struct glsl_src_param div_param;
3205 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3207 if (mask_size > 1) {
3208 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3209 } else {
3210 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3213 else if (src_mod == WINED3DSPSM_DW)
3215 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3216 struct glsl_src_param div_param;
3218 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3220 if (mask_size > 1) {
3221 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3222 } else {
3223 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3225 } else {
3226 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3231 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3232 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3233 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3234 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3236 DWORD sampler_idx = ins->dst[0].reg.idx;
3237 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3238 struct glsl_sample_function sample_function;
3239 struct glsl_src_param src0_param;
3240 UINT mask_size;
3242 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3244 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3245 * scalar, and projected sampling would require 4.
3247 * It is a dependent read - not valid with conditional NP2 textures
3249 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3250 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3252 switch(mask_size)
3254 case 1:
3255 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3256 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3257 break;
3259 case 2:
3260 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3261 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3262 break;
3264 case 3:
3265 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3266 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3267 break;
3269 default:
3270 FIXME("Unexpected mask size %u\n", mask_size);
3271 break;
3275 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3276 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3277 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3279 DWORD dstreg = ins->dst[0].reg.idx;
3280 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3281 struct glsl_src_param src0_param;
3282 DWORD dst_mask;
3283 unsigned int mask_size;
3285 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3286 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3287 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3289 if (mask_size > 1) {
3290 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3291 } else {
3292 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3296 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3297 * Calculate the depth as dst.x / dst.y */
3298 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3300 struct glsl_dst_param dst_param;
3302 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3304 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3305 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3306 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3307 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3308 * >= 1.0 or < 0.0
3310 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3311 dst_param.reg_name, dst_param.reg_name);
3314 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3315 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3316 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3317 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3319 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3321 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3322 DWORD dstreg = ins->dst[0].reg.idx;
3323 struct glsl_src_param src0_param;
3325 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3327 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3328 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3331 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3332 * Calculate the 1st of a 2-row matrix multiplication. */
3333 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3335 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3336 DWORD reg = ins->dst[0].reg.idx;
3337 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3338 struct glsl_src_param src0_param;
3340 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3341 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3344 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3345 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3346 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3348 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3349 DWORD reg = ins->dst[0].reg.idx;
3350 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3351 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3352 struct glsl_src_param src0_param;
3354 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3355 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
3356 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
3359 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3361 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3362 DWORD reg = ins->dst[0].reg.idx;
3363 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3364 struct glsl_sample_function sample_function;
3365 struct glsl_src_param src0_param;
3367 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3368 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3370 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3372 /* Sample the texture using the calculated coordinates */
3373 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
3376 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3377 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3378 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3380 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3381 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3382 struct glsl_sample_function sample_function;
3383 struct glsl_src_param src0_param;
3384 DWORD reg = ins->dst[0].reg.idx;
3386 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3387 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3389 /* Dependent read, not valid with conditional NP2 */
3390 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3392 /* Sample the texture using the calculated coordinates */
3393 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3395 tex_mx->current_row = 0;
3398 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3399 * Perform the 3rd row of a 3x3 matrix multiply */
3400 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
3402 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3403 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3404 struct glsl_src_param src0_param;
3405 char dst_mask[6];
3406 DWORD reg = ins->dst[0].reg.idx;
3408 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3410 shader_glsl_append_dst(ins->ctx->buffer, ins);
3411 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3412 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
3414 tex_mx->current_row = 0;
3417 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3418 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3419 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
3421 struct glsl_src_param src0_param;
3422 struct glsl_src_param src1_param;
3423 DWORD reg = ins->dst[0].reg.idx;
3424 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3425 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3426 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3427 struct glsl_sample_function sample_function;
3428 char coord_mask[6];
3430 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3431 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3433 /* Perform the last matrix multiply operation */
3434 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3435 /* Reflection calculation */
3436 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
3438 /* Dependent read, not valid with conditional NP2 */
3439 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3440 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3442 /* Sample the texture */
3443 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3444 NULL, NULL, NULL, "tmp0%s", coord_mask);
3446 tex_mx->current_row = 0;
3449 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3450 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3451 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
3453 DWORD reg = ins->dst[0].reg.idx;
3454 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3455 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3456 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3457 struct glsl_sample_function sample_function;
3458 struct glsl_src_param src0_param;
3459 char coord_mask[6];
3461 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3463 /* Perform the last matrix multiply operation */
3464 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
3466 /* Construct the eye-ray vector from w coordinates */
3467 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3468 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
3469 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3471 /* Dependent read, not valid with conditional NP2 */
3472 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3473 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3475 /* Sample the texture using the calculated coordinates */
3476 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3477 NULL, NULL, NULL, "tmp0%s", coord_mask);
3479 tex_mx->current_row = 0;
3482 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3483 * Apply a fake bump map transform.
3484 * texbem is pshader <= 1.3 only, this saves a few version checks
3486 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
3488 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3489 struct glsl_sample_function sample_function;
3490 struct glsl_src_param coord_param;
3491 DWORD sampler_idx;
3492 DWORD mask;
3493 DWORD flags;
3494 char coord_mask[6];
3496 sampler_idx = ins->dst[0].reg.idx;
3497 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3498 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3500 /* Dependent read, not valid with conditional NP2 */
3501 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3502 mask = sample_function.coord_mask;
3504 shader_glsl_write_mask_to_str(mask, coord_mask);
3506 /* with projective textures, texbem only divides the static texture coord, not the displacement,
3507 * so we can't let the GL handle this.
3509 if (flags & WINED3D_PSARGS_PROJECTED) {
3510 DWORD div_mask=0;
3511 char coord_div_mask[3];
3512 switch (flags & ~WINED3D_PSARGS_PROJECTED) {
3513 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
3514 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
3515 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
3516 case WINED3DTTFF_COUNT4:
3517 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
3519 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
3520 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
3523 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
3525 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3526 "T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
3527 coord_param.param_str, coord_mask);
3529 if (ins->handler_idx == WINED3DSIH_TEXBEML)
3531 struct glsl_src_param luminance_param;
3532 struct glsl_dst_param dst_param;
3534 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
3535 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3537 shader_addline(ins->ctx->buffer, "%s%s *= (%s * luminancescale%d + luminanceoffset%d);\n",
3538 dst_param.reg_name, dst_param.mask_str,
3539 luminance_param.param_str, sampler_idx, sampler_idx);
3543 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
3545 struct glsl_src_param src0_param, src1_param;
3546 DWORD sampler_idx = ins->dst[0].reg.idx;
3548 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
3549 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
3551 shader_glsl_append_dst(ins->ctx->buffer, ins);
3552 shader_addline(ins->ctx->buffer, "%s + bumpenvmat%d * %s);\n",
3553 src0_param.param_str, sampler_idx, src1_param.param_str);
3556 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
3557 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
3558 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
3560 struct glsl_sample_function sample_function;
3561 struct glsl_src_param src0_param;
3562 DWORD sampler_idx = ins->dst[0].reg.idx;
3564 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
3566 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3567 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3568 "%s.wx", src0_param.reg_name);
3571 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
3572 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
3573 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
3575 struct glsl_sample_function sample_function;
3576 struct glsl_src_param src0_param;
3577 DWORD sampler_idx = ins->dst[0].reg.idx;
3579 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
3581 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3582 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3583 "%s.yz", src0_param.reg_name);
3586 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
3587 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
3588 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
3590 struct glsl_sample_function sample_function;
3591 struct glsl_src_param src0_param;
3592 DWORD sampler_idx = ins->dst[0].reg.idx;
3594 /* Dependent read, not valid with conditional NP2 */
3595 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3596 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
3598 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3599 "%s", src0_param.param_str);
3602 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
3603 * If any of the first 3 components are < 0, discard this pixel */
3604 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
3606 struct glsl_dst_param dst_param;
3608 /* The argument is a destination parameter, and no writemasks are allowed */
3609 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3610 if (ins->ctx->reg_maps->shader_version.major >= 2)
3612 /* 2.0 shaders compare all 4 components in texkill */
3613 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
3614 } else {
3615 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
3616 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
3617 * 4 components are defined, only the first 3 are used
3619 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
3623 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
3624 * dst = dot2(src0, src1) + src2 */
3625 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
3627 struct glsl_src_param src0_param;
3628 struct glsl_src_param src1_param;
3629 struct glsl_src_param src2_param;
3630 DWORD write_mask;
3631 unsigned int mask_size;
3633 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3634 mask_size = shader_glsl_get_write_mask_size(write_mask);
3636 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
3637 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
3638 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
3640 if (mask_size > 1) {
3641 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
3642 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
3643 } else {
3644 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
3645 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3649 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
3650 const struct wined3d_shader_signature_element *input_signature,
3651 const struct wined3d_shader_reg_maps *reg_maps,
3652 enum vertexprocessing_mode vertexprocessing)
3654 WORD map = reg_maps->input_registers;
3655 unsigned int i;
3657 for (i = 0; map; map >>= 1, ++i)
3659 const char *semantic_name;
3660 UINT semantic_idx;
3661 char reg_mask[6];
3663 /* Unused */
3664 if (!(map & 1)) continue;
3666 semantic_name = input_signature[i].semantic_name;
3667 semantic_idx = input_signature[i].semantic_idx;
3668 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
3670 if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
3672 if (semantic_idx < 8 && vertexprocessing == pretransformed)
3673 shader_addline(buffer, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
3674 shader->u.ps.input_reg_map[i], reg_mask, semantic_idx, reg_mask);
3675 else
3676 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3677 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
3679 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
3681 if (!semantic_idx)
3682 shader_addline(buffer, "IN[%u]%s = vec4(gl_Color)%s;\n",
3683 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
3684 else if (semantic_idx == 1)
3685 shader_addline(buffer, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
3686 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
3687 else
3688 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3689 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
3691 else
3693 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3694 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
3699 /*********************************************
3700 * Vertex Shader Specific Code begins here
3701 ********************************************/
3703 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
3705 struct glsl_program_key key;
3707 key.vshader = entry->vshader;
3708 key.pshader = entry->pshader;
3709 key.vs_args = entry->vs_args;
3710 key.ps_args = entry->ps_args;
3712 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
3714 ERR("Failed to insert program entry.\n");
3718 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
3719 const struct wined3d_shader *vshader, const struct wined3d_shader *pshader,
3720 const struct vs_compile_args *vs_args, const struct ps_compile_args *ps_args)
3722 struct wine_rb_entry *entry;
3723 struct glsl_program_key key;
3725 key.vshader = vshader;
3726 key.pshader = pshader;
3727 key.vs_args = *vs_args;
3728 key.ps_args = *ps_args;
3730 entry = wine_rb_get(&priv->program_lookup, &key);
3731 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
3734 /* GL locking is done by the caller */
3735 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
3736 struct glsl_shader_prog_link *entry)
3738 struct glsl_program_key key;
3740 key.vshader = entry->vshader;
3741 key.pshader = entry->pshader;
3742 key.vs_args = entry->vs_args;
3743 key.ps_args = entry->ps_args;
3744 wine_rb_remove(&priv->program_lookup, &key);
3746 GL_EXTCALL(glDeleteObjectARB(entry->programId));
3747 if (entry->vshader) list_remove(&entry->vshader_entry);
3748 if (entry->pshader) list_remove(&entry->pshader_entry);
3749 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
3750 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
3751 HeapFree(GetProcessHeap(), 0, entry);
3754 static void handle_ps3_input(struct wined3d_shader_buffer *buffer,
3755 const struct wined3d_gl_info *gl_info, const DWORD *map,
3756 const struct wined3d_shader_signature_element *input_signature,
3757 const struct wined3d_shader_reg_maps *reg_maps_in,
3758 const struct wined3d_shader_signature_element *output_signature,
3759 const struct wined3d_shader_reg_maps *reg_maps_out)
3761 unsigned int i, j;
3762 const char *semantic_name_in;
3763 UINT semantic_idx_in;
3764 DWORD *set;
3765 DWORD in_idx;
3766 unsigned int in_count = vec4_varyings(3, gl_info);
3767 char reg_mask[6];
3768 char destination[50];
3769 WORD input_map, output_map;
3771 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
3773 input_map = reg_maps_in->input_registers;
3774 for (i = 0; input_map; input_map >>= 1, ++i)
3776 if (!(input_map & 1)) continue;
3778 in_idx = map[i];
3779 /* Declared, but not read register */
3780 if (in_idx == ~0U) continue;
3781 if (in_idx >= (in_count + 2))
3783 FIXME("More input varyings declared than supported, expect issues.\n");
3784 continue;
3787 if (in_idx == in_count) {
3788 sprintf(destination, "gl_FrontColor");
3789 } else if (in_idx == in_count + 1) {
3790 sprintf(destination, "gl_FrontSecondaryColor");
3791 } else {
3792 sprintf(destination, "IN[%u]", in_idx);
3795 semantic_name_in = input_signature[i].semantic_name;
3796 semantic_idx_in = input_signature[i].semantic_idx;
3797 set[in_idx] = ~0U;
3799 output_map = reg_maps_out->output_registers;
3800 for (j = 0; output_map; output_map >>= 1, ++j)
3802 DWORD mask;
3804 if (!(output_map & 1)
3805 || semantic_idx_in != output_signature[j].semantic_idx
3806 || strcmp(semantic_name_in, output_signature[j].semantic_name)
3807 || !(mask = input_signature[i].mask & output_signature[j].mask))
3808 continue;
3810 set[in_idx] = mask;
3811 shader_glsl_write_mask_to_str(mask, reg_mask);
3813 shader_addline(buffer, "%s%s = OUT[%u]%s;\n",
3814 destination, reg_mask, j, reg_mask);
3818 for (i = 0; i < in_count + 2; ++i)
3820 unsigned int size;
3822 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
3823 continue;
3825 if (set[i] == ~0U) set[i] = 0;
3827 size = 0;
3828 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
3829 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
3830 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
3831 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
3832 reg_mask[size] = '\0';
3834 if (i == in_count) sprintf(destination, "gl_FrontColor");
3835 else if (i == in_count + 1) sprintf(destination, "gl_FrontSecondaryColor");
3836 else sprintf(destination, "IN[%u]", i);
3838 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
3839 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
3842 HeapFree(GetProcessHeap(), 0, set);
3845 /* GL locking is done by the caller */
3846 static GLhandleARB generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
3847 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
3848 const struct wined3d_gl_info *gl_info)
3850 GLhandleARB ret = 0;
3851 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
3852 unsigned int i;
3853 const char *semantic_name;
3854 UINT semantic_idx;
3855 char reg_mask[6];
3856 const struct wined3d_shader_signature_element *output_signature = vs->output_signature;
3857 WORD map = vs->reg_maps.output_registers;
3859 shader_buffer_clear(buffer);
3861 shader_addline(buffer, "#version 120\n");
3863 if (ps_major < 3)
3865 shader_addline(buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3867 for (i = 0; map; map >>= 1, ++i)
3869 DWORD write_mask;
3871 if (!(map & 1)) continue;
3873 semantic_name = output_signature[i].semantic_name;
3874 semantic_idx = output_signature[i].semantic_idx;
3875 write_mask = output_signature[i].mask;
3876 shader_glsl_write_mask_to_str(write_mask, reg_mask);
3878 if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
3880 if (!semantic_idx)
3881 shader_addline(buffer, "gl_FrontColor%s = OUT[%u]%s;\n",
3882 reg_mask, i, reg_mask);
3883 else if (semantic_idx == 1)
3884 shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n",
3885 reg_mask, i, reg_mask);
3887 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_POSITION))
3889 shader_addline(buffer, "gl_Position%s = OUT[%u]%s;\n",
3890 reg_mask, i, reg_mask);
3892 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
3894 if (semantic_idx < 8)
3896 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
3897 write_mask |= WINED3DSP_WRITEMASK_3;
3899 shader_addline(buffer, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3900 semantic_idx, reg_mask, i, reg_mask);
3901 if (!(write_mask & WINED3DSP_WRITEMASK_3))
3902 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
3905 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_PSIZE))
3907 shader_addline(buffer, "gl_PointSize = OUT[%u].%c;\n", i, reg_mask[1]);
3909 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_FOG))
3911 shader_addline(buffer, "gl_FogFragCoord = OUT[%u].%c;\n", i, reg_mask[1]);
3914 shader_addline(buffer, "}\n");
3917 else
3919 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3920 shader_addline(buffer, "varying vec4 IN[%u];\n", vec4_varyings(3, gl_info));
3921 shader_addline(buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3923 /* First, sort out position and point size. Those are not passed to the pixel shader */
3924 for (i = 0; map; map >>= 1, ++i)
3926 if (!(map & 1)) continue;
3928 semantic_name = output_signature[i].semantic_name;
3929 shader_glsl_write_mask_to_str(output_signature[i].mask, reg_mask);
3931 if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_POSITION))
3933 shader_addline(buffer, "gl_Position%s = OUT[%u]%s;\n",
3934 reg_mask, i, reg_mask);
3936 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_PSIZE))
3938 shader_addline(buffer, "gl_PointSize = OUT[%u].%c;\n", i, reg_mask[1]);
3942 /* Then, fix the pixel shader input */
3943 handle_ps3_input(buffer, gl_info, ps->u.ps.input_reg_map, ps->input_signature,
3944 &ps->reg_maps, output_signature, &vs->reg_maps);
3946 shader_addline(buffer, "}\n");
3949 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3950 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3951 shader_glsl_compile(gl_info, ret, buffer->buffer);
3953 return ret;
3956 /* GL locking is done by the caller */
3957 static void hardcode_local_constants(const struct wined3d_shader *shader,
3958 const struct wined3d_gl_info *gl_info, GLhandleARB programId, char prefix)
3960 const struct wined3d_shader_lconst *lconst;
3961 GLint tmp_loc;
3962 const float *value;
3963 char glsl_name[8];
3965 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
3967 value = (const float *)lconst->value;
3968 snprintf(glsl_name, sizeof(glsl_name), "%cLC%u", prefix, lconst->idx);
3969 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3970 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, value));
3972 checkGLcall("Hardcoding local constants");
3975 /* GL locking is done by the caller */
3976 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
3977 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
3978 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
3980 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
3981 const struct wined3d_gl_info *gl_info = context->gl_info;
3982 const DWORD *function = shader->function;
3983 struct shader_glsl_ctx_priv priv_ctx;
3985 /* Create the hw GLSL shader object and assign it as the shader->prgId */
3986 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3988 memset(&priv_ctx, 0, sizeof(priv_ctx));
3989 priv_ctx.cur_ps_args = args;
3990 priv_ctx.cur_np2fixup_info = np2fixup_info;
3992 shader_addline(buffer, "#version 120\n");
3994 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3996 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
3998 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4000 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
4001 * drivers write a warning if we don't do so
4003 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4005 if (gl_info->supported[EXT_GPU_SHADER4])
4007 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4010 /* Base Declarations */
4011 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4013 /* Pack 3.0 inputs */
4014 if (reg_maps->shader_version.major >= 3 && args->vp_mode != vertexshader)
4015 shader_glsl_input_pack(shader, buffer, shader->input_signature, reg_maps, args->vp_mode);
4017 /* Base Shader Body */
4018 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4020 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4021 if (reg_maps->shader_version.major < 2)
4023 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4024 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4027 if (args->srgb_correction)
4029 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4030 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4031 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4032 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4033 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4034 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4036 /* Pixel shader < 3.0 do not replace the fog stage.
4037 * This implements linear fog computation and blending.
4038 * TODO: non linear fog
4039 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
4040 * -1/(e-s) and e/(e-s) respectively.
4042 if (reg_maps->shader_version.major < 3)
4044 switch(args->fog) {
4045 case FOG_OFF: break;
4046 case FOG_LINEAR:
4047 shader_addline(buffer, "float fogstart = -1.0 / (gl_Fog.end - gl_Fog.start);\n");
4048 shader_addline(buffer, "float fogend = gl_Fog.end * -fogstart;\n");
4049 shader_addline(buffer, "float Fog = clamp(gl_FogFragCoord * fogstart + fogend, 0.0, 1.0);\n");
4050 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4051 break;
4052 case FOG_EXP:
4053 /* Fog = e^(-gl_Fog.density * gl_FogFragCoord) */
4054 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4055 shader_addline(buffer, "Fog = clamp(Fog, 0.0, 1.0);\n");
4056 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4057 break;
4058 case FOG_EXP2:
4059 /* Fog = e^(-(gl_Fog.density * gl_FogFragCoord)^2) */
4060 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4061 shader_addline(buffer, "Fog = clamp(Fog, 0.0, 1.0);\n");
4062 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4063 break;
4067 shader_addline(buffer, "}\n");
4069 TRACE("Compiling shader object %u\n", shader_obj);
4070 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4072 /* Store the shader object */
4073 return shader_obj;
4076 /* GL locking is done by the caller */
4077 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4078 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4079 const struct vs_compile_args *args)
4081 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4082 const struct wined3d_gl_info *gl_info = context->gl_info;
4083 const DWORD *function = shader->function;
4084 struct shader_glsl_ctx_priv priv_ctx;
4086 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4087 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4089 shader_addline(buffer, "#version 120\n");
4091 if (gl_info->supported[EXT_GPU_SHADER4])
4092 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4094 memset(&priv_ctx, 0, sizeof(priv_ctx));
4095 priv_ctx.cur_vs_args = args;
4097 /* Base Declarations */
4098 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4100 /* Base Shader Body */
4101 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4103 /* Unpack outputs */
4104 shader_addline(buffer, "order_ps_input(OUT);\n");
4106 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4107 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4108 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4109 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4111 if (args->fog_src == VS_FOG_Z)
4112 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4113 else if (!reg_maps->fog)
4114 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4116 /* We always store the clipplanes without y inversion */
4117 if (args->clip_enabled)
4118 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4120 /* Write the final position.
4122 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4123 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4124 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4125 * contains 1.0 to allow a mad.
4127 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4128 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4130 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4132 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4133 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4134 * which is the same as z = z * 2 - w.
4136 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4138 shader_addline(buffer, "}\n");
4140 TRACE("Compiling shader object %u\n", shader_obj);
4141 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4143 return shader_obj;
4146 static GLhandleARB find_glsl_pshader(const struct wined3d_context *context,
4147 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4148 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
4150 struct wined3d_state *state = &shader->device->stateBlock->state;
4151 UINT i;
4152 DWORD new_size;
4153 struct glsl_ps_compiled_shader *new_array;
4154 struct glsl_pshader_private *shader_data;
4155 struct ps_np2fixup_info *np2fixup = NULL;
4156 GLhandleARB ret;
4158 if (!shader->backend_data)
4160 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4161 if (!shader->backend_data)
4163 ERR("Failed to allocate backend data.\n");
4164 return 0;
4167 shader_data = shader->backend_data;
4169 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4170 * so a linear search is more performant than a hashmap or a binary search
4171 * (cache coherency etc)
4173 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4175 if (!memcmp(&shader_data->gl_shaders[i].args, args, sizeof(*args)))
4177 if (args->np2_fixup) *np2fixup_info = &shader_data->gl_shaders[i].np2fixup;
4178 return shader_data->gl_shaders[i].prgId;
4182 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4183 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4184 if (shader_data->num_gl_shaders)
4186 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4187 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders,
4188 new_size * sizeof(*shader_data->gl_shaders));
4189 } else {
4190 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader_data->gl_shaders));
4191 new_size = 1;
4194 if(!new_array) {
4195 ERR("Out of memory\n");
4196 return 0;
4198 shader_data->gl_shaders = new_array;
4199 shader_data->shader_array_size = new_size;
4202 shader_data->gl_shaders[shader_data->num_gl_shaders].args = *args;
4204 memset(&shader_data->gl_shaders[shader_data->num_gl_shaders].np2fixup, 0, sizeof(struct ps_np2fixup_info));
4205 if (args->np2_fixup) np2fixup = &shader_data->gl_shaders[shader_data->num_gl_shaders].np2fixup;
4207 pixelshader_update_samplers(&shader->reg_maps, state->textures);
4209 shader_buffer_clear(buffer);
4210 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4211 shader_data->gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4212 *np2fixup_info = np2fixup;
4214 return ret;
4217 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4218 const DWORD use_map) {
4219 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4220 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4221 return stored->fog_src == new->fog_src;
4224 static GLhandleARB find_glsl_vshader(const struct wined3d_context *context,
4225 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4226 const struct vs_compile_args *args)
4228 UINT i;
4229 DWORD new_size;
4230 struct glsl_vs_compiled_shader *new_array;
4231 DWORD use_map = shader->device->strided_streams.use_map;
4232 struct glsl_vshader_private *shader_data;
4233 GLhandleARB ret;
4235 if (!shader->backend_data)
4237 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4238 if (!shader->backend_data)
4240 ERR("Failed to allocate backend data.\n");
4241 return 0;
4244 shader_data = shader->backend_data;
4246 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4247 * so a linear search is more performant than a hashmap or a binary search
4248 * (cache coherency etc)
4250 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4251 if(vs_args_equal(&shader_data->gl_shaders[i].args, args, use_map)) {
4252 return shader_data->gl_shaders[i].prgId;
4256 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4258 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4259 if (shader_data->num_gl_shaders)
4261 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4262 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders,
4263 new_size * sizeof(*shader_data->gl_shaders));
4264 } else {
4265 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader_data->gl_shaders));
4266 new_size = 1;
4269 if(!new_array) {
4270 ERR("Out of memory\n");
4271 return 0;
4273 shader_data->gl_shaders = new_array;
4274 shader_data->shader_array_size = new_size;
4277 shader_data->gl_shaders[shader_data->num_gl_shaders].args = *args;
4279 shader_buffer_clear(buffer);
4280 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4281 shader_data->gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4283 return ret;
4286 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
4287 * It sets the programId on the current StateBlock (because it should be called
4288 * inside of the DrawPrimitive() part of the render loop).
4290 * If a program for the given combination does not exist, create one, and store
4291 * the program in the hash table. If it creates a program, it will link the
4292 * given objects, too.
4295 /* GL locking is done by the caller */
4296 static void set_glsl_shader_program(const struct wined3d_context *context,
4297 struct wined3d_device *device, BOOL use_ps, BOOL use_vs)
4299 const struct wined3d_state *state = &device->stateBlock->state;
4300 struct wined3d_shader *vshader = use_vs ? state->vertex_shader : NULL;
4301 struct wined3d_shader *pshader = use_ps ? state->pixel_shader : NULL;
4302 const struct wined3d_gl_info *gl_info = context->gl_info;
4303 struct shader_glsl_priv *priv = device->shader_priv;
4304 struct glsl_shader_prog_link *entry = NULL;
4305 GLhandleARB programId = 0;
4306 GLhandleARB reorder_shader_id = 0;
4307 unsigned int i;
4308 char glsl_name[8];
4309 struct ps_compile_args ps_compile_args;
4310 struct vs_compile_args vs_compile_args;
4312 if (vshader) find_vs_compile_args(state, vshader, &vs_compile_args);
4313 if (pshader) find_ps_compile_args(state, pshader, &ps_compile_args);
4315 entry = get_glsl_program_entry(priv, vshader, pshader, &vs_compile_args, &ps_compile_args);
4316 if (entry)
4318 priv->glsl_program = entry;
4319 return;
4322 /* If we get to this point, then no matching program exists, so we create one */
4323 programId = GL_EXTCALL(glCreateProgramObjectARB());
4324 TRACE("Created new GLSL shader program %u\n", programId);
4326 /* Create the entry */
4327 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
4328 entry->programId = programId;
4329 entry->vshader = vshader;
4330 entry->pshader = pshader;
4331 entry->vs_args = vs_compile_args;
4332 entry->ps_args = ps_compile_args;
4333 entry->constant_version = 0;
4334 entry->np2Fixup_info = NULL;
4335 /* Add the hash table entry */
4336 add_glsl_program_entry(priv, entry);
4338 /* Set the current program */
4339 priv->glsl_program = entry;
4341 /* Attach GLSL vshader */
4342 if (vshader)
4344 GLhandleARB vshader_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
4345 WORD map = vshader->reg_maps.input_registers;
4346 char tmp_name[10];
4348 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
4349 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
4350 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
4351 checkGLcall("glAttachObjectARB");
4352 /* Flag the reorder function for deletion, then it will be freed automatically when the program
4353 * is destroyed
4355 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
4357 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
4358 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
4359 checkGLcall("glAttachObjectARB");
4361 /* Bind vertex attributes to a corresponding index number to match
4362 * the same index numbers as ARB_vertex_programs (makes loading
4363 * vertex attributes simpler). With this method, we can use the
4364 * exact same code to load the attributes later for both ARB and
4365 * GLSL shaders.
4367 * We have to do this here because we need to know the Program ID
4368 * in order to make the bindings work, and it has to be done prior
4369 * to linking the GLSL program. */
4370 for (i = 0; map; map >>= 1, ++i)
4372 if (!(map & 1)) continue;
4374 snprintf(tmp_name, sizeof(tmp_name), "attrib%u", i);
4375 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
4377 checkGLcall("glBindAttribLocationARB");
4379 list_add_head(&vshader->linked_programs, &entry->vshader_entry);
4382 /* Attach GLSL pshader */
4383 if (pshader)
4385 GLhandleARB pshader_id = find_glsl_pshader(context, &priv->shader_buffer,
4386 pshader, &ps_compile_args, &entry->np2Fixup_info);
4387 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
4388 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
4389 checkGLcall("glAttachObjectARB");
4391 list_add_head(&pshader->linked_programs, &entry->pshader_entry);
4394 /* Link the program */
4395 TRACE("Linking GLSL shader program %u\n", programId);
4396 GL_EXTCALL(glLinkProgramARB(programId));
4397 shader_glsl_validate_link(gl_info, programId);
4399 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0,
4400 sizeof(GLhandleARB) * gl_info->limits.glsl_vs_float_constants);
4401 for (i = 0; i < gl_info->limits.glsl_vs_float_constants; ++i)
4403 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
4404 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4406 for (i = 0; i < MAX_CONST_I; ++i)
4408 snprintf(glsl_name, sizeof(glsl_name), "VI[%i]", i);
4409 entry->vuniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4411 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0,
4412 sizeof(GLhandleARB) * gl_info->limits.glsl_ps_float_constants);
4413 for (i = 0; i < gl_info->limits.glsl_ps_float_constants; ++i)
4415 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
4416 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4418 for (i = 0; i < MAX_CONST_I; ++i)
4420 snprintf(glsl_name, sizeof(glsl_name), "PI[%i]", i);
4421 entry->puniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4424 if(pshader) {
4425 char name[32];
4427 for(i = 0; i < MAX_TEXTURES; i++) {
4428 sprintf(name, "bumpenvmat%u", i);
4429 entry->bumpenvmat_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4430 sprintf(name, "luminancescale%u", i);
4431 entry->luminancescale_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4432 sprintf(name, "luminanceoffset%u", i);
4433 entry->luminanceoffset_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4436 if (ps_compile_args.np2_fixup) {
4437 if (entry->np2Fixup_info) {
4438 entry->np2Fixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "PsamplerNP2Fixup"));
4439 } else {
4440 FIXME("NP2 texcoord fixup needed for this pixelshader, but no fixup uniform found.\n");
4445 entry->posFixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
4446 entry->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
4447 checkGLcall("Find glsl program uniform locations");
4449 if (pshader && pshader->reg_maps.shader_version.major >= 3
4450 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
4452 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
4453 entry->vertex_color_clamp = GL_FALSE;
4454 } else {
4455 entry->vertex_color_clamp = GL_FIXED_ONLY_ARB;
4458 /* Set the shader to allow uniform loading on it */
4459 GL_EXTCALL(glUseProgramObjectARB(programId));
4460 checkGLcall("glUseProgramObjectARB(programId)");
4462 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
4463 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
4464 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
4465 * vertex shader with fixed function pixel processing is used we make sure that the card
4466 * supports enough samplers to allow the max number of vertex samplers with all possible
4467 * fixed function fragment processing setups. So once the program is linked these samplers
4468 * won't change.
4470 if (vshader) shader_glsl_load_vsamplers(gl_info, device->texUnitMap, programId);
4471 if (pshader) shader_glsl_load_psamplers(gl_info, device->texUnitMap, programId);
4473 /* If the local constants do not have to be loaded with the environment constants,
4474 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
4475 * later
4477 if (pshader && !pshader->load_local_constsF)
4478 hardcode_local_constants(pshader, gl_info, programId, 'P');
4479 if (vshader && !vshader->load_local_constsF)
4480 hardcode_local_constants(vshader, gl_info, programId, 'V');
4483 /* GL locking is done by the caller */
4484 static GLhandleARB create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum tex_types tex_type, BOOL masked)
4486 GLhandleARB program_id;
4487 GLhandleARB vshader_id, pshader_id;
4488 const char *blt_pshader;
4490 static const char *blt_vshader =
4491 "#version 120\n"
4492 "void main(void)\n"
4493 "{\n"
4494 " gl_Position = gl_Vertex;\n"
4495 " gl_FrontColor = vec4(1.0);\n"
4496 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
4497 "}\n";
4499 static const char * const blt_pshaders_full[tex_type_count] =
4501 /* tex_1d */
4502 NULL,
4503 /* tex_2d */
4504 "#version 120\n"
4505 "uniform sampler2D sampler;\n"
4506 "void main(void)\n"
4507 "{\n"
4508 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
4509 "}\n",
4510 /* tex_3d */
4511 NULL,
4512 /* tex_cube */
4513 "#version 120\n"
4514 "uniform samplerCube sampler;\n"
4515 "void main(void)\n"
4516 "{\n"
4517 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
4518 "}\n",
4519 /* tex_rect */
4520 "#version 120\n"
4521 "#extension GL_ARB_texture_rectangle : enable\n"
4522 "uniform sampler2DRect sampler;\n"
4523 "void main(void)\n"
4524 "{\n"
4525 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
4526 "}\n",
4529 static const char * const blt_pshaders_masked[tex_type_count] =
4531 /* tex_1d */
4532 NULL,
4533 /* tex_2d */
4534 "#version 120\n"
4535 "uniform sampler2D sampler;\n"
4536 "uniform vec4 mask;\n"
4537 "void main(void)\n"
4538 "{\n"
4539 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
4540 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
4541 "}\n",
4542 /* tex_3d */
4543 NULL,
4544 /* tex_cube */
4545 "#version 120\n"
4546 "uniform samplerCube sampler;\n"
4547 "uniform vec4 mask;\n"
4548 "void main(void)\n"
4549 "{\n"
4550 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
4551 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
4552 "}\n",
4553 /* tex_rect */
4554 "#version 120\n"
4555 "#extension GL_ARB_texture_rectangle : enable\n"
4556 "uniform sampler2DRect sampler;\n"
4557 "uniform vec4 mask;\n"
4558 "void main(void)\n"
4559 "{\n"
4560 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
4561 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
4562 "}\n",
4565 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
4566 if (!blt_pshader)
4568 FIXME("tex_type %#x not supported\n", tex_type);
4569 return 0;
4572 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4573 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
4575 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4576 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
4578 program_id = GL_EXTCALL(glCreateProgramObjectARB());
4579 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
4580 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
4581 GL_EXTCALL(glLinkProgramARB(program_id));
4583 shader_glsl_validate_link(gl_info, program_id);
4585 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
4586 * is destroyed
4588 GL_EXTCALL(glDeleteObjectARB(vshader_id));
4589 GL_EXTCALL(glDeleteObjectARB(pshader_id));
4590 return program_id;
4593 /* GL locking is done by the caller */
4594 static void shader_glsl_select(const struct wined3d_context *context, BOOL usePS, BOOL useVS)
4596 const struct wined3d_gl_info *gl_info = context->gl_info;
4597 struct wined3d_device *device = context->swapchain->device;
4598 struct shader_glsl_priv *priv = device->shader_priv;
4599 GLhandleARB program_id = 0;
4600 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
4602 old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
4604 if (useVS || usePS) set_glsl_shader_program(context, device, usePS, useVS);
4605 else priv->glsl_program = NULL;
4607 current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
4609 if (old_vertex_color_clamp != current_vertex_color_clamp)
4611 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
4613 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
4614 checkGLcall("glClampColorARB");
4616 else
4618 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
4622 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
4623 if (program_id) TRACE("Using GLSL program %u\n", program_id);
4624 GL_EXTCALL(glUseProgramObjectARB(program_id));
4625 checkGLcall("glUseProgramObjectARB");
4627 /* In case that NP2 texcoord fixup data is found for the selected program, trigger a reload of the
4628 * constants. This has to be done because it can't be guaranteed that sampler() (from state.c) is
4629 * called between selecting the shader and using it, which results in wrong fixup for some frames. */
4630 if (priv->glsl_program && priv->glsl_program->np2Fixup_info)
4632 shader_glsl_load_np2fixup_constants(priv, gl_info, &device->stateBlock->state);
4636 /* GL locking is done by the caller */
4637 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
4638 enum tex_types tex_type, const SIZE *ds_mask_size)
4640 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
4641 struct shader_glsl_priv *priv = shader_priv;
4642 GLhandleARB *blt_program;
4643 GLint loc;
4645 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
4646 if (!*blt_program)
4648 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
4649 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
4650 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
4651 GL_EXTCALL(glUniform1iARB(loc, 0));
4653 else
4655 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
4658 if (masked)
4660 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "mask"));
4661 GL_EXTCALL(glUniform4fARB(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
4665 /* GL locking is done by the caller */
4666 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
4668 struct shader_glsl_priv *priv = shader_priv;
4669 GLhandleARB program_id;
4671 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
4672 if (program_id) TRACE("Using GLSL program %u\n", program_id);
4674 GL_EXTCALL(glUseProgramObjectARB(program_id));
4675 checkGLcall("glUseProgramObjectARB");
4678 static void shader_glsl_destroy(struct wined3d_shader *shader)
4680 struct wined3d_device *device = shader->device;
4681 struct shader_glsl_priv *priv = device->shader_priv;
4682 const struct wined3d_gl_info *gl_info;
4683 const struct list *linked_programs;
4684 struct wined3d_context *context;
4686 char pshader = shader_is_pshader_version(shader->reg_maps.shader_version.type);
4688 if (pshader)
4690 struct glsl_pshader_private *shader_data = shader->backend_data;
4692 if (!shader_data || !shader_data->num_gl_shaders)
4694 HeapFree(GetProcessHeap(), 0, shader_data);
4695 shader->backend_data = NULL;
4696 return;
4699 context = context_acquire(device, NULL);
4700 gl_info = context->gl_info;
4702 if (priv->glsl_program && priv->glsl_program->pshader == shader)
4704 ENTER_GL();
4705 shader_glsl_select(context, FALSE, FALSE);
4706 LEAVE_GL();
4709 else
4711 struct glsl_vshader_private *shader_data = shader->backend_data;
4713 if (!shader_data || !shader_data->num_gl_shaders)
4715 HeapFree(GetProcessHeap(), 0, shader_data);
4716 shader->backend_data = NULL;
4717 return;
4720 context = context_acquire(device, NULL);
4721 gl_info = context->gl_info;
4723 if (priv->glsl_program && priv->glsl_program->vshader == shader)
4725 ENTER_GL();
4726 shader_glsl_select(context, FALSE, FALSE);
4727 LEAVE_GL();
4731 linked_programs = &shader->linked_programs;
4733 TRACE("Deleting linked programs\n");
4734 if (linked_programs->next) {
4735 struct glsl_shader_prog_link *entry, *entry2;
4737 ENTER_GL();
4738 if(pshader) {
4739 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
4740 delete_glsl_program_entry(priv, gl_info, entry);
4742 } else {
4743 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, vshader_entry) {
4744 delete_glsl_program_entry(priv, gl_info, entry);
4747 LEAVE_GL();
4750 if (pshader)
4752 struct glsl_pshader_private *shader_data = shader->backend_data;
4753 UINT i;
4755 ENTER_GL();
4756 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4757 TRACE("deleting pshader %u\n", shader_data->gl_shaders[i].prgId);
4758 GL_EXTCALL(glDeleteObjectARB(shader_data->gl_shaders[i].prgId));
4759 checkGLcall("glDeleteObjectARB");
4761 LEAVE_GL();
4762 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders);
4764 else
4766 struct glsl_vshader_private *shader_data = shader->backend_data;
4767 UINT i;
4769 ENTER_GL();
4770 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4771 TRACE("deleting vshader %u\n", shader_data->gl_shaders[i].prgId);
4772 GL_EXTCALL(glDeleteObjectARB(shader_data->gl_shaders[i].prgId));
4773 checkGLcall("glDeleteObjectARB");
4775 LEAVE_GL();
4776 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders);
4779 HeapFree(GetProcessHeap(), 0, shader->backend_data);
4780 shader->backend_data = NULL;
4782 context_release(context);
4785 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
4787 const struct glsl_program_key *k = key;
4788 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
4789 const struct glsl_shader_prog_link, program_lookup_entry);
4790 int cmp;
4792 if (k->vshader > prog->vshader) return 1;
4793 else if (k->vshader < prog->vshader) return -1;
4795 if (k->pshader > prog->pshader) return 1;
4796 else if (k->pshader < prog->pshader) return -1;
4798 if (k->vshader && (cmp = memcmp(&k->vs_args, &prog->vs_args, sizeof(prog->vs_args)))) return cmp;
4799 if (k->pshader && (cmp = memcmp(&k->ps_args, &prog->ps_args, sizeof(prog->ps_args)))) return cmp;
4801 return 0;
4804 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
4806 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries) + constant_count * sizeof(*heap->positions);
4807 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
4809 if (!mem)
4811 ERR("Failed to allocate memory\n");
4812 return FALSE;
4815 heap->entries = mem;
4816 heap->entries[1].version = 0;
4817 heap->positions = (unsigned int *)(heap->entries + constant_count + 1);
4818 heap->size = 1;
4820 return TRUE;
4823 static void constant_heap_free(struct constant_heap *heap)
4825 HeapFree(GetProcessHeap(), 0, heap->entries);
4828 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
4830 wined3d_rb_alloc,
4831 wined3d_rb_realloc,
4832 wined3d_rb_free,
4833 glsl_program_key_compare,
4836 static HRESULT shader_glsl_alloc(struct wined3d_device *device)
4838 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4839 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
4840 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
4841 gl_info->limits.glsl_ps_float_constants)) + 1;
4843 if (!shader_buffer_init(&priv->shader_buffer))
4845 ERR("Failed to initialize shader buffer.\n");
4846 goto fail;
4849 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
4850 if (!priv->stack)
4852 ERR("Failed to allocate memory.\n");
4853 goto fail;
4856 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
4858 ERR("Failed to initialize vertex shader constant heap\n");
4859 goto fail;
4862 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
4864 ERR("Failed to initialize pixel shader constant heap\n");
4865 goto fail;
4868 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
4870 ERR("Failed to initialize rbtree.\n");
4871 goto fail;
4874 priv->next_constant_version = 1;
4876 device->shader_priv = priv;
4877 return WINED3D_OK;
4879 fail:
4880 constant_heap_free(&priv->pconst_heap);
4881 constant_heap_free(&priv->vconst_heap);
4882 HeapFree(GetProcessHeap(), 0, priv->stack);
4883 shader_buffer_free(&priv->shader_buffer);
4884 HeapFree(GetProcessHeap(), 0, priv);
4885 return E_OUTOFMEMORY;
4888 /* Context activation is done by the caller. */
4889 static void shader_glsl_free(struct wined3d_device *device)
4891 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4892 struct shader_glsl_priv *priv = device->shader_priv;
4893 int i;
4895 ENTER_GL();
4896 for (i = 0; i < tex_type_count; ++i)
4898 if (priv->depth_blt_program_full[i])
4900 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_full[i]));
4902 if (priv->depth_blt_program_masked[i])
4904 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_masked[i]));
4907 LEAVE_GL();
4909 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
4910 constant_heap_free(&priv->pconst_heap);
4911 constant_heap_free(&priv->vconst_heap);
4912 HeapFree(GetProcessHeap(), 0, priv->stack);
4913 shader_buffer_free(&priv->shader_buffer);
4915 HeapFree(GetProcessHeap(), 0, device->shader_priv);
4916 device->shader_priv = NULL;
4919 static void shader_glsl_context_destroyed(void *shader_priv, const struct wined3d_context *context) {}
4921 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
4923 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_GEOMETRY_SHADER4]
4924 && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50))
4926 caps->VertexShaderVersion = 4;
4927 caps->PixelShaderVersion = 4;
4929 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
4930 * texldd and texldl instructions. */
4931 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
4933 caps->VertexShaderVersion = 3;
4934 caps->PixelShaderVersion = 3;
4936 else
4938 caps->VertexShaderVersion = 2;
4939 caps->PixelShaderVersion = 2;
4942 caps->MaxVertexShaderConst = gl_info->limits.glsl_vs_float_constants;
4943 caps->MaxPixelShaderConst = gl_info->limits.glsl_ps_float_constants;
4945 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
4946 * Direct3D minimum requirement.
4948 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
4949 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
4951 * The problem is that the refrast clamps temporary results in the shader to
4952 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
4953 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
4954 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
4955 * offer a way to query this.
4957 caps->PixelShader1xMaxValue = 8.0;
4959 caps->VSClipping = TRUE;
4961 TRACE_(d3d_caps)("Hardware vertex shader version %u enabled (GLSL).\n",
4962 caps->VertexShaderVersion);
4963 TRACE_(d3d_caps)("Hardware pixel shader version %u enabled (GLSL).\n",
4964 caps->PixelShaderVersion);
4967 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
4969 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
4971 TRACE("Checking support for fixup:\n");
4972 dump_color_fixup_desc(fixup);
4975 /* We support everything except YUV conversions. */
4976 if (!is_complex_fixup(fixup))
4978 TRACE("[OK]\n");
4979 return TRUE;
4982 TRACE("[FAILED]\n");
4983 return FALSE;
4986 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
4988 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
4989 /* WINED3DSIH_ADD */ shader_glsl_arith,
4990 /* WINED3DSIH_AND */ NULL,
4991 /* WINED3DSIH_BEM */ shader_glsl_bem,
4992 /* WINED3DSIH_BREAK */ shader_glsl_break,
4993 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
4994 /* WINED3DSIH_BREAKP */ NULL,
4995 /* WINED3DSIH_CALL */ shader_glsl_call,
4996 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
4997 /* WINED3DSIH_CMP */ shader_glsl_cmp,
4998 /* WINED3DSIH_CND */ shader_glsl_cnd,
4999 /* WINED3DSIH_CRS */ shader_glsl_cross,
5000 /* WINED3DSIH_CUT */ NULL,
5001 /* WINED3DSIH_DCL */ NULL,
5002 /* WINED3DSIH_DEF */ NULL,
5003 /* WINED3DSIH_DEFB */ NULL,
5004 /* WINED3DSIH_DEFI */ NULL,
5005 /* WINED3DSIH_DIV */ NULL,
5006 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
5007 /* WINED3DSIH_DP3 */ shader_glsl_dot,
5008 /* WINED3DSIH_DP4 */ shader_glsl_dot,
5009 /* WINED3DSIH_DST */ shader_glsl_dst,
5010 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
5011 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
5012 /* WINED3DSIH_ELSE */ shader_glsl_else,
5013 /* WINED3DSIH_EMIT */ NULL,
5014 /* WINED3DSIH_ENDIF */ shader_glsl_end,
5015 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
5016 /* WINED3DSIH_ENDREP */ shader_glsl_end,
5017 /* WINED3DSIH_EQ */ NULL,
5018 /* WINED3DSIH_EXP */ shader_glsl_map2gl,
5019 /* WINED3DSIH_EXPP */ shader_glsl_expp,
5020 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
5021 /* WINED3DSIH_FTOI */ NULL,
5022 /* WINED3DSIH_GE */ NULL,
5023 /* WINED3DSIH_IADD */ NULL,
5024 /* WINED3DSIH_IEQ */ NULL,
5025 /* WINED3DSIH_IF */ shader_glsl_if,
5026 /* WINED3DSIH_IFC */ shader_glsl_ifc,
5027 /* WINED3DSIH_IGE */ NULL,
5028 /* WINED3DSIH_IMUL */ NULL,
5029 /* WINED3DSIH_ITOF */ NULL,
5030 /* WINED3DSIH_LABEL */ shader_glsl_label,
5031 /* WINED3DSIH_LD */ NULL,
5032 /* WINED3DSIH_LIT */ shader_glsl_lit,
5033 /* WINED3DSIH_LOG */ shader_glsl_log,
5034 /* WINED3DSIH_LOGP */ shader_glsl_log,
5035 /* WINED3DSIH_LOOP */ shader_glsl_loop,
5036 /* WINED3DSIH_LRP */ shader_glsl_lrp,
5037 /* WINED3DSIH_LT */ NULL,
5038 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
5039 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
5040 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
5041 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
5042 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
5043 /* WINED3DSIH_MAD */ shader_glsl_mad,
5044 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
5045 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
5046 /* WINED3DSIH_MOV */ shader_glsl_mov,
5047 /* WINED3DSIH_MOVA */ shader_glsl_mov,
5048 /* WINED3DSIH_MOVC */ NULL,
5049 /* WINED3DSIH_MUL */ shader_glsl_arith,
5050 /* WINED3DSIH_NOP */ NULL,
5051 /* WINED3DSIH_NRM */ shader_glsl_nrm,
5052 /* WINED3DSIH_PHASE */ NULL,
5053 /* WINED3DSIH_POW */ shader_glsl_pow,
5054 /* WINED3DSIH_RCP */ shader_glsl_rcp,
5055 /* WINED3DSIH_REP */ shader_glsl_rep,
5056 /* WINED3DSIH_RET */ shader_glsl_ret,
5057 /* WINED3DSIH_ROUND_NI */ NULL,
5058 /* WINED3DSIH_RSQ */ shader_glsl_rsq,
5059 /* WINED3DSIH_SAMPLE */ NULL,
5060 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
5061 /* WINED3DSIH_SAMPLE_LOD */ NULL,
5062 /* WINED3DSIH_SETP */ NULL,
5063 /* WINED3DSIH_SGE */ shader_glsl_compare,
5064 /* WINED3DSIH_SGN */ shader_glsl_sgn,
5065 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
5066 /* WINED3DSIH_SLT */ shader_glsl_compare,
5067 /* WINED3DSIH_SQRT */ NULL,
5068 /* WINED3DSIH_SUB */ shader_glsl_arith,
5069 /* WINED3DSIH_TEX */ shader_glsl_tex,
5070 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
5071 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
5072 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
5073 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
5074 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
5075 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
5076 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
5077 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
5078 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
5079 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
5080 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
5081 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
5082 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
5083 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
5084 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
5085 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
5086 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
5087 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
5088 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
5089 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
5090 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
5091 /* WINED3DSIH_UDIV */ NULL,
5092 /* WINED3DSIH_USHR */ NULL,
5093 /* WINED3DSIH_UTOF */ NULL,
5094 /* WINED3DSIH_XOR */ NULL,
5097 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
5098 SHADER_HANDLER hw_fct;
5100 /* Select handler */
5101 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
5103 /* Unhandled opcode */
5104 if (!hw_fct)
5106 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
5107 return;
5109 hw_fct(ins);
5111 shader_glsl_add_instruction_modifiers(ins);
5114 const struct wined3d_shader_backend_ops glsl_shader_backend =
5116 shader_glsl_handle_instruction,
5117 shader_glsl_select,
5118 shader_glsl_select_depth_blt,
5119 shader_glsl_deselect_depth_blt,
5120 shader_glsl_update_float_vertex_constants,
5121 shader_glsl_update_float_pixel_constants,
5122 shader_glsl_load_constants,
5123 shader_glsl_load_np2fixup_constants,
5124 shader_glsl_destroy,
5125 shader_glsl_alloc,
5126 shader_glsl_free,
5127 shader_glsl_context_destroyed,
5128 shader_glsl_get_caps,
5129 shader_glsl_color_fixup_supported,