wined3d: Constants in constants_set should always have a valid location in shader_gls...
[wine/multimedia.git] / dlls / wined3d / glsl_shader.c
blob057763f6e7adced0a1eac12455749ef429789667
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 "wine/port.h"
35 #include <limits.h>
36 #include <stdio.h>
38 #include "wined3d_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
41 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
42 WINE_DECLARE_DEBUG_CHANNEL(d3d);
43 WINE_DECLARE_DEBUG_CHANNEL(winediag);
45 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x1
46 #define WINED3D_GLSL_SAMPLE_RECT 0x2
47 #define WINED3D_GLSL_SAMPLE_LOD 0x4
48 #define WINED3D_GLSL_SAMPLE_GRAD 0x8
50 struct glsl_dst_param
52 char reg_name[150];
53 char mask_str[6];
56 struct glsl_src_param
58 char reg_name[150];
59 char param_str[200];
62 struct glsl_sample_function
64 const char *name;
65 DWORD coord_mask;
68 enum heap_node_op
70 HEAP_NODE_TRAVERSE_LEFT,
71 HEAP_NODE_TRAVERSE_RIGHT,
72 HEAP_NODE_POP,
75 struct constant_entry
77 unsigned int idx;
78 unsigned int version;
81 struct constant_heap
83 struct constant_entry *entries;
84 unsigned int *positions;
85 unsigned int size;
88 /* GLSL shader private data */
89 struct shader_glsl_priv {
90 struct wined3d_shader_buffer shader_buffer;
91 struct wine_rb_tree program_lookup;
92 struct glsl_shader_prog_link *glsl_program;
93 struct constant_heap vconst_heap;
94 struct constant_heap pconst_heap;
95 unsigned char *stack;
96 GLhandleARB depth_blt_program_full[tex_type_count];
97 GLhandleARB depth_blt_program_masked[tex_type_count];
98 UINT next_constant_version;
100 const struct fragment_pipeline *fragment_pipe;
101 struct wine_rb_tree ffp_fragment_shaders;
102 BOOL ffp_proj_control;
105 struct glsl_vs_program
107 struct list shader_entry;
108 GLhandleARB id;
109 GLenum vertex_color_clamp;
110 GLint *uniform_f_locations;
111 GLint uniform_i_locations[MAX_CONST_I];
112 GLint pos_fixup_location;
115 struct glsl_gs_program
117 struct list shader_entry;
118 GLhandleARB id;
121 struct glsl_ps_program
123 struct list shader_entry;
124 GLhandleARB id;
125 GLint *uniform_f_locations;
126 GLint uniform_i_locations[MAX_CONST_I];
127 GLint bumpenv_mat_location[MAX_TEXTURES];
128 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
129 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
130 GLint tex_factor_location;
131 GLint specular_enable_location;
132 GLint ycorrection_location;
133 GLint np2_fixup_location;
134 const struct ps_np2fixup_info *np2_fixup_info;
137 /* Struct to maintain data about a linked GLSL program */
138 struct glsl_shader_prog_link
140 struct wine_rb_entry program_lookup_entry;
141 struct glsl_vs_program vs;
142 struct glsl_gs_program gs;
143 struct glsl_ps_program ps;
144 GLhandleARB programId;
145 UINT constant_version;
148 struct glsl_program_key
150 GLhandleARB vs_id;
151 GLhandleARB gs_id;
152 GLhandleARB ps_id;
155 struct shader_glsl_ctx_priv {
156 const struct vs_compile_args *cur_vs_args;
157 const struct ps_compile_args *cur_ps_args;
158 struct ps_np2fixup_info *cur_np2fixup_info;
161 struct glsl_ps_compiled_shader
163 struct ps_compile_args args;
164 struct ps_np2fixup_info np2fixup;
165 GLhandleARB prgId;
168 struct glsl_vs_compiled_shader
170 struct vs_compile_args args;
171 GLhandleARB prgId;
174 struct glsl_gs_compiled_shader
176 GLhandleARB id;
179 struct glsl_shader_private
181 union
183 struct glsl_vs_compiled_shader *vs;
184 struct glsl_gs_compiled_shader *gs;
185 struct glsl_ps_compiled_shader *ps;
186 } gl_shaders;
187 UINT num_gl_shaders, shader_array_size;
190 struct glsl_ffp_fragment_shader
192 struct ffp_frag_desc entry;
193 GLhandleARB id;
194 struct list linked_programs;
197 static const char *debug_gl_shader_type(GLenum type)
199 switch (type)
201 #define WINED3D_TO_STR(u) case u: return #u
202 WINED3D_TO_STR(GL_VERTEX_SHADER_ARB);
203 WINED3D_TO_STR(GL_GEOMETRY_SHADER_ARB);
204 WINED3D_TO_STR(GL_FRAGMENT_SHADER_ARB);
205 #undef WINED3D_TO_STR
206 default:
207 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
211 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
213 switch (type)
215 case WINED3D_SHADER_TYPE_VERTEX:
216 return "vs";
218 case WINED3D_SHADER_TYPE_GEOMETRY:
219 return "gs";
221 case WINED3D_SHADER_TYPE_PIXEL:
222 return "ps";
224 default:
225 FIXME("Unhandled shader type %#x.\n", type);
226 return "unknown";
230 /* Extract a line from the info log.
231 * Note that this modifies the source string. */
232 static char *get_info_log_line(char **ptr)
234 char *p, *q;
236 p = *ptr;
237 if (!(q = strstr(p, "\n")))
239 if (!*p) return NULL;
240 *ptr += strlen(p);
241 return p;
243 *q = '\0';
244 *ptr = q + 1;
246 return p;
249 /* Context activation is done by the caller. */
250 static void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLhandleARB obj)
252 int infologLength = 0;
253 char *infoLog;
255 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
256 return;
258 GL_EXTCALL(glGetObjectParameterivARB(obj,
259 GL_OBJECT_INFO_LOG_LENGTH_ARB,
260 &infologLength));
262 /* A size of 1 is just a null-terminated string, so the log should be bigger than
263 * that if there are errors. */
264 if (infologLength > 1)
266 char *ptr, *line;
268 infoLog = HeapAlloc(GetProcessHeap(), 0, infologLength);
269 /* The info log is supposed to be zero-terminated, but at least some
270 * versions of fglrx don't terminate the string properly. The reported
271 * length does include the terminator, so explicitly set it to zero
272 * here. */
273 infoLog[infologLength - 1] = 0;
274 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
276 ptr = infoLog;
277 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
279 WARN("Info log received from GLSL shader #%u:\n", obj);
280 while ((line = get_info_log_line(&ptr))) WARN(" %s\n", line);
282 else
284 FIXME("Info log received from GLSL shader #%u:\n", obj);
285 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
287 HeapFree(GetProcessHeap(), 0, infoLog);
291 /* Context activation is done by the caller. */
292 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLhandleARB shader, const char *src)
294 TRACE("Compiling shader object %u.\n", shader);
295 GL_EXTCALL(glShaderSourceARB(shader, 1, &src, NULL));
296 checkGLcall("glShaderSourceARB");
297 GL_EXTCALL(glCompileShaderARB(shader));
298 checkGLcall("glCompileShaderARB");
299 print_glsl_info_log(gl_info, shader);
302 /* Context activation is done by the caller. */
303 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLhandleARB program)
305 GLint i, object_count, source_size = -1;
306 GLhandleARB *objects;
307 char *source = NULL;
309 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_ATTACHED_OBJECTS_ARB, &object_count));
310 objects = HeapAlloc(GetProcessHeap(), 0, object_count * sizeof(*objects));
311 if (!objects)
313 ERR("Failed to allocate object array memory.\n");
314 return;
317 GL_EXTCALL(glGetAttachedObjectsARB(program, object_count, NULL, objects));
318 for (i = 0; i < object_count; ++i)
320 char *ptr, *line;
321 GLint tmp;
323 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &tmp));
325 if (source_size < tmp)
327 HeapFree(GetProcessHeap(), 0, source);
329 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
330 if (!source)
332 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
333 HeapFree(GetProcessHeap(), 0, objects);
334 return;
336 source_size = tmp;
339 FIXME("Object %u:\n", objects[i]);
340 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SUBTYPE_ARB, &tmp));
341 FIXME(" GL_OBJECT_SUBTYPE_ARB: %s.\n", debug_gl_shader_type(tmp));
342 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_COMPILE_STATUS_ARB, &tmp));
343 FIXME(" GL_OBJECT_COMPILE_STATUS_ARB: %d.\n", tmp);
344 FIXME("\n");
346 ptr = source;
347 GL_EXTCALL(glGetShaderSourceARB(objects[i], source_size, NULL, source));
348 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
349 FIXME("\n");
352 HeapFree(GetProcessHeap(), 0, source);
353 HeapFree(GetProcessHeap(), 0, objects);
356 /* Context activation is done by the caller. */
357 static void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLhandleARB program)
359 GLint tmp;
361 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader)) return;
363 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_TYPE_ARB, &tmp));
364 if (tmp == GL_PROGRAM_OBJECT_ARB)
366 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &tmp));
367 if (!tmp)
369 FIXME("Program %u link status invalid.\n", program);
370 shader_glsl_dump_program_source(gl_info, program);
374 print_glsl_info_log(gl_info, program);
377 /* Context activation is done by the caller. */
378 static void shader_glsl_load_psamplers(const struct wined3d_gl_info *gl_info,
379 const DWORD *tex_unit_map, GLhandleARB programId)
381 GLint name_loc;
382 char sampler_name[20];
383 unsigned int i;
385 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
387 snprintf(sampler_name, sizeof(sampler_name), "ps_sampler%u", i);
388 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
389 if (name_loc != -1) {
390 DWORD mapped_unit = tex_unit_map[i];
391 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.fragment_samplers)
393 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
394 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
395 checkGLcall("glUniform1iARB");
396 } else {
397 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
403 /* Context activation is done by the caller. */
404 static void shader_glsl_load_vsamplers(const struct wined3d_gl_info *gl_info,
405 const DWORD *tex_unit_map, GLhandleARB programId)
407 GLint name_loc;
408 char sampler_name[20];
409 unsigned int i;
411 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
413 snprintf(sampler_name, sizeof(sampler_name), "vs_sampler%u", i);
414 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
415 if (name_loc != -1) {
416 DWORD mapped_unit = tex_unit_map[MAX_FRAGMENT_SAMPLERS + i];
417 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.combined_samplers)
419 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
420 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
421 checkGLcall("glUniform1iARB");
422 } else {
423 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
429 /* Context activation is done by the caller. */
430 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
431 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
433 int stack_idx = 0;
434 unsigned int heap_idx = 1;
435 unsigned int idx;
437 if (heap->entries[heap_idx].version <= version) return;
439 idx = heap->entries[heap_idx].idx;
440 if (constant_locations[idx] != -1) GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
441 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
443 while (stack_idx >= 0)
445 /* Note that we fall through to the next case statement. */
446 switch(stack[stack_idx])
448 case HEAP_NODE_TRAVERSE_LEFT:
450 unsigned int left_idx = heap_idx << 1;
451 if (left_idx < heap->size && heap->entries[left_idx].version > version)
453 heap_idx = left_idx;
454 idx = heap->entries[heap_idx].idx;
455 if (constant_locations[idx] != -1)
456 GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
458 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
459 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
460 break;
464 case HEAP_NODE_TRAVERSE_RIGHT:
466 unsigned int right_idx = (heap_idx << 1) + 1;
467 if (right_idx < heap->size && heap->entries[right_idx].version > version)
469 heap_idx = right_idx;
470 idx = heap->entries[heap_idx].idx;
471 if (constant_locations[idx] != -1)
472 GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
474 stack[stack_idx++] = HEAP_NODE_POP;
475 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
476 break;
480 case HEAP_NODE_POP:
481 heap_idx >>= 1;
482 --stack_idx;
483 break;
486 checkGLcall("walk_constant_heap()");
489 /* Context activation is done by the caller. */
490 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
492 GLfloat clamped_constant[4];
494 if (location == -1) return;
496 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
497 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
498 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
499 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
501 GL_EXTCALL(glUniform4fvARB(location, 1, clamped_constant));
504 /* Context activation is done by the caller. */
505 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
506 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
508 int stack_idx = 0;
509 unsigned int heap_idx = 1;
510 unsigned int idx;
512 if (heap->entries[heap_idx].version <= version) return;
514 idx = heap->entries[heap_idx].idx;
515 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
516 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
518 while (stack_idx >= 0)
520 /* Note that we fall through to the next case statement. */
521 switch(stack[stack_idx])
523 case HEAP_NODE_TRAVERSE_LEFT:
525 unsigned int left_idx = heap_idx << 1;
526 if (left_idx < heap->size && heap->entries[left_idx].version > version)
528 heap_idx = left_idx;
529 idx = heap->entries[heap_idx].idx;
530 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
532 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
533 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
534 break;
538 case HEAP_NODE_TRAVERSE_RIGHT:
540 unsigned int right_idx = (heap_idx << 1) + 1;
541 if (right_idx < heap->size && heap->entries[right_idx].version > version)
543 heap_idx = right_idx;
544 idx = heap->entries[heap_idx].idx;
545 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
547 stack[stack_idx++] = HEAP_NODE_POP;
548 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
549 break;
553 case HEAP_NODE_POP:
554 heap_idx >>= 1;
555 --stack_idx;
556 break;
559 checkGLcall("walk_constant_heap_clamped()");
562 /* Context activation is done by the caller. */
563 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
564 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
565 unsigned char *stack, UINT version)
567 const struct wined3d_shader_lconst *lconst;
569 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
570 if (shader->reg_maps.shader_version.major == 1
571 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
572 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
573 else
574 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
576 if (!shader->load_local_constsF)
578 TRACE("No need to load local float constants for this shader\n");
579 return;
582 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
583 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
585 GLint location = constant_locations[lconst->idx];
586 /* We found this uniform name in the program - go ahead and send the data */
587 if (location != -1) GL_EXTCALL(glUniform4fvARB(location, 1, (const GLfloat *)lconst->value));
589 checkGLcall("glUniform4fvARB()");
592 /* Context activation is done by the caller. */
593 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
594 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
596 unsigned int i;
597 struct list* ptr;
599 for (i = 0; constants_set; constants_set >>= 1, ++i)
601 if (!(constants_set & 1)) continue;
603 TRACE_(d3d_constants)("Loading constants %u: %i, %i, %i, %i\n",
604 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
606 /* We found this uniform name in the program - go ahead and send the data */
607 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
608 checkGLcall("glUniform4ivARB");
611 /* Load immediate constants */
612 ptr = list_head(&shader->constantsI);
613 while (ptr)
615 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
616 unsigned int idx = lconst->idx;
617 const GLint *values = (const GLint *)lconst->value;
619 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
620 values[0], values[1], values[2], values[3]);
622 /* We found this uniform name in the program - go ahead and send the data */
623 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
624 checkGLcall("glUniform4ivARB");
625 ptr = list_next(&shader->constantsI, ptr);
629 /* Context activation is done by the caller. */
630 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
631 GLhandleARB programId, const BOOL *constants, WORD constants_set)
633 GLint tmp_loc;
634 unsigned int i;
635 char tmp_name[10];
636 const char *prefix;
637 struct list* ptr;
639 prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
641 /* TODO: Benchmark and see if it would be beneficial to store the
642 * locations of the constants to avoid looking up each time */
643 for (i = 0; constants_set; constants_set >>= 1, ++i)
645 if (!(constants_set & 1)) continue;
647 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i]);
649 /* TODO: Benchmark and see if it would be beneficial to store the
650 * locations of the constants to avoid looking up each time */
651 snprintf(tmp_name, sizeof(tmp_name), "%s_b[%i]", prefix, i);
652 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
653 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
654 checkGLcall("glUniform1ivARB");
657 /* Load immediate constants */
658 ptr = list_head(&shader->constantsB);
659 while (ptr)
661 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
662 unsigned int idx = lconst->idx;
663 const GLint *values = (const GLint *)lconst->value;
665 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
667 snprintf(tmp_name, sizeof(tmp_name), "%s_b[%i]", prefix, idx);
668 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
669 if (tmp_loc != -1) {
670 /* We found this uniform name in the program - go ahead and send the data */
671 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
672 checkGLcall("glUniform1ivARB");
674 ptr = list_next(&shader->constantsB, ptr);
678 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
680 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
683 /* Context activation is done by the caller (state handler). */
684 static void shader_glsl_load_np2fixup_constants(void *shader_priv,
685 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
687 struct shader_glsl_priv *glsl_priv = shader_priv;
688 const struct glsl_shader_prog_link *prog = glsl_priv->glsl_program;
690 /* No GLSL program set - nothing to do. */
691 if (!prog) return;
693 /* NP2 texcoord fixup is (currently) only done for pixelshaders. */
694 if (!use_ps(state)) return;
696 if (prog->ps.np2_fixup_info && prog->ps.np2_fixup_location != -1)
698 UINT i;
699 UINT fixup = prog->ps.np2_fixup_info->active;
700 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
702 for (i = 0; fixup; fixup >>= 1, ++i)
704 const struct wined3d_texture *tex = state->textures[i];
705 const unsigned char idx = prog->ps.np2_fixup_info->idx[i];
706 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
708 if (!tex)
710 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
711 continue;
714 if (idx % 2)
716 tex_dim[2] = tex->pow2_matrix[0];
717 tex_dim[3] = tex->pow2_matrix[5];
719 else
721 tex_dim[0] = tex->pow2_matrix[0];
722 tex_dim[1] = tex->pow2_matrix[5];
726 GL_EXTCALL(glUniform4fvARB(prog->ps.np2_fixup_location,
727 prog->ps.np2_fixup_info->num_consts, np2fixup_constants));
731 /* Context activation is done by the caller (state handler). */
732 static void shader_glsl_load_constants(const struct wined3d_context *context,
733 BOOL usePixelShader, BOOL useVertexShader)
735 const struct wined3d_gl_info *gl_info = context->gl_info;
736 struct wined3d_device *device = context->swapchain->device;
737 struct wined3d_stateblock *stateBlock = device->stateBlock;
738 const struct wined3d_state *state = &stateBlock->state;
739 struct shader_glsl_priv *priv = device->shader_priv;
740 float position_fixup[4];
742 GLhandleARB programId;
743 struct glsl_shader_prog_link *prog = priv->glsl_program;
744 UINT constant_version;
745 int i;
747 if (!prog) {
748 /* No GLSL program set - nothing to do. */
749 return;
751 programId = prog->programId;
752 constant_version = prog->constant_version;
754 if (useVertexShader)
756 const struct wined3d_shader *vshader = state->vertex_shader;
758 /* Load DirectX 9 float constants/uniforms for vertex shader */
759 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
760 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
762 /* Load DirectX 9 integer constants/uniforms for vertex shader */
763 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
764 stateBlock->changed.vertexShaderConstantsI & vshader->reg_maps.integer_constants);
766 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
767 shader_glsl_load_constantsB(vshader, gl_info, programId, state->vs_consts_b,
768 stateBlock->changed.vertexShaderConstantsB & vshader->reg_maps.boolean_constants);
770 /* Upload the position fixup params */
771 shader_get_position_fixup(context, state, position_fixup);
772 GL_EXTCALL(glUniform4fvARB(prog->vs.pos_fixup_location, 1, position_fixup));
773 checkGLcall("glUniform4fvARB");
776 if (usePixelShader)
778 const struct wined3d_shader *pshader = state->pixel_shader;
780 /* Load DirectX 9 float constants/uniforms for pixel shader */
781 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
782 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
784 /* Load DirectX 9 integer constants/uniforms for pixel shader */
785 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
786 stateBlock->changed.pixelShaderConstantsI & pshader->reg_maps.integer_constants);
788 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
789 shader_glsl_load_constantsB(pshader, gl_info, programId, state->ps_consts_b,
790 stateBlock->changed.pixelShaderConstantsB & pshader->reg_maps.boolean_constants);
792 /* Upload the environment bump map matrix if needed. The needsbumpmat
793 * member specifies the texture stage to load the matrix from. It
794 * can't be 0 for a valid texbem instruction. */
795 for (i = 0; i < MAX_TEXTURES; ++i)
797 const float *data;
799 if (prog->ps.bumpenv_mat_location[i] == -1)
800 continue;
802 data = (const float *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00];
803 GL_EXTCALL(glUniformMatrix2fvARB(prog->ps.bumpenv_mat_location[i], 1, 0, data));
804 checkGLcall("glUniformMatrix2fvARB");
806 /* texbeml needs the luminance scale and offset too. If texbeml
807 * is used, needsbumpmat is set too, so we can check that in the
808 * needsbumpmat check. */
809 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
811 const GLfloat *scale = (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE];
812 const GLfloat *offset = (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET];
814 GL_EXTCALL(glUniform1fvARB(prog->ps.bumpenv_lum_scale_location[i], 1, scale));
815 checkGLcall("glUniform1fvARB");
816 GL_EXTCALL(glUniform1fvARB(prog->ps.bumpenv_lum_offset_location[i], 1, offset));
817 checkGLcall("glUniform1fvARB");
821 if (prog->ps.ycorrection_location != -1)
823 float correction_params[4];
825 if (context->render_offscreen)
827 correction_params[0] = 0.0f;
828 correction_params[1] = 1.0f;
829 } else {
830 /* position is window relative, not viewport relative */
831 correction_params[0] = (float) context->current_rt->resource.height;
832 correction_params[1] = -1.0f;
834 GL_EXTCALL(glUniform4fvARB(prog->ps.ycorrection_location, 1, correction_params));
837 else if (priv->fragment_pipe == &glsl_fragment_pipe)
839 float col[4];
841 for (i = 0; i < MAX_TEXTURES; ++i)
843 GL_EXTCALL(glUniformMatrix2fvARB(prog->ps.bumpenv_mat_location[i], 1, 0,
844 (const float *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
845 GL_EXTCALL(glUniform1fARB(prog->ps.bumpenv_lum_scale_location[i],
846 *(const float *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
847 GL_EXTCALL(glUniform1fARB(prog->ps.bumpenv_lum_offset_location[i],
848 *(const float *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
851 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_TEXTUREFACTOR], col);
852 GL_EXTCALL(glUniform4fARB(prog->ps.tex_factor_location, col[0], col[1], col[2], col[3]));
854 if (state->render_states[WINED3D_RS_SPECULARENABLE])
855 GL_EXTCALL(glUniform4fARB(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
856 else
857 GL_EXTCALL(glUniform4fARB(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
859 checkGLcall("fixed function uniforms");
862 if (priv->next_constant_version == UINT_MAX)
864 TRACE("Max constant version reached, resetting to 0.\n");
865 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
866 priv->next_constant_version = 1;
868 else
870 prog->constant_version = priv->next_constant_version++;
874 static void update_heap_entry(const struct constant_heap *heap, unsigned int idx,
875 unsigned int heap_idx, DWORD new_version)
877 struct constant_entry *entries = heap->entries;
878 unsigned int *positions = heap->positions;
879 unsigned int parent_idx;
881 while (heap_idx > 1)
883 parent_idx = heap_idx >> 1;
885 if (new_version <= entries[parent_idx].version) break;
887 entries[heap_idx] = entries[parent_idx];
888 positions[entries[parent_idx].idx] = heap_idx;
889 heap_idx = parent_idx;
892 entries[heap_idx].version = new_version;
893 entries[heap_idx].idx = idx;
894 positions[idx] = heap_idx;
897 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
899 struct shader_glsl_priv *priv = device->shader_priv;
900 struct constant_heap *heap = &priv->vconst_heap;
901 UINT i;
903 for (i = start; i < count + start; ++i)
905 if (!device->stateBlock->changed.vertexShaderConstantsF[i])
906 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
907 else
908 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
912 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
914 struct shader_glsl_priv *priv = device->shader_priv;
915 struct constant_heap *heap = &priv->pconst_heap;
916 UINT i;
918 for (i = start; i < count + start; ++i)
920 if (!device->stateBlock->changed.pixelShaderConstantsF[i])
921 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
922 else
923 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
927 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
929 unsigned int ret = gl_info->limits.glsl_varyings / 4;
930 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
931 if(shader_major > 3) return ret;
933 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
934 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
935 return ret;
938 /** Generate the variable & register declarations for the GLSL output target */
939 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
940 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
941 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
943 const struct wined3d_shader_version *version = &reg_maps->shader_version;
944 const struct wined3d_state *state = &shader->device->stateBlock->state;
945 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
946 const struct wined3d_gl_info *gl_info = context->gl_info;
947 const struct wined3d_fb_state *fb = &shader->device->fb;
948 unsigned int i, extra_constants_needed = 0;
949 const struct wined3d_shader_lconst *lconst;
950 const char *prefix;
951 DWORD map;
953 prefix = shader_glsl_get_prefix(version->type);
955 /* Prototype the subroutines */
956 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
958 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
961 /* Declare the constants (aka uniforms) */
962 if (shader->limits.constant_float > 0)
964 unsigned max_constantsF;
966 /* Unless the shader uses indirect addressing, always declare the
967 * maximum array size and ignore that we need some uniforms privately.
968 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
969 * and immediate values, still declare VC[256]. If the shader needs
970 * more uniforms than we have it won't work in any case. If it uses
971 * less, the compiler will figure out which uniforms are really used
972 * and strip them out. This allows a shader to use c255 on a dx9 card,
973 * as long as it doesn't also use all the other constants.
975 * If the shader uses indirect addressing the compiler must assume
976 * that all declared uniforms are used. In this case, declare only the
977 * amount that we're assured to have.
979 * Thus we run into problems in these two cases:
980 * 1) The shader really uses more uniforms than supported.
981 * 2) The shader uses indirect addressing, less constants than
982 * supported, but uses a constant index > #supported consts. */
983 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
985 /* No indirect addressing here. */
986 max_constantsF = gl_info->limits.glsl_ps_float_constants;
988 else
990 if (reg_maps->usesrelconstF)
992 /* Subtract the other potential uniforms from the max
993 * available (bools, ints, and 1 row of projection matrix).
994 * Subtract another uniform for immediate values, which have
995 * to be loaded via uniform by the driver as well. The shader
996 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
997 * shader code, so one vec4 should be enough. (Unfortunately
998 * the Nvidia driver doesn't store 128 and -128 in one float).
1000 * Writing gl_ClipVertex requires one uniform for each
1001 * clipplane as well. */
1002 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1003 if(ctx_priv->cur_vs_args->clip_enabled)
1005 max_constantsF -= gl_info->limits.clipplanes;
1007 max_constantsF -= count_bits(reg_maps->integer_constants);
1008 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1009 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1010 * for now take this into account when calculating the number of available constants
1012 max_constantsF -= count_bits(reg_maps->boolean_constants);
1013 /* Set by driver quirks in directx.c */
1014 max_constantsF -= gl_info->reserved_glsl_constants;
1016 if (max_constantsF < shader->limits.constant_float)
1018 static unsigned int once;
1020 if (!once++)
1021 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1022 " it may not render correctly.\n");
1023 else
1024 WARN("The hardware does not support enough uniform components to run this shader.\n");
1027 else
1029 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1032 max_constantsF = min(shader->limits.constant_float, max_constantsF);
1033 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1036 /* Always declare the full set of constants, the compiler can remove the
1037 * unused ones because d3d doesn't (yet) support indirect int and bool
1038 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1039 if (shader->limits.constant_int > 0 && reg_maps->integer_constants)
1040 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits.constant_int);
1042 if (shader->limits.constant_bool > 0 && reg_maps->boolean_constants)
1043 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits.constant_bool);
1045 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1047 if (reg_maps->cb_sizes[i])
1048 shader_addline(buffer, "uniform vec4 %s_cb%u[%u];\n", prefix, i, reg_maps->cb_sizes[i]);
1051 /* Declare texture samplers */
1052 for (i = 0; i < shader->limits.sampler; ++i)
1054 if (reg_maps->sampler_type[i])
1056 BOOL shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1 << i));
1057 BOOL tex_rect;
1059 switch (reg_maps->sampler_type[i])
1061 case WINED3DSTT_1D:
1062 if (shadow_sampler)
1063 shader_addline(buffer, "uniform sampler1DShadow %s_sampler%u;\n", prefix, i);
1064 else
1065 shader_addline(buffer, "uniform sampler1D %s_sampler%u;\n", prefix, i);
1066 break;
1067 case WINED3DSTT_2D:
1068 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->np2_fixup & (1 << i));
1069 tex_rect = tex_rect && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1070 if (shadow_sampler)
1072 if (tex_rect)
1073 shader_addline(buffer, "uniform sampler2DRectShadow %s_sampler%u;\n", prefix, i);
1074 else
1075 shader_addline(buffer, "uniform sampler2DShadow %s_sampler%u;\n", prefix, i);
1077 else
1079 if (tex_rect)
1080 shader_addline(buffer, "uniform sampler2DRect %s_sampler%u;\n", prefix, i);
1081 else
1082 shader_addline(buffer, "uniform sampler2D %s_sampler%u;\n", prefix, i);
1084 break;
1085 case WINED3DSTT_CUBE:
1086 if (shadow_sampler)
1087 FIXME("Unsupported Cube shadow sampler.\n");
1088 shader_addline(buffer, "uniform samplerCube %s_sampler%u;\n", prefix, i);
1089 break;
1090 case WINED3DSTT_VOLUME:
1091 if (shadow_sampler)
1092 FIXME("Unsupported 3D shadow sampler.\n");
1093 shader_addline(buffer, "uniform sampler3D %s_sampler%u;\n", prefix, i);
1094 break;
1095 default:
1096 shader_addline(buffer, "uniform unsupported_sampler %s_sampler%u;\n", prefix, i);
1097 FIXME("Unrecognized sampler type: %#x\n", reg_maps->sampler_type[i]);
1098 break;
1103 /* Declare uniforms for NP2 texcoord fixup:
1104 * This is NOT done inside the loop that declares the texture samplers
1105 * since the NP2 fixup code is currently only used for the GeforceFX
1106 * series and when forcing the ARB_npot extension off. Modern cards just
1107 * skip the code anyway, so put it inside a separate loop. */
1108 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1110 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1111 UINT cur = 0;
1113 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1114 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1115 * samplerNP2Fixup stores texture dimensions and is updated through
1116 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1118 for (i = 0; i < shader->limits.sampler; ++i)
1120 if (reg_maps->sampler_type[i])
1122 if (!(ps_args->np2_fixup & (1 << i))) continue;
1124 if (WINED3DSTT_2D != reg_maps->sampler_type[i]) {
1125 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1126 continue;
1129 fixup->idx[i] = cur++;
1133 fixup->num_consts = (cur + 1) >> 1;
1134 fixup->active = ps_args->np2_fixup;
1135 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1138 /* Declare address variables */
1139 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1141 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1144 /* Declare texture coordinate temporaries and initialize them */
1145 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1147 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1150 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1152 /* Declare attributes. */
1153 for (i = 0, map = reg_maps->input_registers; map; map >>= 1, ++i)
1155 if (map & 1)
1156 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, i);
1159 shader_addline(buffer, "uniform vec4 posFixup;\n");
1160 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits.packed_output);
1162 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1164 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits.packed_input);
1166 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1168 if (version->major >= 3)
1170 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits.packed_input);
1172 if (use_vs(state))
1173 shader_addline(buffer, "varying vec4 %s_in[%u];\n", prefix, in_count);
1174 else
1175 /* TODO: Write a replacement shader for the fixed function
1176 * vertex pipeline, so this isn't needed. For fixed function
1177 * vertex processing + 3.0 pixel shader we need a separate
1178 * function in the pixel shader that reads the fixed function
1179 * color into the packed input registers. */
1180 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1183 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1185 if (!(map & 1))
1186 continue;
1188 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1190 if (reg_maps->luminanceparams & (1 << i))
1192 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1193 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1194 extra_constants_needed++;
1197 extra_constants_needed++;
1200 if (ps_args->srgb_correction)
1202 shader_addline(buffer, "const vec4 srgb_const0 = vec4(%.8e, %.8e, %.8e, %.8e);\n",
1203 srgb_pow, srgb_mul_high, srgb_sub_high, srgb_mul_low);
1204 shader_addline(buffer, "const vec4 srgb_const1 = vec4(%.8e, 0.0, 0.0, 0.0);\n",
1205 srgb_cmp);
1207 if (reg_maps->vpos || reg_maps->usesdsy)
1209 if (shader->limits.constant_float + extra_constants_needed
1210 + 1 < gl_info->limits.glsl_ps_float_constants)
1212 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1213 extra_constants_needed++;
1215 else
1217 /* This happens because we do not have proper tracking of the constant registers that are
1218 * actually used, only the max limit of the shader version
1220 FIXME("Cannot find a free uniform for vpos correction params\n");
1221 shader_addline(buffer, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
1222 context->render_offscreen ? 0.0f : fb->render_targets[0]->resource.height,
1223 context->render_offscreen ? 1.0f : -1.0f);
1225 shader_addline(buffer, "vec4 vpos;\n");
1229 /* Declare output register temporaries */
1230 if (shader->limits.packed_output)
1231 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits.packed_output);
1233 /* Declare temporary variables */
1234 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1236 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1239 /* Declare loop registers aLx */
1240 if (version->major < 4)
1242 for (i = 0; i < reg_maps->loop_depth; ++i)
1244 shader_addline(buffer, "int aL%u;\n", i);
1245 shader_addline(buffer, "int tmpInt%u;\n", i);
1249 /* Temporary variables for matrix operations */
1250 shader_addline(buffer, "vec4 tmp0;\n");
1251 shader_addline(buffer, "vec4 tmp1;\n");
1253 if (!shader->load_local_constsF)
1255 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1257 const float *value;
1258 value = (const float *)lconst->value;
1259 shader_addline(buffer, "const vec4 %s_lc%u = vec4(%.8e, %.8e, %.8e, %.8e);\n",
1260 prefix, lconst->idx, value[0], value[1], value[2], value[3]);
1264 /* Start the main program. */
1265 shader_addline(buffer, "void main()\n{\n");
1267 /* Direct3D applications expect integer vPos values, while OpenGL drivers
1268 * add approximately 0.5. This causes off-by-one problems as spotted by
1269 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
1270 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
1271 * causes precision troubles when we just subtract 0.5.
1273 * To deal with that, just floor() the position. This will eliminate the
1274 * fraction on all cards.
1276 * TODO: Test how this behaves with multisampling.
1278 * An advantage of floor is that it works even if the driver doesn't add
1279 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
1280 * to return in gl_FragCoord, even though coordinates specify the pixel
1281 * centers instead of the pixel corners. This code will behave correctly
1282 * on drivers that returns integer values. */
1283 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
1284 shader_addline(buffer,
1285 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1288 /*****************************************************************************
1289 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1291 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1292 ****************************************************************************/
1294 /* Prototypes */
1295 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1296 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1298 /** Used for opcode modifiers - They multiply the result by the specified amount */
1299 static const char * const shift_glsl_tab[] = {
1300 "", /* 0 (none) */
1301 "2.0 * ", /* 1 (x2) */
1302 "4.0 * ", /* 2 (x4) */
1303 "8.0 * ", /* 3 (x8) */
1304 "16.0 * ", /* 4 (x16) */
1305 "32.0 * ", /* 5 (x32) */
1306 "", /* 6 (x64) */
1307 "", /* 7 (x128) */
1308 "", /* 8 (d256) */
1309 "", /* 9 (d128) */
1310 "", /* 10 (d64) */
1311 "", /* 11 (d32) */
1312 "0.0625 * ", /* 12 (d16) */
1313 "0.125 * ", /* 13 (d8) */
1314 "0.25 * ", /* 14 (d4) */
1315 "0.5 * " /* 15 (d2) */
1318 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1319 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1320 const char *in_reg, const char *in_regswizzle, char *out_str)
1322 out_str[0] = 0;
1324 switch (src_modifier)
1326 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1327 case WINED3DSPSM_DW:
1328 case WINED3DSPSM_NONE:
1329 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1330 break;
1331 case WINED3DSPSM_NEG:
1332 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1333 break;
1334 case WINED3DSPSM_NOT:
1335 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1336 break;
1337 case WINED3DSPSM_BIAS:
1338 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1339 break;
1340 case WINED3DSPSM_BIASNEG:
1341 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1342 break;
1343 case WINED3DSPSM_SIGN:
1344 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1345 break;
1346 case WINED3DSPSM_SIGNNEG:
1347 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1348 break;
1349 case WINED3DSPSM_COMP:
1350 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1351 break;
1352 case WINED3DSPSM_X2:
1353 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1354 break;
1355 case WINED3DSPSM_X2NEG:
1356 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1357 break;
1358 case WINED3DSPSM_ABS:
1359 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1360 break;
1361 case WINED3DSPSM_ABSNEG:
1362 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1363 break;
1364 default:
1365 FIXME("Unhandled modifier %u\n", src_modifier);
1366 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1370 /** Writes the GLSL variable name that corresponds to the register that the
1371 * DX opcode parameter is trying to access */
1372 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1373 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1375 /* oPos, oFog and oPts in D3D */
1376 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
1378 const struct wined3d_shader *shader = ins->ctx->shader;
1379 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1380 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1381 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1382 const char *prefix = shader_glsl_get_prefix(version->type);
1383 struct glsl_src_param rel_param0, rel_param1;
1385 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
1386 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
1387 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
1388 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
1389 *is_color = FALSE;
1391 switch (reg->type)
1393 case WINED3DSPR_TEMP:
1394 sprintf(register_name, "R%u", reg->idx[0].offset);
1395 break;
1397 case WINED3DSPR_INPUT:
1398 /* vertex shaders */
1399 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1401 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1402 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx[0].offset))
1403 *is_color = TRUE;
1404 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
1405 break;
1408 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1410 if (reg->idx[0].rel_addr)
1412 if (reg->idx[1].rel_addr)
1413 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
1414 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1415 else
1416 sprintf(register_name, "gs_in[%s + %u][%u]",
1417 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
1419 else if (reg->idx[1].rel_addr)
1420 sprintf(register_name, "gs_in[%u][%s + %u]",
1421 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1422 else
1423 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
1424 break;
1427 /* pixel shaders >= 3.0 */
1428 if (version->major >= 3)
1430 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
1431 unsigned int in_count = vec4_varyings(version->major, gl_info);
1433 if (reg->idx[0].rel_addr)
1435 /* Removing a + 0 would be an obvious optimization, but
1436 * OS X doesn't see the NOP operation there. */
1437 if (idx)
1439 if (shader->u.ps.declared_in_count > in_count)
1441 sprintf(register_name,
1442 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
1443 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
1444 prefix, rel_param0.param_str, idx);
1446 else
1448 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
1451 else
1453 if (shader->u.ps.declared_in_count > in_count)
1455 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
1456 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
1457 prefix, rel_param0.param_str);
1459 else
1461 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
1465 else
1467 if (idx == in_count) sprintf(register_name, "gl_Color");
1468 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
1469 else sprintf(register_name, "%s_in[%u]", prefix, idx);
1472 else
1474 if (!reg->idx[0].offset)
1475 strcpy(register_name, "gl_Color");
1476 else
1477 strcpy(register_name, "gl_SecondaryColor");
1478 break;
1480 break;
1482 case WINED3DSPR_CONST:
1484 /* Relative addressing */
1485 if (reg->idx[0].rel_addr)
1487 if (reg->idx[0].offset)
1488 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
1489 else
1490 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
1492 else
1494 if (shader_constant_is_local(shader, reg->idx[0].offset))
1495 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
1496 else
1497 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
1500 break;
1502 case WINED3DSPR_CONSTINT:
1503 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
1504 break;
1506 case WINED3DSPR_CONSTBOOL:
1507 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
1508 break;
1510 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1511 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1512 sprintf(register_name, "T%u", reg->idx[0].offset);
1513 else
1514 sprintf(register_name, "A%u", reg->idx[0].offset);
1515 break;
1517 case WINED3DSPR_LOOP:
1518 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
1519 break;
1521 case WINED3DSPR_SAMPLER:
1522 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
1523 break;
1525 case WINED3DSPR_COLOROUT:
1526 if (reg->idx[0].offset >= gl_info->limits.buffers)
1527 WARN("Write to render target %u, only %d supported.\n",
1528 reg->idx[0].offset, gl_info->limits.buffers);
1530 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
1531 break;
1533 case WINED3DSPR_RASTOUT:
1534 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
1535 break;
1537 case WINED3DSPR_DEPTHOUT:
1538 sprintf(register_name, "gl_FragDepth");
1539 break;
1541 case WINED3DSPR_ATTROUT:
1542 if (!reg->idx[0].offset)
1543 sprintf(register_name, "%s_out[8]", prefix);
1544 else
1545 sprintf(register_name, "%s_out[9]", prefix);
1546 break;
1548 case WINED3DSPR_TEXCRDOUT:
1549 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1550 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
1551 break;
1553 case WINED3DSPR_MISCTYPE:
1554 if (!reg->idx[0].offset)
1556 /* vPos */
1557 sprintf(register_name, "vpos");
1559 else if (reg->idx[0].offset == 1)
1561 /* Note that gl_FrontFacing is a bool, while vFace is
1562 * a float for which the sign determines front/back */
1563 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1565 else
1567 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
1568 sprintf(register_name, "unrecognized_register");
1570 break;
1572 case WINED3DSPR_IMMCONST:
1573 switch (reg->immconst_type)
1575 case WINED3D_IMMCONST_SCALAR:
1576 switch (reg->data_type)
1578 case WINED3D_DATA_FLOAT:
1579 sprintf(register_name, "%.8e", *(const float *)reg->immconst_data);
1580 break;
1581 case WINED3D_DATA_INT:
1582 sprintf(register_name, "%#x", reg->immconst_data[0]);
1583 break;
1584 case WINED3D_DATA_RESOURCE:
1585 case WINED3D_DATA_SAMPLER:
1586 case WINED3D_DATA_UINT:
1587 sprintf(register_name, "%#xu", reg->immconst_data[0]);
1588 break;
1589 default:
1590 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1591 break;
1593 break;
1595 case WINED3D_IMMCONST_VEC4:
1596 switch (reg->data_type)
1598 case WINED3D_DATA_FLOAT:
1599 sprintf(register_name, "vec4(%.8e, %.8e, %.8e, %.8e)",
1600 *(const float *)&reg->immconst_data[0], *(const float *)&reg->immconst_data[1],
1601 *(const float *)&reg->immconst_data[2], *(const float *)&reg->immconst_data[3]);
1602 break;
1603 case WINED3D_DATA_INT:
1604 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
1605 reg->immconst_data[0], reg->immconst_data[1],
1606 reg->immconst_data[2], reg->immconst_data[3]);
1607 break;
1608 case WINED3D_DATA_RESOURCE:
1609 case WINED3D_DATA_SAMPLER:
1610 case WINED3D_DATA_UINT:
1611 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
1612 reg->immconst_data[0], reg->immconst_data[1],
1613 reg->immconst_data[2], reg->immconst_data[3]);
1614 break;
1615 default:
1616 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1617 break;
1619 break;
1621 default:
1622 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
1623 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
1625 break;
1627 case WINED3DSPR_CONSTBUFFER:
1628 if (reg->idx[1].rel_addr)
1629 sprintf(register_name, "%s_cb%u[%s + %u]",
1630 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1631 else
1632 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
1633 break;
1635 case WINED3DSPR_PRIMID:
1636 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
1637 break;
1639 default:
1640 FIXME("Unhandled register type %#x.\n", reg->type);
1641 sprintf(register_name, "unrecognized_register");
1642 break;
1646 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1648 *str++ = '.';
1649 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1650 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1651 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1652 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1653 *str = '\0';
1656 /* Get the GLSL write mask for the destination register */
1657 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1659 DWORD mask = param->write_mask;
1661 if (shader_is_scalar(&param->reg))
1663 mask = WINED3DSP_WRITEMASK_0;
1664 *write_mask = '\0';
1666 else
1668 shader_glsl_write_mask_to_str(mask, write_mask);
1671 return mask;
1674 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1675 unsigned int size = 0;
1677 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1678 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1679 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1680 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1682 return size;
1685 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
1687 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1688 * but addressed as "rgba". To fix this we need to swap the register's x
1689 * and z components. */
1690 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1692 *str++ = '.';
1693 /* swizzle bits fields: wwzzyyxx */
1694 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
1695 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
1696 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
1697 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
1698 *str = '\0';
1701 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
1702 BOOL fixup, DWORD mask, char *swizzle_str)
1704 if (shader_is_scalar(&param->reg))
1705 *swizzle_str = '\0';
1706 else
1707 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
1710 /* From a given parameter token, generate the corresponding GLSL string.
1711 * Also, return the actual register name and swizzle in case the
1712 * caller needs this information as well. */
1713 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1714 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
1716 BOOL is_color = FALSE;
1717 char swizzle_str[6];
1719 glsl_src->reg_name[0] = '\0';
1720 glsl_src->param_str[0] = '\0';
1721 swizzle_str[0] = '\0';
1723 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
1724 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
1726 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
1728 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
1730 else
1732 char param_str[200];
1734 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, param_str);
1736 switch (wined3d_src->reg.data_type)
1738 case WINED3D_DATA_FLOAT:
1739 sprintf(glsl_src->param_str, "%s", param_str);
1740 break;
1741 case WINED3D_DATA_INT:
1742 sprintf(glsl_src->param_str, "floatBitsToInt(%s)", param_str);
1743 break;
1744 case WINED3D_DATA_RESOURCE:
1745 case WINED3D_DATA_SAMPLER:
1746 case WINED3D_DATA_UINT:
1747 sprintf(glsl_src->param_str, "floatBitsToUint(%s)", param_str);
1748 break;
1749 default:
1750 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
1751 sprintf(glsl_src->param_str, "%s", param_str);
1752 break;
1757 /* From a given parameter token, generate the corresponding GLSL string.
1758 * Also, return the actual register name and swizzle in case the
1759 * caller needs this information as well. */
1760 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1761 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
1763 BOOL is_color = FALSE;
1765 glsl_dst->mask_str[0] = '\0';
1766 glsl_dst->reg_name[0] = '\0';
1768 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
1769 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1772 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1773 static DWORD shader_glsl_append_dst_ext(struct wined3d_shader_buffer *buffer,
1774 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst)
1776 struct glsl_dst_param glsl_dst;
1777 DWORD mask;
1779 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
1781 switch (dst->reg.data_type)
1783 case WINED3D_DATA_FLOAT:
1784 shader_addline(buffer, "%s%s = %s(",
1785 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1786 break;
1787 case WINED3D_DATA_INT:
1788 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
1789 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1790 break;
1791 case WINED3D_DATA_RESOURCE:
1792 case WINED3D_DATA_SAMPLER:
1793 case WINED3D_DATA_UINT:
1794 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
1795 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1796 break;
1797 default:
1798 FIXME("Unhandled data type %#x.\n", dst->reg.data_type);
1799 shader_addline(buffer, "%s%s = %s(",
1800 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1801 break;
1805 return mask;
1808 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1809 static DWORD shader_glsl_append_dst(struct wined3d_shader_buffer *buffer, const struct wined3d_shader_instruction *ins)
1811 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
1814 /** Process GLSL instruction modifiers */
1815 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1817 struct glsl_dst_param dst_param;
1818 DWORD modifiers;
1820 if (!ins->dst_count) return;
1822 modifiers = ins->dst[0].modifiers;
1823 if (!modifiers) return;
1825 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
1827 if (modifiers & WINED3DSPDM_SATURATE)
1829 /* _SAT means to clamp the value of the register to between 0 and 1 */
1830 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1831 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1834 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
1836 FIXME("_centroid modifier not handled\n");
1839 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
1841 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1845 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
1847 switch (op)
1849 case WINED3D_SHADER_REL_OP_GT: return ">";
1850 case WINED3D_SHADER_REL_OP_EQ: return "==";
1851 case WINED3D_SHADER_REL_OP_GE: return ">=";
1852 case WINED3D_SHADER_REL_OP_LT: return "<";
1853 case WINED3D_SHADER_REL_OP_NE: return "!=";
1854 case WINED3D_SHADER_REL_OP_LE: return "<=";
1855 default:
1856 FIXME("Unrecognized operator %#x.\n", op);
1857 return "(\?\?)";
1861 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
1862 DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
1864 enum wined3d_sampler_texture_type sampler_type = ctx->reg_maps->sampler_type[sampler_idx];
1865 const struct wined3d_gl_info *gl_info = ctx->gl_info;
1866 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
1867 && (((const struct shader_glsl_ctx_priv *)ctx->backend_data)->cur_ps_args->shadow & (1 << sampler_idx));
1868 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
1869 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_RECT;
1870 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
1871 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
1873 /* Note that there's no such thing as a projected cube texture. */
1874 switch(sampler_type) {
1875 case WINED3DSTT_1D:
1876 if (shadow)
1878 if (lod)
1880 sample_function->name = projected ? "shadow1DProjLod" : "shadow1DLod";
1882 else if (grad)
1884 if (gl_info->supported[EXT_GPU_SHADER4])
1885 sample_function->name = projected ? "shadow1DProjGrad" : "shadow1DGrad";
1886 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1887 sample_function->name = projected ? "shadow1DProjGradARB" : "shadow1DGradARB";
1888 else
1890 FIXME("Unsupported 1D shadow grad function.\n");
1891 sample_function->name = "unsupported1DGrad";
1894 else
1896 sample_function->name = projected ? "shadow1DProj" : "shadow1D";
1898 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1900 else
1902 if (lod)
1904 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
1906 else if (grad)
1908 if (gl_info->supported[EXT_GPU_SHADER4])
1909 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
1910 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1911 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
1912 else
1914 FIXME("Unsupported 1D grad function.\n");
1915 sample_function->name = "unsupported1DGrad";
1918 else
1920 sample_function->name = projected ? "texture1DProj" : "texture1D";
1922 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1924 break;
1926 case WINED3DSTT_2D:
1927 if (shadow)
1929 if (texrect)
1931 if (lod)
1933 sample_function->name = projected ? "shadow2DRectProjLod" : "shadow2DRectLod";
1935 else if (grad)
1937 if (gl_info->supported[EXT_GPU_SHADER4])
1938 sample_function->name = projected ? "shadow2DRectProjGrad" : "shadow2DRectGrad";
1939 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1940 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
1941 else
1943 FIXME("Unsupported RECT shadow grad function.\n");
1944 sample_function->name = "unsupported2DRectGrad";
1947 else
1949 sample_function->name = projected ? "shadow2DRectProj" : "shadow2DRect";
1952 else
1954 if (lod)
1956 sample_function->name = projected ? "shadow2DProjLod" : "shadow2DLod";
1958 else if (grad)
1960 if (gl_info->supported[EXT_GPU_SHADER4])
1961 sample_function->name = projected ? "shadow2DProjGrad" : "shadow2DGrad";
1962 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1963 sample_function->name = projected ? "shadow2DProjGradARB" : "shadow2DGradARB";
1964 else
1966 FIXME("Unsupported 2D shadow grad function.\n");
1967 sample_function->name = "unsupported2DGrad";
1970 else
1972 sample_function->name = projected ? "shadow2DProj" : "shadow2D";
1975 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1977 else
1979 if (texrect)
1981 if (lod)
1983 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
1985 else if (grad)
1987 if (gl_info->supported[EXT_GPU_SHADER4])
1988 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
1989 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1990 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
1991 else
1993 FIXME("Unsupported RECT grad function.\n");
1994 sample_function->name = "unsupported2DRectGrad";
1997 else
1999 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
2002 else
2004 if (lod)
2006 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
2008 else if (grad)
2010 if (gl_info->supported[EXT_GPU_SHADER4])
2011 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
2012 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2013 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
2014 else
2016 FIXME("Unsupported 2D grad function.\n");
2017 sample_function->name = "unsupported2DGrad";
2020 else
2022 sample_function->name = projected ? "texture2DProj" : "texture2D";
2025 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2027 break;
2029 case WINED3DSTT_CUBE:
2030 if (shadow)
2032 FIXME("Unsupported Cube shadow function.\n");
2033 sample_function->name = "unsupportedCubeShadow";
2034 sample_function->coord_mask = 0;
2036 else
2038 if (lod)
2040 sample_function->name = "textureCubeLod";
2042 else if (grad)
2044 if (gl_info->supported[EXT_GPU_SHADER4])
2045 sample_function->name = "textureCubeGrad";
2046 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2047 sample_function->name = "textureCubeGradARB";
2048 else
2050 FIXME("Unsupported Cube grad function.\n");
2051 sample_function->name = "unsupportedCubeGrad";
2054 else
2056 sample_function->name = "textureCube";
2058 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2060 break;
2062 case WINED3DSTT_VOLUME:
2063 if (shadow)
2065 FIXME("Unsupported 3D shadow function.\n");
2066 sample_function->name = "unsupported3DShadow";
2067 sample_function->coord_mask = 0;
2069 else
2071 if (lod)
2073 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
2075 else if (grad)
2077 if (gl_info->supported[EXT_GPU_SHADER4])
2078 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
2079 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2080 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
2081 else
2083 FIXME("Unsupported 3D grad function.\n");
2084 sample_function->name = "unsupported3DGrad";
2087 else
2089 sample_function->name = projected ? "texture3DProj" : "texture3D";
2091 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2093 break;
2095 default:
2096 sample_function->name = "";
2097 sample_function->coord_mask = 0;
2098 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
2099 break;
2103 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2104 BOOL sign_fixup, enum fixup_channel_source channel_source)
2106 switch(channel_source)
2108 case CHANNEL_SOURCE_ZERO:
2109 strcat(arguments, "0.0");
2110 break;
2112 case CHANNEL_SOURCE_ONE:
2113 strcat(arguments, "1.0");
2114 break;
2116 case CHANNEL_SOURCE_X:
2117 strcat(arguments, reg_name);
2118 strcat(arguments, ".x");
2119 break;
2121 case CHANNEL_SOURCE_Y:
2122 strcat(arguments, reg_name);
2123 strcat(arguments, ".y");
2124 break;
2126 case CHANNEL_SOURCE_Z:
2127 strcat(arguments, reg_name);
2128 strcat(arguments, ".z");
2129 break;
2131 case CHANNEL_SOURCE_W:
2132 strcat(arguments, reg_name);
2133 strcat(arguments, ".w");
2134 break;
2136 default:
2137 FIXME("Unhandled channel source %#x\n", channel_source);
2138 strcat(arguments, "undefined");
2139 break;
2142 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2145 static void shader_glsl_color_correction_ext(struct wined3d_shader_buffer *buffer,
2146 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2148 unsigned int mask_size, remaining;
2149 DWORD fixup_mask = 0;
2150 char arguments[256];
2151 char mask_str[6];
2153 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2154 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2155 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2156 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2157 if (!(mask &= fixup_mask))
2158 return;
2160 if (is_complex_fixup(fixup))
2162 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2163 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2164 return;
2167 shader_glsl_write_mask_to_str(mask, mask_str);
2168 mask_size = shader_glsl_get_write_mask_size(mask);
2170 arguments[0] = '\0';
2171 remaining = mask_size;
2172 if (mask & WINED3DSP_WRITEMASK_0)
2174 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2175 if (--remaining) strcat(arguments, ", ");
2177 if (mask & WINED3DSP_WRITEMASK_1)
2179 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2180 if (--remaining) strcat(arguments, ", ");
2182 if (mask & WINED3DSP_WRITEMASK_2)
2184 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2185 if (--remaining) strcat(arguments, ", ");
2187 if (mask & WINED3DSP_WRITEMASK_3)
2189 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2190 if (--remaining) strcat(arguments, ", ");
2193 if (mask_size > 1)
2194 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2195 else
2196 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2199 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2201 char reg_name[256];
2202 BOOL is_color;
2204 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2205 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2208 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2209 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2210 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2212 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2213 char dst_swizzle[6];
2214 struct color_fixup_desc fixup;
2215 BOOL np2_fixup = FALSE;
2216 va_list args;
2218 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2220 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2222 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2223 fixup = priv->cur_ps_args->color_fixup[sampler];
2225 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2226 if(bias) {
2227 FIXME("Biased sampling from NP2 textures is unsupported\n");
2228 } else {
2229 np2_fixup = TRUE;
2233 else
2235 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
2238 shader_glsl_append_dst(ins->ctx->buffer, ins);
2240 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2241 sample_function->name, shader_glsl_get_prefix(version->type), sampler);
2243 va_start(args, coord_reg_fmt);
2244 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2245 va_end(args);
2247 if(bias) {
2248 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2249 } else {
2250 if (np2_fixup) {
2251 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2252 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2254 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2255 (idx % 2) ? "zw" : "xy", dst_swizzle);
2256 } else if(dx && dy) {
2257 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2258 } else {
2259 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2263 if(!is_identity_fixup(fixup)) {
2264 shader_glsl_color_correction(ins, fixup);
2268 /*****************************************************************************
2269 * Begin processing individual instruction opcodes
2270 ****************************************************************************/
2272 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2274 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2275 struct glsl_src_param src0_param;
2276 struct glsl_src_param src1_param;
2277 DWORD write_mask;
2278 const char *op;
2280 /* Determine the GLSL operator to use based on the opcode */
2281 switch (ins->handler_idx)
2283 case WINED3DSIH_ADD: op = "+"; break;
2284 case WINED3DSIH_AND: op = "&"; break;
2285 case WINED3DSIH_DIV: op = "/"; break;
2286 case WINED3DSIH_IADD: op = "+"; break;
2287 case WINED3DSIH_MUL: op = "*"; break;
2288 case WINED3DSIH_SUB: op = "-"; break;
2289 case WINED3DSIH_USHR: op = ">>"; break;
2290 case WINED3DSIH_XOR: op = "^"; break;
2291 default:
2292 op = "<unhandled operator>";
2293 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2294 break;
2297 write_mask = shader_glsl_append_dst(buffer, ins);
2298 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2299 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2300 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2303 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2305 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2306 struct glsl_src_param src0_param;
2307 struct glsl_src_param src1_param;
2308 unsigned int mask_size;
2309 DWORD write_mask;
2310 const char *op;
2312 write_mask = shader_glsl_append_dst(buffer, ins);
2313 mask_size = shader_glsl_get_write_mask_size(write_mask);
2314 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2315 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2317 if (mask_size > 1)
2319 switch (ins->handler_idx)
2321 case WINED3DSIH_EQ: op = "equal"; break;
2322 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2323 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2324 case WINED3DSIH_LT: op = "lessThan"; break;
2325 default:
2326 op = "<unhandled operator>";
2327 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2328 break;
2331 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2332 mask_size, op, src0_param.param_str, src1_param.param_str);
2334 else
2336 switch (ins->handler_idx)
2338 case WINED3DSIH_EQ: op = "=="; break;
2339 case WINED3DSIH_GE: op = ">="; break;
2340 case WINED3DSIH_IGE: op = ">="; break;
2341 case WINED3DSIH_LT: op = "<"; break;
2342 default:
2343 op = "<unhandled operator>";
2344 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2345 break;
2348 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2349 src0_param.param_str, op, src1_param.param_str);
2353 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2355 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2356 struct glsl_src_param src0_param;
2357 struct glsl_src_param src1_param;
2358 DWORD write_mask;
2360 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2361 * not, we can emulate it. */
2362 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2363 FIXME("64-bit integer multiplies not implemented.\n");
2365 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2367 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2368 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2369 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2371 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2372 src0_param.param_str, src1_param.param_str);
2376 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2378 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2379 struct glsl_src_param src0_param, src1_param;
2380 DWORD write_mask;
2382 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2385 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2387 char dst_mask[6];
2389 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2390 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2391 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2392 shader_addline(buffer, "tmp0%s = %s / %s;\n",
2393 dst_mask, src0_param.param_str, src1_param.param_str);
2395 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2396 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2397 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2398 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
2400 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
2401 shader_addline(buffer, "tmp0%s);\n", dst_mask);
2403 else
2405 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
2406 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2407 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2408 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
2411 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2413 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2414 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2415 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2416 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
2420 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2421 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2423 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2424 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2425 struct glsl_src_param src0_param;
2426 DWORD write_mask;
2428 write_mask = shader_glsl_append_dst(buffer, ins);
2429 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2431 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2432 * shader versions WINED3DSIO_MOVA is used for this. */
2433 if (ins->ctx->reg_maps->shader_version.major == 1
2434 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
2435 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2437 /* This is a simple floor() */
2438 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2439 if (mask_size > 1) {
2440 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2441 } else {
2442 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2445 else if(ins->handler_idx == WINED3DSIH_MOVA)
2447 /* We need to *round* to the nearest int here. */
2448 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2450 if (gl_info->supported[EXT_GPU_SHADER4])
2452 if (mask_size > 1)
2453 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2454 else
2455 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2457 else
2459 if (mask_size > 1)
2460 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2461 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2462 else
2463 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2464 src0_param.param_str, src0_param.param_str);
2467 else
2469 shader_addline(buffer, "%s);\n", src0_param.param_str);
2473 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2474 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2476 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2477 struct glsl_src_param src0_param;
2478 struct glsl_src_param src1_param;
2479 DWORD dst_write_mask, src_write_mask;
2480 unsigned int dst_size = 0;
2482 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2483 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2485 /* dp3 works on vec3, dp4 on vec4 */
2486 if (ins->handler_idx == WINED3DSIH_DP4)
2488 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2489 } else {
2490 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2493 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2494 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2496 if (dst_size > 1) {
2497 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2498 } else {
2499 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2503 /* Note that this instruction has some restrictions. The destination write mask
2504 * can't contain the w component, and the source swizzles have to be .xyzw */
2505 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2507 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2508 struct glsl_src_param src0_param;
2509 struct glsl_src_param src1_param;
2510 char dst_mask[6];
2512 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2513 shader_glsl_append_dst(ins->ctx->buffer, ins);
2514 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2515 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2516 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2519 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
2521 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
2524 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2525 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2526 * GLSL uses the value as-is. */
2527 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2529 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2530 struct glsl_src_param src0_param;
2531 struct glsl_src_param src1_param;
2532 DWORD dst_write_mask;
2533 unsigned int dst_size;
2535 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2536 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2538 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2539 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2541 if (dst_size > 1)
2543 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2544 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
2546 else
2548 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
2549 src1_param.param_str, src0_param.param_str, src1_param.param_str);
2553 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
2554 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
2555 * GLSL uses the value as-is. */
2556 static void shader_glsl_log(const struct wined3d_shader_instruction *ins)
2558 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2559 struct glsl_src_param src0_param;
2560 DWORD dst_write_mask;
2561 unsigned int dst_size;
2563 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2564 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2566 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2568 if (dst_size > 1)
2570 shader_addline(buffer, "vec%u(log2(abs(%s))));\n",
2571 dst_size, src0_param.param_str);
2573 else
2575 shader_addline(buffer, "log2(abs(%s)));\n",
2576 src0_param.param_str);
2580 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2581 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
2583 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2584 struct glsl_src_param src_param;
2585 const char *instruction;
2586 DWORD write_mask;
2587 unsigned i;
2589 /* Determine the GLSL function to use based on the opcode */
2590 /* TODO: Possibly make this a table for faster lookups */
2591 switch (ins->handler_idx)
2593 case WINED3DSIH_MIN: instruction = "min"; break;
2594 case WINED3DSIH_MAX: instruction = "max"; break;
2595 case WINED3DSIH_ABS: instruction = "abs"; break;
2596 case WINED3DSIH_FRC: instruction = "fract"; break;
2597 case WINED3DSIH_EXP: instruction = "exp2"; break;
2598 case WINED3DSIH_DSX: instruction = "dFdx"; break;
2599 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
2600 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
2601 default: instruction = "";
2602 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2603 break;
2606 write_mask = shader_glsl_append_dst(buffer, ins);
2608 shader_addline(buffer, "%s(", instruction);
2610 if (ins->src_count)
2612 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2613 shader_addline(buffer, "%s", src_param.param_str);
2614 for (i = 1; i < ins->src_count; ++i)
2616 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
2617 shader_addline(buffer, ", %s", src_param.param_str);
2621 shader_addline(buffer, "));\n");
2624 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
2626 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
2628 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2629 struct glsl_src_param src_param;
2630 unsigned int mask_size;
2631 DWORD write_mask;
2632 char dst_mask[6];
2634 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
2635 mask_size = shader_glsl_get_write_mask_size(write_mask);
2636 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2638 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
2639 src_param.param_str, src_param.param_str);
2640 shader_glsl_append_dst(buffer, ins);
2642 if (mask_size > 1)
2644 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
2645 mask_size, src_param.param_str);
2647 else
2649 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
2650 src_param.param_str);
2654 /** Process the WINED3DSIO_EXPP instruction in GLSL:
2655 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2656 * dst.x = 2^(floor(src))
2657 * dst.y = src - floor(src)
2658 * dst.z = 2^src (partial precision is allowed, but optional)
2659 * dst.w = 1.0;
2660 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2661 * dst = 2^src; (partial precision is allowed, but optional)
2663 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
2665 struct glsl_src_param src_param;
2667 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
2669 if (ins->ctx->reg_maps->shader_version.major < 2)
2671 char dst_mask[6];
2673 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
2674 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
2675 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
2676 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
2678 shader_glsl_append_dst(ins->ctx->buffer, ins);
2679 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2680 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
2681 } else {
2682 DWORD write_mask;
2683 unsigned int mask_size;
2685 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2686 mask_size = shader_glsl_get_write_mask_size(write_mask);
2688 if (mask_size > 1) {
2689 shader_addline(ins->ctx->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
2690 } else {
2691 shader_addline(ins->ctx->buffer, "exp2(%s));\n", src_param.param_str);
2696 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
2698 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2699 struct glsl_src_param src_param;
2700 unsigned int mask_size;
2701 DWORD write_mask;
2703 write_mask = shader_glsl_append_dst(buffer, ins);
2704 mask_size = shader_glsl_get_write_mask_size(write_mask);
2705 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2707 if (mask_size > 1)
2708 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
2709 else
2710 shader_addline(buffer, "int(%s));\n", src_param.param_str);
2713 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
2715 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2716 struct glsl_src_param src_param;
2717 unsigned int mask_size;
2718 DWORD write_mask;
2720 write_mask = shader_glsl_append_dst(buffer, ins);
2721 mask_size = shader_glsl_get_write_mask_size(write_mask);
2722 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2724 if (mask_size > 1)
2725 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
2726 else
2727 shader_addline(buffer, "float(%s));\n", src_param.param_str);
2730 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
2731 static void shader_glsl_rcp(const struct wined3d_shader_instruction *ins)
2733 struct glsl_src_param src_param;
2734 DWORD write_mask;
2735 unsigned int mask_size;
2737 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2738 mask_size = shader_glsl_get_write_mask_size(write_mask);
2739 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2741 if (mask_size > 1)
2743 shader_addline(ins->ctx->buffer, "vec%u(1.0 / %s));\n",
2744 mask_size, src_param.param_str);
2746 else
2748 shader_addline(ins->ctx->buffer, "1.0 / %s);\n",
2749 src_param.param_str);
2753 static void shader_glsl_rsq(const struct wined3d_shader_instruction *ins)
2755 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2756 struct glsl_src_param src_param;
2757 DWORD write_mask;
2758 unsigned int mask_size;
2760 write_mask = shader_glsl_append_dst(buffer, ins);
2761 mask_size = shader_glsl_get_write_mask_size(write_mask);
2763 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2765 if (mask_size > 1)
2767 shader_addline(buffer, "vec%u(inversesqrt(abs(%s))));\n",
2768 mask_size, src_param.param_str);
2770 else
2772 shader_addline(buffer, "inversesqrt(abs(%s)));\n",
2773 src_param.param_str);
2777 /** Process signed comparison opcodes in GLSL. */
2778 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
2780 struct glsl_src_param src0_param;
2781 struct glsl_src_param src1_param;
2782 DWORD write_mask;
2783 unsigned int mask_size;
2785 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2786 mask_size = shader_glsl_get_write_mask_size(write_mask);
2787 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2788 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2790 if (mask_size > 1) {
2791 const char *compare;
2793 switch(ins->handler_idx)
2795 case WINED3DSIH_SLT: compare = "lessThan"; break;
2796 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
2797 default: compare = "";
2798 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2801 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
2802 src0_param.param_str, src1_param.param_str);
2803 } else {
2804 switch(ins->handler_idx)
2806 case WINED3DSIH_SLT:
2807 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2808 * to return 0.0 but step returns 1.0 because step is not < x
2809 * An alternative is a bvec compare padded with an unused second component.
2810 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2811 * issue. Playing with not() is not possible either because not() does not accept
2812 * a scalar.
2814 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
2815 src0_param.param_str, src1_param.param_str);
2816 break;
2817 case WINED3DSIH_SGE:
2818 /* Here we can use the step() function and safe a conditional */
2819 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
2820 break;
2821 default:
2822 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2828 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
2830 const char *condition_prefix, *condition_suffix;
2831 struct wined3d_shader_dst_param dst;
2832 struct glsl_src_param src0_param;
2833 struct glsl_src_param src1_param;
2834 struct glsl_src_param src2_param;
2835 BOOL temp_destination = FALSE;
2836 DWORD cmp_channel = 0;
2837 unsigned int i, j;
2838 char mask_char[6];
2839 DWORD write_mask;
2841 switch (ins->handler_idx)
2843 case WINED3DSIH_CMP:
2844 condition_prefix = "";
2845 condition_suffix = " >= 0.0";
2846 break;
2848 case WINED3DSIH_CND:
2849 condition_prefix = "";
2850 condition_suffix = " > 0.5";
2851 break;
2853 case WINED3DSIH_MOVC:
2854 condition_prefix = "bool(";
2855 condition_suffix = ")";
2856 break;
2858 default:
2859 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2860 condition_prefix = "<unhandled prefix>";
2861 condition_suffix = "<unhandled suffix>";
2862 break;
2865 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
2867 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2868 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2869 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2870 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2872 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2873 condition_prefix, src0_param.param_str, condition_suffix,
2874 src1_param.param_str, src2_param.param_str);
2875 return;
2878 dst = ins->dst[0];
2880 /* Splitting the instruction up in multiple lines imposes a problem:
2881 * The first lines may overwrite source parameters of the following lines.
2882 * Deal with that by using a temporary destination register if needed. */
2883 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
2884 && ins->src[0].reg.type == dst.reg.type)
2885 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
2886 && ins->src[1].reg.type == dst.reg.type)
2887 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
2888 && ins->src[2].reg.type == dst.reg.type))
2889 temp_destination = TRUE;
2891 /* Cycle through all source0 channels. */
2892 for (i = 0; i < 4; ++i)
2894 write_mask = 0;
2895 /* Find the destination channels which use the current source0 channel. */
2896 for (j = 0; j < 4; ++j)
2898 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2900 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2901 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2904 dst.write_mask = ins->dst[0].write_mask & write_mask;
2906 if (temp_destination)
2908 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
2909 continue;
2910 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
2912 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst)))
2913 continue;
2915 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2916 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2917 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2919 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2920 condition_prefix, src0_param.param_str, condition_suffix,
2921 src1_param.param_str, src2_param.param_str);
2924 if (temp_destination)
2926 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
2927 shader_glsl_append_dst(ins->ctx->buffer, ins);
2928 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
2932 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2933 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2934 * the compare is done per component of src0. */
2935 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
2937 struct glsl_src_param src0_param;
2938 struct glsl_src_param src1_param;
2939 struct glsl_src_param src2_param;
2940 DWORD write_mask;
2941 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2942 ins->ctx->reg_maps->shader_version.minor);
2944 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
2946 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2947 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2948 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2949 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2951 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
2952 if (ins->coissue)
2954 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
2955 } else {
2956 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2957 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2959 return;
2962 shader_glsl_conditional_move(ins);
2965 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2966 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
2968 struct glsl_src_param src0_param;
2969 struct glsl_src_param src1_param;
2970 struct glsl_src_param src2_param;
2971 DWORD write_mask;
2973 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2974 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2975 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2976 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2977 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
2978 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2981 /* Handles transforming all WINED3DSIO_M?x? opcodes for
2982 Vertex shaders to GLSL codes */
2983 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
2985 int i;
2986 int nComponents = 0;
2987 struct wined3d_shader_dst_param tmp_dst = {{0}};
2988 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
2989 struct wined3d_shader_instruction tmp_ins;
2991 memset(&tmp_ins, 0, sizeof(tmp_ins));
2993 /* Set constants for the temporary argument */
2994 tmp_ins.ctx = ins->ctx;
2995 tmp_ins.dst_count = 1;
2996 tmp_ins.dst = &tmp_dst;
2997 tmp_ins.src_count = 2;
2998 tmp_ins.src = tmp_src;
3000 switch(ins->handler_idx)
3002 case WINED3DSIH_M4x4:
3003 nComponents = 4;
3004 tmp_ins.handler_idx = WINED3DSIH_DP4;
3005 break;
3006 case WINED3DSIH_M4x3:
3007 nComponents = 3;
3008 tmp_ins.handler_idx = WINED3DSIH_DP4;
3009 break;
3010 case WINED3DSIH_M3x4:
3011 nComponents = 4;
3012 tmp_ins.handler_idx = WINED3DSIH_DP3;
3013 break;
3014 case WINED3DSIH_M3x3:
3015 nComponents = 3;
3016 tmp_ins.handler_idx = WINED3DSIH_DP3;
3017 break;
3018 case WINED3DSIH_M3x2:
3019 nComponents = 2;
3020 tmp_ins.handler_idx = WINED3DSIH_DP3;
3021 break;
3022 default:
3023 break;
3026 tmp_dst = ins->dst[0];
3027 tmp_src[0] = ins->src[0];
3028 tmp_src[1] = ins->src[1];
3029 for (i = 0; i < nComponents; ++i)
3031 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3032 shader_glsl_dot(&tmp_ins);
3033 ++tmp_src[1].reg.idx[0].offset;
3038 The LRP instruction performs a component-wise linear interpolation
3039 between the second and third operands using the first operand as the
3040 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3041 This is equivalent to mix(src2, src1, src0);
3043 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3045 struct glsl_src_param src0_param;
3046 struct glsl_src_param src1_param;
3047 struct glsl_src_param src2_param;
3048 DWORD write_mask;
3050 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3052 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3053 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3054 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3056 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3057 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3060 /** Process the WINED3DSIO_LIT instruction in GLSL:
3061 * dst.x = dst.w = 1.0
3062 * dst.y = (src0.x > 0) ? src0.x
3063 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3064 * where src.w is clamped at +- 128
3066 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3068 struct glsl_src_param src0_param;
3069 struct glsl_src_param src1_param;
3070 struct glsl_src_param src3_param;
3071 char dst_mask[6];
3073 shader_glsl_append_dst(ins->ctx->buffer, ins);
3074 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3076 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3077 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3078 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3080 /* The sdk specifies the instruction like this
3081 * dst.x = 1.0;
3082 * if(src.x > 0.0) dst.y = src.x
3083 * else dst.y = 0.0.
3084 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3085 * else dst.z = 0.0;
3086 * dst.w = 1.0;
3087 * (where power = src.w clamped between -128 and 128)
3089 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3090 * dst.x = 1.0 ... No further explanation needed
3091 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3092 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3093 * dst.w = 1.0. ... Nothing fancy.
3095 * So we still have one conditional in there. So do this:
3096 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3098 * 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),
3099 * 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.
3100 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3102 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3103 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3104 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3106 shader_addline(ins->ctx->buffer,
3107 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3108 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3109 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3110 src0_param.param_str, src3_param.param_str, dst_mask);
3113 /** Process the WINED3DSIO_DST instruction in GLSL:
3114 * dst.x = 1.0
3115 * dst.y = src0.x * src0.y
3116 * dst.z = src0.z
3117 * dst.w = src1.w
3119 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3121 struct glsl_src_param src0y_param;
3122 struct glsl_src_param src0z_param;
3123 struct glsl_src_param src1y_param;
3124 struct glsl_src_param src1w_param;
3125 char dst_mask[6];
3127 shader_glsl_append_dst(ins->ctx->buffer, ins);
3128 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3130 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3131 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3132 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3133 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3135 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3136 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3139 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3140 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3141 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3143 * dst.x = cos(src0.?)
3144 * dst.y = sin(src0.?)
3145 * dst.z = dst.z
3146 * dst.w = dst.w
3148 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3150 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3151 struct glsl_src_param src0_param;
3152 DWORD write_mask;
3154 if (ins->ctx->reg_maps->shader_version.major < 4)
3156 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3158 write_mask = shader_glsl_append_dst(buffer, ins);
3159 switch (write_mask)
3161 case WINED3DSP_WRITEMASK_0:
3162 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3163 break;
3165 case WINED3DSP_WRITEMASK_1:
3166 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3167 break;
3169 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3170 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3171 src0_param.param_str, src0_param.param_str);
3172 break;
3174 default:
3175 ERR("Write mask should be .x, .y or .xy\n");
3176 break;
3179 return;
3182 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3185 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3187 char dst_mask[6];
3189 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3190 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3191 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3193 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
3194 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3195 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3197 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
3198 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3200 else
3202 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
3203 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3204 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3207 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3209 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
3210 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3211 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3215 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3216 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3217 * generate invalid code
3219 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3221 struct glsl_src_param src0_param;
3222 DWORD write_mask;
3224 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3225 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3227 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3230 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3231 * Start a for() loop where src1.y is the initial value of aL,
3232 * increment aL by src1.z for a total of src1.x iterations.
3233 * Need to use a temporary variable for this operation.
3235 /* FIXME: I don't think nested loops will work correctly this way. */
3236 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3238 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3239 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3240 const struct wined3d_shader *shader = ins->ctx->shader;
3241 const struct wined3d_shader_lconst *constant;
3242 struct glsl_src_param src1_param;
3243 const DWORD *control_values = NULL;
3245 if (ins->ctx->reg_maps->shader_version.major < 4)
3247 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3249 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3250 * class hardware doesn't support real varying indexing, but Microsoft
3251 * designed this feature for Shader model 2.x+. If the loop control is
3252 * known at compile time, the GLSL compiler can unroll the loop, and
3253 * replace indirect addressing with direct addressing. */
3254 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3256 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3258 if (constant->idx == ins->src[1].reg.idx[0].offset)
3260 control_values = constant->value;
3261 break;
3266 if (control_values)
3268 struct wined3d_shader_loop_control loop_control;
3269 loop_control.count = control_values[0];
3270 loop_control.start = control_values[1];
3271 loop_control.step = (int)control_values[2];
3273 if (loop_control.step > 0)
3275 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3276 loop_state->current_depth, loop_control.start,
3277 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3278 loop_state->current_depth, loop_control.step);
3280 else if (loop_control.step < 0)
3282 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3283 loop_state->current_depth, loop_control.start,
3284 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3285 loop_state->current_depth, loop_control.step);
3287 else
3289 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3290 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3291 loop_state->current_depth, loop_control.count,
3292 loop_state->current_depth);
3295 else
3297 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3298 loop_state->current_depth, loop_state->current_reg,
3299 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3300 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3303 ++loop_state->current_reg;
3305 else
3307 shader_addline(buffer, "for (;;)\n{\n");
3310 ++loop_state->current_depth;
3313 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3315 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3317 shader_addline(ins->ctx->buffer, "}\n");
3319 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3321 --loop_state->current_depth;
3322 --loop_state->current_reg;
3325 if (ins->handler_idx == WINED3DSIH_ENDREP)
3327 --loop_state->current_depth;
3331 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3333 const struct wined3d_shader *shader = ins->ctx->shader;
3334 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3335 const struct wined3d_shader_lconst *constant;
3336 struct glsl_src_param src0_param;
3337 const DWORD *control_values = NULL;
3339 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3340 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3342 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3344 if (constant->idx == ins->src[0].reg.idx[0].offset)
3346 control_values = constant->value;
3347 break;
3352 if (control_values)
3354 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3355 loop_state->current_depth, loop_state->current_depth,
3356 control_values[0], loop_state->current_depth);
3358 else
3360 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3361 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3362 loop_state->current_depth, loop_state->current_depth,
3363 src0_param.param_str, loop_state->current_depth);
3366 ++loop_state->current_depth;
3369 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3371 struct glsl_src_param src0_param;
3373 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3374 shader_addline(ins->ctx->buffer, "if (%s) {\n", src0_param.param_str);
3377 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3379 struct glsl_src_param src0_param;
3380 struct glsl_src_param src1_param;
3382 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3383 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3385 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3386 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3389 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3391 shader_addline(ins->ctx->buffer, "} else {\n");
3394 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3396 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3399 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3401 shader_addline(ins->ctx->buffer, "break;\n");
3404 /* FIXME: According to MSDN the compare is done per component. */
3405 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3407 struct glsl_src_param src0_param;
3408 struct glsl_src_param src1_param;
3410 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3411 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3413 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3414 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3417 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
3419 struct glsl_src_param src_param;
3421 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
3422 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
3425 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
3427 shader_addline(ins->ctx->buffer, "}\n");
3428 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
3431 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
3433 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
3436 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3438 struct glsl_src_param src1_param;
3440 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3441 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
3442 src1_param.param_str, ins->src[0].reg.idx[0].offset);
3445 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3447 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3448 * function only suppresses the unhandled instruction warning
3452 /*********************************************
3453 * Pixel Shader Specific Code begins here
3454 ********************************************/
3455 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3457 const struct wined3d_shader *shader = ins->ctx->shader;
3458 struct wined3d_device *device = shader->device;
3459 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3460 ins->ctx->reg_maps->shader_version.minor);
3461 struct glsl_sample_function sample_function;
3462 const struct wined3d_texture *texture;
3463 DWORD sample_flags = 0;
3464 DWORD sampler_idx;
3465 DWORD mask = 0, swizzle;
3467 /* 1.0-1.4: Use destination register as sampler source.
3468 * 2.0+: Use provided sampler source. */
3469 if (shader_version < WINED3D_SHADER_VERSION(2,0))
3470 sampler_idx = ins->dst[0].reg.idx[0].offset;
3471 else
3472 sampler_idx = ins->src[1].reg.idx[0].offset;
3473 texture = device->stateBlock->state.textures[sampler_idx];
3475 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3477 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3478 DWORD flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3479 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3480 enum wined3d_sampler_texture_type sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3482 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3483 if (flags & WINED3D_PSARGS_PROJECTED && sampler_type != WINED3DSTT_CUBE)
3485 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3486 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3488 case WINED3D_TTFF_COUNT1:
3489 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3490 break;
3491 case WINED3D_TTFF_COUNT2:
3492 mask = WINED3DSP_WRITEMASK_1;
3493 break;
3494 case WINED3D_TTFF_COUNT3:
3495 mask = WINED3DSP_WRITEMASK_2;
3496 break;
3497 case WINED3D_TTFF_COUNT4:
3498 case WINED3D_TTFF_DISABLE:
3499 mask = WINED3DSP_WRITEMASK_3;
3500 break;
3504 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3506 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3508 if (src_mod == WINED3DSPSM_DZ) {
3509 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3510 mask = WINED3DSP_WRITEMASK_2;
3511 } else if (src_mod == WINED3DSPSM_DW) {
3512 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3513 mask = WINED3DSP_WRITEMASK_3;
3515 } else {
3516 if (ins->flags & WINED3DSI_TEXLD_PROJECT)
3518 /* ps 2.0 texldp instruction always divides by the fourth component. */
3519 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3520 mask = WINED3DSP_WRITEMASK_3;
3524 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
3525 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3527 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3528 mask |= sample_function.coord_mask;
3530 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3531 else swizzle = ins->src[1].swizzle;
3533 /* 1.0-1.3: Use destination register as coordinate source.
3534 1.4+: Use provided coordinate source register. */
3535 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3537 char coord_mask[6];
3538 shader_glsl_write_mask_to_str(mask, coord_mask);
3539 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3540 "T%u%s", sampler_idx, coord_mask);
3542 else
3544 struct glsl_src_param coord_param;
3545 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3546 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3548 struct glsl_src_param bias;
3549 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3550 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3551 "%s", coord_param.param_str);
3552 } else {
3553 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3554 "%s", coord_param.param_str);
3559 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3561 const struct wined3d_shader *shader = ins->ctx->shader;
3562 struct wined3d_device *device = shader->device;
3563 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3564 struct glsl_src_param coord_param, dx_param, dy_param;
3565 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3566 struct glsl_sample_function sample_function;
3567 DWORD sampler_idx;
3568 DWORD swizzle = ins->src[1].swizzle;
3569 const struct wined3d_texture *texture;
3571 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3573 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3574 shader_glsl_tex(ins);
3575 return;
3578 sampler_idx = ins->src[1].reg.idx[0].offset;
3579 texture = device->stateBlock->state.textures[sampler_idx];
3580 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
3581 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3583 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3584 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3585 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3586 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3588 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3589 "%s", coord_param.param_str);
3592 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3594 const struct wined3d_shader *shader = ins->ctx->shader;
3595 struct wined3d_device *device = shader->device;
3596 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3597 struct glsl_src_param coord_param, lod_param;
3598 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3599 struct glsl_sample_function sample_function;
3600 DWORD sampler_idx;
3601 DWORD swizzle = ins->src[1].swizzle;
3602 const struct wined3d_texture *texture;
3604 sampler_idx = ins->src[1].reg.idx[0].offset;
3605 texture = device->stateBlock->state.textures[sampler_idx];
3606 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
3607 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3609 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3610 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3612 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3614 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3615 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
3617 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
3618 * However, the NVIDIA drivers allow them in fragment shaders as well,
3619 * even without the appropriate extension. */
3620 WARN("Using %s in fragment shader.\n", sample_function.name);
3622 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3623 "%s", coord_param.param_str);
3626 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3628 /* FIXME: Make this work for more than just 2D textures */
3629 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3630 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3632 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3634 char dst_mask[6];
3636 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3637 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3638 ins->dst[0].reg.idx[0].offset, dst_mask);
3640 else
3642 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3643 DWORD reg = ins->src[0].reg.idx[0].offset;
3644 char dst_swizzle[6];
3646 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3648 if (src_mod == WINED3DSPSM_DZ)
3650 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3651 struct glsl_src_param div_param;
3653 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3655 if (mask_size > 1) {
3656 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3657 } else {
3658 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3661 else if (src_mod == WINED3DSPSM_DW)
3663 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3664 struct glsl_src_param div_param;
3666 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3668 if (mask_size > 1) {
3669 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3670 } else {
3671 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3673 } else {
3674 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3679 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3680 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3681 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3682 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3684 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3685 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3686 struct glsl_sample_function sample_function;
3687 struct glsl_src_param src0_param;
3688 UINT mask_size;
3690 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3692 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3693 * scalar, and projected sampling would require 4.
3695 * It is a dependent read - not valid with conditional NP2 textures
3697 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3698 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3700 switch(mask_size)
3702 case 1:
3703 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3704 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3705 break;
3707 case 2:
3708 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3709 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3710 break;
3712 case 3:
3713 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3714 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3715 break;
3717 default:
3718 FIXME("Unexpected mask size %u\n", mask_size);
3719 break;
3723 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3724 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3725 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3727 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3728 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3729 struct glsl_src_param src0_param;
3730 DWORD dst_mask;
3731 unsigned int mask_size;
3733 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3734 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3735 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3737 if (mask_size > 1) {
3738 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3739 } else {
3740 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3744 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3745 * Calculate the depth as dst.x / dst.y */
3746 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3748 struct glsl_dst_param dst_param;
3750 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3752 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3753 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3754 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3755 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3756 * >= 1.0 or < 0.0
3758 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3759 dst_param.reg_name, dst_param.reg_name);
3762 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3763 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3764 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3765 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3767 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3769 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3770 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3771 struct glsl_src_param src0_param;
3773 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3775 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3776 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3779 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3780 * Calculate the 1st of a 2-row matrix multiplication. */
3781 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3783 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3784 DWORD reg = ins->dst[0].reg.idx[0].offset;
3785 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3786 struct glsl_src_param src0_param;
3788 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3789 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3792 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3793 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3794 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3796 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3797 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3798 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3799 DWORD reg = ins->dst[0].reg.idx[0].offset;
3800 struct glsl_src_param src0_param;
3802 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3803 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
3804 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
3807 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3809 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3810 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3811 struct glsl_sample_function sample_function;
3812 DWORD reg = ins->dst[0].reg.idx[0].offset;
3813 struct glsl_src_param src0_param;
3815 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3816 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3818 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3820 /* Sample the texture using the calculated coordinates */
3821 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
3824 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3825 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3826 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3828 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3829 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3830 struct glsl_sample_function sample_function;
3831 DWORD reg = ins->dst[0].reg.idx[0].offset;
3832 struct glsl_src_param src0_param;
3834 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3835 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3837 /* Dependent read, not valid with conditional NP2 */
3838 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3840 /* Sample the texture using the calculated coordinates */
3841 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3843 tex_mx->current_row = 0;
3846 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3847 * Perform the 3rd row of a 3x3 matrix multiply */
3848 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
3850 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3851 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3852 DWORD reg = ins->dst[0].reg.idx[0].offset;
3853 struct glsl_src_param src0_param;
3854 char dst_mask[6];
3856 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3858 shader_glsl_append_dst(ins->ctx->buffer, ins);
3859 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3860 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
3862 tex_mx->current_row = 0;
3865 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3866 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3867 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
3869 struct glsl_src_param src0_param;
3870 struct glsl_src_param src1_param;
3871 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3872 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3873 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3874 struct glsl_sample_function sample_function;
3875 DWORD reg = ins->dst[0].reg.idx[0].offset;
3876 char coord_mask[6];
3878 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3879 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3881 /* Perform the last matrix multiply operation */
3882 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3883 /* Reflection calculation */
3884 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
3886 /* Dependent read, not valid with conditional NP2 */
3887 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3888 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3890 /* Sample the texture */
3891 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3892 NULL, NULL, NULL, "tmp0%s", coord_mask);
3894 tex_mx->current_row = 0;
3897 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3898 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3899 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
3901 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3902 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3903 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3904 struct glsl_sample_function sample_function;
3905 DWORD reg = ins->dst[0].reg.idx[0].offset;
3906 struct glsl_src_param src0_param;
3907 char coord_mask[6];
3909 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3911 /* Perform the last matrix multiply operation */
3912 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
3914 /* Construct the eye-ray vector from w coordinates */
3915 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3916 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
3917 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3919 /* Dependent read, not valid with conditional NP2 */
3920 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3921 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3923 /* Sample the texture using the calculated coordinates */
3924 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3925 NULL, NULL, NULL, "tmp0%s", coord_mask);
3927 tex_mx->current_row = 0;
3930 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3931 * Apply a fake bump map transform.
3932 * texbem is pshader <= 1.3 only, this saves a few version checks
3934 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
3936 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3937 struct glsl_sample_function sample_function;
3938 struct glsl_src_param coord_param;
3939 DWORD sampler_idx;
3940 DWORD mask;
3941 DWORD flags;
3942 char coord_mask[6];
3944 sampler_idx = ins->dst[0].reg.idx[0].offset;
3945 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3946 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3948 /* Dependent read, not valid with conditional NP2 */
3949 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3950 mask = sample_function.coord_mask;
3952 shader_glsl_write_mask_to_str(mask, coord_mask);
3954 /* With projected textures, texbem only divides the static texture coord,
3955 * not the displacement, so we can't let GL handle this. */
3956 if (flags & WINED3D_PSARGS_PROJECTED)
3958 DWORD div_mask=0;
3959 char coord_div_mask[3];
3960 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3962 case WINED3D_TTFF_COUNT1:
3963 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3964 break;
3965 case WINED3D_TTFF_COUNT2:
3966 div_mask = WINED3DSP_WRITEMASK_1;
3967 break;
3968 case WINED3D_TTFF_COUNT3:
3969 div_mask = WINED3DSP_WRITEMASK_2;
3970 break;
3971 case WINED3D_TTFF_COUNT4:
3972 case WINED3D_TTFF_DISABLE:
3973 div_mask = WINED3DSP_WRITEMASK_3;
3974 break;
3976 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
3977 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
3980 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
3982 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3983 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
3984 coord_param.param_str, coord_mask);
3986 if (ins->handler_idx == WINED3DSIH_TEXBEML)
3988 struct glsl_src_param luminance_param;
3989 struct glsl_dst_param dst_param;
3991 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
3992 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3994 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
3995 dst_param.reg_name, dst_param.mask_str,
3996 luminance_param.param_str, sampler_idx, sampler_idx);
4000 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4002 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4003 struct glsl_src_param src0_param, src1_param;
4005 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4006 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4008 shader_glsl_append_dst(ins->ctx->buffer, ins);
4009 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4010 src0_param.param_str, sampler_idx, src1_param.param_str);
4013 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4014 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4015 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4017 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4018 struct glsl_sample_function sample_function;
4019 struct glsl_src_param src0_param;
4021 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4023 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4024 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4025 "%s.wx", src0_param.reg_name);
4028 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4029 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4030 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4032 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4033 struct glsl_sample_function sample_function;
4034 struct glsl_src_param src0_param;
4036 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4038 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4039 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4040 "%s.yz", src0_param.reg_name);
4043 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4044 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4045 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4047 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4048 struct glsl_sample_function sample_function;
4049 struct glsl_src_param src0_param;
4051 /* Dependent read, not valid with conditional NP2 */
4052 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4053 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4055 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4056 "%s", src0_param.param_str);
4059 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4060 * If any of the first 3 components are < 0, discard this pixel */
4061 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4063 struct glsl_dst_param dst_param;
4065 /* The argument is a destination parameter, and no writemasks are allowed */
4066 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4067 if (ins->ctx->reg_maps->shader_version.major >= 2)
4069 /* 2.0 shaders compare all 4 components in texkill */
4070 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4071 } else {
4072 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
4073 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
4074 * 4 components are defined, only the first 3 are used
4076 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4080 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4081 * dst = dot2(src0, src1) + src2 */
4082 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4084 struct glsl_src_param src0_param;
4085 struct glsl_src_param src1_param;
4086 struct glsl_src_param src2_param;
4087 DWORD write_mask;
4088 unsigned int mask_size;
4090 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4091 mask_size = shader_glsl_get_write_mask_size(write_mask);
4093 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4094 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4095 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4097 if (mask_size > 1) {
4098 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4099 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4100 } else {
4101 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4102 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4106 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
4107 const struct wined3d_shader_signature_element *input_signature,
4108 const struct wined3d_shader_reg_maps *reg_maps,
4109 enum vertexprocessing_mode vertexprocessing)
4111 WORD map = reg_maps->input_registers;
4112 unsigned int i;
4114 for (i = 0; map; map >>= 1, ++i)
4116 const char *semantic_name;
4117 UINT semantic_idx;
4118 char reg_mask[6];
4120 /* Unused */
4121 if (!(map & 1)) continue;
4123 semantic_name = input_signature[i].semantic_name;
4124 semantic_idx = input_signature[i].semantic_idx;
4125 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
4127 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4129 if (semantic_idx < 8 && vertexprocessing == pretransformed)
4130 shader_addline(buffer, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4131 shader->u.ps.input_reg_map[i], reg_mask, semantic_idx, reg_mask);
4132 else
4133 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4134 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4136 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4138 if (!semantic_idx)
4139 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4140 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4141 else if (semantic_idx == 1)
4142 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4143 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4144 else
4145 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4146 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4148 else
4150 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4151 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4156 /*********************************************
4157 * Vertex Shader Specific Code begins here
4158 ********************************************/
4160 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4162 struct glsl_program_key key;
4164 key.vs_id = entry->vs.id;
4165 key.gs_id = entry->gs.id;
4166 key.ps_id = entry->ps.id;
4168 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4170 ERR("Failed to insert program entry.\n");
4174 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4175 GLhandleARB vs_id, GLhandleARB gs_id, GLhandleARB ps_id)
4177 struct wine_rb_entry *entry;
4178 struct glsl_program_key key;
4180 key.vs_id = vs_id;
4181 key.gs_id = gs_id;
4182 key.ps_id = ps_id;
4184 entry = wine_rb_get(&priv->program_lookup, &key);
4185 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4188 /* Context activation is done by the caller. */
4189 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4190 struct glsl_shader_prog_link *entry)
4192 struct glsl_program_key key;
4194 key.vs_id = entry->vs.id;
4195 key.gs_id = entry->gs.id;
4196 key.ps_id = entry->ps.id;
4197 wine_rb_remove(&priv->program_lookup, &key);
4199 GL_EXTCALL(glDeleteObjectARB(entry->programId));
4200 if (entry->vs.id)
4201 list_remove(&entry->vs.shader_entry);
4202 if (entry->gs.id)
4203 list_remove(&entry->gs.shader_entry);
4204 if (entry->ps.id)
4205 list_remove(&entry->ps.shader_entry);
4206 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4207 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4208 HeapFree(GetProcessHeap(), 0, entry);
4211 static void handle_ps3_input(struct wined3d_shader_buffer *buffer,
4212 const struct wined3d_gl_info *gl_info, const DWORD *map,
4213 const struct wined3d_shader_signature_element *input_signature,
4214 const struct wined3d_shader_reg_maps *reg_maps_in,
4215 const struct wined3d_shader_signature_element *output_signature,
4216 const struct wined3d_shader_reg_maps *reg_maps_out)
4218 unsigned int i, j;
4219 const char *semantic_name_in;
4220 UINT semantic_idx_in;
4221 DWORD *set;
4222 DWORD in_idx;
4223 unsigned int in_count = vec4_varyings(3, gl_info);
4224 char reg_mask[6];
4225 char destination[50];
4226 WORD input_map, output_map;
4228 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
4230 input_map = reg_maps_in->input_registers;
4231 for (i = 0; input_map; input_map >>= 1, ++i)
4233 if (!(input_map & 1)) continue;
4235 in_idx = map[i];
4236 /* Declared, but not read register */
4237 if (in_idx == ~0U) continue;
4238 if (in_idx >= (in_count + 2))
4240 FIXME("More input varyings declared than supported, expect issues.\n");
4241 continue;
4244 if (in_idx == in_count)
4245 sprintf(destination, "gl_FrontColor");
4246 else if (in_idx == in_count + 1)
4247 sprintf(destination, "gl_FrontSecondaryColor");
4248 else
4249 sprintf(destination, "ps_in[%u]", in_idx);
4251 semantic_name_in = input_signature[i].semantic_name;
4252 semantic_idx_in = input_signature[i].semantic_idx;
4253 set[in_idx] = ~0U;
4255 output_map = reg_maps_out->output_registers;
4256 for (j = 0; output_map; output_map >>= 1, ++j)
4258 DWORD mask;
4260 if (!(output_map & 1)
4261 || semantic_idx_in != output_signature[j].semantic_idx
4262 || strcmp(semantic_name_in, output_signature[j].semantic_name)
4263 || !(mask = input_signature[i].mask & output_signature[j].mask))
4264 continue;
4266 set[in_idx] = mask;
4267 shader_glsl_write_mask_to_str(mask, reg_mask);
4269 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4270 destination, reg_mask, j, reg_mask);
4274 for (i = 0; i < in_count + 2; ++i)
4276 unsigned int size;
4278 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4279 continue;
4281 if (set[i] == ~0U) set[i] = 0;
4283 size = 0;
4284 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4285 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4286 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4287 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4288 reg_mask[size] = '\0';
4290 if (i == in_count)
4291 sprintf(destination, "gl_FrontColor");
4292 else if (i == in_count + 1)
4293 sprintf(destination, "gl_FrontSecondaryColor");
4294 else
4295 sprintf(destination, "ps_in[%u]", i);
4297 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
4298 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
4301 HeapFree(GetProcessHeap(), 0, set);
4304 /* Context activation is done by the caller. */
4305 static GLhandleARB generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
4306 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4307 const struct wined3d_gl_info *gl_info)
4309 GLhandleARB ret = 0;
4310 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4311 unsigned int i;
4312 const char *semantic_name;
4313 UINT semantic_idx;
4314 char reg_mask[6];
4315 const struct wined3d_shader_signature_element *output_signature = vs->output_signature;
4316 WORD map = vs->reg_maps.output_registers;
4318 shader_buffer_clear(buffer);
4320 shader_addline(buffer, "#version 120\n");
4322 if (ps_major < 3)
4324 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits.packed_output);
4326 for (i = 0; map; map >>= 1, ++i)
4328 DWORD write_mask;
4330 if (!(map & 1)) continue;
4332 semantic_name = output_signature[i].semantic_name;
4333 semantic_idx = output_signature[i].semantic_idx;
4334 write_mask = output_signature[i].mask;
4335 shader_glsl_write_mask_to_str(write_mask, reg_mask);
4337 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4339 if (!semantic_idx)
4340 shader_addline(buffer, "gl_FrontColor%s = vs_out[%u]%s;\n",
4341 reg_mask, i, reg_mask);
4342 else if (semantic_idx == 1)
4343 shader_addline(buffer, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4344 reg_mask, i, reg_mask);
4346 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4348 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4349 reg_mask, i, reg_mask);
4351 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4353 if (semantic_idx < 8)
4355 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
4356 write_mask |= WINED3DSP_WRITEMASK_3;
4358 shader_addline(buffer, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4359 semantic_idx, reg_mask, i, reg_mask);
4360 if (!(write_mask & WINED3DSP_WRITEMASK_3))
4361 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
4364 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4366 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4368 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
4370 shader_addline(buffer, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n", i, reg_mask[1]);
4373 shader_addline(buffer, "}\n");
4375 else
4377 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits.packed_input);
4378 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4379 shader_addline(buffer, "varying vec4 ps_in[%u];\n", in_count);
4380 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits.packed_output);
4382 /* First, sort out position and point size. Those are not passed to the pixel shader */
4383 for (i = 0; map; map >>= 1, ++i)
4385 if (!(map & 1)) continue;
4387 semantic_name = output_signature[i].semantic_name;
4388 shader_glsl_write_mask_to_str(output_signature[i].mask, reg_mask);
4390 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4392 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4393 reg_mask, i, reg_mask);
4395 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4397 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4401 /* Then, fix the pixel shader input */
4402 handle_ps3_input(buffer, gl_info, ps->u.ps.input_reg_map, ps->input_signature,
4403 &ps->reg_maps, output_signature, &vs->reg_maps);
4405 shader_addline(buffer, "}\n");
4408 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4409 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
4410 shader_glsl_compile(gl_info, ret, buffer->buffer);
4412 return ret;
4415 static void shader_glsl_generate_srgb_write_correction(struct wined3d_shader_buffer *buffer)
4417 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4418 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4419 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4420 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4421 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4422 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4425 static void shader_glsl_generate_fog_code(struct wined3d_shader_buffer *buffer, enum fogmode mode)
4427 switch (mode)
4429 case FOG_OFF:
4430 return;
4432 case FOG_LINEAR:
4433 /* Fog = (gl_Fog.end - gl_FogFragCoord) / (gl_Fog.end - gl_Fog.start) */
4434 shader_addline(buffer, "float Fog = (gl_Fog.end - gl_FogFragCoord) / (gl_Fog.end - gl_Fog.start);\n");
4435 break;
4437 case FOG_EXP:
4438 /* Fog = e^-(gl_Fog.density * gl_FogFragCoord) */
4439 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4440 break;
4442 case FOG_EXP2:
4443 /* Fog = e^-((gl_Fog.density * gl_FogFragCoord)^2) */
4444 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4445 break;
4447 default:
4448 ERR("Invalid fog mode %#x.\n", mode);
4449 return;
4452 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, clamp(Fog, 0.0, 1.0));\n");
4455 /* Context activation is done by the caller. */
4456 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
4457 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4458 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
4460 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4461 const struct wined3d_gl_info *gl_info = context->gl_info;
4462 const DWORD *function = shader->function;
4463 struct shader_glsl_ctx_priv priv_ctx;
4465 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4466 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4468 memset(&priv_ctx, 0, sizeof(priv_ctx));
4469 priv_ctx.cur_ps_args = args;
4470 priv_ctx.cur_np2fixup_info = np2fixup_info;
4472 shader_addline(buffer, "#version 120\n");
4474 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4475 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4476 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4477 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
4478 /* The spec says that it doesn't have to be explicitly enabled, but the
4479 * nvidia drivers write a warning if we don't do so. */
4480 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4481 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4482 if (gl_info->supported[EXT_GPU_SHADER4])
4483 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4485 /* Base Declarations */
4486 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4488 /* Pack 3.0 inputs */
4489 if (reg_maps->shader_version.major >= 3 && args->vp_mode != vertexshader)
4490 shader_glsl_input_pack(shader, buffer, shader->input_signature, reg_maps, args->vp_mode);
4492 /* Base Shader Body */
4493 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4495 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4496 if (reg_maps->shader_version.major < 2)
4498 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4499 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4502 if (args->srgb_correction)
4503 shader_glsl_generate_srgb_write_correction(buffer);
4505 /* SM < 3 does not replace the fog stage. */
4506 if (reg_maps->shader_version.major < 3)
4507 shader_glsl_generate_fog_code(buffer, args->fog);
4509 shader_addline(buffer, "}\n");
4511 TRACE("Compiling shader object %u\n", shader_obj);
4512 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4514 /* Store the shader object */
4515 return shader_obj;
4518 /* Context activation is done by the caller. */
4519 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4520 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4521 const struct vs_compile_args *args)
4523 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4524 const struct wined3d_gl_info *gl_info = context->gl_info;
4525 const DWORD *function = shader->function;
4526 struct shader_glsl_ctx_priv priv_ctx;
4528 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4529 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4531 shader_addline(buffer, "#version 120\n");
4533 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4534 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4535 if (gl_info->supported[EXT_GPU_SHADER4])
4536 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4538 memset(&priv_ctx, 0, sizeof(priv_ctx));
4539 priv_ctx.cur_vs_args = args;
4541 /* Base Declarations */
4542 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4544 /* Base Shader Body */
4545 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4547 /* Unpack outputs */
4548 shader_addline(buffer, "order_ps_input(vs_out);\n");
4550 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4551 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4552 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4553 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4555 if (args->fog_src == VS_FOG_Z)
4556 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4557 else if (!reg_maps->fog)
4558 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4560 /* We always store the clipplanes without y inversion */
4561 if (args->clip_enabled)
4562 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4564 /* Write the final position.
4566 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4567 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4568 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4569 * contains 1.0 to allow a mad.
4571 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4572 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4574 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4576 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4577 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4578 * which is the same as z = z * 2 - w.
4580 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4582 shader_addline(buffer, "}\n");
4584 TRACE("Compiling shader object %u\n", shader_obj);
4585 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4587 return shader_obj;
4590 /* Context activation is done by the caller. */
4591 static GLhandleARB shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
4592 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader)
4594 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4595 const struct wined3d_gl_info *gl_info = context->gl_info;
4596 const DWORD *function = shader->function;
4597 struct shader_glsl_ctx_priv priv_ctx;
4598 GLhandleARB shader_id;
4600 shader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_GEOMETRY_SHADER_ARB));
4602 shader_addline(buffer, "#version 120\n");
4604 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
4605 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
4606 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4607 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4608 if (gl_info->supported[EXT_GPU_SHADER4])
4609 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4611 memset(&priv_ctx, 0, sizeof(priv_ctx));
4612 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4613 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4614 shader_addline(buffer, "}\n");
4616 TRACE("Compiling shader object %u.\n", shader_id);
4617 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4619 return shader_id;
4622 static GLhandleARB find_glsl_pshader(const struct wined3d_context *context,
4623 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4624 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
4626 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
4627 struct glsl_shader_private *shader_data;
4628 struct ps_np2fixup_info *np2fixup;
4629 UINT i;
4630 DWORD new_size;
4631 GLhandleARB ret;
4633 if (!shader->backend_data)
4635 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4636 if (!shader->backend_data)
4638 ERR("Failed to allocate backend data.\n");
4639 return 0;
4642 shader_data = shader->backend_data;
4643 gl_shaders = shader_data->gl_shaders.ps;
4645 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4646 * so a linear search is more performant than a hashmap or a binary search
4647 * (cache coherency etc)
4649 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4651 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
4653 if (args->np2_fixup)
4654 *np2fixup_info = &gl_shaders[i].np2fixup;
4655 return gl_shaders[i].prgId;
4659 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4660 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4661 if (shader_data->num_gl_shaders)
4663 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4664 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
4665 new_size * sizeof(*gl_shaders));
4667 else
4669 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4670 new_size = 1;
4673 if(!new_array) {
4674 ERR("Out of memory\n");
4675 return 0;
4677 shader_data->gl_shaders.ps = new_array;
4678 shader_data->shader_array_size = new_size;
4679 gl_shaders = new_array;
4682 gl_shaders[shader_data->num_gl_shaders].args = *args;
4684 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
4685 memset(np2fixup, 0, sizeof(*np2fixup));
4686 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
4688 pixelshader_update_samplers(shader, args->tex_types);
4690 shader_buffer_clear(buffer);
4691 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4692 gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4694 return ret;
4697 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4698 const DWORD use_map) {
4699 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4700 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4701 return stored->fog_src == new->fog_src;
4704 static GLhandleARB find_glsl_vshader(const struct wined3d_context *context,
4705 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4706 const struct vs_compile_args *args)
4708 UINT i;
4709 DWORD new_size;
4710 DWORD use_map = shader->device->strided_streams.use_map;
4711 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
4712 struct glsl_shader_private *shader_data;
4713 GLhandleARB ret;
4715 if (!shader->backend_data)
4717 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4718 if (!shader->backend_data)
4720 ERR("Failed to allocate backend data.\n");
4721 return 0;
4724 shader_data = shader->backend_data;
4725 gl_shaders = shader_data->gl_shaders.vs;
4727 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4728 * so a linear search is more performant than a hashmap or a binary search
4729 * (cache coherency etc)
4731 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4733 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
4734 return gl_shaders[i].prgId;
4737 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4739 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4740 if (shader_data->num_gl_shaders)
4742 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4743 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
4744 new_size * sizeof(*gl_shaders));
4746 else
4748 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4749 new_size = 1;
4752 if(!new_array) {
4753 ERR("Out of memory\n");
4754 return 0;
4756 shader_data->gl_shaders.vs = new_array;
4757 shader_data->shader_array_size = new_size;
4758 gl_shaders = new_array;
4761 gl_shaders[shader_data->num_gl_shaders].args = *args;
4763 shader_buffer_clear(buffer);
4764 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4765 gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4767 return ret;
4770 static GLhandleARB find_glsl_geometry_shader(const struct wined3d_context *context,
4771 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader)
4773 struct glsl_gs_compiled_shader *gl_shaders;
4774 struct glsl_shader_private *shader_data;
4775 GLhandleARB ret;
4777 if (!shader->backend_data)
4779 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
4781 ERR("Failed to allocate backend data.\n");
4782 return 0;
4785 shader_data = shader->backend_data;
4786 gl_shaders = shader_data->gl_shaders.gs;
4788 if (shader_data->num_gl_shaders)
4789 return gl_shaders[0].id;
4791 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4793 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
4795 ERR("Failed to allocate GL shader array.\n");
4796 return 0;
4798 shader_data->shader_array_size = 1;
4799 gl_shaders = shader_data->gl_shaders.gs;
4801 shader_buffer_clear(buffer);
4802 ret = shader_glsl_generate_geometry_shader(context, buffer, shader);
4803 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4805 return ret;
4808 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_shader_buffer *buffer,
4809 DWORD argnum, unsigned int stage, DWORD arg)
4811 const char *ret;
4813 if (arg == ARG_UNUSED)
4814 return "<unused arg>";
4816 switch (arg & WINED3DTA_SELECTMASK)
4818 case WINED3DTA_DIFFUSE:
4819 ret = "gl_Color";
4820 break;
4822 case WINED3DTA_CURRENT:
4823 if (!stage)
4824 ret = "gl_Color";
4825 else
4826 ret = "ret";
4827 break;
4829 case WINED3DTA_TEXTURE:
4830 switch (stage)
4832 case 0: ret = "tex0"; break;
4833 case 1: ret = "tex1"; break;
4834 case 2: ret = "tex2"; break;
4835 case 3: ret = "tex3"; break;
4836 case 4: ret = "tex4"; break;
4837 case 5: ret = "tex5"; break;
4838 case 6: ret = "tex6"; break;
4839 case 7: ret = "tex7"; break;
4840 default:
4841 ret = "<invalid texture>";
4842 break;
4844 break;
4846 case WINED3DTA_TFACTOR:
4847 ret = "tex_factor";
4848 break;
4850 case WINED3DTA_SPECULAR:
4851 ret = "gl_SecondaryColor";
4852 break;
4854 case WINED3DTA_TEMP:
4855 ret = "temp_reg";
4856 break;
4858 case WINED3DTA_CONSTANT:
4859 FIXME("Per-stage constants not implemented.\n");
4860 switch (stage)
4862 case 0: ret = "const0"; break;
4863 case 1: ret = "const1"; break;
4864 case 2: ret = "const2"; break;
4865 case 3: ret = "const3"; break;
4866 case 4: ret = "const4"; break;
4867 case 5: ret = "const5"; break;
4868 case 6: ret = "const6"; break;
4869 case 7: ret = "const7"; break;
4870 default:
4871 ret = "<invalid constant>";
4872 break;
4874 break;
4876 default:
4877 return "<unhandled arg>";
4880 if (arg & WINED3DTA_COMPLEMENT)
4882 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
4883 if (argnum == 0)
4884 ret = "arg0";
4885 else if (argnum == 1)
4886 ret = "arg1";
4887 else if (argnum == 2)
4888 ret = "arg2";
4891 if (arg & WINED3DTA_ALPHAREPLICATE)
4893 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
4894 if (argnum == 0)
4895 ret = "arg0";
4896 else if (argnum == 1)
4897 ret = "arg1";
4898 else if (argnum == 2)
4899 ret = "arg2";
4902 return ret;
4905 static void shader_glsl_ffp_fragment_op(struct wined3d_shader_buffer *buffer, unsigned int stage, BOOL color,
4906 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
4908 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
4910 if (color && alpha)
4911 dstmask = "";
4912 else if (color)
4913 dstmask = ".xyz";
4914 else
4915 dstmask = ".w";
4917 if (dst == tempreg)
4918 dstreg = "temp_reg";
4919 else
4920 dstreg = "ret";
4922 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
4923 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
4924 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
4926 switch (op)
4928 case WINED3D_TOP_DISABLE:
4929 if (!stage)
4930 shader_addline(buffer, "%s%s = gl_Color%s;\n", dstreg, dstmask, dstmask);
4931 break;
4933 case WINED3D_TOP_SELECT_ARG1:
4934 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
4935 break;
4937 case WINED3D_TOP_SELECT_ARG2:
4938 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
4939 break;
4941 case WINED3D_TOP_MODULATE:
4942 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
4943 break;
4945 case WINED3D_TOP_MODULATE_4X:
4946 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
4947 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
4948 break;
4950 case WINED3D_TOP_MODULATE_2X:
4951 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
4952 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
4953 break;
4955 case WINED3D_TOP_ADD:
4956 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
4957 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
4958 break;
4960 case WINED3D_TOP_ADD_SIGNED:
4961 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
4962 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
4963 break;
4965 case WINED3D_TOP_ADD_SIGNED_2X:
4966 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
4967 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
4968 break;
4970 case WINED3D_TOP_SUBTRACT:
4971 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
4972 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
4973 break;
4975 case WINED3D_TOP_ADD_SMOOTH:
4976 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
4977 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
4978 break;
4980 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
4981 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
4982 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
4983 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
4984 break;
4986 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
4987 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
4988 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
4989 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
4990 break;
4992 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
4993 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
4994 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
4995 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
4996 break;
4998 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
4999 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5000 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5001 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
5002 break;
5004 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
5005 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
5006 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5007 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5008 break;
5010 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
5011 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
5012 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5013 break;
5015 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
5016 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
5017 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5018 break;
5020 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
5021 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5022 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5023 break;
5024 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
5025 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
5026 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5027 break;
5029 case WINED3D_TOP_BUMPENVMAP:
5030 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5031 /* These are handled in the first pass, nothing to do. */
5032 break;
5034 case WINED3D_TOP_DOTPRODUCT3:
5035 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
5036 dstreg, dstmask, arg1, arg2, dstmask);
5037 break;
5039 case WINED3D_TOP_MULTIPLY_ADD:
5040 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
5041 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
5042 break;
5044 case WINED3D_TOP_LERP:
5045 /* MSDN isn't quite right here. */
5046 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
5047 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
5048 break;
5050 default:
5051 FIXME("Unhandled operation %#x.\n", op);
5052 break;
5056 /* Context activation is done by the caller. */
5057 static GLuint shader_glsl_generate_ffp_fragment_shader(struct wined3d_shader_buffer *buffer,
5058 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
5060 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
5061 BYTE lum_map = 0, bump_map = 0, tex_map = 0;
5062 const char *final_combiner_src = "ret";
5063 UINT lowest_disabled_stage;
5064 GLhandleARB shader_obj;
5065 DWORD arg0, arg1, arg2;
5066 unsigned int stage;
5068 shader_buffer_clear(buffer);
5070 /* Find out which textures are read */
5071 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5073 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5074 break;
5076 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
5077 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
5078 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
5080 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5081 tex_map |= 1 << stage;
5082 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5083 tfactor_used = TRUE;
5084 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5085 tempreg_used = TRUE;
5086 if (settings->op[stage].dst == tempreg)
5087 tempreg_used = TRUE;
5089 switch (settings->op[stage].cop)
5091 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5092 lum_map |= 1 << stage;
5093 /* fall through */
5094 case WINED3D_TOP_BUMPENVMAP:
5095 bump_map |= 1 << stage;
5096 /* fall through */
5097 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5098 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5099 tex_map |= 1 << stage;
5100 break;
5102 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5103 tfactor_used = TRUE;
5104 break;
5106 default:
5107 break;
5110 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5111 continue;
5113 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
5114 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
5115 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
5117 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5118 tex_map |= 1 << stage;
5119 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5120 tfactor_used = TRUE;
5121 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5122 tempreg_used = TRUE;
5124 lowest_disabled_stage = stage;
5126 shader_addline(buffer, "#version 120\n");
5128 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
5129 shader_addline(buffer, "vec4 ret;\n");
5130 if (tempreg_used || settings->sRGB_write)
5131 shader_addline(buffer, "vec4 temp_reg;\n");
5132 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
5134 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5136 if (!(tex_map & (1 << stage)))
5137 continue;
5139 switch (settings->op[stage].tex_type)
5141 case tex_1d:
5142 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
5143 break;
5144 case tex_2d:
5145 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
5146 break;
5147 case tex_3d:
5148 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
5149 break;
5150 case tex_cube:
5151 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
5152 break;
5153 case tex_rect:
5154 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
5155 break;
5156 default:
5157 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
5158 break;
5161 shader_addline(buffer, "vec4 tex%u;\n", stage);
5163 if (!(bump_map & (1 << stage)))
5164 continue;
5165 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
5167 if (!(lum_map & (1 << stage)))
5168 continue;
5169 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
5170 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
5172 if (tfactor_used)
5173 shader_addline(buffer, "uniform vec4 tex_factor;\n");
5174 shader_addline(buffer, "uniform vec4 specular_enable;\n");
5176 if (settings->sRGB_write)
5178 shader_addline(buffer, "const vec4 srgb_const0 = vec4(%.8e, %.8e, %.8e, %.8e);\n",
5179 srgb_pow, srgb_mul_high, srgb_sub_high, srgb_mul_low);
5180 shader_addline(buffer, "const vec4 srgb_const1 = vec4(%.8e, 0.0, 0.0, 0.0);\n",
5181 srgb_cmp);
5184 shader_addline(buffer, "void main()\n{\n");
5186 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
5187 shader_addline(buffer, "if (any(lessThan(gl_texCoord[7], vec4(0.0)))) discard;\n");
5189 /* Generate texture sampling instructions) */
5190 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
5192 const char *texture_function, *coord_mask;
5193 char tex_reg_name[8];
5194 BOOL proj, clamp;
5196 if (!(tex_map & (1 << stage)))
5197 continue;
5199 if (settings->op[stage].projected == proj_none)
5201 proj = FALSE;
5203 else if (settings->op[stage].projected == proj_count4
5204 || settings->op[stage].projected == proj_count3)
5206 proj = TRUE;
5208 else
5210 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
5211 proj = TRUE;
5214 if (settings->op[stage].cop == WINED3D_TOP_BUMPENVMAP
5215 || settings->op[stage].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
5216 clamp = FALSE;
5217 else
5218 clamp = TRUE;
5220 switch (settings->op[stage].tex_type)
5222 case tex_1d:
5223 if (proj)
5225 texture_function = "texture1DProj";
5226 coord_mask = "xw";
5228 else
5230 texture_function = "texture1D";
5231 coord_mask = "x";
5233 break;
5234 case tex_2d:
5235 if (proj)
5237 texture_function = "texture2DProj";
5238 coord_mask = "xyw";
5240 else
5242 texture_function = "texture2D";
5243 coord_mask = "xy";
5245 break;
5246 case tex_3d:
5247 if (proj)
5249 texture_function = "texture3DProj";
5250 coord_mask = "xyzw";
5252 else
5254 texture_function = "texture3D";
5255 coord_mask = "xyz";
5257 break;
5258 case tex_cube:
5259 texture_function = "textureCube";
5260 coord_mask = "xyz";
5261 break;
5262 case tex_rect:
5263 if (proj)
5265 texture_function = "texture2DRectProj";
5266 coord_mask = "xyw";
5268 else
5270 texture_function = "texture2DRect";
5271 coord_mask = "xy";
5273 break;
5274 default:
5275 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
5276 texture_function = "";
5277 coord_mask = "xyzw";
5278 break;
5281 if (stage > 0
5282 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
5283 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
5285 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
5287 /* With projective textures, texbem only divides the static
5288 * texture coord, not the displacement, so multiply the
5289 * displacement with the dividing parameter before passing it to
5290 * TXP. */
5291 if (settings->op[stage].projected != proj_none)
5293 if (settings->op[stage].projected == proj_count4)
5295 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].w) + gl_TexCoord[%u].xy;\n",
5296 stage, stage);
5297 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].ww;\n", stage);
5299 else
5301 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].z) + gl_TexCoord[%u].xy;\n",
5302 stage, stage);
5303 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].zz;\n", stage);
5306 else
5308 shader_addline(buffer, "ret = gl_TexCoord[%u] + ret.xyxy;\n", stage);
5311 if (clamp)
5312 shader_addline(buffer, "tex%u = clamp(%s(ps_sampler%u, ret.%s), 0.0, 1.0);\n",
5313 stage, texture_function, stage, coord_mask);
5314 else
5315 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
5316 stage, texture_function, stage, coord_mask);
5318 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
5319 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
5320 stage, stage - 1, stage - 1, stage - 1);
5322 else if (settings->op[stage].projected == proj_count3)
5324 if (clamp)
5325 shader_addline(buffer, "tex%u = clamp(%s(ps_sampler%u, gl_TexCoord[%u].xyz), 0.0, 1.0);\n",
5326 stage, texture_function, stage, stage);
5327 else
5328 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].xyz);\n",
5329 stage, texture_function, stage, stage);
5331 else
5333 if (clamp)
5334 shader_addline(buffer, "tex%u = clamp(%s(ps_sampler%u, gl_TexCoord[%u].%s), 0.0, 1.0);\n",
5335 stage, texture_function, stage, stage, coord_mask);
5336 else
5337 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].%s);\n",
5338 stage, texture_function, stage, stage, coord_mask);
5341 sprintf(tex_reg_name, "tex%u", stage);
5342 shader_glsl_color_correction_ext(buffer, tex_reg_name, WINED3DSP_WRITEMASK_ALL,
5343 settings->op[stage].color_fixup);
5346 /* Generate the main shader */
5347 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5349 BOOL op_equal;
5351 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5353 if (!stage)
5354 final_combiner_src = "gl_Color";
5355 break;
5358 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5359 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5360 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
5361 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5362 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5363 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
5364 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5365 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5366 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
5367 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5368 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5369 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
5370 else
5371 op_equal = settings->op[stage].aop == settings->op[stage].cop
5372 && settings->op[stage].carg0 == settings->op[stage].aarg0
5373 && settings->op[stage].carg1 == settings->op[stage].aarg1
5374 && settings->op[stage].carg2 == settings->op[stage].aarg2;
5376 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5378 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5379 settings->op[stage].cop, settings->op[stage].carg0,
5380 settings->op[stage].carg1, settings->op[stage].carg2);
5381 if (!stage)
5382 shader_addline(buffer, "ret.w = gl_Color.w;\n");
5384 else if (op_equal)
5386 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
5387 settings->op[stage].cop, settings->op[stage].carg0,
5388 settings->op[stage].carg1, settings->op[stage].carg2);
5390 else
5392 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5393 settings->op[stage].cop, settings->op[stage].carg0,
5394 settings->op[stage].carg1, settings->op[stage].carg2);
5395 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
5396 settings->op[stage].aop, settings->op[stage].aarg0,
5397 settings->op[stage].aarg1, settings->op[stage].aarg2);
5401 shader_addline(buffer, "gl_FragData[0] = gl_SecondaryColor * specular_enable + %s;\n", final_combiner_src);
5403 if (settings->sRGB_write)
5404 shader_glsl_generate_srgb_write_correction(buffer);
5406 shader_glsl_generate_fog_code(buffer, settings->fog);
5408 shader_addline(buffer, "}\n");
5410 shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
5411 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5412 return shader_obj;
5415 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
5416 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
5418 struct glsl_ffp_fragment_shader *glsl_desc;
5419 const struct ffp_frag_desc *desc;
5421 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
5422 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
5424 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
5425 return NULL;
5427 glsl_desc->entry.settings = *args;
5428 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(&priv->shader_buffer, args, gl_info);
5429 list_init(&glsl_desc->linked_programs);
5430 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
5432 return glsl_desc;
5436 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
5437 GLhandleARB program_id, struct glsl_vs_program *vs)
5439 unsigned int i;
5440 char name[32];
5442 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5443 sizeof(GLhandleARB) * gl_info->limits.glsl_vs_float_constants);
5444 for (i = 0; i < gl_info->limits.glsl_vs_float_constants; ++i)
5446 snprintf(name, sizeof(name), "vs_c[%u]", i);
5447 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5450 for (i = 0; i < MAX_CONST_I; ++i)
5452 snprintf(name, sizeof(name), "vs_i[%u]", i);
5453 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5456 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "posFixup"));
5459 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
5460 GLhandleARB program_id, struct glsl_ps_program *ps)
5462 unsigned int i;
5463 char name[32];
5465 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5466 sizeof(GLhandleARB) * gl_info->limits.glsl_ps_float_constants);
5467 for (i = 0; i < gl_info->limits.glsl_ps_float_constants; ++i)
5469 snprintf(name, sizeof(name), "ps_c[%u]", i);
5470 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5473 for (i = 0; i < MAX_CONST_I; ++i)
5475 snprintf(name, sizeof(name), "ps_i[%u]", i);
5476 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5479 for (i = 0; i < MAX_TEXTURES; ++i)
5481 snprintf(name, sizeof(name), "bumpenv_mat%u", i);
5482 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5483 snprintf(name, sizeof(name), "bumpenv_lum_scale%u", i);
5484 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5485 snprintf(name, sizeof(name), "bumpenv_lum_offset%u", i);
5486 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5489 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "tex_factor"));
5490 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "specular_enable"));
5491 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "ps_samplerNP2Fixup"));
5492 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "ycorrection"));
5495 /* Context activation is done by the caller. */
5496 static void set_glsl_shader_program(const struct wined3d_context *context, struct wined3d_device *device,
5497 enum wined3d_shader_mode vertex_mode, enum wined3d_shader_mode fragment_mode)
5499 const struct wined3d_state *state = &device->stateBlock->state;
5500 const struct wined3d_gl_info *gl_info = context->gl_info;
5501 const struct ps_np2fixup_info *np2fixup_info = NULL;
5502 struct shader_glsl_priv *priv = device->shader_priv;
5503 struct glsl_shader_prog_link *entry = NULL;
5504 struct wined3d_shader *vshader = NULL;
5505 struct wined3d_shader *gshader = NULL;
5506 struct wined3d_shader *pshader = NULL;
5507 GLhandleARB programId = 0;
5508 GLhandleARB reorder_shader_id = 0;
5509 unsigned int i;
5510 struct ps_compile_args ps_compile_args;
5511 struct vs_compile_args vs_compile_args;
5512 GLhandleARB vs_id, gs_id, ps_id;
5513 struct list *ps_list;
5515 if (vertex_mode == WINED3D_SHADER_MODE_SHADER)
5517 vshader = state->vertex_shader;
5518 find_vs_compile_args(state, vshader, &vs_compile_args);
5519 vs_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
5521 if ((gshader = state->geometry_shader))
5522 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5523 else
5524 gs_id = 0;
5526 else
5528 vs_id = 0;
5529 gs_id = 0;
5532 if (fragment_mode == WINED3D_SHADER_MODE_SHADER)
5534 pshader = state->pixel_shader;
5535 find_ps_compile_args(state, pshader, &ps_compile_args);
5536 ps_id = find_glsl_pshader(context, &priv->shader_buffer,
5537 pshader, &ps_compile_args, &np2fixup_info);
5538 ps_list = &pshader->linked_programs;
5540 else if (fragment_mode == WINED3D_SHADER_MODE_FFP && priv->fragment_pipe == &glsl_fragment_pipe)
5542 struct glsl_ffp_fragment_shader *ffp_shader;
5543 struct ffp_frag_settings settings;
5545 gen_ffp_frag_op(device, state, &settings, FALSE);
5546 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
5547 ps_id = ffp_shader->id;
5548 ps_list = &ffp_shader->linked_programs;
5550 else
5552 ps_id = 0;
5555 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
5557 priv->glsl_program = entry;
5558 return;
5561 /* If we get to this point, then no matching program exists, so we create one */
5562 programId = GL_EXTCALL(glCreateProgramObjectARB());
5563 TRACE("Created new GLSL shader program %u\n", programId);
5565 /* Create the entry */
5566 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
5567 entry->programId = programId;
5568 entry->vs.id = vs_id;
5569 entry->gs.id = gs_id;
5570 entry->ps.id = ps_id;
5571 entry->constant_version = 0;
5572 entry->ps.np2_fixup_info = np2fixup_info;
5573 /* Add the hash table entry */
5574 add_glsl_program_entry(priv, entry);
5576 /* Set the current program */
5577 priv->glsl_program = entry;
5579 /* Attach GLSL vshader */
5580 if (vshader)
5582 WORD map = vshader->reg_maps.input_registers;
5583 char tmp_name[10];
5585 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
5586 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
5587 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
5588 checkGLcall("glAttachObjectARB");
5589 /* Flag the reorder function for deletion, then it will be freed automatically when the program
5590 * is destroyed
5592 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
5594 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, programId);
5595 GL_EXTCALL(glAttachObjectARB(programId, vs_id));
5596 checkGLcall("glAttachObjectARB");
5598 /* Bind vertex attributes to a corresponding index number to match
5599 * the same index numbers as ARB_vertex_programs (makes loading
5600 * vertex attributes simpler). With this method, we can use the
5601 * exact same code to load the attributes later for both ARB and
5602 * GLSL shaders.
5604 * We have to do this here because we need to know the Program ID
5605 * in order to make the bindings work, and it has to be done prior
5606 * to linking the GLSL program. */
5607 for (i = 0; map; map >>= 1, ++i)
5609 if (!(map & 1)) continue;
5611 snprintf(tmp_name, sizeof(tmp_name), "vs_in%u", i);
5612 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
5614 checkGLcall("glBindAttribLocationARB");
5616 list_add_head(&vshader->linked_programs, &entry->vs.shader_entry);
5619 if (gshader)
5621 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, programId);
5622 GL_EXTCALL(glAttachObjectARB(programId, gs_id));
5623 checkGLcall("glAttachObjectARB");
5625 TRACE("input type %s, output type %s, vertices out %u.\n",
5626 debug_d3dprimitivetype(gshader->u.gs.input_type),
5627 debug_d3dprimitivetype(gshader->u.gs.output_type),
5628 gshader->u.gs.vertices_out);
5629 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_INPUT_TYPE_ARB,
5630 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
5631 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_OUTPUT_TYPE_ARB,
5632 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
5633 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_VERTICES_OUT_ARB,
5634 gshader->u.gs.vertices_out));
5635 checkGLcall("glProgramParameteriARB");
5637 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
5640 /* Attach GLSL pshader */
5641 if (ps_id)
5643 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, programId);
5644 GL_EXTCALL(glAttachObjectARB(programId, ps_id));
5645 checkGLcall("glAttachObjectARB");
5647 list_add_head(ps_list, &entry->ps.shader_entry);
5650 /* Link the program */
5651 TRACE("Linking GLSL shader program %u\n", programId);
5652 GL_EXTCALL(glLinkProgramARB(programId));
5653 shader_glsl_validate_link(gl_info, programId);
5655 shader_glsl_init_vs_uniform_locations(gl_info, programId, &entry->vs);
5656 shader_glsl_init_ps_uniform_locations(gl_info, programId, &entry->ps);
5657 checkGLcall("Find glsl program uniform locations");
5659 if (pshader && pshader->reg_maps.shader_version.major >= 3
5660 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
5662 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
5663 entry->vs.vertex_color_clamp = GL_FALSE;
5665 else
5667 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
5670 /* Set the shader to allow uniform loading on it */
5671 GL_EXTCALL(glUseProgramObjectARB(programId));
5672 checkGLcall("glUseProgramObjectARB(programId)");
5674 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
5675 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
5676 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
5677 * vertex shader with fixed function pixel processing is used we make sure that the card
5678 * supports enough samplers to allow the max number of vertex samplers with all possible
5679 * fixed function fragment processing setups. So once the program is linked these samplers
5680 * won't change.
5682 shader_glsl_load_vsamplers(gl_info, device->texUnitMap, programId);
5683 shader_glsl_load_psamplers(gl_info, device->texUnitMap, programId);
5686 /* Context activation is done by the caller. */
5687 static GLhandleARB create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum tex_types tex_type, BOOL masked)
5689 GLhandleARB program_id;
5690 GLhandleARB vshader_id, pshader_id;
5691 const char *blt_pshader;
5693 static const char *blt_vshader =
5694 "#version 120\n"
5695 "void main(void)\n"
5696 "{\n"
5697 " gl_Position = gl_Vertex;\n"
5698 " gl_FrontColor = vec4(1.0);\n"
5699 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
5700 "}\n";
5702 static const char * const blt_pshaders_full[tex_type_count] =
5704 /* tex_1d */
5705 NULL,
5706 /* tex_2d */
5707 "#version 120\n"
5708 "uniform sampler2D sampler;\n"
5709 "void main(void)\n"
5710 "{\n"
5711 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
5712 "}\n",
5713 /* tex_3d */
5714 NULL,
5715 /* tex_cube */
5716 "#version 120\n"
5717 "uniform samplerCube sampler;\n"
5718 "void main(void)\n"
5719 "{\n"
5720 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
5721 "}\n",
5722 /* tex_rect */
5723 "#version 120\n"
5724 "#extension GL_ARB_texture_rectangle : enable\n"
5725 "uniform sampler2DRect sampler;\n"
5726 "void main(void)\n"
5727 "{\n"
5728 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
5729 "}\n",
5732 static const char * const blt_pshaders_masked[tex_type_count] =
5734 /* tex_1d */
5735 NULL,
5736 /* tex_2d */
5737 "#version 120\n"
5738 "uniform sampler2D sampler;\n"
5739 "uniform vec4 mask;\n"
5740 "void main(void)\n"
5741 "{\n"
5742 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
5743 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
5744 "}\n",
5745 /* tex_3d */
5746 NULL,
5747 /* tex_cube */
5748 "#version 120\n"
5749 "uniform samplerCube sampler;\n"
5750 "uniform vec4 mask;\n"
5751 "void main(void)\n"
5752 "{\n"
5753 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
5754 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
5755 "}\n",
5756 /* tex_rect */
5757 "#version 120\n"
5758 "#extension GL_ARB_texture_rectangle : enable\n"
5759 "uniform sampler2DRect sampler;\n"
5760 "uniform vec4 mask;\n"
5761 "void main(void)\n"
5762 "{\n"
5763 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
5764 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
5765 "}\n",
5768 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
5769 if (!blt_pshader)
5771 FIXME("tex_type %#x not supported\n", tex_type);
5772 return 0;
5775 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
5776 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
5778 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
5779 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
5781 program_id = GL_EXTCALL(glCreateProgramObjectARB());
5782 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
5783 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
5784 GL_EXTCALL(glLinkProgramARB(program_id));
5786 shader_glsl_validate_link(gl_info, program_id);
5788 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
5789 * is destroyed
5791 GL_EXTCALL(glDeleteObjectARB(vshader_id));
5792 GL_EXTCALL(glDeleteObjectARB(pshader_id));
5793 return program_id;
5796 /* Context activation is done by the caller. */
5797 static void shader_glsl_select(const struct wined3d_context *context, enum wined3d_shader_mode vertex_mode,
5798 enum wined3d_shader_mode fragment_mode)
5800 const struct wined3d_gl_info *gl_info = context->gl_info;
5801 struct wined3d_device *device = context->swapchain->device;
5802 struct shader_glsl_priv *priv = device->shader_priv;
5803 GLhandleARB program_id = 0;
5804 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
5806 old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vs.vertex_color_clamp : GL_FIXED_ONLY_ARB;
5807 set_glsl_shader_program(context, device, vertex_mode, fragment_mode);
5808 current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vs.vertex_color_clamp : GL_FIXED_ONLY_ARB;
5809 if (old_vertex_color_clamp != current_vertex_color_clamp)
5811 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
5813 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
5814 checkGLcall("glClampColorARB");
5816 else
5818 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
5822 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
5823 if (program_id) TRACE("Using GLSL program %u\n", program_id);
5824 GL_EXTCALL(glUseProgramObjectARB(program_id));
5825 checkGLcall("glUseProgramObjectARB");
5827 /* In case that NP2 texcoord fixup data is found for the selected program, trigger a reload of the
5828 * constants. This has to be done because it can't be guaranteed that sampler() (from state.c) is
5829 * called between selecting the shader and using it, which results in wrong fixup for some frames. */
5830 if (priv->glsl_program && priv->glsl_program->ps.np2_fixup_info)
5832 shader_glsl_load_np2fixup_constants(priv, gl_info, &device->stateBlock->state);
5836 /* Context activation is done by the caller. */
5837 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
5838 enum tex_types tex_type, const SIZE *ds_mask_size)
5840 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
5841 struct shader_glsl_priv *priv = shader_priv;
5842 GLhandleARB *blt_program;
5843 GLint loc;
5845 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
5846 if (!*blt_program)
5848 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
5849 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
5850 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
5851 GL_EXTCALL(glUniform1iARB(loc, 0));
5853 else
5855 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
5858 if (masked)
5860 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "mask"));
5861 GL_EXTCALL(glUniform4fARB(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
5865 /* Context activation is done by the caller. */
5866 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
5868 struct shader_glsl_priv *priv = shader_priv;
5869 GLhandleARB program_id;
5871 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
5872 if (program_id) TRACE("Using GLSL program %u\n", program_id);
5874 GL_EXTCALL(glUseProgramObjectARB(program_id));
5875 checkGLcall("glUseProgramObjectARB");
5878 static void shader_glsl_destroy(struct wined3d_shader *shader)
5880 struct glsl_shader_private *shader_data = shader->backend_data;
5881 struct wined3d_device *device = shader->device;
5882 struct shader_glsl_priv *priv = device->shader_priv;
5883 const struct wined3d_gl_info *gl_info;
5884 const struct list *linked_programs;
5885 struct wined3d_context *context;
5887 if (!shader_data || !shader_data->num_gl_shaders)
5889 HeapFree(GetProcessHeap(), 0, shader_data);
5890 shader->backend_data = NULL;
5891 return;
5894 context = context_acquire(device, NULL);
5895 gl_info = context->gl_info;
5897 TRACE("Deleting linked programs.\n");
5898 linked_programs = &shader->linked_programs;
5899 if (linked_programs->next)
5901 struct glsl_shader_prog_link *entry, *entry2;
5902 UINT i;
5904 switch (shader->reg_maps.shader_version.type)
5906 case WINED3D_SHADER_TYPE_PIXEL:
5908 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
5910 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
5911 struct glsl_shader_prog_link, ps.shader_entry)
5913 delete_glsl_program_entry(priv, gl_info, entry);
5916 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5918 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].prgId);
5919 if (priv->glsl_program && priv->glsl_program->ps.id == gl_shaders[i].prgId)
5920 shader_glsl_select(context, WINED3D_SHADER_MODE_NONE, WINED3D_SHADER_MODE_NONE);
5921 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].prgId));
5922 checkGLcall("glDeleteObjectARB");
5924 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
5926 break;
5929 case WINED3D_SHADER_TYPE_VERTEX:
5931 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
5933 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
5934 struct glsl_shader_prog_link, vs.shader_entry)
5936 delete_glsl_program_entry(priv, gl_info, entry);
5939 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5941 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].prgId);
5942 if (priv->glsl_program && priv->glsl_program->vs.id == gl_shaders[i].prgId)
5943 shader_glsl_select(context, WINED3D_SHADER_MODE_NONE, WINED3D_SHADER_MODE_NONE);
5944 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].prgId));
5945 checkGLcall("glDeleteObjectARB");
5947 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
5949 break;
5952 case WINED3D_SHADER_TYPE_GEOMETRY:
5954 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
5956 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
5957 struct glsl_shader_prog_link, gs.shader_entry)
5959 delete_glsl_program_entry(priv, gl_info, entry);
5962 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5964 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
5965 if (priv->glsl_program && priv->glsl_program->gs.id == gl_shaders[i].id)
5966 shader_glsl_select(context, WINED3D_SHADER_MODE_NONE, WINED3D_SHADER_MODE_NONE);
5967 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].id));
5968 checkGLcall("glDeleteObjectARB");
5970 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
5972 break;
5975 default:
5976 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
5977 break;
5981 HeapFree(GetProcessHeap(), 0, shader->backend_data);
5982 shader->backend_data = NULL;
5984 context_release(context);
5987 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
5989 const struct glsl_program_key *k = key;
5990 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
5991 const struct glsl_shader_prog_link, program_lookup_entry);
5993 if (k->vs_id > prog->vs.id) return 1;
5994 else if (k->vs_id < prog->vs.id) return -1;
5996 if (k->gs_id > prog->gs.id) return 1;
5997 else if (k->gs_id < prog->gs.id) return -1;
5999 if (k->ps_id > prog->ps.id) return 1;
6000 else if (k->ps_id < prog->ps.id) return -1;
6002 return 0;
6005 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
6007 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries) + constant_count * sizeof(*heap->positions);
6008 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
6010 if (!mem)
6012 ERR("Failed to allocate memory\n");
6013 return FALSE;
6016 heap->entries = mem;
6017 heap->entries[1].version = 0;
6018 heap->positions = (unsigned int *)(heap->entries + constant_count + 1);
6019 heap->size = 1;
6021 return TRUE;
6024 static void constant_heap_free(struct constant_heap *heap)
6026 HeapFree(GetProcessHeap(), 0, heap->entries);
6029 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
6031 wined3d_rb_alloc,
6032 wined3d_rb_realloc,
6033 wined3d_rb_free,
6034 glsl_program_key_compare,
6037 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct fragment_pipeline *fragment_pipe)
6039 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6040 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
6041 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
6042 gl_info->limits.glsl_ps_float_constants)) + 1;
6043 struct fragment_caps fragment_caps;
6044 void *fragment_priv;
6046 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
6048 ERR("Failed to initialize fragment pipe.\n");
6049 HeapFree(GetProcessHeap(), 0, priv);
6050 return E_FAIL;
6053 if (!shader_buffer_init(&priv->shader_buffer))
6055 ERR("Failed to initialize shader buffer.\n");
6056 goto fail;
6059 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
6060 if (!priv->stack)
6062 ERR("Failed to allocate memory.\n");
6063 goto fail;
6066 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
6068 ERR("Failed to initialize vertex shader constant heap\n");
6069 goto fail;
6072 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
6074 ERR("Failed to initialize pixel shader constant heap\n");
6075 goto fail;
6078 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
6080 ERR("Failed to initialize rbtree.\n");
6081 goto fail;
6084 priv->next_constant_version = 1;
6085 fragment_pipe->get_caps(gl_info, &fragment_caps);
6086 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
6087 device->fragment_priv = fragment_priv;
6088 priv->fragment_pipe = fragment_pipe;
6090 device->shader_priv = priv;
6091 return WINED3D_OK;
6093 fail:
6094 constant_heap_free(&priv->pconst_heap);
6095 constant_heap_free(&priv->vconst_heap);
6096 HeapFree(GetProcessHeap(), 0, priv->stack);
6097 shader_buffer_free(&priv->shader_buffer);
6098 fragment_pipe->free_private(device);
6099 HeapFree(GetProcessHeap(), 0, priv);
6100 return E_OUTOFMEMORY;
6103 /* Context activation is done by the caller. */
6104 static void shader_glsl_free(struct wined3d_device *device)
6106 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6107 struct shader_glsl_priv *priv = device->shader_priv;
6108 int i;
6110 for (i = 0; i < tex_type_count; ++i)
6112 if (priv->depth_blt_program_full[i])
6114 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_full[i]));
6116 if (priv->depth_blt_program_masked[i])
6118 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_masked[i]));
6122 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
6123 constant_heap_free(&priv->pconst_heap);
6124 constant_heap_free(&priv->vconst_heap);
6125 HeapFree(GetProcessHeap(), 0, priv->stack);
6126 shader_buffer_free(&priv->shader_buffer);
6127 priv->fragment_pipe->free_private(device);
6129 HeapFree(GetProcessHeap(), 0, device->shader_priv);
6130 device->shader_priv = NULL;
6133 static void shader_glsl_context_destroyed(void *shader_priv, const struct wined3d_context *context) {}
6135 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
6137 UINT shader_model;
6139 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
6140 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
6141 && gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] && gl_info->supported[ARB_DRAW_INSTANCED])
6142 shader_model = 4;
6143 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
6144 * texldd and texldl instructions. */
6145 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
6146 shader_model = 3;
6147 else
6148 shader_model = 2;
6149 TRACE("Shader model %u.\n", shader_model);
6151 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
6152 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
6153 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
6155 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
6156 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
6158 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
6159 * Direct3D minimum requirement.
6161 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
6162 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
6164 * The problem is that the refrast clamps temporary results in the shader to
6165 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
6166 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
6167 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
6168 * offer a way to query this.
6170 caps->ps_1x_max_value = 8.0;
6172 /* Ideally we'd only set caps like sRGB writes here if supported by both
6173 * the shader backend and the fragment pipe, but we can get called before
6174 * shader_glsl_alloc(). */
6175 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
6176 | WINED3D_SHADER_CAP_SRGB_WRITE;
6179 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
6181 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
6183 TRACE("Checking support for fixup:\n");
6184 dump_color_fixup_desc(fixup);
6187 /* We support everything except YUV conversions. */
6188 if (!is_complex_fixup(fixup))
6190 TRACE("[OK]\n");
6191 return TRUE;
6194 TRACE("[FAILED]\n");
6195 return FALSE;
6198 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
6200 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
6201 /* WINED3DSIH_ADD */ shader_glsl_binop,
6202 /* WINED3DSIH_AND */ shader_glsl_binop,
6203 /* WINED3DSIH_BEM */ shader_glsl_bem,
6204 /* WINED3DSIH_BREAK */ shader_glsl_break,
6205 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
6206 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
6207 /* WINED3DSIH_CALL */ shader_glsl_call,
6208 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
6209 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
6210 /* WINED3DSIH_CND */ shader_glsl_cnd,
6211 /* WINED3DSIH_CRS */ shader_glsl_cross,
6212 /* WINED3DSIH_CUT */ shader_glsl_cut,
6213 /* WINED3DSIH_DCL */ shader_glsl_nop,
6214 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
6215 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
6216 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
6217 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
6218 /* WINED3DSIH_DEF */ shader_glsl_nop,
6219 /* WINED3DSIH_DEFB */ shader_glsl_nop,
6220 /* WINED3DSIH_DEFI */ shader_glsl_nop,
6221 /* WINED3DSIH_DIV */ shader_glsl_binop,
6222 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
6223 /* WINED3DSIH_DP3 */ shader_glsl_dot,
6224 /* WINED3DSIH_DP4 */ shader_glsl_dot,
6225 /* WINED3DSIH_DST */ shader_glsl_dst,
6226 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
6227 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
6228 /* WINED3DSIH_ELSE */ shader_glsl_else,
6229 /* WINED3DSIH_EMIT */ shader_glsl_emit,
6230 /* WINED3DSIH_ENDIF */ shader_glsl_end,
6231 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
6232 /* WINED3DSIH_ENDREP */ shader_glsl_end,
6233 /* WINED3DSIH_EQ */ shader_glsl_relop,
6234 /* WINED3DSIH_EXP */ shader_glsl_map2gl,
6235 /* WINED3DSIH_EXPP */ shader_glsl_expp,
6236 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
6237 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
6238 /* WINED3DSIH_GE */ shader_glsl_relop,
6239 /* WINED3DSIH_IADD */ shader_glsl_binop,
6240 /* WINED3DSIH_IEQ */ NULL,
6241 /* WINED3DSIH_IF */ shader_glsl_if,
6242 /* WINED3DSIH_IFC */ shader_glsl_ifc,
6243 /* WINED3DSIH_IGE */ shader_glsl_relop,
6244 /* WINED3DSIH_IMUL */ shader_glsl_imul,
6245 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
6246 /* WINED3DSIH_LABEL */ shader_glsl_label,
6247 /* WINED3DSIH_LD */ NULL,
6248 /* WINED3DSIH_LIT */ shader_glsl_lit,
6249 /* WINED3DSIH_LOG */ shader_glsl_log,
6250 /* WINED3DSIH_LOGP */ shader_glsl_log,
6251 /* WINED3DSIH_LOOP */ shader_glsl_loop,
6252 /* WINED3DSIH_LRP */ shader_glsl_lrp,
6253 /* WINED3DSIH_LT */ shader_glsl_relop,
6254 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
6255 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
6256 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
6257 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
6258 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
6259 /* WINED3DSIH_MAD */ shader_glsl_mad,
6260 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
6261 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
6262 /* WINED3DSIH_MOV */ shader_glsl_mov,
6263 /* WINED3DSIH_MOVA */ shader_glsl_mov,
6264 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
6265 /* WINED3DSIH_MUL */ shader_glsl_binop,
6266 /* WINED3DSIH_NOP */ shader_glsl_nop,
6267 /* WINED3DSIH_NRM */ shader_glsl_nrm,
6268 /* WINED3DSIH_PHASE */ shader_glsl_nop,
6269 /* WINED3DSIH_POW */ shader_glsl_pow,
6270 /* WINED3DSIH_RCP */ shader_glsl_rcp,
6271 /* WINED3DSIH_REP */ shader_glsl_rep,
6272 /* WINED3DSIH_RET */ shader_glsl_ret,
6273 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
6274 /* WINED3DSIH_RSQ */ shader_glsl_rsq,
6275 /* WINED3DSIH_SAMPLE */ NULL,
6276 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
6277 /* WINED3DSIH_SAMPLE_LOD */ NULL,
6278 /* WINED3DSIH_SETP */ NULL,
6279 /* WINED3DSIH_SGE */ shader_glsl_compare,
6280 /* WINED3DSIH_SGN */ shader_glsl_sgn,
6281 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
6282 /* WINED3DSIH_SLT */ shader_glsl_compare,
6283 /* WINED3DSIH_SQRT */ NULL,
6284 /* WINED3DSIH_SUB */ shader_glsl_binop,
6285 /* WINED3DSIH_TEX */ shader_glsl_tex,
6286 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
6287 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
6288 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
6289 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
6290 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
6291 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
6292 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
6293 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
6294 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
6295 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
6296 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
6297 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
6298 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
6299 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
6300 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
6301 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
6302 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
6303 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
6304 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
6305 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
6306 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
6307 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
6308 /* WINED3DSIH_USHR */ shader_glsl_binop,
6309 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
6310 /* WINED3DSIH_XOR */ shader_glsl_binop,
6313 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
6314 SHADER_HANDLER hw_fct;
6316 /* Select handler */
6317 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
6319 /* Unhandled opcode */
6320 if (!hw_fct)
6322 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
6323 return;
6325 hw_fct(ins);
6327 shader_glsl_add_instruction_modifiers(ins);
6330 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
6332 struct shader_glsl_priv *priv = shader_priv;
6334 return priv->ffp_proj_control;
6337 const struct wined3d_shader_backend_ops glsl_shader_backend =
6339 shader_glsl_handle_instruction,
6340 shader_glsl_select,
6341 shader_glsl_select_depth_blt,
6342 shader_glsl_deselect_depth_blt,
6343 shader_glsl_update_float_vertex_constants,
6344 shader_glsl_update_float_pixel_constants,
6345 shader_glsl_load_constants,
6346 shader_glsl_load_np2fixup_constants,
6347 shader_glsl_destroy,
6348 shader_glsl_alloc,
6349 shader_glsl_free,
6350 shader_glsl_context_destroyed,
6351 shader_glsl_get_caps,
6352 shader_glsl_color_fixup_supported,
6353 shader_glsl_has_ffp_proj_control,
6356 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
6358 /* Nothing to do. */
6361 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
6363 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
6364 | WINED3D_FRAGMENT_CAP_SRGB_WRITE;
6365 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP;
6366 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
6367 | WINED3DTEXOPCAPS_SELECTARG1
6368 | WINED3DTEXOPCAPS_SELECTARG2
6369 | WINED3DTEXOPCAPS_MODULATE4X
6370 | WINED3DTEXOPCAPS_MODULATE2X
6371 | WINED3DTEXOPCAPS_MODULATE
6372 | WINED3DTEXOPCAPS_ADDSIGNED2X
6373 | WINED3DTEXOPCAPS_ADDSIGNED
6374 | WINED3DTEXOPCAPS_ADD
6375 | WINED3DTEXOPCAPS_SUBTRACT
6376 | WINED3DTEXOPCAPS_ADDSMOOTH
6377 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
6378 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
6379 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
6380 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
6381 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
6382 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
6383 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
6384 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
6385 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
6386 | WINED3DTEXOPCAPS_DOTPRODUCT3
6387 | WINED3DTEXOPCAPS_MULTIPLYADD
6388 | WINED3DTEXOPCAPS_LERP
6389 | WINED3DTEXOPCAPS_BUMPENVMAP
6390 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
6391 caps->MaxTextureBlendStages = 8;
6392 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
6395 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
6397 struct shader_glsl_priv *priv;
6399 if (shader_backend == &glsl_shader_backend)
6401 priv = shader_priv;
6403 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
6405 ERR("Failed to initialize rbtree.\n");
6406 return NULL;
6409 return priv;
6412 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
6414 return NULL;
6417 struct glsl_ffp_destroy_ctx
6419 struct shader_glsl_priv *priv;
6420 const struct wined3d_gl_info *gl_info;
6423 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
6425 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
6426 struct glsl_ffp_fragment_shader, entry.entry);
6427 struct glsl_shader_prog_link *program, *program2;
6428 struct glsl_ffp_destroy_ctx *ctx = context;
6430 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
6431 struct glsl_shader_prog_link, ps.shader_entry)
6433 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
6435 ctx->gl_info->gl_ops.ext.p_glDeleteObjectARB(shader->id);
6436 HeapFree(GetProcessHeap(), 0, shader);
6439 /* Context activation is done by the caller. */
6440 static void glsl_fragment_pipe_free(struct wined3d_device *device)
6442 struct shader_glsl_priv *priv = device->fragment_priv;
6443 struct glsl_ffp_destroy_ctx ctx;
6445 ctx.priv = priv;
6446 ctx.gl_info = &device->adapter->gl_info;
6447 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
6450 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
6451 const struct wined3d_state *state, DWORD state_id)
6453 context->last_was_pshader = use_ps(state);
6455 context->select_shader = 1;
6456 context->load_constants = 1;
6459 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
6460 const struct wined3d_state *state, DWORD state_id)
6462 BOOL use_vshader = use_vs(state);
6463 enum fogsource new_source;
6465 context->select_shader = 1;
6466 context->load_constants = 1;
6468 if (!state->render_states[WINED3D_RS_FOGENABLE])
6469 return;
6471 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
6473 if (use_vshader)
6474 new_source = FOGSOURCE_VS;
6475 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->last_was_rhw)
6476 new_source = FOGSOURCE_COORD;
6477 else
6478 new_source = FOGSOURCE_FFP;
6480 else
6482 new_source = FOGSOURCE_FFP;
6485 if (new_source != context->fog_source)
6487 context->fog_source = new_source;
6488 state_fogstartend(context, state, STATE_RENDER(WINED3D_RS_FOGSTART));
6492 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
6493 const struct wined3d_state *state, DWORD state_id)
6495 context->select_shader = 1;
6496 context->load_constants = 1;
6499 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
6500 const struct wined3d_state *state, DWORD state_id)
6502 context->load_constants = 1;
6505 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
6507 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
6508 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6509 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6510 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6511 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6512 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6513 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6514 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6515 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6516 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6517 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
6518 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6519 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6520 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6521 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
6522 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
6523 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6524 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6525 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6526 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6527 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6528 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6529 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6530 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6531 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6532 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
6533 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6534 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6535 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6536 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
6537 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
6538 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6539 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6540 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6541 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6542 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6543 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6544 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6545 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6546 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6547 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
6548 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6549 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6550 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6551 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
6552 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
6553 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6554 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6555 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6556 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6557 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6558 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6559 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6560 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6561 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6562 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
6563 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6564 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6565 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6566 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
6567 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
6568 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6569 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6570 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6571 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6572 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6573 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6574 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6575 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6576 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6577 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
6578 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6579 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6580 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6581 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
6582 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
6583 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6584 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6585 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6586 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6587 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6588 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6589 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6590 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6591 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6592 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
6593 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6594 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6595 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6596 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
6597 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
6598 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6599 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6600 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6601 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6602 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6603 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6604 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6605 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6606 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6607 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
6608 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6609 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6610 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6611 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
6612 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
6613 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6614 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6615 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6616 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6617 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6618 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6619 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6620 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6621 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6622 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
6623 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6624 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6625 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
6626 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
6627 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
6628 {STATE_PIXELSHADER, {STATE_PIXELSHADER, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
6629 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
6630 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6631 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6632 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
6633 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
6634 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
6635 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
6636 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
6637 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
6638 {STATE_TEXTURESTAGE(0,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_fragment_pipe_tex_transform }, WINED3D_GL_EXT_NONE },
6639 {STATE_TEXTURESTAGE(1,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_fragment_pipe_tex_transform }, WINED3D_GL_EXT_NONE },
6640 {STATE_TEXTURESTAGE(2,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_fragment_pipe_tex_transform }, WINED3D_GL_EXT_NONE },
6641 {STATE_TEXTURESTAGE(3,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_fragment_pipe_tex_transform }, WINED3D_GL_EXT_NONE },
6642 {STATE_TEXTURESTAGE(4,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_fragment_pipe_tex_transform }, WINED3D_GL_EXT_NONE },
6643 {STATE_TEXTURESTAGE(5,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_fragment_pipe_tex_transform }, WINED3D_GL_EXT_NONE },
6644 {STATE_TEXTURESTAGE(6,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_fragment_pipe_tex_transform }, WINED3D_GL_EXT_NONE },
6645 {STATE_TEXTURESTAGE(7,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_fragment_pipe_tex_transform }, WINED3D_GL_EXT_NONE },
6646 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
6647 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
6650 const struct fragment_pipeline glsl_fragment_pipe =
6652 glsl_fragment_pipe_enable,
6653 glsl_fragment_pipe_get_caps,
6654 glsl_fragment_pipe_alloc,
6655 glsl_fragment_pipe_free,
6656 shader_glsl_color_fixup_supported,
6657 glsl_fragment_pipe_state_template,