wined3d: Get rid of the WINED3DTEXTURETRANSFORMFLAGS typedef.
[wine.git] / dlls / wined3d / glsl_shader.c
blob27dbfb095718e62b43bffbd0b88320237a7534a0
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][WINED3D_TSS_BUMPENV_MAT00];
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][WINED3D_TSS_BUMPENV_LSCALE];
789 const GLfloat *offset = (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET];
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 UINT in_count = min(vec4_varyings(reg_maps->shader_version.major, gl_info), shader->limits.packed_input);
1134 if (use_vs(state))
1135 shader_addline(buffer, "varying vec4 IN[%u];\n", in_count);
1136 else
1137 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
1138 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
1139 * pixel shader that reads the fixed function color into the packed input registers. */
1140 shader_addline(buffer, "vec4 IN[%u];\n", in_count);
1143 /* Declare output register temporaries */
1144 if (shader->limits.packed_output)
1145 shader_addline(buffer, "vec4 OUT[%u];\n", shader->limits.packed_output);
1147 /* Declare temporary variables */
1148 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1150 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1153 /* Declare attributes */
1154 if (reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX)
1156 for (i = 0, map = reg_maps->input_registers; map; map >>= 1, ++i)
1158 if (map & 1) shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
1162 /* Declare loop registers aLx */
1163 for (i = 0; i < reg_maps->loop_depth; i++) {
1164 shader_addline(buffer, "int aL%u;\n", i);
1165 shader_addline(buffer, "int tmpInt%u;\n", i);
1168 /* Temporary variables for matrix operations */
1169 shader_addline(buffer, "vec4 tmp0;\n");
1170 shader_addline(buffer, "vec4 tmp1;\n");
1172 /* Local constants use a different name so they can be loaded once at shader link time
1173 * They can't be hardcoded into the shader text via LC = {x, y, z, w}; because the
1174 * float -> string conversion can cause precision loss.
1176 if (!shader->load_local_constsF)
1178 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1180 shader_addline(buffer, "uniform vec4 %cLC%u;\n", prefix, lconst->idx);
1184 /* Start the main program */
1185 shader_addline(buffer, "void main() {\n");
1186 if(pshader && reg_maps->vpos) {
1187 /* DirectX apps expect integer values, while OpenGL drivers add approximately 0.5. This causes
1188 * off-by-one problems as spotted by the vPos d3d9 visual test. Unfortunately the ATI cards do
1189 * not add exactly 0.5, but rather something like 0.49999999 or 0.50000001, which still causes
1190 * precision troubles when we just subtract 0.5.
1192 * To deal with that just floor() the position. This will eliminate the fraction on all cards.
1194 * TODO: Test how that behaves with multisampling once we can enable multisampling in winex11.
1196 * An advantage of floor is that it works even if the driver doesn't add 1/2. It is somewhat
1197 * questionable if 1.5, 2.5, ... are the proper values to return in gl_FragCoord, even though
1198 * coordinates specify the pixel centers instead of the pixel corners. This code will behave
1199 * correctly on drivers that returns integer values.
1201 shader_addline(buffer, "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1205 /*****************************************************************************
1206 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1208 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1209 ****************************************************************************/
1211 /* Prototypes */
1212 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1213 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1215 /** Used for opcode modifiers - They multiply the result by the specified amount */
1216 static const char * const shift_glsl_tab[] = {
1217 "", /* 0 (none) */
1218 "2.0 * ", /* 1 (x2) */
1219 "4.0 * ", /* 2 (x4) */
1220 "8.0 * ", /* 3 (x8) */
1221 "16.0 * ", /* 4 (x16) */
1222 "32.0 * ", /* 5 (x32) */
1223 "", /* 6 (x64) */
1224 "", /* 7 (x128) */
1225 "", /* 8 (d256) */
1226 "", /* 9 (d128) */
1227 "", /* 10 (d64) */
1228 "", /* 11 (d32) */
1229 "0.0625 * ", /* 12 (d16) */
1230 "0.125 * ", /* 13 (d8) */
1231 "0.25 * ", /* 14 (d4) */
1232 "0.5 * " /* 15 (d2) */
1235 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1236 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1237 const char *in_reg, const char *in_regswizzle, char *out_str)
1239 out_str[0] = 0;
1241 switch (src_modifier)
1243 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1244 case WINED3DSPSM_DW:
1245 case WINED3DSPSM_NONE:
1246 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1247 break;
1248 case WINED3DSPSM_NEG:
1249 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1250 break;
1251 case WINED3DSPSM_NOT:
1252 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1253 break;
1254 case WINED3DSPSM_BIAS:
1255 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1256 break;
1257 case WINED3DSPSM_BIASNEG:
1258 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1259 break;
1260 case WINED3DSPSM_SIGN:
1261 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1262 break;
1263 case WINED3DSPSM_SIGNNEG:
1264 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1265 break;
1266 case WINED3DSPSM_COMP:
1267 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1268 break;
1269 case WINED3DSPSM_X2:
1270 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1271 break;
1272 case WINED3DSPSM_X2NEG:
1273 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1274 break;
1275 case WINED3DSPSM_ABS:
1276 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1277 break;
1278 case WINED3DSPSM_ABSNEG:
1279 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1280 break;
1281 default:
1282 FIXME("Unhandled modifier %u\n", src_modifier);
1283 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1287 /** Writes the GLSL variable name that corresponds to the register that the
1288 * DX opcode parameter is trying to access */
1289 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1290 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1292 /* oPos, oFog and oPts in D3D */
1293 static const char * const hwrastout_reg_names[] = {"OUT[10]", "OUT[11].x", "OUT[11].y"};
1295 const struct wined3d_shader *shader = ins->ctx->shader;
1296 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1297 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1298 char pshader = shader_is_pshader_version(reg_maps->shader_version.type);
1300 *is_color = FALSE;
1302 switch (reg->type)
1304 case WINED3DSPR_TEMP:
1305 sprintf(register_name, "R%u", reg->idx);
1306 break;
1308 case WINED3DSPR_INPUT:
1309 /* vertex shaders */
1310 if (!pshader)
1312 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1313 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx)) *is_color = TRUE;
1314 sprintf(register_name, "attrib%u", reg->idx);
1315 break;
1318 /* pixel shaders >= 3.0 */
1319 if (reg_maps->shader_version.major >= 3)
1321 DWORD idx = shader->u.ps.input_reg_map[reg->idx];
1322 unsigned int in_count = vec4_varyings(reg_maps->shader_version.major, gl_info);
1324 if (reg->rel_addr)
1326 struct glsl_src_param rel_param;
1328 shader_glsl_add_src_param(ins, reg->rel_addr, WINED3DSP_WRITEMASK_0, &rel_param);
1330 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
1331 * operation there */
1332 if (idx)
1334 if (shader->u.ps.declared_in_count > in_count)
1336 sprintf(register_name,
1337 "((%s + %u) > %d ? (%s + %u) > %d ? gl_SecondaryColor : gl_Color : IN[%s + %u])",
1338 rel_param.param_str, idx, in_count - 1, rel_param.param_str, idx, in_count,
1339 rel_param.param_str, idx);
1341 else
1343 sprintf(register_name, "IN[%s + %u]", rel_param.param_str, idx);
1346 else
1348 if (shader->u.ps.declared_in_count > in_count)
1350 sprintf(register_name, "((%s) > %d ? (%s) > %d ? gl_SecondaryColor : gl_Color : IN[%s])",
1351 rel_param.param_str, in_count - 1, rel_param.param_str, in_count,
1352 rel_param.param_str);
1354 else
1356 sprintf(register_name, "IN[%s]", rel_param.param_str);
1360 else
1362 if (idx == in_count) sprintf(register_name, "gl_Color");
1363 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
1364 else sprintf(register_name, "IN[%u]", idx);
1367 else
1369 if (!reg->idx) strcpy(register_name, "gl_Color");
1370 else strcpy(register_name, "gl_SecondaryColor");
1371 break;
1373 break;
1375 case WINED3DSPR_CONST:
1377 const char prefix = pshader ? 'P' : 'V';
1379 /* Relative addressing */
1380 if (reg->rel_addr)
1382 struct glsl_src_param rel_param;
1383 shader_glsl_add_src_param(ins, reg->rel_addr, WINED3DSP_WRITEMASK_0, &rel_param);
1384 if (reg->idx) sprintf(register_name, "%cC[%s + %u]", prefix, rel_param.param_str, reg->idx);
1385 else sprintf(register_name, "%cC[%s]", prefix, rel_param.param_str);
1387 else
1389 if (shader_constant_is_local(shader, reg->idx))
1390 sprintf(register_name, "%cLC%u", prefix, reg->idx);
1391 else
1392 sprintf(register_name, "%cC[%u]", prefix, reg->idx);
1395 break;
1397 case WINED3DSPR_CONSTINT:
1398 if (pshader) sprintf(register_name, "PI[%u]", reg->idx);
1399 else sprintf(register_name, "VI[%u]", reg->idx);
1400 break;
1402 case WINED3DSPR_CONSTBOOL:
1403 if (pshader) sprintf(register_name, "PB[%u]", reg->idx);
1404 else sprintf(register_name, "VB[%u]", reg->idx);
1405 break;
1407 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1408 if (pshader) sprintf(register_name, "T%u", reg->idx);
1409 else sprintf(register_name, "A%u", reg->idx);
1410 break;
1412 case WINED3DSPR_LOOP:
1413 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
1414 break;
1416 case WINED3DSPR_SAMPLER:
1417 if (pshader) sprintf(register_name, "Psampler%u", reg->idx);
1418 else sprintf(register_name, "Vsampler%u", reg->idx);
1419 break;
1421 case WINED3DSPR_COLOROUT:
1422 if (reg->idx >= gl_info->limits.buffers)
1423 WARN("Write to render target %u, only %d supported.\n", reg->idx, gl_info->limits.buffers);
1425 sprintf(register_name, "gl_FragData[%u]", reg->idx);
1426 break;
1428 case WINED3DSPR_RASTOUT:
1429 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx]);
1430 break;
1432 case WINED3DSPR_DEPTHOUT:
1433 sprintf(register_name, "gl_FragDepth");
1434 break;
1436 case WINED3DSPR_ATTROUT:
1437 if (!reg->idx) sprintf(register_name, "OUT[8]");
1438 else sprintf(register_name, "OUT[9]");
1439 break;
1441 case WINED3DSPR_TEXCRDOUT:
1442 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1443 sprintf(register_name, "OUT[%u]", reg->idx);
1444 break;
1446 case WINED3DSPR_MISCTYPE:
1447 if (!reg->idx)
1449 /* vPos */
1450 sprintf(register_name, "vpos");
1452 else if (reg->idx == 1)
1454 /* Note that gl_FrontFacing is a bool, while vFace is
1455 * a float for which the sign determines front/back */
1456 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1458 else
1460 FIXME("Unhandled misctype register %d\n", reg->idx);
1461 sprintf(register_name, "unrecognized_register");
1463 break;
1465 case WINED3DSPR_IMMCONST:
1466 switch (reg->immconst_type)
1468 case WINED3D_IMMCONST_SCALAR:
1469 sprintf(register_name, "%.8e", *(const float *)reg->immconst_data);
1470 break;
1472 case WINED3D_IMMCONST_VEC4:
1473 sprintf(register_name, "vec4(%.8e, %.8e, %.8e, %.8e)",
1474 *(const float *)&reg->immconst_data[0], *(const float *)&reg->immconst_data[1],
1475 *(const float *)&reg->immconst_data[2], *(const float *)&reg->immconst_data[3]);
1476 break;
1478 default:
1479 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
1480 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
1482 break;
1484 default:
1485 FIXME("Unhandled register name Type(%d)\n", reg->type);
1486 sprintf(register_name, "unrecognized_register");
1487 break;
1491 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1493 *str++ = '.';
1494 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1495 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1496 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1497 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1498 *str = '\0';
1501 /* Get the GLSL write mask for the destination register */
1502 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1504 DWORD mask = param->write_mask;
1506 if (shader_is_scalar(&param->reg))
1508 mask = WINED3DSP_WRITEMASK_0;
1509 *write_mask = '\0';
1511 else
1513 shader_glsl_write_mask_to_str(mask, write_mask);
1516 return mask;
1519 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1520 unsigned int size = 0;
1522 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1523 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1524 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1525 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1527 return size;
1530 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
1532 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1533 * but addressed as "rgba". To fix this we need to swap the register's x
1534 * and z components. */
1535 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1537 *str++ = '.';
1538 /* swizzle bits fields: wwzzyyxx */
1539 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
1540 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
1541 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
1542 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
1543 *str = '\0';
1546 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
1547 BOOL fixup, DWORD mask, char *swizzle_str)
1549 if (shader_is_scalar(&param->reg))
1550 *swizzle_str = '\0';
1551 else
1552 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
1555 /* From a given parameter token, generate the corresponding GLSL string.
1556 * Also, return the actual register name and swizzle in case the
1557 * caller needs this information as well. */
1558 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1559 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
1561 BOOL is_color = FALSE;
1562 char swizzle_str[6];
1564 glsl_src->reg_name[0] = '\0';
1565 glsl_src->param_str[0] = '\0';
1566 swizzle_str[0] = '\0';
1568 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
1569 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
1570 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
1573 /* From a given parameter token, generate the corresponding GLSL string.
1574 * Also, return the actual register name and swizzle in case the
1575 * caller needs this information as well. */
1576 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1577 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
1579 BOOL is_color = FALSE;
1581 glsl_dst->mask_str[0] = '\0';
1582 glsl_dst->reg_name[0] = '\0';
1584 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
1585 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1588 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1589 static DWORD shader_glsl_append_dst_ext(struct wined3d_shader_buffer *buffer,
1590 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst)
1592 struct glsl_dst_param glsl_dst;
1593 DWORD mask;
1595 mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst);
1596 if (mask) shader_addline(buffer, "%s%s = %s(", glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1598 return mask;
1601 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1602 static DWORD shader_glsl_append_dst(struct wined3d_shader_buffer *buffer, const struct wined3d_shader_instruction *ins)
1604 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
1607 /** Process GLSL instruction modifiers */
1608 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1610 struct glsl_dst_param dst_param;
1611 DWORD modifiers;
1613 if (!ins->dst_count) return;
1615 modifiers = ins->dst[0].modifiers;
1616 if (!modifiers) return;
1618 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
1620 if (modifiers & WINED3DSPDM_SATURATE)
1622 /* _SAT means to clamp the value of the register to between 0 and 1 */
1623 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1624 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1627 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
1629 FIXME("_centroid modifier not handled\n");
1632 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
1634 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1638 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
1640 switch (op)
1642 case WINED3D_SHADER_REL_OP_GT: return ">";
1643 case WINED3D_SHADER_REL_OP_EQ: return "==";
1644 case WINED3D_SHADER_REL_OP_GE: return ">=";
1645 case WINED3D_SHADER_REL_OP_LT: return "<";
1646 case WINED3D_SHADER_REL_OP_NE: return "!=";
1647 case WINED3D_SHADER_REL_OP_LE: return "<=";
1648 default:
1649 FIXME("Unrecognized operator %#x.\n", op);
1650 return "(\?\?)";
1654 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
1655 DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
1657 enum wined3d_sampler_texture_type sampler_type = ctx->reg_maps->sampler_type[sampler_idx];
1658 const struct wined3d_gl_info *gl_info = ctx->gl_info;
1659 BOOL shadow = shader_is_pshader_version(ctx->reg_maps->shader_version.type)
1660 && (((const struct shader_glsl_ctx_priv *)ctx->backend_data)->cur_ps_args->shadow & (1 << sampler_idx));
1661 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
1662 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_RECT;
1663 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
1664 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
1666 /* Note that there's no such thing as a projected cube texture. */
1667 switch(sampler_type) {
1668 case WINED3DSTT_1D:
1669 if (shadow)
1671 if (lod)
1673 sample_function->name = projected ? "shadow1DProjLod" : "shadow1DLod";
1675 else if (grad)
1677 if (gl_info->supported[EXT_GPU_SHADER4])
1678 sample_function->name = projected ? "shadow1DProjGrad" : "shadow1DGrad";
1679 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1680 sample_function->name = projected ? "shadow1DProjGradARB" : "shadow1DGradARB";
1681 else
1683 FIXME("Unsupported 1D shadow grad function.\n");
1684 sample_function->name = "unsupported1DGrad";
1687 else
1689 sample_function->name = projected ? "shadow1DProj" : "shadow1D";
1691 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1693 else
1695 if (lod)
1697 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
1699 else if (grad)
1701 if (gl_info->supported[EXT_GPU_SHADER4])
1702 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
1703 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1704 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
1705 else
1707 FIXME("Unsupported 1D grad function.\n");
1708 sample_function->name = "unsupported1DGrad";
1711 else
1713 sample_function->name = projected ? "texture1DProj" : "texture1D";
1715 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1717 break;
1719 case WINED3DSTT_2D:
1720 if (shadow)
1722 if (texrect)
1724 if (lod)
1726 sample_function->name = projected ? "shadow2DRectProjLod" : "shadow2DRectLod";
1728 else if (grad)
1730 if (gl_info->supported[EXT_GPU_SHADER4])
1731 sample_function->name = projected ? "shadow2DRectProjGrad" : "shadow2DRectGrad";
1732 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1733 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
1734 else
1736 FIXME("Unsupported RECT shadow grad function.\n");
1737 sample_function->name = "unsupported2DRectGrad";
1740 else
1742 sample_function->name = projected ? "shadow2DRectProj" : "shadow2DRect";
1745 else
1747 if (lod)
1749 sample_function->name = projected ? "shadow2DProjLod" : "shadow2DLod";
1751 else if (grad)
1753 if (gl_info->supported[EXT_GPU_SHADER4])
1754 sample_function->name = projected ? "shadow2DProjGrad" : "shadow2DGrad";
1755 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1756 sample_function->name = projected ? "shadow2DProjGradARB" : "shadow2DGradARB";
1757 else
1759 FIXME("Unsupported 2D shadow grad function.\n");
1760 sample_function->name = "unsupported2DGrad";
1763 else
1765 sample_function->name = projected ? "shadow2DProj" : "shadow2D";
1768 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1770 else
1772 if (texrect)
1774 if (lod)
1776 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
1778 else if (grad)
1780 if (gl_info->supported[EXT_GPU_SHADER4])
1781 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
1782 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1783 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
1784 else
1786 FIXME("Unsupported RECT grad function.\n");
1787 sample_function->name = "unsupported2DRectGrad";
1790 else
1792 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
1795 else
1797 if (lod)
1799 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
1801 else if (grad)
1803 if (gl_info->supported[EXT_GPU_SHADER4])
1804 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
1805 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1806 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
1807 else
1809 FIXME("Unsupported 2D grad function.\n");
1810 sample_function->name = "unsupported2DGrad";
1813 else
1815 sample_function->name = projected ? "texture2DProj" : "texture2D";
1818 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1820 break;
1822 case WINED3DSTT_CUBE:
1823 if (shadow)
1825 FIXME("Unsupported Cube shadow function.\n");
1826 sample_function->name = "unsupportedCubeShadow";
1827 sample_function->coord_mask = 0;
1829 else
1831 if (lod)
1833 sample_function->name = "textureCubeLod";
1835 else if (grad)
1837 if (gl_info->supported[EXT_GPU_SHADER4])
1838 sample_function->name = "textureCubeGrad";
1839 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1840 sample_function->name = "textureCubeGradARB";
1841 else
1843 FIXME("Unsupported Cube grad function.\n");
1844 sample_function->name = "unsupportedCubeGrad";
1847 else
1849 sample_function->name = "textureCube";
1851 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1853 break;
1855 case WINED3DSTT_VOLUME:
1856 if (shadow)
1858 FIXME("Unsupported 3D shadow function.\n");
1859 sample_function->name = "unsupported3DShadow";
1860 sample_function->coord_mask = 0;
1862 else
1864 if (lod)
1866 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
1868 else if (grad)
1870 if (gl_info->supported[EXT_GPU_SHADER4])
1871 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
1872 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1873 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
1874 else
1876 FIXME("Unsupported 3D grad function.\n");
1877 sample_function->name = "unsupported3DGrad";
1880 else
1882 sample_function->name = projected ? "texture3DProj" : "texture3D";
1884 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1886 break;
1888 default:
1889 sample_function->name = "";
1890 sample_function->coord_mask = 0;
1891 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
1892 break;
1896 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
1897 BOOL sign_fixup, enum fixup_channel_source channel_source)
1899 switch(channel_source)
1901 case CHANNEL_SOURCE_ZERO:
1902 strcat(arguments, "0.0");
1903 break;
1905 case CHANNEL_SOURCE_ONE:
1906 strcat(arguments, "1.0");
1907 break;
1909 case CHANNEL_SOURCE_X:
1910 strcat(arguments, reg_name);
1911 strcat(arguments, ".x");
1912 break;
1914 case CHANNEL_SOURCE_Y:
1915 strcat(arguments, reg_name);
1916 strcat(arguments, ".y");
1917 break;
1919 case CHANNEL_SOURCE_Z:
1920 strcat(arguments, reg_name);
1921 strcat(arguments, ".z");
1922 break;
1924 case CHANNEL_SOURCE_W:
1925 strcat(arguments, reg_name);
1926 strcat(arguments, ".w");
1927 break;
1929 default:
1930 FIXME("Unhandled channel source %#x\n", channel_source);
1931 strcat(arguments, "undefined");
1932 break;
1935 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
1938 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
1940 struct wined3d_shader_dst_param dst;
1941 unsigned int mask_size, remaining;
1942 struct glsl_dst_param dst_param;
1943 char arguments[256];
1944 DWORD mask;
1946 mask = 0;
1947 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) mask |= WINED3DSP_WRITEMASK_0;
1948 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) mask |= WINED3DSP_WRITEMASK_1;
1949 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) mask |= WINED3DSP_WRITEMASK_2;
1950 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) mask |= WINED3DSP_WRITEMASK_3;
1951 mask &= ins->dst[0].write_mask;
1953 if (!mask) return; /* Nothing to do */
1955 if (is_complex_fixup(fixup))
1957 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
1958 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
1959 return;
1962 mask_size = shader_glsl_get_write_mask_size(mask);
1964 dst = ins->dst[0];
1965 dst.write_mask = mask;
1966 shader_glsl_add_dst_param(ins, &dst, &dst_param);
1968 arguments[0] = '\0';
1969 remaining = mask_size;
1970 if (mask & WINED3DSP_WRITEMASK_0)
1972 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.x_sign_fixup, fixup.x_source);
1973 if (--remaining) strcat(arguments, ", ");
1975 if (mask & WINED3DSP_WRITEMASK_1)
1977 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.y_sign_fixup, fixup.y_source);
1978 if (--remaining) strcat(arguments, ", ");
1980 if (mask & WINED3DSP_WRITEMASK_2)
1982 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.z_sign_fixup, fixup.z_source);
1983 if (--remaining) strcat(arguments, ", ");
1985 if (mask & WINED3DSP_WRITEMASK_3)
1987 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.w_sign_fixup, fixup.w_source);
1988 if (--remaining) strcat(arguments, ", ");
1991 if (mask_size > 1)
1993 shader_addline(ins->ctx->buffer, "%s%s = vec%u(%s);\n",
1994 dst_param.reg_name, dst_param.mask_str, mask_size, arguments);
1996 else
1998 shader_addline(ins->ctx->buffer, "%s%s = %s;\n", dst_param.reg_name, dst_param.mask_str, arguments);
2002 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2003 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2004 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2006 const char *sampler_base;
2007 char dst_swizzle[6];
2008 struct color_fixup_desc fixup;
2009 BOOL np2_fixup = FALSE;
2010 va_list args;
2012 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2014 if (shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type))
2016 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2017 fixup = priv->cur_ps_args->color_fixup[sampler];
2018 sampler_base = "Psampler";
2020 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2021 if(bias) {
2022 FIXME("Biased sampling from NP2 textures is unsupported\n");
2023 } else {
2024 np2_fixup = TRUE;
2027 } else {
2028 sampler_base = "Vsampler";
2029 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
2032 shader_glsl_append_dst(ins->ctx->buffer, ins);
2034 shader_addline(ins->ctx->buffer, "%s(%s%u, ", sample_function->name, sampler_base, sampler);
2036 va_start(args, coord_reg_fmt);
2037 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2038 va_end(args);
2040 if(bias) {
2041 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2042 } else {
2043 if (np2_fixup) {
2044 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2045 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2047 shader_addline(ins->ctx->buffer, " * PsamplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2048 (idx % 2) ? "zw" : "xy", dst_swizzle);
2049 } else if(dx && dy) {
2050 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2051 } else {
2052 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2056 if(!is_identity_fixup(fixup)) {
2057 shader_glsl_color_correction(ins, fixup);
2061 /*****************************************************************************
2062 * Begin processing individual instruction opcodes
2063 ****************************************************************************/
2065 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
2066 static void shader_glsl_arith(const struct wined3d_shader_instruction *ins)
2068 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2069 struct glsl_src_param src0_param;
2070 struct glsl_src_param src1_param;
2071 DWORD write_mask;
2072 char op;
2074 /* Determine the GLSL operator to use based on the opcode */
2075 switch (ins->handler_idx)
2077 case WINED3DSIH_MUL: op = '*'; break;
2078 case WINED3DSIH_ADD: op = '+'; break;
2079 case WINED3DSIH_SUB: op = '-'; break;
2080 default:
2081 op = ' ';
2082 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2083 break;
2086 write_mask = shader_glsl_append_dst(buffer, ins);
2087 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2088 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2089 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
2092 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2093 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2095 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2096 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2097 struct glsl_src_param src0_param;
2098 DWORD write_mask;
2100 write_mask = shader_glsl_append_dst(buffer, ins);
2101 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2103 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2104 * shader versions WINED3DSIO_MOVA is used for this. */
2105 if (ins->ctx->reg_maps->shader_version.major == 1
2106 && !shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type)
2107 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2109 /* This is a simple floor() */
2110 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2111 if (mask_size > 1) {
2112 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2113 } else {
2114 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2117 else if(ins->handler_idx == WINED3DSIH_MOVA)
2119 /* We need to *round* to the nearest int here. */
2120 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2122 if (gl_info->supported[EXT_GPU_SHADER4])
2124 if (mask_size > 1)
2125 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2126 else
2127 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2129 else
2131 if (mask_size > 1)
2132 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2133 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2134 else
2135 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2136 src0_param.param_str, src0_param.param_str);
2139 else
2141 shader_addline(buffer, "%s);\n", src0_param.param_str);
2145 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2146 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2148 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2149 struct glsl_src_param src0_param;
2150 struct glsl_src_param src1_param;
2151 DWORD dst_write_mask, src_write_mask;
2152 unsigned int dst_size = 0;
2154 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2155 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2157 /* dp3 works on vec3, dp4 on vec4 */
2158 if (ins->handler_idx == WINED3DSIH_DP4)
2160 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2161 } else {
2162 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2165 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2166 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2168 if (dst_size > 1) {
2169 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2170 } else {
2171 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2175 /* Note that this instruction has some restrictions. The destination write mask
2176 * can't contain the w component, and the source swizzles have to be .xyzw */
2177 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2179 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2180 struct glsl_src_param src0_param;
2181 struct glsl_src_param src1_param;
2182 char dst_mask[6];
2184 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2185 shader_glsl_append_dst(ins->ctx->buffer, ins);
2186 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2187 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2188 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2191 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2192 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2193 * GLSL uses the value as-is. */
2194 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2196 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2197 struct glsl_src_param src0_param;
2198 struct glsl_src_param src1_param;
2199 DWORD dst_write_mask;
2200 unsigned int dst_size;
2202 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2203 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2205 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2206 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2208 if (dst_size > 1)
2210 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2211 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
2213 else
2215 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
2216 src1_param.param_str, src0_param.param_str, src1_param.param_str);
2220 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
2221 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
2222 * GLSL uses the value as-is. */
2223 static void shader_glsl_log(const struct wined3d_shader_instruction *ins)
2225 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2226 struct glsl_src_param src0_param;
2227 DWORD dst_write_mask;
2228 unsigned int dst_size;
2230 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2231 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2233 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2235 if (dst_size > 1)
2237 shader_addline(buffer, "vec%u(log2(abs(%s))));\n",
2238 dst_size, src0_param.param_str);
2240 else
2242 shader_addline(buffer, "log2(abs(%s)));\n",
2243 src0_param.param_str);
2247 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2248 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
2250 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2251 struct glsl_src_param src_param;
2252 const char *instruction;
2253 DWORD write_mask;
2254 unsigned i;
2256 /* Determine the GLSL function to use based on the opcode */
2257 /* TODO: Possibly make this a table for faster lookups */
2258 switch (ins->handler_idx)
2260 case WINED3DSIH_MIN: instruction = "min"; break;
2261 case WINED3DSIH_MAX: instruction = "max"; break;
2262 case WINED3DSIH_ABS: instruction = "abs"; break;
2263 case WINED3DSIH_FRC: instruction = "fract"; break;
2264 case WINED3DSIH_EXP: instruction = "exp2"; break;
2265 case WINED3DSIH_DSX: instruction = "dFdx"; break;
2266 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
2267 default: instruction = "";
2268 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2269 break;
2272 write_mask = shader_glsl_append_dst(buffer, ins);
2274 shader_addline(buffer, "%s(", instruction);
2276 if (ins->src_count)
2278 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2279 shader_addline(buffer, "%s", src_param.param_str);
2280 for (i = 1; i < ins->src_count; ++i)
2282 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
2283 shader_addline(buffer, ", %s", src_param.param_str);
2287 shader_addline(buffer, "));\n");
2290 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
2292 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2293 struct glsl_src_param src_param;
2294 unsigned int mask_size;
2295 DWORD write_mask;
2296 char dst_mask[6];
2298 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
2299 mask_size = shader_glsl_get_write_mask_size(write_mask);
2300 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2302 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
2303 src_param.param_str, src_param.param_str);
2304 shader_glsl_append_dst(buffer, ins);
2306 if (mask_size > 1)
2308 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
2309 mask_size, src_param.param_str);
2311 else
2313 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
2314 src_param.param_str);
2318 /** Process the WINED3DSIO_EXPP instruction in GLSL:
2319 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2320 * dst.x = 2^(floor(src))
2321 * dst.y = src - floor(src)
2322 * dst.z = 2^src (partial precision is allowed, but optional)
2323 * dst.w = 1.0;
2324 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2325 * dst = 2^src; (partial precision is allowed, but optional)
2327 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
2329 struct glsl_src_param src_param;
2331 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
2333 if (ins->ctx->reg_maps->shader_version.major < 2)
2335 char dst_mask[6];
2337 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
2338 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
2339 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
2340 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
2342 shader_glsl_append_dst(ins->ctx->buffer, ins);
2343 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2344 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
2345 } else {
2346 DWORD write_mask;
2347 unsigned int mask_size;
2349 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2350 mask_size = shader_glsl_get_write_mask_size(write_mask);
2352 if (mask_size > 1) {
2353 shader_addline(ins->ctx->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
2354 } else {
2355 shader_addline(ins->ctx->buffer, "exp2(%s));\n", src_param.param_str);
2360 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
2361 static void shader_glsl_rcp(const struct wined3d_shader_instruction *ins)
2363 struct glsl_src_param src_param;
2364 DWORD write_mask;
2365 unsigned int mask_size;
2367 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2368 mask_size = shader_glsl_get_write_mask_size(write_mask);
2369 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2371 if (mask_size > 1)
2373 shader_addline(ins->ctx->buffer, "vec%u(1.0 / %s));\n",
2374 mask_size, src_param.param_str);
2376 else
2378 shader_addline(ins->ctx->buffer, "1.0 / %s);\n",
2379 src_param.param_str);
2383 static void shader_glsl_rsq(const struct wined3d_shader_instruction *ins)
2385 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2386 struct glsl_src_param src_param;
2387 DWORD write_mask;
2388 unsigned int mask_size;
2390 write_mask = shader_glsl_append_dst(buffer, ins);
2391 mask_size = shader_glsl_get_write_mask_size(write_mask);
2393 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2395 if (mask_size > 1)
2397 shader_addline(buffer, "vec%u(inversesqrt(abs(%s))));\n",
2398 mask_size, src_param.param_str);
2400 else
2402 shader_addline(buffer, "inversesqrt(abs(%s)));\n",
2403 src_param.param_str);
2407 /** Process signed comparison opcodes in GLSL. */
2408 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
2410 struct glsl_src_param src0_param;
2411 struct glsl_src_param src1_param;
2412 DWORD write_mask;
2413 unsigned int mask_size;
2415 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2416 mask_size = shader_glsl_get_write_mask_size(write_mask);
2417 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2418 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2420 if (mask_size > 1) {
2421 const char *compare;
2423 switch(ins->handler_idx)
2425 case WINED3DSIH_SLT: compare = "lessThan"; break;
2426 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
2427 default: compare = "";
2428 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2431 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
2432 src0_param.param_str, src1_param.param_str);
2433 } else {
2434 switch(ins->handler_idx)
2436 case WINED3DSIH_SLT:
2437 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2438 * to return 0.0 but step returns 1.0 because step is not < x
2439 * An alternative is a bvec compare padded with an unused second component.
2440 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2441 * issue. Playing with not() is not possible either because not() does not accept
2442 * a scalar.
2444 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
2445 src0_param.param_str, src1_param.param_str);
2446 break;
2447 case WINED3DSIH_SGE:
2448 /* Here we can use the step() function and safe a conditional */
2449 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
2450 break;
2451 default:
2452 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2458 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
2459 static void shader_glsl_cmp(const struct wined3d_shader_instruction *ins)
2461 struct glsl_src_param src0_param;
2462 struct glsl_src_param src1_param;
2463 struct glsl_src_param src2_param;
2464 DWORD write_mask, cmp_channel = 0;
2465 unsigned int i, j;
2466 char mask_char[6];
2467 BOOL temp_destination = FALSE;
2469 if (shader_is_scalar(&ins->src[0].reg))
2471 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2473 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2474 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2475 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2477 shader_addline(ins->ctx->buffer, "%s >= 0.0 ? %s : %s);\n",
2478 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2479 } else {
2480 DWORD dst_mask = ins->dst[0].write_mask;
2481 struct wined3d_shader_dst_param dst = ins->dst[0];
2483 /* Cycle through all source0 channels */
2484 for (i=0; i<4; i++) {
2485 write_mask = 0;
2486 /* Find the destination channels which use the current source0 channel */
2487 for (j=0; j<4; j++) {
2488 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2490 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2491 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2494 dst.write_mask = dst_mask & write_mask;
2496 /* Splitting the cmp instruction up in multiple lines imposes a problem:
2497 * The first lines may overwrite source parameters of the following lines.
2498 * Deal with that by using a temporary destination register if needed
2500 if ((ins->src[0].reg.idx == ins->dst[0].reg.idx
2501 && ins->src[0].reg.type == ins->dst[0].reg.type)
2502 || (ins->src[1].reg.idx == ins->dst[0].reg.idx
2503 && ins->src[1].reg.type == ins->dst[0].reg.type)
2504 || (ins->src[2].reg.idx == ins->dst[0].reg.idx
2505 && ins->src[2].reg.type == ins->dst[0].reg.type))
2507 write_mask = shader_glsl_get_write_mask(&dst, mask_char);
2508 if (!write_mask) continue;
2509 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
2510 temp_destination = TRUE;
2511 } else {
2512 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst);
2513 if (!write_mask) continue;
2516 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2517 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2518 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2520 shader_addline(ins->ctx->buffer, "%s >= 0.0 ? %s : %s);\n",
2521 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2524 if(temp_destination) {
2525 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
2526 shader_glsl_append_dst(ins->ctx->buffer, ins);
2527 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
2533 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2534 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2535 * the compare is done per component of src0. */
2536 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
2538 struct wined3d_shader_dst_param dst;
2539 struct glsl_src_param src0_param;
2540 struct glsl_src_param src1_param;
2541 struct glsl_src_param src2_param;
2542 DWORD write_mask, cmp_channel = 0;
2543 unsigned int i, j;
2544 DWORD dst_mask;
2545 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2546 ins->ctx->reg_maps->shader_version.minor);
2548 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
2550 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2551 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2552 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2553 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2555 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
2556 if (ins->coissue)
2558 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
2559 } else {
2560 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2561 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2563 return;
2565 /* Cycle through all source0 channels */
2566 dst_mask = ins->dst[0].write_mask;
2567 dst = ins->dst[0];
2568 for (i=0; i<4; i++) {
2569 write_mask = 0;
2570 /* Find the destination channels which use the current source0 channel */
2571 for (j=0; j<4; j++) {
2572 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2574 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2575 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2579 dst.write_mask = dst_mask & write_mask;
2580 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst);
2581 if (!write_mask) continue;
2583 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2584 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2585 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2587 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2588 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2592 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2593 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
2595 struct glsl_src_param src0_param;
2596 struct glsl_src_param src1_param;
2597 struct glsl_src_param src2_param;
2598 DWORD write_mask;
2600 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2601 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2602 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2603 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2604 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
2605 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2608 /* Handles transforming all WINED3DSIO_M?x? opcodes for
2609 Vertex shaders to GLSL codes */
2610 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
2612 int i;
2613 int nComponents = 0;
2614 struct wined3d_shader_dst_param tmp_dst = {{0}};
2615 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
2616 struct wined3d_shader_instruction tmp_ins;
2618 memset(&tmp_ins, 0, sizeof(tmp_ins));
2620 /* Set constants for the temporary argument */
2621 tmp_ins.ctx = ins->ctx;
2622 tmp_ins.dst_count = 1;
2623 tmp_ins.dst = &tmp_dst;
2624 tmp_ins.src_count = 2;
2625 tmp_ins.src = tmp_src;
2627 switch(ins->handler_idx)
2629 case WINED3DSIH_M4x4:
2630 nComponents = 4;
2631 tmp_ins.handler_idx = WINED3DSIH_DP4;
2632 break;
2633 case WINED3DSIH_M4x3:
2634 nComponents = 3;
2635 tmp_ins.handler_idx = WINED3DSIH_DP4;
2636 break;
2637 case WINED3DSIH_M3x4:
2638 nComponents = 4;
2639 tmp_ins.handler_idx = WINED3DSIH_DP3;
2640 break;
2641 case WINED3DSIH_M3x3:
2642 nComponents = 3;
2643 tmp_ins.handler_idx = WINED3DSIH_DP3;
2644 break;
2645 case WINED3DSIH_M3x2:
2646 nComponents = 2;
2647 tmp_ins.handler_idx = WINED3DSIH_DP3;
2648 break;
2649 default:
2650 break;
2653 tmp_dst = ins->dst[0];
2654 tmp_src[0] = ins->src[0];
2655 tmp_src[1] = ins->src[1];
2656 for (i = 0; i < nComponents; ++i)
2658 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
2659 shader_glsl_dot(&tmp_ins);
2660 ++tmp_src[1].reg.idx;
2665 The LRP instruction performs a component-wise linear interpolation
2666 between the second and third operands using the first operand as the
2667 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
2668 This is equivalent to mix(src2, src1, src0);
2670 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
2672 struct glsl_src_param src0_param;
2673 struct glsl_src_param src1_param;
2674 struct glsl_src_param src2_param;
2675 DWORD write_mask;
2677 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2679 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2680 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2681 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2683 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
2684 src2_param.param_str, src1_param.param_str, src0_param.param_str);
2687 /** Process the WINED3DSIO_LIT instruction in GLSL:
2688 * dst.x = dst.w = 1.0
2689 * dst.y = (src0.x > 0) ? src0.x
2690 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
2691 * where src.w is clamped at +- 128
2693 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
2695 struct glsl_src_param src0_param;
2696 struct glsl_src_param src1_param;
2697 struct glsl_src_param src3_param;
2698 char dst_mask[6];
2700 shader_glsl_append_dst(ins->ctx->buffer, ins);
2701 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2703 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2704 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
2705 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
2707 /* The sdk specifies the instruction like this
2708 * dst.x = 1.0;
2709 * if(src.x > 0.0) dst.y = src.x
2710 * else dst.y = 0.0.
2711 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
2712 * else dst.z = 0.0;
2713 * dst.w = 1.0;
2714 * (where power = src.w clamped between -128 and 128)
2716 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
2717 * dst.x = 1.0 ... No further explanation needed
2718 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
2719 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
2720 * dst.w = 1.0. ... Nothing fancy.
2722 * So we still have one conditional in there. So do this:
2723 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
2725 * 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),
2726 * 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.
2727 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
2729 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
2730 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
2731 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
2733 shader_addline(ins->ctx->buffer,
2734 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
2735 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
2736 src0_param.param_str, src3_param.param_str, src1_param.param_str,
2737 src0_param.param_str, src3_param.param_str, dst_mask);
2740 /** Process the WINED3DSIO_DST instruction in GLSL:
2741 * dst.x = 1.0
2742 * dst.y = src0.x * src0.y
2743 * dst.z = src0.z
2744 * dst.w = src1.w
2746 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
2748 struct glsl_src_param src0y_param;
2749 struct glsl_src_param src0z_param;
2750 struct glsl_src_param src1y_param;
2751 struct glsl_src_param src1w_param;
2752 char dst_mask[6];
2754 shader_glsl_append_dst(ins->ctx->buffer, ins);
2755 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2757 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
2758 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
2759 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
2760 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
2762 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
2763 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
2766 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
2767 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
2768 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
2770 * dst.x = cos(src0.?)
2771 * dst.y = sin(src0.?)
2772 * dst.z = dst.z
2773 * dst.w = dst.w
2775 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
2777 struct glsl_src_param src0_param;
2778 DWORD write_mask;
2780 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2781 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2783 switch (write_mask) {
2784 case WINED3DSP_WRITEMASK_0:
2785 shader_addline(ins->ctx->buffer, "cos(%s));\n", src0_param.param_str);
2786 break;
2788 case WINED3DSP_WRITEMASK_1:
2789 shader_addline(ins->ctx->buffer, "sin(%s));\n", src0_param.param_str);
2790 break;
2792 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
2793 shader_addline(ins->ctx->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
2794 break;
2796 default:
2797 ERR("Write mask should be .x, .y or .xy\n");
2798 break;
2802 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
2803 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
2804 * generate invalid code
2806 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
2808 struct glsl_src_param src0_param;
2809 DWORD write_mask;
2811 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2812 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2814 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
2817 /** Process the WINED3DSIO_LOOP instruction in GLSL:
2818 * Start a for() loop where src1.y is the initial value of aL,
2819 * increment aL by src1.z for a total of src1.x iterations.
2820 * Need to use a temporary variable for this operation.
2822 /* FIXME: I don't think nested loops will work correctly this way. */
2823 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
2825 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
2826 const struct wined3d_shader *shader = ins->ctx->shader;
2827 const struct wined3d_shader_lconst *constant;
2828 struct glsl_src_param src1_param;
2829 const DWORD *control_values = NULL;
2831 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
2833 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
2834 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
2835 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
2836 * addressing.
2838 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
2840 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
2842 if (constant->idx == ins->src[1].reg.idx)
2844 control_values = constant->value;
2845 break;
2850 if (control_values)
2852 struct wined3d_shader_loop_control loop_control;
2853 loop_control.count = control_values[0];
2854 loop_control.start = control_values[1];
2855 loop_control.step = (int)control_values[2];
2857 if (loop_control.step > 0)
2859 shader_addline(ins->ctx->buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d) {\n",
2860 loop_state->current_depth, loop_control.start,
2861 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
2862 loop_state->current_depth, loop_control.step);
2864 else if (loop_control.step < 0)
2866 shader_addline(ins->ctx->buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d) {\n",
2867 loop_state->current_depth, loop_control.start,
2868 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
2869 loop_state->current_depth, loop_control.step);
2871 else
2873 shader_addline(ins->ctx->buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++) {\n",
2874 loop_state->current_depth, loop_control.start, loop_state->current_depth,
2875 loop_state->current_depth, loop_control.count,
2876 loop_state->current_depth);
2878 } else {
2879 shader_addline(ins->ctx->buffer,
2880 "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
2881 loop_state->current_depth, loop_state->current_reg,
2882 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
2883 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
2886 ++loop_state->current_depth;
2887 ++loop_state->current_reg;
2890 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
2892 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
2894 shader_addline(ins->ctx->buffer, "}\n");
2896 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
2898 --loop_state->current_depth;
2899 --loop_state->current_reg;
2902 if (ins->handler_idx == WINED3DSIH_ENDREP)
2904 --loop_state->current_depth;
2908 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
2910 const struct wined3d_shader *shader = ins->ctx->shader;
2911 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
2912 const struct wined3d_shader_lconst *constant;
2913 struct glsl_src_param src0_param;
2914 const DWORD *control_values = NULL;
2916 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
2917 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
2919 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
2921 if (constant->idx == ins->src[0].reg.idx)
2923 control_values = constant->value;
2924 break;
2929 if (control_values)
2931 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
2932 loop_state->current_depth, loop_state->current_depth,
2933 control_values[0], loop_state->current_depth);
2935 else
2937 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2938 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2939 loop_state->current_depth, loop_state->current_depth,
2940 src0_param.param_str, loop_state->current_depth);
2943 ++loop_state->current_depth;
2946 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
2948 struct glsl_src_param src0_param;
2950 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2951 shader_addline(ins->ctx->buffer, "if (%s) {\n", src0_param.param_str);
2954 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
2956 struct glsl_src_param src0_param;
2957 struct glsl_src_param src1_param;
2959 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2960 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2962 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
2963 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
2966 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
2968 shader_addline(ins->ctx->buffer, "} else {\n");
2971 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
2973 shader_addline(ins->ctx->buffer, "break;\n");
2976 /* FIXME: According to MSDN the compare is done per component. */
2977 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
2979 struct glsl_src_param src0_param;
2980 struct glsl_src_param src1_param;
2982 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2983 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2985 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
2986 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
2989 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
2991 shader_addline(ins->ctx->buffer, "}\n");
2992 shader_addline(ins->ctx->buffer, "void subroutine%u () {\n", ins->src[0].reg.idx);
2995 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
2997 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx);
3000 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3002 struct glsl_src_param src1_param;
3004 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3005 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n", src1_param.param_str, ins->src[0].reg.idx);
3008 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3010 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3011 * function only suppresses the unhandled instruction warning
3015 /*********************************************
3016 * Pixel Shader Specific Code begins here
3017 ********************************************/
3018 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3020 const struct wined3d_shader *shader = ins->ctx->shader;
3021 struct wined3d_device *device = shader->device;
3022 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3023 ins->ctx->reg_maps->shader_version.minor);
3024 struct glsl_sample_function sample_function;
3025 const struct wined3d_texture *texture;
3026 DWORD sample_flags = 0;
3027 DWORD sampler_idx;
3028 DWORD mask = 0, swizzle;
3030 /* 1.0-1.4: Use destination register as sampler source.
3031 * 2.0+: Use provided sampler source. */
3032 if (shader_version < WINED3D_SHADER_VERSION(2,0)) sampler_idx = ins->dst[0].reg.idx;
3033 else sampler_idx = ins->src[1].reg.idx;
3034 texture = device->stateBlock->state.textures[sampler_idx];
3036 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3038 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3039 DWORD flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3040 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3041 enum wined3d_sampler_texture_type sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3043 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3044 if (flags & WINED3D_PSARGS_PROJECTED && sampler_type != WINED3DSTT_CUBE)
3046 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3047 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3049 case WINED3D_TTFF_COUNT1:
3050 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3051 break;
3052 case WINED3D_TTFF_COUNT2:
3053 mask = WINED3DSP_WRITEMASK_1;
3054 break;
3055 case WINED3D_TTFF_COUNT3:
3056 mask = WINED3DSP_WRITEMASK_2;
3057 break;
3058 case WINED3D_TTFF_COUNT4:
3059 case WINED3D_TTFF_DISABLE:
3060 mask = WINED3DSP_WRITEMASK_3;
3061 break;
3065 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3067 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3069 if (src_mod == WINED3DSPSM_DZ) {
3070 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3071 mask = WINED3DSP_WRITEMASK_2;
3072 } else if (src_mod == WINED3DSPSM_DW) {
3073 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3074 mask = WINED3DSP_WRITEMASK_3;
3076 } else {
3077 if (ins->flags & WINED3DSI_TEXLD_PROJECT)
3079 /* ps 2.0 texldp instruction always divides by the fourth component. */
3080 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3081 mask = WINED3DSP_WRITEMASK_3;
3085 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
3086 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3088 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3089 mask |= sample_function.coord_mask;
3091 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3092 else swizzle = ins->src[1].swizzle;
3094 /* 1.0-1.3: Use destination register as coordinate source.
3095 1.4+: Use provided coordinate source register. */
3096 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3098 char coord_mask[6];
3099 shader_glsl_write_mask_to_str(mask, coord_mask);
3100 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3101 "T%u%s", sampler_idx, coord_mask);
3103 else
3105 struct glsl_src_param coord_param;
3106 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3107 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3109 struct glsl_src_param bias;
3110 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3111 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3112 "%s", coord_param.param_str);
3113 } else {
3114 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3115 "%s", coord_param.param_str);
3120 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3122 const struct wined3d_shader *shader = ins->ctx->shader;
3123 struct wined3d_device *device = shader->device;
3124 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3125 struct glsl_src_param coord_param, dx_param, dy_param;
3126 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3127 struct glsl_sample_function sample_function;
3128 DWORD sampler_idx;
3129 DWORD swizzle = ins->src[1].swizzle;
3130 const struct wined3d_texture *texture;
3132 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3134 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3135 shader_glsl_tex(ins);
3136 return;
3139 sampler_idx = ins->src[1].reg.idx;
3140 texture = device->stateBlock->state.textures[sampler_idx];
3141 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
3142 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3144 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3145 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3146 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3147 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3149 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3150 "%s", coord_param.param_str);
3153 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3155 const struct wined3d_shader *shader = ins->ctx->shader;
3156 struct wined3d_device *device = shader->device;
3157 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3158 struct glsl_src_param coord_param, lod_param;
3159 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3160 struct glsl_sample_function sample_function;
3161 DWORD sampler_idx;
3162 DWORD swizzle = ins->src[1].swizzle;
3163 const struct wined3d_texture *texture;
3165 sampler_idx = ins->src[1].reg.idx;
3166 texture = device->stateBlock->state.textures[sampler_idx];
3167 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
3168 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3170 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3171 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3173 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3175 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3176 && shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type))
3178 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
3179 * However, the NVIDIA drivers allow them in fragment shaders as well,
3180 * even without the appropriate extension. */
3181 WARN("Using %s in fragment shader.\n", sample_function.name);
3183 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3184 "%s", coord_param.param_str);
3187 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3189 /* FIXME: Make this work for more than just 2D textures */
3190 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3191 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3193 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3195 char dst_mask[6];
3197 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3198 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3199 ins->dst[0].reg.idx, dst_mask);
3201 else
3203 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3204 DWORD reg = ins->src[0].reg.idx;
3205 char dst_swizzle[6];
3207 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3209 if (src_mod == WINED3DSPSM_DZ)
3211 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3212 struct glsl_src_param div_param;
3214 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3216 if (mask_size > 1) {
3217 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3218 } else {
3219 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3222 else if (src_mod == WINED3DSPSM_DW)
3224 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3225 struct glsl_src_param div_param;
3227 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3229 if (mask_size > 1) {
3230 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3231 } else {
3232 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3234 } else {
3235 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3240 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3241 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3242 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3243 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3245 DWORD sampler_idx = ins->dst[0].reg.idx;
3246 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3247 struct glsl_sample_function sample_function;
3248 struct glsl_src_param src0_param;
3249 UINT mask_size;
3251 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3253 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3254 * scalar, and projected sampling would require 4.
3256 * It is a dependent read - not valid with conditional NP2 textures
3258 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3259 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3261 switch(mask_size)
3263 case 1:
3264 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3265 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3266 break;
3268 case 2:
3269 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3270 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3271 break;
3273 case 3:
3274 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3275 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3276 break;
3278 default:
3279 FIXME("Unexpected mask size %u\n", mask_size);
3280 break;
3284 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3285 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3286 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3288 DWORD dstreg = ins->dst[0].reg.idx;
3289 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3290 struct glsl_src_param src0_param;
3291 DWORD dst_mask;
3292 unsigned int mask_size;
3294 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3295 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3296 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3298 if (mask_size > 1) {
3299 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3300 } else {
3301 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3305 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3306 * Calculate the depth as dst.x / dst.y */
3307 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3309 struct glsl_dst_param dst_param;
3311 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3313 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3314 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3315 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3316 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3317 * >= 1.0 or < 0.0
3319 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3320 dst_param.reg_name, dst_param.reg_name);
3323 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3324 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3325 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3326 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3328 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3330 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3331 DWORD dstreg = ins->dst[0].reg.idx;
3332 struct glsl_src_param src0_param;
3334 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3336 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3337 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3340 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3341 * Calculate the 1st of a 2-row matrix multiplication. */
3342 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3344 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3345 DWORD reg = ins->dst[0].reg.idx;
3346 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3347 struct glsl_src_param src0_param;
3349 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3350 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3353 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3354 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3355 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3357 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3358 DWORD reg = ins->dst[0].reg.idx;
3359 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3360 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3361 struct glsl_src_param src0_param;
3363 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3364 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
3365 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
3368 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3370 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3371 DWORD reg = ins->dst[0].reg.idx;
3372 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3373 struct glsl_sample_function sample_function;
3374 struct glsl_src_param src0_param;
3376 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3377 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3379 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3381 /* Sample the texture using the calculated coordinates */
3382 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
3385 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3386 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3387 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3389 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3390 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3391 struct glsl_sample_function sample_function;
3392 struct glsl_src_param src0_param;
3393 DWORD reg = ins->dst[0].reg.idx;
3395 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3396 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3398 /* Dependent read, not valid with conditional NP2 */
3399 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3401 /* Sample the texture using the calculated coordinates */
3402 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3404 tex_mx->current_row = 0;
3407 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3408 * Perform the 3rd row of a 3x3 matrix multiply */
3409 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
3411 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3412 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3413 struct glsl_src_param src0_param;
3414 char dst_mask[6];
3415 DWORD reg = ins->dst[0].reg.idx;
3417 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3419 shader_glsl_append_dst(ins->ctx->buffer, ins);
3420 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3421 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
3423 tex_mx->current_row = 0;
3426 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3427 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3428 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
3430 struct glsl_src_param src0_param;
3431 struct glsl_src_param src1_param;
3432 DWORD reg = ins->dst[0].reg.idx;
3433 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3434 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3435 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3436 struct glsl_sample_function sample_function;
3437 char coord_mask[6];
3439 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3440 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3442 /* Perform the last matrix multiply operation */
3443 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3444 /* Reflection calculation */
3445 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
3447 /* Dependent read, not valid with conditional NP2 */
3448 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3449 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3451 /* Sample the texture */
3452 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3453 NULL, NULL, NULL, "tmp0%s", coord_mask);
3455 tex_mx->current_row = 0;
3458 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3459 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3460 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
3462 DWORD reg = ins->dst[0].reg.idx;
3463 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3464 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3465 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3466 struct glsl_sample_function sample_function;
3467 struct glsl_src_param src0_param;
3468 char coord_mask[6];
3470 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3472 /* Perform the last matrix multiply operation */
3473 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
3475 /* Construct the eye-ray vector from w coordinates */
3476 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3477 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
3478 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3480 /* Dependent read, not valid with conditional NP2 */
3481 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3482 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3484 /* Sample the texture using the calculated coordinates */
3485 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3486 NULL, NULL, NULL, "tmp0%s", coord_mask);
3488 tex_mx->current_row = 0;
3491 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3492 * Apply a fake bump map transform.
3493 * texbem is pshader <= 1.3 only, this saves a few version checks
3495 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
3497 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3498 struct glsl_sample_function sample_function;
3499 struct glsl_src_param coord_param;
3500 DWORD sampler_idx;
3501 DWORD mask;
3502 DWORD flags;
3503 char coord_mask[6];
3505 sampler_idx = ins->dst[0].reg.idx;
3506 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3507 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3509 /* Dependent read, not valid with conditional NP2 */
3510 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3511 mask = sample_function.coord_mask;
3513 shader_glsl_write_mask_to_str(mask, coord_mask);
3515 /* With projected textures, texbem only divides the static texture coord,
3516 * not the displacement, so we can't let GL handle this. */
3517 if (flags & WINED3D_PSARGS_PROJECTED)
3519 DWORD div_mask=0;
3520 char coord_div_mask[3];
3521 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3523 case WINED3D_TTFF_COUNT1:
3524 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3525 break;
3526 case WINED3D_TTFF_COUNT2:
3527 div_mask = WINED3DSP_WRITEMASK_1;
3528 break;
3529 case WINED3D_TTFF_COUNT3:
3530 div_mask = WINED3DSP_WRITEMASK_2;
3531 break;
3532 case WINED3D_TTFF_COUNT4:
3533 case WINED3D_TTFF_DISABLE:
3534 div_mask = WINED3DSP_WRITEMASK_3;
3535 break;
3537 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
3538 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
3541 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
3543 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3544 "T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
3545 coord_param.param_str, coord_mask);
3547 if (ins->handler_idx == WINED3DSIH_TEXBEML)
3549 struct glsl_src_param luminance_param;
3550 struct glsl_dst_param dst_param;
3552 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
3553 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3555 shader_addline(ins->ctx->buffer, "%s%s *= (%s * luminancescale%d + luminanceoffset%d);\n",
3556 dst_param.reg_name, dst_param.mask_str,
3557 luminance_param.param_str, sampler_idx, sampler_idx);
3561 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
3563 struct glsl_src_param src0_param, src1_param;
3564 DWORD sampler_idx = ins->dst[0].reg.idx;
3566 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
3567 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
3569 shader_glsl_append_dst(ins->ctx->buffer, ins);
3570 shader_addline(ins->ctx->buffer, "%s + bumpenvmat%d * %s);\n",
3571 src0_param.param_str, sampler_idx, src1_param.param_str);
3574 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
3575 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
3576 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
3578 struct glsl_sample_function sample_function;
3579 struct glsl_src_param src0_param;
3580 DWORD sampler_idx = ins->dst[0].reg.idx;
3582 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
3584 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3585 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3586 "%s.wx", src0_param.reg_name);
3589 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
3590 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
3591 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
3593 struct glsl_sample_function sample_function;
3594 struct glsl_src_param src0_param;
3595 DWORD sampler_idx = ins->dst[0].reg.idx;
3597 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
3599 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3600 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3601 "%s.yz", src0_param.reg_name);
3604 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
3605 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
3606 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
3608 struct glsl_sample_function sample_function;
3609 struct glsl_src_param src0_param;
3610 DWORD sampler_idx = ins->dst[0].reg.idx;
3612 /* Dependent read, not valid with conditional NP2 */
3613 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3614 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
3616 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3617 "%s", src0_param.param_str);
3620 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
3621 * If any of the first 3 components are < 0, discard this pixel */
3622 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
3624 struct glsl_dst_param dst_param;
3626 /* The argument is a destination parameter, and no writemasks are allowed */
3627 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3628 if (ins->ctx->reg_maps->shader_version.major >= 2)
3630 /* 2.0 shaders compare all 4 components in texkill */
3631 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
3632 } else {
3633 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
3634 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
3635 * 4 components are defined, only the first 3 are used
3637 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
3641 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
3642 * dst = dot2(src0, src1) + src2 */
3643 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
3645 struct glsl_src_param src0_param;
3646 struct glsl_src_param src1_param;
3647 struct glsl_src_param src2_param;
3648 DWORD write_mask;
3649 unsigned int mask_size;
3651 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3652 mask_size = shader_glsl_get_write_mask_size(write_mask);
3654 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
3655 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
3656 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
3658 if (mask_size > 1) {
3659 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
3660 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
3661 } else {
3662 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
3663 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3667 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
3668 const struct wined3d_shader_signature_element *input_signature,
3669 const struct wined3d_shader_reg_maps *reg_maps,
3670 enum vertexprocessing_mode vertexprocessing)
3672 WORD map = reg_maps->input_registers;
3673 unsigned int i;
3675 for (i = 0; map; map >>= 1, ++i)
3677 const char *semantic_name;
3678 UINT semantic_idx;
3679 char reg_mask[6];
3681 /* Unused */
3682 if (!(map & 1)) continue;
3684 semantic_name = input_signature[i].semantic_name;
3685 semantic_idx = input_signature[i].semantic_idx;
3686 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
3688 if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
3690 if (semantic_idx < 8 && vertexprocessing == pretransformed)
3691 shader_addline(buffer, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
3692 shader->u.ps.input_reg_map[i], reg_mask, semantic_idx, reg_mask);
3693 else
3694 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3695 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
3697 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
3699 if (!semantic_idx)
3700 shader_addline(buffer, "IN[%u]%s = vec4(gl_Color)%s;\n",
3701 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
3702 else if (semantic_idx == 1)
3703 shader_addline(buffer, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
3704 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
3705 else
3706 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3707 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
3709 else
3711 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3712 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
3717 /*********************************************
3718 * Vertex Shader Specific Code begins here
3719 ********************************************/
3721 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
3723 struct glsl_program_key key;
3725 key.vshader = entry->vshader;
3726 key.pshader = entry->pshader;
3727 key.vs_args = entry->vs_args;
3728 key.ps_args = entry->ps_args;
3730 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
3732 ERR("Failed to insert program entry.\n");
3736 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
3737 const struct wined3d_shader *vshader, const struct wined3d_shader *pshader,
3738 const struct vs_compile_args *vs_args, const struct ps_compile_args *ps_args)
3740 struct wine_rb_entry *entry;
3741 struct glsl_program_key key;
3743 key.vshader = vshader;
3744 key.pshader = pshader;
3745 key.vs_args = *vs_args;
3746 key.ps_args = *ps_args;
3748 entry = wine_rb_get(&priv->program_lookup, &key);
3749 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
3752 /* GL locking is done by the caller */
3753 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
3754 struct glsl_shader_prog_link *entry)
3756 struct glsl_program_key key;
3758 key.vshader = entry->vshader;
3759 key.pshader = entry->pshader;
3760 key.vs_args = entry->vs_args;
3761 key.ps_args = entry->ps_args;
3762 wine_rb_remove(&priv->program_lookup, &key);
3764 GL_EXTCALL(glDeleteObjectARB(entry->programId));
3765 if (entry->vshader) list_remove(&entry->vshader_entry);
3766 if (entry->pshader) list_remove(&entry->pshader_entry);
3767 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
3768 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
3769 HeapFree(GetProcessHeap(), 0, entry);
3772 static void handle_ps3_input(struct wined3d_shader_buffer *buffer,
3773 const struct wined3d_gl_info *gl_info, const DWORD *map,
3774 const struct wined3d_shader_signature_element *input_signature,
3775 const struct wined3d_shader_reg_maps *reg_maps_in,
3776 const struct wined3d_shader_signature_element *output_signature,
3777 const struct wined3d_shader_reg_maps *reg_maps_out)
3779 unsigned int i, j;
3780 const char *semantic_name_in;
3781 UINT semantic_idx_in;
3782 DWORD *set;
3783 DWORD in_idx;
3784 unsigned int in_count = vec4_varyings(3, gl_info);
3785 char reg_mask[6];
3786 char destination[50];
3787 WORD input_map, output_map;
3789 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
3791 input_map = reg_maps_in->input_registers;
3792 for (i = 0; input_map; input_map >>= 1, ++i)
3794 if (!(input_map & 1)) continue;
3796 in_idx = map[i];
3797 /* Declared, but not read register */
3798 if (in_idx == ~0U) continue;
3799 if (in_idx >= (in_count + 2))
3801 FIXME("More input varyings declared than supported, expect issues.\n");
3802 continue;
3805 if (in_idx == in_count) {
3806 sprintf(destination, "gl_FrontColor");
3807 } else if (in_idx == in_count + 1) {
3808 sprintf(destination, "gl_FrontSecondaryColor");
3809 } else {
3810 sprintf(destination, "IN[%u]", in_idx);
3813 semantic_name_in = input_signature[i].semantic_name;
3814 semantic_idx_in = input_signature[i].semantic_idx;
3815 set[in_idx] = ~0U;
3817 output_map = reg_maps_out->output_registers;
3818 for (j = 0; output_map; output_map >>= 1, ++j)
3820 DWORD mask;
3822 if (!(output_map & 1)
3823 || semantic_idx_in != output_signature[j].semantic_idx
3824 || strcmp(semantic_name_in, output_signature[j].semantic_name)
3825 || !(mask = input_signature[i].mask & output_signature[j].mask))
3826 continue;
3828 set[in_idx] = mask;
3829 shader_glsl_write_mask_to_str(mask, reg_mask);
3831 shader_addline(buffer, "%s%s = OUT[%u]%s;\n",
3832 destination, reg_mask, j, reg_mask);
3836 for (i = 0; i < in_count + 2; ++i)
3838 unsigned int size;
3840 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
3841 continue;
3843 if (set[i] == ~0U) set[i] = 0;
3845 size = 0;
3846 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
3847 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
3848 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
3849 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
3850 reg_mask[size] = '\0';
3852 if (i == in_count) sprintf(destination, "gl_FrontColor");
3853 else if (i == in_count + 1) sprintf(destination, "gl_FrontSecondaryColor");
3854 else sprintf(destination, "IN[%u]", i);
3856 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
3857 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
3860 HeapFree(GetProcessHeap(), 0, set);
3863 /* GL locking is done by the caller */
3864 static GLhandleARB generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
3865 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
3866 const struct wined3d_gl_info *gl_info)
3868 GLhandleARB ret = 0;
3869 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
3870 unsigned int i;
3871 const char *semantic_name;
3872 UINT semantic_idx;
3873 char reg_mask[6];
3874 const struct wined3d_shader_signature_element *output_signature = vs->output_signature;
3875 WORD map = vs->reg_maps.output_registers;
3877 shader_buffer_clear(buffer);
3879 shader_addline(buffer, "#version 120\n");
3881 if (ps_major < 3)
3883 shader_addline(buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3885 for (i = 0; map; map >>= 1, ++i)
3887 DWORD write_mask;
3889 if (!(map & 1)) continue;
3891 semantic_name = output_signature[i].semantic_name;
3892 semantic_idx = output_signature[i].semantic_idx;
3893 write_mask = output_signature[i].mask;
3894 shader_glsl_write_mask_to_str(write_mask, reg_mask);
3896 if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
3898 if (!semantic_idx)
3899 shader_addline(buffer, "gl_FrontColor%s = OUT[%u]%s;\n",
3900 reg_mask, i, reg_mask);
3901 else if (semantic_idx == 1)
3902 shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n",
3903 reg_mask, i, reg_mask);
3905 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_POSITION))
3907 shader_addline(buffer, "gl_Position%s = OUT[%u]%s;\n",
3908 reg_mask, i, reg_mask);
3910 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
3912 if (semantic_idx < 8)
3914 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
3915 write_mask |= WINED3DSP_WRITEMASK_3;
3917 shader_addline(buffer, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3918 semantic_idx, reg_mask, i, reg_mask);
3919 if (!(write_mask & WINED3DSP_WRITEMASK_3))
3920 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
3923 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_PSIZE))
3925 shader_addline(buffer, "gl_PointSize = OUT[%u].%c;\n", i, reg_mask[1]);
3927 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_FOG))
3929 shader_addline(buffer, "gl_FogFragCoord = OUT[%u].%c;\n", i, reg_mask[1]);
3932 shader_addline(buffer, "}\n");
3935 else
3937 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits.packed_input);
3938 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3939 shader_addline(buffer, "varying vec4 IN[%u];\n", in_count);
3940 shader_addline(buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3942 /* First, sort out position and point size. Those are not passed to the pixel shader */
3943 for (i = 0; map; map >>= 1, ++i)
3945 if (!(map & 1)) continue;
3947 semantic_name = output_signature[i].semantic_name;
3948 shader_glsl_write_mask_to_str(output_signature[i].mask, reg_mask);
3950 if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_POSITION))
3952 shader_addline(buffer, "gl_Position%s = OUT[%u]%s;\n",
3953 reg_mask, i, reg_mask);
3955 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_PSIZE))
3957 shader_addline(buffer, "gl_PointSize = OUT[%u].%c;\n", i, reg_mask[1]);
3961 /* Then, fix the pixel shader input */
3962 handle_ps3_input(buffer, gl_info, ps->u.ps.input_reg_map, ps->input_signature,
3963 &ps->reg_maps, output_signature, &vs->reg_maps);
3965 shader_addline(buffer, "}\n");
3968 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3969 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3970 shader_glsl_compile(gl_info, ret, buffer->buffer);
3972 return ret;
3975 /* GL locking is done by the caller */
3976 static void hardcode_local_constants(const struct wined3d_shader *shader,
3977 const struct wined3d_gl_info *gl_info, GLhandleARB programId, char prefix)
3979 const struct wined3d_shader_lconst *lconst;
3980 GLint tmp_loc;
3981 const float *value;
3982 char glsl_name[8];
3984 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
3986 value = (const float *)lconst->value;
3987 snprintf(glsl_name, sizeof(glsl_name), "%cLC%u", prefix, lconst->idx);
3988 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3989 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, value));
3991 checkGLcall("Hardcoding local constants");
3994 /* GL locking is done by the caller */
3995 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
3996 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
3997 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
3999 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4000 const struct wined3d_gl_info *gl_info = context->gl_info;
4001 const DWORD *function = shader->function;
4002 struct shader_glsl_ctx_priv priv_ctx;
4004 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4005 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4007 memset(&priv_ctx, 0, sizeof(priv_ctx));
4008 priv_ctx.cur_ps_args = args;
4009 priv_ctx.cur_np2fixup_info = np2fixup_info;
4011 shader_addline(buffer, "#version 120\n");
4013 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4015 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
4017 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4019 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
4020 * drivers write a warning if we don't do so
4022 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4024 if (gl_info->supported[EXT_GPU_SHADER4])
4026 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4029 /* Base Declarations */
4030 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4032 /* Pack 3.0 inputs */
4033 if (reg_maps->shader_version.major >= 3 && args->vp_mode != vertexshader)
4034 shader_glsl_input_pack(shader, buffer, shader->input_signature, reg_maps, args->vp_mode);
4036 /* Base Shader Body */
4037 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4039 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4040 if (reg_maps->shader_version.major < 2)
4042 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4043 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4046 if (args->srgb_correction)
4048 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4049 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4050 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4051 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4052 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4053 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4055 /* Pixel shader < 3.0 do not replace the fog stage.
4056 * This implements linear fog computation and blending.
4057 * TODO: non linear fog
4058 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
4059 * -1/(e-s) and e/(e-s) respectively.
4061 if (reg_maps->shader_version.major < 3)
4063 switch(args->fog) {
4064 case FOG_OFF: break;
4065 case FOG_LINEAR:
4066 shader_addline(buffer, "float fogstart = -1.0 / (gl_Fog.end - gl_Fog.start);\n");
4067 shader_addline(buffer, "float fogend = gl_Fog.end * -fogstart;\n");
4068 shader_addline(buffer, "float Fog = clamp(gl_FogFragCoord * fogstart + fogend, 0.0, 1.0);\n");
4069 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4070 break;
4071 case FOG_EXP:
4072 /* Fog = e^(-gl_Fog.density * gl_FogFragCoord) */
4073 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4074 shader_addline(buffer, "Fog = clamp(Fog, 0.0, 1.0);\n");
4075 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4076 break;
4077 case FOG_EXP2:
4078 /* Fog = e^(-(gl_Fog.density * gl_FogFragCoord)^2) */
4079 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4080 shader_addline(buffer, "Fog = clamp(Fog, 0.0, 1.0);\n");
4081 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4082 break;
4086 shader_addline(buffer, "}\n");
4088 TRACE("Compiling shader object %u\n", shader_obj);
4089 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4091 /* Store the shader object */
4092 return shader_obj;
4095 /* GL locking is done by the caller */
4096 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4097 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4098 const struct vs_compile_args *args)
4100 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4101 const struct wined3d_gl_info *gl_info = context->gl_info;
4102 const DWORD *function = shader->function;
4103 struct shader_glsl_ctx_priv priv_ctx;
4105 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4106 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4108 shader_addline(buffer, "#version 120\n");
4110 if (gl_info->supported[EXT_GPU_SHADER4])
4111 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4113 memset(&priv_ctx, 0, sizeof(priv_ctx));
4114 priv_ctx.cur_vs_args = args;
4116 /* Base Declarations */
4117 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4119 /* Base Shader Body */
4120 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4122 /* Unpack outputs */
4123 shader_addline(buffer, "order_ps_input(OUT);\n");
4125 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4126 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4127 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4128 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4130 if (args->fog_src == VS_FOG_Z)
4131 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4132 else if (!reg_maps->fog)
4133 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4135 /* We always store the clipplanes without y inversion */
4136 if (args->clip_enabled)
4137 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4139 /* Write the final position.
4141 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4142 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4143 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4144 * contains 1.0 to allow a mad.
4146 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4147 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4149 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4151 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4152 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4153 * which is the same as z = z * 2 - w.
4155 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4157 shader_addline(buffer, "}\n");
4159 TRACE("Compiling shader object %u\n", shader_obj);
4160 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4162 return shader_obj;
4165 static GLhandleARB find_glsl_pshader(const struct wined3d_context *context,
4166 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4167 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
4169 struct wined3d_state *state = &shader->device->stateBlock->state;
4170 UINT i;
4171 DWORD new_size;
4172 struct glsl_ps_compiled_shader *new_array;
4173 struct glsl_pshader_private *shader_data;
4174 struct ps_np2fixup_info *np2fixup = NULL;
4175 GLhandleARB ret;
4177 if (!shader->backend_data)
4179 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4180 if (!shader->backend_data)
4182 ERR("Failed to allocate backend data.\n");
4183 return 0;
4186 shader_data = shader->backend_data;
4188 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4189 * so a linear search is more performant than a hashmap or a binary search
4190 * (cache coherency etc)
4192 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4194 if (!memcmp(&shader_data->gl_shaders[i].args, args, sizeof(*args)))
4196 if (args->np2_fixup) *np2fixup_info = &shader_data->gl_shaders[i].np2fixup;
4197 return shader_data->gl_shaders[i].prgId;
4201 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4202 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4203 if (shader_data->num_gl_shaders)
4205 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4206 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders,
4207 new_size * sizeof(*shader_data->gl_shaders));
4208 } else {
4209 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader_data->gl_shaders));
4210 new_size = 1;
4213 if(!new_array) {
4214 ERR("Out of memory\n");
4215 return 0;
4217 shader_data->gl_shaders = new_array;
4218 shader_data->shader_array_size = new_size;
4221 shader_data->gl_shaders[shader_data->num_gl_shaders].args = *args;
4223 memset(&shader_data->gl_shaders[shader_data->num_gl_shaders].np2fixup, 0, sizeof(struct ps_np2fixup_info));
4224 if (args->np2_fixup) np2fixup = &shader_data->gl_shaders[shader_data->num_gl_shaders].np2fixup;
4226 pixelshader_update_samplers(&shader->reg_maps, state->textures);
4228 shader_buffer_clear(buffer);
4229 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4230 shader_data->gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4231 *np2fixup_info = np2fixup;
4233 return ret;
4236 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4237 const DWORD use_map) {
4238 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4239 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4240 return stored->fog_src == new->fog_src;
4243 static GLhandleARB find_glsl_vshader(const struct wined3d_context *context,
4244 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4245 const struct vs_compile_args *args)
4247 UINT i;
4248 DWORD new_size;
4249 struct glsl_vs_compiled_shader *new_array;
4250 DWORD use_map = shader->device->strided_streams.use_map;
4251 struct glsl_vshader_private *shader_data;
4252 GLhandleARB ret;
4254 if (!shader->backend_data)
4256 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4257 if (!shader->backend_data)
4259 ERR("Failed to allocate backend data.\n");
4260 return 0;
4263 shader_data = shader->backend_data;
4265 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4266 * so a linear search is more performant than a hashmap or a binary search
4267 * (cache coherency etc)
4269 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4270 if(vs_args_equal(&shader_data->gl_shaders[i].args, args, use_map)) {
4271 return shader_data->gl_shaders[i].prgId;
4275 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4277 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4278 if (shader_data->num_gl_shaders)
4280 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4281 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders,
4282 new_size * sizeof(*shader_data->gl_shaders));
4283 } else {
4284 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader_data->gl_shaders));
4285 new_size = 1;
4288 if(!new_array) {
4289 ERR("Out of memory\n");
4290 return 0;
4292 shader_data->gl_shaders = new_array;
4293 shader_data->shader_array_size = new_size;
4296 shader_data->gl_shaders[shader_data->num_gl_shaders].args = *args;
4298 shader_buffer_clear(buffer);
4299 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4300 shader_data->gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4302 return ret;
4305 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
4306 * It sets the programId on the current StateBlock (because it should be called
4307 * inside of the DrawPrimitive() part of the render loop).
4309 * If a program for the given combination does not exist, create one, and store
4310 * the program in the hash table. If it creates a program, it will link the
4311 * given objects, too.
4314 /* GL locking is done by the caller */
4315 static void set_glsl_shader_program(const struct wined3d_context *context,
4316 struct wined3d_device *device, BOOL use_ps, BOOL use_vs)
4318 const struct wined3d_state *state = &device->stateBlock->state;
4319 struct wined3d_shader *vshader = use_vs ? state->vertex_shader : NULL;
4320 struct wined3d_shader *pshader = use_ps ? state->pixel_shader : NULL;
4321 const struct wined3d_gl_info *gl_info = context->gl_info;
4322 struct shader_glsl_priv *priv = device->shader_priv;
4323 struct glsl_shader_prog_link *entry = NULL;
4324 GLhandleARB programId = 0;
4325 GLhandleARB reorder_shader_id = 0;
4326 unsigned int i;
4327 char glsl_name[8];
4328 struct ps_compile_args ps_compile_args;
4329 struct vs_compile_args vs_compile_args;
4331 if (vshader) find_vs_compile_args(state, vshader, &vs_compile_args);
4332 if (pshader) find_ps_compile_args(state, pshader, &ps_compile_args);
4334 entry = get_glsl_program_entry(priv, vshader, pshader, &vs_compile_args, &ps_compile_args);
4335 if (entry)
4337 priv->glsl_program = entry;
4338 return;
4341 /* If we get to this point, then no matching program exists, so we create one */
4342 programId = GL_EXTCALL(glCreateProgramObjectARB());
4343 TRACE("Created new GLSL shader program %u\n", programId);
4345 /* Create the entry */
4346 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
4347 entry->programId = programId;
4348 entry->vshader = vshader;
4349 entry->pshader = pshader;
4350 entry->vs_args = vs_compile_args;
4351 entry->ps_args = ps_compile_args;
4352 entry->constant_version = 0;
4353 entry->np2Fixup_info = NULL;
4354 /* Add the hash table entry */
4355 add_glsl_program_entry(priv, entry);
4357 /* Set the current program */
4358 priv->glsl_program = entry;
4360 /* Attach GLSL vshader */
4361 if (vshader)
4363 GLhandleARB vshader_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
4364 WORD map = vshader->reg_maps.input_registers;
4365 char tmp_name[10];
4367 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
4368 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
4369 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
4370 checkGLcall("glAttachObjectARB");
4371 /* Flag the reorder function for deletion, then it will be freed automatically when the program
4372 * is destroyed
4374 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
4376 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
4377 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
4378 checkGLcall("glAttachObjectARB");
4380 /* Bind vertex attributes to a corresponding index number to match
4381 * the same index numbers as ARB_vertex_programs (makes loading
4382 * vertex attributes simpler). With this method, we can use the
4383 * exact same code to load the attributes later for both ARB and
4384 * GLSL shaders.
4386 * We have to do this here because we need to know the Program ID
4387 * in order to make the bindings work, and it has to be done prior
4388 * to linking the GLSL program. */
4389 for (i = 0; map; map >>= 1, ++i)
4391 if (!(map & 1)) continue;
4393 snprintf(tmp_name, sizeof(tmp_name), "attrib%u", i);
4394 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
4396 checkGLcall("glBindAttribLocationARB");
4398 list_add_head(&vshader->linked_programs, &entry->vshader_entry);
4401 /* Attach GLSL pshader */
4402 if (pshader)
4404 GLhandleARB pshader_id = find_glsl_pshader(context, &priv->shader_buffer,
4405 pshader, &ps_compile_args, &entry->np2Fixup_info);
4406 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
4407 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
4408 checkGLcall("glAttachObjectARB");
4410 list_add_head(&pshader->linked_programs, &entry->pshader_entry);
4413 /* Link the program */
4414 TRACE("Linking GLSL shader program %u\n", programId);
4415 GL_EXTCALL(glLinkProgramARB(programId));
4416 shader_glsl_validate_link(gl_info, programId);
4418 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0,
4419 sizeof(GLhandleARB) * gl_info->limits.glsl_vs_float_constants);
4420 for (i = 0; i < gl_info->limits.glsl_vs_float_constants; ++i)
4422 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
4423 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4425 for (i = 0; i < MAX_CONST_I; ++i)
4427 snprintf(glsl_name, sizeof(glsl_name), "VI[%i]", i);
4428 entry->vuniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4430 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0,
4431 sizeof(GLhandleARB) * gl_info->limits.glsl_ps_float_constants);
4432 for (i = 0; i < gl_info->limits.glsl_ps_float_constants; ++i)
4434 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
4435 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4437 for (i = 0; i < MAX_CONST_I; ++i)
4439 snprintf(glsl_name, sizeof(glsl_name), "PI[%i]", i);
4440 entry->puniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4443 if(pshader) {
4444 char name[32];
4446 for(i = 0; i < MAX_TEXTURES; i++) {
4447 sprintf(name, "bumpenvmat%u", i);
4448 entry->bumpenvmat_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4449 sprintf(name, "luminancescale%u", i);
4450 entry->luminancescale_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4451 sprintf(name, "luminanceoffset%u", i);
4452 entry->luminanceoffset_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4455 if (ps_compile_args.np2_fixup) {
4456 if (entry->np2Fixup_info) {
4457 entry->np2Fixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "PsamplerNP2Fixup"));
4458 } else {
4459 FIXME("NP2 texcoord fixup needed for this pixelshader, but no fixup uniform found.\n");
4464 entry->posFixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
4465 entry->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
4466 checkGLcall("Find glsl program uniform locations");
4468 if (pshader && pshader->reg_maps.shader_version.major >= 3
4469 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
4471 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
4472 entry->vertex_color_clamp = GL_FALSE;
4473 } else {
4474 entry->vertex_color_clamp = GL_FIXED_ONLY_ARB;
4477 /* Set the shader to allow uniform loading on it */
4478 GL_EXTCALL(glUseProgramObjectARB(programId));
4479 checkGLcall("glUseProgramObjectARB(programId)");
4481 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
4482 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
4483 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
4484 * vertex shader with fixed function pixel processing is used we make sure that the card
4485 * supports enough samplers to allow the max number of vertex samplers with all possible
4486 * fixed function fragment processing setups. So once the program is linked these samplers
4487 * won't change.
4489 if (vshader) shader_glsl_load_vsamplers(gl_info, device->texUnitMap, programId);
4490 if (pshader) shader_glsl_load_psamplers(gl_info, device->texUnitMap, programId);
4492 /* If the local constants do not have to be loaded with the environment constants,
4493 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
4494 * later
4496 if (pshader && !pshader->load_local_constsF)
4497 hardcode_local_constants(pshader, gl_info, programId, 'P');
4498 if (vshader && !vshader->load_local_constsF)
4499 hardcode_local_constants(vshader, gl_info, programId, 'V');
4502 /* GL locking is done by the caller */
4503 static GLhandleARB create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum tex_types tex_type, BOOL masked)
4505 GLhandleARB program_id;
4506 GLhandleARB vshader_id, pshader_id;
4507 const char *blt_pshader;
4509 static const char *blt_vshader =
4510 "#version 120\n"
4511 "void main(void)\n"
4512 "{\n"
4513 " gl_Position = gl_Vertex;\n"
4514 " gl_FrontColor = vec4(1.0);\n"
4515 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
4516 "}\n";
4518 static const char * const blt_pshaders_full[tex_type_count] =
4520 /* tex_1d */
4521 NULL,
4522 /* tex_2d */
4523 "#version 120\n"
4524 "uniform sampler2D sampler;\n"
4525 "void main(void)\n"
4526 "{\n"
4527 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
4528 "}\n",
4529 /* tex_3d */
4530 NULL,
4531 /* tex_cube */
4532 "#version 120\n"
4533 "uniform samplerCube sampler;\n"
4534 "void main(void)\n"
4535 "{\n"
4536 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
4537 "}\n",
4538 /* tex_rect */
4539 "#version 120\n"
4540 "#extension GL_ARB_texture_rectangle : enable\n"
4541 "uniform sampler2DRect sampler;\n"
4542 "void main(void)\n"
4543 "{\n"
4544 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
4545 "}\n",
4548 static const char * const blt_pshaders_masked[tex_type_count] =
4550 /* tex_1d */
4551 NULL,
4552 /* tex_2d */
4553 "#version 120\n"
4554 "uniform sampler2D sampler;\n"
4555 "uniform vec4 mask;\n"
4556 "void main(void)\n"
4557 "{\n"
4558 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
4559 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
4560 "}\n",
4561 /* tex_3d */
4562 NULL,
4563 /* tex_cube */
4564 "#version 120\n"
4565 "uniform samplerCube sampler;\n"
4566 "uniform vec4 mask;\n"
4567 "void main(void)\n"
4568 "{\n"
4569 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
4570 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
4571 "}\n",
4572 /* tex_rect */
4573 "#version 120\n"
4574 "#extension GL_ARB_texture_rectangle : enable\n"
4575 "uniform sampler2DRect sampler;\n"
4576 "uniform vec4 mask;\n"
4577 "void main(void)\n"
4578 "{\n"
4579 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
4580 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
4581 "}\n",
4584 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
4585 if (!blt_pshader)
4587 FIXME("tex_type %#x not supported\n", tex_type);
4588 return 0;
4591 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4592 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
4594 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4595 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
4597 program_id = GL_EXTCALL(glCreateProgramObjectARB());
4598 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
4599 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
4600 GL_EXTCALL(glLinkProgramARB(program_id));
4602 shader_glsl_validate_link(gl_info, program_id);
4604 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
4605 * is destroyed
4607 GL_EXTCALL(glDeleteObjectARB(vshader_id));
4608 GL_EXTCALL(glDeleteObjectARB(pshader_id));
4609 return program_id;
4612 /* GL locking is done by the caller */
4613 static void shader_glsl_select(const struct wined3d_context *context, BOOL usePS, BOOL useVS)
4615 const struct wined3d_gl_info *gl_info = context->gl_info;
4616 struct wined3d_device *device = context->swapchain->device;
4617 struct shader_glsl_priv *priv = device->shader_priv;
4618 GLhandleARB program_id = 0;
4619 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
4621 old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
4623 if (useVS || usePS) set_glsl_shader_program(context, device, usePS, useVS);
4624 else priv->glsl_program = NULL;
4626 current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
4628 if (old_vertex_color_clamp != current_vertex_color_clamp)
4630 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
4632 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
4633 checkGLcall("glClampColorARB");
4635 else
4637 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
4641 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
4642 if (program_id) TRACE("Using GLSL program %u\n", program_id);
4643 GL_EXTCALL(glUseProgramObjectARB(program_id));
4644 checkGLcall("glUseProgramObjectARB");
4646 /* In case that NP2 texcoord fixup data is found for the selected program, trigger a reload of the
4647 * constants. This has to be done because it can't be guaranteed that sampler() (from state.c) is
4648 * called between selecting the shader and using it, which results in wrong fixup for some frames. */
4649 if (priv->glsl_program && priv->glsl_program->np2Fixup_info)
4651 shader_glsl_load_np2fixup_constants(priv, gl_info, &device->stateBlock->state);
4655 /* GL locking is done by the caller */
4656 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
4657 enum tex_types tex_type, const SIZE *ds_mask_size)
4659 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
4660 struct shader_glsl_priv *priv = shader_priv;
4661 GLhandleARB *blt_program;
4662 GLint loc;
4664 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
4665 if (!*blt_program)
4667 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
4668 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
4669 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
4670 GL_EXTCALL(glUniform1iARB(loc, 0));
4672 else
4674 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
4677 if (masked)
4679 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "mask"));
4680 GL_EXTCALL(glUniform4fARB(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
4684 /* GL locking is done by the caller */
4685 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
4687 struct shader_glsl_priv *priv = shader_priv;
4688 GLhandleARB program_id;
4690 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
4691 if (program_id) TRACE("Using GLSL program %u\n", program_id);
4693 GL_EXTCALL(glUseProgramObjectARB(program_id));
4694 checkGLcall("glUseProgramObjectARB");
4697 static void shader_glsl_destroy(struct wined3d_shader *shader)
4699 struct wined3d_device *device = shader->device;
4700 struct shader_glsl_priv *priv = device->shader_priv;
4701 const struct wined3d_gl_info *gl_info;
4702 const struct list *linked_programs;
4703 struct wined3d_context *context;
4705 char pshader = shader_is_pshader_version(shader->reg_maps.shader_version.type);
4707 if (pshader)
4709 struct glsl_pshader_private *shader_data = shader->backend_data;
4711 if (!shader_data || !shader_data->num_gl_shaders)
4713 HeapFree(GetProcessHeap(), 0, shader_data);
4714 shader->backend_data = NULL;
4715 return;
4718 context = context_acquire(device, NULL);
4719 gl_info = context->gl_info;
4721 if (priv->glsl_program && priv->glsl_program->pshader == shader)
4723 ENTER_GL();
4724 shader_glsl_select(context, FALSE, FALSE);
4725 LEAVE_GL();
4728 else
4730 struct glsl_vshader_private *shader_data = shader->backend_data;
4732 if (!shader_data || !shader_data->num_gl_shaders)
4734 HeapFree(GetProcessHeap(), 0, shader_data);
4735 shader->backend_data = NULL;
4736 return;
4739 context = context_acquire(device, NULL);
4740 gl_info = context->gl_info;
4742 if (priv->glsl_program && priv->glsl_program->vshader == shader)
4744 ENTER_GL();
4745 shader_glsl_select(context, FALSE, FALSE);
4746 LEAVE_GL();
4750 linked_programs = &shader->linked_programs;
4752 TRACE("Deleting linked programs\n");
4753 if (linked_programs->next) {
4754 struct glsl_shader_prog_link *entry, *entry2;
4756 ENTER_GL();
4757 if(pshader) {
4758 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
4759 delete_glsl_program_entry(priv, gl_info, entry);
4761 } else {
4762 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, vshader_entry) {
4763 delete_glsl_program_entry(priv, gl_info, entry);
4766 LEAVE_GL();
4769 if (pshader)
4771 struct glsl_pshader_private *shader_data = shader->backend_data;
4772 UINT i;
4774 ENTER_GL();
4775 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4776 TRACE("deleting pshader %u\n", shader_data->gl_shaders[i].prgId);
4777 GL_EXTCALL(glDeleteObjectARB(shader_data->gl_shaders[i].prgId));
4778 checkGLcall("glDeleteObjectARB");
4780 LEAVE_GL();
4781 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders);
4783 else
4785 struct glsl_vshader_private *shader_data = shader->backend_data;
4786 UINT i;
4788 ENTER_GL();
4789 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4790 TRACE("deleting vshader %u\n", shader_data->gl_shaders[i].prgId);
4791 GL_EXTCALL(glDeleteObjectARB(shader_data->gl_shaders[i].prgId));
4792 checkGLcall("glDeleteObjectARB");
4794 LEAVE_GL();
4795 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders);
4798 HeapFree(GetProcessHeap(), 0, shader->backend_data);
4799 shader->backend_data = NULL;
4801 context_release(context);
4804 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
4806 const struct glsl_program_key *k = key;
4807 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
4808 const struct glsl_shader_prog_link, program_lookup_entry);
4809 int cmp;
4811 if (k->vshader > prog->vshader) return 1;
4812 else if (k->vshader < prog->vshader) return -1;
4814 if (k->pshader > prog->pshader) return 1;
4815 else if (k->pshader < prog->pshader) return -1;
4817 if (k->vshader && (cmp = memcmp(&k->vs_args, &prog->vs_args, sizeof(prog->vs_args)))) return cmp;
4818 if (k->pshader && (cmp = memcmp(&k->ps_args, &prog->ps_args, sizeof(prog->ps_args)))) return cmp;
4820 return 0;
4823 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
4825 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries) + constant_count * sizeof(*heap->positions);
4826 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
4828 if (!mem)
4830 ERR("Failed to allocate memory\n");
4831 return FALSE;
4834 heap->entries = mem;
4835 heap->entries[1].version = 0;
4836 heap->positions = (unsigned int *)(heap->entries + constant_count + 1);
4837 heap->size = 1;
4839 return TRUE;
4842 static void constant_heap_free(struct constant_heap *heap)
4844 HeapFree(GetProcessHeap(), 0, heap->entries);
4847 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
4849 wined3d_rb_alloc,
4850 wined3d_rb_realloc,
4851 wined3d_rb_free,
4852 glsl_program_key_compare,
4855 static HRESULT shader_glsl_alloc(struct wined3d_device *device)
4857 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4858 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
4859 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
4860 gl_info->limits.glsl_ps_float_constants)) + 1;
4862 if (!shader_buffer_init(&priv->shader_buffer))
4864 ERR("Failed to initialize shader buffer.\n");
4865 goto fail;
4868 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
4869 if (!priv->stack)
4871 ERR("Failed to allocate memory.\n");
4872 goto fail;
4875 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
4877 ERR("Failed to initialize vertex shader constant heap\n");
4878 goto fail;
4881 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
4883 ERR("Failed to initialize pixel shader constant heap\n");
4884 goto fail;
4887 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
4889 ERR("Failed to initialize rbtree.\n");
4890 goto fail;
4893 priv->next_constant_version = 1;
4895 device->shader_priv = priv;
4896 return WINED3D_OK;
4898 fail:
4899 constant_heap_free(&priv->pconst_heap);
4900 constant_heap_free(&priv->vconst_heap);
4901 HeapFree(GetProcessHeap(), 0, priv->stack);
4902 shader_buffer_free(&priv->shader_buffer);
4903 HeapFree(GetProcessHeap(), 0, priv);
4904 return E_OUTOFMEMORY;
4907 /* Context activation is done by the caller. */
4908 static void shader_glsl_free(struct wined3d_device *device)
4910 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4911 struct shader_glsl_priv *priv = device->shader_priv;
4912 int i;
4914 ENTER_GL();
4915 for (i = 0; i < tex_type_count; ++i)
4917 if (priv->depth_blt_program_full[i])
4919 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_full[i]));
4921 if (priv->depth_blt_program_masked[i])
4923 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_masked[i]));
4926 LEAVE_GL();
4928 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
4929 constant_heap_free(&priv->pconst_heap);
4930 constant_heap_free(&priv->vconst_heap);
4931 HeapFree(GetProcessHeap(), 0, priv->stack);
4932 shader_buffer_free(&priv->shader_buffer);
4934 HeapFree(GetProcessHeap(), 0, device->shader_priv);
4935 device->shader_priv = NULL;
4938 static void shader_glsl_context_destroyed(void *shader_priv, const struct wined3d_context *context) {}
4940 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
4942 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_GEOMETRY_SHADER4]
4943 && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50))
4945 caps->VertexShaderVersion = 4;
4946 caps->PixelShaderVersion = 4;
4948 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
4949 * texldd and texldl instructions. */
4950 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
4952 caps->VertexShaderVersion = 3;
4953 caps->PixelShaderVersion = 3;
4955 else
4957 caps->VertexShaderVersion = 2;
4958 caps->PixelShaderVersion = 2;
4961 caps->MaxVertexShaderConst = gl_info->limits.glsl_vs_float_constants;
4962 caps->MaxPixelShaderConst = gl_info->limits.glsl_ps_float_constants;
4964 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
4965 * Direct3D minimum requirement.
4967 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
4968 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
4970 * The problem is that the refrast clamps temporary results in the shader to
4971 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
4972 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
4973 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
4974 * offer a way to query this.
4976 caps->PixelShader1xMaxValue = 8.0;
4978 caps->VSClipping = TRUE;
4980 TRACE_(d3d_caps)("Hardware vertex shader version %u enabled (GLSL).\n",
4981 caps->VertexShaderVersion);
4982 TRACE_(d3d_caps)("Hardware pixel shader version %u enabled (GLSL).\n",
4983 caps->PixelShaderVersion);
4986 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
4988 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
4990 TRACE("Checking support for fixup:\n");
4991 dump_color_fixup_desc(fixup);
4994 /* We support everything except YUV conversions. */
4995 if (!is_complex_fixup(fixup))
4997 TRACE("[OK]\n");
4998 return TRUE;
5001 TRACE("[FAILED]\n");
5002 return FALSE;
5005 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
5007 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
5008 /* WINED3DSIH_ADD */ shader_glsl_arith,
5009 /* WINED3DSIH_AND */ NULL,
5010 /* WINED3DSIH_BEM */ shader_glsl_bem,
5011 /* WINED3DSIH_BREAK */ shader_glsl_break,
5012 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
5013 /* WINED3DSIH_BREAKP */ NULL,
5014 /* WINED3DSIH_CALL */ shader_glsl_call,
5015 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
5016 /* WINED3DSIH_CMP */ shader_glsl_cmp,
5017 /* WINED3DSIH_CND */ shader_glsl_cnd,
5018 /* WINED3DSIH_CRS */ shader_glsl_cross,
5019 /* WINED3DSIH_CUT */ NULL,
5020 /* WINED3DSIH_DCL */ NULL,
5021 /* WINED3DSIH_DEF */ NULL,
5022 /* WINED3DSIH_DEFB */ NULL,
5023 /* WINED3DSIH_DEFI */ NULL,
5024 /* WINED3DSIH_DIV */ NULL,
5025 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
5026 /* WINED3DSIH_DP3 */ shader_glsl_dot,
5027 /* WINED3DSIH_DP4 */ shader_glsl_dot,
5028 /* WINED3DSIH_DST */ shader_glsl_dst,
5029 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
5030 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
5031 /* WINED3DSIH_ELSE */ shader_glsl_else,
5032 /* WINED3DSIH_EMIT */ NULL,
5033 /* WINED3DSIH_ENDIF */ shader_glsl_end,
5034 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
5035 /* WINED3DSIH_ENDREP */ shader_glsl_end,
5036 /* WINED3DSIH_EQ */ NULL,
5037 /* WINED3DSIH_EXP */ shader_glsl_map2gl,
5038 /* WINED3DSIH_EXPP */ shader_glsl_expp,
5039 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
5040 /* WINED3DSIH_FTOI */ NULL,
5041 /* WINED3DSIH_GE */ NULL,
5042 /* WINED3DSIH_IADD */ NULL,
5043 /* WINED3DSIH_IEQ */ NULL,
5044 /* WINED3DSIH_IF */ shader_glsl_if,
5045 /* WINED3DSIH_IFC */ shader_glsl_ifc,
5046 /* WINED3DSIH_IGE */ NULL,
5047 /* WINED3DSIH_IMUL */ NULL,
5048 /* WINED3DSIH_ITOF */ NULL,
5049 /* WINED3DSIH_LABEL */ shader_glsl_label,
5050 /* WINED3DSIH_LD */ NULL,
5051 /* WINED3DSIH_LIT */ shader_glsl_lit,
5052 /* WINED3DSIH_LOG */ shader_glsl_log,
5053 /* WINED3DSIH_LOGP */ shader_glsl_log,
5054 /* WINED3DSIH_LOOP */ shader_glsl_loop,
5055 /* WINED3DSIH_LRP */ shader_glsl_lrp,
5056 /* WINED3DSIH_LT */ NULL,
5057 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
5058 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
5059 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
5060 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
5061 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
5062 /* WINED3DSIH_MAD */ shader_glsl_mad,
5063 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
5064 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
5065 /* WINED3DSIH_MOV */ shader_glsl_mov,
5066 /* WINED3DSIH_MOVA */ shader_glsl_mov,
5067 /* WINED3DSIH_MOVC */ NULL,
5068 /* WINED3DSIH_MUL */ shader_glsl_arith,
5069 /* WINED3DSIH_NOP */ NULL,
5070 /* WINED3DSIH_NRM */ shader_glsl_nrm,
5071 /* WINED3DSIH_PHASE */ NULL,
5072 /* WINED3DSIH_POW */ shader_glsl_pow,
5073 /* WINED3DSIH_RCP */ shader_glsl_rcp,
5074 /* WINED3DSIH_REP */ shader_glsl_rep,
5075 /* WINED3DSIH_RET */ shader_glsl_ret,
5076 /* WINED3DSIH_ROUND_NI */ NULL,
5077 /* WINED3DSIH_RSQ */ shader_glsl_rsq,
5078 /* WINED3DSIH_SAMPLE */ NULL,
5079 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
5080 /* WINED3DSIH_SAMPLE_LOD */ NULL,
5081 /* WINED3DSIH_SETP */ NULL,
5082 /* WINED3DSIH_SGE */ shader_glsl_compare,
5083 /* WINED3DSIH_SGN */ shader_glsl_sgn,
5084 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
5085 /* WINED3DSIH_SLT */ shader_glsl_compare,
5086 /* WINED3DSIH_SQRT */ NULL,
5087 /* WINED3DSIH_SUB */ shader_glsl_arith,
5088 /* WINED3DSIH_TEX */ shader_glsl_tex,
5089 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
5090 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
5091 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
5092 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
5093 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
5094 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
5095 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
5096 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
5097 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
5098 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
5099 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
5100 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
5101 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
5102 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
5103 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
5104 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
5105 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
5106 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
5107 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
5108 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
5109 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
5110 /* WINED3DSIH_UDIV */ NULL,
5111 /* WINED3DSIH_USHR */ NULL,
5112 /* WINED3DSIH_UTOF */ NULL,
5113 /* WINED3DSIH_XOR */ NULL,
5116 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
5117 SHADER_HANDLER hw_fct;
5119 /* Select handler */
5120 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
5122 /* Unhandled opcode */
5123 if (!hw_fct)
5125 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
5126 return;
5128 hw_fct(ins);
5130 shader_glsl_add_instruction_modifiers(ins);
5133 const struct wined3d_shader_backend_ops glsl_shader_backend =
5135 shader_glsl_handle_instruction,
5136 shader_glsl_select,
5137 shader_glsl_select_depth_blt,
5138 shader_glsl_deselect_depth_blt,
5139 shader_glsl_update_float_vertex_constants,
5140 shader_glsl_update_float_pixel_constants,
5141 shader_glsl_load_constants,
5142 shader_glsl_load_np2fixup_constants,
5143 shader_glsl_destroy,
5144 shader_glsl_alloc,
5145 shader_glsl_free,
5146 shader_glsl_context_destroyed,
5147 shader_glsl_get_caps,
5148 shader_glsl_color_fixup_supported,