wined3d: Introduce a helper function for printing floats in GLSL shaders.
[wine.git] / dlls / wined3d / glsl_shader.c
blobffe4d25246ca57baec4ebf46439dffe19304ce1a
1 /*
2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
6 * Copyright 2007-2009, 2013 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_NPOT 0x2
47 #define WINED3D_GLSL_SAMPLE_LOD 0x4
48 #define WINED3D_GLSL_SAMPLE_GRAD 0x8
50 static const float srgb_const0[] = {0.41666f, 1.055f, 0.055f, 12.92f}; /* pow, mul_high, sub_high, mul_low */
51 static const float srgb_const1[] = {0.0031308f, 0.0f, 0.0f, 0.0f}; /* cmp */
53 struct glsl_dst_param
55 char reg_name[150];
56 char mask_str[6];
59 struct glsl_src_param
61 char reg_name[150];
62 char param_str[200];
65 struct glsl_sample_function
67 const char *name;
68 DWORD coord_mask;
71 enum heap_node_op
73 HEAP_NODE_TRAVERSE_LEFT,
74 HEAP_NODE_TRAVERSE_RIGHT,
75 HEAP_NODE_POP,
78 struct constant_entry
80 unsigned int idx;
81 unsigned int version;
84 struct constant_heap
86 struct constant_entry *entries;
87 unsigned int *positions;
88 unsigned int size;
91 /* GLSL shader private data */
92 struct shader_glsl_priv {
93 struct wined3d_shader_buffer shader_buffer;
94 struct wine_rb_tree program_lookup;
95 struct glsl_shader_prog_link *glsl_program;
96 struct constant_heap vconst_heap;
97 struct constant_heap pconst_heap;
98 unsigned char *stack;
99 GLhandleARB depth_blt_program_full[tex_type_count];
100 GLhandleARB depth_blt_program_masked[tex_type_count];
101 UINT next_constant_version;
103 const struct wined3d_vertex_pipe_ops *vertex_pipe;
104 const struct fragment_pipeline *fragment_pipe;
105 struct wine_rb_tree ffp_vertex_shaders;
106 struct wine_rb_tree ffp_fragment_shaders;
107 BOOL ffp_proj_control;
110 struct glsl_vs_program
112 struct list shader_entry;
113 GLhandleARB id;
114 GLenum vertex_color_clamp;
115 GLint *uniform_f_locations;
116 GLint uniform_i_locations[MAX_CONST_I];
117 GLint pos_fixup_location;
120 struct glsl_gs_program
122 struct list shader_entry;
123 GLhandleARB id;
126 struct glsl_ps_program
128 struct list shader_entry;
129 GLhandleARB id;
130 GLint *uniform_f_locations;
131 GLint uniform_i_locations[MAX_CONST_I];
132 GLint bumpenv_mat_location[MAX_TEXTURES];
133 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
134 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
135 GLint tex_factor_location;
136 GLint specular_enable_location;
137 GLint ycorrection_location;
138 GLint np2_fixup_location;
139 const struct ps_np2fixup_info *np2_fixup_info;
142 /* Struct to maintain data about a linked GLSL program */
143 struct glsl_shader_prog_link
145 struct wine_rb_entry program_lookup_entry;
146 struct glsl_vs_program vs;
147 struct glsl_gs_program gs;
148 struct glsl_ps_program ps;
149 GLhandleARB programId;
150 UINT constant_version;
153 struct glsl_program_key
155 GLhandleARB vs_id;
156 GLhandleARB gs_id;
157 GLhandleARB ps_id;
160 struct shader_glsl_ctx_priv {
161 const struct vs_compile_args *cur_vs_args;
162 const struct ps_compile_args *cur_ps_args;
163 struct ps_np2fixup_info *cur_np2fixup_info;
166 struct glsl_ps_compiled_shader
168 struct ps_compile_args args;
169 struct ps_np2fixup_info np2fixup;
170 GLhandleARB prgId;
173 struct glsl_vs_compiled_shader
175 struct vs_compile_args args;
176 GLhandleARB prgId;
179 struct glsl_gs_compiled_shader
181 GLhandleARB id;
184 struct glsl_shader_private
186 union
188 struct glsl_vs_compiled_shader *vs;
189 struct glsl_gs_compiled_shader *gs;
190 struct glsl_ps_compiled_shader *ps;
191 } gl_shaders;
192 UINT num_gl_shaders, shader_array_size;
195 struct glsl_ffp_vertex_shader
197 struct wined3d_ffp_vs_desc desc;
198 GLhandleARB id;
199 struct list linked_programs;
202 struct glsl_ffp_fragment_shader
204 struct ffp_frag_desc entry;
205 GLhandleARB id;
206 struct list linked_programs;
209 struct glsl_ffp_destroy_ctx
211 struct shader_glsl_priv *priv;
212 const struct wined3d_gl_info *gl_info;
215 static const char *debug_gl_shader_type(GLenum type)
217 switch (type)
219 #define WINED3D_TO_STR(u) case u: return #u
220 WINED3D_TO_STR(GL_VERTEX_SHADER_ARB);
221 WINED3D_TO_STR(GL_GEOMETRY_SHADER_ARB);
222 WINED3D_TO_STR(GL_FRAGMENT_SHADER_ARB);
223 #undef WINED3D_TO_STR
224 default:
225 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
229 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
231 switch (type)
233 case WINED3D_SHADER_TYPE_VERTEX:
234 return "vs";
236 case WINED3D_SHADER_TYPE_GEOMETRY:
237 return "gs";
239 case WINED3D_SHADER_TYPE_PIXEL:
240 return "ps";
242 default:
243 FIXME("Unhandled shader type %#x.\n", type);
244 return "unknown";
248 /* This should be equivalent to using the %.8e format specifier, but always
249 * using '.' as decimal separator. This doesn't handle +/-INF or NAN, since
250 * the GLSL parser wouldn't be able to handle those anyway. */
251 static void shader_glsl_ftoa(float value, char *s)
253 int x, frac, exponent;
254 const char *sign = "";
255 double d;
257 d = value;
258 if (copysignf(1.0f, value) < 0.0f)
260 d = -d;
261 sign = "-";
264 if (d == 0.0f)
266 x = 0;
267 frac = 0;
268 exponent = 0;
270 else
272 double t, diff;
274 exponent = floorf(log10f(d));
275 d /= pow(10.0, exponent);
277 x = d;
278 t = (d - x) * 100000000;
279 frac = t;
280 diff = t - frac;
282 if ((diff > 0.5) || (diff == 0.5 && (frac & 1)))
284 if (++frac >= 100000000)
286 frac = 0;
287 ++x;
292 sprintf(s, "%s%d.%08de%+03d", sign, x, frac, exponent);
295 static void shader_glsl_append_imm_vec4(struct wined3d_shader_buffer *buffer, const float *values)
297 char str[4][16];
299 shader_glsl_ftoa(values[0], str[0]);
300 shader_glsl_ftoa(values[1], str[1]);
301 shader_glsl_ftoa(values[2], str[2]);
302 shader_glsl_ftoa(values[3], str[3]);
303 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
306 /* Extract a line from the info log.
307 * Note that this modifies the source string. */
308 static char *get_info_log_line(char **ptr)
310 char *p, *q;
312 p = *ptr;
313 if (!(q = strstr(p, "\n")))
315 if (!*p) return NULL;
316 *ptr += strlen(p);
317 return p;
319 *q = '\0';
320 *ptr = q + 1;
322 return p;
325 /* Context activation is done by the caller. */
326 static void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLhandleARB obj)
328 int infologLength = 0;
329 char *infoLog;
331 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
332 return;
334 GL_EXTCALL(glGetObjectParameterivARB(obj,
335 GL_OBJECT_INFO_LOG_LENGTH_ARB,
336 &infologLength));
338 /* A size of 1 is just a null-terminated string, so the log should be bigger than
339 * that if there are errors. */
340 if (infologLength > 1)
342 char *ptr, *line;
344 infoLog = HeapAlloc(GetProcessHeap(), 0, infologLength);
345 /* The info log is supposed to be zero-terminated, but at least some
346 * versions of fglrx don't terminate the string properly. The reported
347 * length does include the terminator, so explicitly set it to zero
348 * here. */
349 infoLog[infologLength - 1] = 0;
350 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
352 ptr = infoLog;
353 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
355 WARN("Info log received from GLSL shader #%u:\n", obj);
356 while ((line = get_info_log_line(&ptr))) WARN(" %s\n", line);
358 else
360 FIXME("Info log received from GLSL shader #%u:\n", obj);
361 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
363 HeapFree(GetProcessHeap(), 0, infoLog);
367 /* Context activation is done by the caller. */
368 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLhandleARB shader, const char *src)
370 TRACE("Compiling shader object %u.\n", shader);
371 GL_EXTCALL(glShaderSourceARB(shader, 1, &src, NULL));
372 checkGLcall("glShaderSourceARB");
373 GL_EXTCALL(glCompileShaderARB(shader));
374 checkGLcall("glCompileShaderARB");
375 print_glsl_info_log(gl_info, shader);
378 /* Context activation is done by the caller. */
379 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLhandleARB program)
381 GLint i, object_count, source_size = -1;
382 GLhandleARB *objects;
383 char *source = NULL;
385 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_ATTACHED_OBJECTS_ARB, &object_count));
386 objects = HeapAlloc(GetProcessHeap(), 0, object_count * sizeof(*objects));
387 if (!objects)
389 ERR("Failed to allocate object array memory.\n");
390 return;
393 GL_EXTCALL(glGetAttachedObjectsARB(program, object_count, NULL, objects));
394 for (i = 0; i < object_count; ++i)
396 char *ptr, *line;
397 GLint tmp;
399 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &tmp));
401 if (source_size < tmp)
403 HeapFree(GetProcessHeap(), 0, source);
405 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
406 if (!source)
408 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
409 HeapFree(GetProcessHeap(), 0, objects);
410 return;
412 source_size = tmp;
415 FIXME("Object %u:\n", objects[i]);
416 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SUBTYPE_ARB, &tmp));
417 FIXME(" GL_OBJECT_SUBTYPE_ARB: %s.\n", debug_gl_shader_type(tmp));
418 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_COMPILE_STATUS_ARB, &tmp));
419 FIXME(" GL_OBJECT_COMPILE_STATUS_ARB: %d.\n", tmp);
420 FIXME("\n");
422 ptr = source;
423 GL_EXTCALL(glGetShaderSourceARB(objects[i], source_size, NULL, source));
424 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
425 FIXME("\n");
428 HeapFree(GetProcessHeap(), 0, source);
429 HeapFree(GetProcessHeap(), 0, objects);
432 /* Context activation is done by the caller. */
433 static void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLhandleARB program)
435 GLint tmp;
437 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader)) return;
439 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_TYPE_ARB, &tmp));
440 if (tmp == GL_PROGRAM_OBJECT_ARB)
442 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &tmp));
443 if (!tmp)
445 FIXME("Program %u link status invalid.\n", program);
446 shader_glsl_dump_program_source(gl_info, program);
450 print_glsl_info_log(gl_info, program);
453 /* Context activation is done by the caller. */
454 static void shader_glsl_load_psamplers(const struct wined3d_gl_info *gl_info,
455 const DWORD *tex_unit_map, GLhandleARB programId)
457 GLint name_loc;
458 char sampler_name[20];
459 unsigned int i;
461 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
463 snprintf(sampler_name, sizeof(sampler_name), "ps_sampler%u", i);
464 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
465 if (name_loc != -1) {
466 DWORD mapped_unit = tex_unit_map[i];
467 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.fragment_samplers)
469 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
470 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
471 checkGLcall("glUniform1iARB");
472 } else {
473 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
479 /* Context activation is done by the caller. */
480 static void shader_glsl_load_vsamplers(const struct wined3d_gl_info *gl_info,
481 const DWORD *tex_unit_map, GLhandleARB programId)
483 GLint name_loc;
484 char sampler_name[20];
485 unsigned int i;
487 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
489 snprintf(sampler_name, sizeof(sampler_name), "vs_sampler%u", i);
490 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
491 if (name_loc != -1) {
492 DWORD mapped_unit = tex_unit_map[MAX_FRAGMENT_SAMPLERS + i];
493 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.combined_samplers)
495 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
496 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
497 checkGLcall("glUniform1iARB");
498 } else {
499 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
505 /* Context activation is done by the caller. */
506 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
507 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
509 int stack_idx = 0;
510 unsigned int heap_idx = 1;
511 unsigned int idx;
513 if (heap->entries[heap_idx].version <= version) return;
515 idx = heap->entries[heap_idx].idx;
516 if (constant_locations[idx] != -1) GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
517 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
519 while (stack_idx >= 0)
521 /* Note that we fall through to the next case statement. */
522 switch(stack[stack_idx])
524 case HEAP_NODE_TRAVERSE_LEFT:
526 unsigned int left_idx = heap_idx << 1;
527 if (left_idx < heap->size && heap->entries[left_idx].version > version)
529 heap_idx = left_idx;
530 idx = heap->entries[heap_idx].idx;
531 if (constant_locations[idx] != -1)
532 GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
534 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
535 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
536 break;
540 case HEAP_NODE_TRAVERSE_RIGHT:
542 unsigned int right_idx = (heap_idx << 1) + 1;
543 if (right_idx < heap->size && heap->entries[right_idx].version > version)
545 heap_idx = right_idx;
546 idx = heap->entries[heap_idx].idx;
547 if (constant_locations[idx] != -1)
548 GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
550 stack[stack_idx++] = HEAP_NODE_POP;
551 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
552 break;
556 case HEAP_NODE_POP:
557 heap_idx >>= 1;
558 --stack_idx;
559 break;
562 checkGLcall("walk_constant_heap()");
565 /* Context activation is done by the caller. */
566 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
568 GLfloat clamped_constant[4];
570 if (location == -1) return;
572 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
573 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
574 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
575 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
577 GL_EXTCALL(glUniform4fvARB(location, 1, clamped_constant));
580 /* Context activation is done by the caller. */
581 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
582 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
584 int stack_idx = 0;
585 unsigned int heap_idx = 1;
586 unsigned int idx;
588 if (heap->entries[heap_idx].version <= version) return;
590 idx = heap->entries[heap_idx].idx;
591 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
592 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
594 while (stack_idx >= 0)
596 /* Note that we fall through to the next case statement. */
597 switch(stack[stack_idx])
599 case HEAP_NODE_TRAVERSE_LEFT:
601 unsigned int left_idx = heap_idx << 1;
602 if (left_idx < heap->size && heap->entries[left_idx].version > version)
604 heap_idx = left_idx;
605 idx = heap->entries[heap_idx].idx;
606 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
608 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
609 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
610 break;
614 case HEAP_NODE_TRAVERSE_RIGHT:
616 unsigned int right_idx = (heap_idx << 1) + 1;
617 if (right_idx < heap->size && heap->entries[right_idx].version > version)
619 heap_idx = right_idx;
620 idx = heap->entries[heap_idx].idx;
621 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
623 stack[stack_idx++] = HEAP_NODE_POP;
624 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
625 break;
629 case HEAP_NODE_POP:
630 heap_idx >>= 1;
631 --stack_idx;
632 break;
635 checkGLcall("walk_constant_heap_clamped()");
638 /* Context activation is done by the caller. */
639 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
640 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
641 unsigned char *stack, UINT version)
643 const struct wined3d_shader_lconst *lconst;
645 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
646 if (shader->reg_maps.shader_version.major == 1
647 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
648 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
649 else
650 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
652 if (!shader->load_local_constsF)
654 TRACE("No need to load local float constants for this shader\n");
655 return;
658 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
659 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
661 GL_EXTCALL(glUniform4fvARB(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
663 checkGLcall("glUniform4fvARB()");
666 /* Context activation is done by the caller. */
667 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
668 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
670 unsigned int i;
671 struct list* ptr;
673 for (i = 0; constants_set; constants_set >>= 1, ++i)
675 if (!(constants_set & 1)) continue;
677 TRACE_(d3d_constants)("Loading constants %u: %i, %i, %i, %i\n",
678 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
680 /* We found this uniform name in the program - go ahead and send the data */
681 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
682 checkGLcall("glUniform4ivARB");
685 /* Load immediate constants */
686 ptr = list_head(&shader->constantsI);
687 while (ptr)
689 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
690 unsigned int idx = lconst->idx;
691 const GLint *values = (const GLint *)lconst->value;
693 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
694 values[0], values[1], values[2], values[3]);
696 /* We found this uniform name in the program - go ahead and send the data */
697 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
698 checkGLcall("glUniform4ivARB");
699 ptr = list_next(&shader->constantsI, ptr);
703 /* Context activation is done by the caller. */
704 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
705 GLhandleARB programId, const BOOL *constants, WORD constants_set)
707 GLint tmp_loc;
708 unsigned int i;
709 char tmp_name[10];
710 const char *prefix;
711 struct list* ptr;
713 prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
715 /* TODO: Benchmark and see if it would be beneficial to store the
716 * locations of the constants to avoid looking up each time */
717 for (i = 0; constants_set; constants_set >>= 1, ++i)
719 if (!(constants_set & 1)) continue;
721 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i]);
723 /* TODO: Benchmark and see if it would be beneficial to store the
724 * locations of the constants to avoid looking up each time */
725 snprintf(tmp_name, sizeof(tmp_name), "%s_b[%i]", prefix, i);
726 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
727 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
730 /* Load immediate constants */
731 ptr = list_head(&shader->constantsB);
732 while (ptr)
734 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
735 unsigned int idx = lconst->idx;
736 const GLint *values = (const GLint *)lconst->value;
738 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
740 snprintf(tmp_name, sizeof(tmp_name), "%s_b[%i]", prefix, idx);
741 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
742 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
743 ptr = list_next(&shader->constantsB, ptr);
746 checkGLcall("shader_glsl_load_constantsB()");
749 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
751 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
754 /* Context activation is done by the caller (state handler). */
755 static void shader_glsl_load_np2fixup_constants(void *shader_priv,
756 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
758 struct shader_glsl_priv *glsl_priv = shader_priv;
759 const struct glsl_shader_prog_link *prog = glsl_priv->glsl_program;
761 /* No GLSL program set - nothing to do. */
762 if (!prog) return;
764 /* NP2 texcoord fixup is (currently) only done for pixelshaders. */
765 if (!use_ps(state)) return;
767 if (prog->ps.np2_fixup_info && prog->ps.np2_fixup_location != -1)
769 UINT i;
770 UINT fixup = prog->ps.np2_fixup_info->active;
771 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
773 for (i = 0; fixup; fixup >>= 1, ++i)
775 const struct wined3d_texture *tex = state->textures[i];
776 const unsigned char idx = prog->ps.np2_fixup_info->idx[i];
777 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
779 if (!tex)
781 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
782 continue;
785 if (idx % 2)
787 tex_dim[2] = tex->pow2_matrix[0];
788 tex_dim[3] = tex->pow2_matrix[5];
790 else
792 tex_dim[0] = tex->pow2_matrix[0];
793 tex_dim[1] = tex->pow2_matrix[5];
797 GL_EXTCALL(glUniform4fvARB(prog->ps.np2_fixup_location,
798 prog->ps.np2_fixup_info->num_consts, np2fixup_constants));
802 /* Context activation is done by the caller (state handler). */
803 static void shader_glsl_load_constants(void *shader_priv, const struct wined3d_context *context,
804 const struct wined3d_state *state)
806 const struct wined3d_gl_info *gl_info = context->gl_info;
807 struct shader_glsl_priv *priv = shader_priv;
808 float position_fixup[4];
810 GLhandleARB programId;
811 struct glsl_shader_prog_link *prog = priv->glsl_program;
812 UINT constant_version;
813 int i;
815 if (!prog) {
816 /* No GLSL program set - nothing to do. */
817 return;
819 programId = prog->programId;
820 constant_version = prog->constant_version;
822 if (use_vs(state))
824 const struct wined3d_shader *vshader = state->vertex_shader;
826 /* Load DirectX 9 float constants/uniforms for vertex shader */
827 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
828 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
830 /* Load DirectX 9 integer constants/uniforms for vertex shader */
831 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
832 vshader->reg_maps.integer_constants);
834 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
835 shader_glsl_load_constantsB(vshader, gl_info, programId, state->vs_consts_b,
836 vshader->reg_maps.boolean_constants);
838 /* Upload the position fixup params */
839 shader_get_position_fixup(context, state, position_fixup);
840 GL_EXTCALL(glUniform4fvARB(prog->vs.pos_fixup_location, 1, position_fixup));
841 checkGLcall("glUniform4fvARB");
844 if (use_ps(state))
846 const struct wined3d_shader *pshader = state->pixel_shader;
848 /* Load DirectX 9 float constants/uniforms for pixel shader */
849 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
850 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
852 /* Load DirectX 9 integer constants/uniforms for pixel shader */
853 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
854 pshader->reg_maps.integer_constants);
856 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
857 shader_glsl_load_constantsB(pshader, gl_info, programId, state->ps_consts_b,
858 pshader->reg_maps.boolean_constants);
860 /* Upload the environment bump map matrix if needed. The needsbumpmat
861 * member specifies the texture stage to load the matrix from. It
862 * can't be 0 for a valid texbem instruction. */
863 for (i = 0; i < MAX_TEXTURES; ++i)
865 const float *data;
867 if (prog->ps.bumpenv_mat_location[i] == -1)
868 continue;
870 data = (const float *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00];
871 GL_EXTCALL(glUniformMatrix2fvARB(prog->ps.bumpenv_mat_location[i], 1, 0, data));
872 checkGLcall("glUniformMatrix2fvARB");
874 /* texbeml needs the luminance scale and offset too. If texbeml
875 * is used, needsbumpmat is set too, so we can check that in the
876 * needsbumpmat check. */
877 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
879 const GLfloat *scale = (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE];
880 const GLfloat *offset = (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET];
882 GL_EXTCALL(glUniform1fvARB(prog->ps.bumpenv_lum_scale_location[i], 1, scale));
883 checkGLcall("glUniform1fvARB");
884 GL_EXTCALL(glUniform1fvARB(prog->ps.bumpenv_lum_offset_location[i], 1, offset));
885 checkGLcall("glUniform1fvARB");
889 if (prog->ps.ycorrection_location != -1)
891 float correction_params[4];
893 if (context->render_offscreen)
895 correction_params[0] = 0.0f;
896 correction_params[1] = 1.0f;
897 } else {
898 /* position is window relative, not viewport relative */
899 correction_params[0] = (float) context->current_rt->resource.height;
900 correction_params[1] = -1.0f;
902 GL_EXTCALL(glUniform4fvARB(prog->ps.ycorrection_location, 1, correction_params));
905 else if (priv->fragment_pipe == &glsl_fragment_pipe)
907 float col[4];
909 for (i = 0; i < MAX_TEXTURES; ++i)
911 GL_EXTCALL(glUniformMatrix2fvARB(prog->ps.bumpenv_mat_location[i], 1, 0,
912 (const float *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
913 GL_EXTCALL(glUniform1fARB(prog->ps.bumpenv_lum_scale_location[i],
914 *(const float *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
915 GL_EXTCALL(glUniform1fARB(prog->ps.bumpenv_lum_offset_location[i],
916 *(const float *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
919 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_TEXTUREFACTOR], col);
920 GL_EXTCALL(glUniform4fARB(prog->ps.tex_factor_location, col[0], col[1], col[2], col[3]));
922 if (state->render_states[WINED3D_RS_SPECULARENABLE])
923 GL_EXTCALL(glUniform4fARB(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
924 else
925 GL_EXTCALL(glUniform4fARB(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
927 checkGLcall("fixed function uniforms");
930 if (priv->next_constant_version == UINT_MAX)
932 TRACE("Max constant version reached, resetting to 0.\n");
933 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
934 priv->next_constant_version = 1;
936 else
938 prog->constant_version = priv->next_constant_version++;
942 static void update_heap_entry(const struct constant_heap *heap, unsigned int idx,
943 unsigned int heap_idx, DWORD new_version)
945 struct constant_entry *entries = heap->entries;
946 unsigned int *positions = heap->positions;
947 unsigned int parent_idx;
949 while (heap_idx > 1)
951 parent_idx = heap_idx >> 1;
953 if (new_version <= entries[parent_idx].version) break;
955 entries[heap_idx] = entries[parent_idx];
956 positions[entries[parent_idx].idx] = heap_idx;
957 heap_idx = parent_idx;
960 entries[heap_idx].version = new_version;
961 entries[heap_idx].idx = idx;
962 positions[idx] = heap_idx;
965 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
967 struct shader_glsl_priv *priv = device->shader_priv;
968 struct constant_heap *heap = &priv->vconst_heap;
969 UINT i;
971 for (i = start; i < count + start; ++i)
973 if (!device->stateBlock->changed.vertexShaderConstantsF[i])
974 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
975 else
976 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
980 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
982 struct shader_glsl_priv *priv = device->shader_priv;
983 struct constant_heap *heap = &priv->pconst_heap;
984 UINT i;
986 for (i = start; i < count + start; ++i)
988 if (!device->stateBlock->changed.pixelShaderConstantsF[i])
989 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
990 else
991 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
995 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
997 unsigned int ret = gl_info->limits.glsl_varyings / 4;
998 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
999 if(shader_major > 3) return ret;
1001 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1002 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1003 return ret;
1006 /** Generate the variable & register declarations for the GLSL output target */
1007 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1008 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
1009 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1011 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1012 const struct wined3d_state *state = &shader->device->stateBlock->state;
1013 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1014 const struct wined3d_gl_info *gl_info = context->gl_info;
1015 const struct wined3d_fb_state *fb = &shader->device->fb;
1016 unsigned int i, extra_constants_needed = 0;
1017 const struct wined3d_shader_lconst *lconst;
1018 const char *prefix;
1019 DWORD map;
1021 prefix = shader_glsl_get_prefix(version->type);
1023 /* Prototype the subroutines */
1024 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1026 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1029 /* Declare the constants (aka uniforms) */
1030 if (shader->limits.constant_float > 0)
1032 unsigned max_constantsF;
1034 /* Unless the shader uses indirect addressing, always declare the
1035 * maximum array size and ignore that we need some uniforms privately.
1036 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1037 * and immediate values, still declare VC[256]. If the shader needs
1038 * more uniforms than we have it won't work in any case. If it uses
1039 * less, the compiler will figure out which uniforms are really used
1040 * and strip them out. This allows a shader to use c255 on a dx9 card,
1041 * as long as it doesn't also use all the other constants.
1043 * If the shader uses indirect addressing the compiler must assume
1044 * that all declared uniforms are used. In this case, declare only the
1045 * amount that we're assured to have.
1047 * Thus we run into problems in these two cases:
1048 * 1) The shader really uses more uniforms than supported.
1049 * 2) The shader uses indirect addressing, less constants than
1050 * supported, but uses a constant index > #supported consts. */
1051 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1053 /* No indirect addressing here. */
1054 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1056 else
1058 if (reg_maps->usesrelconstF)
1060 /* Subtract the other potential uniforms from the max
1061 * available (bools, ints, and 1 row of projection matrix).
1062 * Subtract another uniform for immediate values, which have
1063 * to be loaded via uniform by the driver as well. The shader
1064 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1065 * shader code, so one vec4 should be enough. (Unfortunately
1066 * the Nvidia driver doesn't store 128 and -128 in one float).
1068 * Writing gl_ClipVertex requires one uniform for each
1069 * clipplane as well. */
1070 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1071 if(ctx_priv->cur_vs_args->clip_enabled)
1073 max_constantsF -= gl_info->limits.clipplanes;
1075 max_constantsF -= count_bits(reg_maps->integer_constants);
1076 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1077 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1078 * for now take this into account when calculating the number of available constants
1080 max_constantsF -= count_bits(reg_maps->boolean_constants);
1081 /* Set by driver quirks in directx.c */
1082 max_constantsF -= gl_info->reserved_glsl_constants;
1084 if (max_constantsF < shader->limits.constant_float)
1086 static unsigned int once;
1088 if (!once++)
1089 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1090 " it may not render correctly.\n");
1091 else
1092 WARN("The hardware does not support enough uniform components to run this shader.\n");
1095 else
1097 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1100 max_constantsF = min(shader->limits.constant_float, max_constantsF);
1101 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1104 /* Always declare the full set of constants, the compiler can remove the
1105 * unused ones because d3d doesn't (yet) support indirect int and bool
1106 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1107 if (shader->limits.constant_int > 0 && reg_maps->integer_constants)
1108 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits.constant_int);
1110 if (shader->limits.constant_bool > 0 && reg_maps->boolean_constants)
1111 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits.constant_bool);
1113 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1115 if (reg_maps->cb_sizes[i])
1116 shader_addline(buffer, "uniform vec4 %s_cb%u[%u];\n", prefix, i, reg_maps->cb_sizes[i]);
1119 /* Declare texture samplers */
1120 for (i = 0; i < shader->limits.sampler; ++i)
1122 if (reg_maps->sampler_type[i])
1124 BOOL shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1 << i));
1125 BOOL tex_rect;
1127 switch (reg_maps->sampler_type[i])
1129 case WINED3DSTT_1D:
1130 if (shadow_sampler)
1131 shader_addline(buffer, "uniform sampler1DShadow %s_sampler%u;\n", prefix, i);
1132 else
1133 shader_addline(buffer, "uniform sampler1D %s_sampler%u;\n", prefix, i);
1134 break;
1135 case WINED3DSTT_2D:
1136 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->np2_fixup & (1 << i));
1137 tex_rect = tex_rect && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1138 if (shadow_sampler)
1140 if (tex_rect)
1141 shader_addline(buffer, "uniform sampler2DRectShadow %s_sampler%u;\n", prefix, i);
1142 else
1143 shader_addline(buffer, "uniform sampler2DShadow %s_sampler%u;\n", prefix, i);
1145 else
1147 if (tex_rect)
1148 shader_addline(buffer, "uniform sampler2DRect %s_sampler%u;\n", prefix, i);
1149 else
1150 shader_addline(buffer, "uniform sampler2D %s_sampler%u;\n", prefix, i);
1152 break;
1153 case WINED3DSTT_CUBE:
1154 if (shadow_sampler)
1155 FIXME("Unsupported Cube shadow sampler.\n");
1156 shader_addline(buffer, "uniform samplerCube %s_sampler%u;\n", prefix, i);
1157 break;
1158 case WINED3DSTT_VOLUME:
1159 if (shadow_sampler)
1160 FIXME("Unsupported 3D shadow sampler.\n");
1161 shader_addline(buffer, "uniform sampler3D %s_sampler%u;\n", prefix, i);
1162 break;
1163 default:
1164 shader_addline(buffer, "uniform unsupported_sampler %s_sampler%u;\n", prefix, i);
1165 FIXME("Unrecognized sampler type: %#x\n", reg_maps->sampler_type[i]);
1166 break;
1171 /* Declare uniforms for NP2 texcoord fixup:
1172 * This is NOT done inside the loop that declares the texture samplers
1173 * since the NP2 fixup code is currently only used for the GeforceFX
1174 * series and when forcing the ARB_npot extension off. Modern cards just
1175 * skip the code anyway, so put it inside a separate loop. */
1176 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1178 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1179 UINT cur = 0;
1181 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1182 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1183 * samplerNP2Fixup stores texture dimensions and is updated through
1184 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1186 for (i = 0; i < shader->limits.sampler; ++i)
1188 if (reg_maps->sampler_type[i])
1190 if (!(ps_args->np2_fixup & (1 << i))) continue;
1192 if (WINED3DSTT_2D != reg_maps->sampler_type[i]) {
1193 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1194 continue;
1197 fixup->idx[i] = cur++;
1201 fixup->num_consts = (cur + 1) >> 1;
1202 fixup->active = ps_args->np2_fixup;
1203 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1206 /* Declare address variables */
1207 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1209 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1212 /* Declare texture coordinate temporaries and initialize them */
1213 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1215 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1218 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1220 /* Declare attributes. */
1221 for (i = 0, map = reg_maps->input_registers; map; map >>= 1, ++i)
1223 if (map & 1)
1224 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, i);
1227 shader_addline(buffer, "uniform vec4 posFixup;\n");
1228 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits.packed_output);
1230 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1232 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits.packed_input);
1234 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1236 if (version->major >= 3)
1238 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits.packed_input);
1240 if (use_vs(state))
1241 shader_addline(buffer, "varying vec4 %s_in[%u];\n", prefix, in_count);
1242 else
1243 /* TODO: Write a replacement shader for the fixed function
1244 * vertex pipeline, so this isn't needed. For fixed function
1245 * vertex processing + 3.0 pixel shader we need a separate
1246 * function in the pixel shader that reads the fixed function
1247 * color into the packed input registers. */
1248 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1251 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1253 if (!(map & 1))
1254 continue;
1256 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1258 if (reg_maps->luminanceparams & (1 << i))
1260 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1261 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1262 extra_constants_needed++;
1265 extra_constants_needed++;
1268 if (ps_args->srgb_correction)
1270 shader_addline(buffer, "const vec4 srgb_const0 = ");
1271 shader_glsl_append_imm_vec4(buffer, srgb_const0);
1272 shader_addline(buffer, ";\n");
1273 shader_addline(buffer, "const vec4 srgb_const1 = ");
1274 shader_glsl_append_imm_vec4(buffer, srgb_const1);
1275 shader_addline(buffer, ";\n");
1277 if (reg_maps->vpos || reg_maps->usesdsy)
1279 if (shader->limits.constant_float + extra_constants_needed
1280 + 1 < gl_info->limits.glsl_ps_float_constants)
1282 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1283 extra_constants_needed++;
1285 else
1287 float ycorrection[] =
1289 context->render_offscreen ? 0.0f : fb->render_targets[0]->resource.height,
1290 context->render_offscreen ? 1.0f : -1.0f,
1291 0.0f,
1292 0.0f,
1295 /* This happens because we do not have proper tracking of the
1296 * constant registers that are actually used, only the max
1297 * limit of the shader version. */
1298 FIXME("Cannot find a free uniform for vpos correction params\n");
1299 shader_addline(buffer, "const vec4 ycorrection = ");
1300 shader_glsl_append_imm_vec4(buffer, ycorrection);
1301 shader_addline(buffer, ";\n");
1303 shader_addline(buffer, "vec4 vpos;\n");
1307 /* Declare output register temporaries */
1308 if (shader->limits.packed_output)
1309 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits.packed_output);
1311 /* Declare temporary variables */
1312 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1314 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1317 /* Declare loop registers aLx */
1318 if (version->major < 4)
1320 for (i = 0; i < reg_maps->loop_depth; ++i)
1322 shader_addline(buffer, "int aL%u;\n", i);
1323 shader_addline(buffer, "int tmpInt%u;\n", i);
1327 /* Temporary variables for matrix operations */
1328 shader_addline(buffer, "vec4 tmp0;\n");
1329 shader_addline(buffer, "vec4 tmp1;\n");
1331 if (!shader->load_local_constsF)
1333 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1335 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
1336 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
1337 shader_addline(buffer, ";\n");
1341 /* Start the main program. */
1342 shader_addline(buffer, "void main()\n{\n");
1344 /* Direct3D applications expect integer vPos values, while OpenGL drivers
1345 * add approximately 0.5. This causes off-by-one problems as spotted by
1346 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
1347 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
1348 * causes precision troubles when we just subtract 0.5.
1350 * To deal with that, just floor() the position. This will eliminate the
1351 * fraction on all cards.
1353 * TODO: Test how this behaves with multisampling.
1355 * An advantage of floor is that it works even if the driver doesn't add
1356 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
1357 * to return in gl_FragCoord, even though coordinates specify the pixel
1358 * centers instead of the pixel corners. This code will behave correctly
1359 * on drivers that returns integer values. */
1360 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
1361 shader_addline(buffer,
1362 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1365 /*****************************************************************************
1366 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1368 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1369 ****************************************************************************/
1371 /* Prototypes */
1372 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1373 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1375 /** Used for opcode modifiers - They multiply the result by the specified amount */
1376 static const char * const shift_glsl_tab[] = {
1377 "", /* 0 (none) */
1378 "2.0 * ", /* 1 (x2) */
1379 "4.0 * ", /* 2 (x4) */
1380 "8.0 * ", /* 3 (x8) */
1381 "16.0 * ", /* 4 (x16) */
1382 "32.0 * ", /* 5 (x32) */
1383 "", /* 6 (x64) */
1384 "", /* 7 (x128) */
1385 "", /* 8 (d256) */
1386 "", /* 9 (d128) */
1387 "", /* 10 (d64) */
1388 "", /* 11 (d32) */
1389 "0.0625 * ", /* 12 (d16) */
1390 "0.125 * ", /* 13 (d8) */
1391 "0.25 * ", /* 14 (d4) */
1392 "0.5 * " /* 15 (d2) */
1395 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1396 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1397 const char *in_reg, const char *in_regswizzle, char *out_str)
1399 out_str[0] = 0;
1401 switch (src_modifier)
1403 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1404 case WINED3DSPSM_DW:
1405 case WINED3DSPSM_NONE:
1406 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1407 break;
1408 case WINED3DSPSM_NEG:
1409 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1410 break;
1411 case WINED3DSPSM_NOT:
1412 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1413 break;
1414 case WINED3DSPSM_BIAS:
1415 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1416 break;
1417 case WINED3DSPSM_BIASNEG:
1418 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1419 break;
1420 case WINED3DSPSM_SIGN:
1421 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1422 break;
1423 case WINED3DSPSM_SIGNNEG:
1424 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1425 break;
1426 case WINED3DSPSM_COMP:
1427 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1428 break;
1429 case WINED3DSPSM_X2:
1430 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1431 break;
1432 case WINED3DSPSM_X2NEG:
1433 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1434 break;
1435 case WINED3DSPSM_ABS:
1436 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1437 break;
1438 case WINED3DSPSM_ABSNEG:
1439 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1440 break;
1441 default:
1442 FIXME("Unhandled modifier %u\n", src_modifier);
1443 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1447 /** Writes the GLSL variable name that corresponds to the register that the
1448 * DX opcode parameter is trying to access */
1449 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1450 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1452 /* oPos, oFog and oPts in D3D */
1453 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
1455 const struct wined3d_shader *shader = ins->ctx->shader;
1456 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1457 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1458 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1459 const char *prefix = shader_glsl_get_prefix(version->type);
1460 struct glsl_src_param rel_param0, rel_param1;
1461 char imm_str[4][16];
1463 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
1464 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
1465 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
1466 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
1467 *is_color = FALSE;
1469 switch (reg->type)
1471 case WINED3DSPR_TEMP:
1472 sprintf(register_name, "R%u", reg->idx[0].offset);
1473 break;
1475 case WINED3DSPR_INPUT:
1476 /* vertex shaders */
1477 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1479 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1480 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx[0].offset))
1481 *is_color = TRUE;
1482 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
1483 break;
1486 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1488 if (reg->idx[0].rel_addr)
1490 if (reg->idx[1].rel_addr)
1491 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
1492 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1493 else
1494 sprintf(register_name, "gs_in[%s + %u][%u]",
1495 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
1497 else if (reg->idx[1].rel_addr)
1498 sprintf(register_name, "gs_in[%u][%s + %u]",
1499 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1500 else
1501 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
1502 break;
1505 /* pixel shaders >= 3.0 */
1506 if (version->major >= 3)
1508 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
1509 unsigned int in_count = vec4_varyings(version->major, gl_info);
1511 if (reg->idx[0].rel_addr)
1513 /* Removing a + 0 would be an obvious optimization, but
1514 * OS X doesn't see the NOP operation there. */
1515 if (idx)
1517 if (shader->u.ps.declared_in_count > in_count)
1519 sprintf(register_name,
1520 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
1521 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
1522 prefix, rel_param0.param_str, idx);
1524 else
1526 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
1529 else
1531 if (shader->u.ps.declared_in_count > in_count)
1533 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
1534 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
1535 prefix, rel_param0.param_str);
1537 else
1539 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
1543 else
1545 if (idx == in_count) sprintf(register_name, "gl_Color");
1546 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
1547 else sprintf(register_name, "%s_in[%u]", prefix, idx);
1550 else
1552 if (!reg->idx[0].offset)
1553 strcpy(register_name, "gl_Color");
1554 else
1555 strcpy(register_name, "gl_SecondaryColor");
1556 break;
1558 break;
1560 case WINED3DSPR_CONST:
1562 /* Relative addressing */
1563 if (reg->idx[0].rel_addr)
1565 if (reg->idx[0].offset)
1566 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
1567 else
1568 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
1570 else
1572 if (shader_constant_is_local(shader, reg->idx[0].offset))
1573 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
1574 else
1575 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
1578 break;
1580 case WINED3DSPR_CONSTINT:
1581 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
1582 break;
1584 case WINED3DSPR_CONSTBOOL:
1585 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
1586 break;
1588 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1589 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1590 sprintf(register_name, "T%u", reg->idx[0].offset);
1591 else
1592 sprintf(register_name, "A%u", reg->idx[0].offset);
1593 break;
1595 case WINED3DSPR_LOOP:
1596 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
1597 break;
1599 case WINED3DSPR_SAMPLER:
1600 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
1601 break;
1603 case WINED3DSPR_COLOROUT:
1604 if (reg->idx[0].offset >= gl_info->limits.buffers)
1605 WARN("Write to render target %u, only %d supported.\n",
1606 reg->idx[0].offset, gl_info->limits.buffers);
1608 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
1609 break;
1611 case WINED3DSPR_RASTOUT:
1612 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
1613 break;
1615 case WINED3DSPR_DEPTHOUT:
1616 sprintf(register_name, "gl_FragDepth");
1617 break;
1619 case WINED3DSPR_ATTROUT:
1620 if (!reg->idx[0].offset)
1621 sprintf(register_name, "%s_out[8]", prefix);
1622 else
1623 sprintf(register_name, "%s_out[9]", prefix);
1624 break;
1626 case WINED3DSPR_TEXCRDOUT:
1627 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1628 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
1629 break;
1631 case WINED3DSPR_MISCTYPE:
1632 if (!reg->idx[0].offset)
1634 /* vPos */
1635 sprintf(register_name, "vpos");
1637 else if (reg->idx[0].offset == 1)
1639 /* Note that gl_FrontFacing is a bool, while vFace is
1640 * a float for which the sign determines front/back */
1641 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1643 else
1645 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
1646 sprintf(register_name, "unrecognized_register");
1648 break;
1650 case WINED3DSPR_IMMCONST:
1651 switch (reg->immconst_type)
1653 case WINED3D_IMMCONST_SCALAR:
1654 switch (reg->data_type)
1656 case WINED3D_DATA_FLOAT:
1657 shader_glsl_ftoa(*(const float *)reg->immconst_data, register_name);
1658 break;
1659 case WINED3D_DATA_INT:
1660 sprintf(register_name, "%#x", reg->immconst_data[0]);
1661 break;
1662 case WINED3D_DATA_RESOURCE:
1663 case WINED3D_DATA_SAMPLER:
1664 case WINED3D_DATA_UINT:
1665 sprintf(register_name, "%#xu", reg->immconst_data[0]);
1666 break;
1667 default:
1668 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1669 break;
1671 break;
1673 case WINED3D_IMMCONST_VEC4:
1674 switch (reg->data_type)
1676 case WINED3D_DATA_FLOAT:
1677 shader_glsl_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
1678 shader_glsl_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
1679 shader_glsl_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
1680 shader_glsl_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
1681 sprintf(register_name, "vec4(%s, %s, %s, %s)",
1682 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
1683 break;
1684 case WINED3D_DATA_INT:
1685 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
1686 reg->immconst_data[0], reg->immconst_data[1],
1687 reg->immconst_data[2], reg->immconst_data[3]);
1688 break;
1689 case WINED3D_DATA_RESOURCE:
1690 case WINED3D_DATA_SAMPLER:
1691 case WINED3D_DATA_UINT:
1692 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
1693 reg->immconst_data[0], reg->immconst_data[1],
1694 reg->immconst_data[2], reg->immconst_data[3]);
1695 break;
1696 default:
1697 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1698 break;
1700 break;
1702 default:
1703 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
1704 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
1706 break;
1708 case WINED3DSPR_CONSTBUFFER:
1709 if (reg->idx[1].rel_addr)
1710 sprintf(register_name, "%s_cb%u[%s + %u]",
1711 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1712 else
1713 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
1714 break;
1716 case WINED3DSPR_PRIMID:
1717 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
1718 break;
1720 default:
1721 FIXME("Unhandled register type %#x.\n", reg->type);
1722 sprintf(register_name, "unrecognized_register");
1723 break;
1727 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1729 *str++ = '.';
1730 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1731 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1732 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1733 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1734 *str = '\0';
1737 /* Get the GLSL write mask for the destination register */
1738 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1740 DWORD mask = param->write_mask;
1742 if (shader_is_scalar(&param->reg))
1744 mask = WINED3DSP_WRITEMASK_0;
1745 *write_mask = '\0';
1747 else
1749 shader_glsl_write_mask_to_str(mask, write_mask);
1752 return mask;
1755 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1756 unsigned int size = 0;
1758 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1759 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1760 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1761 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1763 return size;
1766 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
1768 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1769 * but addressed as "rgba". To fix this we need to swap the register's x
1770 * and z components. */
1771 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1773 *str++ = '.';
1774 /* swizzle bits fields: wwzzyyxx */
1775 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
1776 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
1777 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
1778 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
1779 *str = '\0';
1782 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
1783 BOOL fixup, DWORD mask, char *swizzle_str)
1785 if (shader_is_scalar(&param->reg))
1786 *swizzle_str = '\0';
1787 else
1788 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
1791 /* From a given parameter token, generate the corresponding GLSL string.
1792 * Also, return the actual register name and swizzle in case the
1793 * caller needs this information as well. */
1794 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1795 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
1797 BOOL is_color = FALSE;
1798 char swizzle_str[6];
1800 glsl_src->reg_name[0] = '\0';
1801 glsl_src->param_str[0] = '\0';
1802 swizzle_str[0] = '\0';
1804 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
1805 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
1807 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
1809 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
1811 else
1813 char param_str[200];
1815 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, param_str);
1817 switch (wined3d_src->reg.data_type)
1819 case WINED3D_DATA_FLOAT:
1820 sprintf(glsl_src->param_str, "%s", param_str);
1821 break;
1822 case WINED3D_DATA_INT:
1823 sprintf(glsl_src->param_str, "floatBitsToInt(%s)", param_str);
1824 break;
1825 case WINED3D_DATA_RESOURCE:
1826 case WINED3D_DATA_SAMPLER:
1827 case WINED3D_DATA_UINT:
1828 sprintf(glsl_src->param_str, "floatBitsToUint(%s)", param_str);
1829 break;
1830 default:
1831 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
1832 sprintf(glsl_src->param_str, "%s", param_str);
1833 break;
1838 /* From a given parameter token, generate the corresponding GLSL string.
1839 * Also, return the actual register name and swizzle in case the
1840 * caller needs this information as well. */
1841 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1842 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
1844 BOOL is_color = FALSE;
1846 glsl_dst->mask_str[0] = '\0';
1847 glsl_dst->reg_name[0] = '\0';
1849 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
1850 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1853 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1854 static DWORD shader_glsl_append_dst_ext(struct wined3d_shader_buffer *buffer,
1855 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst)
1857 struct glsl_dst_param glsl_dst;
1858 DWORD mask;
1860 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
1862 switch (dst->reg.data_type)
1864 case WINED3D_DATA_FLOAT:
1865 shader_addline(buffer, "%s%s = %s(",
1866 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1867 break;
1868 case WINED3D_DATA_INT:
1869 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
1870 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1871 break;
1872 case WINED3D_DATA_RESOURCE:
1873 case WINED3D_DATA_SAMPLER:
1874 case WINED3D_DATA_UINT:
1875 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
1876 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1877 break;
1878 default:
1879 FIXME("Unhandled data type %#x.\n", dst->reg.data_type);
1880 shader_addline(buffer, "%s%s = %s(",
1881 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1882 break;
1886 return mask;
1889 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1890 static DWORD shader_glsl_append_dst(struct wined3d_shader_buffer *buffer, const struct wined3d_shader_instruction *ins)
1892 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
1895 /** Process GLSL instruction modifiers */
1896 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1898 struct glsl_dst_param dst_param;
1899 DWORD modifiers;
1901 if (!ins->dst_count) return;
1903 modifiers = ins->dst[0].modifiers;
1904 if (!modifiers) return;
1906 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
1908 if (modifiers & WINED3DSPDM_SATURATE)
1910 /* _SAT means to clamp the value of the register to between 0 and 1 */
1911 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1912 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1915 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
1917 FIXME("_centroid modifier not handled\n");
1920 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
1922 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1926 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
1928 switch (op)
1930 case WINED3D_SHADER_REL_OP_GT: return ">";
1931 case WINED3D_SHADER_REL_OP_EQ: return "==";
1932 case WINED3D_SHADER_REL_OP_GE: return ">=";
1933 case WINED3D_SHADER_REL_OP_LT: return "<";
1934 case WINED3D_SHADER_REL_OP_NE: return "!=";
1935 case WINED3D_SHADER_REL_OP_LE: return "<=";
1936 default:
1937 FIXME("Unrecognized operator %#x.\n", op);
1938 return "(\?\?)";
1942 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
1943 DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
1945 enum wined3d_sampler_texture_type sampler_type = ctx->reg_maps->sampler_type[sampler_idx];
1946 const struct wined3d_gl_info *gl_info = ctx->gl_info;
1947 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
1948 && (((const struct shader_glsl_ctx_priv *)ctx->backend_data)->cur_ps_args->shadow & (1 << sampler_idx));
1949 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
1950 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1951 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
1952 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
1954 /* Note that there's no such thing as a projected cube texture. */
1955 switch(sampler_type) {
1956 case WINED3DSTT_1D:
1957 if (shadow)
1959 if (lod)
1961 sample_function->name = projected ? "shadow1DProjLod" : "shadow1DLod";
1963 else if (grad)
1965 if (gl_info->supported[EXT_GPU_SHADER4])
1966 sample_function->name = projected ? "shadow1DProjGrad" : "shadow1DGrad";
1967 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1968 sample_function->name = projected ? "shadow1DProjGradARB" : "shadow1DGradARB";
1969 else
1971 FIXME("Unsupported 1D shadow grad function.\n");
1972 sample_function->name = "unsupported1DGrad";
1975 else
1977 sample_function->name = projected ? "shadow1DProj" : "shadow1D";
1979 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1981 else
1983 if (lod)
1985 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
1987 else if (grad)
1989 if (gl_info->supported[EXT_GPU_SHADER4])
1990 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
1991 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1992 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
1993 else
1995 FIXME("Unsupported 1D grad function.\n");
1996 sample_function->name = "unsupported1DGrad";
1999 else
2001 sample_function->name = projected ? "texture1DProj" : "texture1D";
2003 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
2005 break;
2007 case WINED3DSTT_2D:
2008 if (shadow)
2010 if (texrect)
2012 if (lod)
2014 sample_function->name = projected ? "shadow2DRectProjLod" : "shadow2DRectLod";
2016 else if (grad)
2018 if (gl_info->supported[EXT_GPU_SHADER4])
2019 sample_function->name = projected ? "shadow2DRectProjGrad" : "shadow2DRectGrad";
2020 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2021 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
2022 else
2024 FIXME("Unsupported RECT shadow grad function.\n");
2025 sample_function->name = "unsupported2DRectGrad";
2028 else
2030 sample_function->name = projected ? "shadow2DRectProj" : "shadow2DRect";
2033 else
2035 if (lod)
2037 sample_function->name = projected ? "shadow2DProjLod" : "shadow2DLod";
2039 else if (grad)
2041 if (gl_info->supported[EXT_GPU_SHADER4])
2042 sample_function->name = projected ? "shadow2DProjGrad" : "shadow2DGrad";
2043 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2044 sample_function->name = projected ? "shadow2DProjGradARB" : "shadow2DGradARB";
2045 else
2047 FIXME("Unsupported 2D shadow grad function.\n");
2048 sample_function->name = "unsupported2DGrad";
2051 else
2053 sample_function->name = projected ? "shadow2DProj" : "shadow2D";
2056 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2058 else
2060 if (texrect)
2062 if (lod)
2064 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
2066 else if (grad)
2068 if (gl_info->supported[EXT_GPU_SHADER4])
2069 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
2070 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2071 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
2072 else
2074 FIXME("Unsupported RECT grad function.\n");
2075 sample_function->name = "unsupported2DRectGrad";
2078 else
2080 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
2083 else
2085 if (lod)
2087 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
2089 else if (grad)
2091 if (gl_info->supported[EXT_GPU_SHADER4])
2092 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
2093 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2094 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
2095 else
2097 FIXME("Unsupported 2D grad function.\n");
2098 sample_function->name = "unsupported2DGrad";
2101 else
2103 sample_function->name = projected ? "texture2DProj" : "texture2D";
2106 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2108 break;
2110 case WINED3DSTT_CUBE:
2111 if (shadow)
2113 FIXME("Unsupported Cube shadow function.\n");
2114 sample_function->name = "unsupportedCubeShadow";
2115 sample_function->coord_mask = 0;
2117 else
2119 if (lod)
2121 sample_function->name = "textureCubeLod";
2123 else if (grad)
2125 if (gl_info->supported[EXT_GPU_SHADER4])
2126 sample_function->name = "textureCubeGrad";
2127 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2128 sample_function->name = "textureCubeGradARB";
2129 else
2131 FIXME("Unsupported Cube grad function.\n");
2132 sample_function->name = "unsupportedCubeGrad";
2135 else
2137 sample_function->name = "textureCube";
2139 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2141 break;
2143 case WINED3DSTT_VOLUME:
2144 if (shadow)
2146 FIXME("Unsupported 3D shadow function.\n");
2147 sample_function->name = "unsupported3DShadow";
2148 sample_function->coord_mask = 0;
2150 else
2152 if (lod)
2154 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
2156 else if (grad)
2158 if (gl_info->supported[EXT_GPU_SHADER4])
2159 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
2160 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2161 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
2162 else
2164 FIXME("Unsupported 3D grad function.\n");
2165 sample_function->name = "unsupported3DGrad";
2168 else
2170 sample_function->name = projected ? "texture3DProj" : "texture3D";
2172 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2174 break;
2176 default:
2177 sample_function->name = "";
2178 sample_function->coord_mask = 0;
2179 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
2180 break;
2184 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2185 BOOL sign_fixup, enum fixup_channel_source channel_source)
2187 switch(channel_source)
2189 case CHANNEL_SOURCE_ZERO:
2190 strcat(arguments, "0.0");
2191 break;
2193 case CHANNEL_SOURCE_ONE:
2194 strcat(arguments, "1.0");
2195 break;
2197 case CHANNEL_SOURCE_X:
2198 strcat(arguments, reg_name);
2199 strcat(arguments, ".x");
2200 break;
2202 case CHANNEL_SOURCE_Y:
2203 strcat(arguments, reg_name);
2204 strcat(arguments, ".y");
2205 break;
2207 case CHANNEL_SOURCE_Z:
2208 strcat(arguments, reg_name);
2209 strcat(arguments, ".z");
2210 break;
2212 case CHANNEL_SOURCE_W:
2213 strcat(arguments, reg_name);
2214 strcat(arguments, ".w");
2215 break;
2217 default:
2218 FIXME("Unhandled channel source %#x\n", channel_source);
2219 strcat(arguments, "undefined");
2220 break;
2223 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2226 static void shader_glsl_color_correction_ext(struct wined3d_shader_buffer *buffer,
2227 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2229 unsigned int mask_size, remaining;
2230 DWORD fixup_mask = 0;
2231 char arguments[256];
2232 char mask_str[6];
2234 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2235 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2236 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2237 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2238 if (!(mask &= fixup_mask))
2239 return;
2241 if (is_complex_fixup(fixup))
2243 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2244 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2245 return;
2248 shader_glsl_write_mask_to_str(mask, mask_str);
2249 mask_size = shader_glsl_get_write_mask_size(mask);
2251 arguments[0] = '\0';
2252 remaining = mask_size;
2253 if (mask & WINED3DSP_WRITEMASK_0)
2255 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2256 if (--remaining) strcat(arguments, ", ");
2258 if (mask & WINED3DSP_WRITEMASK_1)
2260 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2261 if (--remaining) strcat(arguments, ", ");
2263 if (mask & WINED3DSP_WRITEMASK_2)
2265 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2266 if (--remaining) strcat(arguments, ", ");
2268 if (mask & WINED3DSP_WRITEMASK_3)
2270 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2271 if (--remaining) strcat(arguments, ", ");
2274 if (mask_size > 1)
2275 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2276 else
2277 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2280 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2282 char reg_name[256];
2283 BOOL is_color;
2285 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2286 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2289 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2290 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2291 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2293 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2294 char dst_swizzle[6];
2295 struct color_fixup_desc fixup;
2296 BOOL np2_fixup = FALSE;
2297 va_list args;
2299 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2301 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2303 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2304 fixup = priv->cur_ps_args->color_fixup[sampler];
2306 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2307 if(bias) {
2308 FIXME("Biased sampling from NP2 textures is unsupported\n");
2309 } else {
2310 np2_fixup = TRUE;
2314 else
2316 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
2319 shader_glsl_append_dst(ins->ctx->buffer, ins);
2321 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2322 sample_function->name, shader_glsl_get_prefix(version->type), sampler);
2324 va_start(args, coord_reg_fmt);
2325 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2326 va_end(args);
2328 if(bias) {
2329 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2330 } else {
2331 if (np2_fixup) {
2332 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2333 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2335 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2336 (idx % 2) ? "zw" : "xy", dst_swizzle);
2337 } else if(dx && dy) {
2338 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2339 } else {
2340 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2344 if(!is_identity_fixup(fixup)) {
2345 shader_glsl_color_correction(ins, fixup);
2349 /*****************************************************************************
2350 * Begin processing individual instruction opcodes
2351 ****************************************************************************/
2353 static void shader_glsl_binop(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;
2359 const char *op;
2361 /* Determine the GLSL operator to use based on the opcode */
2362 switch (ins->handler_idx)
2364 case WINED3DSIH_ADD: op = "+"; break;
2365 case WINED3DSIH_AND: op = "&"; break;
2366 case WINED3DSIH_DIV: op = "/"; break;
2367 case WINED3DSIH_IADD: op = "+"; break;
2368 case WINED3DSIH_MUL: op = "*"; break;
2369 case WINED3DSIH_SUB: op = "-"; break;
2370 case WINED3DSIH_USHR: op = ">>"; break;
2371 case WINED3DSIH_XOR: op = "^"; break;
2372 default:
2373 op = "<unhandled operator>";
2374 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2375 break;
2378 write_mask = shader_glsl_append_dst(buffer, ins);
2379 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2380 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2381 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2384 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2386 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2387 struct glsl_src_param src0_param;
2388 struct glsl_src_param src1_param;
2389 unsigned int mask_size;
2390 DWORD write_mask;
2391 const char *op;
2393 write_mask = shader_glsl_append_dst(buffer, ins);
2394 mask_size = shader_glsl_get_write_mask_size(write_mask);
2395 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2396 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2398 if (mask_size > 1)
2400 switch (ins->handler_idx)
2402 case WINED3DSIH_EQ: op = "equal"; break;
2403 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2404 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2405 case WINED3DSIH_LT: op = "lessThan"; break;
2406 default:
2407 op = "<unhandled operator>";
2408 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2409 break;
2412 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2413 mask_size, op, src0_param.param_str, src1_param.param_str);
2415 else
2417 switch (ins->handler_idx)
2419 case WINED3DSIH_EQ: op = "=="; break;
2420 case WINED3DSIH_GE: op = ">="; break;
2421 case WINED3DSIH_IGE: op = ">="; break;
2422 case WINED3DSIH_LT: op = "<"; break;
2423 default:
2424 op = "<unhandled operator>";
2425 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2426 break;
2429 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2430 src0_param.param_str, op, src1_param.param_str);
2434 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2436 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2437 struct glsl_src_param src0_param;
2438 struct glsl_src_param src1_param;
2439 DWORD write_mask;
2441 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2442 * not, we can emulate it. */
2443 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2444 FIXME("64-bit integer multiplies not implemented.\n");
2446 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2448 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2449 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2450 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2452 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2453 src0_param.param_str, src1_param.param_str);
2457 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2459 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2460 struct glsl_src_param src0_param, src1_param;
2461 DWORD write_mask;
2463 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2466 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2468 char dst_mask[6];
2470 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2471 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2472 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2473 shader_addline(buffer, "tmp0%s = %s / %s;\n",
2474 dst_mask, src0_param.param_str, src1_param.param_str);
2476 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2477 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2478 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2479 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
2481 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
2482 shader_addline(buffer, "tmp0%s);\n", dst_mask);
2484 else
2486 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
2487 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2488 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2489 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
2492 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2494 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2495 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2496 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2497 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
2501 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2502 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2504 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2505 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2506 struct glsl_src_param src0_param;
2507 DWORD write_mask;
2509 write_mask = shader_glsl_append_dst(buffer, ins);
2510 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2512 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2513 * shader versions WINED3DSIO_MOVA is used for this. */
2514 if (ins->ctx->reg_maps->shader_version.major == 1
2515 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
2516 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2518 /* This is a simple floor() */
2519 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2520 if (mask_size > 1) {
2521 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2522 } else {
2523 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2526 else if(ins->handler_idx == WINED3DSIH_MOVA)
2528 /* We need to *round* to the nearest int here. */
2529 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2531 if (gl_info->supported[EXT_GPU_SHADER4])
2533 if (mask_size > 1)
2534 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2535 else
2536 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2538 else
2540 if (mask_size > 1)
2541 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2542 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2543 else
2544 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2545 src0_param.param_str, src0_param.param_str);
2548 else
2550 shader_addline(buffer, "%s);\n", src0_param.param_str);
2554 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2555 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2557 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2558 struct glsl_src_param src0_param;
2559 struct glsl_src_param src1_param;
2560 DWORD dst_write_mask, src_write_mask;
2561 unsigned int dst_size = 0;
2563 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2564 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2566 /* dp3 works on vec3, dp4 on vec4 */
2567 if (ins->handler_idx == WINED3DSIH_DP4)
2569 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2570 } else {
2571 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2574 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2575 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2577 if (dst_size > 1) {
2578 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2579 } else {
2580 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2584 /* Note that this instruction has some restrictions. The destination write mask
2585 * can't contain the w component, and the source swizzles have to be .xyzw */
2586 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2588 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2589 struct glsl_src_param src0_param;
2590 struct glsl_src_param src1_param;
2591 char dst_mask[6];
2593 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2594 shader_glsl_append_dst(ins->ctx->buffer, ins);
2595 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2596 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2597 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2600 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
2602 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
2605 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2606 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2607 * GLSL uses the value as-is. */
2608 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2610 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2611 struct glsl_src_param src0_param;
2612 struct glsl_src_param src1_param;
2613 DWORD dst_write_mask;
2614 unsigned int dst_size;
2616 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2617 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2619 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2620 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2622 if (dst_size > 1)
2624 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2625 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
2627 else
2629 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
2630 src1_param.param_str, src0_param.param_str, src1_param.param_str);
2634 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
2635 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
2636 * GLSL uses the value as-is. */
2637 static void shader_glsl_log(const struct wined3d_shader_instruction *ins)
2639 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2640 struct glsl_src_param src0_param;
2641 DWORD dst_write_mask;
2642 unsigned int dst_size;
2644 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2645 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2647 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2649 if (dst_size > 1)
2651 shader_addline(buffer, "vec%u(log2(abs(%s))));\n",
2652 dst_size, src0_param.param_str);
2654 else
2656 shader_addline(buffer, "log2(abs(%s)));\n",
2657 src0_param.param_str);
2661 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2662 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
2664 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2665 struct glsl_src_param src_param;
2666 const char *instruction;
2667 DWORD write_mask;
2668 unsigned i;
2670 /* Determine the GLSL function to use based on the opcode */
2671 /* TODO: Possibly make this a table for faster lookups */
2672 switch (ins->handler_idx)
2674 case WINED3DSIH_MIN: instruction = "min"; break;
2675 case WINED3DSIH_MAX: instruction = "max"; break;
2676 case WINED3DSIH_ABS: instruction = "abs"; break;
2677 case WINED3DSIH_FRC: instruction = "fract"; break;
2678 case WINED3DSIH_EXP: instruction = "exp2"; break;
2679 case WINED3DSIH_DSX: instruction = "dFdx"; break;
2680 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
2681 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
2682 default: instruction = "";
2683 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2684 break;
2687 write_mask = shader_glsl_append_dst(buffer, ins);
2689 shader_addline(buffer, "%s(", instruction);
2691 if (ins->src_count)
2693 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2694 shader_addline(buffer, "%s", src_param.param_str);
2695 for (i = 1; i < ins->src_count; ++i)
2697 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
2698 shader_addline(buffer, ", %s", src_param.param_str);
2702 shader_addline(buffer, "));\n");
2705 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
2707 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
2709 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2710 struct glsl_src_param src_param;
2711 unsigned int mask_size;
2712 DWORD write_mask;
2713 char dst_mask[6];
2715 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
2716 mask_size = shader_glsl_get_write_mask_size(write_mask);
2717 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2719 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
2720 src_param.param_str, src_param.param_str);
2721 shader_glsl_append_dst(buffer, ins);
2723 if (mask_size > 1)
2725 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
2726 mask_size, src_param.param_str);
2728 else
2730 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
2731 src_param.param_str);
2735 /** Process the WINED3DSIO_EXPP instruction in GLSL:
2736 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2737 * dst.x = 2^(floor(src))
2738 * dst.y = src - floor(src)
2739 * dst.z = 2^src (partial precision is allowed, but optional)
2740 * dst.w = 1.0;
2741 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2742 * dst = 2^src; (partial precision is allowed, but optional)
2744 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
2746 struct glsl_src_param src_param;
2748 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
2750 if (ins->ctx->reg_maps->shader_version.major < 2)
2752 char dst_mask[6];
2754 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
2755 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
2756 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
2757 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
2759 shader_glsl_append_dst(ins->ctx->buffer, ins);
2760 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2761 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
2762 } else {
2763 DWORD write_mask;
2764 unsigned int mask_size;
2766 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2767 mask_size = shader_glsl_get_write_mask_size(write_mask);
2769 if (mask_size > 1) {
2770 shader_addline(ins->ctx->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
2771 } else {
2772 shader_addline(ins->ctx->buffer, "exp2(%s));\n", src_param.param_str);
2777 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
2779 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2780 struct glsl_src_param src_param;
2781 unsigned int mask_size;
2782 DWORD write_mask;
2784 write_mask = shader_glsl_append_dst(buffer, ins);
2785 mask_size = shader_glsl_get_write_mask_size(write_mask);
2786 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2788 if (mask_size > 1)
2789 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
2790 else
2791 shader_addline(buffer, "int(%s));\n", src_param.param_str);
2794 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
2796 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2797 struct glsl_src_param src_param;
2798 unsigned int mask_size;
2799 DWORD write_mask;
2801 write_mask = shader_glsl_append_dst(buffer, ins);
2802 mask_size = shader_glsl_get_write_mask_size(write_mask);
2803 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2805 if (mask_size > 1)
2806 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
2807 else
2808 shader_addline(buffer, "float(%s));\n", src_param.param_str);
2811 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
2812 static void shader_glsl_rcp(const struct wined3d_shader_instruction *ins)
2814 struct glsl_src_param src_param;
2815 DWORD write_mask;
2816 unsigned int mask_size;
2818 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2819 mask_size = shader_glsl_get_write_mask_size(write_mask);
2820 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2822 if (mask_size > 1)
2824 shader_addline(ins->ctx->buffer, "vec%u(1.0 / %s));\n",
2825 mask_size, src_param.param_str);
2827 else
2829 shader_addline(ins->ctx->buffer, "1.0 / %s);\n",
2830 src_param.param_str);
2834 static void shader_glsl_rsq(const struct wined3d_shader_instruction *ins)
2836 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2837 struct glsl_src_param src_param;
2838 DWORD write_mask;
2839 unsigned int mask_size;
2841 write_mask = shader_glsl_append_dst(buffer, ins);
2842 mask_size = shader_glsl_get_write_mask_size(write_mask);
2844 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2846 if (mask_size > 1)
2848 shader_addline(buffer, "vec%u(inversesqrt(abs(%s))));\n",
2849 mask_size, src_param.param_str);
2851 else
2853 shader_addline(buffer, "inversesqrt(abs(%s)));\n",
2854 src_param.param_str);
2858 /** Process signed comparison opcodes in GLSL. */
2859 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
2861 struct glsl_src_param src0_param;
2862 struct glsl_src_param src1_param;
2863 DWORD write_mask;
2864 unsigned int mask_size;
2866 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2867 mask_size = shader_glsl_get_write_mask_size(write_mask);
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);
2871 if (mask_size > 1) {
2872 const char *compare;
2874 switch(ins->handler_idx)
2876 case WINED3DSIH_SLT: compare = "lessThan"; break;
2877 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
2878 default: compare = "";
2879 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2882 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
2883 src0_param.param_str, src1_param.param_str);
2884 } else {
2885 switch(ins->handler_idx)
2887 case WINED3DSIH_SLT:
2888 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2889 * to return 0.0 but step returns 1.0 because step is not < x
2890 * An alternative is a bvec compare padded with an unused second component.
2891 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2892 * issue. Playing with not() is not possible either because not() does not accept
2893 * a scalar.
2895 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
2896 src0_param.param_str, src1_param.param_str);
2897 break;
2898 case WINED3DSIH_SGE:
2899 /* Here we can use the step() function and safe a conditional */
2900 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
2901 break;
2902 default:
2903 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2909 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
2911 const char *condition_prefix, *condition_suffix;
2912 struct wined3d_shader_dst_param dst;
2913 struct glsl_src_param src0_param;
2914 struct glsl_src_param src1_param;
2915 struct glsl_src_param src2_param;
2916 BOOL temp_destination = FALSE;
2917 DWORD cmp_channel = 0;
2918 unsigned int i, j;
2919 char mask_char[6];
2920 DWORD write_mask;
2922 switch (ins->handler_idx)
2924 case WINED3DSIH_CMP:
2925 condition_prefix = "";
2926 condition_suffix = " >= 0.0";
2927 break;
2929 case WINED3DSIH_CND:
2930 condition_prefix = "";
2931 condition_suffix = " > 0.5";
2932 break;
2934 case WINED3DSIH_MOVC:
2935 condition_prefix = "bool(";
2936 condition_suffix = ")";
2937 break;
2939 default:
2940 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2941 condition_prefix = "<unhandled prefix>";
2942 condition_suffix = "<unhandled suffix>";
2943 break;
2946 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
2948 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2949 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2950 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2951 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2953 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2954 condition_prefix, src0_param.param_str, condition_suffix,
2955 src1_param.param_str, src2_param.param_str);
2956 return;
2959 dst = ins->dst[0];
2961 /* Splitting the instruction up in multiple lines imposes a problem:
2962 * The first lines may overwrite source parameters of the following lines.
2963 * Deal with that by using a temporary destination register if needed. */
2964 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
2965 && ins->src[0].reg.type == dst.reg.type)
2966 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
2967 && ins->src[1].reg.type == dst.reg.type)
2968 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
2969 && ins->src[2].reg.type == dst.reg.type))
2970 temp_destination = TRUE;
2972 /* Cycle through all source0 channels. */
2973 for (i = 0; i < 4; ++i)
2975 write_mask = 0;
2976 /* Find the destination channels which use the current source0 channel. */
2977 for (j = 0; j < 4; ++j)
2979 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2981 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2982 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2985 dst.write_mask = ins->dst[0].write_mask & write_mask;
2987 if (temp_destination)
2989 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
2990 continue;
2991 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
2993 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst)))
2994 continue;
2996 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2997 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2998 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3000 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3001 condition_prefix, src0_param.param_str, condition_suffix,
3002 src1_param.param_str, src2_param.param_str);
3005 if (temp_destination)
3007 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3008 shader_glsl_append_dst(ins->ctx->buffer, ins);
3009 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3013 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3014 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3015 * the compare is done per component of src0. */
3016 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3018 struct glsl_src_param src0_param;
3019 struct glsl_src_param src1_param;
3020 struct glsl_src_param src2_param;
3021 DWORD write_mask;
3022 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3023 ins->ctx->reg_maps->shader_version.minor);
3025 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3027 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3028 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3029 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3030 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3032 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
3033 if (ins->coissue)
3035 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3036 } else {
3037 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3038 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3040 return;
3043 shader_glsl_conditional_move(ins);
3046 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3047 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3049 struct glsl_src_param src0_param;
3050 struct glsl_src_param src1_param;
3051 struct glsl_src_param src2_param;
3052 DWORD write_mask;
3054 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3055 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3056 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3057 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3058 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3059 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3062 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3063 Vertex shaders to GLSL codes */
3064 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3066 int i;
3067 int nComponents = 0;
3068 struct wined3d_shader_dst_param tmp_dst = {{0}};
3069 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3070 struct wined3d_shader_instruction tmp_ins;
3072 memset(&tmp_ins, 0, sizeof(tmp_ins));
3074 /* Set constants for the temporary argument */
3075 tmp_ins.ctx = ins->ctx;
3076 tmp_ins.dst_count = 1;
3077 tmp_ins.dst = &tmp_dst;
3078 tmp_ins.src_count = 2;
3079 tmp_ins.src = tmp_src;
3081 switch(ins->handler_idx)
3083 case WINED3DSIH_M4x4:
3084 nComponents = 4;
3085 tmp_ins.handler_idx = WINED3DSIH_DP4;
3086 break;
3087 case WINED3DSIH_M4x3:
3088 nComponents = 3;
3089 tmp_ins.handler_idx = WINED3DSIH_DP4;
3090 break;
3091 case WINED3DSIH_M3x4:
3092 nComponents = 4;
3093 tmp_ins.handler_idx = WINED3DSIH_DP3;
3094 break;
3095 case WINED3DSIH_M3x3:
3096 nComponents = 3;
3097 tmp_ins.handler_idx = WINED3DSIH_DP3;
3098 break;
3099 case WINED3DSIH_M3x2:
3100 nComponents = 2;
3101 tmp_ins.handler_idx = WINED3DSIH_DP3;
3102 break;
3103 default:
3104 break;
3107 tmp_dst = ins->dst[0];
3108 tmp_src[0] = ins->src[0];
3109 tmp_src[1] = ins->src[1];
3110 for (i = 0; i < nComponents; ++i)
3112 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3113 shader_glsl_dot(&tmp_ins);
3114 ++tmp_src[1].reg.idx[0].offset;
3119 The LRP instruction performs a component-wise linear interpolation
3120 between the second and third operands using the first operand as the
3121 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3122 This is equivalent to mix(src2, src1, src0);
3124 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3126 struct glsl_src_param src0_param;
3127 struct glsl_src_param src1_param;
3128 struct glsl_src_param src2_param;
3129 DWORD write_mask;
3131 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3133 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3134 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3135 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3137 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3138 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3141 /** Process the WINED3DSIO_LIT instruction in GLSL:
3142 * dst.x = dst.w = 1.0
3143 * dst.y = (src0.x > 0) ? src0.x
3144 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3145 * where src.w is clamped at +- 128
3147 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3149 struct glsl_src_param src0_param;
3150 struct glsl_src_param src1_param;
3151 struct glsl_src_param src3_param;
3152 char dst_mask[6];
3154 shader_glsl_append_dst(ins->ctx->buffer, ins);
3155 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3157 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3158 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3159 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3161 /* The sdk specifies the instruction like this
3162 * dst.x = 1.0;
3163 * if(src.x > 0.0) dst.y = src.x
3164 * else dst.y = 0.0.
3165 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3166 * else dst.z = 0.0;
3167 * dst.w = 1.0;
3168 * (where power = src.w clamped between -128 and 128)
3170 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3171 * dst.x = 1.0 ... No further explanation needed
3172 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3173 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3174 * dst.w = 1.0. ... Nothing fancy.
3176 * So we still have one conditional in there. So do this:
3177 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3179 * 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),
3180 * 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.
3181 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3183 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3184 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3185 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3187 shader_addline(ins->ctx->buffer,
3188 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3189 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3190 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3191 src0_param.param_str, src3_param.param_str, dst_mask);
3194 /** Process the WINED3DSIO_DST instruction in GLSL:
3195 * dst.x = 1.0
3196 * dst.y = src0.x * src0.y
3197 * dst.z = src0.z
3198 * dst.w = src1.w
3200 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3202 struct glsl_src_param src0y_param;
3203 struct glsl_src_param src0z_param;
3204 struct glsl_src_param src1y_param;
3205 struct glsl_src_param src1w_param;
3206 char dst_mask[6];
3208 shader_glsl_append_dst(ins->ctx->buffer, ins);
3209 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3211 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3212 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3213 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3214 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3216 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3217 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3220 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3221 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3222 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3224 * dst.x = cos(src0.?)
3225 * dst.y = sin(src0.?)
3226 * dst.z = dst.z
3227 * dst.w = dst.w
3229 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3231 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3232 struct glsl_src_param src0_param;
3233 DWORD write_mask;
3235 if (ins->ctx->reg_maps->shader_version.major < 4)
3237 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3239 write_mask = shader_glsl_append_dst(buffer, ins);
3240 switch (write_mask)
3242 case WINED3DSP_WRITEMASK_0:
3243 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3244 break;
3246 case WINED3DSP_WRITEMASK_1:
3247 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3248 break;
3250 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3251 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3252 src0_param.param_str, src0_param.param_str);
3253 break;
3255 default:
3256 ERR("Write mask should be .x, .y or .xy\n");
3257 break;
3260 return;
3263 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3266 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3268 char dst_mask[6];
3270 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3271 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3272 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3274 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
3275 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3276 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3278 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
3279 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3281 else
3283 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
3284 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3285 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3288 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3290 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
3291 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3292 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3296 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3297 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3298 * generate invalid code
3300 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3302 struct glsl_src_param src0_param;
3303 DWORD write_mask;
3305 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3306 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3308 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3311 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3312 * Start a for() loop where src1.y is the initial value of aL,
3313 * increment aL by src1.z for a total of src1.x iterations.
3314 * Need to use a temporary variable for this operation.
3316 /* FIXME: I don't think nested loops will work correctly this way. */
3317 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3319 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3320 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3321 const struct wined3d_shader *shader = ins->ctx->shader;
3322 const struct wined3d_shader_lconst *constant;
3323 struct glsl_src_param src1_param;
3324 const DWORD *control_values = NULL;
3326 if (ins->ctx->reg_maps->shader_version.major < 4)
3328 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3330 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3331 * class hardware doesn't support real varying indexing, but Microsoft
3332 * designed this feature for Shader model 2.x+. If the loop control is
3333 * known at compile time, the GLSL compiler can unroll the loop, and
3334 * replace indirect addressing with direct addressing. */
3335 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3337 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3339 if (constant->idx == ins->src[1].reg.idx[0].offset)
3341 control_values = constant->value;
3342 break;
3347 if (control_values)
3349 struct wined3d_shader_loop_control loop_control;
3350 loop_control.count = control_values[0];
3351 loop_control.start = control_values[1];
3352 loop_control.step = (int)control_values[2];
3354 if (loop_control.step > 0)
3356 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3357 loop_state->current_depth, loop_control.start,
3358 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3359 loop_state->current_depth, loop_control.step);
3361 else if (loop_control.step < 0)
3363 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3364 loop_state->current_depth, loop_control.start,
3365 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3366 loop_state->current_depth, loop_control.step);
3368 else
3370 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3371 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3372 loop_state->current_depth, loop_control.count,
3373 loop_state->current_depth);
3376 else
3378 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3379 loop_state->current_depth, loop_state->current_reg,
3380 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3381 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3384 ++loop_state->current_reg;
3386 else
3388 shader_addline(buffer, "for (;;)\n{\n");
3391 ++loop_state->current_depth;
3394 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3396 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3398 shader_addline(ins->ctx->buffer, "}\n");
3400 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3402 --loop_state->current_depth;
3403 --loop_state->current_reg;
3406 if (ins->handler_idx == WINED3DSIH_ENDREP)
3408 --loop_state->current_depth;
3412 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3414 const struct wined3d_shader *shader = ins->ctx->shader;
3415 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3416 const struct wined3d_shader_lconst *constant;
3417 struct glsl_src_param src0_param;
3418 const DWORD *control_values = NULL;
3420 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3421 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3423 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3425 if (constant->idx == ins->src[0].reg.idx[0].offset)
3427 control_values = constant->value;
3428 break;
3433 if (control_values)
3435 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3436 loop_state->current_depth, loop_state->current_depth,
3437 control_values[0], loop_state->current_depth);
3439 else
3441 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3442 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3443 loop_state->current_depth, loop_state->current_depth,
3444 src0_param.param_str, loop_state->current_depth);
3447 ++loop_state->current_depth;
3450 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3452 struct glsl_src_param src0_param;
3454 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3455 shader_addline(ins->ctx->buffer, "if (%s) {\n", src0_param.param_str);
3458 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3460 struct glsl_src_param src0_param;
3461 struct glsl_src_param src1_param;
3463 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3464 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3466 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3467 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3470 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3472 shader_addline(ins->ctx->buffer, "} else {\n");
3475 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3477 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3480 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3482 shader_addline(ins->ctx->buffer, "break;\n");
3485 /* FIXME: According to MSDN the compare is done per component. */
3486 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3488 struct glsl_src_param src0_param;
3489 struct glsl_src_param src1_param;
3491 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3492 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3494 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3495 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3498 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
3500 struct glsl_src_param src_param;
3502 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
3503 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
3506 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
3508 shader_addline(ins->ctx->buffer, "}\n");
3509 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
3512 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
3514 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
3517 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3519 struct glsl_src_param src1_param;
3521 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3522 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
3523 src1_param.param_str, ins->src[0].reg.idx[0].offset);
3526 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3528 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3529 * function only suppresses the unhandled instruction warning
3533 /*********************************************
3534 * Pixel Shader Specific Code begins here
3535 ********************************************/
3536 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3538 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3539 ins->ctx->reg_maps->shader_version.minor);
3540 struct glsl_sample_function sample_function;
3541 DWORD sample_flags = 0;
3542 DWORD sampler_idx;
3543 DWORD mask = 0, swizzle;
3544 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3546 /* 1.0-1.4: Use destination register as sampler source.
3547 * 2.0+: Use provided sampler source. */
3548 if (shader_version < WINED3D_SHADER_VERSION(2,0))
3549 sampler_idx = ins->dst[0].reg.idx[0].offset;
3550 else
3551 sampler_idx = ins->src[1].reg.idx[0].offset;
3553 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3555 DWORD flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3556 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3557 enum wined3d_sampler_texture_type sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3559 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3560 if (flags & WINED3D_PSARGS_PROJECTED && sampler_type != WINED3DSTT_CUBE)
3562 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3563 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3565 case WINED3D_TTFF_COUNT1:
3566 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3567 break;
3568 case WINED3D_TTFF_COUNT2:
3569 mask = WINED3DSP_WRITEMASK_1;
3570 break;
3571 case WINED3D_TTFF_COUNT3:
3572 mask = WINED3DSP_WRITEMASK_2;
3573 break;
3574 case WINED3D_TTFF_COUNT4:
3575 case WINED3D_TTFF_DISABLE:
3576 mask = WINED3DSP_WRITEMASK_3;
3577 break;
3581 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3583 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3585 if (src_mod == WINED3DSPSM_DZ) {
3586 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3587 mask = WINED3DSP_WRITEMASK_2;
3588 } else if (src_mod == WINED3DSPSM_DW) {
3589 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3590 mask = WINED3DSP_WRITEMASK_3;
3593 else
3595 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
3596 && ins->ctx->reg_maps->sampler_type[sampler_idx] != WINED3DSTT_CUBE)
3598 /* ps 2.0 texldp instruction always divides by the fourth component. */
3599 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3600 mask = WINED3DSP_WRITEMASK_3;
3604 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3605 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3607 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3608 mask |= sample_function.coord_mask;
3610 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3611 else swizzle = ins->src[1].swizzle;
3613 /* 1.0-1.3: Use destination register as coordinate source.
3614 1.4+: Use provided coordinate source register. */
3615 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3617 char coord_mask[6];
3618 shader_glsl_write_mask_to_str(mask, coord_mask);
3619 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3620 "T%u%s", sampler_idx, coord_mask);
3622 else
3624 struct glsl_src_param coord_param;
3625 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3626 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3628 struct glsl_src_param bias;
3629 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3630 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3631 "%s", coord_param.param_str);
3632 } else {
3633 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3634 "%s", coord_param.param_str);
3639 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3641 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3642 struct glsl_src_param coord_param, dx_param, dy_param;
3643 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3644 struct glsl_sample_function sample_function;
3645 DWORD sampler_idx;
3646 DWORD swizzle = ins->src[1].swizzle;
3647 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3649 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3651 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3652 shader_glsl_tex(ins);
3653 return;
3656 sampler_idx = ins->src[1].reg.idx[0].offset;
3657 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3658 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3660 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3661 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3662 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3663 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3665 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3666 "%s", coord_param.param_str);
3669 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3671 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3672 struct glsl_src_param coord_param, lod_param;
3673 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3674 struct glsl_sample_function sample_function;
3675 DWORD sampler_idx;
3676 DWORD swizzle = ins->src[1].swizzle;
3677 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3679 sampler_idx = ins->src[1].reg.idx[0].offset;
3680 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3681 && priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3682 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3684 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3685 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3687 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3689 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3690 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
3692 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
3693 * However, the NVIDIA drivers allow them in fragment shaders as well,
3694 * even without the appropriate extension. */
3695 WARN("Using %s in fragment shader.\n", sample_function.name);
3697 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3698 "%s", coord_param.param_str);
3701 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3703 /* FIXME: Make this work for more than just 2D textures */
3704 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3705 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3707 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3709 char dst_mask[6];
3711 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3712 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3713 ins->dst[0].reg.idx[0].offset, dst_mask);
3715 else
3717 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3718 DWORD reg = ins->src[0].reg.idx[0].offset;
3719 char dst_swizzle[6];
3721 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3723 if (src_mod == WINED3DSPSM_DZ)
3725 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3726 struct glsl_src_param div_param;
3728 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3730 if (mask_size > 1) {
3731 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3732 } else {
3733 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3736 else if (src_mod == WINED3DSPSM_DW)
3738 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3739 struct glsl_src_param div_param;
3741 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3743 if (mask_size > 1) {
3744 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3745 } else {
3746 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3748 } else {
3749 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3754 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3755 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3756 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3757 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3759 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3760 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3761 struct glsl_sample_function sample_function;
3762 struct glsl_src_param src0_param;
3763 UINT mask_size;
3765 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3767 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3768 * scalar, and projected sampling would require 4.
3770 * It is a dependent read - not valid with conditional NP2 textures
3772 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3773 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3775 switch(mask_size)
3777 case 1:
3778 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3779 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3780 break;
3782 case 2:
3783 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3784 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3785 break;
3787 case 3:
3788 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3789 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3790 break;
3792 default:
3793 FIXME("Unexpected mask size %u\n", mask_size);
3794 break;
3798 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3799 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3800 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3802 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3803 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3804 struct glsl_src_param src0_param;
3805 DWORD dst_mask;
3806 unsigned int mask_size;
3808 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3809 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3810 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3812 if (mask_size > 1) {
3813 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3814 } else {
3815 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3819 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3820 * Calculate the depth as dst.x / dst.y */
3821 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3823 struct glsl_dst_param dst_param;
3825 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3827 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3828 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3829 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3830 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3831 * >= 1.0 or < 0.0
3833 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3834 dst_param.reg_name, dst_param.reg_name);
3837 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3838 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3839 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3840 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3842 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3844 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3845 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3846 struct glsl_src_param src0_param;
3848 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3850 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3851 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3854 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3855 * Calculate the 1st of a 2-row matrix multiplication. */
3856 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3858 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3859 DWORD reg = ins->dst[0].reg.idx[0].offset;
3860 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3861 struct glsl_src_param src0_param;
3863 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3864 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3867 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3868 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3869 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3871 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3872 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3873 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3874 DWORD reg = ins->dst[0].reg.idx[0].offset;
3875 struct glsl_src_param src0_param;
3877 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3878 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
3879 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
3882 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3884 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3885 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3886 struct glsl_sample_function sample_function;
3887 DWORD reg = ins->dst[0].reg.idx[0].offset;
3888 struct glsl_src_param src0_param;
3890 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3891 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3893 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3895 /* Sample the texture using the calculated coordinates */
3896 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
3899 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3900 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3901 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3903 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3904 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3905 struct glsl_sample_function sample_function;
3906 DWORD reg = ins->dst[0].reg.idx[0].offset;
3907 struct glsl_src_param src0_param;
3909 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3910 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3912 /* Dependent read, not valid with conditional NP2 */
3913 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3915 /* Sample the texture using the calculated coordinates */
3916 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3918 tex_mx->current_row = 0;
3921 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3922 * Perform the 3rd row of a 3x3 matrix multiply */
3923 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
3925 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3926 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3927 DWORD reg = ins->dst[0].reg.idx[0].offset;
3928 struct glsl_src_param src0_param;
3929 char dst_mask[6];
3931 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3933 shader_glsl_append_dst(ins->ctx->buffer, ins);
3934 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3935 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
3937 tex_mx->current_row = 0;
3940 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3941 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3942 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
3944 struct glsl_src_param src0_param;
3945 struct glsl_src_param src1_param;
3946 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3947 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3948 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3949 struct glsl_sample_function sample_function;
3950 DWORD reg = ins->dst[0].reg.idx[0].offset;
3951 char coord_mask[6];
3953 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3954 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3956 /* Perform the last matrix multiply operation */
3957 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3958 /* Reflection calculation */
3959 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
3961 /* Dependent read, not valid with conditional NP2 */
3962 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3963 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3965 /* Sample the texture */
3966 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3967 NULL, NULL, NULL, "tmp0%s", coord_mask);
3969 tex_mx->current_row = 0;
3972 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3973 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3974 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
3976 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3977 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3978 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3979 struct glsl_sample_function sample_function;
3980 DWORD reg = ins->dst[0].reg.idx[0].offset;
3981 struct glsl_src_param src0_param;
3982 char coord_mask[6];
3984 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3986 /* Perform the last matrix multiply operation */
3987 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
3989 /* Construct the eye-ray vector from w coordinates */
3990 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3991 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
3992 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3994 /* Dependent read, not valid with conditional NP2 */
3995 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3996 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3998 /* Sample the texture using the calculated coordinates */
3999 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4000 NULL, NULL, NULL, "tmp0%s", coord_mask);
4002 tex_mx->current_row = 0;
4005 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4006 * Apply a fake bump map transform.
4007 * texbem is pshader <= 1.3 only, this saves a few version checks
4009 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
4011 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4012 struct glsl_sample_function sample_function;
4013 struct glsl_src_param coord_param;
4014 DWORD sampler_idx;
4015 DWORD mask;
4016 DWORD flags;
4017 char coord_mask[6];
4019 sampler_idx = ins->dst[0].reg.idx[0].offset;
4020 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4021 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4023 /* Dependent read, not valid with conditional NP2 */
4024 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4025 mask = sample_function.coord_mask;
4027 shader_glsl_write_mask_to_str(mask, coord_mask);
4029 /* With projected textures, texbem only divides the static texture coord,
4030 * not the displacement, so we can't let GL handle this. */
4031 if (flags & WINED3D_PSARGS_PROJECTED)
4033 DWORD div_mask=0;
4034 char coord_div_mask[3];
4035 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4037 case WINED3D_TTFF_COUNT1:
4038 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4039 break;
4040 case WINED3D_TTFF_COUNT2:
4041 div_mask = WINED3DSP_WRITEMASK_1;
4042 break;
4043 case WINED3D_TTFF_COUNT3:
4044 div_mask = WINED3DSP_WRITEMASK_2;
4045 break;
4046 case WINED3D_TTFF_COUNT4:
4047 case WINED3D_TTFF_DISABLE:
4048 div_mask = WINED3DSP_WRITEMASK_3;
4049 break;
4051 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4052 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4055 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4057 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4058 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4059 coord_param.param_str, coord_mask);
4061 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4063 struct glsl_src_param luminance_param;
4064 struct glsl_dst_param dst_param;
4066 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4067 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4069 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4070 dst_param.reg_name, dst_param.mask_str,
4071 luminance_param.param_str, sampler_idx, sampler_idx);
4075 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4077 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4078 struct glsl_src_param src0_param, src1_param;
4080 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4081 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4083 shader_glsl_append_dst(ins->ctx->buffer, ins);
4084 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4085 src0_param.param_str, sampler_idx, src1_param.param_str);
4088 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4089 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4090 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4092 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4093 struct glsl_sample_function sample_function;
4094 struct glsl_src_param src0_param;
4096 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4098 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4099 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4100 "%s.wx", src0_param.reg_name);
4103 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4104 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4105 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4107 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4108 struct glsl_sample_function sample_function;
4109 struct glsl_src_param src0_param;
4111 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4113 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4114 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4115 "%s.yz", src0_param.reg_name);
4118 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4119 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4120 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4122 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4123 struct glsl_sample_function sample_function;
4124 struct glsl_src_param src0_param;
4126 /* Dependent read, not valid with conditional NP2 */
4127 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4128 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4130 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4131 "%s", src0_param.param_str);
4134 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4135 * If any of the first 3 components are < 0, discard this pixel */
4136 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4138 struct glsl_dst_param dst_param;
4140 /* The argument is a destination parameter, and no writemasks are allowed */
4141 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4142 if (ins->ctx->reg_maps->shader_version.major >= 2)
4144 /* 2.0 shaders compare all 4 components in texkill */
4145 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4146 } else {
4147 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
4148 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
4149 * 4 components are defined, only the first 3 are used
4151 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4155 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4156 * dst = dot2(src0, src1) + src2 */
4157 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4159 struct glsl_src_param src0_param;
4160 struct glsl_src_param src1_param;
4161 struct glsl_src_param src2_param;
4162 DWORD write_mask;
4163 unsigned int mask_size;
4165 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4166 mask_size = shader_glsl_get_write_mask_size(write_mask);
4168 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4169 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4170 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4172 if (mask_size > 1) {
4173 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4174 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4175 } else {
4176 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4177 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4181 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
4182 const struct wined3d_shader_signature_element *input_signature,
4183 const struct wined3d_shader_reg_maps *reg_maps,
4184 enum vertexprocessing_mode vertexprocessing)
4186 WORD map = reg_maps->input_registers;
4187 unsigned int i;
4189 for (i = 0; map; map >>= 1, ++i)
4191 const char *semantic_name;
4192 UINT semantic_idx;
4193 char reg_mask[6];
4195 /* Unused */
4196 if (!(map & 1)) continue;
4198 semantic_name = input_signature[i].semantic_name;
4199 semantic_idx = input_signature[i].semantic_idx;
4200 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
4202 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4204 if (semantic_idx < 8 && vertexprocessing == pretransformed)
4205 shader_addline(buffer, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4206 shader->u.ps.input_reg_map[i], reg_mask, semantic_idx, reg_mask);
4207 else
4208 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4209 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4211 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4213 if (!semantic_idx)
4214 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4215 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4216 else if (semantic_idx == 1)
4217 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4218 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4219 else
4220 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4221 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4223 else
4225 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4226 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4231 /*********************************************
4232 * Vertex Shader Specific Code begins here
4233 ********************************************/
4235 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4237 struct glsl_program_key key;
4239 key.vs_id = entry->vs.id;
4240 key.gs_id = entry->gs.id;
4241 key.ps_id = entry->ps.id;
4243 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4245 ERR("Failed to insert program entry.\n");
4249 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4250 GLhandleARB vs_id, GLhandleARB gs_id, GLhandleARB ps_id)
4252 struct wine_rb_entry *entry;
4253 struct glsl_program_key key;
4255 key.vs_id = vs_id;
4256 key.gs_id = gs_id;
4257 key.ps_id = ps_id;
4259 entry = wine_rb_get(&priv->program_lookup, &key);
4260 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4263 /* Context activation is done by the caller. */
4264 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4265 struct glsl_shader_prog_link *entry)
4267 struct glsl_program_key key;
4269 key.vs_id = entry->vs.id;
4270 key.gs_id = entry->gs.id;
4271 key.ps_id = entry->ps.id;
4272 wine_rb_remove(&priv->program_lookup, &key);
4274 GL_EXTCALL(glDeleteObjectARB(entry->programId));
4275 if (entry->vs.id)
4276 list_remove(&entry->vs.shader_entry);
4277 if (entry->gs.id)
4278 list_remove(&entry->gs.shader_entry);
4279 if (entry->ps.id)
4280 list_remove(&entry->ps.shader_entry);
4281 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4282 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4283 HeapFree(GetProcessHeap(), 0, entry);
4286 static void handle_ps3_input(struct wined3d_shader_buffer *buffer,
4287 const struct wined3d_gl_info *gl_info, const DWORD *map,
4288 const struct wined3d_shader_signature_element *input_signature,
4289 const struct wined3d_shader_reg_maps *reg_maps_in,
4290 const struct wined3d_shader_signature_element *output_signature,
4291 const struct wined3d_shader_reg_maps *reg_maps_out)
4293 unsigned int i, j;
4294 const char *semantic_name_in;
4295 UINT semantic_idx_in;
4296 DWORD *set;
4297 DWORD in_idx;
4298 unsigned int in_count = vec4_varyings(3, gl_info);
4299 char reg_mask[6];
4300 char destination[50];
4301 WORD input_map, output_map;
4303 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
4305 input_map = reg_maps_in->input_registers;
4306 for (i = 0; input_map; input_map >>= 1, ++i)
4308 if (!(input_map & 1)) continue;
4310 in_idx = map[i];
4311 /* Declared, but not read register */
4312 if (in_idx == ~0U) continue;
4313 if (in_idx >= (in_count + 2))
4315 FIXME("More input varyings declared than supported, expect issues.\n");
4316 continue;
4319 if (in_idx == in_count)
4320 sprintf(destination, "gl_FrontColor");
4321 else if (in_idx == in_count + 1)
4322 sprintf(destination, "gl_FrontSecondaryColor");
4323 else
4324 sprintf(destination, "ps_in[%u]", in_idx);
4326 semantic_name_in = input_signature[i].semantic_name;
4327 semantic_idx_in = input_signature[i].semantic_idx;
4328 set[in_idx] = ~0U;
4330 output_map = reg_maps_out->output_registers;
4331 for (j = 0; output_map; output_map >>= 1, ++j)
4333 DWORD mask;
4335 if (!(output_map & 1)
4336 || semantic_idx_in != output_signature[j].semantic_idx
4337 || strcmp(semantic_name_in, output_signature[j].semantic_name)
4338 || !(mask = input_signature[i].mask & output_signature[j].mask))
4339 continue;
4341 set[in_idx] = mask;
4342 shader_glsl_write_mask_to_str(mask, reg_mask);
4344 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4345 destination, reg_mask, j, reg_mask);
4349 for (i = 0; i < in_count + 2; ++i)
4351 unsigned int size;
4353 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4354 continue;
4356 if (set[i] == ~0U) set[i] = 0;
4358 size = 0;
4359 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4360 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4361 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4362 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4363 reg_mask[size] = '\0';
4365 if (i == in_count)
4366 sprintf(destination, "gl_FrontColor");
4367 else if (i == in_count + 1)
4368 sprintf(destination, "gl_FrontSecondaryColor");
4369 else
4370 sprintf(destination, "ps_in[%u]", i);
4372 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
4373 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
4376 HeapFree(GetProcessHeap(), 0, set);
4379 /* Context activation is done by the caller. */
4380 static GLhandleARB generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
4381 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4382 const struct wined3d_gl_info *gl_info)
4384 GLhandleARB ret = 0;
4385 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4386 unsigned int i;
4387 const char *semantic_name;
4388 UINT semantic_idx;
4389 char reg_mask[6];
4390 const struct wined3d_shader_signature_element *output_signature = vs->output_signature;
4391 WORD map = vs->reg_maps.output_registers;
4393 shader_buffer_clear(buffer);
4395 shader_addline(buffer, "#version 120\n");
4397 if (ps_major < 3)
4399 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits.packed_output);
4401 for (i = 0; map; map >>= 1, ++i)
4403 DWORD write_mask;
4405 if (!(map & 1)) continue;
4407 semantic_name = output_signature[i].semantic_name;
4408 semantic_idx = output_signature[i].semantic_idx;
4409 write_mask = output_signature[i].mask;
4410 shader_glsl_write_mask_to_str(write_mask, reg_mask);
4412 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4414 if (!semantic_idx)
4415 shader_addline(buffer, "gl_FrontColor%s = vs_out[%u]%s;\n",
4416 reg_mask, i, reg_mask);
4417 else if (semantic_idx == 1)
4418 shader_addline(buffer, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4419 reg_mask, i, reg_mask);
4421 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4423 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4424 reg_mask, i, reg_mask);
4426 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4428 if (semantic_idx < 8)
4430 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
4431 write_mask |= WINED3DSP_WRITEMASK_3;
4433 shader_addline(buffer, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4434 semantic_idx, reg_mask, i, reg_mask);
4435 if (!(write_mask & WINED3DSP_WRITEMASK_3))
4436 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
4439 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4441 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4443 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
4445 shader_addline(buffer, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n", i, reg_mask[1]);
4448 shader_addline(buffer, "}\n");
4450 else
4452 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits.packed_input);
4453 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4454 shader_addline(buffer, "varying vec4 ps_in[%u];\n", in_count);
4455 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits.packed_output);
4457 /* First, sort out position and point size. Those are not passed to the pixel shader */
4458 for (i = 0; map; map >>= 1, ++i)
4460 if (!(map & 1)) continue;
4462 semantic_name = output_signature[i].semantic_name;
4463 shader_glsl_write_mask_to_str(output_signature[i].mask, reg_mask);
4465 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4467 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4468 reg_mask, i, reg_mask);
4470 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4472 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4476 /* Then, fix the pixel shader input */
4477 handle_ps3_input(buffer, gl_info, ps->u.ps.input_reg_map, ps->input_signature,
4478 &ps->reg_maps, output_signature, &vs->reg_maps);
4480 shader_addline(buffer, "}\n");
4483 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4484 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
4485 shader_glsl_compile(gl_info, ret, buffer->buffer);
4487 return ret;
4490 static void shader_glsl_generate_srgb_write_correction(struct wined3d_shader_buffer *buffer)
4492 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4493 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4494 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4495 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4496 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4497 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4500 static void shader_glsl_generate_fog_code(struct wined3d_shader_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
4502 switch (mode)
4504 case WINED3D_FFP_PS_FOG_OFF:
4505 return;
4507 case WINED3D_FFP_PS_FOG_LINEAR:
4508 /* Fog = (gl_Fog.end - gl_FogFragCoord) / (gl_Fog.end - gl_Fog.start) */
4509 shader_addline(buffer, "float Fog = (gl_Fog.end - gl_FogFragCoord) / (gl_Fog.end - gl_Fog.start);\n");
4510 break;
4512 case WINED3D_FFP_PS_FOG_EXP:
4513 /* Fog = e^-(gl_Fog.density * gl_FogFragCoord) */
4514 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4515 break;
4517 case WINED3D_FFP_PS_FOG_EXP2:
4518 /* Fog = e^-((gl_Fog.density * gl_FogFragCoord)^2) */
4519 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4520 break;
4522 default:
4523 ERR("Invalid fog mode %#x.\n", mode);
4524 return;
4527 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, clamp(Fog, 0.0, 1.0));\n");
4530 /* Context activation is done by the caller. */
4531 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
4532 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4533 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
4535 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4536 const struct wined3d_gl_info *gl_info = context->gl_info;
4537 const DWORD *function = shader->function;
4538 struct shader_glsl_ctx_priv priv_ctx;
4540 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4541 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4543 memset(&priv_ctx, 0, sizeof(priv_ctx));
4544 priv_ctx.cur_ps_args = args;
4545 priv_ctx.cur_np2fixup_info = np2fixup_info;
4547 shader_addline(buffer, "#version 120\n");
4549 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4550 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4551 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4552 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
4553 /* The spec says that it doesn't have to be explicitly enabled, but the
4554 * nvidia drivers write a warning if we don't do so. */
4555 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4556 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4557 if (gl_info->supported[EXT_GPU_SHADER4])
4558 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4560 /* Base Declarations */
4561 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4563 /* Pack 3.0 inputs */
4564 if (reg_maps->shader_version.major >= 3 && args->vp_mode != vertexshader)
4565 shader_glsl_input_pack(shader, buffer, shader->input_signature, reg_maps, args->vp_mode);
4567 /* Base Shader Body */
4568 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4570 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4571 if (reg_maps->shader_version.major < 2)
4573 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4574 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4577 if (args->srgb_correction)
4578 shader_glsl_generate_srgb_write_correction(buffer);
4580 /* SM < 3 does not replace the fog stage. */
4581 if (reg_maps->shader_version.major < 3)
4582 shader_glsl_generate_fog_code(buffer, args->fog);
4584 shader_addline(buffer, "}\n");
4586 TRACE("Compiling shader object %u\n", shader_obj);
4587 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4589 /* Store the shader object */
4590 return shader_obj;
4593 /* Context activation is done by the caller. */
4594 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4595 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4596 const struct vs_compile_args *args)
4598 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4599 const struct wined3d_gl_info *gl_info = context->gl_info;
4600 const DWORD *function = shader->function;
4601 struct shader_glsl_ctx_priv priv_ctx;
4603 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4604 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4606 shader_addline(buffer, "#version 120\n");
4608 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4609 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4610 if (gl_info->supported[EXT_GPU_SHADER4])
4611 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4613 memset(&priv_ctx, 0, sizeof(priv_ctx));
4614 priv_ctx.cur_vs_args = args;
4616 /* Base Declarations */
4617 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4619 /* Base Shader Body */
4620 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4622 /* Unpack outputs */
4623 shader_addline(buffer, "order_ps_input(vs_out);\n");
4625 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4626 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4627 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4628 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4630 if (args->fog_src == VS_FOG_Z)
4631 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4632 else if (!reg_maps->fog)
4633 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4635 /* We always store the clipplanes without y inversion */
4636 if (args->clip_enabled)
4637 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4639 /* Write the final position.
4641 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4642 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4643 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4644 * contains 1.0 to allow a mad.
4646 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4647 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4649 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4651 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4652 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4653 * which is the same as z = z * 2 - w.
4655 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4657 shader_addline(buffer, "}\n");
4659 TRACE("Compiling shader object %u\n", shader_obj);
4660 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4662 return shader_obj;
4665 /* Context activation is done by the caller. */
4666 static GLhandleARB shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
4667 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader)
4669 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4670 const struct wined3d_gl_info *gl_info = context->gl_info;
4671 const DWORD *function = shader->function;
4672 struct shader_glsl_ctx_priv priv_ctx;
4673 GLhandleARB shader_id;
4675 shader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_GEOMETRY_SHADER_ARB));
4677 shader_addline(buffer, "#version 120\n");
4679 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
4680 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
4681 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4682 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4683 if (gl_info->supported[EXT_GPU_SHADER4])
4684 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4686 memset(&priv_ctx, 0, sizeof(priv_ctx));
4687 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4688 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4689 shader_addline(buffer, "}\n");
4691 TRACE("Compiling shader object %u.\n", shader_id);
4692 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4694 return shader_id;
4697 static GLhandleARB find_glsl_pshader(const struct wined3d_context *context,
4698 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4699 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
4701 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
4702 struct glsl_shader_private *shader_data;
4703 struct ps_np2fixup_info *np2fixup;
4704 UINT i;
4705 DWORD new_size;
4706 GLhandleARB ret;
4708 if (!shader->backend_data)
4710 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4711 if (!shader->backend_data)
4713 ERR("Failed to allocate backend data.\n");
4714 return 0;
4717 shader_data = shader->backend_data;
4718 gl_shaders = shader_data->gl_shaders.ps;
4720 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4721 * so a linear search is more performant than a hashmap or a binary search
4722 * (cache coherency etc)
4724 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4726 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
4728 if (args->np2_fixup)
4729 *np2fixup_info = &gl_shaders[i].np2fixup;
4730 return gl_shaders[i].prgId;
4734 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4735 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4736 if (shader_data->num_gl_shaders)
4738 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4739 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
4740 new_size * sizeof(*gl_shaders));
4742 else
4744 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4745 new_size = 1;
4748 if(!new_array) {
4749 ERR("Out of memory\n");
4750 return 0;
4752 shader_data->gl_shaders.ps = new_array;
4753 shader_data->shader_array_size = new_size;
4754 gl_shaders = new_array;
4757 gl_shaders[shader_data->num_gl_shaders].args = *args;
4759 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
4760 memset(np2fixup, 0, sizeof(*np2fixup));
4761 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
4763 pixelshader_update_samplers(shader, args->tex_types);
4765 shader_buffer_clear(buffer);
4766 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4767 gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4769 return ret;
4772 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4773 const DWORD use_map) {
4774 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4775 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4776 return stored->fog_src == new->fog_src;
4779 static GLhandleARB find_glsl_vshader(const struct wined3d_context *context,
4780 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4781 const struct vs_compile_args *args)
4783 UINT i;
4784 DWORD new_size;
4785 DWORD use_map = shader->device->stream_info.use_map;
4786 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
4787 struct glsl_shader_private *shader_data;
4788 GLhandleARB ret;
4790 if (!shader->backend_data)
4792 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4793 if (!shader->backend_data)
4795 ERR("Failed to allocate backend data.\n");
4796 return 0;
4799 shader_data = shader->backend_data;
4800 gl_shaders = shader_data->gl_shaders.vs;
4802 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4803 * so a linear search is more performant than a hashmap or a binary search
4804 * (cache coherency etc)
4806 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4808 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
4809 return gl_shaders[i].prgId;
4812 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4814 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4815 if (shader_data->num_gl_shaders)
4817 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4818 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
4819 new_size * sizeof(*gl_shaders));
4821 else
4823 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4824 new_size = 1;
4827 if(!new_array) {
4828 ERR("Out of memory\n");
4829 return 0;
4831 shader_data->gl_shaders.vs = new_array;
4832 shader_data->shader_array_size = new_size;
4833 gl_shaders = new_array;
4836 gl_shaders[shader_data->num_gl_shaders].args = *args;
4838 shader_buffer_clear(buffer);
4839 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4840 gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4842 return ret;
4845 static GLhandleARB find_glsl_geometry_shader(const struct wined3d_context *context,
4846 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader)
4848 struct glsl_gs_compiled_shader *gl_shaders;
4849 struct glsl_shader_private *shader_data;
4850 GLhandleARB ret;
4852 if (!shader->backend_data)
4854 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
4856 ERR("Failed to allocate backend data.\n");
4857 return 0;
4860 shader_data = shader->backend_data;
4861 gl_shaders = shader_data->gl_shaders.gs;
4863 if (shader_data->num_gl_shaders)
4864 return gl_shaders[0].id;
4866 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4868 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
4870 ERR("Failed to allocate GL shader array.\n");
4871 return 0;
4873 shader_data->shader_array_size = 1;
4874 gl_shaders = shader_data->gl_shaders.gs;
4876 shader_buffer_clear(buffer);
4877 ret = shader_glsl_generate_geometry_shader(context, buffer, shader);
4878 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4880 return ret;
4883 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
4885 switch (mcs)
4887 case WINED3D_MCS_MATERIAL:
4888 return material;
4889 case WINED3D_MCS_COLOR1:
4890 return "gl_Color";
4891 case WINED3D_MCS_COLOR2:
4892 return "gl_SecondaryColor";
4893 default:
4894 ERR("Invalid material color source %#x.\n", mcs);
4895 return "<invalid>";
4899 static void shader_glsl_ffp_vertex_lighting(struct wined3d_shader_buffer *buffer,
4900 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4902 const char *diffuse, *specular, *emission, *ambient;
4903 enum wined3d_light_type light_type;
4904 unsigned int i;
4906 if (!settings->lighting)
4908 shader_addline(buffer, "gl_FrontColor = gl_Color;\n");
4909 shader_addline(buffer, "gl_FrontSecondaryColor = gl_SecondaryColor;\n");
4910 return;
4913 shader_addline(buffer, "vec3 ambient = gl_LightModel.ambient.xyz;\n");
4914 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
4915 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
4916 shader_addline(buffer, "vec3 dir, dst;\n");
4917 shader_addline(buffer, "float att, t;\n");
4919 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "gl_FrontMaterial.ambient");
4920 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "gl_FrontMaterial.diffuse");
4921 specular = shader_glsl_ffp_mcs(settings->specular_source, "gl_FrontMaterial.specular");
4922 emission = shader_glsl_ffp_mcs(settings->emission_source, "gl_FrontMaterial.emission");
4924 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
4926 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
4927 switch (light_type)
4929 case WINED3D_LIGHT_POINT:
4930 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4931 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4932 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4933 shader_addline(buffer, "dst.x = 1.0;\n");
4934 shader_addline(buffer, "att = dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4935 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n", i, i, i);
4936 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz / att;\n", i);
4937 if (!settings->normal)
4938 break;
4939 shader_addline(buffer, "dir = normalize(dir);\n");
4940 shader_addline(buffer, "diffuse += (max(0.0, dot(dir, normal))"
4941 " * gl_LightSource[%u].diffuse.xyz) / att;\n", i);
4942 if (settings->localviewer)
4943 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4944 else
4945 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4946 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4947 " * gl_LightSource[%u].specular) / att;\n", i);
4948 break;
4950 case WINED3D_LIGHT_SPOT:
4951 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4952 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4953 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4954 shader_addline(buffer, "dst.x = 1.0;\n");
4955 shader_addline(buffer, "dir = normalize(dir);\n");
4956 shader_addline(buffer, "t = dot(-dir, normalize(gl_LightSource[%u].spotDirection));\n", i);
4957 shader_addline(buffer, "if (t < gl_LightSource[%u].spotCosCutoff) att = 0.0;\n", i);
4958 shader_addline(buffer, "else att = pow(t, gl_LightSource[%u].spotExponent)"
4959 " / dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4960 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n",
4961 i, i, i, i);
4962 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz * att;\n", i);
4963 if (!settings->normal)
4964 break;
4965 shader_addline(buffer, "diffuse += (max(0.0, dot(dir, normal))"
4966 " * gl_LightSource[%u].diffuse.xyz) * att;\n", i);
4967 if (settings->localviewer)
4968 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4969 else
4970 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4971 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4972 " * gl_LightSource[%u].specular) * att;\n", i);
4973 break;
4975 case WINED3D_LIGHT_DIRECTIONAL:
4976 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz;\n", i);
4977 if (!settings->normal)
4978 break;
4979 shader_addline(buffer, "dir = normalize(gl_LightSource[%u].position.xyz);\n", i);
4980 shader_addline(buffer, "diffuse += max(0.0, dot(dir, normal)) * gl_LightSource[%u].diffuse.xyz;\n", i);
4981 shader_addline(buffer, "t = dot(normal, gl_LightSource[%u].halfVector.xyz);\n", i);
4982 shader_addline(buffer, "if (t > 0.0) specular += pow(t, gl_FrontMaterial.shininess)"
4983 " * gl_LightSource[%u].specular;\n", i);
4984 break;
4986 default:
4987 if (light_type)
4988 FIXME("Unhandled light type %#x.\n", light_type);
4989 continue;
4993 shader_addline(buffer, "gl_FrontColor.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
4994 ambient, diffuse, emission);
4995 shader_addline(buffer, "gl_FrontColor.w = %s.w;\n", diffuse);
4996 shader_addline(buffer, "gl_FrontSecondaryColor = %s * specular;\n", specular);
4999 /* Context activation is done by the caller. */
5000 static GLhandleARB shader_glsl_generate_ffp_vertex_shader(struct wined3d_shader_buffer *buffer,
5001 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
5003 GLhandleARB shader_obj;
5004 unsigned int i;
5006 shader_buffer_clear(buffer);
5008 shader_addline(buffer, "#version 120\n");
5009 shader_addline(buffer, "\n");
5010 shader_addline(buffer, "void main()\n{\n");
5011 shader_addline(buffer, "float m;\n");
5012 shader_addline(buffer, "vec3 r;\n");
5014 shader_addline(buffer, "vec4 ec_pos = gl_ModelViewMatrix * gl_Vertex;\n");
5015 shader_addline(buffer, "gl_Position = gl_ProjectionMatrix * ec_pos;\n");
5016 if (settings->clipping)
5017 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
5018 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
5020 if (!settings->normal)
5021 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
5022 else if (settings->normalize)
5023 shader_addline(buffer, "vec3 normal = normalize(gl_NormalMatrix * gl_Normal);\n");
5024 else
5025 shader_addline(buffer, "vec3 normal = gl_NormalMatrix * gl_Normal;\n");
5027 shader_glsl_ffp_vertex_lighting(buffer, settings, gl_info);
5029 for (i = 0; i < MAX_TEXTURES; ++i)
5031 switch (settings->texgen[i] << WINED3D_FFP_TCI_SHIFT)
5033 case WINED3DTSS_TCI_PASSTHRU:
5034 if (settings->texcoords & (1 << i))
5035 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * gl_MultiTexCoord%d;\n",
5036 i, i, i);
5037 break;
5039 case WINED3DTSS_TCI_CAMERASPACENORMAL:
5040 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * vec4(normal, 1.0);\n", i, i);
5041 break;
5043 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
5044 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * ec_pos;\n", i, i);
5045 break;
5047 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
5048 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
5049 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
5050 break;
5052 case WINED3DTSS_TCI_SPHEREMAP:
5053 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
5054 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
5055 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
5056 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
5057 break;
5059 default:
5060 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
5061 break;
5065 switch (settings->fog_mode)
5067 case WINED3D_FFP_VS_FOG_OFF:
5068 break;
5070 case WINED3D_FFP_VS_FOG_FOGCOORD:
5071 shader_addline(buffer, "gl_FogFragCoord = gl_SecondaryColor.w * 255.0;\n");
5072 break;
5074 case WINED3D_FFP_VS_FOG_RANGE:
5075 shader_addline(buffer, "gl_FogFragCoord = length(ec_pos.xyz);\n");
5076 break;
5078 case WINED3D_FFP_VS_FOG_DEPTH:
5079 shader_addline(buffer, "gl_FogFragCoord = ec_pos.z;\n");
5080 break;
5082 default:
5083 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
5084 break;
5087 if (settings->point_size)
5089 shader_addline(buffer, "gl_PointSize = gl_Point.size / sqrt(gl_Point.distanceConstantAttenuation"
5090 " + gl_Point.distanceLinearAttenuation * length(ec_pos.xyz)"
5091 " + gl_Point.distanceQuadraticAttenuation * dot(ec_pos.xyz, ec_pos.xyz));\n");
5092 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, gl_Point.sizeMin, gl_Point.sizeMax);\n");
5095 shader_addline(buffer, "}\n");
5097 shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
5098 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5100 return shader_obj;
5103 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_shader_buffer *buffer,
5104 DWORD argnum, unsigned int stage, DWORD arg)
5106 const char *ret;
5108 if (arg == ARG_UNUSED)
5109 return "<unused arg>";
5111 switch (arg & WINED3DTA_SELECTMASK)
5113 case WINED3DTA_DIFFUSE:
5114 ret = "gl_Color";
5115 break;
5117 case WINED3DTA_CURRENT:
5118 if (!stage)
5119 ret = "gl_Color";
5120 else
5121 ret = "ret";
5122 break;
5124 case WINED3DTA_TEXTURE:
5125 switch (stage)
5127 case 0: ret = "tex0"; break;
5128 case 1: ret = "tex1"; break;
5129 case 2: ret = "tex2"; break;
5130 case 3: ret = "tex3"; break;
5131 case 4: ret = "tex4"; break;
5132 case 5: ret = "tex5"; break;
5133 case 6: ret = "tex6"; break;
5134 case 7: ret = "tex7"; break;
5135 default:
5136 ret = "<invalid texture>";
5137 break;
5139 break;
5141 case WINED3DTA_TFACTOR:
5142 ret = "tex_factor";
5143 break;
5145 case WINED3DTA_SPECULAR:
5146 ret = "gl_SecondaryColor";
5147 break;
5149 case WINED3DTA_TEMP:
5150 ret = "temp_reg";
5151 break;
5153 case WINED3DTA_CONSTANT:
5154 FIXME("Per-stage constants not implemented.\n");
5155 switch (stage)
5157 case 0: ret = "const0"; break;
5158 case 1: ret = "const1"; break;
5159 case 2: ret = "const2"; break;
5160 case 3: ret = "const3"; break;
5161 case 4: ret = "const4"; break;
5162 case 5: ret = "const5"; break;
5163 case 6: ret = "const6"; break;
5164 case 7: ret = "const7"; break;
5165 default:
5166 ret = "<invalid constant>";
5167 break;
5169 break;
5171 default:
5172 return "<unhandled arg>";
5175 if (arg & WINED3DTA_COMPLEMENT)
5177 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
5178 if (argnum == 0)
5179 ret = "arg0";
5180 else if (argnum == 1)
5181 ret = "arg1";
5182 else if (argnum == 2)
5183 ret = "arg2";
5186 if (arg & WINED3DTA_ALPHAREPLICATE)
5188 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
5189 if (argnum == 0)
5190 ret = "arg0";
5191 else if (argnum == 1)
5192 ret = "arg1";
5193 else if (argnum == 2)
5194 ret = "arg2";
5197 return ret;
5200 static void shader_glsl_ffp_fragment_op(struct wined3d_shader_buffer *buffer, unsigned int stage, BOOL color,
5201 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
5203 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5205 if (color && alpha)
5206 dstmask = "";
5207 else if (color)
5208 dstmask = ".xyz";
5209 else
5210 dstmask = ".w";
5212 if (dst == tempreg)
5213 dstreg = "temp_reg";
5214 else
5215 dstreg = "ret";
5217 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
5218 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
5219 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
5221 switch (op)
5223 case WINED3D_TOP_DISABLE:
5224 if (!stage)
5225 shader_addline(buffer, "%s%s = gl_Color%s;\n", dstreg, dstmask, dstmask);
5226 break;
5228 case WINED3D_TOP_SELECT_ARG1:
5229 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
5230 break;
5232 case WINED3D_TOP_SELECT_ARG2:
5233 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
5234 break;
5236 case WINED3D_TOP_MODULATE:
5237 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5238 break;
5240 case WINED3D_TOP_MODULATE_4X:
5241 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
5242 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5243 break;
5245 case WINED3D_TOP_MODULATE_2X:
5246 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
5247 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5248 break;
5250 case WINED3D_TOP_ADD:
5251 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
5252 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5253 break;
5255 case WINED3D_TOP_ADD_SIGNED:
5256 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
5257 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5258 break;
5260 case WINED3D_TOP_ADD_SIGNED_2X:
5261 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
5262 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5263 break;
5265 case WINED3D_TOP_SUBTRACT:
5266 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
5267 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5268 break;
5270 case WINED3D_TOP_ADD_SMOOTH:
5271 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
5272 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
5273 break;
5275 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
5276 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5277 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5278 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5279 break;
5281 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5282 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5283 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5284 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5285 break;
5287 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5288 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
5289 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5290 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5291 break;
5293 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5294 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5295 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5296 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
5297 break;
5299 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
5300 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
5301 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5302 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5303 break;
5305 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
5306 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
5307 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5308 break;
5310 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
5311 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
5312 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5313 break;
5315 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
5316 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5317 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5318 break;
5319 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
5320 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
5321 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5322 break;
5324 case WINED3D_TOP_BUMPENVMAP:
5325 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5326 /* These are handled in the first pass, nothing to do. */
5327 break;
5329 case WINED3D_TOP_DOTPRODUCT3:
5330 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
5331 dstreg, dstmask, arg1, arg2, dstmask);
5332 break;
5334 case WINED3D_TOP_MULTIPLY_ADD:
5335 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
5336 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
5337 break;
5339 case WINED3D_TOP_LERP:
5340 /* MSDN isn't quite right here. */
5341 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
5342 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
5343 break;
5345 default:
5346 FIXME("Unhandled operation %#x.\n", op);
5347 break;
5351 /* Context activation is done by the caller. */
5352 static GLuint shader_glsl_generate_ffp_fragment_shader(struct wined3d_shader_buffer *buffer,
5353 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
5355 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
5356 BYTE lum_map = 0, bump_map = 0, tex_map = 0;
5357 const char *final_combiner_src = "ret";
5358 UINT lowest_disabled_stage;
5359 GLhandleARB shader_obj;
5360 DWORD arg0, arg1, arg2;
5361 unsigned int stage;
5363 shader_buffer_clear(buffer);
5365 /* Find out which textures are read */
5366 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5368 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5369 break;
5371 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
5372 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
5373 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
5375 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5376 tex_map |= 1 << stage;
5377 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5378 tfactor_used = TRUE;
5379 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5380 tempreg_used = TRUE;
5381 if (settings->op[stage].dst == tempreg)
5382 tempreg_used = TRUE;
5384 switch (settings->op[stage].cop)
5386 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5387 lum_map |= 1 << stage;
5388 /* fall through */
5389 case WINED3D_TOP_BUMPENVMAP:
5390 bump_map |= 1 << stage;
5391 /* fall through */
5392 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5393 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5394 tex_map |= 1 << stage;
5395 break;
5397 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5398 tfactor_used = TRUE;
5399 break;
5401 default:
5402 break;
5405 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5406 continue;
5408 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
5409 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
5410 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
5412 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5413 tex_map |= 1 << stage;
5414 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5415 tfactor_used = TRUE;
5416 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5417 tempreg_used = TRUE;
5419 lowest_disabled_stage = stage;
5421 shader_addline(buffer, "#version 120\n");
5423 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5424 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5426 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
5427 shader_addline(buffer, "vec4 ret;\n");
5428 if (tempreg_used || settings->sRGB_write)
5429 shader_addline(buffer, "vec4 temp_reg;\n");
5430 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
5432 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5434 if (!(tex_map & (1 << stage)))
5435 continue;
5437 switch (settings->op[stage].tex_type)
5439 case tex_1d:
5440 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
5441 break;
5442 case tex_2d:
5443 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
5444 break;
5445 case tex_3d:
5446 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
5447 break;
5448 case tex_cube:
5449 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
5450 break;
5451 case tex_rect:
5452 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
5453 break;
5454 default:
5455 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
5456 break;
5459 shader_addline(buffer, "vec4 tex%u;\n", stage);
5461 if (!(bump_map & (1 << stage)))
5462 continue;
5463 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
5465 if (!(lum_map & (1 << stage)))
5466 continue;
5467 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
5468 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
5470 if (tfactor_used)
5471 shader_addline(buffer, "uniform vec4 tex_factor;\n");
5472 shader_addline(buffer, "uniform vec4 specular_enable;\n");
5474 if (settings->sRGB_write)
5476 shader_addline(buffer, "const vec4 srgb_const0 = ");
5477 shader_glsl_append_imm_vec4(buffer, srgb_const0);
5478 shader_addline(buffer, ";\n");
5479 shader_addline(buffer, "const vec4 srgb_const1 = ");
5480 shader_glsl_append_imm_vec4(buffer, srgb_const1);
5481 shader_addline(buffer, ";\n");
5484 shader_addline(buffer, "void main()\n{\n");
5486 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
5487 shader_addline(buffer, "if (any(lessThan(gl_TexCoord[7], vec4(0.0)))) discard;\n");
5489 /* Generate texture sampling instructions) */
5490 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
5492 const char *texture_function, *coord_mask;
5493 char tex_reg_name[8];
5494 BOOL proj, clamp;
5496 if (!(tex_map & (1 << stage)))
5497 continue;
5499 if (settings->op[stage].projected == proj_none)
5501 proj = FALSE;
5503 else if (settings->op[stage].projected == proj_count4
5504 || settings->op[stage].projected == proj_count3)
5506 proj = TRUE;
5508 else
5510 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
5511 proj = TRUE;
5514 if (settings->op[stage].cop == WINED3D_TOP_BUMPENVMAP
5515 || settings->op[stage].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
5516 clamp = FALSE;
5517 else
5518 clamp = TRUE;
5520 switch (settings->op[stage].tex_type)
5522 case tex_1d:
5523 if (proj)
5525 texture_function = "texture1DProj";
5526 coord_mask = "xw";
5528 else
5530 texture_function = "texture1D";
5531 coord_mask = "x";
5533 break;
5534 case tex_2d:
5535 if (proj)
5537 texture_function = "texture2DProj";
5538 coord_mask = "xyw";
5540 else
5542 texture_function = "texture2D";
5543 coord_mask = "xy";
5545 break;
5546 case tex_3d:
5547 if (proj)
5549 texture_function = "texture3DProj";
5550 coord_mask = "xyzw";
5552 else
5554 texture_function = "texture3D";
5555 coord_mask = "xyz";
5557 break;
5558 case tex_cube:
5559 texture_function = "textureCube";
5560 coord_mask = "xyz";
5561 break;
5562 case tex_rect:
5563 if (proj)
5565 texture_function = "texture2DRectProj";
5566 coord_mask = "xyw";
5568 else
5570 texture_function = "texture2DRect";
5571 coord_mask = "xy";
5573 break;
5574 default:
5575 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
5576 texture_function = "";
5577 coord_mask = "xyzw";
5578 break;
5581 if (stage > 0
5582 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
5583 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
5585 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
5587 /* With projective textures, texbem only divides the static
5588 * texture coord, not the displacement, so multiply the
5589 * displacement with the dividing parameter before passing it to
5590 * TXP. */
5591 if (settings->op[stage].projected != proj_none)
5593 if (settings->op[stage].projected == proj_count4)
5595 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].w) + gl_TexCoord[%u].xy;\n",
5596 stage, stage);
5597 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].ww;\n", stage);
5599 else
5601 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].z) + gl_TexCoord[%u].xy;\n",
5602 stage, stage);
5603 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].zz;\n", stage);
5606 else
5608 shader_addline(buffer, "ret = gl_TexCoord[%u] + ret.xyxy;\n", stage);
5611 if (clamp)
5612 shader_addline(buffer, "tex%u = clamp(%s(ps_sampler%u, ret.%s), 0.0, 1.0);\n",
5613 stage, texture_function, stage, coord_mask);
5614 else
5615 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
5616 stage, texture_function, stage, coord_mask);
5618 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
5619 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
5620 stage, stage - 1, stage - 1, stage - 1);
5622 else if (settings->op[stage].projected == proj_count3)
5624 if (clamp)
5625 shader_addline(buffer, "tex%u = clamp(%s(ps_sampler%u, gl_TexCoord[%u].xyz), 0.0, 1.0);\n",
5626 stage, texture_function, stage, stage);
5627 else
5628 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].xyz);\n",
5629 stage, texture_function, stage, stage);
5631 else
5633 if (clamp)
5634 shader_addline(buffer, "tex%u = clamp(%s(ps_sampler%u, gl_TexCoord[%u].%s), 0.0, 1.0);\n",
5635 stage, texture_function, stage, stage, coord_mask);
5636 else
5637 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].%s);\n",
5638 stage, texture_function, stage, stage, coord_mask);
5641 sprintf(tex_reg_name, "tex%u", stage);
5642 shader_glsl_color_correction_ext(buffer, tex_reg_name, WINED3DSP_WRITEMASK_ALL,
5643 settings->op[stage].color_fixup);
5646 /* Generate the main shader */
5647 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5649 BOOL op_equal;
5651 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5653 if (!stage)
5654 final_combiner_src = "gl_Color";
5655 break;
5658 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5659 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5660 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
5661 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5662 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5663 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
5664 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5665 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5666 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
5667 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5668 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5669 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
5670 else
5671 op_equal = settings->op[stage].aop == settings->op[stage].cop
5672 && settings->op[stage].carg0 == settings->op[stage].aarg0
5673 && settings->op[stage].carg1 == settings->op[stage].aarg1
5674 && settings->op[stage].carg2 == settings->op[stage].aarg2;
5676 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5678 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5679 settings->op[stage].cop, settings->op[stage].carg0,
5680 settings->op[stage].carg1, settings->op[stage].carg2);
5681 if (!stage)
5682 shader_addline(buffer, "ret.w = gl_Color.w;\n");
5684 else if (op_equal)
5686 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
5687 settings->op[stage].cop, settings->op[stage].carg0,
5688 settings->op[stage].carg1, settings->op[stage].carg2);
5690 else
5692 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5693 settings->op[stage].cop, settings->op[stage].carg0,
5694 settings->op[stage].carg1, settings->op[stage].carg2);
5695 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
5696 settings->op[stage].aop, settings->op[stage].aarg0,
5697 settings->op[stage].aarg1, settings->op[stage].aarg2);
5701 shader_addline(buffer, "gl_FragData[0] = gl_SecondaryColor * specular_enable + %s;\n", final_combiner_src);
5703 if (settings->sRGB_write)
5704 shader_glsl_generate_srgb_write_correction(buffer);
5706 shader_glsl_generate_fog_code(buffer, settings->fog);
5708 shader_addline(buffer, "}\n");
5710 shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
5711 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5712 return shader_obj;
5715 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
5716 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
5718 struct glsl_ffp_vertex_shader *shader;
5719 const struct wine_rb_entry *entry;
5721 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
5722 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
5724 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
5725 return NULL;
5727 shader->desc.settings = *settings;
5728 shader->id = shader_glsl_generate_ffp_vertex_shader(&priv->shader_buffer, settings, gl_info);
5729 list_init(&shader->linked_programs);
5730 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
5731 ERR("Failed to insert ffp vertex shader.\n");
5733 return shader;
5736 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
5737 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
5739 struct glsl_ffp_fragment_shader *glsl_desc;
5740 const struct ffp_frag_desc *desc;
5742 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
5743 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
5745 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
5746 return NULL;
5748 glsl_desc->entry.settings = *args;
5749 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(&priv->shader_buffer, args, gl_info);
5750 list_init(&glsl_desc->linked_programs);
5751 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
5753 return glsl_desc;
5757 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
5758 GLhandleARB program_id, struct glsl_vs_program *vs)
5760 unsigned int i;
5761 char name[32];
5763 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5764 sizeof(GLhandleARB) * gl_info->limits.glsl_vs_float_constants);
5765 for (i = 0; i < gl_info->limits.glsl_vs_float_constants; ++i)
5767 snprintf(name, sizeof(name), "vs_c[%u]", i);
5768 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5771 for (i = 0; i < MAX_CONST_I; ++i)
5773 snprintf(name, sizeof(name), "vs_i[%u]", i);
5774 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5777 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "posFixup"));
5780 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
5781 GLhandleARB program_id, struct glsl_ps_program *ps)
5783 unsigned int i;
5784 char name[32];
5786 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5787 sizeof(GLhandleARB) * gl_info->limits.glsl_ps_float_constants);
5788 for (i = 0; i < gl_info->limits.glsl_ps_float_constants; ++i)
5790 snprintf(name, sizeof(name), "ps_c[%u]", i);
5791 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5794 for (i = 0; i < MAX_CONST_I; ++i)
5796 snprintf(name, sizeof(name), "ps_i[%u]", i);
5797 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5800 for (i = 0; i < MAX_TEXTURES; ++i)
5802 snprintf(name, sizeof(name), "bumpenv_mat%u", i);
5803 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5804 snprintf(name, sizeof(name), "bumpenv_lum_scale%u", i);
5805 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5806 snprintf(name, sizeof(name), "bumpenv_lum_offset%u", i);
5807 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5810 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "tex_factor"));
5811 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "specular_enable"));
5812 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "ps_samplerNP2Fixup"));
5813 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "ycorrection"));
5816 /* Context activation is done by the caller. */
5817 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
5818 struct shader_glsl_priv *priv)
5820 const struct wined3d_gl_info *gl_info = context->gl_info;
5821 const struct ps_np2fixup_info *np2fixup_info = NULL;
5822 struct glsl_shader_prog_link *entry = NULL;
5823 struct wined3d_shader *vshader = NULL;
5824 struct wined3d_shader *gshader = NULL;
5825 struct wined3d_shader *pshader = NULL;
5826 GLhandleARB programId = 0;
5827 GLhandleARB reorder_shader_id = 0;
5828 unsigned int i;
5829 struct ps_compile_args ps_compile_args;
5830 struct vs_compile_args vs_compile_args;
5831 GLhandleARB vs_id, gs_id, ps_id;
5832 struct list *ps_list, *vs_list;
5833 struct wined3d_device *device = context->swapchain->device;
5835 if (use_vs(state))
5837 vshader = state->vertex_shader;
5838 find_vs_compile_args(state, vshader, &vs_compile_args);
5839 vs_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
5840 vs_list = &vshader->linked_programs;
5842 if ((gshader = state->geometry_shader))
5843 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5844 else
5845 gs_id = 0;
5847 else if (priv->vertex_pipe == &glsl_vertex_pipe)
5849 struct glsl_ffp_vertex_shader *ffp_shader;
5850 struct wined3d_ffp_vs_settings settings;
5852 wined3d_ffp_get_vs_settings(state, &device->stream_info, &settings);
5853 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
5854 vs_id = ffp_shader->id;
5855 vs_list = &ffp_shader->linked_programs;
5857 gs_id = 0;
5859 else
5861 vs_id = 0;
5862 gs_id = 0;
5865 if (use_ps(state))
5867 pshader = state->pixel_shader;
5868 find_ps_compile_args(state, pshader, &ps_compile_args);
5869 ps_id = find_glsl_pshader(context, &priv->shader_buffer,
5870 pshader, &ps_compile_args, &np2fixup_info);
5871 ps_list = &pshader->linked_programs;
5873 else if (priv->fragment_pipe == &glsl_fragment_pipe)
5875 struct glsl_ffp_fragment_shader *ffp_shader;
5876 struct ffp_frag_settings settings;
5878 gen_ffp_frag_op(context, state, &settings, FALSE);
5879 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
5880 ps_id = ffp_shader->id;
5881 ps_list = &ffp_shader->linked_programs;
5883 else
5885 ps_id = 0;
5888 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
5890 priv->glsl_program = entry;
5891 return;
5894 /* If we get to this point, then no matching program exists, so we create one */
5895 programId = GL_EXTCALL(glCreateProgramObjectARB());
5896 TRACE("Created new GLSL shader program %u\n", programId);
5898 /* Create the entry */
5899 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
5900 entry->programId = programId;
5901 entry->vs.id = vs_id;
5902 entry->gs.id = gs_id;
5903 entry->ps.id = ps_id;
5904 entry->constant_version = 0;
5905 entry->ps.np2_fixup_info = np2fixup_info;
5906 /* Add the hash table entry */
5907 add_glsl_program_entry(priv, entry);
5909 /* Set the current program */
5910 priv->glsl_program = entry;
5912 /* Attach GLSL vshader */
5913 if (vs_id)
5915 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, programId);
5916 GL_EXTCALL(glAttachObjectARB(programId, vs_id));
5917 checkGLcall("glAttachObjectARB");
5919 list_add_head(vs_list, &entry->vs.shader_entry);
5922 if (vshader)
5924 WORD map = vshader->reg_maps.input_registers;
5925 char tmp_name[10];
5927 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
5928 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
5929 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
5930 checkGLcall("glAttachObjectARB");
5931 /* Flag the reorder function for deletion, then it will be freed automatically when the program
5932 * is destroyed
5934 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
5936 /* Bind vertex attributes to a corresponding index number to match
5937 * the same index numbers as ARB_vertex_programs (makes loading
5938 * vertex attributes simpler). With this method, we can use the
5939 * exact same code to load the attributes later for both ARB and
5940 * GLSL shaders.
5942 * We have to do this here because we need to know the Program ID
5943 * in order to make the bindings work, and it has to be done prior
5944 * to linking the GLSL program. */
5945 for (i = 0; map; map >>= 1, ++i)
5947 if (!(map & 1)) continue;
5949 snprintf(tmp_name, sizeof(tmp_name), "vs_in%u", i);
5950 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
5952 checkGLcall("glBindAttribLocationARB");
5955 if (gshader)
5957 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, programId);
5958 GL_EXTCALL(glAttachObjectARB(programId, gs_id));
5959 checkGLcall("glAttachObjectARB");
5961 TRACE("input type %s, output type %s, vertices out %u.\n",
5962 debug_d3dprimitivetype(gshader->u.gs.input_type),
5963 debug_d3dprimitivetype(gshader->u.gs.output_type),
5964 gshader->u.gs.vertices_out);
5965 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_INPUT_TYPE_ARB,
5966 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
5967 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_OUTPUT_TYPE_ARB,
5968 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
5969 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_VERTICES_OUT_ARB,
5970 gshader->u.gs.vertices_out));
5971 checkGLcall("glProgramParameteriARB");
5973 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
5976 /* Attach GLSL pshader */
5977 if (ps_id)
5979 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, programId);
5980 GL_EXTCALL(glAttachObjectARB(programId, ps_id));
5981 checkGLcall("glAttachObjectARB");
5983 list_add_head(ps_list, &entry->ps.shader_entry);
5986 /* Link the program */
5987 TRACE("Linking GLSL shader program %u\n", programId);
5988 GL_EXTCALL(glLinkProgramARB(programId));
5989 shader_glsl_validate_link(gl_info, programId);
5991 shader_glsl_init_vs_uniform_locations(gl_info, programId, &entry->vs);
5992 shader_glsl_init_ps_uniform_locations(gl_info, programId, &entry->ps);
5993 checkGLcall("Find glsl program uniform locations");
5995 if (pshader && pshader->reg_maps.shader_version.major >= 3
5996 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
5998 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
5999 entry->vs.vertex_color_clamp = GL_FALSE;
6001 else
6003 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
6006 /* Set the shader to allow uniform loading on it */
6007 GL_EXTCALL(glUseProgramObjectARB(programId));
6008 checkGLcall("glUseProgramObjectARB(programId)");
6010 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
6011 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
6012 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
6013 * vertex shader with fixed function pixel processing is used we make sure that the card
6014 * supports enough samplers to allow the max number of vertex samplers with all possible
6015 * fixed function fragment processing setups. So once the program is linked these samplers
6016 * won't change.
6018 shader_glsl_load_vsamplers(gl_info, device->texUnitMap, programId);
6019 shader_glsl_load_psamplers(gl_info, device->texUnitMap, programId);
6022 /* Context activation is done by the caller. */
6023 static GLhandleARB create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum tex_types tex_type, BOOL masked)
6025 GLhandleARB program_id;
6026 GLhandleARB vshader_id, pshader_id;
6027 const char *blt_pshader;
6029 static const char *blt_vshader =
6030 "#version 120\n"
6031 "void main(void)\n"
6032 "{\n"
6033 " gl_Position = gl_Vertex;\n"
6034 " gl_FrontColor = vec4(1.0);\n"
6035 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
6036 "}\n";
6038 static const char * const blt_pshaders_full[tex_type_count] =
6040 /* tex_1d */
6041 NULL,
6042 /* tex_2d */
6043 "#version 120\n"
6044 "uniform sampler2D sampler;\n"
6045 "void main(void)\n"
6046 "{\n"
6047 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6048 "}\n",
6049 /* tex_3d */
6050 NULL,
6051 /* tex_cube */
6052 "#version 120\n"
6053 "uniform samplerCube sampler;\n"
6054 "void main(void)\n"
6055 "{\n"
6056 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6057 "}\n",
6058 /* tex_rect */
6059 "#version 120\n"
6060 "#extension GL_ARB_texture_rectangle : enable\n"
6061 "uniform sampler2DRect sampler;\n"
6062 "void main(void)\n"
6063 "{\n"
6064 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6065 "}\n",
6068 static const char * const blt_pshaders_masked[tex_type_count] =
6070 /* tex_1d */
6071 NULL,
6072 /* tex_2d */
6073 "#version 120\n"
6074 "uniform sampler2D sampler;\n"
6075 "uniform vec4 mask;\n"
6076 "void main(void)\n"
6077 "{\n"
6078 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6079 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6080 "}\n",
6081 /* tex_3d */
6082 NULL,
6083 /* tex_cube */
6084 "#version 120\n"
6085 "uniform samplerCube sampler;\n"
6086 "uniform vec4 mask;\n"
6087 "void main(void)\n"
6088 "{\n"
6089 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6090 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6091 "}\n",
6092 /* tex_rect */
6093 "#version 120\n"
6094 "#extension GL_ARB_texture_rectangle : enable\n"
6095 "uniform sampler2DRect sampler;\n"
6096 "uniform vec4 mask;\n"
6097 "void main(void)\n"
6098 "{\n"
6099 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6100 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6101 "}\n",
6104 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
6105 if (!blt_pshader)
6107 FIXME("tex_type %#x not supported\n", tex_type);
6108 return 0;
6111 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
6112 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
6114 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
6115 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
6117 program_id = GL_EXTCALL(glCreateProgramObjectARB());
6118 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
6119 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
6120 GL_EXTCALL(glLinkProgramARB(program_id));
6122 shader_glsl_validate_link(gl_info, program_id);
6124 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
6125 * is destroyed
6127 GL_EXTCALL(glDeleteObjectARB(vshader_id));
6128 GL_EXTCALL(glDeleteObjectARB(pshader_id));
6129 return program_id;
6132 /* Context activation is done by the caller. */
6133 static void shader_glsl_select(void *shader_priv, const struct wined3d_context *context,
6134 const struct wined3d_state *state)
6136 const struct wined3d_gl_info *gl_info = context->gl_info;
6137 struct shader_glsl_priv *priv = shader_priv;
6138 GLhandleARB program_id = 0;
6139 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
6141 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
6142 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
6144 old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vs.vertex_color_clamp : GL_FIXED_ONLY_ARB;
6145 set_glsl_shader_program(context, state, priv);
6146 current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vs.vertex_color_clamp : GL_FIXED_ONLY_ARB;
6147 if (old_vertex_color_clamp != current_vertex_color_clamp)
6149 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6151 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
6152 checkGLcall("glClampColorARB");
6154 else
6156 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
6160 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
6161 if (program_id) TRACE("Using GLSL program %u\n", program_id);
6162 GL_EXTCALL(glUseProgramObjectARB(program_id));
6163 checkGLcall("glUseProgramObjectARB");
6165 /* In case that NP2 texcoord fixup data is found for the selected program, trigger a reload of the
6166 * constants. This has to be done because it can't be guaranteed that sampler() (from state.c) is
6167 * called between selecting the shader and using it, which results in wrong fixup for some frames. */
6168 if (priv->glsl_program && priv->glsl_program->ps.np2_fixup_info)
6170 shader_glsl_load_np2fixup_constants(priv, gl_info, state);
6174 /* Context activation is done by the caller. */
6175 static void shader_glsl_disable(void *shader_priv, const struct wined3d_context *context)
6177 const struct wined3d_gl_info *gl_info = context->gl_info;
6178 struct shader_glsl_priv *priv = shader_priv;
6180 priv->glsl_program = NULL;
6181 GL_EXTCALL(glUseProgramObjectARB(0));
6182 checkGLcall("glUseProgramObjectARB");
6184 priv->vertex_pipe->vp_enable(gl_info, FALSE);
6185 priv->fragment_pipe->enable_extension(gl_info, FALSE);
6187 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6189 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
6190 checkGLcall("glClampColorARB");
6194 /* Context activation is done by the caller. */
6195 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
6196 enum tex_types tex_type, const SIZE *ds_mask_size)
6198 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
6199 struct shader_glsl_priv *priv = shader_priv;
6200 GLhandleARB *blt_program;
6201 GLint loc;
6203 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
6204 if (!*blt_program)
6206 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
6207 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
6208 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
6209 GL_EXTCALL(glUniform1iARB(loc, 0));
6211 else
6213 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
6216 if (masked)
6218 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "mask"));
6219 GL_EXTCALL(glUniform4fARB(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
6223 /* Context activation is done by the caller. */
6224 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
6226 struct shader_glsl_priv *priv = shader_priv;
6227 GLhandleARB program_id;
6229 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
6230 if (program_id) TRACE("Using GLSL program %u\n", program_id);
6232 GL_EXTCALL(glUseProgramObjectARB(program_id));
6233 checkGLcall("glUseProgramObjectARB");
6236 static void shader_glsl_destroy(struct wined3d_shader *shader)
6238 struct glsl_shader_private *shader_data = shader->backend_data;
6239 struct wined3d_device *device = shader->device;
6240 struct shader_glsl_priv *priv = device->shader_priv;
6241 const struct wined3d_gl_info *gl_info;
6242 const struct list *linked_programs;
6243 struct wined3d_context *context;
6245 if (!shader_data || !shader_data->num_gl_shaders)
6247 HeapFree(GetProcessHeap(), 0, shader_data);
6248 shader->backend_data = NULL;
6249 return;
6252 context = context_acquire(device, NULL);
6253 gl_info = context->gl_info;
6255 TRACE("Deleting linked programs.\n");
6256 linked_programs = &shader->linked_programs;
6257 if (linked_programs->next)
6259 struct glsl_shader_prog_link *entry, *entry2;
6260 UINT i;
6262 switch (shader->reg_maps.shader_version.type)
6264 case WINED3D_SHADER_TYPE_PIXEL:
6266 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
6268 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6269 struct glsl_shader_prog_link, ps.shader_entry)
6271 delete_glsl_program_entry(priv, gl_info, entry);
6274 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6276 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].prgId);
6277 if (priv->glsl_program && priv->glsl_program->ps.id == gl_shaders[i].prgId)
6278 shader_glsl_disable(priv, context);
6279 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].prgId));
6280 checkGLcall("glDeleteObjectARB");
6282 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
6284 break;
6287 case WINED3D_SHADER_TYPE_VERTEX:
6289 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
6291 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6292 struct glsl_shader_prog_link, vs.shader_entry)
6294 delete_glsl_program_entry(priv, gl_info, entry);
6297 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6299 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].prgId);
6300 if (priv->glsl_program && priv->glsl_program->vs.id == gl_shaders[i].prgId)
6301 shader_glsl_disable(priv, context);
6302 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].prgId));
6303 checkGLcall("glDeleteObjectARB");
6305 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
6307 break;
6310 case WINED3D_SHADER_TYPE_GEOMETRY:
6312 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
6314 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6315 struct glsl_shader_prog_link, gs.shader_entry)
6317 delete_glsl_program_entry(priv, gl_info, entry);
6320 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6322 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
6323 if (priv->glsl_program && priv->glsl_program->gs.id == gl_shaders[i].id)
6324 shader_glsl_disable(priv, context);
6325 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].id));
6326 checkGLcall("glDeleteObjectARB");
6328 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
6330 break;
6333 default:
6334 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
6335 break;
6339 HeapFree(GetProcessHeap(), 0, shader->backend_data);
6340 shader->backend_data = NULL;
6342 context_release(context);
6345 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
6347 const struct glsl_program_key *k = key;
6348 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
6349 const struct glsl_shader_prog_link, program_lookup_entry);
6351 if (k->vs_id > prog->vs.id) return 1;
6352 else if (k->vs_id < prog->vs.id) return -1;
6354 if (k->gs_id > prog->gs.id) return 1;
6355 else if (k->gs_id < prog->gs.id) return -1;
6357 if (k->ps_id > prog->ps.id) return 1;
6358 else if (k->ps_id < prog->ps.id) return -1;
6360 return 0;
6363 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
6365 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries) + constant_count * sizeof(*heap->positions);
6366 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
6368 if (!mem)
6370 ERR("Failed to allocate memory\n");
6371 return FALSE;
6374 heap->entries = mem;
6375 heap->entries[1].version = 0;
6376 heap->positions = (unsigned int *)(heap->entries + constant_count + 1);
6377 heap->size = 1;
6379 return TRUE;
6382 static void constant_heap_free(struct constant_heap *heap)
6384 HeapFree(GetProcessHeap(), 0, heap->entries);
6387 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
6389 wined3d_rb_alloc,
6390 wined3d_rb_realloc,
6391 wined3d_rb_free,
6392 glsl_program_key_compare,
6395 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
6396 const struct fragment_pipeline *fragment_pipe)
6398 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6399 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
6400 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
6401 gl_info->limits.glsl_ps_float_constants)) + 1;
6402 struct fragment_caps fragment_caps;
6403 void *vertex_priv, *fragment_priv;
6405 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
6407 ERR("Failed to initialize vertex pipe.\n");
6408 HeapFree(GetProcessHeap(), 0, priv);
6409 return E_FAIL;
6412 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
6414 ERR("Failed to initialize fragment pipe.\n");
6415 vertex_pipe->vp_free(device);
6416 HeapFree(GetProcessHeap(), 0, priv);
6417 return E_FAIL;
6420 if (!shader_buffer_init(&priv->shader_buffer))
6422 ERR("Failed to initialize shader buffer.\n");
6423 goto fail;
6426 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
6427 if (!priv->stack)
6429 ERR("Failed to allocate memory.\n");
6430 goto fail;
6433 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
6435 ERR("Failed to initialize vertex shader constant heap\n");
6436 goto fail;
6439 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
6441 ERR("Failed to initialize pixel shader constant heap\n");
6442 goto fail;
6445 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
6447 ERR("Failed to initialize rbtree.\n");
6448 goto fail;
6451 priv->next_constant_version = 1;
6452 priv->vertex_pipe = vertex_pipe;
6453 priv->fragment_pipe = fragment_pipe;
6454 fragment_pipe->get_caps(gl_info, &fragment_caps);
6455 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
6457 device->vertex_priv = vertex_priv;
6458 device->fragment_priv = fragment_priv;
6459 device->shader_priv = priv;
6461 return WINED3D_OK;
6463 fail:
6464 constant_heap_free(&priv->pconst_heap);
6465 constant_heap_free(&priv->vconst_heap);
6466 HeapFree(GetProcessHeap(), 0, priv->stack);
6467 shader_buffer_free(&priv->shader_buffer);
6468 fragment_pipe->free_private(device);
6469 vertex_pipe->vp_free(device);
6470 HeapFree(GetProcessHeap(), 0, priv);
6471 return E_OUTOFMEMORY;
6474 /* Context activation is done by the caller. */
6475 static void shader_glsl_free(struct wined3d_device *device)
6477 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6478 struct shader_glsl_priv *priv = device->shader_priv;
6479 int i;
6481 for (i = 0; i < tex_type_count; ++i)
6483 if (priv->depth_blt_program_full[i])
6485 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_full[i]));
6487 if (priv->depth_blt_program_masked[i])
6489 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_masked[i]));
6493 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
6494 constant_heap_free(&priv->pconst_heap);
6495 constant_heap_free(&priv->vconst_heap);
6496 HeapFree(GetProcessHeap(), 0, priv->stack);
6497 shader_buffer_free(&priv->shader_buffer);
6498 priv->fragment_pipe->free_private(device);
6499 priv->vertex_pipe->vp_free(device);
6501 HeapFree(GetProcessHeap(), 0, device->shader_priv);
6502 device->shader_priv = NULL;
6505 static void shader_glsl_context_destroyed(void *shader_priv, const struct wined3d_context *context) {}
6507 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
6509 UINT shader_model;
6511 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
6512 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
6513 && gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] && gl_info->supported[ARB_DRAW_INSTANCED])
6514 shader_model = 4;
6515 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
6516 * texldd and texldl instructions. */
6517 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
6518 shader_model = 3;
6519 else
6520 shader_model = 2;
6521 TRACE("Shader model %u.\n", shader_model);
6523 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
6524 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
6525 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
6527 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
6528 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
6530 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
6531 * Direct3D minimum requirement.
6533 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
6534 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
6536 * The problem is that the refrast clamps temporary results in the shader to
6537 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
6538 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
6539 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
6540 * offer a way to query this.
6542 caps->ps_1x_max_value = 8.0;
6544 /* Ideally we'd only set caps like sRGB writes here if supported by both
6545 * the shader backend and the fragment pipe, but we can get called before
6546 * shader_glsl_alloc(). */
6547 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
6548 | WINED3D_SHADER_CAP_SRGB_WRITE;
6551 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
6553 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
6555 TRACE("Checking support for fixup:\n");
6556 dump_color_fixup_desc(fixup);
6559 /* We support everything except YUV conversions. */
6560 if (!is_complex_fixup(fixup))
6562 TRACE("[OK]\n");
6563 return TRUE;
6566 TRACE("[FAILED]\n");
6567 return FALSE;
6570 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
6572 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
6573 /* WINED3DSIH_ADD */ shader_glsl_binop,
6574 /* WINED3DSIH_AND */ shader_glsl_binop,
6575 /* WINED3DSIH_BEM */ shader_glsl_bem,
6576 /* WINED3DSIH_BREAK */ shader_glsl_break,
6577 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
6578 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
6579 /* WINED3DSIH_CALL */ shader_glsl_call,
6580 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
6581 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
6582 /* WINED3DSIH_CND */ shader_glsl_cnd,
6583 /* WINED3DSIH_CRS */ shader_glsl_cross,
6584 /* WINED3DSIH_CUT */ shader_glsl_cut,
6585 /* WINED3DSIH_DCL */ shader_glsl_nop,
6586 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
6587 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
6588 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
6589 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
6590 /* WINED3DSIH_DEF */ shader_glsl_nop,
6591 /* WINED3DSIH_DEFB */ shader_glsl_nop,
6592 /* WINED3DSIH_DEFI */ shader_glsl_nop,
6593 /* WINED3DSIH_DIV */ shader_glsl_binop,
6594 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
6595 /* WINED3DSIH_DP3 */ shader_glsl_dot,
6596 /* WINED3DSIH_DP4 */ shader_glsl_dot,
6597 /* WINED3DSIH_DST */ shader_glsl_dst,
6598 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
6599 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
6600 /* WINED3DSIH_ELSE */ shader_glsl_else,
6601 /* WINED3DSIH_EMIT */ shader_glsl_emit,
6602 /* WINED3DSIH_ENDIF */ shader_glsl_end,
6603 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
6604 /* WINED3DSIH_ENDREP */ shader_glsl_end,
6605 /* WINED3DSIH_EQ */ shader_glsl_relop,
6606 /* WINED3DSIH_EXP */ shader_glsl_map2gl,
6607 /* WINED3DSIH_EXPP */ shader_glsl_expp,
6608 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
6609 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
6610 /* WINED3DSIH_GE */ shader_glsl_relop,
6611 /* WINED3DSIH_IADD */ shader_glsl_binop,
6612 /* WINED3DSIH_IEQ */ NULL,
6613 /* WINED3DSIH_IF */ shader_glsl_if,
6614 /* WINED3DSIH_IFC */ shader_glsl_ifc,
6615 /* WINED3DSIH_IGE */ shader_glsl_relop,
6616 /* WINED3DSIH_IMUL */ shader_glsl_imul,
6617 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
6618 /* WINED3DSIH_LABEL */ shader_glsl_label,
6619 /* WINED3DSIH_LD */ NULL,
6620 /* WINED3DSIH_LIT */ shader_glsl_lit,
6621 /* WINED3DSIH_LOG */ shader_glsl_log,
6622 /* WINED3DSIH_LOGP */ shader_glsl_log,
6623 /* WINED3DSIH_LOOP */ shader_glsl_loop,
6624 /* WINED3DSIH_LRP */ shader_glsl_lrp,
6625 /* WINED3DSIH_LT */ shader_glsl_relop,
6626 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
6627 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
6628 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
6629 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
6630 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
6631 /* WINED3DSIH_MAD */ shader_glsl_mad,
6632 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
6633 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
6634 /* WINED3DSIH_MOV */ shader_glsl_mov,
6635 /* WINED3DSIH_MOVA */ shader_glsl_mov,
6636 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
6637 /* WINED3DSIH_MUL */ shader_glsl_binop,
6638 /* WINED3DSIH_NOP */ shader_glsl_nop,
6639 /* WINED3DSIH_NRM */ shader_glsl_nrm,
6640 /* WINED3DSIH_PHASE */ shader_glsl_nop,
6641 /* WINED3DSIH_POW */ shader_glsl_pow,
6642 /* WINED3DSIH_RCP */ shader_glsl_rcp,
6643 /* WINED3DSIH_REP */ shader_glsl_rep,
6644 /* WINED3DSIH_RET */ shader_glsl_ret,
6645 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
6646 /* WINED3DSIH_RSQ */ shader_glsl_rsq,
6647 /* WINED3DSIH_SAMPLE */ NULL,
6648 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
6649 /* WINED3DSIH_SAMPLE_LOD */ NULL,
6650 /* WINED3DSIH_SETP */ NULL,
6651 /* WINED3DSIH_SGE */ shader_glsl_compare,
6652 /* WINED3DSIH_SGN */ shader_glsl_sgn,
6653 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
6654 /* WINED3DSIH_SLT */ shader_glsl_compare,
6655 /* WINED3DSIH_SQRT */ NULL,
6656 /* WINED3DSIH_SUB */ shader_glsl_binop,
6657 /* WINED3DSIH_TEX */ shader_glsl_tex,
6658 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
6659 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
6660 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
6661 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
6662 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
6663 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
6664 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
6665 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
6666 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
6667 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
6668 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
6669 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
6670 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
6671 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
6672 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
6673 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
6674 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
6675 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
6676 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
6677 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
6678 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
6679 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
6680 /* WINED3DSIH_USHR */ shader_glsl_binop,
6681 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
6682 /* WINED3DSIH_XOR */ shader_glsl_binop,
6685 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
6686 SHADER_HANDLER hw_fct;
6688 /* Select handler */
6689 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
6691 /* Unhandled opcode */
6692 if (!hw_fct)
6694 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
6695 return;
6697 hw_fct(ins);
6699 shader_glsl_add_instruction_modifiers(ins);
6702 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
6704 struct shader_glsl_priv *priv = shader_priv;
6706 return priv->ffp_proj_control;
6709 const struct wined3d_shader_backend_ops glsl_shader_backend =
6711 shader_glsl_handle_instruction,
6712 shader_glsl_select,
6713 shader_glsl_disable,
6714 shader_glsl_select_depth_blt,
6715 shader_glsl_deselect_depth_blt,
6716 shader_glsl_update_float_vertex_constants,
6717 shader_glsl_update_float_pixel_constants,
6718 shader_glsl_load_constants,
6719 shader_glsl_load_np2fixup_constants,
6720 shader_glsl_destroy,
6721 shader_glsl_alloc,
6722 shader_glsl_free,
6723 shader_glsl_context_destroyed,
6724 shader_glsl_get_caps,
6725 shader_glsl_color_fixup_supported,
6726 shader_glsl_has_ffp_proj_control,
6729 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
6731 if (enable)
6732 gl_info->gl_ops.gl.p_glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6733 else
6734 gl_info->gl_ops.gl.p_glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6735 checkGLcall("GL_VERTEX_PROGRAM_POINT_SIZE_ARB");
6738 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
6740 caps->max_active_lights = gl_info->limits.lights;
6741 caps->max_vertex_blend_matrices = 0;
6742 caps->max_vertex_blend_matrix_index = 0;
6743 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
6744 | WINED3DVTXPCAPS_MATERIALSOURCE7
6745 | WINED3DVTXPCAPS_VERTEXFOG
6746 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
6747 | WINED3DVTXPCAPS_POSITIONALLIGHTS
6748 | WINED3DVTXPCAPS_LOCALVIEWER
6749 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
6750 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
6751 caps->max_user_clip_planes = gl_info->limits.clipplanes;
6752 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
6755 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
6757 struct shader_glsl_priv *priv;
6759 if (shader_backend == &glsl_shader_backend)
6761 priv = shader_priv;
6763 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
6765 ERR("Failed to initialize rbtree.\n");
6766 return NULL;
6769 return priv;
6772 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
6774 return NULL;
6777 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
6779 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
6780 struct glsl_ffp_vertex_shader, desc.entry);
6781 struct glsl_shader_prog_link *program, *program2;
6782 struct glsl_ffp_destroy_ctx *ctx = context;
6784 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
6785 struct glsl_shader_prog_link, vs.shader_entry)
6787 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
6789 ctx->gl_info->gl_ops.ext.p_glDeleteObjectARB(shader->id);
6790 HeapFree(GetProcessHeap(), 0, shader);
6793 /* Context activation is done by the caller. */
6794 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
6796 struct shader_glsl_priv *priv = device->vertex_priv;
6797 struct glsl_ffp_destroy_ctx ctx;
6799 ctx.priv = priv;
6800 ctx.gl_info = &device->adapter->gl_info;
6801 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
6804 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
6805 const struct wined3d_state *state, DWORD state_id)
6807 context->select_shader = 1;
6810 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
6812 {STATE_VDECL, {STATE_VDECL, vertexdeclaration }, WINED3D_GL_EXT_NONE },
6813 {STATE_VSHADER, {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6814 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
6815 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), state_specularenable }, WINED3D_GL_EXT_NONE },
6816 /* Clip planes */
6817 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
6818 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
6819 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
6820 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
6821 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
6822 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
6823 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
6824 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
6825 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
6826 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
6827 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
6828 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
6829 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
6830 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
6831 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
6832 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
6833 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
6834 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
6835 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
6836 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
6837 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
6838 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
6839 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
6840 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
6841 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
6842 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
6843 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
6844 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
6845 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
6846 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
6847 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
6848 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
6849 /* Lights */
6850 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6851 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), light }, WINED3D_GL_EXT_NONE },
6852 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), light }, WINED3D_GL_EXT_NONE },
6853 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), light }, WINED3D_GL_EXT_NONE },
6854 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), light }, WINED3D_GL_EXT_NONE },
6855 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), light }, WINED3D_GL_EXT_NONE },
6856 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), light }, WINED3D_GL_EXT_NONE },
6857 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), light }, WINED3D_GL_EXT_NONE },
6858 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), light }, WINED3D_GL_EXT_NONE },
6859 /* Viewport */
6860 {STATE_VIEWPORT, {STATE_VIEWPORT, viewport_vertexpart }, WINED3D_GL_EXT_NONE },
6861 /* Transform states */
6862 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), transform_view }, WINED3D_GL_EXT_NONE },
6863 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), transform_projection }, WINED3D_GL_EXT_NONE },
6864 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6865 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6866 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6867 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6868 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6869 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6870 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6871 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6872 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), transform_world }, WINED3D_GL_EXT_NONE },
6873 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6874 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6875 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6876 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6877 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6878 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6879 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6880 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6881 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6882 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6883 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6884 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6885 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6886 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6887 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6888 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6889 /* Fog */
6890 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
6891 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6892 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6893 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6894 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
6895 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
6896 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6897 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), state_ambient }, WINED3D_GL_EXT_NONE },
6898 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6899 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6900 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6901 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6902 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6903 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6904 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6905 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6906 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
6907 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_arb }, ARB_POINT_PARAMETERS },
6908 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_ext }, EXT_POINT_PARAMETERS },
6909 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_w }, WINED3D_GL_EXT_NONE },
6910 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
6911 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
6912 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), state_pscale }, WINED3D_GL_EXT_NONE },
6913 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
6914 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
6915 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
6916 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, ARB_POINT_PARAMETERS },
6917 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, EXT_POINT_PARAMETERS },
6918 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
6919 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6920 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6921 /* Samplers for NP2 texture matrix adjustions. They are not needed if
6922 * GL_ARB_texture_non_power_of_two is supported, so register a NULL state
6923 * handler in that case to get the vertex part of sampler() skipped (VTF
6924 * is handled in the misc states). Otherwise, register
6925 * sampler_texmatrix(), which takes care of updating the texture matrix. */
6926 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6927 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6928 {STATE_SAMPLER(0), {STATE_SAMPLER(0), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6929 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6930 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6931 {STATE_SAMPLER(1), {STATE_SAMPLER(1), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6932 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6933 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6934 {STATE_SAMPLER(2), {STATE_SAMPLER(2), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6935 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6936 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6937 {STATE_SAMPLER(3), {STATE_SAMPLER(3), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6938 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6939 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6940 {STATE_SAMPLER(4), {STATE_SAMPLER(4), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6941 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6942 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6943 {STATE_SAMPLER(5), {STATE_SAMPLER(5), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6944 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6945 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6946 {STATE_SAMPLER(6), {STATE_SAMPLER(6), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6947 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6948 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6949 {STATE_SAMPLER(7), {STATE_SAMPLER(7), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6950 {STATE_POINT_SIZE_ENABLE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6951 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
6954 /* TODO:
6955 * - This currently depends on GL fixed function functions to set things
6956 * like light parameters. Ideally we'd use regular uniforms for that.
6957 * - In part because of the previous point, much of this is modelled after
6958 * GL fixed function, and has much of the same limitations. For example,
6959 * D3D spot lights are slightly different from GL spot lights.
6960 * - We can now implement drawing transformed vertices using the GLSL pipe,
6961 * instead of using the immediate mode fallback.
6962 * - Similarly, we don't need the fallback for certain combinations of
6963 * material sources anymore.
6964 * - Implement vertex blending and vertex tweening.
6965 * - Handle WINED3D_TSS_TEXCOORD_INDEX in the shader, instead of duplicating
6966 * attribute arrays in load_tex_coords().
6967 * - Per-vertex point sizes. */
6968 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
6970 glsl_vertex_pipe_vp_enable,
6971 glsl_vertex_pipe_vp_get_caps,
6972 glsl_vertex_pipe_vp_alloc,
6973 glsl_vertex_pipe_vp_free,
6974 glsl_vertex_pipe_vp_states,
6977 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
6979 /* Nothing to do. */
6982 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
6984 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
6985 | WINED3D_FRAGMENT_CAP_SRGB_WRITE;
6986 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP;
6987 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
6988 | WINED3DTEXOPCAPS_SELECTARG1
6989 | WINED3DTEXOPCAPS_SELECTARG2
6990 | WINED3DTEXOPCAPS_MODULATE4X
6991 | WINED3DTEXOPCAPS_MODULATE2X
6992 | WINED3DTEXOPCAPS_MODULATE
6993 | WINED3DTEXOPCAPS_ADDSIGNED2X
6994 | WINED3DTEXOPCAPS_ADDSIGNED
6995 | WINED3DTEXOPCAPS_ADD
6996 | WINED3DTEXOPCAPS_SUBTRACT
6997 | WINED3DTEXOPCAPS_ADDSMOOTH
6998 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
6999 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
7000 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
7001 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
7002 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
7003 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
7004 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
7005 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
7006 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
7007 | WINED3DTEXOPCAPS_DOTPRODUCT3
7008 | WINED3DTEXOPCAPS_MULTIPLYADD
7009 | WINED3DTEXOPCAPS_LERP
7010 | WINED3DTEXOPCAPS_BUMPENVMAP
7011 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
7012 caps->MaxTextureBlendStages = 8;
7013 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
7016 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7018 struct shader_glsl_priv *priv;
7020 if (shader_backend == &glsl_shader_backend)
7022 priv = shader_priv;
7024 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
7026 ERR("Failed to initialize rbtree.\n");
7027 return NULL;
7030 return priv;
7033 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
7035 return NULL;
7038 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
7040 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7041 struct glsl_ffp_fragment_shader, entry.entry);
7042 struct glsl_shader_prog_link *program, *program2;
7043 struct glsl_ffp_destroy_ctx *ctx = context;
7045 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7046 struct glsl_shader_prog_link, ps.shader_entry)
7048 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7050 ctx->gl_info->gl_ops.ext.p_glDeleteObjectARB(shader->id);
7051 HeapFree(GetProcessHeap(), 0, shader);
7054 /* Context activation is done by the caller. */
7055 static void glsl_fragment_pipe_free(struct wined3d_device *device)
7057 struct shader_glsl_priv *priv = device->fragment_priv;
7058 struct glsl_ffp_destroy_ctx ctx;
7060 ctx.priv = priv;
7061 ctx.gl_info = &device->adapter->gl_info;
7062 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
7065 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
7066 const struct wined3d_state *state, DWORD state_id)
7068 context->last_was_pshader = use_ps(state);
7070 context->select_shader = 1;
7071 context->load_constants = 1;
7074 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
7075 const struct wined3d_state *state, DWORD state_id)
7077 BOOL use_vshader = use_vs(state);
7078 enum fogsource new_source;
7080 context->select_shader = 1;
7081 context->load_constants = 1;
7083 if (!state->render_states[WINED3D_RS_FOGENABLE])
7084 return;
7086 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
7088 if (use_vshader)
7089 new_source = FOGSOURCE_VS;
7090 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->last_was_rhw)
7091 new_source = FOGSOURCE_COORD;
7092 else
7093 new_source = FOGSOURCE_FFP;
7095 else
7097 new_source = FOGSOURCE_FFP;
7100 if (new_source != context->fog_source)
7102 context->fog_source = new_source;
7103 state_fogstartend(context, state, STATE_RENDER(WINED3D_RS_FOGSTART));
7107 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
7108 const struct wined3d_state *state, DWORD state_id)
7110 context->select_shader = 1;
7111 context->load_constants = 1;
7114 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
7115 const struct wined3d_state *state, DWORD state_id)
7117 context->load_constants = 1;
7120 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
7122 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7123 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7124 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7125 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7126 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7127 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7128 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7129 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7130 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7131 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7132 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7133 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7134 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7135 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7136 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7137 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
7138 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7139 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7140 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7141 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7142 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7143 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7144 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7145 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7146 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7147 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7148 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7149 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7150 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7151 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7152 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
7153 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7154 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7155 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7156 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7157 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7158 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7159 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7160 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7161 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7162 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7163 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7164 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7165 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7166 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7167 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
7168 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7169 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7170 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7171 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7172 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7173 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7174 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7175 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7176 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7177 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7178 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7179 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7180 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7181 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7182 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
7183 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7184 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7185 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7186 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7187 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7188 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7189 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7190 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7191 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7192 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7193 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7194 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7195 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7196 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7197 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
7198 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7199 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7200 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7201 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7202 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7203 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7204 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7205 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7206 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7207 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7208 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7209 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7210 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7211 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7212 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
7213 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7214 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7215 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7216 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7217 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7218 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7219 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7220 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7221 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7222 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7223 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7224 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7225 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7226 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7227 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
7228 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7229 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7230 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7231 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7232 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7233 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7234 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7235 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7236 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7237 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7238 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7239 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7240 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7241 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7242 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
7243 {STATE_PIXELSHADER, {STATE_PIXELSHADER, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
7244 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
7245 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7246 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7247 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
7248 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
7249 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
7250 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7251 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
7252 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
7253 {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 },
7254 {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 },
7255 {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 },
7256 {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 },
7257 {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 },
7258 {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 },
7259 {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 },
7260 {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 },
7261 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7262 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
7265 const struct fragment_pipeline glsl_fragment_pipe =
7267 glsl_fragment_pipe_enable,
7268 glsl_fragment_pipe_get_caps,
7269 glsl_fragment_pipe_alloc,
7270 glsl_fragment_pipe_free,
7271 shader_glsl_color_fixup_supported,
7272 glsl_fragment_pipe_state_template,