wined3d: Add support for GLSL based fixed function vertex shaders.
[wine/multimedia.git] / dlls / wined3d / glsl_shader.c
blob60a904a57f06fc82cc4d55aa3c6dbfaf63d4c4ae
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 struct glsl_dst_param
52 char reg_name[150];
53 char mask_str[6];
56 struct glsl_src_param
58 char reg_name[150];
59 char param_str[200];
62 struct glsl_sample_function
64 const char *name;
65 DWORD coord_mask;
68 enum heap_node_op
70 HEAP_NODE_TRAVERSE_LEFT,
71 HEAP_NODE_TRAVERSE_RIGHT,
72 HEAP_NODE_POP,
75 struct constant_entry
77 unsigned int idx;
78 unsigned int version;
81 struct constant_heap
83 struct constant_entry *entries;
84 unsigned int *positions;
85 unsigned int size;
88 /* GLSL shader private data */
89 struct shader_glsl_priv {
90 struct wined3d_shader_buffer shader_buffer;
91 struct wine_rb_tree program_lookup;
92 struct glsl_shader_prog_link *glsl_program;
93 struct constant_heap vconst_heap;
94 struct constant_heap pconst_heap;
95 unsigned char *stack;
96 GLhandleARB depth_blt_program_full[tex_type_count];
97 GLhandleARB depth_blt_program_masked[tex_type_count];
98 UINT next_constant_version;
100 const struct wined3d_vertex_pipe_ops *vertex_pipe;
101 const struct fragment_pipeline *fragment_pipe;
102 struct wine_rb_tree ffp_vertex_shaders;
103 struct wine_rb_tree ffp_fragment_shaders;
104 BOOL ffp_proj_control;
107 struct glsl_vs_program
109 struct list shader_entry;
110 GLhandleARB id;
111 GLenum vertex_color_clamp;
112 GLint *uniform_f_locations;
113 GLint uniform_i_locations[MAX_CONST_I];
114 GLint pos_fixup_location;
117 struct glsl_gs_program
119 struct list shader_entry;
120 GLhandleARB id;
123 struct glsl_ps_program
125 struct list shader_entry;
126 GLhandleARB id;
127 GLint *uniform_f_locations;
128 GLint uniform_i_locations[MAX_CONST_I];
129 GLint bumpenv_mat_location[MAX_TEXTURES];
130 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
131 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
132 GLint tex_factor_location;
133 GLint specular_enable_location;
134 GLint ycorrection_location;
135 GLint np2_fixup_location;
136 const struct ps_np2fixup_info *np2_fixup_info;
139 /* Struct to maintain data about a linked GLSL program */
140 struct glsl_shader_prog_link
142 struct wine_rb_entry program_lookup_entry;
143 struct glsl_vs_program vs;
144 struct glsl_gs_program gs;
145 struct glsl_ps_program ps;
146 GLhandleARB programId;
147 UINT constant_version;
150 struct glsl_program_key
152 GLhandleARB vs_id;
153 GLhandleARB gs_id;
154 GLhandleARB ps_id;
157 struct shader_glsl_ctx_priv {
158 const struct vs_compile_args *cur_vs_args;
159 const struct ps_compile_args *cur_ps_args;
160 struct ps_np2fixup_info *cur_np2fixup_info;
163 struct glsl_ps_compiled_shader
165 struct ps_compile_args args;
166 struct ps_np2fixup_info np2fixup;
167 GLhandleARB prgId;
170 struct glsl_vs_compiled_shader
172 struct vs_compile_args args;
173 GLhandleARB prgId;
176 struct glsl_gs_compiled_shader
178 GLhandleARB id;
181 struct glsl_shader_private
183 union
185 struct glsl_vs_compiled_shader *vs;
186 struct glsl_gs_compiled_shader *gs;
187 struct glsl_ps_compiled_shader *ps;
188 } gl_shaders;
189 UINT num_gl_shaders, shader_array_size;
192 struct glsl_ffp_vertex_shader
194 struct wined3d_ffp_vs_desc desc;
195 GLhandleARB id;
196 struct list linked_programs;
199 struct glsl_ffp_fragment_shader
201 struct ffp_frag_desc entry;
202 GLhandleARB id;
203 struct list linked_programs;
206 struct glsl_ffp_destroy_ctx
208 struct shader_glsl_priv *priv;
209 const struct wined3d_gl_info *gl_info;
212 static const char *debug_gl_shader_type(GLenum type)
214 switch (type)
216 #define WINED3D_TO_STR(u) case u: return #u
217 WINED3D_TO_STR(GL_VERTEX_SHADER_ARB);
218 WINED3D_TO_STR(GL_GEOMETRY_SHADER_ARB);
219 WINED3D_TO_STR(GL_FRAGMENT_SHADER_ARB);
220 #undef WINED3D_TO_STR
221 default:
222 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
226 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
228 switch (type)
230 case WINED3D_SHADER_TYPE_VERTEX:
231 return "vs";
233 case WINED3D_SHADER_TYPE_GEOMETRY:
234 return "gs";
236 case WINED3D_SHADER_TYPE_PIXEL:
237 return "ps";
239 default:
240 FIXME("Unhandled shader type %#x.\n", type);
241 return "unknown";
245 /* Extract a line from the info log.
246 * Note that this modifies the source string. */
247 static char *get_info_log_line(char **ptr)
249 char *p, *q;
251 p = *ptr;
252 if (!(q = strstr(p, "\n")))
254 if (!*p) return NULL;
255 *ptr += strlen(p);
256 return p;
258 *q = '\0';
259 *ptr = q + 1;
261 return p;
264 /* Context activation is done by the caller. */
265 static void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLhandleARB obj)
267 int infologLength = 0;
268 char *infoLog;
270 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
271 return;
273 GL_EXTCALL(glGetObjectParameterivARB(obj,
274 GL_OBJECT_INFO_LOG_LENGTH_ARB,
275 &infologLength));
277 /* A size of 1 is just a null-terminated string, so the log should be bigger than
278 * that if there are errors. */
279 if (infologLength > 1)
281 char *ptr, *line;
283 infoLog = HeapAlloc(GetProcessHeap(), 0, infologLength);
284 /* The info log is supposed to be zero-terminated, but at least some
285 * versions of fglrx don't terminate the string properly. The reported
286 * length does include the terminator, so explicitly set it to zero
287 * here. */
288 infoLog[infologLength - 1] = 0;
289 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
291 ptr = infoLog;
292 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
294 WARN("Info log received from GLSL shader #%u:\n", obj);
295 while ((line = get_info_log_line(&ptr))) WARN(" %s\n", line);
297 else
299 FIXME("Info log received from GLSL shader #%u:\n", obj);
300 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
302 HeapFree(GetProcessHeap(), 0, infoLog);
306 /* Context activation is done by the caller. */
307 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLhandleARB shader, const char *src)
309 TRACE("Compiling shader object %u.\n", shader);
310 GL_EXTCALL(glShaderSourceARB(shader, 1, &src, NULL));
311 checkGLcall("glShaderSourceARB");
312 GL_EXTCALL(glCompileShaderARB(shader));
313 checkGLcall("glCompileShaderARB");
314 print_glsl_info_log(gl_info, shader);
317 /* Context activation is done by the caller. */
318 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLhandleARB program)
320 GLint i, object_count, source_size = -1;
321 GLhandleARB *objects;
322 char *source = NULL;
324 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_ATTACHED_OBJECTS_ARB, &object_count));
325 objects = HeapAlloc(GetProcessHeap(), 0, object_count * sizeof(*objects));
326 if (!objects)
328 ERR("Failed to allocate object array memory.\n");
329 return;
332 GL_EXTCALL(glGetAttachedObjectsARB(program, object_count, NULL, objects));
333 for (i = 0; i < object_count; ++i)
335 char *ptr, *line;
336 GLint tmp;
338 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &tmp));
340 if (source_size < tmp)
342 HeapFree(GetProcessHeap(), 0, source);
344 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
345 if (!source)
347 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
348 HeapFree(GetProcessHeap(), 0, objects);
349 return;
351 source_size = tmp;
354 FIXME("Object %u:\n", objects[i]);
355 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SUBTYPE_ARB, &tmp));
356 FIXME(" GL_OBJECT_SUBTYPE_ARB: %s.\n", debug_gl_shader_type(tmp));
357 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_COMPILE_STATUS_ARB, &tmp));
358 FIXME(" GL_OBJECT_COMPILE_STATUS_ARB: %d.\n", tmp);
359 FIXME("\n");
361 ptr = source;
362 GL_EXTCALL(glGetShaderSourceARB(objects[i], source_size, NULL, source));
363 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
364 FIXME("\n");
367 HeapFree(GetProcessHeap(), 0, source);
368 HeapFree(GetProcessHeap(), 0, objects);
371 /* Context activation is done by the caller. */
372 static void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLhandleARB program)
374 GLint tmp;
376 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader)) return;
378 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_TYPE_ARB, &tmp));
379 if (tmp == GL_PROGRAM_OBJECT_ARB)
381 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &tmp));
382 if (!tmp)
384 FIXME("Program %u link status invalid.\n", program);
385 shader_glsl_dump_program_source(gl_info, program);
389 print_glsl_info_log(gl_info, program);
392 /* Context activation is done by the caller. */
393 static void shader_glsl_load_psamplers(const struct wined3d_gl_info *gl_info,
394 const DWORD *tex_unit_map, GLhandleARB programId)
396 GLint name_loc;
397 char sampler_name[20];
398 unsigned int i;
400 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
402 snprintf(sampler_name, sizeof(sampler_name), "ps_sampler%u", i);
403 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
404 if (name_loc != -1) {
405 DWORD mapped_unit = tex_unit_map[i];
406 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.fragment_samplers)
408 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
409 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
410 checkGLcall("glUniform1iARB");
411 } else {
412 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
418 /* Context activation is done by the caller. */
419 static void shader_glsl_load_vsamplers(const struct wined3d_gl_info *gl_info,
420 const DWORD *tex_unit_map, GLhandleARB programId)
422 GLint name_loc;
423 char sampler_name[20];
424 unsigned int i;
426 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
428 snprintf(sampler_name, sizeof(sampler_name), "vs_sampler%u", i);
429 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
430 if (name_loc != -1) {
431 DWORD mapped_unit = tex_unit_map[MAX_FRAGMENT_SAMPLERS + i];
432 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.combined_samplers)
434 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
435 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
436 checkGLcall("glUniform1iARB");
437 } else {
438 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
444 /* Context activation is done by the caller. */
445 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
446 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
448 int stack_idx = 0;
449 unsigned int heap_idx = 1;
450 unsigned int idx;
452 if (heap->entries[heap_idx].version <= version) return;
454 idx = heap->entries[heap_idx].idx;
455 if (constant_locations[idx] != -1) GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
456 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
458 while (stack_idx >= 0)
460 /* Note that we fall through to the next case statement. */
461 switch(stack[stack_idx])
463 case HEAP_NODE_TRAVERSE_LEFT:
465 unsigned int left_idx = heap_idx << 1;
466 if (left_idx < heap->size && heap->entries[left_idx].version > version)
468 heap_idx = left_idx;
469 idx = heap->entries[heap_idx].idx;
470 if (constant_locations[idx] != -1)
471 GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
473 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
474 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
475 break;
479 case HEAP_NODE_TRAVERSE_RIGHT:
481 unsigned int right_idx = (heap_idx << 1) + 1;
482 if (right_idx < heap->size && heap->entries[right_idx].version > version)
484 heap_idx = right_idx;
485 idx = heap->entries[heap_idx].idx;
486 if (constant_locations[idx] != -1)
487 GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
489 stack[stack_idx++] = HEAP_NODE_POP;
490 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
491 break;
495 case HEAP_NODE_POP:
496 heap_idx >>= 1;
497 --stack_idx;
498 break;
501 checkGLcall("walk_constant_heap()");
504 /* Context activation is done by the caller. */
505 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
507 GLfloat clamped_constant[4];
509 if (location == -1) return;
511 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
512 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
513 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
514 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
516 GL_EXTCALL(glUniform4fvARB(location, 1, clamped_constant));
519 /* Context activation is done by the caller. */
520 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
521 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
523 int stack_idx = 0;
524 unsigned int heap_idx = 1;
525 unsigned int idx;
527 if (heap->entries[heap_idx].version <= version) return;
529 idx = heap->entries[heap_idx].idx;
530 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
531 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
533 while (stack_idx >= 0)
535 /* Note that we fall through to the next case statement. */
536 switch(stack[stack_idx])
538 case HEAP_NODE_TRAVERSE_LEFT:
540 unsigned int left_idx = heap_idx << 1;
541 if (left_idx < heap->size && heap->entries[left_idx].version > version)
543 heap_idx = left_idx;
544 idx = heap->entries[heap_idx].idx;
545 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
547 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
548 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
549 break;
553 case HEAP_NODE_TRAVERSE_RIGHT:
555 unsigned int right_idx = (heap_idx << 1) + 1;
556 if (right_idx < heap->size && heap->entries[right_idx].version > version)
558 heap_idx = right_idx;
559 idx = heap->entries[heap_idx].idx;
560 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
562 stack[stack_idx++] = HEAP_NODE_POP;
563 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
564 break;
568 case HEAP_NODE_POP:
569 heap_idx >>= 1;
570 --stack_idx;
571 break;
574 checkGLcall("walk_constant_heap_clamped()");
577 /* Context activation is done by the caller. */
578 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
579 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
580 unsigned char *stack, UINT version)
582 const struct wined3d_shader_lconst *lconst;
584 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
585 if (shader->reg_maps.shader_version.major == 1
586 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
587 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
588 else
589 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
591 if (!shader->load_local_constsF)
593 TRACE("No need to load local float constants for this shader\n");
594 return;
597 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
598 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
600 GL_EXTCALL(glUniform4fvARB(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
602 checkGLcall("glUniform4fvARB()");
605 /* Context activation is done by the caller. */
606 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
607 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
609 unsigned int i;
610 struct list* ptr;
612 for (i = 0; constants_set; constants_set >>= 1, ++i)
614 if (!(constants_set & 1)) continue;
616 TRACE_(d3d_constants)("Loading constants %u: %i, %i, %i, %i\n",
617 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
619 /* We found this uniform name in the program - go ahead and send the data */
620 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
621 checkGLcall("glUniform4ivARB");
624 /* Load immediate constants */
625 ptr = list_head(&shader->constantsI);
626 while (ptr)
628 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
629 unsigned int idx = lconst->idx;
630 const GLint *values = (const GLint *)lconst->value;
632 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
633 values[0], values[1], values[2], values[3]);
635 /* We found this uniform name in the program - go ahead and send the data */
636 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
637 checkGLcall("glUniform4ivARB");
638 ptr = list_next(&shader->constantsI, ptr);
642 /* Context activation is done by the caller. */
643 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
644 GLhandleARB programId, const BOOL *constants, WORD constants_set)
646 GLint tmp_loc;
647 unsigned int i;
648 char tmp_name[10];
649 const char *prefix;
650 struct list* ptr;
652 prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
654 /* TODO: Benchmark and see if it would be beneficial to store the
655 * locations of the constants to avoid looking up each time */
656 for (i = 0; constants_set; constants_set >>= 1, ++i)
658 if (!(constants_set & 1)) continue;
660 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i]);
662 /* TODO: Benchmark and see if it would be beneficial to store the
663 * locations of the constants to avoid looking up each time */
664 snprintf(tmp_name, sizeof(tmp_name), "%s_b[%i]", prefix, i);
665 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
666 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
669 /* Load immediate constants */
670 ptr = list_head(&shader->constantsB);
671 while (ptr)
673 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
674 unsigned int idx = lconst->idx;
675 const GLint *values = (const GLint *)lconst->value;
677 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
679 snprintf(tmp_name, sizeof(tmp_name), "%s_b[%i]", prefix, idx);
680 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
681 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
682 ptr = list_next(&shader->constantsB, ptr);
685 checkGLcall("shader_glsl_load_constantsB()");
688 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
690 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
693 /* Context activation is done by the caller (state handler). */
694 static void shader_glsl_load_np2fixup_constants(void *shader_priv,
695 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
697 struct shader_glsl_priv *glsl_priv = shader_priv;
698 const struct glsl_shader_prog_link *prog = glsl_priv->glsl_program;
700 /* No GLSL program set - nothing to do. */
701 if (!prog) return;
703 /* NP2 texcoord fixup is (currently) only done for pixelshaders. */
704 if (!use_ps(state)) return;
706 if (prog->ps.np2_fixup_info && prog->ps.np2_fixup_location != -1)
708 UINT i;
709 UINT fixup = prog->ps.np2_fixup_info->active;
710 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
712 for (i = 0; fixup; fixup >>= 1, ++i)
714 const struct wined3d_texture *tex = state->textures[i];
715 const unsigned char idx = prog->ps.np2_fixup_info->idx[i];
716 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
718 if (!tex)
720 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
721 continue;
724 if (idx % 2)
726 tex_dim[2] = tex->pow2_matrix[0];
727 tex_dim[3] = tex->pow2_matrix[5];
729 else
731 tex_dim[0] = tex->pow2_matrix[0];
732 tex_dim[1] = tex->pow2_matrix[5];
736 GL_EXTCALL(glUniform4fvARB(prog->ps.np2_fixup_location,
737 prog->ps.np2_fixup_info->num_consts, np2fixup_constants));
741 /* Context activation is done by the caller (state handler). */
742 static void shader_glsl_load_constants(void *shader_priv, const struct wined3d_context *context,
743 const struct wined3d_state *state)
745 const struct wined3d_gl_info *gl_info = context->gl_info;
746 struct shader_glsl_priv *priv = shader_priv;
747 float position_fixup[4];
749 GLhandleARB programId;
750 struct glsl_shader_prog_link *prog = priv->glsl_program;
751 UINT constant_version;
752 int i;
754 if (!prog) {
755 /* No GLSL program set - nothing to do. */
756 return;
758 programId = prog->programId;
759 constant_version = prog->constant_version;
761 if (use_vs(state))
763 const struct wined3d_shader *vshader = state->vertex_shader;
765 /* Load DirectX 9 float constants/uniforms for vertex shader */
766 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
767 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
769 /* Load DirectX 9 integer constants/uniforms for vertex shader */
770 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
771 vshader->reg_maps.integer_constants);
773 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
774 shader_glsl_load_constantsB(vshader, gl_info, programId, state->vs_consts_b,
775 vshader->reg_maps.boolean_constants);
777 /* Upload the position fixup params */
778 shader_get_position_fixup(context, state, position_fixup);
779 GL_EXTCALL(glUniform4fvARB(prog->vs.pos_fixup_location, 1, position_fixup));
780 checkGLcall("glUniform4fvARB");
783 if (use_ps(state))
785 const struct wined3d_shader *pshader = state->pixel_shader;
787 /* Load DirectX 9 float constants/uniforms for pixel shader */
788 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
789 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
791 /* Load DirectX 9 integer constants/uniforms for pixel shader */
792 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
793 pshader->reg_maps.integer_constants);
795 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
796 shader_glsl_load_constantsB(pshader, gl_info, programId, state->ps_consts_b,
797 pshader->reg_maps.boolean_constants);
799 /* Upload the environment bump map matrix if needed. The needsbumpmat
800 * member specifies the texture stage to load the matrix from. It
801 * can't be 0 for a valid texbem instruction. */
802 for (i = 0; i < MAX_TEXTURES; ++i)
804 const float *data;
806 if (prog->ps.bumpenv_mat_location[i] == -1)
807 continue;
809 data = (const float *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00];
810 GL_EXTCALL(glUniformMatrix2fvARB(prog->ps.bumpenv_mat_location[i], 1, 0, data));
811 checkGLcall("glUniformMatrix2fvARB");
813 /* texbeml needs the luminance scale and offset too. If texbeml
814 * is used, needsbumpmat is set too, so we can check that in the
815 * needsbumpmat check. */
816 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
818 const GLfloat *scale = (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE];
819 const GLfloat *offset = (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET];
821 GL_EXTCALL(glUniform1fvARB(prog->ps.bumpenv_lum_scale_location[i], 1, scale));
822 checkGLcall("glUniform1fvARB");
823 GL_EXTCALL(glUniform1fvARB(prog->ps.bumpenv_lum_offset_location[i], 1, offset));
824 checkGLcall("glUniform1fvARB");
828 if (prog->ps.ycorrection_location != -1)
830 float correction_params[4];
832 if (context->render_offscreen)
834 correction_params[0] = 0.0f;
835 correction_params[1] = 1.0f;
836 } else {
837 /* position is window relative, not viewport relative */
838 correction_params[0] = (float) context->current_rt->resource.height;
839 correction_params[1] = -1.0f;
841 GL_EXTCALL(glUniform4fvARB(prog->ps.ycorrection_location, 1, correction_params));
844 else if (priv->fragment_pipe == &glsl_fragment_pipe)
846 float col[4];
848 for (i = 0; i < MAX_TEXTURES; ++i)
850 GL_EXTCALL(glUniformMatrix2fvARB(prog->ps.bumpenv_mat_location[i], 1, 0,
851 (const float *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
852 GL_EXTCALL(glUniform1fARB(prog->ps.bumpenv_lum_scale_location[i],
853 *(const float *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
854 GL_EXTCALL(glUniform1fARB(prog->ps.bumpenv_lum_offset_location[i],
855 *(const float *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
858 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_TEXTUREFACTOR], col);
859 GL_EXTCALL(glUniform4fARB(prog->ps.tex_factor_location, col[0], col[1], col[2], col[3]));
861 if (state->render_states[WINED3D_RS_SPECULARENABLE])
862 GL_EXTCALL(glUniform4fARB(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
863 else
864 GL_EXTCALL(glUniform4fARB(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
866 checkGLcall("fixed function uniforms");
869 if (priv->next_constant_version == UINT_MAX)
871 TRACE("Max constant version reached, resetting to 0.\n");
872 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
873 priv->next_constant_version = 1;
875 else
877 prog->constant_version = priv->next_constant_version++;
881 static void update_heap_entry(const struct constant_heap *heap, unsigned int idx,
882 unsigned int heap_idx, DWORD new_version)
884 struct constant_entry *entries = heap->entries;
885 unsigned int *positions = heap->positions;
886 unsigned int parent_idx;
888 while (heap_idx > 1)
890 parent_idx = heap_idx >> 1;
892 if (new_version <= entries[parent_idx].version) break;
894 entries[heap_idx] = entries[parent_idx];
895 positions[entries[parent_idx].idx] = heap_idx;
896 heap_idx = parent_idx;
899 entries[heap_idx].version = new_version;
900 entries[heap_idx].idx = idx;
901 positions[idx] = heap_idx;
904 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
906 struct shader_glsl_priv *priv = device->shader_priv;
907 struct constant_heap *heap = &priv->vconst_heap;
908 UINT i;
910 for (i = start; i < count + start; ++i)
912 if (!device->stateBlock->changed.vertexShaderConstantsF[i])
913 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
914 else
915 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
919 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
921 struct shader_glsl_priv *priv = device->shader_priv;
922 struct constant_heap *heap = &priv->pconst_heap;
923 UINT i;
925 for (i = start; i < count + start; ++i)
927 if (!device->stateBlock->changed.pixelShaderConstantsF[i])
928 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
929 else
930 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
934 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
936 unsigned int ret = gl_info->limits.glsl_varyings / 4;
937 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
938 if(shader_major > 3) return ret;
940 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
941 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
942 return ret;
945 /** Generate the variable & register declarations for the GLSL output target */
946 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
947 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
948 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
950 const struct wined3d_shader_version *version = &reg_maps->shader_version;
951 const struct wined3d_state *state = &shader->device->stateBlock->state;
952 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
953 const struct wined3d_gl_info *gl_info = context->gl_info;
954 const struct wined3d_fb_state *fb = &shader->device->fb;
955 unsigned int i, extra_constants_needed = 0;
956 const struct wined3d_shader_lconst *lconst;
957 const char *prefix;
958 DWORD map;
960 prefix = shader_glsl_get_prefix(version->type);
962 /* Prototype the subroutines */
963 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
965 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
968 /* Declare the constants (aka uniforms) */
969 if (shader->limits.constant_float > 0)
971 unsigned max_constantsF;
973 /* Unless the shader uses indirect addressing, always declare the
974 * maximum array size and ignore that we need some uniforms privately.
975 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
976 * and immediate values, still declare VC[256]. If the shader needs
977 * more uniforms than we have it won't work in any case. If it uses
978 * less, the compiler will figure out which uniforms are really used
979 * and strip them out. This allows a shader to use c255 on a dx9 card,
980 * as long as it doesn't also use all the other constants.
982 * If the shader uses indirect addressing the compiler must assume
983 * that all declared uniforms are used. In this case, declare only the
984 * amount that we're assured to have.
986 * Thus we run into problems in these two cases:
987 * 1) The shader really uses more uniforms than supported.
988 * 2) The shader uses indirect addressing, less constants than
989 * supported, but uses a constant index > #supported consts. */
990 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
992 /* No indirect addressing here. */
993 max_constantsF = gl_info->limits.glsl_ps_float_constants;
995 else
997 if (reg_maps->usesrelconstF)
999 /* Subtract the other potential uniforms from the max
1000 * available (bools, ints, and 1 row of projection matrix).
1001 * Subtract another uniform for immediate values, which have
1002 * to be loaded via uniform by the driver as well. The shader
1003 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1004 * shader code, so one vec4 should be enough. (Unfortunately
1005 * the Nvidia driver doesn't store 128 and -128 in one float).
1007 * Writing gl_ClipVertex requires one uniform for each
1008 * clipplane as well. */
1009 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1010 if(ctx_priv->cur_vs_args->clip_enabled)
1012 max_constantsF -= gl_info->limits.clipplanes;
1014 max_constantsF -= count_bits(reg_maps->integer_constants);
1015 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1016 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1017 * for now take this into account when calculating the number of available constants
1019 max_constantsF -= count_bits(reg_maps->boolean_constants);
1020 /* Set by driver quirks in directx.c */
1021 max_constantsF -= gl_info->reserved_glsl_constants;
1023 if (max_constantsF < shader->limits.constant_float)
1025 static unsigned int once;
1027 if (!once++)
1028 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1029 " it may not render correctly.\n");
1030 else
1031 WARN("The hardware does not support enough uniform components to run this shader.\n");
1034 else
1036 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1039 max_constantsF = min(shader->limits.constant_float, max_constantsF);
1040 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1043 /* Always declare the full set of constants, the compiler can remove the
1044 * unused ones because d3d doesn't (yet) support indirect int and bool
1045 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1046 if (shader->limits.constant_int > 0 && reg_maps->integer_constants)
1047 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits.constant_int);
1049 if (shader->limits.constant_bool > 0 && reg_maps->boolean_constants)
1050 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits.constant_bool);
1052 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1054 if (reg_maps->cb_sizes[i])
1055 shader_addline(buffer, "uniform vec4 %s_cb%u[%u];\n", prefix, i, reg_maps->cb_sizes[i]);
1058 /* Declare texture samplers */
1059 for (i = 0; i < shader->limits.sampler; ++i)
1061 if (reg_maps->sampler_type[i])
1063 BOOL shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1 << i));
1064 BOOL tex_rect;
1066 switch (reg_maps->sampler_type[i])
1068 case WINED3DSTT_1D:
1069 if (shadow_sampler)
1070 shader_addline(buffer, "uniform sampler1DShadow %s_sampler%u;\n", prefix, i);
1071 else
1072 shader_addline(buffer, "uniform sampler1D %s_sampler%u;\n", prefix, i);
1073 break;
1074 case WINED3DSTT_2D:
1075 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->np2_fixup & (1 << i));
1076 tex_rect = tex_rect && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1077 if (shadow_sampler)
1079 if (tex_rect)
1080 shader_addline(buffer, "uniform sampler2DRectShadow %s_sampler%u;\n", prefix, i);
1081 else
1082 shader_addline(buffer, "uniform sampler2DShadow %s_sampler%u;\n", prefix, i);
1084 else
1086 if (tex_rect)
1087 shader_addline(buffer, "uniform sampler2DRect %s_sampler%u;\n", prefix, i);
1088 else
1089 shader_addline(buffer, "uniform sampler2D %s_sampler%u;\n", prefix, i);
1091 break;
1092 case WINED3DSTT_CUBE:
1093 if (shadow_sampler)
1094 FIXME("Unsupported Cube shadow sampler.\n");
1095 shader_addline(buffer, "uniform samplerCube %s_sampler%u;\n", prefix, i);
1096 break;
1097 case WINED3DSTT_VOLUME:
1098 if (shadow_sampler)
1099 FIXME("Unsupported 3D shadow sampler.\n");
1100 shader_addline(buffer, "uniform sampler3D %s_sampler%u;\n", prefix, i);
1101 break;
1102 default:
1103 shader_addline(buffer, "uniform unsupported_sampler %s_sampler%u;\n", prefix, i);
1104 FIXME("Unrecognized sampler type: %#x\n", reg_maps->sampler_type[i]);
1105 break;
1110 /* Declare uniforms for NP2 texcoord fixup:
1111 * This is NOT done inside the loop that declares the texture samplers
1112 * since the NP2 fixup code is currently only used for the GeforceFX
1113 * series and when forcing the ARB_npot extension off. Modern cards just
1114 * skip the code anyway, so put it inside a separate loop. */
1115 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1117 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1118 UINT cur = 0;
1120 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1121 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1122 * samplerNP2Fixup stores texture dimensions and is updated through
1123 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1125 for (i = 0; i < shader->limits.sampler; ++i)
1127 if (reg_maps->sampler_type[i])
1129 if (!(ps_args->np2_fixup & (1 << i))) continue;
1131 if (WINED3DSTT_2D != reg_maps->sampler_type[i]) {
1132 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1133 continue;
1136 fixup->idx[i] = cur++;
1140 fixup->num_consts = (cur + 1) >> 1;
1141 fixup->active = ps_args->np2_fixup;
1142 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1145 /* Declare address variables */
1146 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1148 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1151 /* Declare texture coordinate temporaries and initialize them */
1152 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1154 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1157 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1159 /* Declare attributes. */
1160 for (i = 0, map = reg_maps->input_registers; map; map >>= 1, ++i)
1162 if (map & 1)
1163 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, i);
1166 shader_addline(buffer, "uniform vec4 posFixup;\n");
1167 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits.packed_output);
1169 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1171 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits.packed_input);
1173 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1175 if (version->major >= 3)
1177 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits.packed_input);
1179 if (use_vs(state))
1180 shader_addline(buffer, "varying vec4 %s_in[%u];\n", prefix, in_count);
1181 else
1182 /* TODO: Write a replacement shader for the fixed function
1183 * vertex pipeline, so this isn't needed. For fixed function
1184 * vertex processing + 3.0 pixel shader we need a separate
1185 * function in the pixel shader that reads the fixed function
1186 * color into the packed input registers. */
1187 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1190 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1192 if (!(map & 1))
1193 continue;
1195 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1197 if (reg_maps->luminanceparams & (1 << i))
1199 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1200 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1201 extra_constants_needed++;
1204 extra_constants_needed++;
1207 if (ps_args->srgb_correction)
1209 shader_addline(buffer, "const vec4 srgb_const0 = vec4(%.8e, %.8e, %.8e, %.8e);\n",
1210 srgb_pow, srgb_mul_high, srgb_sub_high, srgb_mul_low);
1211 shader_addline(buffer, "const vec4 srgb_const1 = vec4(%.8e, 0.0, 0.0, 0.0);\n",
1212 srgb_cmp);
1214 if (reg_maps->vpos || reg_maps->usesdsy)
1216 if (shader->limits.constant_float + extra_constants_needed
1217 + 1 < gl_info->limits.glsl_ps_float_constants)
1219 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1220 extra_constants_needed++;
1222 else
1224 /* This happens because we do not have proper tracking of the constant registers that are
1225 * actually used, only the max limit of the shader version
1227 FIXME("Cannot find a free uniform for vpos correction params\n");
1228 shader_addline(buffer, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
1229 context->render_offscreen ? 0.0f : fb->render_targets[0]->resource.height,
1230 context->render_offscreen ? 1.0f : -1.0f);
1232 shader_addline(buffer, "vec4 vpos;\n");
1236 /* Declare output register temporaries */
1237 if (shader->limits.packed_output)
1238 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits.packed_output);
1240 /* Declare temporary variables */
1241 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1243 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1246 /* Declare loop registers aLx */
1247 if (version->major < 4)
1249 for (i = 0; i < reg_maps->loop_depth; ++i)
1251 shader_addline(buffer, "int aL%u;\n", i);
1252 shader_addline(buffer, "int tmpInt%u;\n", i);
1256 /* Temporary variables for matrix operations */
1257 shader_addline(buffer, "vec4 tmp0;\n");
1258 shader_addline(buffer, "vec4 tmp1;\n");
1260 if (!shader->load_local_constsF)
1262 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1264 const float *value;
1265 value = (const float *)lconst->value;
1266 shader_addline(buffer, "const vec4 %s_lc%u = vec4(%.8e, %.8e, %.8e, %.8e);\n",
1267 prefix, lconst->idx, value[0], value[1], value[2], value[3]);
1271 /* Start the main program. */
1272 shader_addline(buffer, "void main()\n{\n");
1274 /* Direct3D applications expect integer vPos values, while OpenGL drivers
1275 * add approximately 0.5. This causes off-by-one problems as spotted by
1276 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
1277 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
1278 * causes precision troubles when we just subtract 0.5.
1280 * To deal with that, just floor() the position. This will eliminate the
1281 * fraction on all cards.
1283 * TODO: Test how this behaves with multisampling.
1285 * An advantage of floor is that it works even if the driver doesn't add
1286 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
1287 * to return in gl_FragCoord, even though coordinates specify the pixel
1288 * centers instead of the pixel corners. This code will behave correctly
1289 * on drivers that returns integer values. */
1290 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
1291 shader_addline(buffer,
1292 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1295 /*****************************************************************************
1296 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1298 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1299 ****************************************************************************/
1301 /* Prototypes */
1302 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1303 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1305 /** Used for opcode modifiers - They multiply the result by the specified amount */
1306 static const char * const shift_glsl_tab[] = {
1307 "", /* 0 (none) */
1308 "2.0 * ", /* 1 (x2) */
1309 "4.0 * ", /* 2 (x4) */
1310 "8.0 * ", /* 3 (x8) */
1311 "16.0 * ", /* 4 (x16) */
1312 "32.0 * ", /* 5 (x32) */
1313 "", /* 6 (x64) */
1314 "", /* 7 (x128) */
1315 "", /* 8 (d256) */
1316 "", /* 9 (d128) */
1317 "", /* 10 (d64) */
1318 "", /* 11 (d32) */
1319 "0.0625 * ", /* 12 (d16) */
1320 "0.125 * ", /* 13 (d8) */
1321 "0.25 * ", /* 14 (d4) */
1322 "0.5 * " /* 15 (d2) */
1325 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1326 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1327 const char *in_reg, const char *in_regswizzle, char *out_str)
1329 out_str[0] = 0;
1331 switch (src_modifier)
1333 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1334 case WINED3DSPSM_DW:
1335 case WINED3DSPSM_NONE:
1336 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1337 break;
1338 case WINED3DSPSM_NEG:
1339 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1340 break;
1341 case WINED3DSPSM_NOT:
1342 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1343 break;
1344 case WINED3DSPSM_BIAS:
1345 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1346 break;
1347 case WINED3DSPSM_BIASNEG:
1348 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1349 break;
1350 case WINED3DSPSM_SIGN:
1351 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1352 break;
1353 case WINED3DSPSM_SIGNNEG:
1354 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1355 break;
1356 case WINED3DSPSM_COMP:
1357 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1358 break;
1359 case WINED3DSPSM_X2:
1360 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1361 break;
1362 case WINED3DSPSM_X2NEG:
1363 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1364 break;
1365 case WINED3DSPSM_ABS:
1366 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1367 break;
1368 case WINED3DSPSM_ABSNEG:
1369 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1370 break;
1371 default:
1372 FIXME("Unhandled modifier %u\n", src_modifier);
1373 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1377 /** Writes the GLSL variable name that corresponds to the register that the
1378 * DX opcode parameter is trying to access */
1379 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1380 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1382 /* oPos, oFog and oPts in D3D */
1383 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
1385 const struct wined3d_shader *shader = ins->ctx->shader;
1386 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1387 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1388 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1389 const char *prefix = shader_glsl_get_prefix(version->type);
1390 struct glsl_src_param rel_param0, rel_param1;
1392 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
1393 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
1394 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
1395 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
1396 *is_color = FALSE;
1398 switch (reg->type)
1400 case WINED3DSPR_TEMP:
1401 sprintf(register_name, "R%u", reg->idx[0].offset);
1402 break;
1404 case WINED3DSPR_INPUT:
1405 /* vertex shaders */
1406 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1408 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1409 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx[0].offset))
1410 *is_color = TRUE;
1411 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
1412 break;
1415 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1417 if (reg->idx[0].rel_addr)
1419 if (reg->idx[1].rel_addr)
1420 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
1421 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1422 else
1423 sprintf(register_name, "gs_in[%s + %u][%u]",
1424 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
1426 else if (reg->idx[1].rel_addr)
1427 sprintf(register_name, "gs_in[%u][%s + %u]",
1428 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1429 else
1430 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
1431 break;
1434 /* pixel shaders >= 3.0 */
1435 if (version->major >= 3)
1437 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
1438 unsigned int in_count = vec4_varyings(version->major, gl_info);
1440 if (reg->idx[0].rel_addr)
1442 /* Removing a + 0 would be an obvious optimization, but
1443 * OS X doesn't see the NOP operation there. */
1444 if (idx)
1446 if (shader->u.ps.declared_in_count > in_count)
1448 sprintf(register_name,
1449 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
1450 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
1451 prefix, rel_param0.param_str, idx);
1453 else
1455 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
1458 else
1460 if (shader->u.ps.declared_in_count > in_count)
1462 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
1463 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
1464 prefix, rel_param0.param_str);
1466 else
1468 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
1472 else
1474 if (idx == in_count) sprintf(register_name, "gl_Color");
1475 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
1476 else sprintf(register_name, "%s_in[%u]", prefix, idx);
1479 else
1481 if (!reg->idx[0].offset)
1482 strcpy(register_name, "gl_Color");
1483 else
1484 strcpy(register_name, "gl_SecondaryColor");
1485 break;
1487 break;
1489 case WINED3DSPR_CONST:
1491 /* Relative addressing */
1492 if (reg->idx[0].rel_addr)
1494 if (reg->idx[0].offset)
1495 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
1496 else
1497 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
1499 else
1501 if (shader_constant_is_local(shader, reg->idx[0].offset))
1502 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
1503 else
1504 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
1507 break;
1509 case WINED3DSPR_CONSTINT:
1510 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
1511 break;
1513 case WINED3DSPR_CONSTBOOL:
1514 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
1515 break;
1517 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1518 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1519 sprintf(register_name, "T%u", reg->idx[0].offset);
1520 else
1521 sprintf(register_name, "A%u", reg->idx[0].offset);
1522 break;
1524 case WINED3DSPR_LOOP:
1525 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
1526 break;
1528 case WINED3DSPR_SAMPLER:
1529 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
1530 break;
1532 case WINED3DSPR_COLOROUT:
1533 if (reg->idx[0].offset >= gl_info->limits.buffers)
1534 WARN("Write to render target %u, only %d supported.\n",
1535 reg->idx[0].offset, gl_info->limits.buffers);
1537 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
1538 break;
1540 case WINED3DSPR_RASTOUT:
1541 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
1542 break;
1544 case WINED3DSPR_DEPTHOUT:
1545 sprintf(register_name, "gl_FragDepth");
1546 break;
1548 case WINED3DSPR_ATTROUT:
1549 if (!reg->idx[0].offset)
1550 sprintf(register_name, "%s_out[8]", prefix);
1551 else
1552 sprintf(register_name, "%s_out[9]", prefix);
1553 break;
1555 case WINED3DSPR_TEXCRDOUT:
1556 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1557 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
1558 break;
1560 case WINED3DSPR_MISCTYPE:
1561 if (!reg->idx[0].offset)
1563 /* vPos */
1564 sprintf(register_name, "vpos");
1566 else if (reg->idx[0].offset == 1)
1568 /* Note that gl_FrontFacing is a bool, while vFace is
1569 * a float for which the sign determines front/back */
1570 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1572 else
1574 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
1575 sprintf(register_name, "unrecognized_register");
1577 break;
1579 case WINED3DSPR_IMMCONST:
1580 switch (reg->immconst_type)
1582 case WINED3D_IMMCONST_SCALAR:
1583 switch (reg->data_type)
1585 case WINED3D_DATA_FLOAT:
1586 sprintf(register_name, "%.8e", *(const float *)reg->immconst_data);
1587 break;
1588 case WINED3D_DATA_INT:
1589 sprintf(register_name, "%#x", reg->immconst_data[0]);
1590 break;
1591 case WINED3D_DATA_RESOURCE:
1592 case WINED3D_DATA_SAMPLER:
1593 case WINED3D_DATA_UINT:
1594 sprintf(register_name, "%#xu", reg->immconst_data[0]);
1595 break;
1596 default:
1597 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1598 break;
1600 break;
1602 case WINED3D_IMMCONST_VEC4:
1603 switch (reg->data_type)
1605 case WINED3D_DATA_FLOAT:
1606 sprintf(register_name, "vec4(%.8e, %.8e, %.8e, %.8e)",
1607 *(const float *)&reg->immconst_data[0], *(const float *)&reg->immconst_data[1],
1608 *(const float *)&reg->immconst_data[2], *(const float *)&reg->immconst_data[3]);
1609 break;
1610 case WINED3D_DATA_INT:
1611 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
1612 reg->immconst_data[0], reg->immconst_data[1],
1613 reg->immconst_data[2], reg->immconst_data[3]);
1614 break;
1615 case WINED3D_DATA_RESOURCE:
1616 case WINED3D_DATA_SAMPLER:
1617 case WINED3D_DATA_UINT:
1618 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
1619 reg->immconst_data[0], reg->immconst_data[1],
1620 reg->immconst_data[2], reg->immconst_data[3]);
1621 break;
1622 default:
1623 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1624 break;
1626 break;
1628 default:
1629 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
1630 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
1632 break;
1634 case WINED3DSPR_CONSTBUFFER:
1635 if (reg->idx[1].rel_addr)
1636 sprintf(register_name, "%s_cb%u[%s + %u]",
1637 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1638 else
1639 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
1640 break;
1642 case WINED3DSPR_PRIMID:
1643 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
1644 break;
1646 default:
1647 FIXME("Unhandled register type %#x.\n", reg->type);
1648 sprintf(register_name, "unrecognized_register");
1649 break;
1653 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1655 *str++ = '.';
1656 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1657 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1658 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1659 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1660 *str = '\0';
1663 /* Get the GLSL write mask for the destination register */
1664 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1666 DWORD mask = param->write_mask;
1668 if (shader_is_scalar(&param->reg))
1670 mask = WINED3DSP_WRITEMASK_0;
1671 *write_mask = '\0';
1673 else
1675 shader_glsl_write_mask_to_str(mask, write_mask);
1678 return mask;
1681 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1682 unsigned int size = 0;
1684 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1685 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1686 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1687 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1689 return size;
1692 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
1694 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1695 * but addressed as "rgba". To fix this we need to swap the register's x
1696 * and z components. */
1697 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1699 *str++ = '.';
1700 /* swizzle bits fields: wwzzyyxx */
1701 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
1702 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
1703 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
1704 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
1705 *str = '\0';
1708 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
1709 BOOL fixup, DWORD mask, char *swizzle_str)
1711 if (shader_is_scalar(&param->reg))
1712 *swizzle_str = '\0';
1713 else
1714 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
1717 /* From a given parameter token, generate the corresponding GLSL string.
1718 * Also, return the actual register name and swizzle in case the
1719 * caller needs this information as well. */
1720 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1721 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
1723 BOOL is_color = FALSE;
1724 char swizzle_str[6];
1726 glsl_src->reg_name[0] = '\0';
1727 glsl_src->param_str[0] = '\0';
1728 swizzle_str[0] = '\0';
1730 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
1731 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
1733 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
1735 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
1737 else
1739 char param_str[200];
1741 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, param_str);
1743 switch (wined3d_src->reg.data_type)
1745 case WINED3D_DATA_FLOAT:
1746 sprintf(glsl_src->param_str, "%s", param_str);
1747 break;
1748 case WINED3D_DATA_INT:
1749 sprintf(glsl_src->param_str, "floatBitsToInt(%s)", param_str);
1750 break;
1751 case WINED3D_DATA_RESOURCE:
1752 case WINED3D_DATA_SAMPLER:
1753 case WINED3D_DATA_UINT:
1754 sprintf(glsl_src->param_str, "floatBitsToUint(%s)", param_str);
1755 break;
1756 default:
1757 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
1758 sprintf(glsl_src->param_str, "%s", param_str);
1759 break;
1764 /* From a given parameter token, generate the corresponding GLSL string.
1765 * Also, return the actual register name and swizzle in case the
1766 * caller needs this information as well. */
1767 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1768 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
1770 BOOL is_color = FALSE;
1772 glsl_dst->mask_str[0] = '\0';
1773 glsl_dst->reg_name[0] = '\0';
1775 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
1776 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1779 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1780 static DWORD shader_glsl_append_dst_ext(struct wined3d_shader_buffer *buffer,
1781 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst)
1783 struct glsl_dst_param glsl_dst;
1784 DWORD mask;
1786 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
1788 switch (dst->reg.data_type)
1790 case WINED3D_DATA_FLOAT:
1791 shader_addline(buffer, "%s%s = %s(",
1792 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1793 break;
1794 case WINED3D_DATA_INT:
1795 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
1796 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1797 break;
1798 case WINED3D_DATA_RESOURCE:
1799 case WINED3D_DATA_SAMPLER:
1800 case WINED3D_DATA_UINT:
1801 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
1802 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1803 break;
1804 default:
1805 FIXME("Unhandled data type %#x.\n", dst->reg.data_type);
1806 shader_addline(buffer, "%s%s = %s(",
1807 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1808 break;
1812 return mask;
1815 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1816 static DWORD shader_glsl_append_dst(struct wined3d_shader_buffer *buffer, const struct wined3d_shader_instruction *ins)
1818 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
1821 /** Process GLSL instruction modifiers */
1822 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1824 struct glsl_dst_param dst_param;
1825 DWORD modifiers;
1827 if (!ins->dst_count) return;
1829 modifiers = ins->dst[0].modifiers;
1830 if (!modifiers) return;
1832 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
1834 if (modifiers & WINED3DSPDM_SATURATE)
1836 /* _SAT means to clamp the value of the register to between 0 and 1 */
1837 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1838 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1841 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
1843 FIXME("_centroid modifier not handled\n");
1846 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
1848 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1852 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
1854 switch (op)
1856 case WINED3D_SHADER_REL_OP_GT: return ">";
1857 case WINED3D_SHADER_REL_OP_EQ: return "==";
1858 case WINED3D_SHADER_REL_OP_GE: return ">=";
1859 case WINED3D_SHADER_REL_OP_LT: return "<";
1860 case WINED3D_SHADER_REL_OP_NE: return "!=";
1861 case WINED3D_SHADER_REL_OP_LE: return "<=";
1862 default:
1863 FIXME("Unrecognized operator %#x.\n", op);
1864 return "(\?\?)";
1868 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
1869 DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
1871 enum wined3d_sampler_texture_type sampler_type = ctx->reg_maps->sampler_type[sampler_idx];
1872 const struct wined3d_gl_info *gl_info = ctx->gl_info;
1873 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
1874 && (((const struct shader_glsl_ctx_priv *)ctx->backend_data)->cur_ps_args->shadow & (1 << sampler_idx));
1875 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
1876 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1877 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
1878 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
1880 /* Note that there's no such thing as a projected cube texture. */
1881 switch(sampler_type) {
1882 case WINED3DSTT_1D:
1883 if (shadow)
1885 if (lod)
1887 sample_function->name = projected ? "shadow1DProjLod" : "shadow1DLod";
1889 else if (grad)
1891 if (gl_info->supported[EXT_GPU_SHADER4])
1892 sample_function->name = projected ? "shadow1DProjGrad" : "shadow1DGrad";
1893 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1894 sample_function->name = projected ? "shadow1DProjGradARB" : "shadow1DGradARB";
1895 else
1897 FIXME("Unsupported 1D shadow grad function.\n");
1898 sample_function->name = "unsupported1DGrad";
1901 else
1903 sample_function->name = projected ? "shadow1DProj" : "shadow1D";
1905 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1907 else
1909 if (lod)
1911 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
1913 else if (grad)
1915 if (gl_info->supported[EXT_GPU_SHADER4])
1916 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
1917 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1918 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
1919 else
1921 FIXME("Unsupported 1D grad function.\n");
1922 sample_function->name = "unsupported1DGrad";
1925 else
1927 sample_function->name = projected ? "texture1DProj" : "texture1D";
1929 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1931 break;
1933 case WINED3DSTT_2D:
1934 if (shadow)
1936 if (texrect)
1938 if (lod)
1940 sample_function->name = projected ? "shadow2DRectProjLod" : "shadow2DRectLod";
1942 else if (grad)
1944 if (gl_info->supported[EXT_GPU_SHADER4])
1945 sample_function->name = projected ? "shadow2DRectProjGrad" : "shadow2DRectGrad";
1946 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1947 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
1948 else
1950 FIXME("Unsupported RECT shadow grad function.\n");
1951 sample_function->name = "unsupported2DRectGrad";
1954 else
1956 sample_function->name = projected ? "shadow2DRectProj" : "shadow2DRect";
1959 else
1961 if (lod)
1963 sample_function->name = projected ? "shadow2DProjLod" : "shadow2DLod";
1965 else if (grad)
1967 if (gl_info->supported[EXT_GPU_SHADER4])
1968 sample_function->name = projected ? "shadow2DProjGrad" : "shadow2DGrad";
1969 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1970 sample_function->name = projected ? "shadow2DProjGradARB" : "shadow2DGradARB";
1971 else
1973 FIXME("Unsupported 2D shadow grad function.\n");
1974 sample_function->name = "unsupported2DGrad";
1977 else
1979 sample_function->name = projected ? "shadow2DProj" : "shadow2D";
1982 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1984 else
1986 if (texrect)
1988 if (lod)
1990 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
1992 else if (grad)
1994 if (gl_info->supported[EXT_GPU_SHADER4])
1995 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
1996 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1997 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
1998 else
2000 FIXME("Unsupported RECT grad function.\n");
2001 sample_function->name = "unsupported2DRectGrad";
2004 else
2006 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
2009 else
2011 if (lod)
2013 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
2015 else if (grad)
2017 if (gl_info->supported[EXT_GPU_SHADER4])
2018 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
2019 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2020 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
2021 else
2023 FIXME("Unsupported 2D grad function.\n");
2024 sample_function->name = "unsupported2DGrad";
2027 else
2029 sample_function->name = projected ? "texture2DProj" : "texture2D";
2032 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2034 break;
2036 case WINED3DSTT_CUBE:
2037 if (shadow)
2039 FIXME("Unsupported Cube shadow function.\n");
2040 sample_function->name = "unsupportedCubeShadow";
2041 sample_function->coord_mask = 0;
2043 else
2045 if (lod)
2047 sample_function->name = "textureCubeLod";
2049 else if (grad)
2051 if (gl_info->supported[EXT_GPU_SHADER4])
2052 sample_function->name = "textureCubeGrad";
2053 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2054 sample_function->name = "textureCubeGradARB";
2055 else
2057 FIXME("Unsupported Cube grad function.\n");
2058 sample_function->name = "unsupportedCubeGrad";
2061 else
2063 sample_function->name = "textureCube";
2065 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2067 break;
2069 case WINED3DSTT_VOLUME:
2070 if (shadow)
2072 FIXME("Unsupported 3D shadow function.\n");
2073 sample_function->name = "unsupported3DShadow";
2074 sample_function->coord_mask = 0;
2076 else
2078 if (lod)
2080 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
2082 else if (grad)
2084 if (gl_info->supported[EXT_GPU_SHADER4])
2085 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
2086 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2087 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
2088 else
2090 FIXME("Unsupported 3D grad function.\n");
2091 sample_function->name = "unsupported3DGrad";
2094 else
2096 sample_function->name = projected ? "texture3DProj" : "texture3D";
2098 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2100 break;
2102 default:
2103 sample_function->name = "";
2104 sample_function->coord_mask = 0;
2105 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
2106 break;
2110 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2111 BOOL sign_fixup, enum fixup_channel_source channel_source)
2113 switch(channel_source)
2115 case CHANNEL_SOURCE_ZERO:
2116 strcat(arguments, "0.0");
2117 break;
2119 case CHANNEL_SOURCE_ONE:
2120 strcat(arguments, "1.0");
2121 break;
2123 case CHANNEL_SOURCE_X:
2124 strcat(arguments, reg_name);
2125 strcat(arguments, ".x");
2126 break;
2128 case CHANNEL_SOURCE_Y:
2129 strcat(arguments, reg_name);
2130 strcat(arguments, ".y");
2131 break;
2133 case CHANNEL_SOURCE_Z:
2134 strcat(arguments, reg_name);
2135 strcat(arguments, ".z");
2136 break;
2138 case CHANNEL_SOURCE_W:
2139 strcat(arguments, reg_name);
2140 strcat(arguments, ".w");
2141 break;
2143 default:
2144 FIXME("Unhandled channel source %#x\n", channel_source);
2145 strcat(arguments, "undefined");
2146 break;
2149 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2152 static void shader_glsl_color_correction_ext(struct wined3d_shader_buffer *buffer,
2153 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2155 unsigned int mask_size, remaining;
2156 DWORD fixup_mask = 0;
2157 char arguments[256];
2158 char mask_str[6];
2160 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2161 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2162 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2163 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2164 if (!(mask &= fixup_mask))
2165 return;
2167 if (is_complex_fixup(fixup))
2169 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2170 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2171 return;
2174 shader_glsl_write_mask_to_str(mask, mask_str);
2175 mask_size = shader_glsl_get_write_mask_size(mask);
2177 arguments[0] = '\0';
2178 remaining = mask_size;
2179 if (mask & WINED3DSP_WRITEMASK_0)
2181 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2182 if (--remaining) strcat(arguments, ", ");
2184 if (mask & WINED3DSP_WRITEMASK_1)
2186 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2187 if (--remaining) strcat(arguments, ", ");
2189 if (mask & WINED3DSP_WRITEMASK_2)
2191 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2192 if (--remaining) strcat(arguments, ", ");
2194 if (mask & WINED3DSP_WRITEMASK_3)
2196 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2197 if (--remaining) strcat(arguments, ", ");
2200 if (mask_size > 1)
2201 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2202 else
2203 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2206 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2208 char reg_name[256];
2209 BOOL is_color;
2211 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2212 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2215 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2216 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2217 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2219 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2220 char dst_swizzle[6];
2221 struct color_fixup_desc fixup;
2222 BOOL np2_fixup = FALSE;
2223 va_list args;
2225 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2227 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2229 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2230 fixup = priv->cur_ps_args->color_fixup[sampler];
2232 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2233 if(bias) {
2234 FIXME("Biased sampling from NP2 textures is unsupported\n");
2235 } else {
2236 np2_fixup = TRUE;
2240 else
2242 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
2245 shader_glsl_append_dst(ins->ctx->buffer, ins);
2247 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2248 sample_function->name, shader_glsl_get_prefix(version->type), sampler);
2250 va_start(args, coord_reg_fmt);
2251 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2252 va_end(args);
2254 if(bias) {
2255 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2256 } else {
2257 if (np2_fixup) {
2258 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2259 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2261 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2262 (idx % 2) ? "zw" : "xy", dst_swizzle);
2263 } else if(dx && dy) {
2264 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2265 } else {
2266 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2270 if(!is_identity_fixup(fixup)) {
2271 shader_glsl_color_correction(ins, fixup);
2275 /*****************************************************************************
2276 * Begin processing individual instruction opcodes
2277 ****************************************************************************/
2279 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2281 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2282 struct glsl_src_param src0_param;
2283 struct glsl_src_param src1_param;
2284 DWORD write_mask;
2285 const char *op;
2287 /* Determine the GLSL operator to use based on the opcode */
2288 switch (ins->handler_idx)
2290 case WINED3DSIH_ADD: op = "+"; break;
2291 case WINED3DSIH_AND: op = "&"; break;
2292 case WINED3DSIH_DIV: op = "/"; break;
2293 case WINED3DSIH_IADD: op = "+"; break;
2294 case WINED3DSIH_MUL: op = "*"; break;
2295 case WINED3DSIH_SUB: op = "-"; break;
2296 case WINED3DSIH_USHR: op = ">>"; break;
2297 case WINED3DSIH_XOR: op = "^"; break;
2298 default:
2299 op = "<unhandled operator>";
2300 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2301 break;
2304 write_mask = shader_glsl_append_dst(buffer, ins);
2305 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2306 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2307 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2310 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2312 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2313 struct glsl_src_param src0_param;
2314 struct glsl_src_param src1_param;
2315 unsigned int mask_size;
2316 DWORD write_mask;
2317 const char *op;
2319 write_mask = shader_glsl_append_dst(buffer, ins);
2320 mask_size = shader_glsl_get_write_mask_size(write_mask);
2321 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2322 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2324 if (mask_size > 1)
2326 switch (ins->handler_idx)
2328 case WINED3DSIH_EQ: op = "equal"; break;
2329 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2330 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2331 case WINED3DSIH_LT: op = "lessThan"; break;
2332 default:
2333 op = "<unhandled operator>";
2334 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2335 break;
2338 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2339 mask_size, op, src0_param.param_str, src1_param.param_str);
2341 else
2343 switch (ins->handler_idx)
2345 case WINED3DSIH_EQ: op = "=="; break;
2346 case WINED3DSIH_GE: op = ">="; break;
2347 case WINED3DSIH_IGE: op = ">="; break;
2348 case WINED3DSIH_LT: op = "<"; break;
2349 default:
2350 op = "<unhandled operator>";
2351 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2352 break;
2355 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2356 src0_param.param_str, op, src1_param.param_str);
2360 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2362 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2363 struct glsl_src_param src0_param;
2364 struct glsl_src_param src1_param;
2365 DWORD write_mask;
2367 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2368 * not, we can emulate it. */
2369 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2370 FIXME("64-bit integer multiplies not implemented.\n");
2372 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2374 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2375 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2376 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2378 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2379 src0_param.param_str, src1_param.param_str);
2383 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2385 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2386 struct glsl_src_param src0_param, src1_param;
2387 DWORD write_mask;
2389 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2392 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2394 char dst_mask[6];
2396 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2397 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2398 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2399 shader_addline(buffer, "tmp0%s = %s / %s;\n",
2400 dst_mask, src0_param.param_str, src1_param.param_str);
2402 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2403 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2404 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2405 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
2407 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
2408 shader_addline(buffer, "tmp0%s);\n", dst_mask);
2410 else
2412 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
2413 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2414 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2415 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
2418 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2420 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2421 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2422 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2423 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
2427 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2428 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2430 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2431 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2432 struct glsl_src_param src0_param;
2433 DWORD write_mask;
2435 write_mask = shader_glsl_append_dst(buffer, ins);
2436 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2438 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2439 * shader versions WINED3DSIO_MOVA is used for this. */
2440 if (ins->ctx->reg_maps->shader_version.major == 1
2441 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
2442 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2444 /* This is a simple floor() */
2445 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2446 if (mask_size > 1) {
2447 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2448 } else {
2449 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2452 else if(ins->handler_idx == WINED3DSIH_MOVA)
2454 /* We need to *round* to the nearest int here. */
2455 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2457 if (gl_info->supported[EXT_GPU_SHADER4])
2459 if (mask_size > 1)
2460 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2461 else
2462 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2464 else
2466 if (mask_size > 1)
2467 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2468 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2469 else
2470 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2471 src0_param.param_str, src0_param.param_str);
2474 else
2476 shader_addline(buffer, "%s);\n", src0_param.param_str);
2480 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2481 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2483 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2484 struct glsl_src_param src0_param;
2485 struct glsl_src_param src1_param;
2486 DWORD dst_write_mask, src_write_mask;
2487 unsigned int dst_size = 0;
2489 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2490 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2492 /* dp3 works on vec3, dp4 on vec4 */
2493 if (ins->handler_idx == WINED3DSIH_DP4)
2495 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2496 } else {
2497 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2500 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2501 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2503 if (dst_size > 1) {
2504 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2505 } else {
2506 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2510 /* Note that this instruction has some restrictions. The destination write mask
2511 * can't contain the w component, and the source swizzles have to be .xyzw */
2512 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2514 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2515 struct glsl_src_param src0_param;
2516 struct glsl_src_param src1_param;
2517 char dst_mask[6];
2519 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2520 shader_glsl_append_dst(ins->ctx->buffer, ins);
2521 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2522 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2523 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2526 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
2528 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
2531 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2532 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2533 * GLSL uses the value as-is. */
2534 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2536 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2537 struct glsl_src_param src0_param;
2538 struct glsl_src_param src1_param;
2539 DWORD dst_write_mask;
2540 unsigned int dst_size;
2542 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2543 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2545 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2546 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2548 if (dst_size > 1)
2550 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2551 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
2553 else
2555 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
2556 src1_param.param_str, src0_param.param_str, src1_param.param_str);
2560 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
2561 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
2562 * GLSL uses the value as-is. */
2563 static void shader_glsl_log(const struct wined3d_shader_instruction *ins)
2565 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2566 struct glsl_src_param src0_param;
2567 DWORD dst_write_mask;
2568 unsigned int dst_size;
2570 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2571 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2573 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2575 if (dst_size > 1)
2577 shader_addline(buffer, "vec%u(log2(abs(%s))));\n",
2578 dst_size, src0_param.param_str);
2580 else
2582 shader_addline(buffer, "log2(abs(%s)));\n",
2583 src0_param.param_str);
2587 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2588 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
2590 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2591 struct glsl_src_param src_param;
2592 const char *instruction;
2593 DWORD write_mask;
2594 unsigned i;
2596 /* Determine the GLSL function to use based on the opcode */
2597 /* TODO: Possibly make this a table for faster lookups */
2598 switch (ins->handler_idx)
2600 case WINED3DSIH_MIN: instruction = "min"; break;
2601 case WINED3DSIH_MAX: instruction = "max"; break;
2602 case WINED3DSIH_ABS: instruction = "abs"; break;
2603 case WINED3DSIH_FRC: instruction = "fract"; break;
2604 case WINED3DSIH_EXP: instruction = "exp2"; break;
2605 case WINED3DSIH_DSX: instruction = "dFdx"; break;
2606 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
2607 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
2608 default: instruction = "";
2609 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2610 break;
2613 write_mask = shader_glsl_append_dst(buffer, ins);
2615 shader_addline(buffer, "%s(", instruction);
2617 if (ins->src_count)
2619 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2620 shader_addline(buffer, "%s", src_param.param_str);
2621 for (i = 1; i < ins->src_count; ++i)
2623 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
2624 shader_addline(buffer, ", %s", src_param.param_str);
2628 shader_addline(buffer, "));\n");
2631 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
2633 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
2635 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2636 struct glsl_src_param src_param;
2637 unsigned int mask_size;
2638 DWORD write_mask;
2639 char dst_mask[6];
2641 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
2642 mask_size = shader_glsl_get_write_mask_size(write_mask);
2643 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2645 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
2646 src_param.param_str, src_param.param_str);
2647 shader_glsl_append_dst(buffer, ins);
2649 if (mask_size > 1)
2651 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
2652 mask_size, src_param.param_str);
2654 else
2656 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
2657 src_param.param_str);
2661 /** Process the WINED3DSIO_EXPP instruction in GLSL:
2662 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2663 * dst.x = 2^(floor(src))
2664 * dst.y = src - floor(src)
2665 * dst.z = 2^src (partial precision is allowed, but optional)
2666 * dst.w = 1.0;
2667 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2668 * dst = 2^src; (partial precision is allowed, but optional)
2670 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
2672 struct glsl_src_param src_param;
2674 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
2676 if (ins->ctx->reg_maps->shader_version.major < 2)
2678 char dst_mask[6];
2680 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
2681 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
2682 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
2683 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
2685 shader_glsl_append_dst(ins->ctx->buffer, ins);
2686 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2687 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
2688 } else {
2689 DWORD write_mask;
2690 unsigned int mask_size;
2692 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2693 mask_size = shader_glsl_get_write_mask_size(write_mask);
2695 if (mask_size > 1) {
2696 shader_addline(ins->ctx->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
2697 } else {
2698 shader_addline(ins->ctx->buffer, "exp2(%s));\n", src_param.param_str);
2703 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
2705 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2706 struct glsl_src_param src_param;
2707 unsigned int mask_size;
2708 DWORD write_mask;
2710 write_mask = shader_glsl_append_dst(buffer, ins);
2711 mask_size = shader_glsl_get_write_mask_size(write_mask);
2712 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2714 if (mask_size > 1)
2715 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
2716 else
2717 shader_addline(buffer, "int(%s));\n", src_param.param_str);
2720 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
2722 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2723 struct glsl_src_param src_param;
2724 unsigned int mask_size;
2725 DWORD write_mask;
2727 write_mask = shader_glsl_append_dst(buffer, ins);
2728 mask_size = shader_glsl_get_write_mask_size(write_mask);
2729 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2731 if (mask_size > 1)
2732 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
2733 else
2734 shader_addline(buffer, "float(%s));\n", src_param.param_str);
2737 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
2738 static void shader_glsl_rcp(const struct wined3d_shader_instruction *ins)
2740 struct glsl_src_param src_param;
2741 DWORD write_mask;
2742 unsigned int mask_size;
2744 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2745 mask_size = shader_glsl_get_write_mask_size(write_mask);
2746 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2748 if (mask_size > 1)
2750 shader_addline(ins->ctx->buffer, "vec%u(1.0 / %s));\n",
2751 mask_size, src_param.param_str);
2753 else
2755 shader_addline(ins->ctx->buffer, "1.0 / %s);\n",
2756 src_param.param_str);
2760 static void shader_glsl_rsq(const struct wined3d_shader_instruction *ins)
2762 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2763 struct glsl_src_param src_param;
2764 DWORD write_mask;
2765 unsigned int mask_size;
2767 write_mask = shader_glsl_append_dst(buffer, ins);
2768 mask_size = shader_glsl_get_write_mask_size(write_mask);
2770 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2772 if (mask_size > 1)
2774 shader_addline(buffer, "vec%u(inversesqrt(abs(%s))));\n",
2775 mask_size, src_param.param_str);
2777 else
2779 shader_addline(buffer, "inversesqrt(abs(%s)));\n",
2780 src_param.param_str);
2784 /** Process signed comparison opcodes in GLSL. */
2785 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
2787 struct glsl_src_param src0_param;
2788 struct glsl_src_param src1_param;
2789 DWORD write_mask;
2790 unsigned int mask_size;
2792 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2793 mask_size = shader_glsl_get_write_mask_size(write_mask);
2794 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2795 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2797 if (mask_size > 1) {
2798 const char *compare;
2800 switch(ins->handler_idx)
2802 case WINED3DSIH_SLT: compare = "lessThan"; break;
2803 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
2804 default: compare = "";
2805 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2808 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
2809 src0_param.param_str, src1_param.param_str);
2810 } else {
2811 switch(ins->handler_idx)
2813 case WINED3DSIH_SLT:
2814 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2815 * to return 0.0 but step returns 1.0 because step is not < x
2816 * An alternative is a bvec compare padded with an unused second component.
2817 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2818 * issue. Playing with not() is not possible either because not() does not accept
2819 * a scalar.
2821 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
2822 src0_param.param_str, src1_param.param_str);
2823 break;
2824 case WINED3DSIH_SGE:
2825 /* Here we can use the step() function and safe a conditional */
2826 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
2827 break;
2828 default:
2829 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2835 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
2837 const char *condition_prefix, *condition_suffix;
2838 struct wined3d_shader_dst_param dst;
2839 struct glsl_src_param src0_param;
2840 struct glsl_src_param src1_param;
2841 struct glsl_src_param src2_param;
2842 BOOL temp_destination = FALSE;
2843 DWORD cmp_channel = 0;
2844 unsigned int i, j;
2845 char mask_char[6];
2846 DWORD write_mask;
2848 switch (ins->handler_idx)
2850 case WINED3DSIH_CMP:
2851 condition_prefix = "";
2852 condition_suffix = " >= 0.0";
2853 break;
2855 case WINED3DSIH_CND:
2856 condition_prefix = "";
2857 condition_suffix = " > 0.5";
2858 break;
2860 case WINED3DSIH_MOVC:
2861 condition_prefix = "bool(";
2862 condition_suffix = ")";
2863 break;
2865 default:
2866 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2867 condition_prefix = "<unhandled prefix>";
2868 condition_suffix = "<unhandled suffix>";
2869 break;
2872 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
2874 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2875 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2876 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2877 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2879 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2880 condition_prefix, src0_param.param_str, condition_suffix,
2881 src1_param.param_str, src2_param.param_str);
2882 return;
2885 dst = ins->dst[0];
2887 /* Splitting the instruction up in multiple lines imposes a problem:
2888 * The first lines may overwrite source parameters of the following lines.
2889 * Deal with that by using a temporary destination register if needed. */
2890 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
2891 && ins->src[0].reg.type == dst.reg.type)
2892 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
2893 && ins->src[1].reg.type == dst.reg.type)
2894 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
2895 && ins->src[2].reg.type == dst.reg.type))
2896 temp_destination = TRUE;
2898 /* Cycle through all source0 channels. */
2899 for (i = 0; i < 4; ++i)
2901 write_mask = 0;
2902 /* Find the destination channels which use the current source0 channel. */
2903 for (j = 0; j < 4; ++j)
2905 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2907 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2908 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2911 dst.write_mask = ins->dst[0].write_mask & write_mask;
2913 if (temp_destination)
2915 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
2916 continue;
2917 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
2919 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst)))
2920 continue;
2922 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2923 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2924 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2926 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2927 condition_prefix, src0_param.param_str, condition_suffix,
2928 src1_param.param_str, src2_param.param_str);
2931 if (temp_destination)
2933 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
2934 shader_glsl_append_dst(ins->ctx->buffer, ins);
2935 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
2939 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2940 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2941 * the compare is done per component of src0. */
2942 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
2944 struct glsl_src_param src0_param;
2945 struct glsl_src_param src1_param;
2946 struct glsl_src_param src2_param;
2947 DWORD write_mask;
2948 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2949 ins->ctx->reg_maps->shader_version.minor);
2951 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
2953 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2954 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2955 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2956 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2958 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
2959 if (ins->coissue)
2961 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
2962 } else {
2963 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2964 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2966 return;
2969 shader_glsl_conditional_move(ins);
2972 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2973 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
2975 struct glsl_src_param src0_param;
2976 struct glsl_src_param src1_param;
2977 struct glsl_src_param src2_param;
2978 DWORD write_mask;
2980 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2981 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2982 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2983 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2984 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
2985 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2988 /* Handles transforming all WINED3DSIO_M?x? opcodes for
2989 Vertex shaders to GLSL codes */
2990 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
2992 int i;
2993 int nComponents = 0;
2994 struct wined3d_shader_dst_param tmp_dst = {{0}};
2995 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
2996 struct wined3d_shader_instruction tmp_ins;
2998 memset(&tmp_ins, 0, sizeof(tmp_ins));
3000 /* Set constants for the temporary argument */
3001 tmp_ins.ctx = ins->ctx;
3002 tmp_ins.dst_count = 1;
3003 tmp_ins.dst = &tmp_dst;
3004 tmp_ins.src_count = 2;
3005 tmp_ins.src = tmp_src;
3007 switch(ins->handler_idx)
3009 case WINED3DSIH_M4x4:
3010 nComponents = 4;
3011 tmp_ins.handler_idx = WINED3DSIH_DP4;
3012 break;
3013 case WINED3DSIH_M4x3:
3014 nComponents = 3;
3015 tmp_ins.handler_idx = WINED3DSIH_DP4;
3016 break;
3017 case WINED3DSIH_M3x4:
3018 nComponents = 4;
3019 tmp_ins.handler_idx = WINED3DSIH_DP3;
3020 break;
3021 case WINED3DSIH_M3x3:
3022 nComponents = 3;
3023 tmp_ins.handler_idx = WINED3DSIH_DP3;
3024 break;
3025 case WINED3DSIH_M3x2:
3026 nComponents = 2;
3027 tmp_ins.handler_idx = WINED3DSIH_DP3;
3028 break;
3029 default:
3030 break;
3033 tmp_dst = ins->dst[0];
3034 tmp_src[0] = ins->src[0];
3035 tmp_src[1] = ins->src[1];
3036 for (i = 0; i < nComponents; ++i)
3038 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3039 shader_glsl_dot(&tmp_ins);
3040 ++tmp_src[1].reg.idx[0].offset;
3045 The LRP instruction performs a component-wise linear interpolation
3046 between the second and third operands using the first operand as the
3047 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3048 This is equivalent to mix(src2, src1, src0);
3050 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3052 struct glsl_src_param src0_param;
3053 struct glsl_src_param src1_param;
3054 struct glsl_src_param src2_param;
3055 DWORD write_mask;
3057 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3059 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3060 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3061 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3063 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3064 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3067 /** Process the WINED3DSIO_LIT instruction in GLSL:
3068 * dst.x = dst.w = 1.0
3069 * dst.y = (src0.x > 0) ? src0.x
3070 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3071 * where src.w is clamped at +- 128
3073 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3075 struct glsl_src_param src0_param;
3076 struct glsl_src_param src1_param;
3077 struct glsl_src_param src3_param;
3078 char dst_mask[6];
3080 shader_glsl_append_dst(ins->ctx->buffer, ins);
3081 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3083 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3084 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3085 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3087 /* The sdk specifies the instruction like this
3088 * dst.x = 1.0;
3089 * if(src.x > 0.0) dst.y = src.x
3090 * else dst.y = 0.0.
3091 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3092 * else dst.z = 0.0;
3093 * dst.w = 1.0;
3094 * (where power = src.w clamped between -128 and 128)
3096 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3097 * dst.x = 1.0 ... No further explanation needed
3098 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3099 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3100 * dst.w = 1.0. ... Nothing fancy.
3102 * So we still have one conditional in there. So do this:
3103 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3105 * 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),
3106 * 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.
3107 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3109 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3110 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3111 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3113 shader_addline(ins->ctx->buffer,
3114 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3115 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3116 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3117 src0_param.param_str, src3_param.param_str, dst_mask);
3120 /** Process the WINED3DSIO_DST instruction in GLSL:
3121 * dst.x = 1.0
3122 * dst.y = src0.x * src0.y
3123 * dst.z = src0.z
3124 * dst.w = src1.w
3126 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3128 struct glsl_src_param src0y_param;
3129 struct glsl_src_param src0z_param;
3130 struct glsl_src_param src1y_param;
3131 struct glsl_src_param src1w_param;
3132 char dst_mask[6];
3134 shader_glsl_append_dst(ins->ctx->buffer, ins);
3135 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3137 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3138 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3139 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3140 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3142 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3143 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3146 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3147 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3148 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3150 * dst.x = cos(src0.?)
3151 * dst.y = sin(src0.?)
3152 * dst.z = dst.z
3153 * dst.w = dst.w
3155 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3157 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3158 struct glsl_src_param src0_param;
3159 DWORD write_mask;
3161 if (ins->ctx->reg_maps->shader_version.major < 4)
3163 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3165 write_mask = shader_glsl_append_dst(buffer, ins);
3166 switch (write_mask)
3168 case WINED3DSP_WRITEMASK_0:
3169 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3170 break;
3172 case WINED3DSP_WRITEMASK_1:
3173 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3174 break;
3176 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3177 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3178 src0_param.param_str, src0_param.param_str);
3179 break;
3181 default:
3182 ERR("Write mask should be .x, .y or .xy\n");
3183 break;
3186 return;
3189 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3192 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3194 char dst_mask[6];
3196 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3197 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3198 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3200 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
3201 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3202 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3204 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
3205 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3207 else
3209 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
3210 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3211 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3214 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3216 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
3217 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3218 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3222 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3223 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3224 * generate invalid code
3226 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3228 struct glsl_src_param src0_param;
3229 DWORD write_mask;
3231 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3232 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3234 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3237 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3238 * Start a for() loop where src1.y is the initial value of aL,
3239 * increment aL by src1.z for a total of src1.x iterations.
3240 * Need to use a temporary variable for this operation.
3242 /* FIXME: I don't think nested loops will work correctly this way. */
3243 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3245 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3246 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3247 const struct wined3d_shader *shader = ins->ctx->shader;
3248 const struct wined3d_shader_lconst *constant;
3249 struct glsl_src_param src1_param;
3250 const DWORD *control_values = NULL;
3252 if (ins->ctx->reg_maps->shader_version.major < 4)
3254 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3256 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3257 * class hardware doesn't support real varying indexing, but Microsoft
3258 * designed this feature for Shader model 2.x+. If the loop control is
3259 * known at compile time, the GLSL compiler can unroll the loop, and
3260 * replace indirect addressing with direct addressing. */
3261 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3263 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3265 if (constant->idx == ins->src[1].reg.idx[0].offset)
3267 control_values = constant->value;
3268 break;
3273 if (control_values)
3275 struct wined3d_shader_loop_control loop_control;
3276 loop_control.count = control_values[0];
3277 loop_control.start = control_values[1];
3278 loop_control.step = (int)control_values[2];
3280 if (loop_control.step > 0)
3282 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3283 loop_state->current_depth, loop_control.start,
3284 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3285 loop_state->current_depth, loop_control.step);
3287 else if (loop_control.step < 0)
3289 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3290 loop_state->current_depth, loop_control.start,
3291 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3292 loop_state->current_depth, loop_control.step);
3294 else
3296 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3297 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3298 loop_state->current_depth, loop_control.count,
3299 loop_state->current_depth);
3302 else
3304 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3305 loop_state->current_depth, loop_state->current_reg,
3306 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3307 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3310 ++loop_state->current_reg;
3312 else
3314 shader_addline(buffer, "for (;;)\n{\n");
3317 ++loop_state->current_depth;
3320 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3322 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3324 shader_addline(ins->ctx->buffer, "}\n");
3326 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3328 --loop_state->current_depth;
3329 --loop_state->current_reg;
3332 if (ins->handler_idx == WINED3DSIH_ENDREP)
3334 --loop_state->current_depth;
3338 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3340 const struct wined3d_shader *shader = ins->ctx->shader;
3341 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3342 const struct wined3d_shader_lconst *constant;
3343 struct glsl_src_param src0_param;
3344 const DWORD *control_values = NULL;
3346 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3347 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3349 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3351 if (constant->idx == ins->src[0].reg.idx[0].offset)
3353 control_values = constant->value;
3354 break;
3359 if (control_values)
3361 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3362 loop_state->current_depth, loop_state->current_depth,
3363 control_values[0], loop_state->current_depth);
3365 else
3367 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3368 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3369 loop_state->current_depth, loop_state->current_depth,
3370 src0_param.param_str, loop_state->current_depth);
3373 ++loop_state->current_depth;
3376 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3378 struct glsl_src_param src0_param;
3380 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3381 shader_addline(ins->ctx->buffer, "if (%s) {\n", src0_param.param_str);
3384 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3386 struct glsl_src_param src0_param;
3387 struct glsl_src_param src1_param;
3389 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3390 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3392 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3393 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3396 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3398 shader_addline(ins->ctx->buffer, "} else {\n");
3401 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3403 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3406 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3408 shader_addline(ins->ctx->buffer, "break;\n");
3411 /* FIXME: According to MSDN the compare is done per component. */
3412 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3414 struct glsl_src_param src0_param;
3415 struct glsl_src_param src1_param;
3417 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3418 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3420 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3421 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3424 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
3426 struct glsl_src_param src_param;
3428 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
3429 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
3432 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
3434 shader_addline(ins->ctx->buffer, "}\n");
3435 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
3438 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
3440 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
3443 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3445 struct glsl_src_param src1_param;
3447 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3448 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
3449 src1_param.param_str, ins->src[0].reg.idx[0].offset);
3452 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3454 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3455 * function only suppresses the unhandled instruction warning
3459 /*********************************************
3460 * Pixel Shader Specific Code begins here
3461 ********************************************/
3462 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3464 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3465 ins->ctx->reg_maps->shader_version.minor);
3466 struct glsl_sample_function sample_function;
3467 DWORD sample_flags = 0;
3468 DWORD sampler_idx;
3469 DWORD mask = 0, swizzle;
3470 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3472 /* 1.0-1.4: Use destination register as sampler source.
3473 * 2.0+: Use provided sampler source. */
3474 if (shader_version < WINED3D_SHADER_VERSION(2,0))
3475 sampler_idx = ins->dst[0].reg.idx[0].offset;
3476 else
3477 sampler_idx = ins->src[1].reg.idx[0].offset;
3479 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3481 DWORD flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3482 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3483 enum wined3d_sampler_texture_type sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3485 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3486 if (flags & WINED3D_PSARGS_PROJECTED && sampler_type != WINED3DSTT_CUBE)
3488 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3489 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3491 case WINED3D_TTFF_COUNT1:
3492 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3493 break;
3494 case WINED3D_TTFF_COUNT2:
3495 mask = WINED3DSP_WRITEMASK_1;
3496 break;
3497 case WINED3D_TTFF_COUNT3:
3498 mask = WINED3DSP_WRITEMASK_2;
3499 break;
3500 case WINED3D_TTFF_COUNT4:
3501 case WINED3D_TTFF_DISABLE:
3502 mask = WINED3DSP_WRITEMASK_3;
3503 break;
3507 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3509 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3511 if (src_mod == WINED3DSPSM_DZ) {
3512 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3513 mask = WINED3DSP_WRITEMASK_2;
3514 } else if (src_mod == WINED3DSPSM_DW) {
3515 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3516 mask = WINED3DSP_WRITEMASK_3;
3518 } else {
3519 if (ins->flags & WINED3DSI_TEXLD_PROJECT)
3521 /* ps 2.0 texldp instruction always divides by the fourth component. */
3522 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3523 mask = WINED3DSP_WRITEMASK_3;
3527 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3528 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3530 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3531 mask |= sample_function.coord_mask;
3533 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3534 else swizzle = ins->src[1].swizzle;
3536 /* 1.0-1.3: Use destination register as coordinate source.
3537 1.4+: Use provided coordinate source register. */
3538 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3540 char coord_mask[6];
3541 shader_glsl_write_mask_to_str(mask, coord_mask);
3542 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3543 "T%u%s", sampler_idx, coord_mask);
3545 else
3547 struct glsl_src_param coord_param;
3548 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3549 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3551 struct glsl_src_param bias;
3552 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3553 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3554 "%s", coord_param.param_str);
3555 } else {
3556 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3557 "%s", coord_param.param_str);
3562 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3564 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3565 struct glsl_src_param coord_param, dx_param, dy_param;
3566 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3567 struct glsl_sample_function sample_function;
3568 DWORD sampler_idx;
3569 DWORD swizzle = ins->src[1].swizzle;
3570 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3572 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3574 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3575 shader_glsl_tex(ins);
3576 return;
3579 sampler_idx = ins->src[1].reg.idx[0].offset;
3580 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3581 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3583 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3584 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3585 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3586 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3588 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3589 "%s", coord_param.param_str);
3592 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3594 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3595 struct glsl_src_param coord_param, lod_param;
3596 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3597 struct glsl_sample_function sample_function;
3598 DWORD sampler_idx;
3599 DWORD swizzle = ins->src[1].swizzle;
3600 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3602 sampler_idx = ins->src[1].reg.idx[0].offset;
3603 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3604 && 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 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3610 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3612 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3613 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
3615 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
3616 * However, the NVIDIA drivers allow them in fragment shaders as well,
3617 * even without the appropriate extension. */
3618 WARN("Using %s in fragment shader.\n", sample_function.name);
3620 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3621 "%s", coord_param.param_str);
3624 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3626 /* FIXME: Make this work for more than just 2D textures */
3627 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3628 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3630 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3632 char dst_mask[6];
3634 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3635 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3636 ins->dst[0].reg.idx[0].offset, dst_mask);
3638 else
3640 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3641 DWORD reg = ins->src[0].reg.idx[0].offset;
3642 char dst_swizzle[6];
3644 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3646 if (src_mod == WINED3DSPSM_DZ)
3648 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3649 struct glsl_src_param div_param;
3651 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3653 if (mask_size > 1) {
3654 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3655 } else {
3656 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3659 else if (src_mod == WINED3DSPSM_DW)
3661 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3662 struct glsl_src_param div_param;
3664 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3666 if (mask_size > 1) {
3667 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3668 } else {
3669 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3671 } else {
3672 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3677 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3678 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3679 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3680 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3682 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3683 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3684 struct glsl_sample_function sample_function;
3685 struct glsl_src_param src0_param;
3686 UINT mask_size;
3688 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3690 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3691 * scalar, and projected sampling would require 4.
3693 * It is a dependent read - not valid with conditional NP2 textures
3695 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3696 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3698 switch(mask_size)
3700 case 1:
3701 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3702 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3703 break;
3705 case 2:
3706 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3707 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3708 break;
3710 case 3:
3711 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3712 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3713 break;
3715 default:
3716 FIXME("Unexpected mask size %u\n", mask_size);
3717 break;
3721 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3722 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3723 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3725 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3726 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3727 struct glsl_src_param src0_param;
3728 DWORD dst_mask;
3729 unsigned int mask_size;
3731 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3732 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3733 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3735 if (mask_size > 1) {
3736 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3737 } else {
3738 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3742 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3743 * Calculate the depth as dst.x / dst.y */
3744 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3746 struct glsl_dst_param dst_param;
3748 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3750 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3751 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3752 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3753 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3754 * >= 1.0 or < 0.0
3756 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3757 dst_param.reg_name, dst_param.reg_name);
3760 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3761 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3762 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3763 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3765 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3767 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3768 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3769 struct glsl_src_param src0_param;
3771 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3773 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3774 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3777 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3778 * Calculate the 1st of a 2-row matrix multiplication. */
3779 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3781 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3782 DWORD reg = ins->dst[0].reg.idx[0].offset;
3783 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3784 struct glsl_src_param src0_param;
3786 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3787 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3790 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3791 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3792 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3794 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3795 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3796 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3797 DWORD reg = ins->dst[0].reg.idx[0].offset;
3798 struct glsl_src_param src0_param;
3800 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3801 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
3802 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
3805 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3807 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3808 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3809 struct glsl_sample_function sample_function;
3810 DWORD reg = ins->dst[0].reg.idx[0].offset;
3811 struct glsl_src_param src0_param;
3813 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3814 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3816 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3818 /* Sample the texture using the calculated coordinates */
3819 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
3822 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3823 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3824 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3826 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3827 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3828 struct glsl_sample_function sample_function;
3829 DWORD reg = ins->dst[0].reg.idx[0].offset;
3830 struct glsl_src_param src0_param;
3832 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3833 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3835 /* Dependent read, not valid with conditional NP2 */
3836 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3838 /* Sample the texture using the calculated coordinates */
3839 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3841 tex_mx->current_row = 0;
3844 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3845 * Perform the 3rd row of a 3x3 matrix multiply */
3846 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
3848 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3849 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3850 DWORD reg = ins->dst[0].reg.idx[0].offset;
3851 struct glsl_src_param src0_param;
3852 char dst_mask[6];
3854 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3856 shader_glsl_append_dst(ins->ctx->buffer, ins);
3857 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3858 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
3860 tex_mx->current_row = 0;
3863 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3864 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3865 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
3867 struct glsl_src_param src0_param;
3868 struct glsl_src_param src1_param;
3869 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3870 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3871 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3872 struct glsl_sample_function sample_function;
3873 DWORD reg = ins->dst[0].reg.idx[0].offset;
3874 char coord_mask[6];
3876 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3877 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3879 /* Perform the last matrix multiply operation */
3880 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3881 /* Reflection calculation */
3882 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
3884 /* Dependent read, not valid with conditional NP2 */
3885 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3886 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3888 /* Sample the texture */
3889 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3890 NULL, NULL, NULL, "tmp0%s", coord_mask);
3892 tex_mx->current_row = 0;
3895 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3896 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3897 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
3899 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3900 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3901 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3902 struct glsl_sample_function sample_function;
3903 DWORD reg = ins->dst[0].reg.idx[0].offset;
3904 struct glsl_src_param src0_param;
3905 char coord_mask[6];
3907 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3909 /* Perform the last matrix multiply operation */
3910 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
3912 /* Construct the eye-ray vector from w coordinates */
3913 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3914 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
3915 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3917 /* Dependent read, not valid with conditional NP2 */
3918 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3919 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3921 /* Sample the texture using the calculated coordinates */
3922 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3923 NULL, NULL, NULL, "tmp0%s", coord_mask);
3925 tex_mx->current_row = 0;
3928 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3929 * Apply a fake bump map transform.
3930 * texbem is pshader <= 1.3 only, this saves a few version checks
3932 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
3934 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3935 struct glsl_sample_function sample_function;
3936 struct glsl_src_param coord_param;
3937 DWORD sampler_idx;
3938 DWORD mask;
3939 DWORD flags;
3940 char coord_mask[6];
3942 sampler_idx = ins->dst[0].reg.idx[0].offset;
3943 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3944 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3946 /* Dependent read, not valid with conditional NP2 */
3947 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3948 mask = sample_function.coord_mask;
3950 shader_glsl_write_mask_to_str(mask, coord_mask);
3952 /* With projected textures, texbem only divides the static texture coord,
3953 * not the displacement, so we can't let GL handle this. */
3954 if (flags & WINED3D_PSARGS_PROJECTED)
3956 DWORD div_mask=0;
3957 char coord_div_mask[3];
3958 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3960 case WINED3D_TTFF_COUNT1:
3961 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3962 break;
3963 case WINED3D_TTFF_COUNT2:
3964 div_mask = WINED3DSP_WRITEMASK_1;
3965 break;
3966 case WINED3D_TTFF_COUNT3:
3967 div_mask = WINED3DSP_WRITEMASK_2;
3968 break;
3969 case WINED3D_TTFF_COUNT4:
3970 case WINED3D_TTFF_DISABLE:
3971 div_mask = WINED3DSP_WRITEMASK_3;
3972 break;
3974 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
3975 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
3978 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
3980 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3981 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
3982 coord_param.param_str, coord_mask);
3984 if (ins->handler_idx == WINED3DSIH_TEXBEML)
3986 struct glsl_src_param luminance_param;
3987 struct glsl_dst_param dst_param;
3989 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
3990 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3992 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
3993 dst_param.reg_name, dst_param.mask_str,
3994 luminance_param.param_str, sampler_idx, sampler_idx);
3998 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4000 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4001 struct glsl_src_param src0_param, src1_param;
4003 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4004 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4006 shader_glsl_append_dst(ins->ctx->buffer, ins);
4007 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4008 src0_param.param_str, sampler_idx, src1_param.param_str);
4011 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4012 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4013 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4015 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4016 struct glsl_sample_function sample_function;
4017 struct glsl_src_param src0_param;
4019 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4021 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4022 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4023 "%s.wx", src0_param.reg_name);
4026 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4027 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4028 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4030 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4031 struct glsl_sample_function sample_function;
4032 struct glsl_src_param src0_param;
4034 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4036 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4037 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4038 "%s.yz", src0_param.reg_name);
4041 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4042 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4043 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4045 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4046 struct glsl_sample_function sample_function;
4047 struct glsl_src_param src0_param;
4049 /* Dependent read, not valid with conditional NP2 */
4050 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4051 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4053 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4054 "%s", src0_param.param_str);
4057 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4058 * If any of the first 3 components are < 0, discard this pixel */
4059 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4061 struct glsl_dst_param dst_param;
4063 /* The argument is a destination parameter, and no writemasks are allowed */
4064 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4065 if (ins->ctx->reg_maps->shader_version.major >= 2)
4067 /* 2.0 shaders compare all 4 components in texkill */
4068 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4069 } else {
4070 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
4071 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
4072 * 4 components are defined, only the first 3 are used
4074 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4078 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4079 * dst = dot2(src0, src1) + src2 */
4080 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4082 struct glsl_src_param src0_param;
4083 struct glsl_src_param src1_param;
4084 struct glsl_src_param src2_param;
4085 DWORD write_mask;
4086 unsigned int mask_size;
4088 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4089 mask_size = shader_glsl_get_write_mask_size(write_mask);
4091 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4092 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4093 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4095 if (mask_size > 1) {
4096 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4097 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4098 } else {
4099 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4100 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4104 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
4105 const struct wined3d_shader_signature_element *input_signature,
4106 const struct wined3d_shader_reg_maps *reg_maps,
4107 enum vertexprocessing_mode vertexprocessing)
4109 WORD map = reg_maps->input_registers;
4110 unsigned int i;
4112 for (i = 0; map; map >>= 1, ++i)
4114 const char *semantic_name;
4115 UINT semantic_idx;
4116 char reg_mask[6];
4118 /* Unused */
4119 if (!(map & 1)) continue;
4121 semantic_name = input_signature[i].semantic_name;
4122 semantic_idx = input_signature[i].semantic_idx;
4123 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
4125 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4127 if (semantic_idx < 8 && vertexprocessing == pretransformed)
4128 shader_addline(buffer, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4129 shader->u.ps.input_reg_map[i], reg_mask, semantic_idx, reg_mask);
4130 else
4131 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4132 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4134 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4136 if (!semantic_idx)
4137 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4138 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4139 else if (semantic_idx == 1)
4140 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4141 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4142 else
4143 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4144 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4146 else
4148 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4149 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4154 /*********************************************
4155 * Vertex Shader Specific Code begins here
4156 ********************************************/
4158 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4160 struct glsl_program_key key;
4162 key.vs_id = entry->vs.id;
4163 key.gs_id = entry->gs.id;
4164 key.ps_id = entry->ps.id;
4166 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4168 ERR("Failed to insert program entry.\n");
4172 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4173 GLhandleARB vs_id, GLhandleARB gs_id, GLhandleARB ps_id)
4175 struct wine_rb_entry *entry;
4176 struct glsl_program_key key;
4178 key.vs_id = vs_id;
4179 key.gs_id = gs_id;
4180 key.ps_id = ps_id;
4182 entry = wine_rb_get(&priv->program_lookup, &key);
4183 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4186 /* Context activation is done by the caller. */
4187 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4188 struct glsl_shader_prog_link *entry)
4190 struct glsl_program_key key;
4192 key.vs_id = entry->vs.id;
4193 key.gs_id = entry->gs.id;
4194 key.ps_id = entry->ps.id;
4195 wine_rb_remove(&priv->program_lookup, &key);
4197 GL_EXTCALL(glDeleteObjectARB(entry->programId));
4198 if (entry->vs.id)
4199 list_remove(&entry->vs.shader_entry);
4200 if (entry->gs.id)
4201 list_remove(&entry->gs.shader_entry);
4202 if (entry->ps.id)
4203 list_remove(&entry->ps.shader_entry);
4204 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4205 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4206 HeapFree(GetProcessHeap(), 0, entry);
4209 static void handle_ps3_input(struct wined3d_shader_buffer *buffer,
4210 const struct wined3d_gl_info *gl_info, const DWORD *map,
4211 const struct wined3d_shader_signature_element *input_signature,
4212 const struct wined3d_shader_reg_maps *reg_maps_in,
4213 const struct wined3d_shader_signature_element *output_signature,
4214 const struct wined3d_shader_reg_maps *reg_maps_out)
4216 unsigned int i, j;
4217 const char *semantic_name_in;
4218 UINT semantic_idx_in;
4219 DWORD *set;
4220 DWORD in_idx;
4221 unsigned int in_count = vec4_varyings(3, gl_info);
4222 char reg_mask[6];
4223 char destination[50];
4224 WORD input_map, output_map;
4226 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
4228 input_map = reg_maps_in->input_registers;
4229 for (i = 0; input_map; input_map >>= 1, ++i)
4231 if (!(input_map & 1)) continue;
4233 in_idx = map[i];
4234 /* Declared, but not read register */
4235 if (in_idx == ~0U) continue;
4236 if (in_idx >= (in_count + 2))
4238 FIXME("More input varyings declared than supported, expect issues.\n");
4239 continue;
4242 if (in_idx == in_count)
4243 sprintf(destination, "gl_FrontColor");
4244 else if (in_idx == in_count + 1)
4245 sprintf(destination, "gl_FrontSecondaryColor");
4246 else
4247 sprintf(destination, "ps_in[%u]", in_idx);
4249 semantic_name_in = input_signature[i].semantic_name;
4250 semantic_idx_in = input_signature[i].semantic_idx;
4251 set[in_idx] = ~0U;
4253 output_map = reg_maps_out->output_registers;
4254 for (j = 0; output_map; output_map >>= 1, ++j)
4256 DWORD mask;
4258 if (!(output_map & 1)
4259 || semantic_idx_in != output_signature[j].semantic_idx
4260 || strcmp(semantic_name_in, output_signature[j].semantic_name)
4261 || !(mask = input_signature[i].mask & output_signature[j].mask))
4262 continue;
4264 set[in_idx] = mask;
4265 shader_glsl_write_mask_to_str(mask, reg_mask);
4267 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4268 destination, reg_mask, j, reg_mask);
4272 for (i = 0; i < in_count + 2; ++i)
4274 unsigned int size;
4276 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4277 continue;
4279 if (set[i] == ~0U) set[i] = 0;
4281 size = 0;
4282 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4283 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4284 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4285 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4286 reg_mask[size] = '\0';
4288 if (i == in_count)
4289 sprintf(destination, "gl_FrontColor");
4290 else if (i == in_count + 1)
4291 sprintf(destination, "gl_FrontSecondaryColor");
4292 else
4293 sprintf(destination, "ps_in[%u]", i);
4295 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
4296 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
4299 HeapFree(GetProcessHeap(), 0, set);
4302 /* Context activation is done by the caller. */
4303 static GLhandleARB generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
4304 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4305 const struct wined3d_gl_info *gl_info)
4307 GLhandleARB ret = 0;
4308 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4309 unsigned int i;
4310 const char *semantic_name;
4311 UINT semantic_idx;
4312 char reg_mask[6];
4313 const struct wined3d_shader_signature_element *output_signature = vs->output_signature;
4314 WORD map = vs->reg_maps.output_registers;
4316 shader_buffer_clear(buffer);
4318 shader_addline(buffer, "#version 120\n");
4320 if (ps_major < 3)
4322 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits.packed_output);
4324 for (i = 0; map; map >>= 1, ++i)
4326 DWORD write_mask;
4328 if (!(map & 1)) continue;
4330 semantic_name = output_signature[i].semantic_name;
4331 semantic_idx = output_signature[i].semantic_idx;
4332 write_mask = output_signature[i].mask;
4333 shader_glsl_write_mask_to_str(write_mask, reg_mask);
4335 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4337 if (!semantic_idx)
4338 shader_addline(buffer, "gl_FrontColor%s = vs_out[%u]%s;\n",
4339 reg_mask, i, reg_mask);
4340 else if (semantic_idx == 1)
4341 shader_addline(buffer, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4342 reg_mask, i, reg_mask);
4344 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4346 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4347 reg_mask, i, reg_mask);
4349 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4351 if (semantic_idx < 8)
4353 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
4354 write_mask |= WINED3DSP_WRITEMASK_3;
4356 shader_addline(buffer, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4357 semantic_idx, reg_mask, i, reg_mask);
4358 if (!(write_mask & WINED3DSP_WRITEMASK_3))
4359 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
4362 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4364 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4366 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
4368 shader_addline(buffer, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n", i, reg_mask[1]);
4371 shader_addline(buffer, "}\n");
4373 else
4375 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits.packed_input);
4376 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4377 shader_addline(buffer, "varying vec4 ps_in[%u];\n", in_count);
4378 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits.packed_output);
4380 /* First, sort out position and point size. Those are not passed to the pixel shader */
4381 for (i = 0; map; map >>= 1, ++i)
4383 if (!(map & 1)) continue;
4385 semantic_name = output_signature[i].semantic_name;
4386 shader_glsl_write_mask_to_str(output_signature[i].mask, reg_mask);
4388 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4390 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4391 reg_mask, i, reg_mask);
4393 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4395 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4399 /* Then, fix the pixel shader input */
4400 handle_ps3_input(buffer, gl_info, ps->u.ps.input_reg_map, ps->input_signature,
4401 &ps->reg_maps, output_signature, &vs->reg_maps);
4403 shader_addline(buffer, "}\n");
4406 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4407 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
4408 shader_glsl_compile(gl_info, ret, buffer->buffer);
4410 return ret;
4413 static void shader_glsl_generate_srgb_write_correction(struct wined3d_shader_buffer *buffer)
4415 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4416 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4417 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4418 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4419 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4420 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4423 static void shader_glsl_generate_fog_code(struct wined3d_shader_buffer *buffer, enum fogmode mode)
4425 switch (mode)
4427 case FOG_OFF:
4428 return;
4430 case FOG_LINEAR:
4431 /* Fog = (gl_Fog.end - gl_FogFragCoord) / (gl_Fog.end - gl_Fog.start) */
4432 shader_addline(buffer, "float Fog = (gl_Fog.end - gl_FogFragCoord) / (gl_Fog.end - gl_Fog.start);\n");
4433 break;
4435 case FOG_EXP:
4436 /* Fog = e^-(gl_Fog.density * gl_FogFragCoord) */
4437 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4438 break;
4440 case FOG_EXP2:
4441 /* Fog = e^-((gl_Fog.density * gl_FogFragCoord)^2) */
4442 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4443 break;
4445 default:
4446 ERR("Invalid fog mode %#x.\n", mode);
4447 return;
4450 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, clamp(Fog, 0.0, 1.0));\n");
4453 /* Context activation is done by the caller. */
4454 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
4455 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4456 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
4458 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4459 const struct wined3d_gl_info *gl_info = context->gl_info;
4460 const DWORD *function = shader->function;
4461 struct shader_glsl_ctx_priv priv_ctx;
4463 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4464 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4466 memset(&priv_ctx, 0, sizeof(priv_ctx));
4467 priv_ctx.cur_ps_args = args;
4468 priv_ctx.cur_np2fixup_info = np2fixup_info;
4470 shader_addline(buffer, "#version 120\n");
4472 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4473 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4474 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4475 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
4476 /* The spec says that it doesn't have to be explicitly enabled, but the
4477 * nvidia drivers write a warning if we don't do so. */
4478 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4479 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4480 if (gl_info->supported[EXT_GPU_SHADER4])
4481 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4483 /* Base Declarations */
4484 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4486 /* Pack 3.0 inputs */
4487 if (reg_maps->shader_version.major >= 3 && args->vp_mode != vertexshader)
4488 shader_glsl_input_pack(shader, buffer, shader->input_signature, reg_maps, args->vp_mode);
4490 /* Base Shader Body */
4491 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4493 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4494 if (reg_maps->shader_version.major < 2)
4496 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4497 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4500 if (args->srgb_correction)
4501 shader_glsl_generate_srgb_write_correction(buffer);
4503 /* SM < 3 does not replace the fog stage. */
4504 if (reg_maps->shader_version.major < 3)
4505 shader_glsl_generate_fog_code(buffer, args->fog);
4507 shader_addline(buffer, "}\n");
4509 TRACE("Compiling shader object %u\n", shader_obj);
4510 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4512 /* Store the shader object */
4513 return shader_obj;
4516 /* Context activation is done by the caller. */
4517 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4518 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4519 const struct vs_compile_args *args)
4521 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4522 const struct wined3d_gl_info *gl_info = context->gl_info;
4523 const DWORD *function = shader->function;
4524 struct shader_glsl_ctx_priv priv_ctx;
4526 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4527 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4529 shader_addline(buffer, "#version 120\n");
4531 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4532 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4533 if (gl_info->supported[EXT_GPU_SHADER4])
4534 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4536 memset(&priv_ctx, 0, sizeof(priv_ctx));
4537 priv_ctx.cur_vs_args = args;
4539 /* Base Declarations */
4540 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4542 /* Base Shader Body */
4543 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4545 /* Unpack outputs */
4546 shader_addline(buffer, "order_ps_input(vs_out);\n");
4548 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4549 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4550 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4551 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4553 if (args->fog_src == VS_FOG_Z)
4554 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4555 else if (!reg_maps->fog)
4556 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4558 /* We always store the clipplanes without y inversion */
4559 if (args->clip_enabled)
4560 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4562 /* Write the final position.
4564 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4565 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4566 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4567 * contains 1.0 to allow a mad.
4569 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4570 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4572 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4574 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4575 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4576 * which is the same as z = z * 2 - w.
4578 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4580 shader_addline(buffer, "}\n");
4582 TRACE("Compiling shader object %u\n", shader_obj);
4583 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4585 return shader_obj;
4588 /* Context activation is done by the caller. */
4589 static GLhandleARB shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
4590 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader)
4592 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4593 const struct wined3d_gl_info *gl_info = context->gl_info;
4594 const DWORD *function = shader->function;
4595 struct shader_glsl_ctx_priv priv_ctx;
4596 GLhandleARB shader_id;
4598 shader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_GEOMETRY_SHADER_ARB));
4600 shader_addline(buffer, "#version 120\n");
4602 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
4603 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
4604 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4605 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4606 if (gl_info->supported[EXT_GPU_SHADER4])
4607 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4609 memset(&priv_ctx, 0, sizeof(priv_ctx));
4610 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4611 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4612 shader_addline(buffer, "}\n");
4614 TRACE("Compiling shader object %u.\n", shader_id);
4615 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4617 return shader_id;
4620 static GLhandleARB find_glsl_pshader(const struct wined3d_context *context,
4621 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4622 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
4624 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
4625 struct glsl_shader_private *shader_data;
4626 struct ps_np2fixup_info *np2fixup;
4627 UINT i;
4628 DWORD new_size;
4629 GLhandleARB ret;
4631 if (!shader->backend_data)
4633 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4634 if (!shader->backend_data)
4636 ERR("Failed to allocate backend data.\n");
4637 return 0;
4640 shader_data = shader->backend_data;
4641 gl_shaders = shader_data->gl_shaders.ps;
4643 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4644 * so a linear search is more performant than a hashmap or a binary search
4645 * (cache coherency etc)
4647 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4649 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
4651 if (args->np2_fixup)
4652 *np2fixup_info = &gl_shaders[i].np2fixup;
4653 return gl_shaders[i].prgId;
4657 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4658 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4659 if (shader_data->num_gl_shaders)
4661 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4662 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
4663 new_size * sizeof(*gl_shaders));
4665 else
4667 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4668 new_size = 1;
4671 if(!new_array) {
4672 ERR("Out of memory\n");
4673 return 0;
4675 shader_data->gl_shaders.ps = new_array;
4676 shader_data->shader_array_size = new_size;
4677 gl_shaders = new_array;
4680 gl_shaders[shader_data->num_gl_shaders].args = *args;
4682 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
4683 memset(np2fixup, 0, sizeof(*np2fixup));
4684 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
4686 pixelshader_update_samplers(shader, args->tex_types);
4688 shader_buffer_clear(buffer);
4689 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4690 gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4692 return ret;
4695 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4696 const DWORD use_map) {
4697 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4698 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4699 return stored->fog_src == new->fog_src;
4702 static GLhandleARB find_glsl_vshader(const struct wined3d_context *context,
4703 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4704 const struct vs_compile_args *args)
4706 UINT i;
4707 DWORD new_size;
4708 DWORD use_map = shader->device->stream_info.use_map;
4709 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
4710 struct glsl_shader_private *shader_data;
4711 GLhandleARB ret;
4713 if (!shader->backend_data)
4715 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4716 if (!shader->backend_data)
4718 ERR("Failed to allocate backend data.\n");
4719 return 0;
4722 shader_data = shader->backend_data;
4723 gl_shaders = shader_data->gl_shaders.vs;
4725 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4726 * so a linear search is more performant than a hashmap or a binary search
4727 * (cache coherency etc)
4729 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4731 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
4732 return gl_shaders[i].prgId;
4735 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4737 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4738 if (shader_data->num_gl_shaders)
4740 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4741 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
4742 new_size * sizeof(*gl_shaders));
4744 else
4746 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4747 new_size = 1;
4750 if(!new_array) {
4751 ERR("Out of memory\n");
4752 return 0;
4754 shader_data->gl_shaders.vs = new_array;
4755 shader_data->shader_array_size = new_size;
4756 gl_shaders = new_array;
4759 gl_shaders[shader_data->num_gl_shaders].args = *args;
4761 shader_buffer_clear(buffer);
4762 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4763 gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4765 return ret;
4768 static GLhandleARB find_glsl_geometry_shader(const struct wined3d_context *context,
4769 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader)
4771 struct glsl_gs_compiled_shader *gl_shaders;
4772 struct glsl_shader_private *shader_data;
4773 GLhandleARB ret;
4775 if (!shader->backend_data)
4777 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
4779 ERR("Failed to allocate backend data.\n");
4780 return 0;
4783 shader_data = shader->backend_data;
4784 gl_shaders = shader_data->gl_shaders.gs;
4786 if (shader_data->num_gl_shaders)
4787 return gl_shaders[0].id;
4789 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4791 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
4793 ERR("Failed to allocate GL shader array.\n");
4794 return 0;
4796 shader_data->shader_array_size = 1;
4797 gl_shaders = shader_data->gl_shaders.gs;
4799 shader_buffer_clear(buffer);
4800 ret = shader_glsl_generate_geometry_shader(context, buffer, shader);
4801 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4803 return ret;
4806 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
4808 switch (mcs)
4810 case WINED3D_MCS_MATERIAL:
4811 return material;
4812 case WINED3D_MCS_COLOR1:
4813 return "gl_Color";
4814 case WINED3D_MCS_COLOR2:
4815 return "gl_SecondaryColor";
4816 default:
4817 ERR("Invalid material color source %#x.\n", mcs);
4818 return "<invalid>";
4822 static void shader_glsl_ffp_vertex_lighting(struct wined3d_shader_buffer *buffer,
4823 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4825 const char *diffuse, *specular, *emission, *ambient;
4826 enum wined3d_light_type light_type;
4827 unsigned int i;
4829 if (!settings->lighting)
4831 shader_addline(buffer, "gl_FrontColor = gl_Color;\n");
4832 shader_addline(buffer, "gl_FrontSecondaryColor = gl_SecondaryColor;\n");
4833 return;
4836 shader_addline(buffer, "vec3 ambient = gl_LightModel.ambient.xyz;\n");
4837 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
4838 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
4839 shader_addline(buffer, "vec3 dir, dst, half;\n");
4840 shader_addline(buffer, "float att, t;\n");
4842 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "gl_FrontMaterial.ambient");
4843 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "gl_FrontMaterial.diffuse");
4844 specular = shader_glsl_ffp_mcs(settings->specular_source, "gl_FrontMaterial.specular");
4845 emission = shader_glsl_ffp_mcs(settings->emission_source, "gl_FrontMaterial.emission");
4847 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
4850 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
4851 switch (light_type)
4853 case WINED3D_LIGHT_POINT:
4854 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4855 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4856 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4857 shader_addline(buffer, "dst.x = 1.0;\n");
4858 shader_addline(buffer, "att = dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4859 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n", i, i, i);
4860 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz / att;\n", i);
4861 if (!settings->normal)
4862 break;
4863 shader_addline(buffer, "dir = normalize(dir);\n");
4864 shader_addline(buffer, "diffuse += (max(0.0, dot(dir, normal))"
4865 " * gl_LightSource[%u].diffuse.xyz) / att;\n", i);
4866 if (settings->localviewer)
4867 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4868 else
4869 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4870 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4871 " * gl_LightSource[%u].specular) / att;\n", i);
4872 break;
4874 case WINED3D_LIGHT_SPOT:
4875 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4876 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4877 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4878 shader_addline(buffer, "dst.x = 1.0;\n");
4879 shader_addline(buffer, "dir = normalize(dir);\n");
4880 shader_addline(buffer, "t = dot(-dir, normalize(gl_LightSource[%u].spotDirection));\n", i);
4881 shader_addline(buffer, "if (t < gl_LightSource[%u].spotCosCutoff) att = 0.0;\n", i);
4882 shader_addline(buffer, "else att = pow(t, gl_LightSource[%u].spotExponent)"
4883 " / dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4884 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n",
4885 i, i, i, i);
4886 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz * att;\n", i);
4887 if (!settings->normal)
4888 break;
4889 shader_addline(buffer, "diffuse += (max(0.0, dot(dir, normal))"
4890 " * gl_LightSource[%u].diffuse.xyz) * att;\n", i);
4891 if (settings->localviewer)
4892 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4893 else
4894 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4895 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4896 " * gl_LightSource[%u].specular) * att;\n", i);
4897 break;
4899 case WINED3D_LIGHT_DIRECTIONAL:
4900 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz;\n", i);
4901 if (!settings->normal)
4902 break;
4903 shader_addline(buffer, "dir = normalize(gl_LightSource[%u].position.xyz);\n", i);
4904 shader_addline(buffer, "diffuse += max(0.0, dot(dir, normal)) * gl_LightSource[%u].diffuse.xyz;\n", i);
4905 shader_addline(buffer, "t = dot(normal, gl_LightSource[%u].halfVector.xyz);\n", i);
4906 shader_addline(buffer, "if (t > 0.0) specular += pow(t, gl_FrontMaterial.shininess)"
4907 " * gl_LightSource[%u].specular;\n", i);
4908 break;
4910 default:
4911 if (light_type)
4912 FIXME("Unhandled light type %#x.\n", light_type);
4913 continue;
4917 shader_addline(buffer, "gl_FrontColor.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
4918 ambient, diffuse, emission);
4919 shader_addline(buffer, "gl_FrontColor.w = %s.w;\n", diffuse);
4920 shader_addline(buffer, "gl_FrontSecondaryColor = %s * specular;\n", specular);
4923 /* Context activation is done by the caller. */
4924 static GLhandleARB shader_glsl_generate_ffp_vertex_shader(struct wined3d_shader_buffer *buffer,
4925 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4927 GLhandleARB shader_obj;
4928 unsigned int i;
4930 shader_buffer_clear(buffer);
4932 shader_addline(buffer, "#version 120\n");
4933 shader_addline(buffer, "\n");
4934 shader_addline(buffer, "void main()\n{\n");
4935 shader_addline(buffer, "float m;\n");
4936 shader_addline(buffer, "vec3 r;\n");
4938 shader_addline(buffer, "vec4 ec_pos = gl_ModelViewMatrix * gl_Vertex;\n");
4939 shader_addline(buffer, "gl_Position = gl_ProjectionMatrix * ec_pos;\n");
4940 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
4941 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
4943 if (!settings->normal)
4944 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
4945 else if (settings->normalize)
4946 shader_addline(buffer, "vec3 normal = normalize(gl_NormalMatrix * gl_Normal);\n");
4947 else
4948 shader_addline(buffer, "vec3 normal = gl_NormalMatrix * gl_Normal;\n");
4950 shader_glsl_ffp_vertex_lighting(buffer, settings, gl_info);
4952 for (i = 0; i < MAX_TEXTURES; ++i)
4954 switch (settings->texgen[i] << WINED3D_FFP_TCI_SHIFT)
4956 case WINED3DTSS_TCI_PASSTHRU:
4957 if (settings->texcoords & (1 << i))
4958 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * gl_MultiTexCoord%d;\n",
4959 i, i, i);
4960 break;
4962 case WINED3DTSS_TCI_CAMERASPACENORMAL:
4963 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * vec4(normal, 1.0);\n", i, i);
4964 break;
4966 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
4967 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * ec_pos;\n", i, i);
4968 break;
4970 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
4971 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
4972 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
4973 break;
4975 case WINED3DTSS_TCI_SPHEREMAP:
4976 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
4977 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
4978 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
4979 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);", i, i);
4980 break;
4982 default:
4983 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
4984 break;
4988 switch (settings->fog_mode)
4990 case WINED3D_FFP_VS_FOG_OFF:
4991 break;
4993 case WINED3D_FFP_VS_FOG_FOGCOORD:
4994 shader_addline(buffer, "gl_FogFragCoord = gl_SecondaryColor.w * 255.0;\n");
4995 break;
4997 case WINED3D_FFP_VS_FOG_RANGE:
4998 shader_addline(buffer, "gl_FogFragCoord = length(ec_pos.xyz);\n");
4999 break;
5001 case WINED3D_FFP_VS_FOG_DEPTH:
5002 shader_addline(buffer, "gl_FogFragCoord = ec_pos.z;\n");
5003 break;
5005 default:
5006 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
5007 break;
5010 shader_addline(buffer, "gl_PointSize = gl_Point.size / sqrt(gl_Point.distanceConstantAttenuation"
5011 " + gl_Point.distanceLinearAttenuation * length(ec_pos.xyz)"
5012 " + gl_Point.distanceQuadraticAttenuation * dot(ec_pos.xyz, ec_pos.xyz));\n");
5013 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, gl_Point.sizeMin, gl_Point.sizeMax);\n");
5015 shader_addline(buffer, "}\n");
5017 shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
5018 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5020 return shader_obj;
5023 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_shader_buffer *buffer,
5024 DWORD argnum, unsigned int stage, DWORD arg)
5026 const char *ret;
5028 if (arg == ARG_UNUSED)
5029 return "<unused arg>";
5031 switch (arg & WINED3DTA_SELECTMASK)
5033 case WINED3DTA_DIFFUSE:
5034 ret = "gl_Color";
5035 break;
5037 case WINED3DTA_CURRENT:
5038 if (!stage)
5039 ret = "gl_Color";
5040 else
5041 ret = "ret";
5042 break;
5044 case WINED3DTA_TEXTURE:
5045 switch (stage)
5047 case 0: ret = "tex0"; break;
5048 case 1: ret = "tex1"; break;
5049 case 2: ret = "tex2"; break;
5050 case 3: ret = "tex3"; break;
5051 case 4: ret = "tex4"; break;
5052 case 5: ret = "tex5"; break;
5053 case 6: ret = "tex6"; break;
5054 case 7: ret = "tex7"; break;
5055 default:
5056 ret = "<invalid texture>";
5057 break;
5059 break;
5061 case WINED3DTA_TFACTOR:
5062 ret = "tex_factor";
5063 break;
5065 case WINED3DTA_SPECULAR:
5066 ret = "gl_SecondaryColor";
5067 break;
5069 case WINED3DTA_TEMP:
5070 ret = "temp_reg";
5071 break;
5073 case WINED3DTA_CONSTANT:
5074 FIXME("Per-stage constants not implemented.\n");
5075 switch (stage)
5077 case 0: ret = "const0"; break;
5078 case 1: ret = "const1"; break;
5079 case 2: ret = "const2"; break;
5080 case 3: ret = "const3"; break;
5081 case 4: ret = "const4"; break;
5082 case 5: ret = "const5"; break;
5083 case 6: ret = "const6"; break;
5084 case 7: ret = "const7"; break;
5085 default:
5086 ret = "<invalid constant>";
5087 break;
5089 break;
5091 default:
5092 return "<unhandled arg>";
5095 if (arg & WINED3DTA_COMPLEMENT)
5097 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
5098 if (argnum == 0)
5099 ret = "arg0";
5100 else if (argnum == 1)
5101 ret = "arg1";
5102 else if (argnum == 2)
5103 ret = "arg2";
5106 if (arg & WINED3DTA_ALPHAREPLICATE)
5108 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
5109 if (argnum == 0)
5110 ret = "arg0";
5111 else if (argnum == 1)
5112 ret = "arg1";
5113 else if (argnum == 2)
5114 ret = "arg2";
5117 return ret;
5120 static void shader_glsl_ffp_fragment_op(struct wined3d_shader_buffer *buffer, unsigned int stage, BOOL color,
5121 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
5123 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5125 if (color && alpha)
5126 dstmask = "";
5127 else if (color)
5128 dstmask = ".xyz";
5129 else
5130 dstmask = ".w";
5132 if (dst == tempreg)
5133 dstreg = "temp_reg";
5134 else
5135 dstreg = "ret";
5137 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
5138 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
5139 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
5141 switch (op)
5143 case WINED3D_TOP_DISABLE:
5144 if (!stage)
5145 shader_addline(buffer, "%s%s = gl_Color%s;\n", dstreg, dstmask, dstmask);
5146 break;
5148 case WINED3D_TOP_SELECT_ARG1:
5149 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
5150 break;
5152 case WINED3D_TOP_SELECT_ARG2:
5153 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
5154 break;
5156 case WINED3D_TOP_MODULATE:
5157 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5158 break;
5160 case WINED3D_TOP_MODULATE_4X:
5161 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
5162 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5163 break;
5165 case WINED3D_TOP_MODULATE_2X:
5166 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
5167 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5168 break;
5170 case WINED3D_TOP_ADD:
5171 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
5172 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5173 break;
5175 case WINED3D_TOP_ADD_SIGNED:
5176 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
5177 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5178 break;
5180 case WINED3D_TOP_ADD_SIGNED_2X:
5181 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
5182 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5183 break;
5185 case WINED3D_TOP_SUBTRACT:
5186 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
5187 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5188 break;
5190 case WINED3D_TOP_ADD_SMOOTH:
5191 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
5192 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
5193 break;
5195 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
5196 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5197 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5198 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5199 break;
5201 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5202 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5203 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5204 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5205 break;
5207 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5208 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
5209 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5210 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5211 break;
5213 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5214 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5215 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5216 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
5217 break;
5219 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
5220 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
5221 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5222 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5223 break;
5225 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
5226 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
5227 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5228 break;
5230 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
5231 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
5232 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5233 break;
5235 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
5236 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5237 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5238 break;
5239 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
5240 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
5241 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5242 break;
5244 case WINED3D_TOP_BUMPENVMAP:
5245 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5246 /* These are handled in the first pass, nothing to do. */
5247 break;
5249 case WINED3D_TOP_DOTPRODUCT3:
5250 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
5251 dstreg, dstmask, arg1, arg2, dstmask);
5252 break;
5254 case WINED3D_TOP_MULTIPLY_ADD:
5255 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
5256 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
5257 break;
5259 case WINED3D_TOP_LERP:
5260 /* MSDN isn't quite right here. */
5261 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
5262 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
5263 break;
5265 default:
5266 FIXME("Unhandled operation %#x.\n", op);
5267 break;
5271 /* Context activation is done by the caller. */
5272 static GLuint shader_glsl_generate_ffp_fragment_shader(struct wined3d_shader_buffer *buffer,
5273 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
5275 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
5276 BYTE lum_map = 0, bump_map = 0, tex_map = 0;
5277 const char *final_combiner_src = "ret";
5278 UINT lowest_disabled_stage;
5279 GLhandleARB shader_obj;
5280 DWORD arg0, arg1, arg2;
5281 unsigned int stage;
5283 shader_buffer_clear(buffer);
5285 /* Find out which textures are read */
5286 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5288 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5289 break;
5291 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
5292 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
5293 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
5295 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5296 tex_map |= 1 << stage;
5297 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5298 tfactor_used = TRUE;
5299 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5300 tempreg_used = TRUE;
5301 if (settings->op[stage].dst == tempreg)
5302 tempreg_used = TRUE;
5304 switch (settings->op[stage].cop)
5306 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5307 lum_map |= 1 << stage;
5308 /* fall through */
5309 case WINED3D_TOP_BUMPENVMAP:
5310 bump_map |= 1 << stage;
5311 /* fall through */
5312 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5313 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5314 tex_map |= 1 << stage;
5315 break;
5317 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5318 tfactor_used = TRUE;
5319 break;
5321 default:
5322 break;
5325 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5326 continue;
5328 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
5329 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
5330 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
5332 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5333 tex_map |= 1 << stage;
5334 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5335 tfactor_used = TRUE;
5336 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5337 tempreg_used = TRUE;
5339 lowest_disabled_stage = stage;
5341 shader_addline(buffer, "#version 120\n");
5343 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5344 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5346 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
5347 shader_addline(buffer, "vec4 ret;\n");
5348 if (tempreg_used || settings->sRGB_write)
5349 shader_addline(buffer, "vec4 temp_reg;\n");
5350 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
5352 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5354 if (!(tex_map & (1 << stage)))
5355 continue;
5357 switch (settings->op[stage].tex_type)
5359 case tex_1d:
5360 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
5361 break;
5362 case tex_2d:
5363 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
5364 break;
5365 case tex_3d:
5366 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
5367 break;
5368 case tex_cube:
5369 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
5370 break;
5371 case tex_rect:
5372 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
5373 break;
5374 default:
5375 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
5376 break;
5379 shader_addline(buffer, "vec4 tex%u;\n", stage);
5381 if (!(bump_map & (1 << stage)))
5382 continue;
5383 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
5385 if (!(lum_map & (1 << stage)))
5386 continue;
5387 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
5388 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
5390 if (tfactor_used)
5391 shader_addline(buffer, "uniform vec4 tex_factor;\n");
5392 shader_addline(buffer, "uniform vec4 specular_enable;\n");
5394 if (settings->sRGB_write)
5396 shader_addline(buffer, "const vec4 srgb_const0 = vec4(%.8e, %.8e, %.8e, %.8e);\n",
5397 srgb_pow, srgb_mul_high, srgb_sub_high, srgb_mul_low);
5398 shader_addline(buffer, "const vec4 srgb_const1 = vec4(%.8e, 0.0, 0.0, 0.0);\n",
5399 srgb_cmp);
5402 shader_addline(buffer, "void main()\n{\n");
5404 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
5405 shader_addline(buffer, "if (any(lessThan(gl_texCoord[7], vec4(0.0)))) discard;\n");
5407 /* Generate texture sampling instructions) */
5408 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
5410 const char *texture_function, *coord_mask;
5411 char tex_reg_name[8];
5412 BOOL proj, clamp;
5414 if (!(tex_map & (1 << stage)))
5415 continue;
5417 if (settings->op[stage].projected == proj_none)
5419 proj = FALSE;
5421 else if (settings->op[stage].projected == proj_count4
5422 || settings->op[stage].projected == proj_count3)
5424 proj = TRUE;
5426 else
5428 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
5429 proj = TRUE;
5432 if (settings->op[stage].cop == WINED3D_TOP_BUMPENVMAP
5433 || settings->op[stage].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
5434 clamp = FALSE;
5435 else
5436 clamp = TRUE;
5438 switch (settings->op[stage].tex_type)
5440 case tex_1d:
5441 if (proj)
5443 texture_function = "texture1DProj";
5444 coord_mask = "xw";
5446 else
5448 texture_function = "texture1D";
5449 coord_mask = "x";
5451 break;
5452 case tex_2d:
5453 if (proj)
5455 texture_function = "texture2DProj";
5456 coord_mask = "xyw";
5458 else
5460 texture_function = "texture2D";
5461 coord_mask = "xy";
5463 break;
5464 case tex_3d:
5465 if (proj)
5467 texture_function = "texture3DProj";
5468 coord_mask = "xyzw";
5470 else
5472 texture_function = "texture3D";
5473 coord_mask = "xyz";
5475 break;
5476 case tex_cube:
5477 texture_function = "textureCube";
5478 coord_mask = "xyz";
5479 break;
5480 case tex_rect:
5481 if (proj)
5483 texture_function = "texture2DRectProj";
5484 coord_mask = "xyw";
5486 else
5488 texture_function = "texture2DRect";
5489 coord_mask = "xy";
5491 break;
5492 default:
5493 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
5494 texture_function = "";
5495 coord_mask = "xyzw";
5496 break;
5499 if (stage > 0
5500 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
5501 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
5503 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
5505 /* With projective textures, texbem only divides the static
5506 * texture coord, not the displacement, so multiply the
5507 * displacement with the dividing parameter before passing it to
5508 * TXP. */
5509 if (settings->op[stage].projected != proj_none)
5511 if (settings->op[stage].projected == proj_count4)
5513 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].w) + gl_TexCoord[%u].xy;\n",
5514 stage, stage);
5515 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].ww;\n", stage);
5517 else
5519 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].z) + gl_TexCoord[%u].xy;\n",
5520 stage, stage);
5521 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].zz;\n", stage);
5524 else
5526 shader_addline(buffer, "ret = gl_TexCoord[%u] + ret.xyxy;\n", stage);
5529 if (clamp)
5530 shader_addline(buffer, "tex%u = clamp(%s(ps_sampler%u, ret.%s), 0.0, 1.0);\n",
5531 stage, texture_function, stage, coord_mask);
5532 else
5533 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
5534 stage, texture_function, stage, coord_mask);
5536 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
5537 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
5538 stage, stage - 1, stage - 1, stage - 1);
5540 else if (settings->op[stage].projected == proj_count3)
5542 if (clamp)
5543 shader_addline(buffer, "tex%u = clamp(%s(ps_sampler%u, gl_TexCoord[%u].xyz), 0.0, 1.0);\n",
5544 stage, texture_function, stage, stage);
5545 else
5546 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].xyz);\n",
5547 stage, texture_function, stage, stage);
5549 else
5551 if (clamp)
5552 shader_addline(buffer, "tex%u = clamp(%s(ps_sampler%u, gl_TexCoord[%u].%s), 0.0, 1.0);\n",
5553 stage, texture_function, stage, stage, coord_mask);
5554 else
5555 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].%s);\n",
5556 stage, texture_function, stage, stage, coord_mask);
5559 sprintf(tex_reg_name, "tex%u", stage);
5560 shader_glsl_color_correction_ext(buffer, tex_reg_name, WINED3DSP_WRITEMASK_ALL,
5561 settings->op[stage].color_fixup);
5564 /* Generate the main shader */
5565 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5567 BOOL op_equal;
5569 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5571 if (!stage)
5572 final_combiner_src = "gl_Color";
5573 break;
5576 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5577 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5578 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
5579 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5580 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5581 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
5582 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5583 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5584 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
5585 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5586 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5587 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
5588 else
5589 op_equal = settings->op[stage].aop == settings->op[stage].cop
5590 && settings->op[stage].carg0 == settings->op[stage].aarg0
5591 && settings->op[stage].carg1 == settings->op[stage].aarg1
5592 && settings->op[stage].carg2 == settings->op[stage].aarg2;
5594 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5596 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5597 settings->op[stage].cop, settings->op[stage].carg0,
5598 settings->op[stage].carg1, settings->op[stage].carg2);
5599 if (!stage)
5600 shader_addline(buffer, "ret.w = gl_Color.w;\n");
5602 else if (op_equal)
5604 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
5605 settings->op[stage].cop, settings->op[stage].carg0,
5606 settings->op[stage].carg1, settings->op[stage].carg2);
5608 else
5610 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5611 settings->op[stage].cop, settings->op[stage].carg0,
5612 settings->op[stage].carg1, settings->op[stage].carg2);
5613 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
5614 settings->op[stage].aop, settings->op[stage].aarg0,
5615 settings->op[stage].aarg1, settings->op[stage].aarg2);
5619 shader_addline(buffer, "gl_FragData[0] = gl_SecondaryColor * specular_enable + %s;\n", final_combiner_src);
5621 if (settings->sRGB_write)
5622 shader_glsl_generate_srgb_write_correction(buffer);
5624 shader_glsl_generate_fog_code(buffer, settings->fog);
5626 shader_addline(buffer, "}\n");
5628 shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
5629 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5630 return shader_obj;
5633 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
5634 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
5636 struct glsl_ffp_vertex_shader *shader;
5637 const struct wine_rb_entry *entry;
5639 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
5640 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
5642 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
5643 return NULL;
5645 shader->desc.settings = *settings;
5646 shader->id = shader_glsl_generate_ffp_vertex_shader(&priv->shader_buffer, settings, gl_info);
5647 list_init(&shader->linked_programs);
5648 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
5649 ERR("Failed to insert ffp vertex shader.\n");
5651 return shader;
5654 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
5655 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
5657 struct glsl_ffp_fragment_shader *glsl_desc;
5658 const struct ffp_frag_desc *desc;
5660 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
5661 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
5663 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
5664 return NULL;
5666 glsl_desc->entry.settings = *args;
5667 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(&priv->shader_buffer, args, gl_info);
5668 list_init(&glsl_desc->linked_programs);
5669 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
5671 return glsl_desc;
5675 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
5676 GLhandleARB program_id, struct glsl_vs_program *vs)
5678 unsigned int i;
5679 char name[32];
5681 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5682 sizeof(GLhandleARB) * gl_info->limits.glsl_vs_float_constants);
5683 for (i = 0; i < gl_info->limits.glsl_vs_float_constants; ++i)
5685 snprintf(name, sizeof(name), "vs_c[%u]", i);
5686 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5689 for (i = 0; i < MAX_CONST_I; ++i)
5691 snprintf(name, sizeof(name), "vs_i[%u]", i);
5692 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5695 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "posFixup"));
5698 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
5699 GLhandleARB program_id, struct glsl_ps_program *ps)
5701 unsigned int i;
5702 char name[32];
5704 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5705 sizeof(GLhandleARB) * gl_info->limits.glsl_ps_float_constants);
5706 for (i = 0; i < gl_info->limits.glsl_ps_float_constants; ++i)
5708 snprintf(name, sizeof(name), "ps_c[%u]", i);
5709 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5712 for (i = 0; i < MAX_CONST_I; ++i)
5714 snprintf(name, sizeof(name), "ps_i[%u]", i);
5715 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5718 for (i = 0; i < MAX_TEXTURES; ++i)
5720 snprintf(name, sizeof(name), "bumpenv_mat%u", i);
5721 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5722 snprintf(name, sizeof(name), "bumpenv_lum_scale%u", i);
5723 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5724 snprintf(name, sizeof(name), "bumpenv_lum_offset%u", i);
5725 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5728 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "tex_factor"));
5729 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "specular_enable"));
5730 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "ps_samplerNP2Fixup"));
5731 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "ycorrection"));
5734 /* Context activation is done by the caller. */
5735 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
5736 struct shader_glsl_priv *priv)
5738 const struct wined3d_gl_info *gl_info = context->gl_info;
5739 const struct ps_np2fixup_info *np2fixup_info = NULL;
5740 struct glsl_shader_prog_link *entry = NULL;
5741 struct wined3d_shader *vshader = NULL;
5742 struct wined3d_shader *gshader = NULL;
5743 struct wined3d_shader *pshader = NULL;
5744 GLhandleARB programId = 0;
5745 GLhandleARB reorder_shader_id = 0;
5746 unsigned int i;
5747 struct ps_compile_args ps_compile_args;
5748 struct vs_compile_args vs_compile_args;
5749 GLhandleARB vs_id, gs_id, ps_id;
5750 struct list *ps_list, *vs_list;
5751 struct wined3d_device *device = context->swapchain->device;
5753 if (use_vs(state))
5755 vshader = state->vertex_shader;
5756 find_vs_compile_args(state, vshader, &vs_compile_args);
5757 vs_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
5758 vs_list = &vshader->linked_programs;
5760 if ((gshader = state->geometry_shader))
5761 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5762 else
5763 gs_id = 0;
5765 else if (priv->vertex_pipe == &glsl_vertex_pipe)
5767 struct glsl_ffp_vertex_shader *ffp_shader;
5768 struct wined3d_ffp_vs_settings settings;
5770 wined3d_ffp_get_vs_settings(state, &device->stream_info, &settings);
5771 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
5772 vs_id = ffp_shader->id;
5773 vs_list = &ffp_shader->linked_programs;
5775 gs_id = 0;
5777 else
5779 vs_id = 0;
5780 gs_id = 0;
5783 if (use_ps(state))
5785 pshader = state->pixel_shader;
5786 find_ps_compile_args(state, pshader, &ps_compile_args);
5787 ps_id = find_glsl_pshader(context, &priv->shader_buffer,
5788 pshader, &ps_compile_args, &np2fixup_info);
5789 ps_list = &pshader->linked_programs;
5791 else if (priv->fragment_pipe == &glsl_fragment_pipe)
5793 struct glsl_ffp_fragment_shader *ffp_shader;
5794 struct ffp_frag_settings settings;
5796 gen_ffp_frag_op(context, state, &settings, FALSE);
5797 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
5798 ps_id = ffp_shader->id;
5799 ps_list = &ffp_shader->linked_programs;
5801 else
5803 ps_id = 0;
5806 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
5808 priv->glsl_program = entry;
5809 return;
5812 /* If we get to this point, then no matching program exists, so we create one */
5813 programId = GL_EXTCALL(glCreateProgramObjectARB());
5814 TRACE("Created new GLSL shader program %u\n", programId);
5816 /* Create the entry */
5817 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
5818 entry->programId = programId;
5819 entry->vs.id = vs_id;
5820 entry->gs.id = gs_id;
5821 entry->ps.id = ps_id;
5822 entry->constant_version = 0;
5823 entry->ps.np2_fixup_info = np2fixup_info;
5824 /* Add the hash table entry */
5825 add_glsl_program_entry(priv, entry);
5827 /* Set the current program */
5828 priv->glsl_program = entry;
5830 /* Attach GLSL vshader */
5831 if (vs_id)
5833 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, programId);
5834 GL_EXTCALL(glAttachObjectARB(programId, vs_id));
5835 checkGLcall("glAttachObjectARB");
5837 list_add_head(vs_list, &entry->vs.shader_entry);
5840 if (vshader)
5842 WORD map = vshader->reg_maps.input_registers;
5843 char tmp_name[10];
5845 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
5846 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
5847 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
5848 checkGLcall("glAttachObjectARB");
5849 /* Flag the reorder function for deletion, then it will be freed automatically when the program
5850 * is destroyed
5852 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
5854 /* Bind vertex attributes to a corresponding index number to match
5855 * the same index numbers as ARB_vertex_programs (makes loading
5856 * vertex attributes simpler). With this method, we can use the
5857 * exact same code to load the attributes later for both ARB and
5858 * GLSL shaders.
5860 * We have to do this here because we need to know the Program ID
5861 * in order to make the bindings work, and it has to be done prior
5862 * to linking the GLSL program. */
5863 for (i = 0; map; map >>= 1, ++i)
5865 if (!(map & 1)) continue;
5867 snprintf(tmp_name, sizeof(tmp_name), "vs_in%u", i);
5868 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
5870 checkGLcall("glBindAttribLocationARB");
5873 if (gshader)
5875 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, programId);
5876 GL_EXTCALL(glAttachObjectARB(programId, gs_id));
5877 checkGLcall("glAttachObjectARB");
5879 TRACE("input type %s, output type %s, vertices out %u.\n",
5880 debug_d3dprimitivetype(gshader->u.gs.input_type),
5881 debug_d3dprimitivetype(gshader->u.gs.output_type),
5882 gshader->u.gs.vertices_out);
5883 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_INPUT_TYPE_ARB,
5884 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
5885 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_OUTPUT_TYPE_ARB,
5886 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
5887 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_VERTICES_OUT_ARB,
5888 gshader->u.gs.vertices_out));
5889 checkGLcall("glProgramParameteriARB");
5891 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
5894 /* Attach GLSL pshader */
5895 if (ps_id)
5897 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, programId);
5898 GL_EXTCALL(glAttachObjectARB(programId, ps_id));
5899 checkGLcall("glAttachObjectARB");
5901 list_add_head(ps_list, &entry->ps.shader_entry);
5904 /* Link the program */
5905 TRACE("Linking GLSL shader program %u\n", programId);
5906 GL_EXTCALL(glLinkProgramARB(programId));
5907 shader_glsl_validate_link(gl_info, programId);
5909 shader_glsl_init_vs_uniform_locations(gl_info, programId, &entry->vs);
5910 shader_glsl_init_ps_uniform_locations(gl_info, programId, &entry->ps);
5911 checkGLcall("Find glsl program uniform locations");
5913 if (pshader && pshader->reg_maps.shader_version.major >= 3
5914 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
5916 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
5917 entry->vs.vertex_color_clamp = GL_FALSE;
5919 else
5921 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
5924 /* Set the shader to allow uniform loading on it */
5925 GL_EXTCALL(glUseProgramObjectARB(programId));
5926 checkGLcall("glUseProgramObjectARB(programId)");
5928 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
5929 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
5930 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
5931 * vertex shader with fixed function pixel processing is used we make sure that the card
5932 * supports enough samplers to allow the max number of vertex samplers with all possible
5933 * fixed function fragment processing setups. So once the program is linked these samplers
5934 * won't change.
5936 shader_glsl_load_vsamplers(gl_info, device->texUnitMap, programId);
5937 shader_glsl_load_psamplers(gl_info, device->texUnitMap, programId);
5940 /* Context activation is done by the caller. */
5941 static GLhandleARB create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum tex_types tex_type, BOOL masked)
5943 GLhandleARB program_id;
5944 GLhandleARB vshader_id, pshader_id;
5945 const char *blt_pshader;
5947 static const char *blt_vshader =
5948 "#version 120\n"
5949 "void main(void)\n"
5950 "{\n"
5951 " gl_Position = gl_Vertex;\n"
5952 " gl_FrontColor = vec4(1.0);\n"
5953 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
5954 "}\n";
5956 static const char * const blt_pshaders_full[tex_type_count] =
5958 /* tex_1d */
5959 NULL,
5960 /* tex_2d */
5961 "#version 120\n"
5962 "uniform sampler2D sampler;\n"
5963 "void main(void)\n"
5964 "{\n"
5965 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
5966 "}\n",
5967 /* tex_3d */
5968 NULL,
5969 /* tex_cube */
5970 "#version 120\n"
5971 "uniform samplerCube sampler;\n"
5972 "void main(void)\n"
5973 "{\n"
5974 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
5975 "}\n",
5976 /* tex_rect */
5977 "#version 120\n"
5978 "#extension GL_ARB_texture_rectangle : enable\n"
5979 "uniform sampler2DRect sampler;\n"
5980 "void main(void)\n"
5981 "{\n"
5982 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
5983 "}\n",
5986 static const char * const blt_pshaders_masked[tex_type_count] =
5988 /* tex_1d */
5989 NULL,
5990 /* tex_2d */
5991 "#version 120\n"
5992 "uniform sampler2D sampler;\n"
5993 "uniform vec4 mask;\n"
5994 "void main(void)\n"
5995 "{\n"
5996 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
5997 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
5998 "}\n",
5999 /* tex_3d */
6000 NULL,
6001 /* tex_cube */
6002 "#version 120\n"
6003 "uniform samplerCube sampler;\n"
6004 "uniform vec4 mask;\n"
6005 "void main(void)\n"
6006 "{\n"
6007 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6008 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6009 "}\n",
6010 /* tex_rect */
6011 "#version 120\n"
6012 "#extension GL_ARB_texture_rectangle : enable\n"
6013 "uniform sampler2DRect sampler;\n"
6014 "uniform vec4 mask;\n"
6015 "void main(void)\n"
6016 "{\n"
6017 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6018 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6019 "}\n",
6022 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
6023 if (!blt_pshader)
6025 FIXME("tex_type %#x not supported\n", tex_type);
6026 return 0;
6029 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
6030 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
6032 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
6033 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
6035 program_id = GL_EXTCALL(glCreateProgramObjectARB());
6036 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
6037 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
6038 GL_EXTCALL(glLinkProgramARB(program_id));
6040 shader_glsl_validate_link(gl_info, program_id);
6042 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
6043 * is destroyed
6045 GL_EXTCALL(glDeleteObjectARB(vshader_id));
6046 GL_EXTCALL(glDeleteObjectARB(pshader_id));
6047 return program_id;
6050 /* Context activation is done by the caller. */
6051 static void shader_glsl_select(void *shader_priv, const struct wined3d_context *context,
6052 const struct wined3d_state *state)
6054 const struct wined3d_gl_info *gl_info = context->gl_info;
6055 struct shader_glsl_priv *priv = shader_priv;
6056 GLhandleARB program_id = 0;
6057 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
6059 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
6060 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
6062 old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vs.vertex_color_clamp : GL_FIXED_ONLY_ARB;
6063 set_glsl_shader_program(context, state, priv);
6064 current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vs.vertex_color_clamp : GL_FIXED_ONLY_ARB;
6065 if (old_vertex_color_clamp != current_vertex_color_clamp)
6067 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6069 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
6070 checkGLcall("glClampColorARB");
6072 else
6074 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
6078 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
6079 if (program_id) TRACE("Using GLSL program %u\n", program_id);
6080 GL_EXTCALL(glUseProgramObjectARB(program_id));
6081 checkGLcall("glUseProgramObjectARB");
6083 /* In case that NP2 texcoord fixup data is found for the selected program, trigger a reload of the
6084 * constants. This has to be done because it can't be guaranteed that sampler() (from state.c) is
6085 * called between selecting the shader and using it, which results in wrong fixup for some frames. */
6086 if (priv->glsl_program && priv->glsl_program->ps.np2_fixup_info)
6088 shader_glsl_load_np2fixup_constants(priv, gl_info, state);
6092 /* Context activation is done by the caller. */
6093 static void shader_glsl_disable(void *shader_priv, const struct wined3d_context *context)
6095 const struct wined3d_gl_info *gl_info = context->gl_info;
6096 struct shader_glsl_priv *priv = shader_priv;
6098 priv->glsl_program = NULL;
6099 GL_EXTCALL(glUseProgramObjectARB(0));
6100 checkGLcall("glUseProgramObjectARB");
6102 priv->vertex_pipe->vp_enable(gl_info, FALSE);
6103 priv->fragment_pipe->enable_extension(gl_info, FALSE);
6105 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6107 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
6108 checkGLcall("glClampColorARB");
6112 /* Context activation is done by the caller. */
6113 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
6114 enum tex_types tex_type, const SIZE *ds_mask_size)
6116 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
6117 struct shader_glsl_priv *priv = shader_priv;
6118 GLhandleARB *blt_program;
6119 GLint loc;
6121 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
6122 if (!*blt_program)
6124 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
6125 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
6126 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
6127 GL_EXTCALL(glUniform1iARB(loc, 0));
6129 else
6131 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
6134 if (masked)
6136 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "mask"));
6137 GL_EXTCALL(glUniform4fARB(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
6141 /* Context activation is done by the caller. */
6142 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
6144 struct shader_glsl_priv *priv = shader_priv;
6145 GLhandleARB program_id;
6147 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
6148 if (program_id) TRACE("Using GLSL program %u\n", program_id);
6150 GL_EXTCALL(glUseProgramObjectARB(program_id));
6151 checkGLcall("glUseProgramObjectARB");
6154 static void shader_glsl_destroy(struct wined3d_shader *shader)
6156 struct glsl_shader_private *shader_data = shader->backend_data;
6157 struct wined3d_device *device = shader->device;
6158 struct shader_glsl_priv *priv = device->shader_priv;
6159 const struct wined3d_gl_info *gl_info;
6160 const struct list *linked_programs;
6161 struct wined3d_context *context;
6163 if (!shader_data || !shader_data->num_gl_shaders)
6165 HeapFree(GetProcessHeap(), 0, shader_data);
6166 shader->backend_data = NULL;
6167 return;
6170 context = context_acquire(device, NULL);
6171 gl_info = context->gl_info;
6173 TRACE("Deleting linked programs.\n");
6174 linked_programs = &shader->linked_programs;
6175 if (linked_programs->next)
6177 struct glsl_shader_prog_link *entry, *entry2;
6178 UINT i;
6180 switch (shader->reg_maps.shader_version.type)
6182 case WINED3D_SHADER_TYPE_PIXEL:
6184 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
6186 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6187 struct glsl_shader_prog_link, ps.shader_entry)
6189 delete_glsl_program_entry(priv, gl_info, entry);
6192 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6194 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].prgId);
6195 if (priv->glsl_program && priv->glsl_program->ps.id == gl_shaders[i].prgId)
6196 shader_glsl_disable(priv, context);
6197 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].prgId));
6198 checkGLcall("glDeleteObjectARB");
6200 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
6202 break;
6205 case WINED3D_SHADER_TYPE_VERTEX:
6207 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
6209 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6210 struct glsl_shader_prog_link, vs.shader_entry)
6212 delete_glsl_program_entry(priv, gl_info, entry);
6215 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6217 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].prgId);
6218 if (priv->glsl_program && priv->glsl_program->vs.id == gl_shaders[i].prgId)
6219 shader_glsl_disable(priv, context);
6220 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].prgId));
6221 checkGLcall("glDeleteObjectARB");
6223 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
6225 break;
6228 case WINED3D_SHADER_TYPE_GEOMETRY:
6230 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
6232 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6233 struct glsl_shader_prog_link, gs.shader_entry)
6235 delete_glsl_program_entry(priv, gl_info, entry);
6238 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6240 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
6241 if (priv->glsl_program && priv->glsl_program->gs.id == gl_shaders[i].id)
6242 shader_glsl_disable(priv, context);
6243 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].id));
6244 checkGLcall("glDeleteObjectARB");
6246 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
6248 break;
6251 default:
6252 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
6253 break;
6257 HeapFree(GetProcessHeap(), 0, shader->backend_data);
6258 shader->backend_data = NULL;
6260 context_release(context);
6263 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
6265 const struct glsl_program_key *k = key;
6266 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
6267 const struct glsl_shader_prog_link, program_lookup_entry);
6269 if (k->vs_id > prog->vs.id) return 1;
6270 else if (k->vs_id < prog->vs.id) return -1;
6272 if (k->gs_id > prog->gs.id) return 1;
6273 else if (k->gs_id < prog->gs.id) return -1;
6275 if (k->ps_id > prog->ps.id) return 1;
6276 else if (k->ps_id < prog->ps.id) return -1;
6278 return 0;
6281 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
6283 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries) + constant_count * sizeof(*heap->positions);
6284 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
6286 if (!mem)
6288 ERR("Failed to allocate memory\n");
6289 return FALSE;
6292 heap->entries = mem;
6293 heap->entries[1].version = 0;
6294 heap->positions = (unsigned int *)(heap->entries + constant_count + 1);
6295 heap->size = 1;
6297 return TRUE;
6300 static void constant_heap_free(struct constant_heap *heap)
6302 HeapFree(GetProcessHeap(), 0, heap->entries);
6305 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
6307 wined3d_rb_alloc,
6308 wined3d_rb_realloc,
6309 wined3d_rb_free,
6310 glsl_program_key_compare,
6313 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
6314 const struct fragment_pipeline *fragment_pipe)
6316 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6317 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
6318 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
6319 gl_info->limits.glsl_ps_float_constants)) + 1;
6320 struct fragment_caps fragment_caps;
6321 void *vertex_priv, *fragment_priv;
6323 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
6325 ERR("Failed to initialize vertex pipe.\n");
6326 HeapFree(GetProcessHeap(), 0, priv);
6327 return E_FAIL;
6330 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
6332 ERR("Failed to initialize fragment pipe.\n");
6333 vertex_pipe->vp_free(device);
6334 HeapFree(GetProcessHeap(), 0, priv);
6335 return E_FAIL;
6338 if (!shader_buffer_init(&priv->shader_buffer))
6340 ERR("Failed to initialize shader buffer.\n");
6341 goto fail;
6344 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
6345 if (!priv->stack)
6347 ERR("Failed to allocate memory.\n");
6348 goto fail;
6351 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
6353 ERR("Failed to initialize vertex shader constant heap\n");
6354 goto fail;
6357 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
6359 ERR("Failed to initialize pixel shader constant heap\n");
6360 goto fail;
6363 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
6365 ERR("Failed to initialize rbtree.\n");
6366 goto fail;
6369 priv->next_constant_version = 1;
6370 priv->vertex_pipe = vertex_pipe;
6371 priv->fragment_pipe = fragment_pipe;
6372 fragment_pipe->get_caps(gl_info, &fragment_caps);
6373 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
6375 device->vertex_priv = vertex_priv;
6376 device->fragment_priv = fragment_priv;
6377 device->shader_priv = priv;
6379 return WINED3D_OK;
6381 fail:
6382 constant_heap_free(&priv->pconst_heap);
6383 constant_heap_free(&priv->vconst_heap);
6384 HeapFree(GetProcessHeap(), 0, priv->stack);
6385 shader_buffer_free(&priv->shader_buffer);
6386 fragment_pipe->free_private(device);
6387 vertex_pipe->vp_free(device);
6388 HeapFree(GetProcessHeap(), 0, priv);
6389 return E_OUTOFMEMORY;
6392 /* Context activation is done by the caller. */
6393 static void shader_glsl_free(struct wined3d_device *device)
6395 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6396 struct shader_glsl_priv *priv = device->shader_priv;
6397 int i;
6399 for (i = 0; i < tex_type_count; ++i)
6401 if (priv->depth_blt_program_full[i])
6403 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_full[i]));
6405 if (priv->depth_blt_program_masked[i])
6407 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_masked[i]));
6411 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
6412 constant_heap_free(&priv->pconst_heap);
6413 constant_heap_free(&priv->vconst_heap);
6414 HeapFree(GetProcessHeap(), 0, priv->stack);
6415 shader_buffer_free(&priv->shader_buffer);
6416 priv->fragment_pipe->free_private(device);
6417 priv->vertex_pipe->vp_free(device);
6419 HeapFree(GetProcessHeap(), 0, device->shader_priv);
6420 device->shader_priv = NULL;
6423 static void shader_glsl_context_destroyed(void *shader_priv, const struct wined3d_context *context) {}
6425 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
6427 UINT shader_model;
6429 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
6430 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
6431 && gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] && gl_info->supported[ARB_DRAW_INSTANCED])
6432 shader_model = 4;
6433 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
6434 * texldd and texldl instructions. */
6435 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
6436 shader_model = 3;
6437 else
6438 shader_model = 2;
6439 TRACE("Shader model %u.\n", shader_model);
6441 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
6442 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
6443 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
6445 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
6446 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
6448 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
6449 * Direct3D minimum requirement.
6451 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
6452 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
6454 * The problem is that the refrast clamps temporary results in the shader to
6455 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
6456 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
6457 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
6458 * offer a way to query this.
6460 caps->ps_1x_max_value = 8.0;
6462 /* Ideally we'd only set caps like sRGB writes here if supported by both
6463 * the shader backend and the fragment pipe, but we can get called before
6464 * shader_glsl_alloc(). */
6465 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
6466 | WINED3D_SHADER_CAP_SRGB_WRITE;
6469 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
6471 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
6473 TRACE("Checking support for fixup:\n");
6474 dump_color_fixup_desc(fixup);
6477 /* We support everything except YUV conversions. */
6478 if (!is_complex_fixup(fixup))
6480 TRACE("[OK]\n");
6481 return TRUE;
6484 TRACE("[FAILED]\n");
6485 return FALSE;
6488 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
6490 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
6491 /* WINED3DSIH_ADD */ shader_glsl_binop,
6492 /* WINED3DSIH_AND */ shader_glsl_binop,
6493 /* WINED3DSIH_BEM */ shader_glsl_bem,
6494 /* WINED3DSIH_BREAK */ shader_glsl_break,
6495 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
6496 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
6497 /* WINED3DSIH_CALL */ shader_glsl_call,
6498 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
6499 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
6500 /* WINED3DSIH_CND */ shader_glsl_cnd,
6501 /* WINED3DSIH_CRS */ shader_glsl_cross,
6502 /* WINED3DSIH_CUT */ shader_glsl_cut,
6503 /* WINED3DSIH_DCL */ shader_glsl_nop,
6504 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
6505 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
6506 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
6507 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
6508 /* WINED3DSIH_DEF */ shader_glsl_nop,
6509 /* WINED3DSIH_DEFB */ shader_glsl_nop,
6510 /* WINED3DSIH_DEFI */ shader_glsl_nop,
6511 /* WINED3DSIH_DIV */ shader_glsl_binop,
6512 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
6513 /* WINED3DSIH_DP3 */ shader_glsl_dot,
6514 /* WINED3DSIH_DP4 */ shader_glsl_dot,
6515 /* WINED3DSIH_DST */ shader_glsl_dst,
6516 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
6517 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
6518 /* WINED3DSIH_ELSE */ shader_glsl_else,
6519 /* WINED3DSIH_EMIT */ shader_glsl_emit,
6520 /* WINED3DSIH_ENDIF */ shader_glsl_end,
6521 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
6522 /* WINED3DSIH_ENDREP */ shader_glsl_end,
6523 /* WINED3DSIH_EQ */ shader_glsl_relop,
6524 /* WINED3DSIH_EXP */ shader_glsl_map2gl,
6525 /* WINED3DSIH_EXPP */ shader_glsl_expp,
6526 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
6527 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
6528 /* WINED3DSIH_GE */ shader_glsl_relop,
6529 /* WINED3DSIH_IADD */ shader_glsl_binop,
6530 /* WINED3DSIH_IEQ */ NULL,
6531 /* WINED3DSIH_IF */ shader_glsl_if,
6532 /* WINED3DSIH_IFC */ shader_glsl_ifc,
6533 /* WINED3DSIH_IGE */ shader_glsl_relop,
6534 /* WINED3DSIH_IMUL */ shader_glsl_imul,
6535 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
6536 /* WINED3DSIH_LABEL */ shader_glsl_label,
6537 /* WINED3DSIH_LD */ NULL,
6538 /* WINED3DSIH_LIT */ shader_glsl_lit,
6539 /* WINED3DSIH_LOG */ shader_glsl_log,
6540 /* WINED3DSIH_LOGP */ shader_glsl_log,
6541 /* WINED3DSIH_LOOP */ shader_glsl_loop,
6542 /* WINED3DSIH_LRP */ shader_glsl_lrp,
6543 /* WINED3DSIH_LT */ shader_glsl_relop,
6544 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
6545 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
6546 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
6547 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
6548 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
6549 /* WINED3DSIH_MAD */ shader_glsl_mad,
6550 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
6551 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
6552 /* WINED3DSIH_MOV */ shader_glsl_mov,
6553 /* WINED3DSIH_MOVA */ shader_glsl_mov,
6554 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
6555 /* WINED3DSIH_MUL */ shader_glsl_binop,
6556 /* WINED3DSIH_NOP */ shader_glsl_nop,
6557 /* WINED3DSIH_NRM */ shader_glsl_nrm,
6558 /* WINED3DSIH_PHASE */ shader_glsl_nop,
6559 /* WINED3DSIH_POW */ shader_glsl_pow,
6560 /* WINED3DSIH_RCP */ shader_glsl_rcp,
6561 /* WINED3DSIH_REP */ shader_glsl_rep,
6562 /* WINED3DSIH_RET */ shader_glsl_ret,
6563 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
6564 /* WINED3DSIH_RSQ */ shader_glsl_rsq,
6565 /* WINED3DSIH_SAMPLE */ NULL,
6566 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
6567 /* WINED3DSIH_SAMPLE_LOD */ NULL,
6568 /* WINED3DSIH_SETP */ NULL,
6569 /* WINED3DSIH_SGE */ shader_glsl_compare,
6570 /* WINED3DSIH_SGN */ shader_glsl_sgn,
6571 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
6572 /* WINED3DSIH_SLT */ shader_glsl_compare,
6573 /* WINED3DSIH_SQRT */ NULL,
6574 /* WINED3DSIH_SUB */ shader_glsl_binop,
6575 /* WINED3DSIH_TEX */ shader_glsl_tex,
6576 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
6577 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
6578 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
6579 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
6580 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
6581 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
6582 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
6583 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
6584 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
6585 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
6586 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
6587 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
6588 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
6589 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
6590 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
6591 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
6592 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
6593 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
6594 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
6595 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
6596 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
6597 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
6598 /* WINED3DSIH_USHR */ shader_glsl_binop,
6599 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
6600 /* WINED3DSIH_XOR */ shader_glsl_binop,
6603 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
6604 SHADER_HANDLER hw_fct;
6606 /* Select handler */
6607 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
6609 /* Unhandled opcode */
6610 if (!hw_fct)
6612 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
6613 return;
6615 hw_fct(ins);
6617 shader_glsl_add_instruction_modifiers(ins);
6620 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
6622 struct shader_glsl_priv *priv = shader_priv;
6624 return priv->ffp_proj_control;
6627 const struct wined3d_shader_backend_ops glsl_shader_backend =
6629 shader_glsl_handle_instruction,
6630 shader_glsl_select,
6631 shader_glsl_disable,
6632 shader_glsl_select_depth_blt,
6633 shader_glsl_deselect_depth_blt,
6634 shader_glsl_update_float_vertex_constants,
6635 shader_glsl_update_float_pixel_constants,
6636 shader_glsl_load_constants,
6637 shader_glsl_load_np2fixup_constants,
6638 shader_glsl_destroy,
6639 shader_glsl_alloc,
6640 shader_glsl_free,
6641 shader_glsl_context_destroyed,
6642 shader_glsl_get_caps,
6643 shader_glsl_color_fixup_supported,
6644 shader_glsl_has_ffp_proj_control,
6647 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
6649 if (enable)
6650 gl_info->gl_ops.gl.p_glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6651 else
6652 gl_info->gl_ops.gl.p_glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6653 checkGLcall("GL_VERTEX_PROGRAM_POINT_SIZE_ARB");
6656 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
6658 caps->max_active_lights = gl_info->limits.lights;
6659 caps->max_vertex_blend_matrices = 0;
6660 caps->max_vertex_blend_matrix_index = 0;
6661 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
6662 | WINED3DVTXPCAPS_MATERIALSOURCE7
6663 | WINED3DVTXPCAPS_VERTEXFOG
6664 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
6665 | WINED3DVTXPCAPS_POSITIONALLIGHTS
6666 | WINED3DVTXPCAPS_LOCALVIEWER
6667 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
6668 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
6669 caps->max_user_clip_planes = gl_info->limits.clipplanes;
6670 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
6673 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
6675 struct shader_glsl_priv *priv;
6677 if (shader_backend == &glsl_shader_backend)
6679 priv = shader_priv;
6681 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
6683 ERR("Failed to initialize rbtree.\n");
6684 return NULL;
6687 return priv;
6690 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
6692 return NULL;
6695 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
6697 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
6698 struct glsl_ffp_vertex_shader, desc.entry);
6699 struct glsl_shader_prog_link *program, *program2;
6700 struct glsl_ffp_destroy_ctx *ctx = context;
6702 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
6703 struct glsl_shader_prog_link, vs.shader_entry)
6705 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
6707 ctx->gl_info->gl_ops.ext.p_glDeleteObjectARB(shader->id);
6708 HeapFree(GetProcessHeap(), 0, shader);
6711 /* Context activation is done by the caller. */
6712 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
6714 struct shader_glsl_priv *priv = device->vertex_priv;
6715 struct glsl_ffp_destroy_ctx ctx;
6717 ctx.priv = priv;
6718 ctx.gl_info = &device->adapter->gl_info;
6719 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
6722 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
6723 const struct wined3d_state *state, DWORD state_id)
6725 context->select_shader = 1;
6728 static void glsl_vertex_pipe_light(struct wined3d_context *context,
6729 const struct wined3d_state *state, DWORD state_id)
6731 light(context, state, state_id);
6732 context->select_shader = 1;
6735 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
6737 {STATE_VDECL, {STATE_VDECL, vertexdeclaration }, WINED3D_GL_EXT_NONE },
6738 {STATE_VSHADER, {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6739 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
6740 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), state_specularenable }, WINED3D_GL_EXT_NONE },
6741 /* Clip planes */
6742 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
6743 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
6744 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
6745 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
6746 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
6747 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
6748 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
6749 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
6750 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
6751 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
6752 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
6753 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
6754 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
6755 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
6756 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
6757 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
6758 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
6759 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
6760 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
6761 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
6762 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
6763 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
6764 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
6765 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
6766 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
6767 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
6768 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
6769 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
6770 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
6771 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
6772 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
6773 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
6774 /* Lights */
6775 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
6776 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
6777 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
6778 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
6779 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
6780 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
6781 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
6782 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
6783 /* Viewport */
6784 {STATE_VIEWPORT, {STATE_VIEWPORT, viewport_vertexpart }, WINED3D_GL_EXT_NONE },
6785 /* Transform states */
6786 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), transform_view }, WINED3D_GL_EXT_NONE },
6787 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), transform_projection }, WINED3D_GL_EXT_NONE },
6788 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6789 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6790 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6791 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6792 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6793 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6794 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6795 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6796 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), transform_world }, WINED3D_GL_EXT_NONE },
6797 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6798 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6799 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6800 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6801 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6802 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6803 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6804 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6805 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6806 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6807 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6808 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6809 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6810 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6811 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6812 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6813 /* Fog */
6814 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
6815 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6816 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6817 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6818 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
6819 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
6820 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6821 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), state_ambient }, WINED3D_GL_EXT_NONE },
6822 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6823 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6824 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6825 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6826 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6827 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6828 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6829 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6830 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
6831 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_arb }, ARB_POINT_PARAMETERS },
6832 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_ext }, EXT_POINT_PARAMETERS },
6833 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_w }, WINED3D_GL_EXT_NONE },
6834 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
6835 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
6836 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), state_pscale }, WINED3D_GL_EXT_NONE },
6837 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
6838 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
6839 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
6840 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, ARB_POINT_PARAMETERS },
6841 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, EXT_POINT_PARAMETERS },
6842 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
6843 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6844 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6845 /* Samplers for NP2 texture matrix adjustions. They are not needed if
6846 * GL_ARB_texture_non_power_of_two is supported, so register a NULL state
6847 * handler in that case to get the vertex part of sampler() skipped (VTF
6848 * is handled in the misc states). Otherwise, register
6849 * sampler_texmatrix(), which takes care of updating the texture matrix. */
6850 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6851 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6852 {STATE_SAMPLER(0), {STATE_SAMPLER(0), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6853 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6854 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6855 {STATE_SAMPLER(1), {STATE_SAMPLER(1), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6856 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6857 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6858 {STATE_SAMPLER(2), {STATE_SAMPLER(2), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6859 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6860 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6861 {STATE_SAMPLER(3), {STATE_SAMPLER(3), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6862 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6863 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6864 {STATE_SAMPLER(4), {STATE_SAMPLER(4), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6865 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6866 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6867 {STATE_SAMPLER(5), {STATE_SAMPLER(5), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6868 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6869 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6870 {STATE_SAMPLER(6), {STATE_SAMPLER(6), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6871 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6872 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6873 {STATE_SAMPLER(7), {STATE_SAMPLER(7), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6874 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
6877 /* TODO:
6878 * - This currently depends on GL fixed function functions to set things
6879 * like light parameters. Ideally we'd use regular uniforms for that.
6880 * - In part because of the previous point, much of this is modelled after
6881 * GL fixed function, and has much of the same limitations. For example,
6882 * D3D spot lights are slightly different from GL spot lights.
6883 * - We can now implement drawing transformed vertices using the GLSL pipe,
6884 * instead of using the immediate mode fallback.
6885 * - Similarly, we don't need the fallback for certain combinations of
6886 * material sources anymore.
6887 * - Implement vertex blending and vertex tweening.
6888 * - Handle WINED3D_TSS_TEXCOORD_INDEX in the shader, instead of duplicating
6889 * attribute arrays in load_tex_coords().
6890 * - Per-vertex point sizes. */
6891 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
6893 glsl_vertex_pipe_vp_enable,
6894 glsl_vertex_pipe_vp_get_caps,
6895 glsl_vertex_pipe_vp_alloc,
6896 glsl_vertex_pipe_vp_free,
6897 glsl_vertex_pipe_vp_states,
6900 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
6902 /* Nothing to do. */
6905 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
6907 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
6908 | WINED3D_FRAGMENT_CAP_SRGB_WRITE;
6909 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP;
6910 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
6911 | WINED3DTEXOPCAPS_SELECTARG1
6912 | WINED3DTEXOPCAPS_SELECTARG2
6913 | WINED3DTEXOPCAPS_MODULATE4X
6914 | WINED3DTEXOPCAPS_MODULATE2X
6915 | WINED3DTEXOPCAPS_MODULATE
6916 | WINED3DTEXOPCAPS_ADDSIGNED2X
6917 | WINED3DTEXOPCAPS_ADDSIGNED
6918 | WINED3DTEXOPCAPS_ADD
6919 | WINED3DTEXOPCAPS_SUBTRACT
6920 | WINED3DTEXOPCAPS_ADDSMOOTH
6921 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
6922 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
6923 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
6924 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
6925 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
6926 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
6927 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
6928 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
6929 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
6930 | WINED3DTEXOPCAPS_DOTPRODUCT3
6931 | WINED3DTEXOPCAPS_MULTIPLYADD
6932 | WINED3DTEXOPCAPS_LERP
6933 | WINED3DTEXOPCAPS_BUMPENVMAP
6934 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
6935 caps->MaxTextureBlendStages = 8;
6936 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
6939 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
6941 struct shader_glsl_priv *priv;
6943 if (shader_backend == &glsl_shader_backend)
6945 priv = shader_priv;
6947 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
6949 ERR("Failed to initialize rbtree.\n");
6950 return NULL;
6953 return priv;
6956 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
6958 return NULL;
6961 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
6963 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
6964 struct glsl_ffp_fragment_shader, entry.entry);
6965 struct glsl_shader_prog_link *program, *program2;
6966 struct glsl_ffp_destroy_ctx *ctx = context;
6968 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
6969 struct glsl_shader_prog_link, ps.shader_entry)
6971 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
6973 ctx->gl_info->gl_ops.ext.p_glDeleteObjectARB(shader->id);
6974 HeapFree(GetProcessHeap(), 0, shader);
6977 /* Context activation is done by the caller. */
6978 static void glsl_fragment_pipe_free(struct wined3d_device *device)
6980 struct shader_glsl_priv *priv = device->fragment_priv;
6981 struct glsl_ffp_destroy_ctx ctx;
6983 ctx.priv = priv;
6984 ctx.gl_info = &device->adapter->gl_info;
6985 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
6988 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
6989 const struct wined3d_state *state, DWORD state_id)
6991 context->last_was_pshader = use_ps(state);
6993 context->select_shader = 1;
6994 context->load_constants = 1;
6997 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
6998 const struct wined3d_state *state, DWORD state_id)
7000 BOOL use_vshader = use_vs(state);
7001 enum fogsource new_source;
7003 context->select_shader = 1;
7004 context->load_constants = 1;
7006 if (!state->render_states[WINED3D_RS_FOGENABLE])
7007 return;
7009 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
7011 if (use_vshader)
7012 new_source = FOGSOURCE_VS;
7013 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->last_was_rhw)
7014 new_source = FOGSOURCE_COORD;
7015 else
7016 new_source = FOGSOURCE_FFP;
7018 else
7020 new_source = FOGSOURCE_FFP;
7023 if (new_source != context->fog_source)
7025 context->fog_source = new_source;
7026 state_fogstartend(context, state, STATE_RENDER(WINED3D_RS_FOGSTART));
7030 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
7031 const struct wined3d_state *state, DWORD state_id)
7033 context->select_shader = 1;
7034 context->load_constants = 1;
7037 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
7038 const struct wined3d_state *state, DWORD state_id)
7040 context->load_constants = 1;
7043 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
7045 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7046 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7047 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7048 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7049 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7050 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7051 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7052 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7053 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7054 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7055 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7056 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7057 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7058 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7059 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7060 {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(0, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
7061 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7062 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7063 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7064 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7065 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7066 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7067 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7068 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7069 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7070 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7071 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7072 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7073 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7074 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7075 {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(1, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
7076 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7077 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7078 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7079 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7080 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7081 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7082 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7083 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7084 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7085 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7086 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7087 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7088 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7089 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7090 {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(2, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
7091 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7092 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7093 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7094 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7095 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7096 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7097 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7098 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7099 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7100 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7101 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7102 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7103 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7104 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7105 {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(3, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
7106 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7107 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7108 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7109 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7110 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7111 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7112 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7113 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7114 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7115 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7116 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7117 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7118 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7119 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7120 {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(4, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
7121 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7122 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7123 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7124 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7125 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7126 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7127 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7128 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7129 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7130 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7131 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7132 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7133 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7134 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7135 {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(5, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
7136 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7137 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7138 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7139 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7140 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7141 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7142 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7143 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7144 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7145 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7146 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7147 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7148 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7149 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7150 {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(6, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
7151 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7152 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7153 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7154 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7155 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7156 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7157 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7158 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7159 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7160 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7161 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT01), {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7162 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT10), {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7163 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT11), {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_MAT00), NULL }, WINED3D_GL_EXT_NONE },
7164 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_LSCALE), {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_LSCALE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7165 {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_LOFFSET), {STATE_TEXTURESTAGE(7, WINED3D_TSS_BUMPENV_LSCALE), NULL }, WINED3D_GL_EXT_NONE },
7166 {STATE_PIXELSHADER, {STATE_PIXELSHADER, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
7167 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
7168 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7169 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7170 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
7171 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
7172 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
7173 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7174 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
7175 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
7176 {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 },
7177 {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 },
7178 {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 },
7179 {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 },
7180 {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 },
7181 {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 },
7182 {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 },
7183 {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 },
7184 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7185 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
7188 const struct fragment_pipeline glsl_fragment_pipe =
7190 glsl_fragment_pipe_enable,
7191 glsl_fragment_pipe_get_caps,
7192 glsl_fragment_pipe_alloc,
7193 glsl_fragment_pipe_free,
7194 shader_glsl_color_fixup_supported,
7195 glsl_fragment_pipe_state_template,