wined3d: Don't use the builtin FFP uniform for the projection matrix.
[wine.git] / dlls / wined3d / glsl_shader.c
blobe4ddbe62876cb38450f8ef8617c5539b6d6f8395
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>
37 #ifdef HAVE_FLOAT_H
38 # include <float.h>
39 #endif
41 #include "wined3d_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
44 WINE_DECLARE_DEBUG_CHANNEL(d3d);
45 WINE_DECLARE_DEBUG_CHANNEL(winediag);
47 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x1
48 #define WINED3D_GLSL_SAMPLE_NPOT 0x2
49 #define WINED3D_GLSL_SAMPLE_LOD 0x4
50 #define WINED3D_GLSL_SAMPLE_GRAD 0x8
52 struct glsl_dst_param
54 char reg_name[150];
55 char mask_str[6];
58 struct glsl_src_param
60 char reg_name[150];
61 char param_str[200];
64 struct glsl_sample_function
66 const char *name;
67 DWORD coord_mask;
68 enum wined3d_data_type data_type;
71 enum heap_node_op
73 HEAP_NODE_TRAVERSE_LEFT,
74 HEAP_NODE_TRAVERSE_RIGHT,
75 HEAP_NODE_POP,
78 struct constant_entry
80 unsigned int idx;
81 unsigned int version;
84 struct constant_heap
86 struct constant_entry *entries;
87 BOOL *contained;
88 unsigned int *positions;
89 unsigned int size;
92 /* GLSL shader private data */
93 struct shader_glsl_priv {
94 struct wined3d_shader_buffer shader_buffer;
95 struct wine_rb_tree program_lookup;
96 struct constant_heap vconst_heap;
97 struct constant_heap pconst_heap;
98 unsigned char *stack;
99 GLuint depth_blt_program_full[tex_type_count];
100 GLuint depth_blt_program_masked[tex_type_count];
101 UINT next_constant_version;
103 const struct wined3d_vertex_pipe_ops *vertex_pipe;
104 const struct fragment_pipeline *fragment_pipe;
105 struct wine_rb_tree ffp_vertex_shaders;
106 struct wine_rb_tree ffp_fragment_shaders;
107 BOOL ffp_proj_control;
110 struct glsl_vs_program
112 struct list shader_entry;
113 GLuint id;
114 GLenum vertex_color_clamp;
115 GLint *uniform_f_locations;
116 GLint uniform_i_locations[MAX_CONST_I];
117 GLint uniform_b_locations[MAX_CONST_B];
118 GLint pos_fixup_location;
120 GLint modelview_matrix_location;
121 GLint projection_matrix_location;
122 GLint normal_matrix_location;
125 struct glsl_gs_program
127 struct list shader_entry;
128 GLuint id;
131 struct glsl_ps_program
133 struct list shader_entry;
134 GLuint id;
135 GLint *uniform_f_locations;
136 GLint uniform_i_locations[MAX_CONST_I];
137 GLint uniform_b_locations[MAX_CONST_B];
138 GLint bumpenv_mat_location[MAX_TEXTURES];
139 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
140 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
141 GLint tss_constant_location[MAX_TEXTURES];
142 GLint tex_factor_location;
143 GLint specular_enable_location;
144 GLint ycorrection_location;
145 GLint np2_fixup_location;
146 const struct ps_np2fixup_info *np2_fixup_info;
149 /* Struct to maintain data about a linked GLSL program */
150 struct glsl_shader_prog_link
152 struct wine_rb_entry program_lookup_entry;
153 struct glsl_vs_program vs;
154 struct glsl_gs_program gs;
155 struct glsl_ps_program ps;
156 GLuint id;
157 DWORD constant_update_mask;
158 UINT constant_version;
161 struct glsl_program_key
163 GLuint vs_id;
164 GLuint gs_id;
165 GLuint ps_id;
168 struct shader_glsl_ctx_priv {
169 const struct vs_compile_args *cur_vs_args;
170 const struct ps_compile_args *cur_ps_args;
171 struct ps_np2fixup_info *cur_np2fixup_info;
174 struct glsl_context_data
176 struct glsl_shader_prog_link *glsl_program;
179 struct glsl_ps_compiled_shader
181 struct ps_compile_args args;
182 struct ps_np2fixup_info np2fixup;
183 GLuint id;
186 struct glsl_vs_compiled_shader
188 struct vs_compile_args args;
189 GLuint id;
192 struct glsl_gs_compiled_shader
194 GLuint id;
197 struct glsl_shader_private
199 union
201 struct glsl_vs_compiled_shader *vs;
202 struct glsl_gs_compiled_shader *gs;
203 struct glsl_ps_compiled_shader *ps;
204 } gl_shaders;
205 UINT num_gl_shaders, shader_array_size;
208 struct glsl_ffp_vertex_shader
210 struct wined3d_ffp_vs_desc desc;
211 GLuint id;
212 struct list linked_programs;
215 struct glsl_ffp_fragment_shader
217 struct ffp_frag_desc entry;
218 GLuint id;
219 struct list linked_programs;
222 struct glsl_ffp_destroy_ctx
224 struct shader_glsl_priv *priv;
225 const struct wined3d_gl_info *gl_info;
228 static const char *debug_gl_shader_type(GLenum type)
230 switch (type)
232 #define WINED3D_TO_STR(u) case u: return #u
233 WINED3D_TO_STR(GL_VERTEX_SHADER);
234 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
235 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
236 #undef WINED3D_TO_STR
237 default:
238 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
242 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
244 switch (type)
246 case WINED3D_SHADER_TYPE_VERTEX:
247 return "vs";
249 case WINED3D_SHADER_TYPE_GEOMETRY:
250 return "gs";
252 case WINED3D_SHADER_TYPE_PIXEL:
253 return "ps";
255 default:
256 FIXME("Unhandled shader type %#x.\n", type);
257 return "unknown";
261 static void shader_glsl_append_imm_vec4(struct wined3d_shader_buffer *buffer, const float *values)
263 char str[4][17];
265 wined3d_ftoa(values[0], str[0]);
266 wined3d_ftoa(values[1], str[1]);
267 wined3d_ftoa(values[2], str[2]);
268 wined3d_ftoa(values[3], str[3]);
269 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
272 /* Extract a line from the info log.
273 * Note that this modifies the source string. */
274 static char *get_info_log_line(char **ptr)
276 char *p, *q;
278 p = *ptr;
279 if (!(q = strstr(p, "\n")))
281 if (!*p) return NULL;
282 *ptr += strlen(p);
283 return p;
285 *q = '\0';
286 *ptr = q + 1;
288 return p;
291 /* Context activation is done by the caller. */
292 static void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
294 int length = 0;
295 char *log;
297 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
298 return;
300 if (program)
301 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
302 else
303 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
305 /* A size of 1 is just a null-terminated string, so the log should be bigger than
306 * that if there are errors. */
307 if (length > 1)
309 char *ptr, *line;
311 log = HeapAlloc(GetProcessHeap(), 0, length);
312 /* The info log is supposed to be zero-terminated, but at least some
313 * versions of fglrx don't terminate the string properly. The reported
314 * length does include the terminator, so explicitly set it to zero
315 * here. */
316 log[length - 1] = 0;
317 if (program)
318 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
319 else
320 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
322 ptr = log;
323 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
325 WARN("Info log received from GLSL shader #%u:\n", id);
326 while ((line = get_info_log_line(&ptr))) WARN(" %s\n", line);
328 else
330 FIXME("Info log received from GLSL shader #%u:\n", id);
331 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
333 HeapFree(GetProcessHeap(), 0, log);
337 /* Context activation is done by the caller. */
338 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
340 TRACE("Compiling shader object %u.\n", shader);
341 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
342 checkGLcall("glShaderSource");
343 GL_EXTCALL(glCompileShader(shader));
344 checkGLcall("glCompileShader");
345 print_glsl_info_log(gl_info, shader, FALSE);
348 /* Context activation is done by the caller. */
349 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
351 GLint i, shader_count, source_size = -1;
352 GLuint *shaders;
353 char *source = NULL;
355 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
356 shaders = HeapAlloc(GetProcessHeap(), 0, shader_count * sizeof(*shaders));
357 if (!shaders)
359 ERR("Failed to allocate shader array memory.\n");
360 return;
363 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
364 for (i = 0; i < shader_count; ++i)
366 char *ptr, *line;
367 GLint tmp;
369 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
371 if (source_size < tmp)
373 HeapFree(GetProcessHeap(), 0, source);
375 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
376 if (!source)
378 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
379 HeapFree(GetProcessHeap(), 0, shaders);
380 return;
382 source_size = tmp;
385 FIXME("Shader %u:\n", shaders[i]);
386 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
387 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
388 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
389 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
390 FIXME("\n");
392 ptr = source;
393 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
394 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
395 FIXME("\n");
398 HeapFree(GetProcessHeap(), 0, source);
399 HeapFree(GetProcessHeap(), 0, shaders);
402 /* Context activation is done by the caller. */
403 static void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
405 GLint tmp;
407 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
408 return;
410 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
411 if (!tmp)
413 FIXME("Program %u link status invalid.\n", program);
414 shader_glsl_dump_program_source(gl_info, program);
417 print_glsl_info_log(gl_info, program, TRUE);
420 /* Context activation is done by the caller. */
421 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
422 const DWORD *tex_unit_map, GLuint program_id)
424 unsigned int mapped_unit;
425 char sampler_name[20];
426 const char *prefix;
427 unsigned int i, j;
428 GLint name_loc;
430 static const struct
432 enum wined3d_shader_type type;
433 unsigned int base_idx;
434 unsigned int count;
436 sampler_info[] =
438 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
439 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
442 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
444 prefix = shader_glsl_get_prefix(sampler_info[i].type);
446 for (j = 0; j < sampler_info[i].count; ++j)
448 snprintf(sampler_name, sizeof(sampler_name), "%s_sampler%u", prefix, j);
449 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name));
450 if (name_loc == -1)
451 continue;
453 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
454 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
456 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name, mapped_unit);
457 continue;
460 TRACE("Loading sampler %s on unit %u.\n", sampler_name, mapped_unit);
461 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
464 checkGLcall("glUniform1i");
467 /* Context activation is done by the caller. */
468 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
469 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
471 unsigned int start = ~0U, end = 0;
472 int stack_idx = 0;
473 unsigned int heap_idx = 1;
474 unsigned int idx;
476 if (heap->entries[heap_idx].version <= version) return;
478 idx = heap->entries[heap_idx].idx;
479 if (constant_locations[idx] != -1)
480 start = end = idx;
481 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
483 while (stack_idx >= 0)
485 /* Note that we fall through to the next case statement. */
486 switch(stack[stack_idx])
488 case HEAP_NODE_TRAVERSE_LEFT:
490 unsigned int left_idx = heap_idx << 1;
491 if (left_idx < heap->size && heap->entries[left_idx].version > version)
493 heap_idx = left_idx;
494 idx = heap->entries[heap_idx].idx;
495 if (constant_locations[idx] != -1)
497 if (start > idx)
498 start = idx;
499 if (end < idx)
500 end = idx;
503 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
504 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
505 break;
509 case HEAP_NODE_TRAVERSE_RIGHT:
511 unsigned int right_idx = (heap_idx << 1) + 1;
512 if (right_idx < heap->size && heap->entries[right_idx].version > version)
514 heap_idx = right_idx;
515 idx = heap->entries[heap_idx].idx;
516 if (constant_locations[idx] != -1)
518 if (start > idx)
519 start = idx;
520 if (end < idx)
521 end = idx;
524 stack[stack_idx++] = HEAP_NODE_POP;
525 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
526 break;
530 case HEAP_NODE_POP:
531 heap_idx >>= 1;
532 --stack_idx;
533 break;
536 if (start <= end)
537 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start * 4]));
538 checkGLcall("walk_constant_heap()");
541 /* Context activation is done by the caller. */
542 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
544 GLfloat clamped_constant[4];
546 if (location == -1) return;
548 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
549 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
550 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
551 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
553 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
556 /* Context activation is done by the caller. */
557 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
558 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
560 int stack_idx = 0;
561 unsigned int heap_idx = 1;
562 unsigned int idx;
564 if (heap->entries[heap_idx].version <= version) return;
566 idx = heap->entries[heap_idx].idx;
567 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
568 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
570 while (stack_idx >= 0)
572 /* Note that we fall through to the next case statement. */
573 switch(stack[stack_idx])
575 case HEAP_NODE_TRAVERSE_LEFT:
577 unsigned int left_idx = heap_idx << 1;
578 if (left_idx < heap->size && heap->entries[left_idx].version > version)
580 heap_idx = left_idx;
581 idx = heap->entries[heap_idx].idx;
582 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
584 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
585 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
586 break;
590 case HEAP_NODE_TRAVERSE_RIGHT:
592 unsigned int right_idx = (heap_idx << 1) + 1;
593 if (right_idx < heap->size && heap->entries[right_idx].version > version)
595 heap_idx = right_idx;
596 idx = heap->entries[heap_idx].idx;
597 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
599 stack[stack_idx++] = HEAP_NODE_POP;
600 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
601 break;
605 case HEAP_NODE_POP:
606 heap_idx >>= 1;
607 --stack_idx;
608 break;
611 checkGLcall("walk_constant_heap_clamped()");
614 /* Context activation is done by the caller. */
615 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
616 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
617 unsigned char *stack, UINT version)
619 const struct wined3d_shader_lconst *lconst;
621 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
622 if (shader->reg_maps.shader_version.major == 1
623 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
624 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
625 else
626 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
628 if (!shader->load_local_constsF)
630 TRACE("No need to load local float constants for this shader\n");
631 return;
634 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
635 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
637 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
639 checkGLcall("glUniform4fv()");
642 /* Context activation is done by the caller. */
643 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
644 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
646 unsigned int i;
647 struct list* ptr;
649 for (i = 0; constants_set; constants_set >>= 1, ++i)
651 if (!(constants_set & 1)) continue;
653 /* We found this uniform name in the program - go ahead and send the data */
654 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i * 4]));
657 /* Load immediate constants */
658 ptr = list_head(&shader->constantsI);
659 while (ptr)
661 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
662 unsigned int idx = lconst->idx;
663 const GLint *values = (const GLint *)lconst->value;
665 /* We found this uniform name in the program - go ahead and send the data */
666 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
667 ptr = list_next(&shader->constantsI, ptr);
669 checkGLcall("glUniform4iv()");
672 /* Context activation is done by the caller. */
673 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
674 const GLint locations[MAX_CONST_B], const BOOL *constants, WORD constants_set)
676 unsigned int i;
677 struct list* ptr;
679 for (i = 0; constants_set; constants_set >>= 1, ++i)
681 if (!(constants_set & 1)) continue;
683 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
686 /* Load immediate constants */
687 ptr = list_head(&shader->constantsB);
688 while (ptr)
690 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
691 unsigned int idx = lconst->idx;
692 const GLint *values = (const GLint *)lconst->value;
694 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
695 ptr = list_next(&shader->constantsB, ptr);
697 checkGLcall("glUniform1iv()");
700 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
702 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
705 /* Context activation is done by the caller (state handler). */
706 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
707 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
709 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
710 UINT fixup = ps->np2_fixup_info->active;
711 UINT i;
713 for (i = 0; fixup; fixup >>= 1, ++i)
715 const struct wined3d_texture *tex = state->textures[i];
716 unsigned char idx = ps->np2_fixup_info->idx[i];
717 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
719 if (!tex)
721 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
722 continue;
725 if (idx % 2)
727 tex_dim[2] = tex->pow2_matrix[0];
728 tex_dim[3] = tex->pow2_matrix[5];
730 else
732 tex_dim[0] = tex->pow2_matrix[0];
733 tex_dim[1] = tex->pow2_matrix[5];
737 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
740 /* Taken and adapted from Mesa. */
741 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
743 float pos, neg, t, det;
744 struct wined3d_matrix temp;
746 /* Calculate the determinant of upper left 3x3 submatrix and
747 * determine if the matrix is singular. */
748 pos = neg = 0.0f;
749 t = in->_11 * in->_22 * in->_33;
750 if (t >= 0.0f)
751 pos += t;
752 else
753 neg += t;
755 t = in->_21 * in->_32 * in->_13;
756 if (t >= 0.0f)
757 pos += t;
758 else
759 neg += t;
760 t = in->_31 * in->_12 * in->_23;
761 if (t >= 0.0f)
762 pos += t;
763 else
764 neg += t;
766 t = -in->_31 * in->_22 * in->_13;
767 if (t >= 0.0f)
768 pos += t;
769 else
770 neg += t;
771 t = -in->_21 * in->_12 * in->_33;
772 if (t >= 0.0f)
773 pos += t;
774 else
775 neg += t;
777 t = -in->_11 * in->_32 * in->_23;
778 if (t >= 0.0f)
779 pos += t;
780 else
781 neg += t;
783 det = pos + neg;
785 if (fabsf(det) < 1e-25f)
786 return FALSE;
788 det = 1.0f / det;
789 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
790 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
791 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
792 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
793 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
794 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
795 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
796 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
797 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
799 *out = temp;
800 return TRUE;
803 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
804 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
806 const struct wined3d_gl_info *gl_info = context->gl_info;
807 float mat[3 * 3];
808 struct wined3d_matrix mv;
809 unsigned int i, j;
811 /* gl_NormalMatrix is defined in the OpenGL spec as "transpose of the
812 * inverse of the upper leftmost 3x3 of gl_ModelViewMatrix" and that
813 * seems to be correct for D3D too. */
814 get_modelview_matrix(context, state, &mv);
815 invert_matrix_3d(&mv, &mv);
816 /* Tests show that singular modelview matrices are used unchanged as normal
817 * matrices on D3D3 and older. There seems to be no clearly consistent
818 * behavior on newer D3D versions so always follow older ddraw behavior. */
819 for (i = 0; i < 3; ++i)
820 for (j = 0; j < 3; ++j)
821 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
823 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
824 checkGLcall("glUniformMatrix3fv");
827 /* Context activation is done by the caller (state handler). */
828 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
829 const struct wined3d_state *state)
831 const struct glsl_context_data *ctx_data = context->shader_backend_data;
832 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
833 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
834 const struct wined3d_gl_info *gl_info = context->gl_info;
835 struct shader_glsl_priv *priv = shader_priv;
836 float position_fixup[4];
837 DWORD update_mask = 0;
839 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
840 UINT constant_version;
841 int i;
843 if (!prog) {
844 /* No GLSL program set - nothing to do. */
845 return;
847 constant_version = prog->constant_version;
848 update_mask = context->constant_update_mask & prog->constant_update_mask;
850 if (update_mask & WINED3D_SHADER_CONST_VS_F)
851 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
852 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
854 if (update_mask & WINED3D_SHADER_CONST_VS_I)
855 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
856 vshader->reg_maps.integer_constants);
858 if (update_mask & WINED3D_SHADER_CONST_VS_B)
859 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
860 vshader->reg_maps.boolean_constants);
862 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
864 shader_get_position_fixup(context, state, position_fixup);
865 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
866 checkGLcall("glUniform4fv");
869 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
871 struct wined3d_matrix mat;
873 get_modelview_matrix(context, state, &mat);
874 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location, 1, FALSE, &mat._11));
875 checkGLcall("glUniformMatrix4fv");
877 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
880 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
882 struct wined3d_matrix projection;
884 get_projection_matrix(context, state, &projection);
885 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
886 checkGLcall("glUniformMatrix4fv");
889 if (update_mask & WINED3D_SHADER_CONST_PS_F)
890 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
891 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
893 if (update_mask & WINED3D_SHADER_CONST_PS_I)
894 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
895 pshader->reg_maps.integer_constants);
897 if (update_mask & WINED3D_SHADER_CONST_PS_B)
898 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
899 pshader->reg_maps.boolean_constants);
901 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
903 for (i = 0; i < MAX_TEXTURES; ++i)
905 if (prog->ps.bumpenv_mat_location[i] == -1)
906 continue;
908 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
909 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
911 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
913 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
914 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
915 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
916 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
920 checkGLcall("bump env uniforms");
923 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
925 float correction_params[4];
927 if (context->render_offscreen)
929 correction_params[0] = 0.0f;
930 correction_params[1] = 1.0f;
931 } else {
932 /* position is window relative, not viewport relative */
933 correction_params[0] = (float) context->current_rt->resource.height;
934 correction_params[1] = -1.0f;
936 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, correction_params));
939 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
940 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
942 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
944 float col[4];
946 if (prog->ps.tex_factor_location != -1)
948 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_TEXTUREFACTOR], col);
949 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, col));
952 if (state->render_states[WINED3D_RS_SPECULARENABLE])
953 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
954 else
955 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
957 for (i = 0; i < MAX_TEXTURES; ++i)
959 if (prog->ps.tss_constant_location[i] == -1)
960 continue;
962 D3DCOLORTOGLFLOAT4(state->texture_states[i][WINED3D_TSS_CONSTANT], col);
963 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, col));
966 checkGLcall("fixed function uniforms");
969 if (priv->next_constant_version == UINT_MAX)
971 TRACE("Max constant version reached, resetting to 0.\n");
972 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
973 priv->next_constant_version = 1;
975 else
977 prog->constant_version = priv->next_constant_version++;
981 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
983 struct constant_entry *entries = heap->entries;
984 unsigned int *positions = heap->positions;
985 unsigned int heap_idx, parent_idx;
987 if (!heap->contained[idx])
989 heap_idx = heap->size++;
990 heap->contained[idx] = TRUE;
992 else
994 heap_idx = positions[idx];
997 while (heap_idx > 1)
999 parent_idx = heap_idx >> 1;
1001 if (new_version <= entries[parent_idx].version) break;
1003 entries[heap_idx] = entries[parent_idx];
1004 positions[entries[parent_idx].idx] = heap_idx;
1005 heap_idx = parent_idx;
1008 entries[heap_idx].version = new_version;
1009 entries[heap_idx].idx = idx;
1010 positions[idx] = heap_idx;
1013 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1015 struct shader_glsl_priv *priv = device->shader_priv;
1016 struct constant_heap *heap = &priv->vconst_heap;
1017 UINT i;
1019 for (i = start; i < count + start; ++i)
1021 update_heap_entry(heap, i, priv->next_constant_version);
1024 for (i = 0; i < device->context_count; ++i)
1026 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
1030 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1032 struct shader_glsl_priv *priv = device->shader_priv;
1033 struct constant_heap *heap = &priv->pconst_heap;
1034 UINT i;
1036 for (i = start; i < count + start; ++i)
1038 update_heap_entry(heap, i, priv->next_constant_version);
1041 for (i = 0; i < device->context_count; ++i)
1043 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
1047 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1049 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1050 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1051 if(shader_major > 3) return ret;
1053 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1054 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1055 return ret;
1058 /** Generate the variable & register declarations for the GLSL output target */
1059 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1060 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
1061 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1063 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1064 const struct wined3d_state *state = &shader->device->state;
1065 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1066 const struct wined3d_gl_info *gl_info = context->gl_info;
1067 const struct wined3d_fb_state *fb = &shader->device->fb;
1068 unsigned int i, extra_constants_needed = 0;
1069 const struct wined3d_shader_lconst *lconst;
1070 const char *prefix;
1071 DWORD map;
1073 prefix = shader_glsl_get_prefix(version->type);
1075 /* Prototype the subroutines */
1076 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1078 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1081 /* Declare the constants (aka uniforms) */
1082 if (shader->limits->constant_float > 0)
1084 unsigned max_constantsF;
1086 /* Unless the shader uses indirect addressing, always declare the
1087 * maximum array size and ignore that we need some uniforms privately.
1088 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1089 * and immediate values, still declare VC[256]. If the shader needs
1090 * more uniforms than we have it won't work in any case. If it uses
1091 * less, the compiler will figure out which uniforms are really used
1092 * and strip them out. This allows a shader to use c255 on a dx9 card,
1093 * as long as it doesn't also use all the other constants.
1095 * If the shader uses indirect addressing the compiler must assume
1096 * that all declared uniforms are used. In this case, declare only the
1097 * amount that we're assured to have.
1099 * Thus we run into problems in these two cases:
1100 * 1) The shader really uses more uniforms than supported.
1101 * 2) The shader uses indirect addressing, less constants than
1102 * supported, but uses a constant index > #supported consts. */
1103 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1105 /* No indirect addressing here. */
1106 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1108 else
1110 if (reg_maps->usesrelconstF)
1112 /* Subtract the other potential uniforms from the max
1113 * available (bools, ints, and 1 row of projection matrix).
1114 * Subtract another uniform for immediate values, which have
1115 * to be loaded via uniform by the driver as well. The shader
1116 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1117 * shader code, so one vec4 should be enough. (Unfortunately
1118 * the Nvidia driver doesn't store 128 and -128 in one float).
1120 * Writing gl_ClipVertex requires one uniform for each
1121 * clipplane as well. */
1122 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1123 if(ctx_priv->cur_vs_args->clip_enabled)
1125 max_constantsF -= gl_info->limits.clipplanes;
1127 max_constantsF -= count_bits(reg_maps->integer_constants);
1128 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1129 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1130 * for now take this into account when calculating the number of available constants
1132 max_constantsF -= count_bits(reg_maps->boolean_constants);
1133 /* Set by driver quirks in directx.c */
1134 max_constantsF -= gl_info->reserved_glsl_constants;
1136 if (max_constantsF < shader->limits->constant_float)
1138 static unsigned int once;
1140 if (!once++)
1141 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1142 " it may not render correctly.\n");
1143 else
1144 WARN("The hardware does not support enough uniform components to run this shader.\n");
1147 else
1149 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1152 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1153 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1156 /* Always declare the full set of constants, the compiler can remove the
1157 * unused ones because d3d doesn't (yet) support indirect int and bool
1158 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1159 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1160 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1162 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1163 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1165 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1167 if (reg_maps->cb_sizes[i])
1168 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1169 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1172 /* Declare texture samplers */
1173 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1175 struct wined3d_shader_sampler_map_entry *entry;
1176 BOOL shadow_sampler, tex_rect;
1177 const char *sampler_type;
1179 entry = &reg_maps->sampler_map.entries[i];
1181 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1183 ERR("Invalid resource index %u.\n", entry->resource_idx);
1184 continue;
1187 shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1 << entry->sampler_idx));
1188 switch (reg_maps->resource_info[entry->resource_idx].type)
1190 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1191 if (shadow_sampler)
1192 sampler_type = "sampler1DShadow";
1193 else
1194 sampler_type = "sampler1D";
1195 break;
1197 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1198 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1199 && (ps_args->np2_fixup & (1 << entry->resource_idx))
1200 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1201 if (shadow_sampler)
1203 if (tex_rect)
1204 sampler_type = "sampler2DRectShadow";
1205 else
1206 sampler_type = "sampler2DShadow";
1208 else
1210 if (tex_rect)
1211 sampler_type = "sampler2DRect";
1212 else
1213 sampler_type = "sampler2D";
1215 break;
1217 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1218 if (shadow_sampler)
1219 FIXME("Unsupported 3D shadow sampler.\n");
1220 sampler_type = "sampler3D";
1221 break;
1223 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1224 if (shadow_sampler)
1225 FIXME("Unsupported Cube shadow sampler.\n");
1226 sampler_type = "samplerCube";
1227 break;
1229 default:
1230 sampler_type = "unsupported_sampler";
1231 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[i].type);
1232 break;
1234 shader_addline(buffer, "uniform %s %s_sampler%u;\n", sampler_type, prefix, entry->bind_idx);
1237 /* Declare uniforms for NP2 texcoord fixup:
1238 * This is NOT done inside the loop that declares the texture samplers
1239 * since the NP2 fixup code is currently only used for the GeforceFX
1240 * series and when forcing the ARB_npot extension off. Modern cards just
1241 * skip the code anyway, so put it inside a separate loop. */
1242 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1244 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1245 UINT cur = 0;
1247 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1248 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1249 * samplerNP2Fixup stores texture dimensions and is updated through
1250 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1252 for (i = 0; i < shader->limits->sampler; ++i)
1254 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1 << i)))
1255 continue;
1257 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1259 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1260 continue;
1263 fixup->idx[i] = cur++;
1266 fixup->num_consts = (cur + 1) >> 1;
1267 fixup->active = ps_args->np2_fixup;
1268 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1271 /* Declare address variables */
1272 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1274 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1277 /* Declare texture coordinate temporaries and initialize them */
1278 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1280 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1283 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1285 for (i = 0; i < shader->input_signature.element_count; ++i)
1287 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
1288 if (e->sysval_semantic == WINED3D_SV_INSTANCEID)
1289 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1290 prefix, e->register_idx);
1291 else
1292 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, e->register_idx);
1295 shader_addline(buffer, "uniform vec4 posFixup;\n");
1296 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits->packed_output);
1298 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1300 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
1302 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1304 if (version->major >= 3)
1306 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
1308 if (use_vs(state))
1309 shader_addline(buffer, "varying vec4 %s_link[%u];\n", prefix, in_count);
1310 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1313 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1315 if (!(map & 1))
1316 continue;
1318 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1320 if (reg_maps->luminanceparams & (1 << i))
1322 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1323 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1324 extra_constants_needed++;
1327 extra_constants_needed++;
1330 if (ps_args->srgb_correction)
1332 shader_addline(buffer, "const vec4 srgb_const0 = ");
1333 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
1334 shader_addline(buffer, ";\n");
1335 shader_addline(buffer, "const vec4 srgb_const1 = ");
1336 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
1337 shader_addline(buffer, ";\n");
1339 if (reg_maps->vpos || reg_maps->usesdsy)
1341 if (shader->limits->constant_float + extra_constants_needed
1342 + 1 < gl_info->limits.glsl_ps_float_constants)
1344 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1345 extra_constants_needed++;
1347 else
1349 float ycorrection[] =
1351 context->render_offscreen ? 0.0f : fb->render_targets[0]->height,
1352 context->render_offscreen ? 1.0f : -1.0f,
1353 0.0f,
1354 0.0f,
1357 /* This happens because we do not have proper tracking of the
1358 * constant registers that are actually used, only the max
1359 * limit of the shader version. */
1360 FIXME("Cannot find a free uniform for vpos correction params\n");
1361 shader_addline(buffer, "const vec4 ycorrection = ");
1362 shader_glsl_append_imm_vec4(buffer, ycorrection);
1363 shader_addline(buffer, ";\n");
1365 shader_addline(buffer, "vec4 vpos;\n");
1369 /* Declare output register temporaries */
1370 if (shader->limits->packed_output)
1371 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
1373 /* Declare temporary variables */
1374 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1376 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1379 /* Declare loop registers aLx */
1380 if (version->major < 4)
1382 for (i = 0; i < reg_maps->loop_depth; ++i)
1384 shader_addline(buffer, "int aL%u;\n", i);
1385 shader_addline(buffer, "int tmpInt%u;\n", i);
1389 /* Temporary variables for matrix operations */
1390 shader_addline(buffer, "vec4 tmp0;\n");
1391 shader_addline(buffer, "vec4 tmp1;\n");
1393 if (!shader->load_local_constsF)
1395 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1397 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
1398 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
1399 shader_addline(buffer, ";\n");
1403 /* Start the main program. */
1404 shader_addline(buffer, "void main()\n{\n");
1406 /* Direct3D applications expect integer vPos values, while OpenGL drivers
1407 * add approximately 0.5. This causes off-by-one problems as spotted by
1408 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
1409 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
1410 * causes precision troubles when we just subtract 0.5.
1412 * To deal with that, just floor() the position. This will eliminate the
1413 * fraction on all cards.
1415 * TODO: Test how this behaves with multisampling.
1417 * An advantage of floor is that it works even if the driver doesn't add
1418 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
1419 * to return in gl_FragCoord, even though coordinates specify the pixel
1420 * centers instead of the pixel corners. This code will behave correctly
1421 * on drivers that returns integer values. */
1422 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
1423 shader_addline(buffer,
1424 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1427 /*****************************************************************************
1428 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1430 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1431 ****************************************************************************/
1433 /* Prototypes */
1434 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1435 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1437 /** Used for opcode modifiers - They multiply the result by the specified amount */
1438 static const char * const shift_glsl_tab[] = {
1439 "", /* 0 (none) */
1440 "2.0 * ", /* 1 (x2) */
1441 "4.0 * ", /* 2 (x4) */
1442 "8.0 * ", /* 3 (x8) */
1443 "16.0 * ", /* 4 (x16) */
1444 "32.0 * ", /* 5 (x32) */
1445 "", /* 6 (x64) */
1446 "", /* 7 (x128) */
1447 "", /* 8 (d256) */
1448 "", /* 9 (d128) */
1449 "", /* 10 (d64) */
1450 "", /* 11 (d32) */
1451 "0.0625 * ", /* 12 (d16) */
1452 "0.125 * ", /* 13 (d8) */
1453 "0.25 * ", /* 14 (d4) */
1454 "0.5 * " /* 15 (d2) */
1457 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1458 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1459 const char *in_reg, const char *in_regswizzle, char *out_str)
1461 out_str[0] = 0;
1463 switch (src_modifier)
1465 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1466 case WINED3DSPSM_DW:
1467 case WINED3DSPSM_NONE:
1468 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1469 break;
1470 case WINED3DSPSM_NEG:
1471 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1472 break;
1473 case WINED3DSPSM_NOT:
1474 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1475 break;
1476 case WINED3DSPSM_BIAS:
1477 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1478 break;
1479 case WINED3DSPSM_BIASNEG:
1480 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1481 break;
1482 case WINED3DSPSM_SIGN:
1483 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1484 break;
1485 case WINED3DSPSM_SIGNNEG:
1486 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1487 break;
1488 case WINED3DSPSM_COMP:
1489 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1490 break;
1491 case WINED3DSPSM_X2:
1492 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1493 break;
1494 case WINED3DSPSM_X2NEG:
1495 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1496 break;
1497 case WINED3DSPSM_ABS:
1498 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1499 break;
1500 case WINED3DSPSM_ABSNEG:
1501 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1502 break;
1503 default:
1504 FIXME("Unhandled modifier %u\n", src_modifier);
1505 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1509 /** Writes the GLSL variable name that corresponds to the register that the
1510 * DX opcode parameter is trying to access */
1511 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1512 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1514 /* oPos, oFog and oPts in D3D */
1515 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
1517 const struct wined3d_shader *shader = ins->ctx->shader;
1518 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1519 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1520 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1521 const char *prefix = shader_glsl_get_prefix(version->type);
1522 struct glsl_src_param rel_param0, rel_param1;
1523 char imm_str[4][17];
1525 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
1526 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
1527 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
1528 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
1529 *is_color = FALSE;
1531 switch (reg->type)
1533 case WINED3DSPR_TEMP:
1534 sprintf(register_name, "R%u", reg->idx[0].offset);
1535 break;
1537 case WINED3DSPR_INPUT:
1538 /* vertex shaders */
1539 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1541 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1542 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx[0].offset))
1543 *is_color = TRUE;
1544 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
1545 break;
1548 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1550 if (reg->idx[0].rel_addr)
1552 if (reg->idx[1].rel_addr)
1553 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
1554 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1555 else
1556 sprintf(register_name, "gs_in[%s + %u][%u]",
1557 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
1559 else if (reg->idx[1].rel_addr)
1560 sprintf(register_name, "gs_in[%u][%s + %u]",
1561 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1562 else
1563 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
1564 break;
1567 /* pixel shaders >= 3.0 */
1568 if (version->major >= 3)
1570 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
1571 unsigned int in_count = vec4_varyings(version->major, gl_info);
1573 if (reg->idx[0].rel_addr)
1575 /* Removing a + 0 would be an obvious optimization, but
1576 * OS X doesn't see the NOP operation there. */
1577 if (idx)
1579 if (shader->u.ps.declared_in_count > in_count)
1581 sprintf(register_name,
1582 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
1583 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
1584 prefix, rel_param0.param_str, idx);
1586 else
1588 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
1591 else
1593 if (shader->u.ps.declared_in_count > in_count)
1595 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
1596 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
1597 prefix, rel_param0.param_str);
1599 else
1601 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
1605 else
1607 if (idx == in_count) sprintf(register_name, "gl_Color");
1608 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
1609 else sprintf(register_name, "%s_in[%u]", prefix, idx);
1612 else
1614 if (!reg->idx[0].offset)
1615 strcpy(register_name, "gl_Color");
1616 else
1617 strcpy(register_name, "gl_SecondaryColor");
1618 break;
1620 break;
1622 case WINED3DSPR_CONST:
1624 /* Relative addressing */
1625 if (reg->idx[0].rel_addr)
1627 if (reg->idx[0].offset)
1628 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
1629 else
1630 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
1632 else
1634 if (shader_constant_is_local(shader, reg->idx[0].offset))
1635 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
1636 else
1637 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
1640 break;
1642 case WINED3DSPR_CONSTINT:
1643 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
1644 break;
1646 case WINED3DSPR_CONSTBOOL:
1647 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
1648 break;
1650 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1651 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1652 sprintf(register_name, "T%u", reg->idx[0].offset);
1653 else
1654 sprintf(register_name, "A%u", reg->idx[0].offset);
1655 break;
1657 case WINED3DSPR_LOOP:
1658 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
1659 break;
1661 case WINED3DSPR_SAMPLER:
1662 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
1663 break;
1665 case WINED3DSPR_COLOROUT:
1666 if (reg->idx[0].offset >= gl_info->limits.buffers)
1667 WARN("Write to render target %u, only %d supported.\n",
1668 reg->idx[0].offset, gl_info->limits.buffers);
1670 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
1671 break;
1673 case WINED3DSPR_RASTOUT:
1674 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
1675 break;
1677 case WINED3DSPR_DEPTHOUT:
1678 sprintf(register_name, "gl_FragDepth");
1679 break;
1681 case WINED3DSPR_ATTROUT:
1682 if (!reg->idx[0].offset)
1683 sprintf(register_name, "%s_out[8]", prefix);
1684 else
1685 sprintf(register_name, "%s_out[9]", prefix);
1686 break;
1688 case WINED3DSPR_TEXCRDOUT:
1689 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1690 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
1691 break;
1693 case WINED3DSPR_MISCTYPE:
1694 if (!reg->idx[0].offset)
1696 /* vPos */
1697 sprintf(register_name, "vpos");
1699 else if (reg->idx[0].offset == 1)
1701 /* Note that gl_FrontFacing is a bool, while vFace is
1702 * a float for which the sign determines front/back */
1703 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1705 else
1707 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
1708 sprintf(register_name, "unrecognized_register");
1710 break;
1712 case WINED3DSPR_IMMCONST:
1713 switch (reg->immconst_type)
1715 case WINED3D_IMMCONST_SCALAR:
1716 switch (reg->data_type)
1718 case WINED3D_DATA_FLOAT:
1719 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
1720 break;
1721 case WINED3D_DATA_INT:
1722 sprintf(register_name, "%#x", reg->immconst_data[0]);
1723 break;
1724 case WINED3D_DATA_RESOURCE:
1725 case WINED3D_DATA_SAMPLER:
1726 case WINED3D_DATA_UINT:
1727 sprintf(register_name, "%#xu", reg->immconst_data[0]);
1728 break;
1729 default:
1730 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1731 break;
1733 break;
1735 case WINED3D_IMMCONST_VEC4:
1736 switch (reg->data_type)
1738 case WINED3D_DATA_FLOAT:
1739 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
1740 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
1741 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
1742 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
1743 sprintf(register_name, "vec4(%s, %s, %s, %s)",
1744 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
1745 break;
1746 case WINED3D_DATA_INT:
1747 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
1748 reg->immconst_data[0], reg->immconst_data[1],
1749 reg->immconst_data[2], reg->immconst_data[3]);
1750 break;
1751 case WINED3D_DATA_RESOURCE:
1752 case WINED3D_DATA_SAMPLER:
1753 case WINED3D_DATA_UINT:
1754 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
1755 reg->immconst_data[0], reg->immconst_data[1],
1756 reg->immconst_data[2], reg->immconst_data[3]);
1757 break;
1758 default:
1759 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1760 break;
1762 break;
1764 default:
1765 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
1766 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
1768 break;
1770 case WINED3DSPR_CONSTBUFFER:
1771 if (reg->idx[1].rel_addr)
1772 sprintf(register_name, "%s_cb%u[%s + %u]",
1773 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1774 else
1775 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
1776 break;
1778 case WINED3DSPR_PRIMID:
1779 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
1780 break;
1782 default:
1783 FIXME("Unhandled register type %#x.\n", reg->type);
1784 sprintf(register_name, "unrecognized_register");
1785 break;
1789 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1791 *str++ = '.';
1792 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1793 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1794 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1795 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1796 *str = '\0';
1799 /* Get the GLSL write mask for the destination register */
1800 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1802 DWORD mask = param->write_mask;
1804 if (shader_is_scalar(&param->reg))
1806 mask = WINED3DSP_WRITEMASK_0;
1807 *write_mask = '\0';
1809 else
1811 shader_glsl_write_mask_to_str(mask, write_mask);
1814 return mask;
1817 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1818 unsigned int size = 0;
1820 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1821 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1822 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1823 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1825 return size;
1828 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
1830 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1831 * but addressed as "rgba". To fix this we need to swap the register's x
1832 * and z components. */
1833 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1835 *str++ = '.';
1836 /* swizzle bits fields: wwzzyyxx */
1837 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
1838 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
1839 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
1840 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
1841 *str = '\0';
1844 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
1845 BOOL fixup, DWORD mask, char *swizzle_str)
1847 if (shader_is_scalar(&param->reg))
1848 *swizzle_str = '\0';
1849 else
1850 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
1853 /* From a given parameter token, generate the corresponding GLSL string.
1854 * Also, return the actual register name and swizzle in case the
1855 * caller needs this information as well. */
1856 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1857 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
1859 BOOL is_color = FALSE;
1860 char swizzle_str[6];
1862 glsl_src->reg_name[0] = '\0';
1863 glsl_src->param_str[0] = '\0';
1864 swizzle_str[0] = '\0';
1866 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
1867 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
1869 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
1871 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
1873 else
1875 char reg_name[200];
1877 switch (wined3d_src->reg.data_type)
1879 case WINED3D_DATA_FLOAT:
1880 sprintf(reg_name, "%s", glsl_src->reg_name);
1881 break;
1882 case WINED3D_DATA_INT:
1883 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
1884 break;
1885 case WINED3D_DATA_RESOURCE:
1886 case WINED3D_DATA_SAMPLER:
1887 case WINED3D_DATA_UINT:
1888 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
1889 break;
1890 default:
1891 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
1892 sprintf(reg_name, "%s", glsl_src->reg_name);
1893 break;
1896 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
1900 /* From a given parameter token, generate the corresponding GLSL string.
1901 * Also, return the actual register name and swizzle in case the
1902 * caller needs this information as well. */
1903 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1904 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
1906 BOOL is_color = FALSE;
1908 glsl_dst->mask_str[0] = '\0';
1909 glsl_dst->reg_name[0] = '\0';
1911 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
1912 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1915 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1916 static DWORD shader_glsl_append_dst_ext(struct wined3d_shader_buffer *buffer,
1917 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
1918 enum wined3d_data_type data_type)
1920 struct glsl_dst_param glsl_dst;
1921 DWORD mask;
1923 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
1925 switch (data_type)
1927 case WINED3D_DATA_FLOAT:
1928 shader_addline(buffer, "%s%s = %s(",
1929 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1930 break;
1931 case WINED3D_DATA_INT:
1932 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
1933 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1934 break;
1935 case WINED3D_DATA_RESOURCE:
1936 case WINED3D_DATA_SAMPLER:
1937 case WINED3D_DATA_UINT:
1938 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
1939 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1940 break;
1941 default:
1942 FIXME("Unhandled data type %#x.\n", data_type);
1943 shader_addline(buffer, "%s%s = %s(",
1944 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1945 break;
1949 return mask;
1952 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1953 static DWORD shader_glsl_append_dst(struct wined3d_shader_buffer *buffer, const struct wined3d_shader_instruction *ins)
1955 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
1958 /** Process GLSL instruction modifiers */
1959 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1961 struct glsl_dst_param dst_param;
1962 DWORD modifiers;
1964 if (!ins->dst_count) return;
1966 modifiers = ins->dst[0].modifiers;
1967 if (!modifiers) return;
1969 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
1971 if (modifiers & WINED3DSPDM_SATURATE)
1973 /* _SAT means to clamp the value of the register to between 0 and 1 */
1974 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1975 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1978 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
1980 FIXME("_centroid modifier not handled\n");
1983 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
1985 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1989 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
1991 switch (op)
1993 case WINED3D_SHADER_REL_OP_GT: return ">";
1994 case WINED3D_SHADER_REL_OP_EQ: return "==";
1995 case WINED3D_SHADER_REL_OP_GE: return ">=";
1996 case WINED3D_SHADER_REL_OP_LT: return "<";
1997 case WINED3D_SHADER_REL_OP_NE: return "!=";
1998 case WINED3D_SHADER_REL_OP_LE: return "<=";
1999 default:
2000 FIXME("Unrecognized operator %#x.\n", op);
2001 return "(\?\?)";
2005 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
2006 DWORD resource_idx, DWORD flags, struct glsl_sample_function *sample_function)
2008 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
2009 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2010 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
2011 && (((const struct shader_glsl_ctx_priv *)ctx->backend_data)->cur_ps_args->shadow & (1 << resource_idx));
2012 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
2013 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2014 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
2015 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
2017 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
2019 /* Note that there's no such thing as a projected cube texture. */
2020 switch (resource_type)
2022 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2023 if (shadow)
2025 if (lod)
2027 sample_function->name = projected ? "shadow1DProjLod" : "shadow1DLod";
2029 else if (grad)
2031 if (gl_info->supported[EXT_GPU_SHADER4])
2032 sample_function->name = projected ? "shadow1DProjGrad" : "shadow1DGrad";
2033 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2034 sample_function->name = projected ? "shadow1DProjGradARB" : "shadow1DGradARB";
2035 else
2037 FIXME("Unsupported 1D shadow grad function.\n");
2038 sample_function->name = "unsupported1DGrad";
2041 else
2043 sample_function->name = projected ? "shadow1DProj" : "shadow1D";
2045 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2047 else
2049 if (lod)
2051 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
2053 else if (grad)
2055 if (gl_info->supported[EXT_GPU_SHADER4])
2056 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
2057 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2058 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
2059 else
2061 FIXME("Unsupported 1D grad function.\n");
2062 sample_function->name = "unsupported1DGrad";
2065 else
2067 sample_function->name = projected ? "texture1DProj" : "texture1D";
2069 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
2071 break;
2073 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2074 if (shadow)
2076 if (texrect)
2078 if (lod)
2080 sample_function->name = projected ? "shadow2DRectProjLod" : "shadow2DRectLod";
2082 else if (grad)
2084 if (gl_info->supported[EXT_GPU_SHADER4])
2085 sample_function->name = projected ? "shadow2DRectProjGrad" : "shadow2DRectGrad";
2086 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2087 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
2088 else
2090 FIXME("Unsupported RECT shadow grad function.\n");
2091 sample_function->name = "unsupported2DRectGrad";
2094 else
2096 sample_function->name = projected ? "shadow2DRectProj" : "shadow2DRect";
2099 else
2101 if (lod)
2103 sample_function->name = projected ? "shadow2DProjLod" : "shadow2DLod";
2105 else if (grad)
2107 if (gl_info->supported[EXT_GPU_SHADER4])
2108 sample_function->name = projected ? "shadow2DProjGrad" : "shadow2DGrad";
2109 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2110 sample_function->name = projected ? "shadow2DProjGradARB" : "shadow2DGradARB";
2111 else
2113 FIXME("Unsupported 2D shadow grad function.\n");
2114 sample_function->name = "unsupported2DGrad";
2117 else
2119 sample_function->name = projected ? "shadow2DProj" : "shadow2D";
2122 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2124 else
2126 if (texrect)
2128 if (lod)
2130 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
2132 else if (grad)
2134 if (gl_info->supported[EXT_GPU_SHADER4])
2135 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
2136 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2137 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
2138 else
2140 FIXME("Unsupported RECT grad function.\n");
2141 sample_function->name = "unsupported2DRectGrad";
2144 else
2146 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
2149 else
2151 if (lod)
2153 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
2155 else if (grad)
2157 if (gl_info->supported[EXT_GPU_SHADER4])
2158 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
2159 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2160 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
2161 else
2163 FIXME("Unsupported 2D grad function.\n");
2164 sample_function->name = "unsupported2DGrad";
2167 else
2169 sample_function->name = projected ? "texture2DProj" : "texture2D";
2172 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2174 break;
2176 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2177 if (shadow)
2179 FIXME("Unsupported 3D shadow function.\n");
2180 sample_function->name = "unsupported3DShadow";
2181 sample_function->coord_mask = 0;
2183 else
2185 if (lod)
2187 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
2189 else if (grad)
2191 if (gl_info->supported[EXT_GPU_SHADER4])
2192 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
2193 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2194 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
2195 else
2197 FIXME("Unsupported 3D grad function.\n");
2198 sample_function->name = "unsupported3DGrad";
2201 else
2203 sample_function->name = projected ? "texture3DProj" : "texture3D";
2205 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2207 break;
2209 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2210 if (shadow)
2212 FIXME("Unsupported Cube shadow function.\n");
2213 sample_function->name = "unsupportedCubeShadow";
2214 sample_function->coord_mask = 0;
2216 else
2218 if (lod)
2220 sample_function->name = "textureCubeLod";
2222 else if (grad)
2224 if (gl_info->supported[EXT_GPU_SHADER4])
2225 sample_function->name = "textureCubeGrad";
2226 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2227 sample_function->name = "textureCubeGradARB";
2228 else
2230 FIXME("Unsupported Cube grad function.\n");
2231 sample_function->name = "unsupportedCubeGrad";
2234 else
2236 sample_function->name = "textureCube";
2238 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2240 break;
2242 default:
2243 sample_function->name = "";
2244 sample_function->coord_mask = 0;
2245 FIXME("Unhandled resource type %#x.\n", resource_type);
2246 break;
2250 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2251 BOOL sign_fixup, enum fixup_channel_source channel_source)
2253 switch(channel_source)
2255 case CHANNEL_SOURCE_ZERO:
2256 strcat(arguments, "0.0");
2257 break;
2259 case CHANNEL_SOURCE_ONE:
2260 strcat(arguments, "1.0");
2261 break;
2263 case CHANNEL_SOURCE_X:
2264 strcat(arguments, reg_name);
2265 strcat(arguments, ".x");
2266 break;
2268 case CHANNEL_SOURCE_Y:
2269 strcat(arguments, reg_name);
2270 strcat(arguments, ".y");
2271 break;
2273 case CHANNEL_SOURCE_Z:
2274 strcat(arguments, reg_name);
2275 strcat(arguments, ".z");
2276 break;
2278 case CHANNEL_SOURCE_W:
2279 strcat(arguments, reg_name);
2280 strcat(arguments, ".w");
2281 break;
2283 default:
2284 FIXME("Unhandled channel source %#x\n", channel_source);
2285 strcat(arguments, "undefined");
2286 break;
2289 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2292 static void shader_glsl_color_correction_ext(struct wined3d_shader_buffer *buffer,
2293 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2295 unsigned int mask_size, remaining;
2296 DWORD fixup_mask = 0;
2297 char arguments[256];
2298 char mask_str[6];
2300 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2301 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2302 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2303 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2304 if (!(mask &= fixup_mask))
2305 return;
2307 if (is_complex_fixup(fixup))
2309 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2310 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2311 return;
2314 shader_glsl_write_mask_to_str(mask, mask_str);
2315 mask_size = shader_glsl_get_write_mask_size(mask);
2317 arguments[0] = '\0';
2318 remaining = mask_size;
2319 if (mask & WINED3DSP_WRITEMASK_0)
2321 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2322 if (--remaining) strcat(arguments, ", ");
2324 if (mask & WINED3DSP_WRITEMASK_1)
2326 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2327 if (--remaining) strcat(arguments, ", ");
2329 if (mask & WINED3DSP_WRITEMASK_2)
2331 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2332 if (--remaining) strcat(arguments, ", ");
2334 if (mask & WINED3DSP_WRITEMASK_3)
2336 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2337 if (--remaining) strcat(arguments, ", ");
2340 if (mask_size > 1)
2341 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2342 else
2343 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2346 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2348 char reg_name[256];
2349 BOOL is_color;
2351 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2352 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2355 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2356 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2357 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2359 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2360 char dst_swizzle[6];
2361 struct color_fixup_desc fixup;
2362 BOOL np2_fixup = FALSE;
2363 va_list args;
2365 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2367 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2369 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2370 fixup = priv->cur_ps_args->color_fixup[sampler];
2372 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2373 if(bias) {
2374 FIXME("Biased sampling from NP2 textures is unsupported\n");
2375 } else {
2376 np2_fixup = TRUE;
2380 else
2382 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
2385 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2387 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2388 sample_function->name, shader_glsl_get_prefix(version->type), sampler);
2390 va_start(args, coord_reg_fmt);
2391 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2392 va_end(args);
2394 if(bias) {
2395 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2396 } else {
2397 if (np2_fixup) {
2398 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2399 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2401 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2402 (idx % 2) ? "zw" : "xy", dst_swizzle);
2403 } else if(dx && dy) {
2404 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2405 } else {
2406 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2410 if(!is_identity_fixup(fixup)) {
2411 shader_glsl_color_correction(ins, fixup);
2415 /*****************************************************************************
2416 * Begin processing individual instruction opcodes
2417 ****************************************************************************/
2419 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2421 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2422 struct glsl_src_param src0_param;
2423 struct glsl_src_param src1_param;
2424 DWORD write_mask;
2425 const char *op;
2427 /* Determine the GLSL operator to use based on the opcode */
2428 switch (ins->handler_idx)
2430 case WINED3DSIH_ADD: op = "+"; break;
2431 case WINED3DSIH_AND: op = "&"; break;
2432 case WINED3DSIH_DIV: op = "/"; break;
2433 case WINED3DSIH_IADD: op = "+"; break;
2434 case WINED3DSIH_ISHL: op = "<<"; break;
2435 case WINED3DSIH_MUL: op = "*"; break;
2436 case WINED3DSIH_OR: op = "|"; break;
2437 case WINED3DSIH_SUB: op = "-"; break;
2438 case WINED3DSIH_USHR: op = ">>"; break;
2439 case WINED3DSIH_XOR: op = "^"; break;
2440 default:
2441 op = "<unhandled operator>";
2442 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2443 break;
2446 write_mask = shader_glsl_append_dst(buffer, ins);
2447 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2448 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2449 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2452 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2454 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2455 struct glsl_src_param src0_param;
2456 struct glsl_src_param src1_param;
2457 unsigned int mask_size;
2458 DWORD write_mask;
2459 const char *op;
2461 write_mask = shader_glsl_append_dst(buffer, ins);
2462 mask_size = shader_glsl_get_write_mask_size(write_mask);
2463 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2464 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2466 if (mask_size > 1)
2468 switch (ins->handler_idx)
2470 case WINED3DSIH_EQ: op = "equal"; break;
2471 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2472 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2473 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
2474 case WINED3DSIH_LT: op = "lessThan"; break;
2475 case WINED3DSIH_NE: op = "notEqual"; break;
2476 default:
2477 op = "<unhandled operator>";
2478 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2479 break;
2482 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2483 mask_size, op, src0_param.param_str, src1_param.param_str);
2485 else
2487 switch (ins->handler_idx)
2489 case WINED3DSIH_EQ: op = "=="; break;
2490 case WINED3DSIH_GE: op = ">="; break;
2491 case WINED3DSIH_IGE: op = ">="; break;
2492 case WINED3DSIH_UGE: op = ">="; break;
2493 case WINED3DSIH_LT: op = "<"; break;
2494 case WINED3DSIH_NE: op = "!="; break;
2495 default:
2496 op = "<unhandled operator>";
2497 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2498 break;
2501 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2502 src0_param.param_str, op, src1_param.param_str);
2506 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2508 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2509 struct glsl_src_param src0_param;
2510 struct glsl_src_param src1_param;
2511 DWORD write_mask;
2513 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2514 * not, we can emulate it. */
2515 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2516 FIXME("64-bit integer multiplies not implemented.\n");
2518 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2520 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2521 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2522 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2524 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2525 src0_param.param_str, src1_param.param_str);
2529 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2531 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2532 struct glsl_src_param src0_param, src1_param;
2533 DWORD write_mask;
2535 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2538 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2540 char dst_mask[6];
2542 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2543 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2544 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2545 shader_addline(buffer, "tmp0%s = %s / %s;\n",
2546 dst_mask, src0_param.param_str, src1_param.param_str);
2548 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2549 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2550 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2551 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
2553 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2554 shader_addline(buffer, "tmp0%s);\n", dst_mask);
2556 else
2558 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2559 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2560 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2561 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
2564 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2566 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2567 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2568 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2569 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
2573 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2574 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2576 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2577 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2578 struct glsl_src_param src0_param;
2579 DWORD write_mask;
2581 write_mask = shader_glsl_append_dst(buffer, ins);
2582 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2584 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2585 * shader versions WINED3DSIO_MOVA is used for this. */
2586 if (ins->ctx->reg_maps->shader_version.major == 1
2587 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
2588 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2590 /* This is a simple floor() */
2591 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2592 if (mask_size > 1) {
2593 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2594 } else {
2595 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2598 else if(ins->handler_idx == WINED3DSIH_MOVA)
2600 /* We need to *round* to the nearest int here. */
2601 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2603 if (gl_info->supported[EXT_GPU_SHADER4])
2605 if (mask_size > 1)
2606 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2607 else
2608 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2610 else
2612 if (mask_size > 1)
2613 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2614 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2615 else
2616 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2617 src0_param.param_str, src0_param.param_str);
2620 else
2622 shader_addline(buffer, "%s);\n", src0_param.param_str);
2626 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2627 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2629 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2630 struct glsl_src_param src0_param;
2631 struct glsl_src_param src1_param;
2632 DWORD dst_write_mask, src_write_mask;
2633 unsigned int dst_size = 0;
2635 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2636 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2638 /* dp3 works on vec3, dp4 on vec4 */
2639 if (ins->handler_idx == WINED3DSIH_DP4)
2641 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2642 } else {
2643 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2646 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2647 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2649 if (dst_size > 1) {
2650 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2651 } else {
2652 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2656 /* Note that this instruction has some restrictions. The destination write mask
2657 * can't contain the w component, and the source swizzles have to be .xyzw */
2658 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2660 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2661 struct glsl_src_param src0_param;
2662 struct glsl_src_param src1_param;
2663 char dst_mask[6];
2665 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2666 shader_glsl_append_dst(ins->ctx->buffer, ins);
2667 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2668 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2669 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2672 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
2674 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
2677 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2678 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2679 * GLSL uses the value as-is. */
2680 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2682 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2683 struct glsl_src_param src0_param;
2684 struct glsl_src_param src1_param;
2685 DWORD dst_write_mask;
2686 unsigned int dst_size;
2688 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2689 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2691 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2692 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2694 if (dst_size > 1)
2696 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2697 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
2699 else
2701 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
2702 src1_param.param_str, src0_param.param_str, src1_param.param_str);
2706 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2707 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
2709 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2710 struct glsl_src_param src_param;
2711 const char *instruction;
2712 DWORD write_mask;
2713 unsigned i;
2715 /* Determine the GLSL function to use based on the opcode */
2716 /* TODO: Possibly make this a table for faster lookups */
2717 switch (ins->handler_idx)
2719 case WINED3DSIH_MIN: instruction = "min"; break;
2720 case WINED3DSIH_MAX: instruction = "max"; break;
2721 case WINED3DSIH_ABS: instruction = "abs"; break;
2722 case WINED3DSIH_FRC: instruction = "fract"; break;
2723 case WINED3DSIH_DSX: instruction = "dFdx"; break;
2724 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
2725 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
2726 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
2727 default: instruction = "";
2728 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2729 break;
2732 write_mask = shader_glsl_append_dst(buffer, ins);
2734 shader_addline(buffer, "%s(", instruction);
2736 if (ins->src_count)
2738 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2739 shader_addline(buffer, "%s", src_param.param_str);
2740 for (i = 1; i < ins->src_count; ++i)
2742 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
2743 shader_addline(buffer, ", %s", src_param.param_str);
2747 shader_addline(buffer, "));\n");
2750 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
2752 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
2754 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2755 struct glsl_src_param src_param;
2756 unsigned int mask_size;
2757 DWORD write_mask;
2758 char dst_mask[6];
2760 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
2761 mask_size = shader_glsl_get_write_mask_size(write_mask);
2762 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2764 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
2765 src_param.param_str, src_param.param_str);
2766 shader_glsl_append_dst(buffer, ins);
2768 if (mask_size > 1)
2770 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
2771 mask_size, src_param.param_str);
2773 else
2775 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
2776 src_param.param_str);
2780 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
2782 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2783 struct glsl_src_param src0_param;
2784 const char *prefix, *suffix;
2785 unsigned int dst_size;
2786 DWORD dst_write_mask;
2788 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2789 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2791 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src0_param);
2793 switch (ins->handler_idx)
2795 case WINED3DSIH_EXP:
2796 case WINED3DSIH_EXPP:
2797 prefix = "exp2(";
2798 suffix = ")";
2799 break;
2801 case WINED3DSIH_LOG:
2802 case WINED3DSIH_LOGP:
2803 prefix = "log2(abs(";
2804 suffix = "))";
2805 break;
2807 case WINED3DSIH_RCP:
2808 prefix = "1.0 / ";
2809 suffix = "";
2810 break;
2812 case WINED3DSIH_RSQ:
2813 prefix = "inversesqrt(abs(";
2814 suffix = "))";
2815 break;
2817 default:
2818 prefix = "";
2819 suffix = "";
2820 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2821 break;
2824 if (dst_size > 1)
2825 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
2826 else
2827 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
2830 /** Process the WINED3DSIO_EXPP instruction in GLSL:
2831 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2832 * dst.x = 2^(floor(src))
2833 * dst.y = src - floor(src)
2834 * dst.z = 2^src (partial precision is allowed, but optional)
2835 * dst.w = 1.0;
2836 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2837 * dst = 2^src; (partial precision is allowed, but optional)
2839 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
2841 if (ins->ctx->reg_maps->shader_version.major < 2)
2843 struct glsl_src_param src_param;
2844 char dst_mask[6];
2846 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2848 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
2849 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
2850 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
2851 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
2853 shader_glsl_append_dst(ins->ctx->buffer, ins);
2854 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2855 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
2856 return;
2859 shader_glsl_scalar_op(ins);
2862 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
2864 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2865 struct glsl_src_param src_param;
2866 unsigned int mask_size;
2867 DWORD write_mask;
2869 write_mask = shader_glsl_append_dst(buffer, ins);
2870 mask_size = shader_glsl_get_write_mask_size(write_mask);
2871 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2873 if (mask_size > 1)
2874 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
2875 else
2876 shader_addline(buffer, "int(%s));\n", src_param.param_str);
2879 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
2881 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2882 struct glsl_src_param src_param;
2883 unsigned int mask_size;
2884 DWORD write_mask;
2886 write_mask = shader_glsl_append_dst(buffer, ins);
2887 mask_size = shader_glsl_get_write_mask_size(write_mask);
2888 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2890 if (mask_size > 1)
2891 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
2892 else
2893 shader_addline(buffer, "float(%s));\n", src_param.param_str);
2896 /** Process signed comparison opcodes in GLSL. */
2897 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
2899 struct glsl_src_param src0_param;
2900 struct glsl_src_param src1_param;
2901 DWORD write_mask;
2902 unsigned int mask_size;
2904 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2905 mask_size = shader_glsl_get_write_mask_size(write_mask);
2906 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2907 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2909 if (mask_size > 1) {
2910 const char *compare;
2912 switch(ins->handler_idx)
2914 case WINED3DSIH_SLT: compare = "lessThan"; break;
2915 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
2916 default: compare = "";
2917 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2920 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
2921 src0_param.param_str, src1_param.param_str);
2922 } else {
2923 switch(ins->handler_idx)
2925 case WINED3DSIH_SLT:
2926 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2927 * to return 0.0 but step returns 1.0 because step is not < x
2928 * An alternative is a bvec compare padded with an unused second component.
2929 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2930 * issue. Playing with not() is not possible either because not() does not accept
2931 * a scalar.
2933 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
2934 src0_param.param_str, src1_param.param_str);
2935 break;
2936 case WINED3DSIH_SGE:
2937 /* Here we can use the step() function and safe a conditional */
2938 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
2939 break;
2940 default:
2941 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2947 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
2949 const char *condition_prefix, *condition_suffix;
2950 struct wined3d_shader_dst_param dst;
2951 struct glsl_src_param src0_param;
2952 struct glsl_src_param src1_param;
2953 struct glsl_src_param src2_param;
2954 BOOL temp_destination = FALSE;
2955 DWORD cmp_channel = 0;
2956 unsigned int i, j;
2957 char mask_char[6];
2958 DWORD write_mask;
2960 switch (ins->handler_idx)
2962 case WINED3DSIH_CMP:
2963 condition_prefix = "";
2964 condition_suffix = " >= 0.0";
2965 break;
2967 case WINED3DSIH_CND:
2968 condition_prefix = "";
2969 condition_suffix = " > 0.5";
2970 break;
2972 case WINED3DSIH_MOVC:
2973 condition_prefix = "bool(";
2974 condition_suffix = ")";
2975 break;
2977 default:
2978 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2979 condition_prefix = "<unhandled prefix>";
2980 condition_suffix = "<unhandled suffix>";
2981 break;
2984 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
2986 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2987 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2988 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2989 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2991 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2992 condition_prefix, src0_param.param_str, condition_suffix,
2993 src1_param.param_str, src2_param.param_str);
2994 return;
2997 dst = ins->dst[0];
2999 /* Splitting the instruction up in multiple lines imposes a problem:
3000 * The first lines may overwrite source parameters of the following lines.
3001 * Deal with that by using a temporary destination register if needed. */
3002 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
3003 && ins->src[0].reg.type == dst.reg.type)
3004 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
3005 && ins->src[1].reg.type == dst.reg.type)
3006 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
3007 && ins->src[2].reg.type == dst.reg.type))
3008 temp_destination = TRUE;
3010 /* Cycle through all source0 channels. */
3011 for (i = 0; i < 4; ++i)
3013 write_mask = 0;
3014 /* Find the destination channels which use the current source0 channel. */
3015 for (j = 0; j < 4; ++j)
3017 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
3019 write_mask |= WINED3DSP_WRITEMASK_0 << j;
3020 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
3023 dst.write_mask = ins->dst[0].write_mask & write_mask;
3025 if (temp_destination)
3027 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
3028 continue;
3029 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
3031 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
3032 continue;
3034 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
3035 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3036 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3038 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3039 condition_prefix, src0_param.param_str, condition_suffix,
3040 src1_param.param_str, src2_param.param_str);
3043 if (temp_destination)
3045 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3046 shader_glsl_append_dst(ins->ctx->buffer, ins);
3047 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3051 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3052 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3053 * the compare is done per component of src0. */
3054 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3056 struct glsl_src_param src0_param;
3057 struct glsl_src_param src1_param;
3058 struct glsl_src_param src2_param;
3059 DWORD write_mask;
3060 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3061 ins->ctx->reg_maps->shader_version.minor);
3063 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3065 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3066 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3067 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3068 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3070 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
3071 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3072 else
3073 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3074 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3075 return;
3078 shader_glsl_conditional_move(ins);
3081 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3082 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3084 struct glsl_src_param src0_param;
3085 struct glsl_src_param src1_param;
3086 struct glsl_src_param src2_param;
3087 DWORD write_mask;
3089 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3090 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3091 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3092 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3093 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3094 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3097 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3098 Vertex shaders to GLSL codes */
3099 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3101 int i;
3102 int nComponents = 0;
3103 struct wined3d_shader_dst_param tmp_dst = {{0}};
3104 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3105 struct wined3d_shader_instruction tmp_ins;
3107 memset(&tmp_ins, 0, sizeof(tmp_ins));
3109 /* Set constants for the temporary argument */
3110 tmp_ins.ctx = ins->ctx;
3111 tmp_ins.dst_count = 1;
3112 tmp_ins.dst = &tmp_dst;
3113 tmp_ins.src_count = 2;
3114 tmp_ins.src = tmp_src;
3116 switch(ins->handler_idx)
3118 case WINED3DSIH_M4x4:
3119 nComponents = 4;
3120 tmp_ins.handler_idx = WINED3DSIH_DP4;
3121 break;
3122 case WINED3DSIH_M4x3:
3123 nComponents = 3;
3124 tmp_ins.handler_idx = WINED3DSIH_DP4;
3125 break;
3126 case WINED3DSIH_M3x4:
3127 nComponents = 4;
3128 tmp_ins.handler_idx = WINED3DSIH_DP3;
3129 break;
3130 case WINED3DSIH_M3x3:
3131 nComponents = 3;
3132 tmp_ins.handler_idx = WINED3DSIH_DP3;
3133 break;
3134 case WINED3DSIH_M3x2:
3135 nComponents = 2;
3136 tmp_ins.handler_idx = WINED3DSIH_DP3;
3137 break;
3138 default:
3139 break;
3142 tmp_dst = ins->dst[0];
3143 tmp_src[0] = ins->src[0];
3144 tmp_src[1] = ins->src[1];
3145 for (i = 0; i < nComponents; ++i)
3147 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3148 shader_glsl_dot(&tmp_ins);
3149 ++tmp_src[1].reg.idx[0].offset;
3154 The LRP instruction performs a component-wise linear interpolation
3155 between the second and third operands using the first operand as the
3156 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3157 This is equivalent to mix(src2, src1, src0);
3159 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3161 struct glsl_src_param src0_param;
3162 struct glsl_src_param src1_param;
3163 struct glsl_src_param src2_param;
3164 DWORD write_mask;
3166 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3168 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3169 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3170 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3172 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3173 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3176 /** Process the WINED3DSIO_LIT instruction in GLSL:
3177 * dst.x = dst.w = 1.0
3178 * dst.y = (src0.x > 0) ? src0.x
3179 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3180 * where src.w is clamped at +- 128
3182 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3184 struct glsl_src_param src0_param;
3185 struct glsl_src_param src1_param;
3186 struct glsl_src_param src3_param;
3187 char dst_mask[6];
3189 shader_glsl_append_dst(ins->ctx->buffer, ins);
3190 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3192 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3193 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3194 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3196 /* The sdk specifies the instruction like this
3197 * dst.x = 1.0;
3198 * if(src.x > 0.0) dst.y = src.x
3199 * else dst.y = 0.0.
3200 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3201 * else dst.z = 0.0;
3202 * dst.w = 1.0;
3203 * (where power = src.w clamped between -128 and 128)
3205 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3206 * dst.x = 1.0 ... No further explanation needed
3207 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3208 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3209 * dst.w = 1.0. ... Nothing fancy.
3211 * So we still have one conditional in there. So do this:
3212 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3214 * 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),
3215 * 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.
3216 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3218 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3219 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3220 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3222 shader_addline(ins->ctx->buffer,
3223 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3224 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3225 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3226 src0_param.param_str, src3_param.param_str, dst_mask);
3229 /** Process the WINED3DSIO_DST instruction in GLSL:
3230 * dst.x = 1.0
3231 * dst.y = src0.x * src0.y
3232 * dst.z = src0.z
3233 * dst.w = src1.w
3235 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3237 struct glsl_src_param src0y_param;
3238 struct glsl_src_param src0z_param;
3239 struct glsl_src_param src1y_param;
3240 struct glsl_src_param src1w_param;
3241 char dst_mask[6];
3243 shader_glsl_append_dst(ins->ctx->buffer, ins);
3244 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3246 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3247 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3248 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3249 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3251 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3252 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3255 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3256 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3257 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3259 * dst.x = cos(src0.?)
3260 * dst.y = sin(src0.?)
3261 * dst.z = dst.z
3262 * dst.w = dst.w
3264 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3266 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3267 struct glsl_src_param src0_param;
3268 DWORD write_mask;
3270 if (ins->ctx->reg_maps->shader_version.major < 4)
3272 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3274 write_mask = shader_glsl_append_dst(buffer, ins);
3275 switch (write_mask)
3277 case WINED3DSP_WRITEMASK_0:
3278 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3279 break;
3281 case WINED3DSP_WRITEMASK_1:
3282 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3283 break;
3285 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3286 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3287 src0_param.param_str, src0_param.param_str);
3288 break;
3290 default:
3291 ERR("Write mask should be .x, .y or .xy\n");
3292 break;
3295 return;
3298 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3301 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3303 char dst_mask[6];
3305 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3306 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3307 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3309 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3310 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3311 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3313 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3314 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3316 else
3318 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3319 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3320 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3323 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3325 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3326 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3327 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3331 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3332 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3333 * generate invalid code
3335 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3337 struct glsl_src_param src0_param;
3338 DWORD write_mask;
3340 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3341 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3343 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3346 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3347 * Start a for() loop where src1.y is the initial value of aL,
3348 * increment aL by src1.z for a total of src1.x iterations.
3349 * Need to use a temporary variable for this operation.
3351 /* FIXME: I don't think nested loops will work correctly this way. */
3352 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3354 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3355 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3356 const struct wined3d_shader *shader = ins->ctx->shader;
3357 const struct wined3d_shader_lconst *constant;
3358 struct glsl_src_param src1_param;
3359 const DWORD *control_values = NULL;
3361 if (ins->ctx->reg_maps->shader_version.major < 4)
3363 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3365 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3366 * class hardware doesn't support real varying indexing, but Microsoft
3367 * designed this feature for Shader model 2.x+. If the loop control is
3368 * known at compile time, the GLSL compiler can unroll the loop, and
3369 * replace indirect addressing with direct addressing. */
3370 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3372 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3374 if (constant->idx == ins->src[1].reg.idx[0].offset)
3376 control_values = constant->value;
3377 break;
3382 if (control_values)
3384 struct wined3d_shader_loop_control loop_control;
3385 loop_control.count = control_values[0];
3386 loop_control.start = control_values[1];
3387 loop_control.step = (int)control_values[2];
3389 if (loop_control.step > 0)
3391 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3392 loop_state->current_depth, loop_control.start,
3393 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3394 loop_state->current_depth, loop_control.step);
3396 else if (loop_control.step < 0)
3398 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3399 loop_state->current_depth, loop_control.start,
3400 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3401 loop_state->current_depth, loop_control.step);
3403 else
3405 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3406 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3407 loop_state->current_depth, loop_control.count,
3408 loop_state->current_depth);
3411 else
3413 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3414 loop_state->current_depth, loop_state->current_reg,
3415 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3416 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3419 ++loop_state->current_reg;
3421 else
3423 shader_addline(buffer, "for (;;)\n{\n");
3426 ++loop_state->current_depth;
3429 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3431 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3433 shader_addline(ins->ctx->buffer, "}\n");
3435 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3437 --loop_state->current_depth;
3438 --loop_state->current_reg;
3441 if (ins->handler_idx == WINED3DSIH_ENDREP)
3443 --loop_state->current_depth;
3447 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3449 const struct wined3d_shader *shader = ins->ctx->shader;
3450 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3451 const struct wined3d_shader_lconst *constant;
3452 struct glsl_src_param src0_param;
3453 const DWORD *control_values = NULL;
3455 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3456 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3458 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3460 if (constant->idx == ins->src[0].reg.idx[0].offset)
3462 control_values = constant->value;
3463 break;
3468 if (control_values)
3470 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3471 loop_state->current_depth, loop_state->current_depth,
3472 control_values[0], loop_state->current_depth);
3474 else
3476 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3477 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3478 loop_state->current_depth, loop_state->current_depth,
3479 src0_param.param_str, loop_state->current_depth);
3482 ++loop_state->current_depth;
3485 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3487 struct glsl_src_param src0_param;
3489 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3490 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
3493 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3495 struct glsl_src_param src0_param;
3496 struct glsl_src_param src1_param;
3498 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3499 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3501 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3502 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3505 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3507 shader_addline(ins->ctx->buffer, "} else {\n");
3510 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3512 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3515 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3517 shader_addline(ins->ctx->buffer, "break;\n");
3520 /* FIXME: According to MSDN the compare is done per component. */
3521 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3523 struct glsl_src_param src0_param;
3524 struct glsl_src_param src1_param;
3526 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3527 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3529 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3530 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3533 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
3535 struct glsl_src_param src_param;
3537 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
3538 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
3541 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
3543 shader_addline(ins->ctx->buffer, "}\n");
3544 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
3547 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
3549 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
3552 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3554 struct glsl_src_param src1_param;
3556 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3557 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
3558 src1_param.param_str, ins->src[0].reg.idx[0].offset);
3561 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3563 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3564 * function only suppresses the unhandled instruction warning
3568 /*********************************************
3569 * Pixel Shader Specific Code begins here
3570 ********************************************/
3571 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3573 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3574 ins->ctx->reg_maps->shader_version.minor);
3575 struct glsl_sample_function sample_function;
3576 DWORD sample_flags = 0;
3577 DWORD resource_idx;
3578 DWORD mask = 0, swizzle;
3579 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3581 /* 1.0-1.4: Use destination register as sampler source.
3582 * 2.0+: Use provided sampler source. */
3583 if (shader_version < WINED3D_SHADER_VERSION(2,0))
3584 resource_idx = ins->dst[0].reg.idx[0].offset;
3585 else
3586 resource_idx = ins->src[1].reg.idx[0].offset;
3588 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3590 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3591 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3592 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
3594 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3595 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3597 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3598 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3600 case WINED3D_TTFF_COUNT1:
3601 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3602 break;
3603 case WINED3D_TTFF_COUNT2:
3604 mask = WINED3DSP_WRITEMASK_1;
3605 break;
3606 case WINED3D_TTFF_COUNT3:
3607 mask = WINED3DSP_WRITEMASK_2;
3608 break;
3609 case WINED3D_TTFF_COUNT4:
3610 case WINED3D_TTFF_DISABLE:
3611 mask = WINED3DSP_WRITEMASK_3;
3612 break;
3616 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3618 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3620 if (src_mod == WINED3DSPSM_DZ) {
3621 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3622 mask = WINED3DSP_WRITEMASK_2;
3623 } else if (src_mod == WINED3DSPSM_DW) {
3624 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3625 mask = WINED3DSP_WRITEMASK_3;
3628 else
3630 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
3631 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3633 /* ps 2.0 texldp instruction always divides by the fourth component. */
3634 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3635 mask = WINED3DSP_WRITEMASK_3;
3639 if (priv->cur_ps_args->np2_fixup & (1 << resource_idx))
3640 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3642 shader_glsl_get_sample_function(ins->ctx, resource_idx, sample_flags, &sample_function);
3643 mask |= sample_function.coord_mask;
3645 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3646 else swizzle = ins->src[1].swizzle;
3648 /* 1.0-1.3: Use destination register as coordinate source.
3649 1.4+: Use provided coordinate source register. */
3650 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3652 char coord_mask[6];
3653 shader_glsl_write_mask_to_str(mask, coord_mask);
3654 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
3655 "T%u%s", resource_idx, coord_mask);
3657 else
3659 struct glsl_src_param coord_param;
3660 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3661 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3663 struct glsl_src_param bias;
3664 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3665 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3666 "%s", coord_param.param_str);
3667 } else {
3668 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
3669 "%s", coord_param.param_str);
3674 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3676 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3677 struct glsl_src_param coord_param, dx_param, dy_param;
3678 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3679 struct glsl_sample_function sample_function;
3680 DWORD sampler_idx;
3681 DWORD swizzle = ins->src[1].swizzle;
3682 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3684 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3686 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3687 shader_glsl_tex(ins);
3688 return;
3691 sampler_idx = ins->src[1].reg.idx[0].offset;
3692 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3693 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3695 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3696 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3697 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3698 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3700 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3701 "%s", coord_param.param_str);
3704 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3706 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3707 struct glsl_src_param coord_param, lod_param;
3708 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3709 struct glsl_sample_function sample_function;
3710 DWORD sampler_idx;
3711 DWORD swizzle = ins->src[1].swizzle;
3712 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3714 sampler_idx = ins->src[1].reg.idx[0].offset;
3715 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3716 && priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3717 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3719 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3720 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3722 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3724 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3725 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
3727 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
3728 * However, the NVIDIA drivers allow them in fragment shaders as well,
3729 * even without the appropriate extension. */
3730 WARN("Using %s in fragment shader.\n", sample_function.name);
3732 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3733 "%s", coord_param.param_str);
3736 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
3737 unsigned int resource_idx, unsigned int sampler_idx)
3739 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
3740 unsigned int i;
3742 for (i = 0; i < sampler_map->count; ++i)
3744 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
3745 return entries[i].bind_idx;
3748 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
3750 return ~0u;
3753 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
3755 struct glsl_sample_function sample_function;
3756 struct glsl_src_param coord_param;
3757 unsigned int sampler_idx;
3759 shader_glsl_get_sample_function(ins->ctx, ins->src[1].reg.idx[0].offset, 0, &sample_function);
3760 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3761 sampler_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
3762 ins->src[1].reg.idx[0].offset, ins->src[2].reg.idx[0].offset);
3763 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE,
3764 NULL, NULL, NULL, "%s", coord_param.param_str);
3767 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3769 /* FIXME: Make this work for more than just 2D textures */
3770 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3771 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3773 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3775 char dst_mask[6];
3777 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3778 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3779 ins->dst[0].reg.idx[0].offset, dst_mask);
3781 else
3783 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3784 DWORD reg = ins->src[0].reg.idx[0].offset;
3785 char dst_swizzle[6];
3787 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3789 if (src_mod == WINED3DSPSM_DZ)
3791 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3792 struct glsl_src_param div_param;
3794 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3796 if (mask_size > 1) {
3797 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3798 } else {
3799 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3802 else if (src_mod == WINED3DSPSM_DW)
3804 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3805 struct glsl_src_param div_param;
3807 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3809 if (mask_size > 1) {
3810 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3811 } else {
3812 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3814 } else {
3815 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3820 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3821 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3822 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3823 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3825 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3826 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3827 struct glsl_sample_function sample_function;
3828 struct glsl_src_param src0_param;
3829 UINT mask_size;
3831 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3833 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3834 * scalar, and projected sampling would require 4.
3836 * It is a dependent read - not valid with conditional NP2 textures
3838 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3839 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3841 switch(mask_size)
3843 case 1:
3844 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3845 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3846 break;
3848 case 2:
3849 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3850 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3851 break;
3853 case 3:
3854 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3855 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3856 break;
3858 default:
3859 FIXME("Unexpected mask size %u\n", mask_size);
3860 break;
3864 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3865 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3866 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3868 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3869 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3870 struct glsl_src_param src0_param;
3871 DWORD dst_mask;
3872 unsigned int mask_size;
3874 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3875 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3876 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3878 if (mask_size > 1) {
3879 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3880 } else {
3881 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3885 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3886 * Calculate the depth as dst.x / dst.y */
3887 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3889 struct glsl_dst_param dst_param;
3891 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3893 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3894 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3895 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3896 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3897 * >= 1.0 or < 0.0
3899 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3900 dst_param.reg_name, dst_param.reg_name);
3903 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3904 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3905 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3906 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3908 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3910 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3911 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3912 struct glsl_src_param src0_param;
3914 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3916 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3917 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3920 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3921 * Calculate the 1st of a 2-row matrix multiplication. */
3922 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3924 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3925 DWORD reg = ins->dst[0].reg.idx[0].offset;
3926 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3927 struct glsl_src_param src0_param;
3929 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3930 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3933 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3934 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3935 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3937 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3938 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3939 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3940 DWORD reg = ins->dst[0].reg.idx[0].offset;
3941 struct glsl_src_param src0_param;
3943 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3944 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
3945 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
3948 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3950 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3951 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3952 struct glsl_sample_function sample_function;
3953 DWORD reg = ins->dst[0].reg.idx[0].offset;
3954 struct glsl_src_param src0_param;
3956 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3957 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3959 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3961 /* Sample the texture using the calculated coordinates */
3962 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
3965 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3966 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3967 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3969 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3970 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3971 struct glsl_sample_function sample_function;
3972 DWORD reg = ins->dst[0].reg.idx[0].offset;
3973 struct glsl_src_param src0_param;
3975 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3976 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3978 /* Dependent read, not valid with conditional NP2 */
3979 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3981 /* Sample the texture using the calculated coordinates */
3982 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3984 tex_mx->current_row = 0;
3987 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3988 * Perform the 3rd row of a 3x3 matrix multiply */
3989 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
3991 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3992 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3993 DWORD reg = ins->dst[0].reg.idx[0].offset;
3994 struct glsl_src_param src0_param;
3995 char dst_mask[6];
3997 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3999 shader_glsl_append_dst(ins->ctx->buffer, ins);
4000 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4001 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
4003 tex_mx->current_row = 0;
4006 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4007 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4008 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
4010 struct glsl_src_param src0_param;
4011 struct glsl_src_param src1_param;
4012 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
4013 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4014 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4015 struct glsl_sample_function sample_function;
4016 DWORD reg = ins->dst[0].reg.idx[0].offset;
4017 char coord_mask[6];
4019 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4020 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4022 /* Perform the last matrix multiply operation */
4023 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4024 /* Reflection calculation */
4025 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
4027 /* Dependent read, not valid with conditional NP2 */
4028 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4029 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4031 /* Sample the texture */
4032 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4033 NULL, NULL, NULL, "tmp0%s", coord_mask);
4035 tex_mx->current_row = 0;
4038 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4039 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4040 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
4042 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
4043 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4044 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4045 struct glsl_sample_function sample_function;
4046 DWORD reg = ins->dst[0].reg.idx[0].offset;
4047 struct glsl_src_param src0_param;
4048 char coord_mask[6];
4050 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4052 /* Perform the last matrix multiply operation */
4053 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
4055 /* Construct the eye-ray vector from w coordinates */
4056 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
4057 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
4058 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4060 /* Dependent read, not valid with conditional NP2 */
4061 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4062 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4064 /* Sample the texture using the calculated coordinates */
4065 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4066 NULL, NULL, NULL, "tmp0%s", coord_mask);
4068 tex_mx->current_row = 0;
4071 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4072 * Apply a fake bump map transform.
4073 * texbem is pshader <= 1.3 only, this saves a few version checks
4075 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
4077 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4078 struct glsl_sample_function sample_function;
4079 struct glsl_src_param coord_param;
4080 DWORD sampler_idx;
4081 DWORD mask;
4082 DWORD flags;
4083 char coord_mask[6];
4085 sampler_idx = ins->dst[0].reg.idx[0].offset;
4086 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4087 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4089 /* Dependent read, not valid with conditional NP2 */
4090 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4091 mask = sample_function.coord_mask;
4093 shader_glsl_write_mask_to_str(mask, coord_mask);
4095 /* With projected textures, texbem only divides the static texture coord,
4096 * not the displacement, so we can't let GL handle this. */
4097 if (flags & WINED3D_PSARGS_PROJECTED)
4099 DWORD div_mask=0;
4100 char coord_div_mask[3];
4101 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4103 case WINED3D_TTFF_COUNT1:
4104 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4105 break;
4106 case WINED3D_TTFF_COUNT2:
4107 div_mask = WINED3DSP_WRITEMASK_1;
4108 break;
4109 case WINED3D_TTFF_COUNT3:
4110 div_mask = WINED3DSP_WRITEMASK_2;
4111 break;
4112 case WINED3D_TTFF_COUNT4:
4113 case WINED3D_TTFF_DISABLE:
4114 div_mask = WINED3DSP_WRITEMASK_3;
4115 break;
4117 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4118 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4121 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4123 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4124 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4125 coord_param.param_str, coord_mask);
4127 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4129 struct glsl_src_param luminance_param;
4130 struct glsl_dst_param dst_param;
4132 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4133 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4135 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4136 dst_param.reg_name, dst_param.mask_str,
4137 luminance_param.param_str, sampler_idx, sampler_idx);
4141 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4143 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4144 struct glsl_src_param src0_param, src1_param;
4146 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4147 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4149 shader_glsl_append_dst(ins->ctx->buffer, ins);
4150 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4151 src0_param.param_str, sampler_idx, src1_param.param_str);
4154 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4155 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4156 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4158 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4159 struct glsl_sample_function sample_function;
4160 struct glsl_src_param src0_param;
4162 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4164 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4165 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4166 "%s.wx", src0_param.reg_name);
4169 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4170 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4171 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4173 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4174 struct glsl_sample_function sample_function;
4175 struct glsl_src_param src0_param;
4177 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4179 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4180 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4181 "%s.yz", src0_param.reg_name);
4184 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4185 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4186 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4188 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4189 struct glsl_sample_function sample_function;
4190 struct glsl_src_param src0_param;
4192 /* Dependent read, not valid with conditional NP2 */
4193 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4194 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4196 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4197 "%s", src0_param.param_str);
4200 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4201 * If any of the first 3 components are < 0, discard this pixel */
4202 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4204 struct glsl_dst_param dst_param;
4206 /* The argument is a destination parameter, and no writemasks are allowed */
4207 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4208 if (ins->ctx->reg_maps->shader_version.major >= 2)
4210 if (ins->ctx->reg_maps->shader_version.major >= 4)
4211 FIXME("SM4 discard not implemented.\n");
4212 /* 2.0 shaders compare all 4 components in texkill */
4213 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4214 } else {
4215 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
4216 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
4217 * 4 components are defined, only the first 3 are used
4219 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4223 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4224 * dst = dot2(src0, src1) + src2 */
4225 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4227 struct glsl_src_param src0_param;
4228 struct glsl_src_param src1_param;
4229 struct glsl_src_param src2_param;
4230 DWORD write_mask;
4231 unsigned int mask_size;
4233 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4234 mask_size = shader_glsl_get_write_mask_size(write_mask);
4236 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4237 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4238 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4240 if (mask_size > 1) {
4241 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4242 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4243 } else {
4244 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4245 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4249 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
4250 const struct wined3d_shader_signature *input_signature,
4251 const struct wined3d_shader_reg_maps *reg_maps,
4252 enum vertexprocessing_mode vertexprocessing)
4254 unsigned int i;
4256 for (i = 0; i < input_signature->element_count; ++i)
4258 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4259 const char *semantic_name;
4260 UINT semantic_idx;
4261 char reg_mask[6];
4263 /* Unused */
4264 if (!(reg_maps->input_registers & (1 << input->register_idx)))
4265 continue;
4267 semantic_name = input->semantic_name;
4268 semantic_idx = input->semantic_idx;
4269 shader_glsl_write_mask_to_str(input->mask, reg_mask);
4271 if (vertexprocessing == vertexshader)
4273 if (input->sysval_semantic == WINED3D_SV_POSITION)
4274 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
4275 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4276 else
4277 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
4278 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
4279 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
4281 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4283 if (semantic_idx < 8 && vertexprocessing == pretransformed)
4284 shader_addline(buffer, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4285 shader->u.ps.input_reg_map[input->register_idx], reg_mask, semantic_idx, reg_mask);
4286 else
4287 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4288 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4290 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4292 if (!semantic_idx)
4293 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4294 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4295 else if (semantic_idx == 1)
4296 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4297 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4298 else
4299 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4300 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4302 else
4304 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4305 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4310 /*********************************************
4311 * Vertex Shader Specific Code begins here
4312 ********************************************/
4314 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4316 struct glsl_program_key key;
4318 key.vs_id = entry->vs.id;
4319 key.gs_id = entry->gs.id;
4320 key.ps_id = entry->ps.id;
4322 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4324 ERR("Failed to insert program entry.\n");
4328 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4329 GLuint vs_id, GLuint gs_id, GLuint ps_id)
4331 struct wine_rb_entry *entry;
4332 struct glsl_program_key key;
4334 key.vs_id = vs_id;
4335 key.gs_id = gs_id;
4336 key.ps_id = ps_id;
4338 entry = wine_rb_get(&priv->program_lookup, &key);
4339 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4342 /* Context activation is done by the caller. */
4343 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4344 struct glsl_shader_prog_link *entry)
4346 struct glsl_program_key key;
4348 key.vs_id = entry->vs.id;
4349 key.gs_id = entry->gs.id;
4350 key.ps_id = entry->ps.id;
4351 wine_rb_remove(&priv->program_lookup, &key);
4353 GL_EXTCALL(glDeleteProgram(entry->id));
4354 if (entry->vs.id)
4355 list_remove(&entry->vs.shader_entry);
4356 if (entry->gs.id)
4357 list_remove(&entry->gs.shader_entry);
4358 if (entry->ps.id)
4359 list_remove(&entry->ps.shader_entry);
4360 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4361 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4362 HeapFree(GetProcessHeap(), 0, entry);
4365 static void handle_ps3_input(struct wined3d_shader_buffer *buffer,
4366 const struct wined3d_gl_info *gl_info, const DWORD *map,
4367 const struct wined3d_shader_signature *input_signature,
4368 const struct wined3d_shader_reg_maps *reg_maps_in,
4369 const struct wined3d_shader_signature *output_signature,
4370 const struct wined3d_shader_reg_maps *reg_maps_out)
4372 unsigned int i, j;
4373 DWORD *set;
4374 DWORD in_idx;
4375 unsigned int in_count = vec4_varyings(3, gl_info);
4376 char reg_mask[6];
4377 char destination[50];
4379 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
4381 for (i = 0; i < input_signature->element_count; ++i)
4383 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4385 if (!(reg_maps_in->input_registers & (1 << input->register_idx)))
4386 continue;
4388 in_idx = map[input->register_idx];
4389 /* Declared, but not read register */
4390 if (in_idx == ~0u)
4391 continue;
4392 if (in_idx >= (in_count + 2))
4394 FIXME("More input varyings declared than supported, expect issues.\n");
4395 continue;
4398 if (in_idx == in_count)
4399 sprintf(destination, "gl_FrontColor");
4400 else if (in_idx == in_count + 1)
4401 sprintf(destination, "gl_FrontSecondaryColor");
4402 else
4403 sprintf(destination, "ps_link[%u]", in_idx);
4405 if (!set[in_idx])
4406 set[in_idx] = ~0u;
4408 for (j = 0; j < output_signature->element_count; ++j)
4410 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
4411 DWORD mask;
4413 if (!(reg_maps_out->output_registers & (1 << output->register_idx))
4414 || input->semantic_idx != output->semantic_idx
4415 || strcmp(input->semantic_name, output->semantic_name)
4416 || !(mask = input->mask & output->mask))
4417 continue;
4419 if (set[in_idx] == ~0u)
4420 set[in_idx] = mask;
4421 else
4422 set[in_idx] |= mask;
4423 shader_glsl_write_mask_to_str(mask, reg_mask);
4425 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4426 destination, reg_mask, output->register_idx, reg_mask);
4430 for (i = 0; i < in_count + 2; ++i)
4432 unsigned int size;
4434 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4435 continue;
4437 if (set[i] == ~0U) set[i] = 0;
4439 size = 0;
4440 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4441 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4442 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4443 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4444 reg_mask[size] = '\0';
4446 if (i == in_count)
4447 sprintf(destination, "gl_FrontColor");
4448 else if (i == in_count + 1)
4449 sprintf(destination, "gl_FrontSecondaryColor");
4450 else
4451 sprintf(destination, "ps_link[%u]", i);
4453 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
4454 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
4457 HeapFree(GetProcessHeap(), 0, set);
4460 /* Context activation is done by the caller. */
4461 static GLuint generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
4462 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4463 const struct wined3d_gl_info *gl_info)
4465 GLuint ret = 0;
4466 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4467 unsigned int i;
4468 const char *semantic_name;
4469 UINT semantic_idx;
4470 char reg_mask[6];
4472 shader_buffer_clear(buffer);
4474 shader_addline(buffer, "#version 120\n");
4476 if (ps_major < 3)
4478 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4480 for (i = 0; i < vs->output_signature.element_count; ++i)
4482 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
4483 DWORD write_mask;
4485 if (!(vs->reg_maps.output_registers & (1 << output->register_idx)))
4486 continue;
4488 semantic_name = output->semantic_name;
4489 semantic_idx = output->semantic_idx;
4490 write_mask = output->mask;
4491 shader_glsl_write_mask_to_str(write_mask, reg_mask);
4493 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4495 if (!semantic_idx)
4496 shader_addline(buffer, "gl_FrontColor%s = vs_out[%u]%s;\n",
4497 reg_mask, output->register_idx, reg_mask);
4498 else if (semantic_idx == 1)
4499 shader_addline(buffer, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4500 reg_mask, output->register_idx, reg_mask);
4502 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
4504 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4505 reg_mask, output->register_idx, reg_mask);
4507 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4509 if (semantic_idx < 8)
4511 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
4512 write_mask |= WINED3DSP_WRITEMASK_3;
4514 shader_addline(buffer, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4515 semantic_idx, reg_mask, output->register_idx, reg_mask);
4516 if (!(write_mask & WINED3DSP_WRITEMASK_3))
4517 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
4520 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4522 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", output->register_idx, reg_mask[1]);
4524 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
4526 shader_addline(buffer, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n",
4527 output->register_idx, reg_mask[1]);
4530 shader_addline(buffer, "}\n");
4532 else
4534 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
4535 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4536 shader_addline(buffer, "varying vec4 ps_link[%u];\n", in_count);
4537 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4539 /* First, sort out position and point size. Those are not passed to the pixel shader */
4540 for (i = 0; i < vs->output_signature.element_count; ++i)
4542 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
4544 if (!(vs->reg_maps.output_registers & (1 << output->register_idx)))
4545 continue;
4547 semantic_name = output->semantic_name;
4548 semantic_idx = output->semantic_idx;
4549 shader_glsl_write_mask_to_str(output->mask, reg_mask);
4551 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
4553 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4554 reg_mask, output->register_idx, reg_mask);
4556 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4558 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", output->register_idx, reg_mask[1]);
4562 /* Then, fix the pixel shader input */
4563 handle_ps3_input(buffer, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
4564 &ps->reg_maps, &vs->output_signature, &vs->reg_maps);
4566 shader_addline(buffer, "}\n");
4569 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
4570 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
4571 shader_glsl_compile(gl_info, ret, buffer->buffer);
4573 return ret;
4576 static void shader_glsl_generate_srgb_write_correction(struct wined3d_shader_buffer *buffer)
4578 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4579 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4580 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4581 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4582 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4583 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4586 static void shader_glsl_generate_fog_code(struct wined3d_shader_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
4588 switch (mode)
4590 case WINED3D_FFP_PS_FOG_OFF:
4591 return;
4593 case WINED3D_FFP_PS_FOG_LINEAR:
4594 shader_addline(buffer, "float Fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;\n");
4595 break;
4597 case WINED3D_FFP_PS_FOG_EXP:
4598 /* Fog = e^-(gl_Fog.density * gl_FogFragCoord) */
4599 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4600 break;
4602 case WINED3D_FFP_PS_FOG_EXP2:
4603 /* Fog = e^-((gl_Fog.density * gl_FogFragCoord)^2) */
4604 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4605 break;
4607 default:
4608 ERR("Invalid fog mode %#x.\n", mode);
4609 return;
4612 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, clamp(Fog, 0.0, 1.0));\n");
4615 /* Context activation is done by the caller. */
4616 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
4617 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4618 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
4620 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4621 const struct wined3d_gl_info *gl_info = context->gl_info;
4622 const DWORD *function = shader->function;
4623 struct shader_glsl_ctx_priv priv_ctx;
4625 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4626 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
4628 memset(&priv_ctx, 0, sizeof(priv_ctx));
4629 priv_ctx.cur_ps_args = args;
4630 priv_ctx.cur_np2fixup_info = np2fixup_info;
4632 shader_addline(buffer, "#version 120\n");
4634 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4635 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4636 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4637 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
4638 /* The spec says that it doesn't have to be explicitly enabled, but the
4639 * nvidia drivers write a warning if we don't do so. */
4640 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4641 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4642 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4643 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4644 if (gl_info->supported[EXT_GPU_SHADER4])
4645 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4647 /* Base Declarations */
4648 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4650 /* Pack 3.0 inputs */
4651 if (reg_maps->shader_version.major >= 3)
4652 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args->vp_mode);
4654 /* Base Shader Body */
4655 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4657 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4658 if (reg_maps->shader_version.major < 2)
4660 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4661 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4664 if (args->srgb_correction)
4665 shader_glsl_generate_srgb_write_correction(buffer);
4667 /* SM < 3 does not replace the fog stage. */
4668 if (reg_maps->shader_version.major < 3)
4669 shader_glsl_generate_fog_code(buffer, args->fog);
4671 shader_addline(buffer, "}\n");
4673 TRACE("Compiling shader object %u.\n", shader_id);
4674 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4676 return shader_id;
4679 /* Context activation is done by the caller. */
4680 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4681 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4682 const struct vs_compile_args *args)
4684 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4685 const struct wined3d_gl_info *gl_info = context->gl_info;
4686 const DWORD *function = shader->function;
4687 struct shader_glsl_ctx_priv priv_ctx;
4689 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4690 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
4692 shader_addline(buffer, "#version 120\n");
4694 if (gl_info->supported[ARB_DRAW_INSTANCED])
4695 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
4696 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4697 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4698 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4699 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4700 if (gl_info->supported[EXT_GPU_SHADER4])
4701 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4703 memset(&priv_ctx, 0, sizeof(priv_ctx));
4704 priv_ctx.cur_vs_args = args;
4706 /* Base Declarations */
4707 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4709 /* Base Shader Body */
4710 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4712 /* Unpack outputs */
4713 shader_addline(buffer, "order_ps_input(vs_out);\n");
4715 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4716 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4717 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4718 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4720 if (args->fog_src == VS_FOG_Z)
4721 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4722 else if (!reg_maps->fog)
4723 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4725 /* We always store the clipplanes without y inversion */
4726 if (args->clip_enabled)
4727 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4729 /* Write the final position.
4731 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4732 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4733 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4734 * contains 1.0 to allow a mad.
4736 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4737 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4739 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4741 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4742 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4743 * which is the same as z = z * 2 - w.
4745 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4747 shader_addline(buffer, "}\n");
4749 TRACE("Compiling shader object %u.\n", shader_id);
4750 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4752 return shader_id;
4755 /* Context activation is done by the caller. */
4756 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
4757 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader)
4759 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4760 const struct wined3d_gl_info *gl_info = context->gl_info;
4761 const DWORD *function = shader->function;
4762 struct shader_glsl_ctx_priv priv_ctx;
4763 GLuint shader_id;
4765 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
4767 shader_addline(buffer, "#version 120\n");
4769 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
4770 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
4771 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4772 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4773 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4774 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4775 if (gl_info->supported[EXT_GPU_SHADER4])
4776 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4778 memset(&priv_ctx, 0, sizeof(priv_ctx));
4779 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4780 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4781 shader_addline(buffer, "}\n");
4783 TRACE("Compiling shader object %u.\n", shader_id);
4784 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4786 return shader_id;
4789 static GLuint find_glsl_pshader(const struct wined3d_context *context,
4790 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4791 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
4793 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
4794 struct glsl_shader_private *shader_data;
4795 struct ps_np2fixup_info *np2fixup;
4796 UINT i;
4797 DWORD new_size;
4798 GLuint ret;
4800 if (!shader->backend_data)
4802 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4803 if (!shader->backend_data)
4805 ERR("Failed to allocate backend data.\n");
4806 return 0;
4809 shader_data = shader->backend_data;
4810 gl_shaders = shader_data->gl_shaders.ps;
4812 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4813 * so a linear search is more performant than a hashmap or a binary search
4814 * (cache coherency etc)
4816 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4818 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
4820 if (args->np2_fixup)
4821 *np2fixup_info = &gl_shaders[i].np2fixup;
4822 return gl_shaders[i].id;
4826 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4827 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4828 if (shader_data->num_gl_shaders)
4830 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4831 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
4832 new_size * sizeof(*gl_shaders));
4834 else
4836 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4837 new_size = 1;
4840 if(!new_array) {
4841 ERR("Out of memory\n");
4842 return 0;
4844 shader_data->gl_shaders.ps = new_array;
4845 shader_data->shader_array_size = new_size;
4846 gl_shaders = new_array;
4849 gl_shaders[shader_data->num_gl_shaders].args = *args;
4851 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
4852 memset(np2fixup, 0, sizeof(*np2fixup));
4853 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
4855 pixelshader_update_resource_types(shader, args->tex_types);
4857 shader_buffer_clear(buffer);
4858 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4859 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4861 return ret;
4864 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4865 const DWORD use_map) {
4866 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4867 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4868 return stored->fog_src == new->fog_src;
4871 static GLuint find_glsl_vshader(const struct wined3d_context *context,
4872 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4873 const struct vs_compile_args *args)
4875 UINT i;
4876 DWORD new_size;
4877 DWORD use_map = context->stream_info.use_map;
4878 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
4879 struct glsl_shader_private *shader_data;
4880 GLuint ret;
4882 if (!shader->backend_data)
4884 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4885 if (!shader->backend_data)
4887 ERR("Failed to allocate backend data.\n");
4888 return 0;
4891 shader_data = shader->backend_data;
4892 gl_shaders = shader_data->gl_shaders.vs;
4894 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4895 * so a linear search is more performant than a hashmap or a binary search
4896 * (cache coherency etc)
4898 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4900 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
4901 return gl_shaders[i].id;
4904 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4906 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4907 if (shader_data->num_gl_shaders)
4909 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4910 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
4911 new_size * sizeof(*gl_shaders));
4913 else
4915 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4916 new_size = 1;
4919 if(!new_array) {
4920 ERR("Out of memory\n");
4921 return 0;
4923 shader_data->gl_shaders.vs = new_array;
4924 shader_data->shader_array_size = new_size;
4925 gl_shaders = new_array;
4928 gl_shaders[shader_data->num_gl_shaders].args = *args;
4930 shader_buffer_clear(buffer);
4931 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4932 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4934 return ret;
4937 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
4938 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader)
4940 struct glsl_gs_compiled_shader *gl_shaders;
4941 struct glsl_shader_private *shader_data;
4942 GLuint ret;
4944 if (!shader->backend_data)
4946 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
4948 ERR("Failed to allocate backend data.\n");
4949 return 0;
4952 shader_data = shader->backend_data;
4953 gl_shaders = shader_data->gl_shaders.gs;
4955 if (shader_data->num_gl_shaders)
4956 return gl_shaders[0].id;
4958 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4960 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
4962 ERR("Failed to allocate GL shader array.\n");
4963 return 0;
4965 shader_data->shader_array_size = 1;
4966 gl_shaders = shader_data->gl_shaders.gs;
4968 shader_buffer_clear(buffer);
4969 ret = shader_glsl_generate_geometry_shader(context, buffer, shader);
4970 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4972 return ret;
4975 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
4977 switch (mcs)
4979 case WINED3D_MCS_MATERIAL:
4980 return material;
4981 case WINED3D_MCS_COLOR1:
4982 return "gl_Color";
4983 case WINED3D_MCS_COLOR2:
4984 return "gl_SecondaryColor";
4985 default:
4986 ERR("Invalid material color source %#x.\n", mcs);
4987 return "<invalid>";
4991 static void shader_glsl_ffp_vertex_lighting(struct wined3d_shader_buffer *buffer,
4992 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4994 const char *diffuse, *specular, *emission, *ambient;
4995 enum wined3d_light_type light_type;
4996 unsigned int i;
4998 if (!settings->lighting)
5000 shader_addline(buffer, "gl_FrontColor = gl_Color;\n");
5001 shader_addline(buffer, "gl_FrontSecondaryColor = gl_SecondaryColor;\n");
5002 return;
5005 shader_addline(buffer, "vec3 ambient = gl_LightModel.ambient.xyz;\n");
5006 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
5007 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
5008 shader_addline(buffer, "vec3 dir, dst;\n");
5009 shader_addline(buffer, "float att, t;\n");
5011 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "gl_FrontMaterial.ambient");
5012 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "gl_FrontMaterial.diffuse");
5013 specular = shader_glsl_ffp_mcs(settings->specular_source, "gl_FrontMaterial.specular");
5014 emission = shader_glsl_ffp_mcs(settings->emission_source, "gl_FrontMaterial.emission");
5016 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
5018 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
5019 switch (light_type)
5021 case WINED3D_LIGHT_POINT:
5022 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
5023 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5024 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5025 shader_addline(buffer, "dst.x = 1.0;\n");
5026 shader_addline(buffer, "att = dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
5027 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n", i, i, i);
5028 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz / att;\n", i);
5029 if (!settings->normal)
5030 break;
5031 shader_addline(buffer, "dir = normalize(dir);\n");
5032 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5033 " * gl_LightSource[%u].diffuse.xyz) / att;\n", i);
5034 if (settings->localviewer)
5035 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5036 else
5037 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
5038 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
5039 " * gl_LightSource[%u].specular) / att;\n", i);
5040 break;
5042 case WINED3D_LIGHT_SPOT:
5043 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
5044 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5045 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5046 shader_addline(buffer, "dst.x = 1.0;\n");
5047 shader_addline(buffer, "dir = normalize(dir);\n");
5048 shader_addline(buffer, "t = dot(-dir, normalize(gl_LightSource[%u].spotDirection));\n", i);
5049 shader_addline(buffer, "if (t < gl_LightSource[%u].spotCosCutoff) att = 0.0;\n", i);
5050 shader_addline(buffer, "else att = pow(t, gl_LightSource[%u].spotExponent)"
5051 " / dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
5052 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n",
5053 i, i, i, i);
5054 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz * att;\n", i);
5055 if (!settings->normal)
5056 break;
5057 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5058 " * gl_LightSource[%u].diffuse.xyz) * att;\n", i);
5059 if (settings->localviewer)
5060 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5061 else
5062 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
5063 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
5064 " * gl_LightSource[%u].specular) * att;\n", i);
5065 break;
5067 case WINED3D_LIGHT_DIRECTIONAL:
5068 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz;\n", i);
5069 if (!settings->normal)
5070 break;
5071 shader_addline(buffer, "dir = normalize(gl_LightSource[%u].position.xyz);\n", i);
5072 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5073 " * gl_LightSource[%u].diffuse.xyz;\n", i);
5074 shader_addline(buffer, "t = dot(normal, gl_LightSource[%u].halfVector.xyz);\n", i);
5075 shader_addline(buffer, "if (t > 0.0) specular += pow(t, gl_FrontMaterial.shininess)"
5076 " * gl_LightSource[%u].specular;\n", i);
5077 break;
5079 default:
5080 if (light_type)
5081 FIXME("Unhandled light type %#x.\n", light_type);
5082 continue;
5086 shader_addline(buffer, "gl_FrontColor.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
5087 ambient, diffuse, emission);
5088 shader_addline(buffer, "gl_FrontColor.w = %s.w;\n", diffuse);
5089 shader_addline(buffer, "gl_FrontSecondaryColor = %s * specular;\n", specular);
5092 /* Context activation is done by the caller. */
5093 static GLuint shader_glsl_generate_ffp_vertex_shader(struct wined3d_shader_buffer *buffer,
5094 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
5096 GLuint shader_obj;
5097 unsigned int i;
5099 shader_buffer_clear(buffer);
5101 shader_addline(buffer, "#version 120\n");
5102 shader_addline(buffer, "\n");
5104 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix;\n");
5105 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
5106 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
5108 shader_addline(buffer, "\nvoid main()\n{\n");
5109 shader_addline(buffer, "float m;\n");
5110 shader_addline(buffer, "vec3 r;\n");
5112 if (settings->transformed)
5114 shader_addline(buffer, "vec4 ec_pos = vec4(gl_Vertex.xyz, 1.0);\n");
5115 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5116 shader_addline(buffer, "if (gl_Vertex.w != 0.0) gl_Position /= gl_Vertex.w;\n");
5118 else
5120 shader_addline(buffer, "vec4 ec_pos = ffp_modelview_matrix * gl_Vertex;\n");
5121 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5122 if (settings->clipping)
5123 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
5124 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
5127 if (!settings->normal)
5128 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
5129 else if (settings->normalize)
5130 shader_addline(buffer, "vec3 normal = normalize(ffp_normal_matrix * gl_Normal);\n");
5131 else
5132 shader_addline(buffer, "vec3 normal = ffp_normal_matrix * gl_Normal;\n");
5134 shader_glsl_ffp_vertex_lighting(buffer, settings, gl_info);
5136 for (i = 0; i < MAX_TEXTURES; ++i)
5138 switch (settings->texgen[i] << WINED3D_FFP_TCI_SHIFT)
5140 case WINED3DTSS_TCI_PASSTHRU:
5141 if (settings->texcoords & (1 << i))
5142 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * gl_MultiTexCoord%d;\n",
5143 i, i, i);
5144 break;
5146 case WINED3DTSS_TCI_CAMERASPACENORMAL:
5147 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * vec4(normal, 1.0);\n", i, i);
5148 break;
5150 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
5151 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * ec_pos;\n", i, i);
5152 break;
5154 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
5155 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
5156 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
5157 break;
5159 case WINED3DTSS_TCI_SPHEREMAP:
5160 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
5161 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
5162 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
5163 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
5164 break;
5166 default:
5167 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
5168 break;
5172 switch (settings->fog_mode)
5174 case WINED3D_FFP_VS_FOG_OFF:
5175 break;
5177 case WINED3D_FFP_VS_FOG_FOGCOORD:
5178 shader_addline(buffer, "gl_FogFragCoord = gl_SecondaryColor.w * 255.0;\n");
5179 break;
5181 case WINED3D_FFP_VS_FOG_RANGE:
5182 shader_addline(buffer, "gl_FogFragCoord = length(ec_pos.xyz);\n");
5183 break;
5185 case WINED3D_FFP_VS_FOG_DEPTH:
5186 if (settings->ortho_fog)
5187 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
5188 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z * 0.5 + 0.5;\n");
5189 else if (settings->transformed)
5190 shader_addline(buffer, "gl_FogFragCoord = ec_pos.z;\n");
5191 else
5192 shader_addline(buffer, "gl_FogFragCoord = abs(ec_pos.z);\n");
5193 break;
5195 default:
5196 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
5197 break;
5200 if (settings->point_size)
5202 shader_addline(buffer, "gl_PointSize = gl_Point.size / sqrt(gl_Point.distanceConstantAttenuation"
5203 " + gl_Point.distanceLinearAttenuation * length(ec_pos.xyz)"
5204 " + gl_Point.distanceQuadraticAttenuation * dot(ec_pos.xyz, ec_pos.xyz));\n");
5205 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, gl_Point.sizeMin, gl_Point.sizeMax);\n");
5208 shader_addline(buffer, "}\n");
5210 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5211 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5213 return shader_obj;
5216 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_shader_buffer *buffer,
5217 DWORD argnum, unsigned int stage, DWORD arg)
5219 const char *ret;
5221 if (arg == ARG_UNUSED)
5222 return "<unused arg>";
5224 switch (arg & WINED3DTA_SELECTMASK)
5226 case WINED3DTA_DIFFUSE:
5227 ret = "gl_Color";
5228 break;
5230 case WINED3DTA_CURRENT:
5231 if (!stage)
5232 ret = "gl_Color";
5233 else
5234 ret = "ret";
5235 break;
5237 case WINED3DTA_TEXTURE:
5238 switch (stage)
5240 case 0: ret = "tex0"; break;
5241 case 1: ret = "tex1"; break;
5242 case 2: ret = "tex2"; break;
5243 case 3: ret = "tex3"; break;
5244 case 4: ret = "tex4"; break;
5245 case 5: ret = "tex5"; break;
5246 case 6: ret = "tex6"; break;
5247 case 7: ret = "tex7"; break;
5248 default:
5249 ret = "<invalid texture>";
5250 break;
5252 break;
5254 case WINED3DTA_TFACTOR:
5255 ret = "tex_factor";
5256 break;
5258 case WINED3DTA_SPECULAR:
5259 ret = "gl_SecondaryColor";
5260 break;
5262 case WINED3DTA_TEMP:
5263 ret = "temp_reg";
5264 break;
5266 case WINED3DTA_CONSTANT:
5267 switch (stage)
5269 case 0: ret = "tss_const0"; break;
5270 case 1: ret = "tss_const1"; break;
5271 case 2: ret = "tss_const2"; break;
5272 case 3: ret = "tss_const3"; break;
5273 case 4: ret = "tss_const4"; break;
5274 case 5: ret = "tss_const5"; break;
5275 case 6: ret = "tss_const6"; break;
5276 case 7: ret = "tss_const7"; break;
5277 default:
5278 ret = "<invalid constant>";
5279 break;
5281 break;
5283 default:
5284 return "<unhandled arg>";
5287 if (arg & WINED3DTA_COMPLEMENT)
5289 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
5290 if (argnum == 0)
5291 ret = "arg0";
5292 else if (argnum == 1)
5293 ret = "arg1";
5294 else if (argnum == 2)
5295 ret = "arg2";
5298 if (arg & WINED3DTA_ALPHAREPLICATE)
5300 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
5301 if (argnum == 0)
5302 ret = "arg0";
5303 else if (argnum == 1)
5304 ret = "arg1";
5305 else if (argnum == 2)
5306 ret = "arg2";
5309 return ret;
5312 static void shader_glsl_ffp_fragment_op(struct wined3d_shader_buffer *buffer, unsigned int stage, BOOL color,
5313 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
5315 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5317 if (color && alpha)
5318 dstmask = "";
5319 else if (color)
5320 dstmask = ".xyz";
5321 else
5322 dstmask = ".w";
5324 if (dst == tempreg)
5325 dstreg = "temp_reg";
5326 else
5327 dstreg = "ret";
5329 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
5330 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
5331 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
5333 switch (op)
5335 case WINED3D_TOP_DISABLE:
5336 if (!stage)
5337 shader_addline(buffer, "%s%s = gl_Color%s;\n", dstreg, dstmask, dstmask);
5338 break;
5340 case WINED3D_TOP_SELECT_ARG1:
5341 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
5342 break;
5344 case WINED3D_TOP_SELECT_ARG2:
5345 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
5346 break;
5348 case WINED3D_TOP_MODULATE:
5349 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5350 break;
5352 case WINED3D_TOP_MODULATE_4X:
5353 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
5354 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5355 break;
5357 case WINED3D_TOP_MODULATE_2X:
5358 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
5359 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5360 break;
5362 case WINED3D_TOP_ADD:
5363 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
5364 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5365 break;
5367 case WINED3D_TOP_ADD_SIGNED:
5368 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
5369 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5370 break;
5372 case WINED3D_TOP_ADD_SIGNED_2X:
5373 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
5374 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5375 break;
5377 case WINED3D_TOP_SUBTRACT:
5378 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
5379 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5380 break;
5382 case WINED3D_TOP_ADD_SMOOTH:
5383 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
5384 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
5385 break;
5387 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
5388 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5389 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5390 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5391 break;
5393 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5394 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5395 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5396 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5397 break;
5399 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5400 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
5401 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5402 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5403 break;
5405 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5406 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5407 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5408 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
5409 break;
5411 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
5412 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
5413 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5414 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5415 break;
5417 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
5418 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
5419 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5420 break;
5422 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
5423 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
5424 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5425 break;
5427 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
5428 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5429 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5430 break;
5431 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
5432 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
5433 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5434 break;
5436 case WINED3D_TOP_BUMPENVMAP:
5437 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5438 /* These are handled in the first pass, nothing to do. */
5439 break;
5441 case WINED3D_TOP_DOTPRODUCT3:
5442 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
5443 dstreg, dstmask, arg1, arg2, dstmask);
5444 break;
5446 case WINED3D_TOP_MULTIPLY_ADD:
5447 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
5448 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
5449 break;
5451 case WINED3D_TOP_LERP:
5452 /* MSDN isn't quite right here. */
5453 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
5454 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
5455 break;
5457 default:
5458 FIXME("Unhandled operation %#x.\n", op);
5459 break;
5463 /* Context activation is done by the caller. */
5464 static GLuint shader_glsl_generate_ffp_fragment_shader(struct wined3d_shader_buffer *buffer,
5465 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
5467 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
5468 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
5469 const char *final_combiner_src = "ret";
5470 UINT lowest_disabled_stage;
5471 GLuint shader_id;
5472 DWORD arg0, arg1, arg2;
5473 unsigned int stage;
5475 shader_buffer_clear(buffer);
5477 /* Find out which textures are read */
5478 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5480 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5481 break;
5483 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
5484 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
5485 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
5487 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5488 tex_map |= 1 << stage;
5489 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5490 tfactor_used = TRUE;
5491 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5492 tempreg_used = TRUE;
5493 if (settings->op[stage].dst == tempreg)
5494 tempreg_used = TRUE;
5495 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5496 tss_const_map |= 1 << stage;
5498 switch (settings->op[stage].cop)
5500 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5501 lum_map |= 1 << stage;
5502 /* fall through */
5503 case WINED3D_TOP_BUMPENVMAP:
5504 bump_map |= 1 << stage;
5505 /* fall through */
5506 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5507 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5508 tex_map |= 1 << stage;
5509 break;
5511 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5512 tfactor_used = TRUE;
5513 break;
5515 default:
5516 break;
5519 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5520 continue;
5522 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
5523 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
5524 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
5526 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5527 tex_map |= 1 << stage;
5528 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5529 tfactor_used = TRUE;
5530 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5531 tempreg_used = TRUE;
5532 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5533 tss_const_map |= 1 << stage;
5535 lowest_disabled_stage = stage;
5537 shader_addline(buffer, "#version 120\n");
5539 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5540 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5542 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
5543 shader_addline(buffer, "vec4 ret;\n");
5544 if (tempreg_used || settings->sRGB_write)
5545 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
5546 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
5548 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5550 if (tss_const_map & (1 << stage))
5551 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
5553 if (!(tex_map & (1 << stage)))
5554 continue;
5556 switch (settings->op[stage].tex_type)
5558 case tex_1d:
5559 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
5560 break;
5561 case tex_2d:
5562 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
5563 break;
5564 case tex_3d:
5565 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
5566 break;
5567 case tex_cube:
5568 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
5569 break;
5570 case tex_rect:
5571 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
5572 break;
5573 default:
5574 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
5575 break;
5578 shader_addline(buffer, "vec4 tex%u;\n", stage);
5580 if (!(bump_map & (1 << stage)))
5581 continue;
5582 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
5584 if (!(lum_map & (1 << stage)))
5585 continue;
5586 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
5587 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
5589 if (tfactor_used)
5590 shader_addline(buffer, "uniform vec4 tex_factor;\n");
5591 shader_addline(buffer, "uniform vec4 specular_enable;\n");
5593 if (settings->sRGB_write)
5595 shader_addline(buffer, "const vec4 srgb_const0 = ");
5596 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
5597 shader_addline(buffer, ";\n");
5598 shader_addline(buffer, "const vec4 srgb_const1 = ");
5599 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
5600 shader_addline(buffer, ";\n");
5603 shader_addline(buffer, "void main()\n{\n");
5605 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
5606 shader_addline(buffer, "if (any(lessThan(gl_TexCoord[7], vec4(0.0)))) discard;\n");
5608 /* Generate texture sampling instructions) */
5609 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
5611 const char *texture_function, *coord_mask;
5612 char tex_reg_name[8];
5613 BOOL proj;
5615 if (!(tex_map & (1 << stage)))
5616 continue;
5618 if (settings->op[stage].projected == proj_none)
5620 proj = FALSE;
5622 else if (settings->op[stage].projected == proj_count4
5623 || settings->op[stage].projected == proj_count3)
5625 proj = TRUE;
5627 else
5629 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
5630 proj = TRUE;
5633 switch (settings->op[stage].tex_type)
5635 case tex_1d:
5636 if (proj)
5638 texture_function = "texture1DProj";
5639 coord_mask = "xw";
5641 else
5643 texture_function = "texture1D";
5644 coord_mask = "x";
5646 break;
5647 case tex_2d:
5648 if (proj)
5650 texture_function = "texture2DProj";
5651 coord_mask = "xyw";
5653 else
5655 texture_function = "texture2D";
5656 coord_mask = "xy";
5658 break;
5659 case tex_3d:
5660 if (proj)
5662 texture_function = "texture3DProj";
5663 coord_mask = "xyzw";
5665 else
5667 texture_function = "texture3D";
5668 coord_mask = "xyz";
5670 break;
5671 case tex_cube:
5672 texture_function = "textureCube";
5673 coord_mask = "xyz";
5674 break;
5675 case tex_rect:
5676 if (proj)
5678 texture_function = "texture2DRectProj";
5679 coord_mask = "xyw";
5681 else
5683 texture_function = "texture2DRect";
5684 coord_mask = "xy";
5686 break;
5687 default:
5688 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
5689 texture_function = "";
5690 coord_mask = "xyzw";
5691 break;
5694 if (stage > 0
5695 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
5696 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
5698 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
5700 /* With projective textures, texbem only divides the static
5701 * texture coord, not the displacement, so multiply the
5702 * displacement with the dividing parameter before passing it to
5703 * TXP. */
5704 if (settings->op[stage].projected != proj_none)
5706 if (settings->op[stage].projected == proj_count4)
5708 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].w) + gl_TexCoord[%u].xy;\n",
5709 stage, stage);
5710 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].ww;\n", stage);
5712 else
5714 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].z) + gl_TexCoord[%u].xy;\n",
5715 stage, stage);
5716 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].zz;\n", stage);
5719 else
5721 shader_addline(buffer, "ret = gl_TexCoord[%u] + ret.xyxy;\n", stage);
5724 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
5725 stage, texture_function, stage, coord_mask);
5727 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
5728 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
5729 stage, stage - 1, stage - 1, stage - 1);
5731 else if (settings->op[stage].projected == proj_count3)
5733 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].xyz);\n",
5734 stage, texture_function, stage, stage);
5736 else
5738 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].%s);\n",
5739 stage, texture_function, stage, stage, coord_mask);
5742 sprintf(tex_reg_name, "tex%u", stage);
5743 shader_glsl_color_correction_ext(buffer, tex_reg_name, WINED3DSP_WRITEMASK_ALL,
5744 settings->op[stage].color_fixup);
5747 /* Generate the main shader */
5748 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5750 BOOL op_equal;
5752 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5754 if (!stage)
5755 final_combiner_src = "gl_Color";
5756 break;
5759 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5760 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5761 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
5762 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5763 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5764 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
5765 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5766 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5767 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
5768 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5769 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5770 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
5771 else
5772 op_equal = settings->op[stage].aop == settings->op[stage].cop
5773 && settings->op[stage].carg0 == settings->op[stage].aarg0
5774 && settings->op[stage].carg1 == settings->op[stage].aarg1
5775 && settings->op[stage].carg2 == settings->op[stage].aarg2;
5777 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5779 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5780 settings->op[stage].cop, settings->op[stage].carg0,
5781 settings->op[stage].carg1, settings->op[stage].carg2);
5782 if (!stage)
5783 shader_addline(buffer, "ret.w = gl_Color.w;\n");
5785 else if (op_equal)
5787 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
5788 settings->op[stage].cop, settings->op[stage].carg0,
5789 settings->op[stage].carg1, settings->op[stage].carg2);
5791 else
5793 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5794 settings->op[stage].cop, settings->op[stage].carg0,
5795 settings->op[stage].carg1, settings->op[stage].carg2);
5796 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
5797 settings->op[stage].aop, settings->op[stage].aarg0,
5798 settings->op[stage].aarg1, settings->op[stage].aarg2);
5802 shader_addline(buffer, "gl_FragData[0] = gl_SecondaryColor * specular_enable + %s;\n", final_combiner_src);
5804 if (settings->sRGB_write)
5805 shader_glsl_generate_srgb_write_correction(buffer);
5807 shader_glsl_generate_fog_code(buffer, settings->fog);
5809 shader_addline(buffer, "}\n");
5811 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5812 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5813 return shader_id;
5816 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
5817 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
5819 struct glsl_ffp_vertex_shader *shader;
5820 const struct wine_rb_entry *entry;
5822 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
5823 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
5825 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
5826 return NULL;
5828 shader->desc.settings = *settings;
5829 shader->id = shader_glsl_generate_ffp_vertex_shader(&priv->shader_buffer, settings, gl_info);
5830 list_init(&shader->linked_programs);
5831 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
5832 ERR("Failed to insert ffp vertex shader.\n");
5834 return shader;
5837 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
5838 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
5840 struct glsl_ffp_fragment_shader *glsl_desc;
5841 const struct ffp_frag_desc *desc;
5843 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
5844 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
5846 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
5847 return NULL;
5849 glsl_desc->entry.settings = *args;
5850 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(&priv->shader_buffer, args, gl_info);
5851 list_init(&glsl_desc->linked_programs);
5852 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
5854 return glsl_desc;
5858 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
5859 GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
5861 unsigned int i;
5862 char name[32];
5864 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5865 sizeof(GLuint) * gl_info->limits.glsl_vs_float_constants);
5866 for (i = 0; i < vs_c_count; ++i)
5868 snprintf(name, sizeof(name), "vs_c[%u]", i);
5869 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5871 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
5872 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLuint));
5874 for (i = 0; i < MAX_CONST_I; ++i)
5876 snprintf(name, sizeof(name), "vs_i[%u]", i);
5877 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5880 for (i = 0; i < MAX_CONST_B; ++i)
5882 snprintf(name, sizeof(name), "vs_b[%u]", i);
5883 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5886 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "posFixup"));
5888 vs->modelview_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_modelview_matrix"));
5889 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
5890 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
5893 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
5894 GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
5896 unsigned int i;
5897 char name[32];
5899 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5900 sizeof(GLuint) * gl_info->limits.glsl_ps_float_constants);
5901 for (i = 0; i < ps_c_count; ++i)
5903 snprintf(name, sizeof(name), "ps_c[%u]", i);
5904 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5906 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
5907 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLuint));
5909 for (i = 0; i < MAX_CONST_I; ++i)
5911 snprintf(name, sizeof(name), "ps_i[%u]", i);
5912 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5915 for (i = 0; i < MAX_CONST_B; ++i)
5917 snprintf(name, sizeof(name), "ps_b[%u]", i);
5918 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5921 for (i = 0; i < MAX_TEXTURES; ++i)
5923 snprintf(name, sizeof(name), "bumpenv_mat%u", i);
5924 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5925 snprintf(name, sizeof(name), "bumpenv_lum_scale%u", i);
5926 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5927 snprintf(name, sizeof(name), "bumpenv_lum_offset%u", i);
5928 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5929 snprintf(name, sizeof(name), "tss_const%u", i);
5930 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5933 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
5934 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
5935 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
5936 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
5939 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info, GLuint program_id,
5940 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
5942 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
5943 GLuint block_idx;
5944 unsigned int i;
5945 char name[16];
5947 for (i = 0; i < count; ++i)
5949 if (!reg_maps->cb_sizes[i])
5950 continue;
5952 snprintf(name, sizeof(name), "block_%s_cb%u", prefix, i);
5953 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name));
5954 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
5956 checkGLcall("glUniformBlockBinding");
5959 /* Context activation is done by the caller. */
5960 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
5961 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
5963 const struct wined3d_gl_info *gl_info = context->gl_info;
5964 const struct ps_np2fixup_info *np2fixup_info = NULL;
5965 struct glsl_shader_prog_link *entry = NULL;
5966 struct wined3d_shader *vshader = NULL;
5967 struct wined3d_shader *gshader = NULL;
5968 struct wined3d_shader *pshader = NULL;
5969 GLuint program_id = 0;
5970 GLuint reorder_shader_id = 0;
5971 unsigned int i;
5972 GLuint vs_id = 0;
5973 GLuint gs_id = 0;
5974 GLuint ps_id = 0;
5975 struct list *ps_list, *vs_list;
5977 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_VERTEX)))
5979 vs_id = ctx_data->glsl_program->vs.id;
5980 vs_list = &ctx_data->glsl_program->vs.shader_entry;
5982 if (use_vs(state))
5984 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5985 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
5987 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_GEOMETRY))
5988 && ctx_data->glsl_program->gs.id)
5989 gs_id = ctx_data->glsl_program->gs.id;
5990 else if (gshader)
5991 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5994 else if (use_vs(state))
5996 struct vs_compile_args vs_compile_args;
5997 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5999 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args);
6000 vs_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
6001 vs_list = &vshader->linked_programs;
6003 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
6004 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
6006 else if (priv->vertex_pipe == &glsl_vertex_pipe)
6008 struct glsl_ffp_vertex_shader *ffp_shader;
6009 struct wined3d_ffp_vs_settings settings;
6011 wined3d_ffp_get_vs_settings(state, &context->stream_info, &settings);
6012 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
6013 vs_id = ffp_shader->id;
6014 vs_list = &ffp_shader->linked_programs;
6017 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_PIXEL)))
6019 ps_id = ctx_data->glsl_program->ps.id;
6020 ps_list = &ctx_data->glsl_program->ps.shader_entry;
6022 if (use_ps(state))
6023 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
6025 else if (use_ps(state))
6027 struct ps_compile_args ps_compile_args;
6028 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
6029 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, gl_info);
6030 ps_id = find_glsl_pshader(context, &priv->shader_buffer,
6031 pshader, &ps_compile_args, &np2fixup_info);
6032 ps_list = &pshader->linked_programs;
6034 else if (priv->fragment_pipe == &glsl_fragment_pipe)
6036 struct glsl_ffp_fragment_shader *ffp_shader;
6037 struct ffp_frag_settings settings;
6039 gen_ffp_frag_op(context, state, &settings, FALSE);
6040 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
6041 ps_id = ffp_shader->id;
6042 ps_list = &ffp_shader->linked_programs;
6045 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
6047 ctx_data->glsl_program = entry;
6048 return;
6051 /* If we get to this point, then no matching program exists, so we create one */
6052 program_id = GL_EXTCALL(glCreateProgram());
6053 TRACE("Created new GLSL shader program %u.\n", program_id);
6055 /* Create the entry */
6056 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
6057 entry->id = program_id;
6058 entry->vs.id = vs_id;
6059 entry->gs.id = gs_id;
6060 entry->ps.id = ps_id;
6061 entry->constant_version = 0;
6062 entry->ps.np2_fixup_info = np2fixup_info;
6063 /* Add the hash table entry */
6064 add_glsl_program_entry(priv, entry);
6066 /* Set the current program */
6067 ctx_data->glsl_program = entry;
6069 /* Attach GLSL vshader */
6070 if (vs_id)
6072 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
6073 GL_EXTCALL(glAttachShader(program_id, vs_id));
6074 checkGLcall("glAttachShader");
6076 list_add_head(vs_list, &entry->vs.shader_entry);
6079 if (vshader)
6081 WORD map = vshader->reg_maps.input_registers;
6082 char tmp_name[10];
6084 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
6085 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
6086 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
6087 checkGLcall("glAttachShader");
6088 /* Flag the reorder function for deletion, then it will be freed automatically when the program
6089 * is destroyed
6091 GL_EXTCALL(glDeleteShader(reorder_shader_id));
6093 /* Bind vertex attributes to a corresponding index number to match
6094 * the same index numbers as ARB_vertex_programs (makes loading
6095 * vertex attributes simpler). With this method, we can use the
6096 * exact same code to load the attributes later for both ARB and
6097 * GLSL shaders.
6099 * We have to do this here because we need to know the Program ID
6100 * in order to make the bindings work, and it has to be done prior
6101 * to linking the GLSL program. */
6102 for (i = 0; map; map >>= 1, ++i)
6104 if (!(map & 1)) continue;
6106 snprintf(tmp_name, sizeof(tmp_name), "vs_in%u", i);
6107 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name));
6109 checkGLcall("glBindAttribLocation");
6112 if (gshader)
6114 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
6115 GL_EXTCALL(glAttachShader(program_id, gs_id));
6116 checkGLcall("glAttachShader");
6118 TRACE("input type %s, output type %s, vertices out %u.\n",
6119 debug_d3dprimitivetype(gshader->u.gs.input_type),
6120 debug_d3dprimitivetype(gshader->u.gs.output_type),
6121 gshader->u.gs.vertices_out);
6122 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
6123 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
6124 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
6125 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
6126 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
6127 gshader->u.gs.vertices_out));
6128 checkGLcall("glProgramParameteriARB");
6130 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
6133 /* Attach GLSL pshader */
6134 if (ps_id)
6136 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
6137 GL_EXTCALL(glAttachShader(program_id, ps_id));
6138 checkGLcall("glAttachShader");
6140 list_add_head(ps_list, &entry->ps.shader_entry);
6143 /* Link the program */
6144 TRACE("Linking GLSL shader program %u.\n", program_id);
6145 GL_EXTCALL(glLinkProgram(program_id));
6146 shader_glsl_validate_link(gl_info, program_id);
6148 shader_glsl_init_vs_uniform_locations(gl_info, program_id, &entry->vs,
6149 vshader ? min(vshader->limits->constant_float, gl_info->limits.glsl_vs_float_constants) : 0);
6150 shader_glsl_init_ps_uniform_locations(gl_info, program_id, &entry->ps,
6151 pshader ? min(pshader->limits->constant_float, gl_info->limits.glsl_ps_float_constants) : 0);
6152 checkGLcall("Find glsl program uniform locations");
6154 if (pshader && pshader->reg_maps.shader_version.major >= 3
6155 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
6157 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
6158 entry->vs.vertex_color_clamp = GL_FALSE;
6160 else
6162 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
6165 /* Set the shader to allow uniform loading on it */
6166 GL_EXTCALL(glUseProgram(program_id));
6167 checkGLcall("glUseProgram");
6169 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
6170 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
6171 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
6172 * vertex shader with fixed function pixel processing is used we make sure that the card
6173 * supports enough samplers to allow the max number of vertex samplers with all possible
6174 * fixed function fragment processing setups. So once the program is linked these samplers
6175 * won't change. */
6176 shader_glsl_load_samplers(gl_info, context->tex_unit_map, program_id);
6178 entry->constant_update_mask = 0;
6179 if (vshader)
6181 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
6182 if (vshader->reg_maps.integer_constants)
6183 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
6184 if (vshader->reg_maps.boolean_constants)
6185 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
6186 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
6188 shader_glsl_init_uniform_block_bindings(gl_info, program_id, &vshader->reg_maps,
6189 0, gl_info->limits.vertex_uniform_blocks);
6191 else
6193 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
6194 | WINED3D_SHADER_CONST_FFP_PROJ;
6197 if (gshader)
6198 shader_glsl_init_uniform_block_bindings(gl_info, program_id, &gshader->reg_maps,
6199 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
6201 if (ps_id)
6203 if (pshader)
6205 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
6206 if (pshader->reg_maps.integer_constants)
6207 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
6208 if (pshader->reg_maps.boolean_constants)
6209 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
6210 if (entry->ps.ycorrection_location != -1)
6211 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
6213 shader_glsl_init_uniform_block_bindings(gl_info, program_id, &pshader->reg_maps,
6214 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
6215 gl_info->limits.fragment_uniform_blocks);
6217 else
6219 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
6222 for (i = 0; i < MAX_TEXTURES; ++i)
6224 if (entry->ps.bumpenv_mat_location[i] != -1)
6226 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
6227 break;
6231 if (entry->ps.np2_fixup_location != -1)
6232 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
6236 /* Context activation is done by the caller. */
6237 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum tex_types tex_type, BOOL masked)
6239 GLuint program_id;
6240 GLuint vshader_id, pshader_id;
6241 const char *blt_pshader;
6243 static const char blt_vshader[] =
6244 "#version 120\n"
6245 "void main(void)\n"
6246 "{\n"
6247 " gl_Position = gl_Vertex;\n"
6248 " gl_FrontColor = vec4(1.0);\n"
6249 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
6250 "}\n";
6252 static const char * const blt_pshaders_full[tex_type_count] =
6254 /* tex_1d */
6255 NULL,
6256 /* tex_2d */
6257 "#version 120\n"
6258 "uniform sampler2D sampler;\n"
6259 "void main(void)\n"
6260 "{\n"
6261 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6262 "}\n",
6263 /* tex_3d */
6264 NULL,
6265 /* tex_cube */
6266 "#version 120\n"
6267 "uniform samplerCube sampler;\n"
6268 "void main(void)\n"
6269 "{\n"
6270 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6271 "}\n",
6272 /* tex_rect */
6273 "#version 120\n"
6274 "#extension GL_ARB_texture_rectangle : enable\n"
6275 "uniform sampler2DRect sampler;\n"
6276 "void main(void)\n"
6277 "{\n"
6278 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6279 "}\n",
6282 static const char * const blt_pshaders_masked[tex_type_count] =
6284 /* tex_1d */
6285 NULL,
6286 /* tex_2d */
6287 "#version 120\n"
6288 "uniform sampler2D sampler;\n"
6289 "uniform vec4 mask;\n"
6290 "void main(void)\n"
6291 "{\n"
6292 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6293 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6294 "}\n",
6295 /* tex_3d */
6296 NULL,
6297 /* tex_cube */
6298 "#version 120\n"
6299 "uniform samplerCube sampler;\n"
6300 "uniform vec4 mask;\n"
6301 "void main(void)\n"
6302 "{\n"
6303 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6304 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6305 "}\n",
6306 /* tex_rect */
6307 "#version 120\n"
6308 "#extension GL_ARB_texture_rectangle : enable\n"
6309 "uniform sampler2DRect sampler;\n"
6310 "uniform vec4 mask;\n"
6311 "void main(void)\n"
6312 "{\n"
6313 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6314 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6315 "}\n",
6318 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
6319 if (!blt_pshader)
6321 FIXME("tex_type %#x not supported\n", tex_type);
6322 return 0;
6325 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6326 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
6328 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6329 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
6331 program_id = GL_EXTCALL(glCreateProgram());
6332 GL_EXTCALL(glAttachShader(program_id, vshader_id));
6333 GL_EXTCALL(glAttachShader(program_id, pshader_id));
6334 GL_EXTCALL(glLinkProgram(program_id));
6336 shader_glsl_validate_link(gl_info, program_id);
6338 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
6339 * is destroyed
6341 GL_EXTCALL(glDeleteShader(vshader_id));
6342 GL_EXTCALL(glDeleteShader(pshader_id));
6343 return program_id;
6346 /* Context activation is done by the caller. */
6347 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
6348 const struct wined3d_state *state)
6350 struct glsl_context_data *ctx_data = context->shader_backend_data;
6351 const struct wined3d_gl_info *gl_info = context->gl_info;
6352 struct shader_glsl_priv *priv = shader_priv;
6353 GLuint program_id = 0, prev_id = 0;
6354 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
6356 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
6357 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
6359 if (ctx_data->glsl_program)
6361 prev_id = ctx_data->glsl_program->id;
6362 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6364 else
6366 prev_id = 0;
6367 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6370 set_glsl_shader_program(context, state, priv, ctx_data);
6372 if (ctx_data->glsl_program)
6374 program_id = ctx_data->glsl_program->id;
6375 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6377 else
6379 program_id = 0;
6380 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6383 if (old_vertex_color_clamp != current_vertex_color_clamp)
6385 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6387 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
6388 checkGLcall("glClampColorARB");
6390 else
6392 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
6396 TRACE("Using GLSL program %u.\n", program_id);
6398 if (prev_id != program_id)
6400 GL_EXTCALL(glUseProgram(program_id));
6401 checkGLcall("glUseProgram");
6403 if (program_id)
6404 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
6408 /* "context" is not necessarily the currently active context. */
6409 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
6411 struct glsl_context_data *ctx_data = context->shader_backend_data;
6413 ctx_data->glsl_program = NULL;
6414 context->shader_update_mask = (1 << WINED3D_SHADER_TYPE_PIXEL)
6415 | (1 << WINED3D_SHADER_TYPE_VERTEX)
6416 | (1 << WINED3D_SHADER_TYPE_GEOMETRY);
6419 /* Context activation is done by the caller. */
6420 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
6422 const struct wined3d_gl_info *gl_info = context->gl_info;
6423 struct shader_glsl_priv *priv = shader_priv;
6425 shader_glsl_invalidate_current_program(context);
6426 GL_EXTCALL(glUseProgram(0));
6427 checkGLcall("glUseProgram");
6429 priv->vertex_pipe->vp_enable(gl_info, FALSE);
6430 priv->fragment_pipe->enable_extension(gl_info, FALSE);
6432 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6434 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
6435 checkGLcall("glClampColorARB");
6439 /* Context activation is done by the caller. */
6440 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
6441 enum tex_types tex_type, const SIZE *ds_mask_size)
6443 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
6444 struct shader_glsl_priv *priv = shader_priv;
6445 GLuint *blt_program;
6446 GLint loc;
6448 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
6449 if (!*blt_program)
6451 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
6452 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
6453 GL_EXTCALL(glUseProgram(*blt_program));
6454 GL_EXTCALL(glUniform1i(loc, 0));
6456 else
6458 GL_EXTCALL(glUseProgram(*blt_program));
6461 if (masked)
6463 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
6464 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
6468 /* Context activation is done by the caller. */
6469 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
6471 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
6472 GLuint program_id;
6474 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
6475 if (program_id) TRACE("Using GLSL program %u\n", program_id);
6477 GL_EXTCALL(glUseProgram(program_id));
6478 checkGLcall("glUseProgram");
6481 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
6482 const struct glsl_shader_prog_link *program)
6484 const struct glsl_context_data *ctx_data;
6485 struct wined3d_context *context;
6486 unsigned int i;
6488 for (i = 0; i < device->context_count; ++i)
6490 context = device->contexts[i];
6491 ctx_data = context->shader_backend_data;
6493 if (ctx_data->glsl_program == program)
6494 shader_glsl_invalidate_current_program(context);
6498 static void shader_glsl_destroy(struct wined3d_shader *shader)
6500 struct glsl_shader_private *shader_data = shader->backend_data;
6501 struct wined3d_device *device = shader->device;
6502 struct shader_glsl_priv *priv = device->shader_priv;
6503 const struct wined3d_gl_info *gl_info;
6504 const struct list *linked_programs;
6505 struct wined3d_context *context;
6507 if (!shader_data || !shader_data->num_gl_shaders)
6509 HeapFree(GetProcessHeap(), 0, shader_data);
6510 shader->backend_data = NULL;
6511 return;
6514 context = context_acquire(device, NULL);
6515 gl_info = context->gl_info;
6517 TRACE("Deleting linked programs.\n");
6518 linked_programs = &shader->linked_programs;
6519 if (linked_programs->next)
6521 struct glsl_shader_prog_link *entry, *entry2;
6522 UINT i;
6524 switch (shader->reg_maps.shader_version.type)
6526 case WINED3D_SHADER_TYPE_PIXEL:
6528 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
6530 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6532 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
6533 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
6534 checkGLcall("glDeleteShader");
6536 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
6538 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6539 struct glsl_shader_prog_link, ps.shader_entry)
6541 shader_glsl_invalidate_contexts_program(device, entry);
6542 delete_glsl_program_entry(priv, gl_info, entry);
6545 break;
6548 case WINED3D_SHADER_TYPE_VERTEX:
6550 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
6552 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6554 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
6555 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
6556 checkGLcall("glDeleteShader");
6558 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
6560 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6561 struct glsl_shader_prog_link, vs.shader_entry)
6563 shader_glsl_invalidate_contexts_program(device, entry);
6564 delete_glsl_program_entry(priv, gl_info, entry);
6567 break;
6570 case WINED3D_SHADER_TYPE_GEOMETRY:
6572 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
6574 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6576 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
6577 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
6578 checkGLcall("glDeleteShader");
6580 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
6582 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6583 struct glsl_shader_prog_link, gs.shader_entry)
6585 shader_glsl_invalidate_contexts_program(device, entry);
6586 delete_glsl_program_entry(priv, gl_info, entry);
6589 break;
6592 default:
6593 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
6594 break;
6598 HeapFree(GetProcessHeap(), 0, shader->backend_data);
6599 shader->backend_data = NULL;
6601 context_release(context);
6604 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
6606 const struct glsl_program_key *k = key;
6607 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
6608 const struct glsl_shader_prog_link, program_lookup_entry);
6610 if (k->vs_id > prog->vs.id) return 1;
6611 else if (k->vs_id < prog->vs.id) return -1;
6613 if (k->gs_id > prog->gs.id) return 1;
6614 else if (k->gs_id < prog->gs.id) return -1;
6616 if (k->ps_id > prog->ps.id) return 1;
6617 else if (k->ps_id < prog->ps.id) return -1;
6619 return 0;
6622 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
6624 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
6625 + constant_count * sizeof(*heap->contained)
6626 + constant_count * sizeof(*heap->positions);
6627 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
6629 if (!mem)
6631 ERR("Failed to allocate memory\n");
6632 return FALSE;
6635 heap->entries = mem;
6636 heap->entries[1].version = 0;
6637 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
6638 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
6639 heap->positions = (unsigned int *)(heap->contained + constant_count);
6640 heap->size = 1;
6642 return TRUE;
6645 static void constant_heap_free(struct constant_heap *heap)
6647 HeapFree(GetProcessHeap(), 0, heap->entries);
6650 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
6652 wined3d_rb_alloc,
6653 wined3d_rb_realloc,
6654 wined3d_rb_free,
6655 glsl_program_key_compare,
6658 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
6659 const struct fragment_pipeline *fragment_pipe)
6661 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6662 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
6663 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
6664 gl_info->limits.glsl_ps_float_constants)) + 1;
6665 struct fragment_caps fragment_caps;
6666 void *vertex_priv, *fragment_priv;
6668 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
6670 ERR("Failed to initialize vertex pipe.\n");
6671 HeapFree(GetProcessHeap(), 0, priv);
6672 return E_FAIL;
6675 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
6677 ERR("Failed to initialize fragment pipe.\n");
6678 vertex_pipe->vp_free(device);
6679 HeapFree(GetProcessHeap(), 0, priv);
6680 return E_FAIL;
6683 if (!shader_buffer_init(&priv->shader_buffer))
6685 ERR("Failed to initialize shader buffer.\n");
6686 goto fail;
6689 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
6690 if (!priv->stack)
6692 ERR("Failed to allocate memory.\n");
6693 goto fail;
6696 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
6698 ERR("Failed to initialize vertex shader constant heap\n");
6699 goto fail;
6702 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
6704 ERR("Failed to initialize pixel shader constant heap\n");
6705 goto fail;
6708 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
6710 ERR("Failed to initialize rbtree.\n");
6711 goto fail;
6714 priv->next_constant_version = 1;
6715 priv->vertex_pipe = vertex_pipe;
6716 priv->fragment_pipe = fragment_pipe;
6717 fragment_pipe->get_caps(gl_info, &fragment_caps);
6718 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
6720 device->vertex_priv = vertex_priv;
6721 device->fragment_priv = fragment_priv;
6722 device->shader_priv = priv;
6724 return WINED3D_OK;
6726 fail:
6727 constant_heap_free(&priv->pconst_heap);
6728 constant_heap_free(&priv->vconst_heap);
6729 HeapFree(GetProcessHeap(), 0, priv->stack);
6730 shader_buffer_free(&priv->shader_buffer);
6731 fragment_pipe->free_private(device);
6732 vertex_pipe->vp_free(device);
6733 HeapFree(GetProcessHeap(), 0, priv);
6734 return E_OUTOFMEMORY;
6737 /* Context activation is done by the caller. */
6738 static void shader_glsl_free(struct wined3d_device *device)
6740 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6741 struct shader_glsl_priv *priv = device->shader_priv;
6742 int i;
6744 for (i = 0; i < tex_type_count; ++i)
6746 if (priv->depth_blt_program_full[i])
6748 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
6750 if (priv->depth_blt_program_masked[i])
6752 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
6756 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
6757 constant_heap_free(&priv->pconst_heap);
6758 constant_heap_free(&priv->vconst_heap);
6759 HeapFree(GetProcessHeap(), 0, priv->stack);
6760 shader_buffer_free(&priv->shader_buffer);
6761 priv->fragment_pipe->free_private(device);
6762 priv->vertex_pipe->vp_free(device);
6764 HeapFree(GetProcessHeap(), 0, device->shader_priv);
6765 device->shader_priv = NULL;
6768 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
6770 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
6771 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
6774 static void shader_glsl_free_context_data(struct wined3d_context *context)
6776 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
6779 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
6781 UINT shader_model;
6783 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
6784 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
6785 && gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] && gl_info->supported[ARB_DRAW_INSTANCED]
6786 && gl_info->supported[ARB_TEXTURE_RG] && gl_info->supported[ARB_SAMPLER_OBJECTS])
6787 shader_model = 4;
6788 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
6789 * texldd and texldl instructions. */
6790 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
6791 shader_model = 3;
6792 else
6793 shader_model = 2;
6794 TRACE("Shader model %u.\n", shader_model);
6796 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
6797 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
6798 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
6800 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
6801 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
6803 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
6804 * Direct3D minimum requirement.
6806 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
6807 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
6809 * The problem is that the refrast clamps temporary results in the shader to
6810 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
6811 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
6812 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
6813 * offer a way to query this.
6815 if (shader_model >= 4)
6816 caps->ps_1x_max_value = FLT_MAX;
6817 else
6818 caps->ps_1x_max_value = 1024.0f;
6820 /* Ideally we'd only set caps like sRGB writes here if supported by both
6821 * the shader backend and the fragment pipe, but we can get called before
6822 * shader_glsl_alloc(). */
6823 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
6824 | WINED3D_SHADER_CAP_SRGB_WRITE;
6827 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
6829 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
6831 TRACE("Checking support for fixup:\n");
6832 dump_color_fixup_desc(fixup);
6835 /* We support everything except YUV conversions. */
6836 if (!is_complex_fixup(fixup))
6838 TRACE("[OK]\n");
6839 return TRUE;
6842 TRACE("[FAILED]\n");
6843 return FALSE;
6846 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
6848 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
6849 /* WINED3DSIH_ADD */ shader_glsl_binop,
6850 /* WINED3DSIH_AND */ shader_glsl_binop,
6851 /* WINED3DSIH_BEM */ shader_glsl_bem,
6852 /* WINED3DSIH_BREAK */ shader_glsl_break,
6853 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
6854 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
6855 /* WINED3DSIH_CALL */ shader_glsl_call,
6856 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
6857 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
6858 /* WINED3DSIH_CND */ shader_glsl_cnd,
6859 /* WINED3DSIH_CRS */ shader_glsl_cross,
6860 /* WINED3DSIH_CUT */ shader_glsl_cut,
6861 /* WINED3DSIH_DCL */ shader_glsl_nop,
6862 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
6863 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
6864 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
6865 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
6866 /* WINED3DSIH_DEF */ shader_glsl_nop,
6867 /* WINED3DSIH_DEFB */ shader_glsl_nop,
6868 /* WINED3DSIH_DEFI */ shader_glsl_nop,
6869 /* WINED3DSIH_DIV */ shader_glsl_binop,
6870 /* WINED3DSIH_DP2 */ shader_glsl_dot,
6871 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
6872 /* WINED3DSIH_DP3 */ shader_glsl_dot,
6873 /* WINED3DSIH_DP4 */ shader_glsl_dot,
6874 /* WINED3DSIH_DST */ shader_glsl_dst,
6875 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
6876 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
6877 /* WINED3DSIH_ELSE */ shader_glsl_else,
6878 /* WINED3DSIH_EMIT */ shader_glsl_emit,
6879 /* WINED3DSIH_ENDIF */ shader_glsl_end,
6880 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
6881 /* WINED3DSIH_ENDREP */ shader_glsl_end,
6882 /* WINED3DSIH_EQ */ shader_glsl_relop,
6883 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
6884 /* WINED3DSIH_EXPP */ shader_glsl_expp,
6885 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
6886 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
6887 /* WINED3DSIH_GE */ shader_glsl_relop,
6888 /* WINED3DSIH_IADD */ shader_glsl_binop,
6889 /* WINED3DSIH_IEQ */ NULL,
6890 /* WINED3DSIH_IF */ shader_glsl_if,
6891 /* WINED3DSIH_IFC */ shader_glsl_ifc,
6892 /* WINED3DSIH_IGE */ shader_glsl_relop,
6893 /* WINED3DSIH_IMUL */ shader_glsl_imul,
6894 /* WINED3DSIH_ISHL */ shader_glsl_binop,
6895 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
6896 /* WINED3DSIH_LABEL */ shader_glsl_label,
6897 /* WINED3DSIH_LD */ NULL,
6898 /* WINED3DSIH_LIT */ shader_glsl_lit,
6899 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
6900 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
6901 /* WINED3DSIH_LOOP */ shader_glsl_loop,
6902 /* WINED3DSIH_LRP */ shader_glsl_lrp,
6903 /* WINED3DSIH_LT */ shader_glsl_relop,
6904 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
6905 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
6906 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
6907 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
6908 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
6909 /* WINED3DSIH_MAD */ shader_glsl_mad,
6910 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
6911 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
6912 /* WINED3DSIH_MOV */ shader_glsl_mov,
6913 /* WINED3DSIH_MOVA */ shader_glsl_mov,
6914 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
6915 /* WINED3DSIH_MUL */ shader_glsl_binop,
6916 /* WINED3DSIH_NE */ shader_glsl_relop,
6917 /* WINED3DSIH_NOP */ shader_glsl_nop,
6918 /* WINED3DSIH_NRM */ shader_glsl_nrm,
6919 /* WINED3DSIH_OR */ shader_glsl_binop,
6920 /* WINED3DSIH_PHASE */ shader_glsl_nop,
6921 /* WINED3DSIH_POW */ shader_glsl_pow,
6922 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
6923 /* WINED3DSIH_REP */ shader_glsl_rep,
6924 /* WINED3DSIH_RET */ shader_glsl_ret,
6925 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
6926 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
6927 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
6928 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
6929 /* WINED3DSIH_SAMPLE_LOD */ NULL,
6930 /* WINED3DSIH_SETP */ NULL,
6931 /* WINED3DSIH_SGE */ shader_glsl_compare,
6932 /* WINED3DSIH_SGN */ shader_glsl_sgn,
6933 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
6934 /* WINED3DSIH_SLT */ shader_glsl_compare,
6935 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
6936 /* WINED3DSIH_SUB */ shader_glsl_binop,
6937 /* WINED3DSIH_TEX */ shader_glsl_tex,
6938 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
6939 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
6940 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
6941 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
6942 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
6943 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
6944 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
6945 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
6946 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
6947 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
6948 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
6949 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
6950 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
6951 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
6952 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
6953 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
6954 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
6955 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
6956 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
6957 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
6958 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
6959 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
6960 /* WINED3DSIH_UGE */ shader_glsl_relop,
6961 /* WINED3DSIH_USHR */ shader_glsl_binop,
6962 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
6963 /* WINED3DSIH_XOR */ shader_glsl_binop,
6966 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
6967 SHADER_HANDLER hw_fct;
6969 /* Select handler */
6970 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
6972 /* Unhandled opcode */
6973 if (!hw_fct)
6975 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
6976 return;
6978 hw_fct(ins);
6980 shader_glsl_add_instruction_modifiers(ins);
6983 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
6985 struct shader_glsl_priv *priv = shader_priv;
6987 return priv->ffp_proj_control;
6990 const struct wined3d_shader_backend_ops glsl_shader_backend =
6992 shader_glsl_handle_instruction,
6993 shader_glsl_select,
6994 shader_glsl_disable,
6995 shader_glsl_select_depth_blt,
6996 shader_glsl_deselect_depth_blt,
6997 shader_glsl_update_float_vertex_constants,
6998 shader_glsl_update_float_pixel_constants,
6999 shader_glsl_load_constants,
7000 shader_glsl_destroy,
7001 shader_glsl_alloc,
7002 shader_glsl_free,
7003 shader_glsl_allocate_context_data,
7004 shader_glsl_free_context_data,
7005 shader_glsl_get_caps,
7006 shader_glsl_color_fixup_supported,
7007 shader_glsl_has_ffp_proj_control,
7010 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
7012 if (enable)
7013 gl_info->gl_ops.gl.p_glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
7014 else
7015 gl_info->gl_ops.gl.p_glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
7016 checkGLcall("GL_VERTEX_PROGRAM_POINT_SIZE_ARB");
7019 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
7021 caps->xyzrhw = TRUE;
7022 caps->max_active_lights = gl_info->limits.lights;
7023 caps->max_vertex_blend_matrices = 1;
7024 caps->max_vertex_blend_matrix_index = 0;
7025 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
7026 | WINED3DVTXPCAPS_MATERIALSOURCE7
7027 | WINED3DVTXPCAPS_VERTEXFOG
7028 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
7029 | WINED3DVTXPCAPS_POSITIONALLIGHTS
7030 | WINED3DVTXPCAPS_LOCALVIEWER
7031 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
7032 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
7033 caps->max_user_clip_planes = gl_info->limits.clipplanes;
7034 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
7037 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7039 struct shader_glsl_priv *priv;
7041 if (shader_backend == &glsl_shader_backend)
7043 priv = shader_priv;
7045 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
7047 ERR("Failed to initialize rbtree.\n");
7048 return NULL;
7051 return priv;
7054 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
7056 return NULL;
7059 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
7061 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7062 struct glsl_ffp_vertex_shader, desc.entry);
7063 struct glsl_shader_prog_link *program, *program2;
7064 struct glsl_ffp_destroy_ctx *ctx = context;
7066 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7067 struct glsl_shader_prog_link, vs.shader_entry)
7069 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7071 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
7072 HeapFree(GetProcessHeap(), 0, shader);
7075 /* Context activation is done by the caller. */
7076 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
7078 struct shader_glsl_priv *priv = device->vertex_priv;
7079 struct glsl_ffp_destroy_ctx ctx;
7081 ctx.priv = priv;
7082 ctx.gl_info = &device->adapter->gl_info;
7083 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
7086 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
7087 const struct wined3d_state *state, DWORD state_id)
7089 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7092 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
7093 const struct wined3d_state *state, DWORD state_id)
7095 const struct wined3d_gl_info *gl_info = context->gl_info;
7096 BOOL transformed = context->stream_info.position_transformed;
7097 BOOL wasrhw = context->last_was_rhw;
7098 unsigned int i;
7100 context->last_was_rhw = transformed;
7102 if (!use_vs(state))
7104 if (context->last_was_vshader)
7106 for (i = 0; i < gl_info->limits.clipplanes; ++i)
7107 clipplane(context, state, STATE_CLIPPLANE(i));
7110 if (transformed != wasrhw)
7111 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
7113 for (i = 0; i < MAX_TEXTURES; ++i)
7115 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_TEXTURE0 + i)))
7116 transform_texture(context, state, STATE_TEXTURESTAGE(i, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS));
7119 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_LIGHTING)))
7120 state_lighting(context, state, STATE_RENDER(WINED3D_RS_LIGHTING));
7121 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_NORMALIZENORMALS)))
7122 state_normalize(context, state, STATE_RENDER(WINED3D_RS_NORMALIZENORMALS));
7124 /* Because of settings->texcoords, we have to always regenerate the
7125 * vertex shader on a vdecl change.
7126 * TODO: Just always output all the texcoords when there are enough
7127 * varyings available to drop the dependency. */
7128 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7130 if (use_ps(state)
7131 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
7132 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
7133 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7135 else
7137 if (!context->last_was_vshader)
7139 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
7140 for (i = 0; i < gl_info->limits.clipplanes; ++i)
7141 clipplane(context, state, STATE_CLIPPLANE(i));
7145 context->last_was_vshader = use_vs(state);
7148 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
7149 const struct wined3d_state *state, DWORD state_id)
7151 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7152 /* Different vertex shaders potentially require a different vertex attributes setup. */
7153 if (!isStateDirty(context, STATE_VDECL))
7154 context_apply_state(context, state, STATE_VDECL);
7157 static void glsl_vertex_pipe_world(struct wined3d_context *context,
7158 const struct wined3d_state *state, DWORD state_id)
7160 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
7163 void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
7165 const struct wined3d_gl_info *gl_info = context->gl_info;
7166 const struct wined3d_light_info *light = NULL;
7167 unsigned int k;
7169 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
7171 for (k = 0; k < gl_info->limits.lights; ++k)
7173 if (!(light = state->lights[k]))
7174 continue;
7175 gl_info->gl_ops.gl.p_glLightfv(GL_LIGHT0 + light->glIndex, GL_POSITION, light->lightPosn);
7176 checkGLcall("glLightfv posn");
7177 gl_info->gl_ops.gl.p_glLightfv(GL_LIGHT0 + light->glIndex, GL_SPOT_DIRECTION, light->lightDirn);
7178 checkGLcall("glLightfv dirn");
7181 for (k = 0; k < gl_info->limits.clipplanes; ++k)
7183 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
7184 clipplane(context, state, STATE_CLIPPLANE(k));
7187 if (context->swapchain->device->vertexBlendUsed)
7189 static int warned;
7191 if (!warned++)
7192 FIXME("Vertex blending emulation.\n");
7196 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
7197 const struct wined3d_state *state, DWORD state_id)
7199 /* Table fog behavior depends on the projection matrix. */
7200 if (state->render_states[WINED3D_RS_FOGENABLE]
7201 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
7202 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7203 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
7206 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
7207 const struct wined3d_state *state, DWORD state_id)
7209 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
7210 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
7211 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
7212 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
7213 state_pscale(context, state, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE));
7214 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
7217 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
7219 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
7220 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
7221 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
7222 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), state_specularenable }, WINED3D_GL_EXT_NONE },
7223 /* Clip planes */
7224 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
7225 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
7226 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
7227 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
7228 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
7229 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
7230 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
7231 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
7232 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
7233 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
7234 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
7235 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
7236 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
7237 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
7238 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
7239 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
7240 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
7241 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
7242 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
7243 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
7244 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
7245 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
7246 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
7247 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
7248 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
7249 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
7250 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
7251 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
7252 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
7253 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
7254 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
7255 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
7256 /* Lights */
7257 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7258 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), light }, WINED3D_GL_EXT_NONE },
7259 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), light }, WINED3D_GL_EXT_NONE },
7260 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), light }, WINED3D_GL_EXT_NONE },
7261 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), light }, WINED3D_GL_EXT_NONE },
7262 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), light }, WINED3D_GL_EXT_NONE },
7263 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), light }, WINED3D_GL_EXT_NONE },
7264 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), light }, WINED3D_GL_EXT_NONE },
7265 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), light }, WINED3D_GL_EXT_NONE },
7266 /* Viewport */
7267 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
7268 /* Transform states */
7269 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
7270 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
7271 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7272 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7273 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7274 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7275 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7276 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7277 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7278 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7279 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
7280 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7281 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7282 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7283 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7284 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7285 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7286 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7287 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7288 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7289 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7290 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7291 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7292 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7293 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7294 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7295 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7296 /* Fog */
7297 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7298 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7299 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7300 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7301 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
7302 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
7303 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7304 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), state_ambient }, WINED3D_GL_EXT_NONE },
7305 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7306 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7307 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7308 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7309 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7310 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7311 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7312 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7313 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7314 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_arb }, ARB_POINT_PARAMETERS },
7315 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_ext }, EXT_POINT_PARAMETERS },
7316 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_w }, WINED3D_GL_EXT_NONE },
7317 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
7318 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
7319 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), state_pscale }, WINED3D_GL_EXT_NONE },
7320 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7321 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7322 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7323 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, ARB_POINT_PARAMETERS },
7324 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, EXT_POINT_PARAMETERS },
7325 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
7326 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7327 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7328 /* Samplers for NP2 texture matrix adjustions. They are not needed if
7329 * GL_ARB_texture_non_power_of_two is supported, so register a NULL state
7330 * handler in that case to get the vertex part of sampler() skipped (VTF
7331 * is handled in the misc states). Otherwise, register
7332 * sampler_texmatrix(), which takes care of updating the texture matrix. */
7333 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7334 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7335 {STATE_SAMPLER(0), {STATE_SAMPLER(0), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7336 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7337 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7338 {STATE_SAMPLER(1), {STATE_SAMPLER(1), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7339 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7340 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7341 {STATE_SAMPLER(2), {STATE_SAMPLER(2), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7342 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7343 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7344 {STATE_SAMPLER(3), {STATE_SAMPLER(3), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7345 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7346 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7347 {STATE_SAMPLER(4), {STATE_SAMPLER(4), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7348 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7349 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7350 {STATE_SAMPLER(5), {STATE_SAMPLER(5), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7351 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7352 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7353 {STATE_SAMPLER(6), {STATE_SAMPLER(6), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7354 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7355 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7356 {STATE_SAMPLER(7), {STATE_SAMPLER(7), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7357 {STATE_POINT_SIZE_ENABLE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7358 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
7361 /* TODO:
7362 * - This currently depends on GL fixed function functions to set things
7363 * like light parameters. Ideally we'd use regular uniforms for that.
7364 * - In part because of the previous point, much of this is modelled after
7365 * GL fixed function, and has much of the same limitations. For example,
7366 * D3D spot lights are slightly different from GL spot lights.
7367 * - We can now implement drawing transformed vertices using the GLSL pipe,
7368 * instead of using the immediate mode fallback.
7369 * - Similarly, we don't need the fallback for certain combinations of
7370 * material sources anymore.
7371 * - Implement vertex blending and vertex tweening.
7372 * - Handle WINED3D_TSS_TEXCOORD_INDEX in the shader, instead of duplicating
7373 * attribute arrays in load_tex_coords().
7374 * - Per-vertex point sizes. */
7375 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
7377 glsl_vertex_pipe_vp_enable,
7378 glsl_vertex_pipe_vp_get_caps,
7379 glsl_vertex_pipe_vp_alloc,
7380 glsl_vertex_pipe_vp_free,
7381 glsl_vertex_pipe_vp_states,
7384 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
7386 /* Nothing to do. */
7389 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
7391 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
7392 | WINED3D_FRAGMENT_CAP_SRGB_WRITE;
7393 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
7394 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
7395 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
7396 | WINED3DTEXOPCAPS_SELECTARG1
7397 | WINED3DTEXOPCAPS_SELECTARG2
7398 | WINED3DTEXOPCAPS_MODULATE4X
7399 | WINED3DTEXOPCAPS_MODULATE2X
7400 | WINED3DTEXOPCAPS_MODULATE
7401 | WINED3DTEXOPCAPS_ADDSIGNED2X
7402 | WINED3DTEXOPCAPS_ADDSIGNED
7403 | WINED3DTEXOPCAPS_ADD
7404 | WINED3DTEXOPCAPS_SUBTRACT
7405 | WINED3DTEXOPCAPS_ADDSMOOTH
7406 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
7407 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
7408 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
7409 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
7410 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
7411 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
7412 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
7413 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
7414 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
7415 | WINED3DTEXOPCAPS_DOTPRODUCT3
7416 | WINED3DTEXOPCAPS_MULTIPLYADD
7417 | WINED3DTEXOPCAPS_LERP
7418 | WINED3DTEXOPCAPS_BUMPENVMAP
7419 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
7420 caps->MaxTextureBlendStages = 8;
7421 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
7424 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7426 struct shader_glsl_priv *priv;
7428 if (shader_backend == &glsl_shader_backend)
7430 priv = shader_priv;
7432 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
7434 ERR("Failed to initialize rbtree.\n");
7435 return NULL;
7438 return priv;
7441 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
7443 return NULL;
7446 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
7448 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7449 struct glsl_ffp_fragment_shader, entry.entry);
7450 struct glsl_shader_prog_link *program, *program2;
7451 struct glsl_ffp_destroy_ctx *ctx = context;
7453 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7454 struct glsl_shader_prog_link, ps.shader_entry)
7456 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7458 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
7459 HeapFree(GetProcessHeap(), 0, shader);
7462 /* Context activation is done by the caller. */
7463 static void glsl_fragment_pipe_free(struct wined3d_device *device)
7465 struct shader_glsl_priv *priv = device->fragment_priv;
7466 struct glsl_ffp_destroy_ctx ctx;
7468 ctx.priv = priv;
7469 ctx.gl_info = &device->adapter->gl_info;
7470 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
7473 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
7474 const struct wined3d_state *state, DWORD state_id)
7476 context->last_was_pshader = use_ps(state);
7478 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7481 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
7482 const struct wined3d_state *state, DWORD state_id)
7484 BOOL use_vshader = use_vs(state);
7485 enum fogsource new_source;
7486 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
7487 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
7489 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7491 if (!state->render_states[WINED3D_RS_FOGENABLE])
7492 return;
7494 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
7496 if (use_vshader)
7497 new_source = FOGSOURCE_VS;
7498 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
7499 new_source = FOGSOURCE_COORD;
7500 else
7501 new_source = FOGSOURCE_FFP;
7503 else
7505 new_source = FOGSOURCE_FFP;
7508 if (new_source != context->fog_source || fogstart == fogend)
7510 context->fog_source = new_source;
7511 state_fogstartend(context, state, STATE_RENDER(WINED3D_RS_FOGSTART));
7515 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
7516 const struct wined3d_state *state, DWORD state_id)
7518 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
7519 glsl_fragment_pipe_fog(context, state, state_id);
7522 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
7523 const struct wined3d_state *state, DWORD state_id)
7525 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7528 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
7529 const struct wined3d_state *state, DWORD state_id)
7531 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7534 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
7536 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
7537 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7538 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7539 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7540 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7541 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7542 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7543 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7544 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7545 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7546 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7547 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7548 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7549 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7550 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7551 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7552 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7553 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7554 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7555 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7556 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7557 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7558 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7559 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7560 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7561 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7562 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7563 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7564 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7565 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7566 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7567 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7568 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7569 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7570 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7571 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7572 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7573 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7574 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7575 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7576 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7577 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7578 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7579 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7580 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7581 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7582 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7583 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7584 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7585 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7586 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7587 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7588 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7589 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7590 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7591 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7592 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7593 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7594 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7595 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7596 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7597 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7598 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7599 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7600 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7601 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7602 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7603 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7604 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7605 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7606 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7607 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7608 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7609 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7610 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
7611 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
7612 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7613 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7614 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
7615 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
7616 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
7617 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7618 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
7619 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
7620 {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 },
7621 {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 },
7622 {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 },
7623 {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 },
7624 {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 },
7625 {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 },
7626 {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 },
7627 {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 },
7628 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7629 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7630 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7631 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7632 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7633 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7634 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7635 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7636 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7637 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
7640 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
7642 return TRUE;
7645 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
7649 const struct fragment_pipeline glsl_fragment_pipe =
7651 glsl_fragment_pipe_enable,
7652 glsl_fragment_pipe_get_caps,
7653 glsl_fragment_pipe_alloc,
7654 glsl_fragment_pipe_free,
7655 glsl_fragment_pipe_alloc_context_data,
7656 glsl_fragment_pipe_free_context_data,
7657 shader_glsl_color_fixup_supported,
7658 glsl_fragment_pipe_state_template,