dbghelp: Parse the DWARF information in Mach-O modules.
[wine.git] / dlls / wined3d / glsl_shader.c
blob1f6ce095c998004cd5c42a48310e086187b6dd45
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 struct wined3d_string_buffer *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_string_buffer shader_buffer;
95 struct wined3d_string_buffer_list string_buffers;
96 struct wine_rb_tree program_lookup;
97 struct constant_heap vconst_heap;
98 struct constant_heap pconst_heap;
99 unsigned char *stack;
100 GLuint depth_blt_program_full[WINED3D_GL_RES_TYPE_COUNT];
101 GLuint depth_blt_program_masked[WINED3D_GL_RES_TYPE_COUNT];
102 UINT next_constant_version;
104 const struct wined3d_vertex_pipe_ops *vertex_pipe;
105 const struct fragment_pipeline *fragment_pipe;
106 struct wine_rb_tree ffp_vertex_shaders;
107 struct wine_rb_tree ffp_fragment_shaders;
108 BOOL ffp_proj_control;
109 BOOL legacy_lighting;
112 struct glsl_vs_program
114 struct list shader_entry;
115 GLuint id;
116 GLenum vertex_color_clamp;
117 GLint *uniform_f_locations;
118 GLint uniform_i_locations[MAX_CONST_I];
119 GLint uniform_b_locations[MAX_CONST_B];
120 GLint pos_fixup_location;
122 GLint modelview_matrix_location;
123 GLint projection_matrix_location;
124 GLint normal_matrix_location;
125 GLint texture_matrix_location[MAX_TEXTURES];
126 GLint material_ambient_location;
127 GLint material_diffuse_location;
128 GLint material_specular_location;
129 GLint material_emissive_location;
130 GLint material_shininess_location;
131 GLint light_ambient_location;
132 struct
134 GLint diffuse;
135 GLint specular;
136 GLint ambient;
137 GLint position;
138 GLint direction;
139 GLint range;
140 GLint falloff;
141 GLint c_att;
142 GLint l_att;
143 GLint q_att;
144 GLint cos_htheta;
145 GLint cos_hphi;
146 } light_location[MAX_ACTIVE_LIGHTS];
147 GLint pointsize_location;
148 GLint pointsize_min_location;
149 GLint pointsize_max_location;
150 GLint pointsize_c_att_location;
151 GLint pointsize_l_att_location;
152 GLint pointsize_q_att_location;
155 struct glsl_gs_program
157 struct list shader_entry;
158 GLuint id;
161 struct glsl_ps_program
163 struct list shader_entry;
164 GLuint id;
165 GLint *uniform_f_locations;
166 GLint uniform_i_locations[MAX_CONST_I];
167 GLint uniform_b_locations[MAX_CONST_B];
168 GLint bumpenv_mat_location[MAX_TEXTURES];
169 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
170 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
171 GLint tss_constant_location[MAX_TEXTURES];
172 GLint tex_factor_location;
173 GLint specular_enable_location;
174 GLint fog_color_location;
175 GLint fog_density_location;
176 GLint fog_end_location;
177 GLint fog_scale_location;
178 GLint ycorrection_location;
179 GLint np2_fixup_location;
180 GLint color_key_location;
181 const struct ps_np2fixup_info *np2_fixup_info;
184 /* Struct to maintain data about a linked GLSL program */
185 struct glsl_shader_prog_link
187 struct wine_rb_entry program_lookup_entry;
188 struct glsl_vs_program vs;
189 struct glsl_gs_program gs;
190 struct glsl_ps_program ps;
191 GLuint id;
192 DWORD constant_update_mask;
193 UINT constant_version;
196 struct glsl_program_key
198 GLuint vs_id;
199 GLuint gs_id;
200 GLuint ps_id;
203 struct shader_glsl_ctx_priv {
204 const struct vs_compile_args *cur_vs_args;
205 const struct ps_compile_args *cur_ps_args;
206 struct ps_np2fixup_info *cur_np2fixup_info;
207 struct wined3d_string_buffer_list *string_buffers;
210 struct glsl_context_data
212 struct glsl_shader_prog_link *glsl_program;
215 struct glsl_ps_compiled_shader
217 struct ps_compile_args args;
218 struct ps_np2fixup_info np2fixup;
219 GLuint id;
222 struct glsl_vs_compiled_shader
224 struct vs_compile_args args;
225 GLuint id;
228 struct glsl_gs_compiled_shader
230 GLuint id;
233 struct glsl_shader_private
235 union
237 struct glsl_vs_compiled_shader *vs;
238 struct glsl_gs_compiled_shader *gs;
239 struct glsl_ps_compiled_shader *ps;
240 } gl_shaders;
241 UINT num_gl_shaders, shader_array_size;
244 struct glsl_ffp_vertex_shader
246 struct wined3d_ffp_vs_desc desc;
247 GLuint id;
248 struct list linked_programs;
251 struct glsl_ffp_fragment_shader
253 struct ffp_frag_desc entry;
254 GLuint id;
255 struct list linked_programs;
258 struct glsl_ffp_destroy_ctx
260 struct shader_glsl_priv *priv;
261 const struct wined3d_gl_info *gl_info;
264 static const char *debug_gl_shader_type(GLenum type)
266 switch (type)
268 #define WINED3D_TO_STR(u) case u: return #u
269 WINED3D_TO_STR(GL_VERTEX_SHADER);
270 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
271 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
272 #undef WINED3D_TO_STR
273 default:
274 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
278 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
280 switch (type)
282 case WINED3D_SHADER_TYPE_VERTEX:
283 return "vs";
285 case WINED3D_SHADER_TYPE_GEOMETRY:
286 return "gs";
288 case WINED3D_SHADER_TYPE_PIXEL:
289 return "ps";
291 default:
292 FIXME("Unhandled shader type %#x.\n", type);
293 return "unknown";
297 static const char *shader_glsl_get_version(const struct wined3d_gl_info *gl_info,
298 const struct wined3d_shader_version *version)
300 if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30) && version->major >= 4)
301 return "#version 130";
302 else
303 return "#version 120";
306 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
308 char str[4][17];
310 wined3d_ftoa(values[0], str[0]);
311 wined3d_ftoa(values[1], str[1]);
312 wined3d_ftoa(values[2], str[2]);
313 wined3d_ftoa(values[3], str[3]);
314 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
317 static const char *get_info_log_line(const char **ptr)
319 const char *p, *q;
321 p = *ptr;
322 if (!(q = strstr(p, "\n")))
324 if (!*p) return NULL;
325 *ptr += strlen(p);
326 return p;
328 *ptr = q + 1;
330 return p;
333 /* Context activation is done by the caller. */
334 static void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
336 int length = 0;
337 char *log;
339 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
340 return;
342 if (program)
343 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
344 else
345 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
347 /* A size of 1 is just a null-terminated string, so the log should be bigger than
348 * that if there are errors. */
349 if (length > 1)
351 const char *ptr, *line;
353 log = HeapAlloc(GetProcessHeap(), 0, length);
354 /* The info log is supposed to be zero-terminated, but at least some
355 * versions of fglrx don't terminate the string properly. The reported
356 * length does include the terminator, so explicitly set it to zero
357 * here. */
358 log[length - 1] = 0;
359 if (program)
360 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
361 else
362 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
364 ptr = log;
365 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
367 WARN("Info log received from GLSL shader #%u:\n", id);
368 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
370 else
372 FIXME("Info log received from GLSL shader #%u:\n", id);
373 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
375 HeapFree(GetProcessHeap(), 0, log);
379 /* Context activation is done by the caller. */
380 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
382 const char *ptr, *line;
384 TRACE("Compiling shader object %u.\n", shader);
386 if (TRACE_ON(d3d_shader))
388 ptr = src;
389 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
392 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
393 checkGLcall("glShaderSource");
394 GL_EXTCALL(glCompileShader(shader));
395 checkGLcall("glCompileShader");
396 print_glsl_info_log(gl_info, shader, FALSE);
399 /* Context activation is done by the caller. */
400 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
402 GLint i, shader_count, source_size = -1;
403 GLuint *shaders;
404 char *source = NULL;
406 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
407 shaders = HeapAlloc(GetProcessHeap(), 0, shader_count * sizeof(*shaders));
408 if (!shaders)
410 ERR("Failed to allocate shader array memory.\n");
411 return;
414 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
415 for (i = 0; i < shader_count; ++i)
417 const char *ptr, *line;
418 GLint tmp;
420 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
422 if (source_size < tmp)
424 HeapFree(GetProcessHeap(), 0, source);
426 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
427 if (!source)
429 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
430 HeapFree(GetProcessHeap(), 0, shaders);
431 return;
433 source_size = tmp;
436 FIXME("Shader %u:\n", shaders[i]);
437 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
438 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
439 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
440 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
441 FIXME("\n");
443 ptr = source;
444 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
445 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
446 FIXME("\n");
449 HeapFree(GetProcessHeap(), 0, source);
450 HeapFree(GetProcessHeap(), 0, shaders);
453 /* Context activation is done by the caller. */
454 static void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
456 GLint tmp;
458 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
459 return;
461 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
462 if (!tmp)
464 FIXME("Program %u link status invalid.\n", program);
465 shader_glsl_dump_program_source(gl_info, program);
468 print_glsl_info_log(gl_info, program, TRUE);
471 /* Context activation is done by the caller. */
472 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
473 struct shader_glsl_priv *priv, const DWORD *tex_unit_map, GLuint program_id)
475 unsigned int mapped_unit;
476 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
477 const char *prefix;
478 unsigned int i, j;
479 GLint name_loc;
481 static const struct
483 enum wined3d_shader_type type;
484 unsigned int base_idx;
485 unsigned int count;
487 sampler_info[] =
489 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
490 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
493 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
495 prefix = shader_glsl_get_prefix(sampler_info[i].type);
497 for (j = 0; j < sampler_info[i].count; ++j)
499 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, j);
500 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
501 if (name_loc == -1)
502 continue;
504 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
505 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
507 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
508 continue;
511 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
512 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
515 checkGLcall("glUniform1i");
516 string_buffer_release(&priv->string_buffers, sampler_name);
519 /* Context activation is done by the caller. */
520 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
521 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
523 unsigned int start = ~0U, end = 0;
524 int stack_idx = 0;
525 unsigned int heap_idx = 1;
526 unsigned int idx;
528 if (heap->entries[heap_idx].version <= version) return;
530 idx = heap->entries[heap_idx].idx;
531 if (constant_locations[idx] != -1)
532 start = end = idx;
533 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
535 while (stack_idx >= 0)
537 /* Note that we fall through to the next case statement. */
538 switch(stack[stack_idx])
540 case HEAP_NODE_TRAVERSE_LEFT:
542 unsigned int left_idx = heap_idx << 1;
543 if (left_idx < heap->size && heap->entries[left_idx].version > version)
545 heap_idx = left_idx;
546 idx = heap->entries[heap_idx].idx;
547 if (constant_locations[idx] != -1)
549 if (start > idx)
550 start = idx;
551 if (end < idx)
552 end = idx;
555 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
556 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
557 break;
561 case HEAP_NODE_TRAVERSE_RIGHT:
563 unsigned int right_idx = (heap_idx << 1) + 1;
564 if (right_idx < heap->size && heap->entries[right_idx].version > version)
566 heap_idx = right_idx;
567 idx = heap->entries[heap_idx].idx;
568 if (constant_locations[idx] != -1)
570 if (start > idx)
571 start = idx;
572 if (end < idx)
573 end = idx;
576 stack[stack_idx++] = HEAP_NODE_POP;
577 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
578 break;
582 case HEAP_NODE_POP:
583 heap_idx >>= 1;
584 --stack_idx;
585 break;
588 if (start <= end)
589 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start * 4]));
590 checkGLcall("walk_constant_heap()");
593 /* Context activation is done by the caller. */
594 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
596 GLfloat clamped_constant[4];
598 if (location == -1) return;
600 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
601 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
602 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
603 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
605 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
608 /* Context activation is done by the caller. */
609 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
610 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
612 int stack_idx = 0;
613 unsigned int heap_idx = 1;
614 unsigned int idx;
616 if (heap->entries[heap_idx].version <= version) return;
618 idx = heap->entries[heap_idx].idx;
619 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
620 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
622 while (stack_idx >= 0)
624 /* Note that we fall through to the next case statement. */
625 switch(stack[stack_idx])
627 case HEAP_NODE_TRAVERSE_LEFT:
629 unsigned int left_idx = heap_idx << 1;
630 if (left_idx < heap->size && heap->entries[left_idx].version > version)
632 heap_idx = left_idx;
633 idx = heap->entries[heap_idx].idx;
634 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
636 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
637 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
638 break;
642 case HEAP_NODE_TRAVERSE_RIGHT:
644 unsigned int right_idx = (heap_idx << 1) + 1;
645 if (right_idx < heap->size && heap->entries[right_idx].version > version)
647 heap_idx = right_idx;
648 idx = heap->entries[heap_idx].idx;
649 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
651 stack[stack_idx++] = HEAP_NODE_POP;
652 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
653 break;
657 case HEAP_NODE_POP:
658 heap_idx >>= 1;
659 --stack_idx;
660 break;
663 checkGLcall("walk_constant_heap_clamped()");
666 /* Context activation is done by the caller. */
667 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
668 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
669 unsigned char *stack, UINT version)
671 const struct wined3d_shader_lconst *lconst;
673 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
674 if (shader->reg_maps.shader_version.major == 1
675 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
676 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
677 else
678 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
680 if (!shader->load_local_constsF)
682 TRACE("No need to load local float constants for this shader\n");
683 return;
686 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
687 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
689 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
691 checkGLcall("glUniform4fv()");
694 /* Context activation is done by the caller. */
695 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
696 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
698 unsigned int i;
699 struct list* ptr;
701 for (i = 0; constants_set; constants_set >>= 1, ++i)
703 if (!(constants_set & 1)) continue;
705 /* We found this uniform name in the program - go ahead and send the data */
706 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i * 4]));
709 /* Load immediate constants */
710 ptr = list_head(&shader->constantsI);
711 while (ptr)
713 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
714 unsigned int idx = lconst->idx;
715 const GLint *values = (const GLint *)lconst->value;
717 /* We found this uniform name in the program - go ahead and send the data */
718 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
719 ptr = list_next(&shader->constantsI, ptr);
721 checkGLcall("glUniform4iv()");
724 /* Context activation is done by the caller. */
725 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
726 const GLint locations[MAX_CONST_B], const BOOL *constants, WORD constants_set)
728 unsigned int i;
729 struct list* ptr;
731 for (i = 0; constants_set; constants_set >>= 1, ++i)
733 if (!(constants_set & 1)) continue;
735 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
738 /* Load immediate constants */
739 ptr = list_head(&shader->constantsB);
740 while (ptr)
742 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
743 unsigned int idx = lconst->idx;
744 const GLint *values = (const GLint *)lconst->value;
746 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
747 ptr = list_next(&shader->constantsB, ptr);
749 checkGLcall("glUniform1iv()");
752 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
754 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
757 /* Context activation is done by the caller (state handler). */
758 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
759 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
761 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
762 UINT fixup = ps->np2_fixup_info->active;
763 UINT i;
765 for (i = 0; fixup; fixup >>= 1, ++i)
767 const struct wined3d_texture *tex = state->textures[i];
768 unsigned char idx = ps->np2_fixup_info->idx[i];
769 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
771 if (!tex)
773 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
774 continue;
777 if (idx % 2)
779 tex_dim[2] = tex->pow2_matrix[0];
780 tex_dim[3] = tex->pow2_matrix[5];
782 else
784 tex_dim[0] = tex->pow2_matrix[0];
785 tex_dim[1] = tex->pow2_matrix[5];
789 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
792 /* Taken and adapted from Mesa. */
793 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
795 float pos, neg, t, det;
796 struct wined3d_matrix temp;
798 /* Calculate the determinant of upper left 3x3 submatrix and
799 * determine if the matrix is singular. */
800 pos = neg = 0.0f;
801 t = in->_11 * in->_22 * in->_33;
802 if (t >= 0.0f)
803 pos += t;
804 else
805 neg += t;
807 t = in->_21 * in->_32 * in->_13;
808 if (t >= 0.0f)
809 pos += t;
810 else
811 neg += t;
812 t = in->_31 * in->_12 * in->_23;
813 if (t >= 0.0f)
814 pos += t;
815 else
816 neg += t;
818 t = -in->_31 * in->_22 * in->_13;
819 if (t >= 0.0f)
820 pos += t;
821 else
822 neg += t;
823 t = -in->_21 * in->_12 * in->_33;
824 if (t >= 0.0f)
825 pos += t;
826 else
827 neg += t;
829 t = -in->_11 * in->_32 * in->_23;
830 if (t >= 0.0f)
831 pos += t;
832 else
833 neg += t;
835 det = pos + neg;
837 if (fabsf(det) < 1e-25f)
838 return FALSE;
840 det = 1.0f / det;
841 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
842 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
843 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
844 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
845 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
846 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
847 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
848 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
849 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
851 *out = temp;
852 return TRUE;
855 static void swap_rows(float **a, float **b)
857 float *tmp = *a;
859 *a = *b;
860 *b = tmp;
863 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
865 float wtmp[4][8];
866 float m0, m1, m2, m3, s;
867 float *r0, *r1, *r2, *r3;
869 r0 = wtmp[0];
870 r1 = wtmp[1];
871 r2 = wtmp[2];
872 r3 = wtmp[3];
874 r0[0] = m->_11;
875 r0[1] = m->_12;
876 r0[2] = m->_13;
877 r0[3] = m->_14;
878 r0[4] = 1.0f;
879 r0[5] = r0[6] = r0[7] = 0.0f;
881 r1[0] = m->_21;
882 r1[1] = m->_22;
883 r1[2] = m->_23;
884 r1[3] = m->_24;
885 r1[5] = 1.0f;
886 r1[4] = r1[6] = r1[7] = 0.0f;
888 r2[0] = m->_31;
889 r2[1] = m->_32;
890 r2[2] = m->_33;
891 r2[3] = m->_34;
892 r2[6] = 1.0f;
893 r2[4] = r2[5] = r2[7] = 0.0f;
895 r3[0] = m->_41;
896 r3[1] = m->_42;
897 r3[2] = m->_43;
898 r3[3] = m->_44;
899 r3[7] = 1.0f;
900 r3[4] = r3[5] = r3[6] = 0.0f;
902 /* Choose pivot - or die. */
903 if (fabsf(r3[0]) > fabsf(r2[0]))
904 swap_rows(&r3, &r2);
905 if (fabsf(r2[0]) > fabsf(r1[0]))
906 swap_rows(&r2, &r1);
907 if (fabsf(r1[0]) > fabsf(r0[0]))
908 swap_rows(&r1, &r0);
909 if (r0[0] == 0.0f)
910 return FALSE;
912 /* Eliminate first variable. */
913 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
914 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
915 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
916 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
917 s = r0[4];
918 if (s != 0.0f)
920 r1[4] -= m1 * s;
921 r2[4] -= m2 * s;
922 r3[4] -= m3 * s;
924 s = r0[5];
925 if (s != 0.0f)
927 r1[5] -= m1 * s;
928 r2[5] -= m2 * s;
929 r3[5] -= m3 * s;
931 s = r0[6];
932 if (s != 0.0f)
934 r1[6] -= m1 * s;
935 r2[6] -= m2 * s;
936 r3[6] -= m3 * s;
938 s = r0[7];
939 if (s != 0.0f)
941 r1[7] -= m1 * s;
942 r2[7] -= m2 * s;
943 r3[7] -= m3 * s;
946 /* Choose pivot - or die. */
947 if (fabsf(r3[1]) > fabsf(r2[1]))
948 swap_rows(&r3, &r2);
949 if (fabsf(r2[1]) > fabsf(r1[1]))
950 swap_rows(&r2, &r1);
951 if (r1[1] == 0.0f)
952 return FALSE;
954 /* Eliminate second variable. */
955 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
956 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
957 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
958 s = r1[4];
959 if (s != 0.0f)
961 r2[4] -= m2 * s;
962 r3[4] -= m3 * s;
964 s = r1[5];
965 if (s != 0.0f)
967 r2[5] -= m2 * s;
968 r3[5] -= m3 * s;
970 s = r1[6];
971 if (s != 0.0f)
973 r2[6] -= m2 * s;
974 r3[6] -= m3 * s;
976 s = r1[7];
977 if (s != 0.0f)
979 r2[7] -= m2 * s;
980 r3[7] -= m3 * s;
983 /* Choose pivot - or die. */
984 if (fabsf(r3[2]) > fabsf(r2[2]))
985 swap_rows(&r3, &r2);
986 if (r2[2] == 0.0f)
987 return FALSE;
989 /* Eliminate third variable. */
990 m3 = r3[2] / r2[2];
991 r3[3] -= m3 * r2[3];
992 r3[4] -= m3 * r2[4];
993 r3[5] -= m3 * r2[5];
994 r3[6] -= m3 * r2[6];
995 r3[7] -= m3 * r2[7];
997 /* Last check. */
998 if (r3[3] == 0.0f)
999 return FALSE;
1001 /* Back substitute row 3. */
1002 s = 1.0f / r3[3];
1003 r3[4] *= s;
1004 r3[5] *= s;
1005 r3[6] *= s;
1006 r3[7] *= s;
1008 /* Back substitute row 2. */
1009 m2 = r2[3];
1010 s = 1.0f / r2[2];
1011 r2[4] = s * (r2[4] - r3[4] * m2);
1012 r2[5] = s * (r2[5] - r3[5] * m2);
1013 r2[6] = s * (r2[6] - r3[6] * m2);
1014 r2[7] = s * (r2[7] - r3[7] * m2);
1015 m1 = r1[3];
1016 r1[4] -= r3[4] * m1;
1017 r1[5] -= r3[5] * m1;
1018 r1[6] -= r3[6] * m1;
1019 r1[7] -= r3[7] * m1;
1020 m0 = r0[3];
1021 r0[4] -= r3[4] * m0;
1022 r0[5] -= r3[5] * m0;
1023 r0[6] -= r3[6] * m0;
1024 r0[7] -= r3[7] * m0;
1026 /* Back substitute row 1. */
1027 m1 = r1[2];
1028 s = 1.0f / r1[1];
1029 r1[4] = s * (r1[4] - r2[4] * m1);
1030 r1[5] = s * (r1[5] - r2[5] * m1);
1031 r1[6] = s * (r1[6] - r2[6] * m1);
1032 r1[7] = s * (r1[7] - r2[7] * m1);
1033 m0 = r0[2];
1034 r0[4] -= r2[4] * m0;
1035 r0[5] -= r2[5] * m0;
1036 r0[6] -= r2[6] * m0;
1037 r0[7] -= r2[7] * m0;
1039 /* Back substitute row 0. */
1040 m0 = r0[1];
1041 s = 1.0f / r0[0];
1042 r0[4] = s * (r0[4] - r1[4] * m0);
1043 r0[5] = s * (r0[5] - r1[5] * m0);
1044 r0[6] = s * (r0[6] - r1[6] * m0);
1045 r0[7] = s * (r0[7] - r1[7] * m0);
1047 out->_11 = r0[4];
1048 out->_12 = r0[5];
1049 out->_13 = r0[6];
1050 out->_14 = r0[7];
1051 out->_21 = r1[4];
1052 out->_22 = r1[5];
1053 out->_23 = r1[6];
1054 out->_24 = r1[7];
1055 out->_31 = r2[4];
1056 out->_32 = r2[5];
1057 out->_33 = r2[6];
1058 out->_34 = r2[7];
1059 out->_41 = r3[4];
1060 out->_42 = r3[5];
1061 out->_43 = r3[6];
1062 out->_44 = r3[7];
1064 return TRUE;
1067 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1068 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1070 const struct wined3d_gl_info *gl_info = context->gl_info;
1071 float mat[3 * 3];
1072 struct wined3d_matrix mv;
1073 unsigned int i, j;
1075 get_modelview_matrix(context, state, &mv);
1076 if (context->swapchain->device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING)
1077 invert_matrix_3d(&mv, &mv);
1078 else
1079 invert_matrix(&mv, &mv);
1080 /* Tests show that singular modelview matrices are used unchanged as normal
1081 * matrices on D3D3 and older. There seems to be no clearly consistent
1082 * behavior on newer D3D versions so always follow older ddraw behavior. */
1083 for (i = 0; i < 3; ++i)
1084 for (j = 0; j < 3; ++j)
1085 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1087 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1088 checkGLcall("glUniformMatrix3fv");
1091 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1092 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1094 const struct wined3d_gl_info *gl_info = context->gl_info;
1095 struct wined3d_matrix mat;
1097 if (tex >= MAX_TEXTURES)
1098 return;
1099 if (prog->vs.texture_matrix_location[tex] == -1)
1100 return;
1102 get_texture_matrix(context, state, tex, &mat);
1103 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1104 checkGLcall("glUniformMatrix4fv");
1107 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1108 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1110 const struct wined3d_gl_info *gl_info = context->gl_info;
1112 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1114 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1115 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1117 else
1119 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1121 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1123 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1124 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1125 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1126 checkGLcall("setting FFP material uniforms");
1129 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1130 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1132 const struct wined3d_gl_info *gl_info = context->gl_info;
1133 float col[4];
1135 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_AMBIENT], col);
1136 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, col));
1137 checkGLcall("glUniform3fv");
1140 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1141 const struct wined3d_matrix *src2)
1143 struct wined3d_vec4 temp;
1145 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1146 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1147 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1148 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1150 *dest = temp;
1153 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1154 const struct wined3d_state *state, unsigned int light, struct glsl_shader_prog_link *prog)
1156 const struct wined3d_gl_info *gl_info = context->gl_info;
1157 const struct wined3d_light_info *light_info = state->lights[light];
1158 struct wined3d_vec4 vec4;
1159 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1161 if (!light_info)
1162 return;
1164 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1165 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1166 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1168 switch (light_info->OriginalParms.type)
1170 case WINED3D_LIGHT_POINT:
1171 multiply_vector_matrix(&vec4, &light_info->position, view);
1172 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1173 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1174 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1175 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1176 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1177 break;
1179 case WINED3D_LIGHT_SPOT:
1180 multiply_vector_matrix(&vec4, &light_info->position, view);
1181 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1183 multiply_vector_matrix(&vec4, &light_info->direction, view);
1184 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1186 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1187 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1188 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1189 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1190 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1191 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1192 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1193 break;
1195 case WINED3D_LIGHT_DIRECTIONAL:
1196 multiply_vector_matrix(&vec4, &light_info->direction, view);
1197 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1198 break;
1200 case WINED3D_LIGHT_PARALLELPOINT:
1201 multiply_vector_matrix(&vec4, &light_info->position, view);
1202 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1203 break;
1205 default:
1206 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1208 checkGLcall("setting FFP lights uniforms");
1211 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1212 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1214 const struct wined3d_gl_info *gl_info = context->gl_info;
1215 float min, max;
1216 float size, att[3];
1218 get_pointsize_minmax(context, state, &min, &max);
1220 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1221 checkGLcall("glUniform1f");
1222 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1223 checkGLcall("glUniform1f");
1225 get_pointsize(context, state, &size, att);
1227 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1228 checkGLcall("glUniform1f");
1229 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1230 checkGLcall("glUniform1f");
1231 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1232 checkGLcall("glUniform1f");
1233 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1234 checkGLcall("glUniform1f");
1237 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1238 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1240 const struct wined3d_gl_info *gl_info = context->gl_info;
1241 float start, end, scale;
1242 union
1244 DWORD d;
1245 float f;
1246 } tmpvalue;
1247 float col[4];
1249 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_FOGCOLOR], col);
1250 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, col));
1251 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1252 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1253 get_fog_start_end(context, state, &start, &end);
1254 scale = 1.0f / (end - start);
1255 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1256 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1257 checkGLcall("fog emulation uniforms");
1260 /* Context activation is done by the caller (state handler). */
1261 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1262 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1264 struct wined3d_color float_key;
1265 const struct wined3d_texture *texture = state->textures[0];
1267 wined3d_format_convert_color_to_float(texture->resource.format, NULL,
1268 texture->async.src_blt_color_key.color_space_high_value, &float_key);
1269 GL_EXTCALL(glUniform4fv(ps->color_key_location, 1, &float_key.r));
1272 /* Context activation is done by the caller (state handler). */
1273 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1274 const struct wined3d_state *state)
1276 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1277 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1278 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1279 const struct wined3d_gl_info *gl_info = context->gl_info;
1280 struct shader_glsl_priv *priv = shader_priv;
1281 float position_fixup[4];
1282 DWORD update_mask = 0;
1284 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1285 UINT constant_version;
1286 int i;
1288 if (!prog) {
1289 /* No GLSL program set - nothing to do. */
1290 return;
1292 constant_version = prog->constant_version;
1293 update_mask = context->constant_update_mask & prog->constant_update_mask;
1295 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1296 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
1297 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1299 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1300 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
1301 vshader->reg_maps.integer_constants);
1303 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1304 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1305 vshader->reg_maps.boolean_constants);
1307 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1308 shader_glsl_pointsize_uniform(context, state, prog);
1310 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
1312 shader_get_position_fixup(context, state, position_fixup);
1313 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1314 checkGLcall("glUniform4fv");
1317 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1319 struct wined3d_matrix mat;
1321 get_modelview_matrix(context, state, &mat);
1322 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location, 1, FALSE, &mat._11));
1323 checkGLcall("glUniformMatrix4fv");
1325 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1328 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1330 struct wined3d_matrix projection;
1332 get_projection_matrix(context, state, &projection);
1333 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1334 checkGLcall("glUniformMatrix4fv");
1337 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1339 for (i = 0; i < MAX_TEXTURES; ++i)
1340 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1343 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1344 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1346 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1348 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1349 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1350 shader_glsl_ffp_vertex_light_uniform(context, state, i, prog);
1353 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1354 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
1355 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1357 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1358 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
1359 pshader->reg_maps.integer_constants);
1361 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1362 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1363 pshader->reg_maps.boolean_constants);
1365 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1367 for (i = 0; i < MAX_TEXTURES; ++i)
1369 if (prog->ps.bumpenv_mat_location[i] == -1)
1370 continue;
1372 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1373 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1375 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1377 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1378 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1379 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1380 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1384 checkGLcall("bump env uniforms");
1387 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1389 float correction_params[] =
1391 /* position is window relative, not viewport relative */
1392 context->render_offscreen ? 0.0f : (float)context->current_rt->resource.height,
1393 context->render_offscreen ? 1.0f : -1.0f,
1394 0.0f,
1395 0.0f,
1398 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, correction_params));
1401 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1402 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1403 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1404 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1406 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1408 float col[4];
1410 if (prog->ps.tex_factor_location != -1)
1412 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_TEXTUREFACTOR], col);
1413 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, col));
1416 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1417 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1418 else
1419 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1421 for (i = 0; i < MAX_TEXTURES; ++i)
1423 if (prog->ps.tss_constant_location[i] == -1)
1424 continue;
1426 D3DCOLORTOGLFLOAT4(state->texture_states[i][WINED3D_TSS_CONSTANT], col);
1427 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, col));
1430 checkGLcall("fixed function uniforms");
1433 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1434 shader_glsl_load_fog_uniform(context, state, prog);
1436 if (priv->next_constant_version == UINT_MAX)
1438 TRACE("Max constant version reached, resetting to 0.\n");
1439 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1440 priv->next_constant_version = 1;
1442 else
1444 prog->constant_version = priv->next_constant_version++;
1448 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1450 struct constant_entry *entries = heap->entries;
1451 unsigned int *positions = heap->positions;
1452 unsigned int heap_idx, parent_idx;
1454 if (!heap->contained[idx])
1456 heap_idx = heap->size++;
1457 heap->contained[idx] = TRUE;
1459 else
1461 heap_idx = positions[idx];
1464 while (heap_idx > 1)
1466 parent_idx = heap_idx >> 1;
1468 if (new_version <= entries[parent_idx].version) break;
1470 entries[heap_idx] = entries[parent_idx];
1471 positions[entries[parent_idx].idx] = heap_idx;
1472 heap_idx = parent_idx;
1475 entries[heap_idx].version = new_version;
1476 entries[heap_idx].idx = idx;
1477 positions[idx] = heap_idx;
1480 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1482 struct shader_glsl_priv *priv = device->shader_priv;
1483 struct constant_heap *heap = &priv->vconst_heap;
1484 UINT i;
1486 for (i = start; i < count + start; ++i)
1488 update_heap_entry(heap, i, priv->next_constant_version);
1491 for (i = 0; i < device->context_count; ++i)
1493 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
1497 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1499 struct shader_glsl_priv *priv = device->shader_priv;
1500 struct constant_heap *heap = &priv->pconst_heap;
1501 UINT i;
1503 for (i = start; i < count + start; ++i)
1505 update_heap_entry(heap, i, priv->next_constant_version);
1508 for (i = 0; i < device->context_count; ++i)
1510 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
1514 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1516 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1517 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1518 if(shader_major > 3) return ret;
1520 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1521 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1522 return ret;
1525 /** Generate the variable & register declarations for the GLSL output target */
1526 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1527 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1528 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1530 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1531 const struct wined3d_state *state = &shader->device->state;
1532 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1533 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1534 const struct wined3d_gl_info *gl_info = context->gl_info;
1535 const struct wined3d_fb_state *fb = &shader->device->fb;
1536 unsigned int i, extra_constants_needed = 0;
1537 const struct wined3d_shader_lconst *lconst;
1538 const char *prefix;
1539 DWORD map;
1541 prefix = shader_glsl_get_prefix(version->type);
1543 /* Prototype the subroutines */
1544 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1546 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1549 /* Declare the constants (aka uniforms) */
1550 if (shader->limits->constant_float > 0)
1552 unsigned max_constantsF;
1554 /* Unless the shader uses indirect addressing, always declare the
1555 * maximum array size and ignore that we need some uniforms privately.
1556 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1557 * and immediate values, still declare VC[256]. If the shader needs
1558 * more uniforms than we have it won't work in any case. If it uses
1559 * less, the compiler will figure out which uniforms are really used
1560 * and strip them out. This allows a shader to use c255 on a dx9 card,
1561 * as long as it doesn't also use all the other constants.
1563 * If the shader uses indirect addressing the compiler must assume
1564 * that all declared uniforms are used. In this case, declare only the
1565 * amount that we're assured to have.
1567 * Thus we run into problems in these two cases:
1568 * 1) The shader really uses more uniforms than supported.
1569 * 2) The shader uses indirect addressing, less constants than
1570 * supported, but uses a constant index > #supported consts. */
1571 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1573 /* No indirect addressing here. */
1574 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1576 else
1578 if (reg_maps->usesrelconstF)
1580 /* Subtract the other potential uniforms from the max
1581 * available (bools, ints, and 1 row of projection matrix).
1582 * Subtract another uniform for immediate values, which have
1583 * to be loaded via uniform by the driver as well. The shader
1584 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1585 * shader code, so one vec4 should be enough. (Unfortunately
1586 * the Nvidia driver doesn't store 128 and -128 in one float).
1588 * Writing gl_ClipVertex requires one uniform for each
1589 * clipplane as well. */
1590 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1591 if(ctx_priv->cur_vs_args->clip_enabled)
1593 max_constantsF -= gl_info->limits.clipplanes;
1595 max_constantsF -= count_bits(reg_maps->integer_constants);
1596 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1597 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1598 * for now take this into account when calculating the number of available constants
1600 max_constantsF -= count_bits(reg_maps->boolean_constants);
1601 /* Set by driver quirks in directx.c */
1602 max_constantsF -= gl_info->reserved_glsl_constants;
1604 if (max_constantsF < shader->limits->constant_float)
1606 static unsigned int once;
1608 if (!once++)
1609 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1610 " it may not render correctly.\n");
1611 else
1612 WARN("The hardware does not support enough uniform components to run this shader.\n");
1615 else
1617 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1620 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1621 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1624 /* Always declare the full set of constants, the compiler can remove the
1625 * unused ones because d3d doesn't (yet) support indirect int and bool
1626 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1627 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1628 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1630 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1631 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1633 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1635 if (reg_maps->cb_sizes[i])
1636 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1637 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1640 /* Declare texture samplers */
1641 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1643 struct wined3d_shader_sampler_map_entry *entry;
1644 BOOL shadow_sampler, tex_rect;
1645 const char *sampler_type;
1647 entry = &reg_maps->sampler_map.entries[i];
1649 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1651 ERR("Invalid resource index %u.\n", entry->resource_idx);
1652 continue;
1655 shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1 << entry->sampler_idx));
1656 switch (reg_maps->resource_info[entry->resource_idx].type)
1658 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1659 if (shadow_sampler)
1660 sampler_type = "sampler1DShadow";
1661 else
1662 sampler_type = "sampler1D";
1663 break;
1665 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1666 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1667 && (ps_args->np2_fixup & (1 << entry->resource_idx))
1668 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1669 if (shadow_sampler)
1671 if (tex_rect)
1672 sampler_type = "sampler2DRectShadow";
1673 else
1674 sampler_type = "sampler2DShadow";
1676 else
1678 if (tex_rect)
1679 sampler_type = "sampler2DRect";
1680 else
1681 sampler_type = "sampler2D";
1683 break;
1685 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1686 if (shadow_sampler)
1687 FIXME("Unsupported 3D shadow sampler.\n");
1688 sampler_type = "sampler3D";
1689 break;
1691 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1692 if (shadow_sampler)
1693 FIXME("Unsupported Cube shadow sampler.\n");
1694 sampler_type = "samplerCube";
1695 break;
1697 default:
1698 sampler_type = "unsupported_sampler";
1699 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[i].type);
1700 break;
1702 shader_addline(buffer, "uniform %s %s_sampler%u;\n", sampler_type, prefix, entry->bind_idx);
1705 /* Declare uniforms for NP2 texcoord fixup:
1706 * This is NOT done inside the loop that declares the texture samplers
1707 * since the NP2 fixup code is currently only used for the GeforceFX
1708 * series and when forcing the ARB_npot extension off. Modern cards just
1709 * skip the code anyway, so put it inside a separate loop. */
1710 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1712 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1713 UINT cur = 0;
1715 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1716 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1717 * samplerNP2Fixup stores texture dimensions and is updated through
1718 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1720 for (i = 0; i < shader->limits->sampler; ++i)
1722 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1 << i)))
1723 continue;
1725 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1727 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1728 continue;
1731 fixup->idx[i] = cur++;
1734 fixup->num_consts = (cur + 1) >> 1;
1735 fixup->active = ps_args->np2_fixup;
1736 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1739 /* Declare address variables */
1740 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1742 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1745 /* Declare texture coordinate temporaries and initialize them */
1746 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1748 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1751 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1753 for (i = 0; i < shader->input_signature.element_count; ++i)
1755 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
1756 if (e->sysval_semantic == WINED3D_SV_INSTANCEID)
1757 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1758 prefix, e->register_idx);
1759 else
1760 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, e->register_idx);
1763 if (vs_args->point_size && !vs_args->per_vertex_point_size)
1765 shader_addline(buffer, "uniform struct\n{\n");
1766 shader_addline(buffer, " float size;\n");
1767 shader_addline(buffer, " float size_min;\n");
1768 shader_addline(buffer, " float size_max;\n");
1769 shader_addline(buffer, "} ffp_point;\n");
1772 shader_addline(buffer, "uniform vec4 posFixup;\n");
1773 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits->packed_output);
1775 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1777 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
1779 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1781 if (version->major < 3 || ps_args->vp_mode != vertexshader)
1783 shader_addline(buffer, "uniform struct\n{\n");
1784 shader_addline(buffer, " vec4 color;\n");
1785 shader_addline(buffer, " float density;\n");
1786 shader_addline(buffer, " float end;\n");
1787 shader_addline(buffer, " float scale;\n");
1788 shader_addline(buffer, "} ffp_fog;\n");
1791 if (version->major >= 3)
1793 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
1795 if (use_vs(state))
1796 shader_addline(buffer, "varying vec4 %s_link[%u];\n", prefix, in_count);
1797 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1800 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1802 if (!(map & 1))
1803 continue;
1805 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1807 if (reg_maps->luminanceparams & (1 << i))
1809 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1810 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1811 extra_constants_needed++;
1814 extra_constants_needed++;
1817 if (ps_args->srgb_correction)
1819 shader_addline(buffer, "const vec4 srgb_const0 = ");
1820 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
1821 shader_addline(buffer, ";\n");
1822 shader_addline(buffer, "const vec4 srgb_const1 = ");
1823 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
1824 shader_addline(buffer, ";\n");
1826 if (reg_maps->vpos || reg_maps->usesdsy)
1828 if (shader->limits->constant_float + extra_constants_needed
1829 + 1 < gl_info->limits.glsl_ps_float_constants)
1831 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1832 extra_constants_needed++;
1834 else
1836 float ycorrection[] =
1838 context->render_offscreen ? 0.0f : fb->render_targets[0]->height,
1839 context->render_offscreen ? 1.0f : -1.0f,
1840 0.0f,
1841 0.0f,
1844 /* This happens because we do not have proper tracking of the
1845 * constant registers that are actually used, only the max
1846 * limit of the shader version. */
1847 FIXME("Cannot find a free uniform for vpos correction params\n");
1848 shader_addline(buffer, "const vec4 ycorrection = ");
1849 shader_glsl_append_imm_vec4(buffer, ycorrection);
1850 shader_addline(buffer, ";\n");
1852 shader_addline(buffer, "vec4 vpos;\n");
1856 /* Declare output register temporaries */
1857 if (shader->limits->packed_output)
1858 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
1860 /* Declare temporary variables */
1861 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1863 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1866 /* Declare loop registers aLx */
1867 if (version->major < 4)
1869 for (i = 0; i < reg_maps->loop_depth; ++i)
1871 shader_addline(buffer, "int aL%u;\n", i);
1872 shader_addline(buffer, "int tmpInt%u;\n", i);
1876 /* Temporary variables for matrix operations */
1877 shader_addline(buffer, "vec4 tmp0;\n");
1878 shader_addline(buffer, "vec4 tmp1;\n");
1880 if (!shader->load_local_constsF)
1882 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1884 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
1885 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
1886 shader_addline(buffer, ";\n");
1890 /* Start the main program. */
1891 shader_addline(buffer, "void main()\n{\n");
1893 /* Direct3D applications expect integer vPos values, while OpenGL drivers
1894 * add approximately 0.5. This causes off-by-one problems as spotted by
1895 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
1896 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
1897 * causes precision troubles when we just subtract 0.5.
1899 * To deal with that, just floor() the position. This will eliminate the
1900 * fraction on all cards.
1902 * TODO: Test how this behaves with multisampling.
1904 * An advantage of floor is that it works even if the driver doesn't add
1905 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
1906 * to return in gl_FragCoord, even though coordinates specify the pixel
1907 * centers instead of the pixel corners. This code will behave correctly
1908 * on drivers that returns integer values. */
1909 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
1911 if (shader->device->wined3d->flags & WINED3D_PIXEL_CENTER_INTEGER)
1912 shader_addline(buffer,
1913 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1914 else
1915 shader_addline(buffer,
1916 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
1920 /*****************************************************************************
1921 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1923 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1924 ****************************************************************************/
1926 /* Prototypes */
1927 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1928 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1930 /** Used for opcode modifiers - They multiply the result by the specified amount */
1931 static const char * const shift_glsl_tab[] = {
1932 "", /* 0 (none) */
1933 "2.0 * ", /* 1 (x2) */
1934 "4.0 * ", /* 2 (x4) */
1935 "8.0 * ", /* 3 (x8) */
1936 "16.0 * ", /* 4 (x16) */
1937 "32.0 * ", /* 5 (x32) */
1938 "", /* 6 (x64) */
1939 "", /* 7 (x128) */
1940 "", /* 8 (d256) */
1941 "", /* 9 (d128) */
1942 "", /* 10 (d64) */
1943 "", /* 11 (d32) */
1944 "0.0625 * ", /* 12 (d16) */
1945 "0.125 * ", /* 13 (d8) */
1946 "0.25 * ", /* 14 (d4) */
1947 "0.5 * " /* 15 (d2) */
1950 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1951 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1952 const char *in_reg, const char *in_regswizzle, char *out_str)
1954 out_str[0] = 0;
1956 switch (src_modifier)
1958 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1959 case WINED3DSPSM_DW:
1960 case WINED3DSPSM_NONE:
1961 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1962 break;
1963 case WINED3DSPSM_NEG:
1964 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1965 break;
1966 case WINED3DSPSM_NOT:
1967 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1968 break;
1969 case WINED3DSPSM_BIAS:
1970 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1971 break;
1972 case WINED3DSPSM_BIASNEG:
1973 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1974 break;
1975 case WINED3DSPSM_SIGN:
1976 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1977 break;
1978 case WINED3DSPSM_SIGNNEG:
1979 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1980 break;
1981 case WINED3DSPSM_COMP:
1982 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1983 break;
1984 case WINED3DSPSM_X2:
1985 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1986 break;
1987 case WINED3DSPSM_X2NEG:
1988 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1989 break;
1990 case WINED3DSPSM_ABS:
1991 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1992 break;
1993 case WINED3DSPSM_ABSNEG:
1994 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1995 break;
1996 default:
1997 FIXME("Unhandled modifier %u\n", src_modifier);
1998 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2002 /** Writes the GLSL variable name that corresponds to the register that the
2003 * DX opcode parameter is trying to access */
2004 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2005 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2007 /* oPos, oFog and oPts in D3D */
2008 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2010 const struct wined3d_shader *shader = ins->ctx->shader;
2011 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2012 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2013 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2014 const char *prefix = shader_glsl_get_prefix(version->type);
2015 struct glsl_src_param rel_param0, rel_param1;
2016 char imm_str[4][17];
2018 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2019 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2020 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2021 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2022 *is_color = FALSE;
2024 switch (reg->type)
2026 case WINED3DSPR_TEMP:
2027 sprintf(register_name, "R%u", reg->idx[0].offset);
2028 break;
2030 case WINED3DSPR_INPUT:
2031 /* vertex shaders */
2032 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2034 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2035 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx[0].offset))
2036 *is_color = TRUE;
2037 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2038 break;
2041 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2043 if (reg->idx[0].rel_addr)
2045 if (reg->idx[1].rel_addr)
2046 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
2047 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2048 else
2049 sprintf(register_name, "gs_in[%s + %u][%u]",
2050 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
2052 else if (reg->idx[1].rel_addr)
2053 sprintf(register_name, "gs_in[%u][%s + %u]",
2054 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2055 else
2056 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
2057 break;
2060 /* pixel shaders >= 3.0 */
2061 if (version->major >= 3)
2063 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2064 unsigned int in_count = vec4_varyings(version->major, gl_info);
2066 if (reg->idx[0].rel_addr)
2068 /* Removing a + 0 would be an obvious optimization, but
2069 * OS X doesn't see the NOP operation there. */
2070 if (idx)
2072 if (shader->u.ps.declared_in_count > in_count)
2074 sprintf(register_name,
2075 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2076 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2077 prefix, rel_param0.param_str, idx);
2079 else
2081 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2084 else
2086 if (shader->u.ps.declared_in_count > in_count)
2088 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2089 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2090 prefix, rel_param0.param_str);
2092 else
2094 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2098 else
2100 if (idx == in_count) sprintf(register_name, "gl_Color");
2101 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2102 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2105 else
2107 if (!reg->idx[0].offset)
2108 strcpy(register_name, "gl_Color");
2109 else
2110 strcpy(register_name, "gl_SecondaryColor");
2111 break;
2113 break;
2115 case WINED3DSPR_CONST:
2117 /* Relative addressing */
2118 if (reg->idx[0].rel_addr)
2120 if (reg->idx[0].offset)
2121 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2122 else
2123 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2125 else
2127 if (shader_constant_is_local(shader, reg->idx[0].offset))
2128 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2129 else
2130 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2133 break;
2135 case WINED3DSPR_CONSTINT:
2136 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2137 break;
2139 case WINED3DSPR_CONSTBOOL:
2140 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2141 break;
2143 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2144 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2145 sprintf(register_name, "T%u", reg->idx[0].offset);
2146 else
2147 sprintf(register_name, "A%u", reg->idx[0].offset);
2148 break;
2150 case WINED3DSPR_LOOP:
2151 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
2152 break;
2154 case WINED3DSPR_SAMPLER:
2155 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2156 break;
2158 case WINED3DSPR_COLOROUT:
2159 if (reg->idx[0].offset >= gl_info->limits.buffers)
2160 WARN("Write to render target %u, only %d supported.\n",
2161 reg->idx[0].offset, gl_info->limits.buffers);
2163 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
2164 break;
2166 case WINED3DSPR_RASTOUT:
2167 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2168 break;
2170 case WINED3DSPR_DEPTHOUT:
2171 sprintf(register_name, "gl_FragDepth");
2172 break;
2174 case WINED3DSPR_ATTROUT:
2175 if (!reg->idx[0].offset)
2176 sprintf(register_name, "%s_out[8]", prefix);
2177 else
2178 sprintf(register_name, "%s_out[9]", prefix);
2179 break;
2181 case WINED3DSPR_TEXCRDOUT:
2182 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2183 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2184 break;
2186 case WINED3DSPR_MISCTYPE:
2187 if (!reg->idx[0].offset)
2189 /* vPos */
2190 sprintf(register_name, "vpos");
2192 else if (reg->idx[0].offset == 1)
2194 /* Note that gl_FrontFacing is a bool, while vFace is
2195 * a float for which the sign determines front/back */
2196 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2198 else
2200 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2201 sprintf(register_name, "unrecognized_register");
2203 break;
2205 case WINED3DSPR_IMMCONST:
2206 switch (reg->immconst_type)
2208 case WINED3D_IMMCONST_SCALAR:
2209 switch (reg->data_type)
2211 case WINED3D_DATA_FLOAT:
2212 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
2213 break;
2214 case WINED3D_DATA_INT:
2215 sprintf(register_name, "%#x", reg->immconst_data[0]);
2216 break;
2217 case WINED3D_DATA_RESOURCE:
2218 case WINED3D_DATA_SAMPLER:
2219 case WINED3D_DATA_UINT:
2220 sprintf(register_name, "%#xu", reg->immconst_data[0]);
2221 break;
2222 default:
2223 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2224 break;
2226 break;
2228 case WINED3D_IMMCONST_VEC4:
2229 switch (reg->data_type)
2231 case WINED3D_DATA_FLOAT:
2232 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
2233 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
2234 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
2235 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
2236 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2237 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2238 break;
2239 case WINED3D_DATA_INT:
2240 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2241 reg->immconst_data[0], reg->immconst_data[1],
2242 reg->immconst_data[2], reg->immconst_data[3]);
2243 break;
2244 case WINED3D_DATA_RESOURCE:
2245 case WINED3D_DATA_SAMPLER:
2246 case WINED3D_DATA_UINT:
2247 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2248 reg->immconst_data[0], reg->immconst_data[1],
2249 reg->immconst_data[2], reg->immconst_data[3]);
2250 break;
2251 default:
2252 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2253 break;
2255 break;
2257 default:
2258 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2259 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2261 break;
2263 case WINED3DSPR_CONSTBUFFER:
2264 if (reg->idx[1].rel_addr)
2265 sprintf(register_name, "%s_cb%u[%s + %u]",
2266 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2267 else
2268 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2269 break;
2271 case WINED3DSPR_PRIMID:
2272 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2273 break;
2275 default:
2276 FIXME("Unhandled register type %#x.\n", reg->type);
2277 sprintf(register_name, "unrecognized_register");
2278 break;
2282 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2284 *str++ = '.';
2285 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2286 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2287 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2288 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2289 *str = '\0';
2292 /* Get the GLSL write mask for the destination register */
2293 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2295 DWORD mask = param->write_mask;
2297 if (shader_is_scalar(&param->reg))
2299 mask = WINED3DSP_WRITEMASK_0;
2300 *write_mask = '\0';
2302 else
2304 shader_glsl_write_mask_to_str(mask, write_mask);
2307 return mask;
2310 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
2311 unsigned int size = 0;
2313 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2314 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2315 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2316 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2318 return size;
2321 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2323 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2324 * but addressed as "rgba". To fix this we need to swap the register's x
2325 * and z components. */
2326 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2328 *str++ = '.';
2329 /* swizzle bits fields: wwzzyyxx */
2330 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
2331 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
2332 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
2333 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
2334 *str = '\0';
2337 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2338 BOOL fixup, DWORD mask, char *swizzle_str)
2340 if (shader_is_scalar(&param->reg))
2341 *swizzle_str = '\0';
2342 else
2343 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2346 /* From a given parameter token, generate the corresponding GLSL string.
2347 * Also, return the actual register name and swizzle in case the
2348 * caller needs this information as well. */
2349 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2350 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
2352 BOOL is_color = FALSE;
2353 char swizzle_str[6];
2355 glsl_src->reg_name[0] = '\0';
2356 glsl_src->param_str[0] = '\0';
2357 swizzle_str[0] = '\0';
2359 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
2360 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
2362 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
2364 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
2366 else
2368 char reg_name[200];
2370 switch (wined3d_src->reg.data_type)
2372 case WINED3D_DATA_FLOAT:
2373 sprintf(reg_name, "%s", glsl_src->reg_name);
2374 break;
2375 case WINED3D_DATA_INT:
2376 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
2377 break;
2378 case WINED3D_DATA_RESOURCE:
2379 case WINED3D_DATA_SAMPLER:
2380 case WINED3D_DATA_UINT:
2381 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
2382 break;
2383 default:
2384 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
2385 sprintf(reg_name, "%s", glsl_src->reg_name);
2386 break;
2389 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
2393 /* From a given parameter token, generate the corresponding GLSL string.
2394 * Also, return the actual register name and swizzle in case the
2395 * caller needs this information as well. */
2396 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
2397 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
2399 BOOL is_color = FALSE;
2401 glsl_dst->mask_str[0] = '\0';
2402 glsl_dst->reg_name[0] = '\0';
2404 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
2405 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
2408 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2409 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
2410 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
2411 enum wined3d_data_type data_type)
2413 struct glsl_dst_param glsl_dst;
2414 DWORD mask;
2416 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
2418 switch (data_type)
2420 case WINED3D_DATA_FLOAT:
2421 shader_addline(buffer, "%s%s = %s(",
2422 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2423 break;
2424 case WINED3D_DATA_INT:
2425 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
2426 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2427 break;
2428 case WINED3D_DATA_RESOURCE:
2429 case WINED3D_DATA_SAMPLER:
2430 case WINED3D_DATA_UINT:
2431 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
2432 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2433 break;
2434 default:
2435 FIXME("Unhandled data type %#x.\n", data_type);
2436 shader_addline(buffer, "%s%s = %s(",
2437 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2438 break;
2442 return mask;
2445 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2446 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
2448 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2451 /** Process GLSL instruction modifiers */
2452 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
2454 struct glsl_dst_param dst_param;
2455 DWORD modifiers;
2457 if (!ins->dst_count) return;
2459 modifiers = ins->dst[0].modifiers;
2460 if (!modifiers) return;
2462 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2464 if (modifiers & WINED3DSPDM_SATURATE)
2466 /* _SAT means to clamp the value of the register to between 0 and 1 */
2467 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
2468 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
2471 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
2473 FIXME("_centroid modifier not handled\n");
2476 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
2478 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2482 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
2484 switch (op)
2486 case WINED3D_SHADER_REL_OP_GT: return ">";
2487 case WINED3D_SHADER_REL_OP_EQ: return "==";
2488 case WINED3D_SHADER_REL_OP_GE: return ">=";
2489 case WINED3D_SHADER_REL_OP_LT: return "<";
2490 case WINED3D_SHADER_REL_OP_NE: return "!=";
2491 case WINED3D_SHADER_REL_OP_LE: return "<=";
2492 default:
2493 FIXME("Unrecognized operator %#x.\n", op);
2494 return "(\?\?)";
2498 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
2499 DWORD resource_idx, DWORD flags, struct glsl_sample_function *sample_function)
2501 static const unsigned int type_coord_size[] =
2503 0, /* WINED3D_SHADER_RESOURCE_NONE */
2504 1, /* WINED3D_SHADER_RESOURCE_BUFFER */
2505 1, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
2506 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
2507 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
2508 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
2509 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
2510 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
2511 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
2512 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
2514 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2515 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
2516 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2517 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
2518 && (priv->cur_ps_args->shadow & (1 << resource_idx));
2519 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
2520 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2521 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
2522 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
2523 const char *base = "texture", *type_part = "", *suffix = "";
2524 unsigned int coord_size;
2526 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
2528 /* Note that there's no such thing as a projected cube texture. */
2529 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
2530 projected = FALSE;
2532 if (shadow)
2533 base = "shadow";
2535 switch (resource_type)
2537 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2538 type_part = "1D";
2539 break;
2540 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2541 if (texrect)
2542 type_part = "2DRect";
2543 else
2544 type_part = "2D";
2545 break;
2546 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2547 type_part = "3D";
2548 break;
2549 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2550 type_part = "Cube";
2551 break;
2552 default:
2553 FIXME("Unhandled resource type %#x.\n", resource_type);
2556 if (!lod && grad && !gl_info->supported[EXT_GPU_SHADER4])
2558 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2559 suffix = "ARB";
2560 else
2561 FIXME("Unsupported grad function.\n");
2564 sample_function->name = string_buffer_get(priv->string_buffers);
2565 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
2566 lod ? "Lod" : grad ? "Grad" : "", suffix);
2568 if (resource_type >= ARRAY_SIZE(type_coord_size))
2570 ERR("Unexpected resource type %#x.\n", resource_type);
2571 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
2573 coord_size = type_coord_size[resource_type];
2574 if (shadow)
2575 ++coord_size;
2576 sample_function->coord_mask = (1 << coord_size) - 1;
2579 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
2580 struct glsl_sample_function *sample_function)
2582 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2584 string_buffer_release(priv->string_buffers, sample_function->name);
2587 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2588 BOOL sign_fixup, enum fixup_channel_source channel_source)
2590 switch(channel_source)
2592 case CHANNEL_SOURCE_ZERO:
2593 strcat(arguments, "0.0");
2594 break;
2596 case CHANNEL_SOURCE_ONE:
2597 strcat(arguments, "1.0");
2598 break;
2600 case CHANNEL_SOURCE_X:
2601 strcat(arguments, reg_name);
2602 strcat(arguments, ".x");
2603 break;
2605 case CHANNEL_SOURCE_Y:
2606 strcat(arguments, reg_name);
2607 strcat(arguments, ".y");
2608 break;
2610 case CHANNEL_SOURCE_Z:
2611 strcat(arguments, reg_name);
2612 strcat(arguments, ".z");
2613 break;
2615 case CHANNEL_SOURCE_W:
2616 strcat(arguments, reg_name);
2617 strcat(arguments, ".w");
2618 break;
2620 default:
2621 FIXME("Unhandled channel source %#x\n", channel_source);
2622 strcat(arguments, "undefined");
2623 break;
2626 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2629 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
2630 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2632 unsigned int mask_size, remaining;
2633 DWORD fixup_mask = 0;
2634 char arguments[256];
2635 char mask_str[6];
2637 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2638 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2639 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2640 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2641 if (!(mask &= fixup_mask))
2642 return;
2644 if (is_complex_fixup(fixup))
2646 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2647 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2648 return;
2651 shader_glsl_write_mask_to_str(mask, mask_str);
2652 mask_size = shader_glsl_get_write_mask_size(mask);
2654 arguments[0] = '\0';
2655 remaining = mask_size;
2656 if (mask & WINED3DSP_WRITEMASK_0)
2658 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2659 if (--remaining) strcat(arguments, ", ");
2661 if (mask & WINED3DSP_WRITEMASK_1)
2663 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2664 if (--remaining) strcat(arguments, ", ");
2666 if (mask & WINED3DSP_WRITEMASK_2)
2668 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2669 if (--remaining) strcat(arguments, ", ");
2671 if (mask & WINED3DSP_WRITEMASK_3)
2673 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2674 if (--remaining) strcat(arguments, ", ");
2677 if (mask_size > 1)
2678 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2679 else
2680 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2683 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2685 char reg_name[256];
2686 BOOL is_color;
2688 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2689 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2692 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2693 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2694 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2696 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2697 char dst_swizzle[6];
2698 struct color_fixup_desc fixup;
2699 BOOL np2_fixup = FALSE;
2700 va_list args;
2701 int ret;
2703 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2705 /* FIXME: We currently don't support fixups for vertex shaders or anything
2706 * above SM3. Note that for SM4+ the sampler index doesn't have to match
2707 * the resource index. */
2708 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
2710 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2711 fixup = priv->cur_ps_args->color_fixup[sampler];
2713 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2714 if(bias) {
2715 FIXME("Biased sampling from NP2 textures is unsupported\n");
2716 } else {
2717 np2_fixup = TRUE;
2721 else
2723 fixup = COLOR_FIXUP_IDENTITY;
2726 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2728 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2729 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler);
2731 for (;;)
2733 va_start(args, coord_reg_fmt);
2734 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2735 va_end(args);
2736 if (!ret)
2737 break;
2738 if (!string_buffer_resize(ins->ctx->buffer, ret))
2739 break;
2742 if(bias) {
2743 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2744 } else {
2745 if (np2_fixup) {
2746 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2747 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2749 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2750 (idx % 2) ? "zw" : "xy", dst_swizzle);
2751 } else if(dx && dy) {
2752 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2753 } else {
2754 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2758 if(!is_identity_fixup(fixup)) {
2759 shader_glsl_color_correction(ins, fixup);
2763 /*****************************************************************************
2764 * Begin processing individual instruction opcodes
2765 ****************************************************************************/
2767 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2769 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2770 struct glsl_src_param src0_param;
2771 struct glsl_src_param src1_param;
2772 DWORD write_mask;
2773 const char *op;
2775 /* Determine the GLSL operator to use based on the opcode */
2776 switch (ins->handler_idx)
2778 case WINED3DSIH_ADD: op = "+"; break;
2779 case WINED3DSIH_AND: op = "&"; break;
2780 case WINED3DSIH_DIV: op = "/"; break;
2781 case WINED3DSIH_IADD: op = "+"; break;
2782 case WINED3DSIH_ISHL: op = "<<"; break;
2783 case WINED3DSIH_MUL: op = "*"; break;
2784 case WINED3DSIH_OR: op = "|"; break;
2785 case WINED3DSIH_SUB: op = "-"; break;
2786 case WINED3DSIH_USHR: op = ">>"; break;
2787 case WINED3DSIH_XOR: op = "^"; break;
2788 default:
2789 op = "<unhandled operator>";
2790 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2791 break;
2794 write_mask = shader_glsl_append_dst(buffer, ins);
2795 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2796 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2797 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2800 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2802 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2803 struct glsl_src_param src0_param;
2804 struct glsl_src_param src1_param;
2805 unsigned int mask_size;
2806 DWORD write_mask;
2807 const char *op;
2809 write_mask = shader_glsl_append_dst(buffer, ins);
2810 mask_size = shader_glsl_get_write_mask_size(write_mask);
2811 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2812 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2814 if (mask_size > 1)
2816 switch (ins->handler_idx)
2818 case WINED3DSIH_EQ: op = "equal"; break;
2819 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2820 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2821 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
2822 case WINED3DSIH_LT: op = "lessThan"; break;
2823 case WINED3DSIH_NE: op = "notEqual"; break;
2824 default:
2825 op = "<unhandled operator>";
2826 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2827 break;
2830 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2831 mask_size, op, src0_param.param_str, src1_param.param_str);
2833 else
2835 switch (ins->handler_idx)
2837 case WINED3DSIH_EQ: op = "=="; break;
2838 case WINED3DSIH_GE: op = ">="; break;
2839 case WINED3DSIH_IGE: op = ">="; break;
2840 case WINED3DSIH_UGE: op = ">="; break;
2841 case WINED3DSIH_LT: op = "<"; break;
2842 case WINED3DSIH_NE: op = "!="; break;
2843 default:
2844 op = "<unhandled operator>";
2845 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2846 break;
2849 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2850 src0_param.param_str, op, src1_param.param_str);
2854 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2856 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2857 struct glsl_src_param src0_param;
2858 struct glsl_src_param src1_param;
2859 DWORD write_mask;
2861 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2862 * not, we can emulate it. */
2863 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2864 FIXME("64-bit integer multiplies not implemented.\n");
2866 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2868 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2869 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2870 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2872 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2873 src0_param.param_str, src1_param.param_str);
2877 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2879 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2880 struct glsl_src_param src0_param, src1_param;
2881 DWORD write_mask;
2883 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2886 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2888 char dst_mask[6];
2890 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2891 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2892 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2893 shader_addline(buffer, "tmp0%s = %s / %s;\n",
2894 dst_mask, src0_param.param_str, src1_param.param_str);
2896 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2897 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2898 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2899 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
2901 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2902 shader_addline(buffer, "tmp0%s);\n", dst_mask);
2904 else
2906 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2907 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2908 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2909 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
2912 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2914 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2915 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2916 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2917 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
2921 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2922 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2924 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2925 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2926 struct glsl_src_param src0_param;
2927 DWORD write_mask;
2929 write_mask = shader_glsl_append_dst(buffer, ins);
2930 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2932 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2933 * shader versions WINED3DSIO_MOVA is used for this. */
2934 if (ins->ctx->reg_maps->shader_version.major == 1
2935 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
2936 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2938 /* This is a simple floor() */
2939 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2940 if (mask_size > 1) {
2941 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2942 } else {
2943 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2946 else if(ins->handler_idx == WINED3DSIH_MOVA)
2948 /* We need to *round* to the nearest int here. */
2949 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2951 if (gl_info->supported[EXT_GPU_SHADER4])
2953 if (mask_size > 1)
2954 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2955 else
2956 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2958 else
2960 if (mask_size > 1)
2961 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2962 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2963 else
2964 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2965 src0_param.param_str, src0_param.param_str);
2968 else
2970 shader_addline(buffer, "%s);\n", src0_param.param_str);
2974 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2975 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2977 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2978 struct glsl_src_param src0_param;
2979 struct glsl_src_param src1_param;
2980 DWORD dst_write_mask, src_write_mask;
2981 unsigned int dst_size = 0;
2983 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2984 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2986 /* dp4 works on vec4, dp3 on vec3, etc. */
2987 if (ins->handler_idx == WINED3DSIH_DP4)
2988 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2989 else if (ins->handler_idx == WINED3DSIH_DP3)
2990 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2991 else
2992 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2994 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2995 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2997 if (dst_size > 1) {
2998 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2999 } else {
3000 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3004 /* Note that this instruction has some restrictions. The destination write mask
3005 * can't contain the w component, and the source swizzles have to be .xyzw */
3006 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3008 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3009 struct glsl_src_param src0_param;
3010 struct glsl_src_param src1_param;
3011 char dst_mask[6];
3013 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3014 shader_glsl_append_dst(ins->ctx->buffer, ins);
3015 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3016 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3017 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3020 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3022 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3025 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3026 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3027 * GLSL uses the value as-is. */
3028 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3030 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3031 struct glsl_src_param src0_param;
3032 struct glsl_src_param src1_param;
3033 DWORD dst_write_mask;
3034 unsigned int dst_size;
3036 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3037 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3039 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3040 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3042 if (dst_size > 1)
3044 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3045 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3047 else
3049 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3050 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3054 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3055 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3057 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3058 struct glsl_src_param src_param;
3059 const char *instruction;
3060 DWORD write_mask;
3061 unsigned i;
3063 /* Determine the GLSL function to use based on the opcode */
3064 /* TODO: Possibly make this a table for faster lookups */
3065 switch (ins->handler_idx)
3067 case WINED3DSIH_MIN: instruction = "min"; break;
3068 case WINED3DSIH_MAX: instruction = "max"; break;
3069 case WINED3DSIH_ABS: instruction = "abs"; break;
3070 case WINED3DSIH_FRC: instruction = "fract"; break;
3071 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3072 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3073 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3074 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3075 default: instruction = "";
3076 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
3077 break;
3080 write_mask = shader_glsl_append_dst(buffer, ins);
3082 shader_addline(buffer, "%s(", instruction);
3084 if (ins->src_count)
3086 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3087 shader_addline(buffer, "%s", src_param.param_str);
3088 for (i = 1; i < ins->src_count; ++i)
3090 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3091 shader_addline(buffer, ", %s", src_param.param_str);
3095 shader_addline(buffer, "));\n");
3098 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3100 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3102 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3103 struct glsl_src_param src_param;
3104 unsigned int mask_size;
3105 DWORD write_mask;
3106 char dst_mask[6];
3108 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3109 mask_size = shader_glsl_get_write_mask_size(write_mask);
3110 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3112 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3113 src_param.param_str, src_param.param_str);
3114 shader_glsl_append_dst(buffer, ins);
3116 if (mask_size > 1)
3118 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3119 mask_size, src_param.param_str);
3121 else
3123 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3124 src_param.param_str);
3128 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3130 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3131 struct glsl_src_param src0_param;
3132 const char *prefix, *suffix;
3133 unsigned int dst_size;
3134 DWORD dst_write_mask;
3136 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3137 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3139 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src0_param);
3141 switch (ins->handler_idx)
3143 case WINED3DSIH_EXP:
3144 case WINED3DSIH_EXPP:
3145 prefix = "exp2(";
3146 suffix = ")";
3147 break;
3149 case WINED3DSIH_LOG:
3150 case WINED3DSIH_LOGP:
3151 prefix = "log2(abs(";
3152 suffix = "))";
3153 break;
3155 case WINED3DSIH_RCP:
3156 prefix = "1.0 / ";
3157 suffix = "";
3158 break;
3160 case WINED3DSIH_RSQ:
3161 prefix = "inversesqrt(abs(";
3162 suffix = "))";
3163 break;
3165 default:
3166 prefix = "";
3167 suffix = "";
3168 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3169 break;
3172 if (dst_size > 1)
3173 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3174 else
3175 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3178 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3179 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3180 * dst.x = 2^(floor(src))
3181 * dst.y = src - floor(src)
3182 * dst.z = 2^src (partial precision is allowed, but optional)
3183 * dst.w = 1.0;
3184 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3185 * dst = 2^src; (partial precision is allowed, but optional)
3187 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
3189 if (ins->ctx->reg_maps->shader_version.major < 2)
3191 struct glsl_src_param src_param;
3192 char dst_mask[6];
3194 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
3196 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
3197 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
3198 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
3199 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
3201 shader_glsl_append_dst(ins->ctx->buffer, ins);
3202 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3203 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
3204 return;
3207 shader_glsl_scalar_op(ins);
3210 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
3212 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3213 struct glsl_src_param src_param;
3214 unsigned int mask_size;
3215 DWORD write_mask;
3217 write_mask = shader_glsl_append_dst(buffer, ins);
3218 mask_size = shader_glsl_get_write_mask_size(write_mask);
3219 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3221 if (mask_size > 1)
3222 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
3223 else
3224 shader_addline(buffer, "int(%s));\n", src_param.param_str);
3227 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
3229 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3230 struct glsl_src_param src_param;
3231 unsigned int mask_size;
3232 DWORD write_mask;
3234 write_mask = shader_glsl_append_dst(buffer, ins);
3235 mask_size = shader_glsl_get_write_mask_size(write_mask);
3236 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3238 if (mask_size > 1)
3239 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
3240 else
3241 shader_addline(buffer, "float(%s));\n", src_param.param_str);
3244 /** Process signed comparison opcodes in GLSL. */
3245 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
3247 struct glsl_src_param src0_param;
3248 struct glsl_src_param src1_param;
3249 DWORD write_mask;
3250 unsigned int mask_size;
3252 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3253 mask_size = shader_glsl_get_write_mask_size(write_mask);
3254 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3255 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3257 if (mask_size > 1) {
3258 const char *compare;
3260 switch(ins->handler_idx)
3262 case WINED3DSIH_SLT: compare = "lessThan"; break;
3263 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
3264 default: compare = "";
3265 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
3268 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
3269 src0_param.param_str, src1_param.param_str);
3270 } else {
3271 switch(ins->handler_idx)
3273 case WINED3DSIH_SLT:
3274 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3275 * to return 0.0 but step returns 1.0 because step is not < x
3276 * An alternative is a bvec compare padded with an unused second component.
3277 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3278 * issue. Playing with not() is not possible either because not() does not accept
3279 * a scalar.
3281 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
3282 src0_param.param_str, src1_param.param_str);
3283 break;
3284 case WINED3DSIH_SGE:
3285 /* Here we can use the step() function and safe a conditional */
3286 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
3287 break;
3288 default:
3289 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
3295 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
3297 const char *condition_prefix, *condition_suffix;
3298 struct wined3d_shader_dst_param dst;
3299 struct glsl_src_param src0_param;
3300 struct glsl_src_param src1_param;
3301 struct glsl_src_param src2_param;
3302 BOOL temp_destination = FALSE;
3303 DWORD cmp_channel = 0;
3304 unsigned int i, j;
3305 char mask_char[6];
3306 DWORD write_mask;
3308 switch (ins->handler_idx)
3310 case WINED3DSIH_CMP:
3311 condition_prefix = "";
3312 condition_suffix = " >= 0.0";
3313 break;
3315 case WINED3DSIH_CND:
3316 condition_prefix = "";
3317 condition_suffix = " > 0.5";
3318 break;
3320 case WINED3DSIH_MOVC:
3321 condition_prefix = "bool(";
3322 condition_suffix = ")";
3323 break;
3325 default:
3326 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3327 condition_prefix = "<unhandled prefix>";
3328 condition_suffix = "<unhandled suffix>";
3329 break;
3332 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
3334 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3335 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3336 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3337 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3339 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3340 condition_prefix, src0_param.param_str, condition_suffix,
3341 src1_param.param_str, src2_param.param_str);
3342 return;
3345 dst = ins->dst[0];
3347 /* Splitting the instruction up in multiple lines imposes a problem:
3348 * The first lines may overwrite source parameters of the following lines.
3349 * Deal with that by using a temporary destination register if needed. */
3350 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
3351 && ins->src[0].reg.type == dst.reg.type)
3352 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
3353 && ins->src[1].reg.type == dst.reg.type)
3354 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
3355 && ins->src[2].reg.type == dst.reg.type))
3356 temp_destination = TRUE;
3358 /* Cycle through all source0 channels. */
3359 for (i = 0; i < 4; ++i)
3361 write_mask = 0;
3362 /* Find the destination channels which use the current source0 channel. */
3363 for (j = 0; j < 4; ++j)
3365 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
3367 write_mask |= WINED3DSP_WRITEMASK_0 << j;
3368 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
3371 dst.write_mask = ins->dst[0].write_mask & write_mask;
3373 if (temp_destination)
3375 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
3376 continue;
3377 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
3379 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
3380 continue;
3382 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
3383 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3384 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3386 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3387 condition_prefix, src0_param.param_str, condition_suffix,
3388 src1_param.param_str, src2_param.param_str);
3391 if (temp_destination)
3393 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3394 shader_glsl_append_dst(ins->ctx->buffer, ins);
3395 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3399 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3400 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3401 * the compare is done per component of src0. */
3402 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3404 struct glsl_src_param src0_param;
3405 struct glsl_src_param src1_param;
3406 struct glsl_src_param src2_param;
3407 DWORD write_mask;
3408 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3409 ins->ctx->reg_maps->shader_version.minor);
3411 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3413 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3414 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3415 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3416 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3418 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
3419 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3420 else
3421 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3422 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3423 return;
3426 shader_glsl_conditional_move(ins);
3429 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3430 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3432 struct glsl_src_param src0_param;
3433 struct glsl_src_param src1_param;
3434 struct glsl_src_param src2_param;
3435 DWORD write_mask;
3437 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3438 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3439 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3440 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3441 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3442 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3445 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3446 Vertex shaders to GLSL codes */
3447 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3449 int i;
3450 int nComponents = 0;
3451 struct wined3d_shader_dst_param tmp_dst = {{0}};
3452 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3453 struct wined3d_shader_instruction tmp_ins;
3455 memset(&tmp_ins, 0, sizeof(tmp_ins));
3457 /* Set constants for the temporary argument */
3458 tmp_ins.ctx = ins->ctx;
3459 tmp_ins.dst_count = 1;
3460 tmp_ins.dst = &tmp_dst;
3461 tmp_ins.src_count = 2;
3462 tmp_ins.src = tmp_src;
3464 switch(ins->handler_idx)
3466 case WINED3DSIH_M4x4:
3467 nComponents = 4;
3468 tmp_ins.handler_idx = WINED3DSIH_DP4;
3469 break;
3470 case WINED3DSIH_M4x3:
3471 nComponents = 3;
3472 tmp_ins.handler_idx = WINED3DSIH_DP4;
3473 break;
3474 case WINED3DSIH_M3x4:
3475 nComponents = 4;
3476 tmp_ins.handler_idx = WINED3DSIH_DP3;
3477 break;
3478 case WINED3DSIH_M3x3:
3479 nComponents = 3;
3480 tmp_ins.handler_idx = WINED3DSIH_DP3;
3481 break;
3482 case WINED3DSIH_M3x2:
3483 nComponents = 2;
3484 tmp_ins.handler_idx = WINED3DSIH_DP3;
3485 break;
3486 default:
3487 break;
3490 tmp_dst = ins->dst[0];
3491 tmp_src[0] = ins->src[0];
3492 tmp_src[1] = ins->src[1];
3493 for (i = 0; i < nComponents; ++i)
3495 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3496 shader_glsl_dot(&tmp_ins);
3497 ++tmp_src[1].reg.idx[0].offset;
3502 The LRP instruction performs a component-wise linear interpolation
3503 between the second and third operands using the first operand as the
3504 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3505 This is equivalent to mix(src2, src1, src0);
3507 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3509 struct glsl_src_param src0_param;
3510 struct glsl_src_param src1_param;
3511 struct glsl_src_param src2_param;
3512 DWORD write_mask;
3514 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3516 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3517 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3518 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3520 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3521 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3524 /** Process the WINED3DSIO_LIT instruction in GLSL:
3525 * dst.x = dst.w = 1.0
3526 * dst.y = (src0.x > 0) ? src0.x
3527 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3528 * where src.w is clamped at +- 128
3530 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3532 struct glsl_src_param src0_param;
3533 struct glsl_src_param src1_param;
3534 struct glsl_src_param src3_param;
3535 char dst_mask[6];
3537 shader_glsl_append_dst(ins->ctx->buffer, ins);
3538 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3540 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3541 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3542 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3544 /* The sdk specifies the instruction like this
3545 * dst.x = 1.0;
3546 * if(src.x > 0.0) dst.y = src.x
3547 * else dst.y = 0.0.
3548 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3549 * else dst.z = 0.0;
3550 * dst.w = 1.0;
3551 * (where power = src.w clamped between -128 and 128)
3553 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3554 * dst.x = 1.0 ... No further explanation needed
3555 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3556 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3557 * dst.w = 1.0. ... Nothing fancy.
3559 * So we still have one conditional in there. So do this:
3560 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3562 * 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),
3563 * 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.
3564 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3566 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3567 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3568 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3570 shader_addline(ins->ctx->buffer,
3571 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3572 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3573 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3574 src0_param.param_str, src3_param.param_str, dst_mask);
3577 /** Process the WINED3DSIO_DST instruction in GLSL:
3578 * dst.x = 1.0
3579 * dst.y = src0.x * src0.y
3580 * dst.z = src0.z
3581 * dst.w = src1.w
3583 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3585 struct glsl_src_param src0y_param;
3586 struct glsl_src_param src0z_param;
3587 struct glsl_src_param src1y_param;
3588 struct glsl_src_param src1w_param;
3589 char dst_mask[6];
3591 shader_glsl_append_dst(ins->ctx->buffer, ins);
3592 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3594 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3595 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3596 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3597 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3599 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3600 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3603 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3604 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3605 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3607 * dst.x = cos(src0.?)
3608 * dst.y = sin(src0.?)
3609 * dst.z = dst.z
3610 * dst.w = dst.w
3612 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3614 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3615 struct glsl_src_param src0_param;
3616 DWORD write_mask;
3618 if (ins->ctx->reg_maps->shader_version.major < 4)
3620 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3622 write_mask = shader_glsl_append_dst(buffer, ins);
3623 switch (write_mask)
3625 case WINED3DSP_WRITEMASK_0:
3626 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3627 break;
3629 case WINED3DSP_WRITEMASK_1:
3630 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3631 break;
3633 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3634 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3635 src0_param.param_str, src0_param.param_str);
3636 break;
3638 default:
3639 ERR("Write mask should be .x, .y or .xy\n");
3640 break;
3643 return;
3646 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3649 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3651 char dst_mask[6];
3653 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3654 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3655 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3657 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3658 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3659 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3661 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3662 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3664 else
3666 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3667 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3668 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3671 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3673 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3674 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3675 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3679 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3680 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3681 * generate invalid code
3683 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3685 struct glsl_src_param src0_param;
3686 DWORD write_mask;
3688 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3689 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3691 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3694 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3695 * Start a for() loop where src1.y is the initial value of aL,
3696 * increment aL by src1.z for a total of src1.x iterations.
3697 * Need to use a temporary variable for this operation.
3699 /* FIXME: I don't think nested loops will work correctly this way. */
3700 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3702 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3703 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3704 const struct wined3d_shader *shader = ins->ctx->shader;
3705 const struct wined3d_shader_lconst *constant;
3706 struct glsl_src_param src1_param;
3707 const DWORD *control_values = NULL;
3709 if (ins->ctx->reg_maps->shader_version.major < 4)
3711 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3713 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3714 * class hardware doesn't support real varying indexing, but Microsoft
3715 * designed this feature for Shader model 2.x+. If the loop control is
3716 * known at compile time, the GLSL compiler can unroll the loop, and
3717 * replace indirect addressing with direct addressing. */
3718 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3720 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3722 if (constant->idx == ins->src[1].reg.idx[0].offset)
3724 control_values = constant->value;
3725 break;
3730 if (control_values)
3732 struct wined3d_shader_loop_control loop_control;
3733 loop_control.count = control_values[0];
3734 loop_control.start = control_values[1];
3735 loop_control.step = (int)control_values[2];
3737 if (loop_control.step > 0)
3739 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3740 loop_state->current_depth, loop_control.start,
3741 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3742 loop_state->current_depth, loop_control.step);
3744 else if (loop_control.step < 0)
3746 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3747 loop_state->current_depth, loop_control.start,
3748 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3749 loop_state->current_depth, loop_control.step);
3751 else
3753 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3754 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3755 loop_state->current_depth, loop_control.count,
3756 loop_state->current_depth);
3759 else
3761 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3762 loop_state->current_depth, loop_state->current_reg,
3763 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3764 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3767 ++loop_state->current_reg;
3769 else
3771 shader_addline(buffer, "for (;;)\n{\n");
3774 ++loop_state->current_depth;
3777 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3779 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3781 shader_addline(ins->ctx->buffer, "}\n");
3783 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3785 --loop_state->current_depth;
3786 --loop_state->current_reg;
3789 if (ins->handler_idx == WINED3DSIH_ENDREP)
3791 --loop_state->current_depth;
3795 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3797 const struct wined3d_shader *shader = ins->ctx->shader;
3798 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3799 const struct wined3d_shader_lconst *constant;
3800 struct glsl_src_param src0_param;
3801 const DWORD *control_values = NULL;
3803 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3804 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3806 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3808 if (constant->idx == ins->src[0].reg.idx[0].offset)
3810 control_values = constant->value;
3811 break;
3816 if (control_values)
3818 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3819 loop_state->current_depth, loop_state->current_depth,
3820 control_values[0], loop_state->current_depth);
3822 else
3824 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3825 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3826 loop_state->current_depth, loop_state->current_depth,
3827 src0_param.param_str, loop_state->current_depth);
3830 ++loop_state->current_depth;
3833 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3835 struct glsl_src_param src0_param;
3837 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3838 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
3841 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3843 struct glsl_src_param src0_param;
3844 struct glsl_src_param src1_param;
3846 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3847 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3849 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3850 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3853 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3855 shader_addline(ins->ctx->buffer, "} else {\n");
3858 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3860 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3863 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3865 shader_addline(ins->ctx->buffer, "break;\n");
3868 /* FIXME: According to MSDN the compare is done per component. */
3869 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3871 struct glsl_src_param src0_param;
3872 struct glsl_src_param src1_param;
3874 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3875 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3877 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3878 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3881 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
3883 struct glsl_src_param src_param;
3885 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
3886 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
3889 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
3891 shader_addline(ins->ctx->buffer, "}\n");
3892 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
3895 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
3897 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
3900 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3902 struct glsl_src_param src1_param;
3904 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3905 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
3906 src1_param.param_str, ins->src[0].reg.idx[0].offset);
3909 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3911 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3912 * function only suppresses the unhandled instruction warning
3916 /*********************************************
3917 * Pixel Shader Specific Code begins here
3918 ********************************************/
3919 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3921 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3922 ins->ctx->reg_maps->shader_version.minor);
3923 struct glsl_sample_function sample_function;
3924 DWORD sample_flags = 0;
3925 DWORD resource_idx;
3926 DWORD mask = 0, swizzle;
3927 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3929 /* 1.0-1.4: Use destination register as sampler source.
3930 * 2.0+: Use provided sampler source. */
3931 if (shader_version < WINED3D_SHADER_VERSION(2,0))
3932 resource_idx = ins->dst[0].reg.idx[0].offset;
3933 else
3934 resource_idx = ins->src[1].reg.idx[0].offset;
3936 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3938 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3939 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3940 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
3942 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3943 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3945 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3946 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3948 case WINED3D_TTFF_COUNT1:
3949 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3950 break;
3951 case WINED3D_TTFF_COUNT2:
3952 mask = WINED3DSP_WRITEMASK_1;
3953 break;
3954 case WINED3D_TTFF_COUNT3:
3955 mask = WINED3DSP_WRITEMASK_2;
3956 break;
3957 case WINED3D_TTFF_COUNT4:
3958 case WINED3D_TTFF_DISABLE:
3959 mask = WINED3DSP_WRITEMASK_3;
3960 break;
3964 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3966 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3968 if (src_mod == WINED3DSPSM_DZ) {
3969 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3970 mask = WINED3DSP_WRITEMASK_2;
3971 } else if (src_mod == WINED3DSPSM_DW) {
3972 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3973 mask = WINED3DSP_WRITEMASK_3;
3976 else
3978 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
3979 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3981 /* ps 2.0 texldp instruction always divides by the fourth component. */
3982 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3983 mask = WINED3DSP_WRITEMASK_3;
3987 if (priv->cur_ps_args->np2_fixup & (1 << resource_idx))
3988 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3990 shader_glsl_get_sample_function(ins->ctx, resource_idx, sample_flags, &sample_function);
3991 mask |= sample_function.coord_mask;
3993 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3994 else swizzle = ins->src[1].swizzle;
3996 /* 1.0-1.3: Use destination register as coordinate source.
3997 1.4+: Use provided coordinate source register. */
3998 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4000 char coord_mask[6];
4001 shader_glsl_write_mask_to_str(mask, coord_mask);
4002 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
4003 "T%u%s", resource_idx, coord_mask);
4005 else
4007 struct glsl_src_param coord_param;
4008 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4009 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4011 struct glsl_src_param bias;
4012 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4013 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4014 "%s", coord_param.param_str);
4015 } else {
4016 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
4017 "%s", coord_param.param_str);
4020 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4023 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4025 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4026 struct glsl_src_param coord_param, dx_param, dy_param;
4027 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
4028 struct glsl_sample_function sample_function;
4029 DWORD sampler_idx;
4030 DWORD swizzle = ins->src[1].swizzle;
4031 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4033 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
4035 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4036 shader_glsl_tex(ins);
4037 return;
4040 sampler_idx = ins->src[1].reg.idx[0].offset;
4041 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
4042 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4044 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
4045 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4046 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
4047 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
4049 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
4050 "%s", coord_param.param_str);
4051 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4054 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4056 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4057 struct glsl_src_param coord_param, lod_param;
4058 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
4059 struct glsl_sample_function sample_function;
4060 DWORD sampler_idx;
4061 DWORD swizzle = ins->src[1].swizzle;
4062 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4064 sampler_idx = ins->src[1].reg.idx[0].offset;
4065 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
4066 && priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
4067 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4069 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
4070 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4072 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4074 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
4075 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
4077 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4078 * However, the NVIDIA drivers allow them in fragment shaders as well,
4079 * even without the appropriate extension. */
4080 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4082 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
4083 "%s", coord_param.param_str);
4084 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4087 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4088 unsigned int resource_idx, unsigned int sampler_idx)
4090 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4091 unsigned int i;
4093 for (i = 0; i < sampler_map->count; ++i)
4095 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4096 return entries[i].bind_idx;
4099 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4101 return ~0u;
4104 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
4106 struct glsl_sample_function sample_function;
4107 struct glsl_src_param coord_param;
4108 unsigned int sampler_idx;
4110 shader_glsl_get_sample_function(ins->ctx, ins->src[1].reg.idx[0].offset, 0, &sample_function);
4111 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4112 sampler_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4113 ins->src[1].reg.idx[0].offset, ins->src[2].reg.idx[0].offset);
4114 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE,
4115 NULL, NULL, NULL, "%s", coord_param.param_str);
4116 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4119 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
4121 /* FIXME: Make this work for more than just 2D textures */
4122 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4123 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4125 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
4127 char dst_mask[6];
4129 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4130 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
4131 ins->dst[0].reg.idx[0].offset, dst_mask);
4133 else
4135 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4136 DWORD reg = ins->src[0].reg.idx[0].offset;
4137 char dst_swizzle[6];
4139 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
4141 if (src_mod == WINED3DSPSM_DZ)
4143 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4144 struct glsl_src_param div_param;
4146 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
4148 if (mask_size > 1) {
4149 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4150 } else {
4151 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4154 else if (src_mod == WINED3DSPSM_DW)
4156 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4157 struct glsl_src_param div_param;
4159 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
4161 if (mask_size > 1) {
4162 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4163 } else {
4164 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4166 } else {
4167 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
4172 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4173 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4174 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4175 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
4177 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4178 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4179 struct glsl_sample_function sample_function;
4180 struct glsl_src_param src0_param;
4181 UINT mask_size;
4183 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4185 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4186 * scalar, and projected sampling would require 4.
4188 * It is a dependent read - not valid with conditional NP2 textures
4190 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4191 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4193 switch(mask_size)
4195 case 1:
4196 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4197 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
4198 break;
4200 case 2:
4201 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4202 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
4203 break;
4205 case 3:
4206 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4207 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
4208 break;
4210 default:
4211 FIXME("Unexpected mask size %u\n", mask_size);
4212 break;
4214 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4217 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4218 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4219 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
4221 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4222 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4223 struct glsl_src_param src0_param;
4224 DWORD dst_mask;
4225 unsigned int mask_size;
4227 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4228 mask_size = shader_glsl_get_write_mask_size(dst_mask);
4229 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4231 if (mask_size > 1) {
4232 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
4233 } else {
4234 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
4238 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4239 * Calculate the depth as dst.x / dst.y */
4240 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
4242 struct glsl_dst_param dst_param;
4244 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4246 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4247 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4248 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4249 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4250 * >= 1.0 or < 0.0
4252 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4253 dst_param.reg_name, dst_param.reg_name);
4256 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4257 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4258 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4259 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4261 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
4263 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4264 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4265 struct glsl_src_param src0_param;
4267 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4269 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
4270 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4273 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4274 * Calculate the 1st of a 2-row matrix multiplication. */
4275 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
4277 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4278 DWORD reg = ins->dst[0].reg.idx[0].offset;
4279 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4280 struct glsl_src_param src0_param;
4282 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4283 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4286 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4287 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4288 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
4290 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4291 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4292 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4293 DWORD reg = ins->dst[0].reg.idx[0].offset;
4294 struct glsl_src_param src0_param;
4296 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4297 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
4298 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
4301 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
4303 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4304 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4305 struct glsl_sample_function sample_function;
4306 DWORD reg = ins->dst[0].reg.idx[0].offset;
4307 struct glsl_src_param src0_param;
4309 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4310 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4312 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4314 /* Sample the texture using the calculated coordinates */
4315 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
4316 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4319 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
4320 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
4321 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
4323 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4324 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4325 struct glsl_sample_function sample_function;
4326 DWORD reg = ins->dst[0].reg.idx[0].offset;
4327 struct glsl_src_param src0_param;
4329 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4330 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4332 /* Dependent read, not valid with conditional NP2 */
4333 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4335 /* Sample the texture using the calculated coordinates */
4336 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
4337 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4339 tex_mx->current_row = 0;
4342 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4343 * Perform the 3rd row of a 3x3 matrix multiply */
4344 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
4346 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4347 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4348 DWORD reg = ins->dst[0].reg.idx[0].offset;
4349 struct glsl_src_param src0_param;
4350 char dst_mask[6];
4352 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4354 shader_glsl_append_dst(ins->ctx->buffer, ins);
4355 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4356 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
4358 tex_mx->current_row = 0;
4361 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4362 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4363 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
4365 struct glsl_src_param src0_param;
4366 struct glsl_src_param src1_param;
4367 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4368 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4369 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4370 struct glsl_sample_function sample_function;
4371 DWORD reg = ins->dst[0].reg.idx[0].offset;
4372 char coord_mask[6];
4374 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4375 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4377 /* Perform the last matrix multiply operation */
4378 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4379 /* Reflection calculation */
4380 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
4382 /* Dependent read, not valid with conditional NP2 */
4383 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4384 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4386 /* Sample the texture */
4387 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4388 NULL, NULL, NULL, "tmp0%s", coord_mask);
4389 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4391 tex_mx->current_row = 0;
4394 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4395 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4396 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
4398 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4399 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4400 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4401 struct glsl_sample_function sample_function;
4402 DWORD reg = ins->dst[0].reg.idx[0].offset;
4403 struct glsl_src_param src0_param;
4404 char coord_mask[6];
4406 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4408 /* Perform the last matrix multiply operation */
4409 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
4411 /* Construct the eye-ray vector from w coordinates */
4412 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
4413 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
4414 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4416 /* Dependent read, not valid with conditional NP2 */
4417 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4418 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4420 /* Sample the texture using the calculated coordinates */
4421 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4422 NULL, NULL, NULL, "tmp0%s", coord_mask);
4423 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4425 tex_mx->current_row = 0;
4428 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4429 * Apply a fake bump map transform.
4430 * texbem is pshader <= 1.3 only, this saves a few version checks
4432 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
4434 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4435 struct glsl_sample_function sample_function;
4436 struct glsl_src_param coord_param;
4437 DWORD sampler_idx;
4438 DWORD mask;
4439 DWORD flags;
4440 char coord_mask[6];
4442 sampler_idx = ins->dst[0].reg.idx[0].offset;
4443 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4444 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4446 /* Dependent read, not valid with conditional NP2 */
4447 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4448 mask = sample_function.coord_mask;
4450 shader_glsl_write_mask_to_str(mask, coord_mask);
4452 /* With projected textures, texbem only divides the static texture coord,
4453 * not the displacement, so we can't let GL handle this. */
4454 if (flags & WINED3D_PSARGS_PROJECTED)
4456 DWORD div_mask=0;
4457 char coord_div_mask[3];
4458 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4460 case WINED3D_TTFF_COUNT1:
4461 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4462 break;
4463 case WINED3D_TTFF_COUNT2:
4464 div_mask = WINED3DSP_WRITEMASK_1;
4465 break;
4466 case WINED3D_TTFF_COUNT3:
4467 div_mask = WINED3DSP_WRITEMASK_2;
4468 break;
4469 case WINED3D_TTFF_COUNT4:
4470 case WINED3D_TTFF_DISABLE:
4471 div_mask = WINED3DSP_WRITEMASK_3;
4472 break;
4474 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4475 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4478 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4480 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4481 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4482 coord_param.param_str, coord_mask);
4484 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4486 struct glsl_src_param luminance_param;
4487 struct glsl_dst_param dst_param;
4489 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4490 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4492 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4493 dst_param.reg_name, dst_param.mask_str,
4494 luminance_param.param_str, sampler_idx, sampler_idx);
4496 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4499 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4501 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4502 struct glsl_src_param src0_param, src1_param;
4504 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4505 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4507 shader_glsl_append_dst(ins->ctx->buffer, ins);
4508 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4509 src0_param.param_str, sampler_idx, src1_param.param_str);
4512 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4513 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4514 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4516 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4517 struct glsl_sample_function sample_function;
4518 struct glsl_src_param src0_param;
4520 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4522 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4523 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4524 "%s.wx", src0_param.reg_name);
4525 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4528 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4529 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4530 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4532 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4533 struct glsl_sample_function sample_function;
4534 struct glsl_src_param src0_param;
4536 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4538 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4539 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4540 "%s.yz", src0_param.reg_name);
4541 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4544 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4545 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4546 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4548 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4549 struct glsl_sample_function sample_function;
4550 struct glsl_src_param src0_param;
4552 /* Dependent read, not valid with conditional NP2 */
4553 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4554 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4556 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4557 "%s", src0_param.param_str);
4558 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4561 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4562 * If any of the first 3 components are < 0, discard this pixel */
4563 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4565 if (ins->ctx->reg_maps->shader_version.major >= 4)
4567 struct glsl_src_param src_param;
4569 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4570 shader_addline(ins->ctx->buffer, "if (bool(floatBitsToUint(%s))) discard;\n", src_param.param_str);
4572 else
4574 struct glsl_dst_param dst_param;
4576 /* The argument is a destination parameter, and no writemasks are allowed */
4577 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4579 /* 2.0 shaders compare all 4 components in texkill. */
4580 if (ins->ctx->reg_maps->shader_version.major >= 2)
4581 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4582 /* 1.x shaders only compare the first 3 components, probably due to
4583 * the nature of the texkill instruction as a tex* instruction, and
4584 * phase, which kills all .w components. Even if all 4 components are
4585 * defined, only the first 3 are used. */
4586 else
4587 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4591 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4592 * dst = dot2(src0, src1) + src2 */
4593 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4595 struct glsl_src_param src0_param;
4596 struct glsl_src_param src1_param;
4597 struct glsl_src_param src2_param;
4598 DWORD write_mask;
4599 unsigned int mask_size;
4601 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4602 mask_size = shader_glsl_get_write_mask_size(write_mask);
4604 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4605 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4606 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4608 if (mask_size > 1) {
4609 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4610 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4611 } else {
4612 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4613 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4617 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
4618 const struct wined3d_shader_signature *input_signature,
4619 const struct wined3d_shader_reg_maps *reg_maps,
4620 const struct ps_compile_args *args)
4622 unsigned int i;
4624 for (i = 0; i < input_signature->element_count; ++i)
4626 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4627 const char *semantic_name;
4628 UINT semantic_idx;
4629 char reg_mask[6];
4631 /* Unused */
4632 if (!(reg_maps->input_registers & (1 << input->register_idx)))
4633 continue;
4635 semantic_name = input->semantic_name;
4636 semantic_idx = input->semantic_idx;
4637 shader_glsl_write_mask_to_str(input->mask, reg_mask);
4639 if (args->vp_mode == vertexshader)
4641 if (input->sysval_semantic == WINED3D_SV_POSITION)
4642 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
4643 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4644 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4645 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
4646 else
4647 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
4648 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
4649 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
4651 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4653 if (semantic_idx < 8 && args->vp_mode == pretransformed)
4654 shader_addline(buffer, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4655 shader->u.ps.input_reg_map[input->register_idx], reg_mask, semantic_idx, reg_mask);
4656 else
4657 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4658 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4660 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4662 if (!semantic_idx)
4663 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4664 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4665 else if (semantic_idx == 1)
4666 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4667 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4668 else
4669 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4670 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4672 else
4674 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4675 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4680 /*********************************************
4681 * Vertex Shader Specific Code begins here
4682 ********************************************/
4684 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4686 struct glsl_program_key key;
4688 key.vs_id = entry->vs.id;
4689 key.gs_id = entry->gs.id;
4690 key.ps_id = entry->ps.id;
4692 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4694 ERR("Failed to insert program entry.\n");
4698 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4699 GLuint vs_id, GLuint gs_id, GLuint ps_id)
4701 struct wine_rb_entry *entry;
4702 struct glsl_program_key key;
4704 key.vs_id = vs_id;
4705 key.gs_id = gs_id;
4706 key.ps_id = ps_id;
4708 entry = wine_rb_get(&priv->program_lookup, &key);
4709 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4712 /* Context activation is done by the caller. */
4713 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4714 struct glsl_shader_prog_link *entry)
4716 struct glsl_program_key key;
4718 key.vs_id = entry->vs.id;
4719 key.gs_id = entry->gs.id;
4720 key.ps_id = entry->ps.id;
4721 wine_rb_remove(&priv->program_lookup, &key);
4723 GL_EXTCALL(glDeleteProgram(entry->id));
4724 if (entry->vs.id)
4725 list_remove(&entry->vs.shader_entry);
4726 if (entry->gs.id)
4727 list_remove(&entry->gs.shader_entry);
4728 if (entry->ps.id)
4729 list_remove(&entry->ps.shader_entry);
4730 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4731 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4732 HeapFree(GetProcessHeap(), 0, entry);
4735 static void handle_ps3_input(struct shader_glsl_priv *priv,
4736 const struct wined3d_gl_info *gl_info, const DWORD *map,
4737 const struct wined3d_shader_signature *input_signature,
4738 const struct wined3d_shader_reg_maps *reg_maps_in,
4739 const struct wined3d_shader_signature *output_signature,
4740 const struct wined3d_shader_reg_maps *reg_maps_out)
4742 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
4743 unsigned int i, j;
4744 DWORD *set;
4745 DWORD in_idx;
4746 unsigned int in_count = vec4_varyings(3, gl_info);
4747 char reg_mask[6];
4748 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
4750 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
4752 for (i = 0; i < input_signature->element_count; ++i)
4754 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4756 if (!(reg_maps_in->input_registers & (1 << input->register_idx)))
4757 continue;
4759 in_idx = map[input->register_idx];
4760 /* Declared, but not read register */
4761 if (in_idx == ~0u)
4762 continue;
4763 if (in_idx >= (in_count + 2))
4765 FIXME("More input varyings declared than supported, expect issues.\n");
4766 continue;
4769 if (in_idx == in_count)
4770 string_buffer_sprintf(destination, "gl_FrontColor");
4771 else if (in_idx == in_count + 1)
4772 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
4773 else
4774 string_buffer_sprintf(destination, "ps_link[%u]", in_idx);
4776 if (!set[in_idx])
4777 set[in_idx] = ~0u;
4779 for (j = 0; j < output_signature->element_count; ++j)
4781 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
4782 DWORD mask;
4784 if (!(reg_maps_out->output_registers & (1 << output->register_idx))
4785 || input->semantic_idx != output->semantic_idx
4786 || strcmp(input->semantic_name, output->semantic_name)
4787 || !(mask = input->mask & output->mask))
4788 continue;
4790 if (set[in_idx] == ~0u)
4791 set[in_idx] = mask;
4792 else
4793 set[in_idx] |= mask;
4794 shader_glsl_write_mask_to_str(mask, reg_mask);
4796 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4797 destination->buffer, reg_mask, output->register_idx, reg_mask);
4801 for (i = 0; i < in_count + 2; ++i)
4803 unsigned int size;
4805 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4806 continue;
4808 if (set[i] == ~0U) set[i] = 0;
4810 size = 0;
4811 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4812 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4813 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4814 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4815 reg_mask[size] = '\0';
4817 if (i == in_count)
4818 string_buffer_sprintf(destination, "gl_FrontColor");
4819 else if (i == in_count + 1)
4820 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
4821 else
4822 string_buffer_sprintf(destination, "ps_link[%u]", i);
4824 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
4825 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
4828 HeapFree(GetProcessHeap(), 0, set);
4829 string_buffer_release(&priv->string_buffers, destination);
4832 /* Context activation is done by the caller. */
4833 static GLuint generate_param_reorder_function(struct shader_glsl_priv *priv,
4834 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4835 BOOL per_vertex_point_size, const struct wined3d_gl_info *gl_info)
4837 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
4838 GLuint ret = 0;
4839 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4840 unsigned int i;
4841 const char *semantic_name;
4842 UINT semantic_idx;
4843 char reg_mask[6];
4845 string_buffer_clear(buffer);
4847 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &vs->reg_maps.shader_version));
4849 if (per_vertex_point_size)
4851 shader_addline(buffer, "uniform struct\n{\n");
4852 shader_addline(buffer, " float size_min;\n");
4853 shader_addline(buffer, " float size_max;\n");
4854 shader_addline(buffer, "} ffp_point;\n");
4857 if (ps_major < 3)
4859 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4861 for (i = 0; i < vs->output_signature.element_count; ++i)
4863 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
4864 DWORD write_mask;
4866 if (!(vs->reg_maps.output_registers & (1 << output->register_idx)))
4867 continue;
4869 semantic_name = output->semantic_name;
4870 semantic_idx = output->semantic_idx;
4871 write_mask = output->mask;
4872 shader_glsl_write_mask_to_str(write_mask, reg_mask);
4874 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4876 if (!semantic_idx)
4877 shader_addline(buffer, "gl_FrontColor%s = vs_out[%u]%s;\n",
4878 reg_mask, output->register_idx, reg_mask);
4879 else if (semantic_idx == 1)
4880 shader_addline(buffer, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4881 reg_mask, output->register_idx, reg_mask);
4883 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
4885 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4886 reg_mask, output->register_idx, reg_mask);
4888 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4890 if (semantic_idx < 8)
4892 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
4893 write_mask |= WINED3DSP_WRITEMASK_3;
4895 shader_addline(buffer, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4896 semantic_idx, reg_mask, output->register_idx, reg_mask);
4897 if (!(write_mask & WINED3DSP_WRITEMASK_3))
4898 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
4901 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
4903 shader_addline(buffer, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
4904 output->register_idx, reg_mask[1]);
4906 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
4908 shader_addline(buffer, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n",
4909 output->register_idx, reg_mask[1]);
4912 shader_addline(buffer, "}\n");
4914 else
4916 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
4917 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4918 shader_addline(buffer, "varying vec4 ps_link[%u];\n", in_count);
4919 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4921 /* First, sort out position and point size. Those are not passed to the pixel shader */
4922 for (i = 0; i < vs->output_signature.element_count; ++i)
4924 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
4926 if (!(vs->reg_maps.output_registers & (1 << output->register_idx)))
4927 continue;
4929 semantic_name = output->semantic_name;
4930 semantic_idx = output->semantic_idx;
4931 shader_glsl_write_mask_to_str(output->mask, reg_mask);
4933 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
4935 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4936 reg_mask, output->register_idx, reg_mask);
4938 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
4940 shader_addline(buffer, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
4941 output->register_idx, reg_mask[1]);
4945 /* Then, fix the pixel shader input */
4946 handle_ps3_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
4947 &ps->reg_maps, &vs->output_signature, &vs->reg_maps);
4949 shader_addline(buffer, "}\n");
4952 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
4953 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
4954 shader_glsl_compile(gl_info, ret, buffer->buffer);
4956 return ret;
4959 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer)
4961 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4962 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4963 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4964 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4965 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4966 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4969 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
4971 switch (mode)
4973 case WINED3D_FFP_PS_FOG_OFF:
4974 return;
4976 case WINED3D_FFP_PS_FOG_LINEAR:
4977 shader_addline(buffer, "float fog = (ffp_fog.end - gl_FogFragCoord) * ffp_fog.scale;\n");
4978 break;
4980 case WINED3D_FFP_PS_FOG_EXP:
4981 shader_addline(buffer, "float fog = exp(-ffp_fog.density * gl_FogFragCoord);\n");
4982 break;
4984 case WINED3D_FFP_PS_FOG_EXP2:
4985 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
4986 " * gl_FogFragCoord * gl_FogFragCoord);\n");
4987 break;
4989 default:
4990 ERR("Invalid fog mode %#x.\n", mode);
4991 return;
4994 shader_addline(buffer, "gl_FragData[0].xyz = mix(ffp_fog.color.xyz, gl_FragData[0].xyz,"
4995 " clamp(fog, 0.0, 1.0));\n");
4998 /* Context activation is done by the caller. */
4999 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
5000 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5001 const struct wined3d_shader *shader,
5002 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
5004 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5005 const struct wined3d_gl_info *gl_info = context->gl_info;
5006 const DWORD *function = shader->function;
5007 struct shader_glsl_ctx_priv priv_ctx;
5009 /* Create the hw GLSL shader object and assign it as the shader->prgId */
5010 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5012 memset(&priv_ctx, 0, sizeof(priv_ctx));
5013 priv_ctx.cur_ps_args = args;
5014 priv_ctx.cur_np2fixup_info = np2fixup_info;
5015 priv_ctx.string_buffers = string_buffers;
5017 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5019 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5020 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5021 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5022 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
5023 /* The spec says that it doesn't have to be explicitly enabled, but the
5024 * nvidia drivers write a warning if we don't do so. */
5025 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5026 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5027 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5028 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5029 if (gl_info->supported[EXT_GPU_SHADER4])
5030 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5032 /* Base Declarations */
5033 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5035 /* Pack 3.0 inputs */
5036 if (reg_maps->shader_version.major >= 3)
5037 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args);
5039 /* Base Shader Body */
5040 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5042 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
5043 if (reg_maps->shader_version.major < 2)
5045 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
5046 shader_addline(buffer, "gl_FragData[0] = R0;\n");
5049 if (args->srgb_correction)
5050 shader_glsl_generate_srgb_write_correction(buffer);
5052 /* SM < 3 does not replace the fog stage. */
5053 if (reg_maps->shader_version.major < 3)
5054 shader_glsl_generate_fog_code(buffer, args->fog);
5056 shader_addline(buffer, "}\n");
5058 TRACE("Compiling shader object %u.\n", shader_id);
5059 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5061 return shader_id;
5064 /* Context activation is done by the caller. */
5065 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
5066 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5067 const struct wined3d_shader *shader,
5068 const struct vs_compile_args *args)
5070 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5071 const struct wined3d_gl_info *gl_info = context->gl_info;
5072 const DWORD *function = shader->function;
5073 struct shader_glsl_ctx_priv priv_ctx;
5075 /* Create the hw GLSL shader program and assign it as the shader->prgId */
5076 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5078 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5080 if (gl_info->supported[ARB_DRAW_INSTANCED])
5081 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
5082 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5083 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5084 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5085 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5086 if (gl_info->supported[EXT_GPU_SHADER4])
5087 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5089 memset(&priv_ctx, 0, sizeof(priv_ctx));
5090 priv_ctx.cur_vs_args = args;
5091 priv_ctx.string_buffers = string_buffers;
5093 /* Base Declarations */
5094 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5096 /* Base Shader Body */
5097 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5099 /* Unpack outputs */
5100 shader_addline(buffer, "order_ps_input(vs_out);\n");
5102 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
5103 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
5104 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
5105 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
5107 if (args->fog_src == VS_FOG_Z)
5108 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
5109 else if (!reg_maps->fog)
5110 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
5112 /* We always store the clipplanes without y inversion */
5113 if (args->clip_enabled)
5114 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
5116 if (args->point_size && !args->per_vertex_point_size)
5117 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
5119 /* Write the final position.
5121 * OpenGL coordinates specify the center of the pixel while d3d coords specify
5122 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
5123 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
5124 * contains 1.0 to allow a mad.
5126 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
5127 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
5129 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
5131 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
5132 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
5133 * which is the same as z = z * 2 - w.
5135 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
5137 shader_addline(buffer, "}\n");
5139 TRACE("Compiling shader object %u.\n", shader_id);
5140 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5142 return shader_id;
5145 /* Context activation is done by the caller. */
5146 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
5147 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5148 const struct wined3d_shader *shader)
5150 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5151 const struct wined3d_gl_info *gl_info = context->gl_info;
5152 const DWORD *function = shader->function;
5153 struct shader_glsl_ctx_priv priv_ctx;
5154 GLuint shader_id;
5156 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
5158 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5160 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
5161 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
5162 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5163 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5164 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5165 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5166 if (gl_info->supported[EXT_GPU_SHADER4])
5167 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5169 memset(&priv_ctx, 0, sizeof(priv_ctx));
5170 priv_ctx.string_buffers = string_buffers;
5171 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5172 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5173 shader_addline(buffer, "}\n");
5175 TRACE("Compiling shader object %u.\n", shader_id);
5176 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5178 return shader_id;
5181 static GLuint find_glsl_pshader(const struct wined3d_context *context,
5182 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5183 struct wined3d_shader *shader,
5184 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
5186 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
5187 struct glsl_shader_private *shader_data;
5188 struct ps_np2fixup_info *np2fixup;
5189 UINT i;
5190 DWORD new_size;
5191 GLuint ret;
5193 if (!shader->backend_data)
5195 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5196 if (!shader->backend_data)
5198 ERR("Failed to allocate backend data.\n");
5199 return 0;
5202 shader_data = shader->backend_data;
5203 gl_shaders = shader_data->gl_shaders.ps;
5205 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5206 * so a linear search is more performant than a hashmap or a binary search
5207 * (cache coherency etc)
5209 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5211 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
5213 if (args->np2_fixup)
5214 *np2fixup_info = &gl_shaders[i].np2fixup;
5215 return gl_shaders[i].id;
5219 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5220 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5221 if (shader_data->num_gl_shaders)
5223 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5224 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
5225 new_size * sizeof(*gl_shaders));
5227 else
5229 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5230 new_size = 1;
5233 if(!new_array) {
5234 ERR("Out of memory\n");
5235 return 0;
5237 shader_data->gl_shaders.ps = new_array;
5238 shader_data->shader_array_size = new_size;
5239 gl_shaders = new_array;
5242 gl_shaders[shader_data->num_gl_shaders].args = *args;
5244 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
5245 memset(np2fixup, 0, sizeof(*np2fixup));
5246 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
5248 pixelshader_update_resource_types(shader, args->tex_types);
5250 string_buffer_clear(buffer);
5251 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
5252 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5254 return ret;
5257 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
5258 const DWORD use_map)
5260 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
5261 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
5262 if (stored->point_size != new->point_size)
5263 return FALSE;
5264 if (stored->per_vertex_point_size != new->per_vertex_point_size)
5265 return FALSE;
5266 return stored->fog_src == new->fog_src;
5269 static GLuint find_glsl_vshader(const struct wined3d_context *context,
5270 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5271 struct wined3d_shader *shader,
5272 const struct vs_compile_args *args)
5274 UINT i;
5275 DWORD new_size;
5276 DWORD use_map = context->stream_info.use_map;
5277 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
5278 struct glsl_shader_private *shader_data;
5279 GLuint ret;
5281 if (!shader->backend_data)
5283 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5284 if (!shader->backend_data)
5286 ERR("Failed to allocate backend data.\n");
5287 return 0;
5290 shader_data = shader->backend_data;
5291 gl_shaders = shader_data->gl_shaders.vs;
5293 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5294 * so a linear search is more performant than a hashmap or a binary search
5295 * (cache coherency etc)
5297 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5299 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
5300 return gl_shaders[i].id;
5303 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5305 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5306 if (shader_data->num_gl_shaders)
5308 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5309 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
5310 new_size * sizeof(*gl_shaders));
5312 else
5314 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5315 new_size = 1;
5318 if(!new_array) {
5319 ERR("Out of memory\n");
5320 return 0;
5322 shader_data->gl_shaders.vs = new_array;
5323 shader_data->shader_array_size = new_size;
5324 gl_shaders = new_array;
5327 gl_shaders[shader_data->num_gl_shaders].args = *args;
5329 string_buffer_clear(buffer);
5330 ret = shader_glsl_generate_vshader(context, buffer, string_buffers, shader, args);
5331 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5333 return ret;
5336 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
5337 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5338 struct wined3d_shader *shader)
5340 struct glsl_gs_compiled_shader *gl_shaders;
5341 struct glsl_shader_private *shader_data;
5342 GLuint ret;
5344 if (!shader->backend_data)
5346 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
5348 ERR("Failed to allocate backend data.\n");
5349 return 0;
5352 shader_data = shader->backend_data;
5353 gl_shaders = shader_data->gl_shaders.gs;
5355 if (shader_data->num_gl_shaders)
5356 return gl_shaders[0].id;
5358 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5360 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
5362 ERR("Failed to allocate GL shader array.\n");
5363 return 0;
5365 shader_data->shader_array_size = 1;
5366 gl_shaders = shader_data->gl_shaders.gs;
5368 string_buffer_clear(buffer);
5369 ret = shader_glsl_generate_geometry_shader(context, buffer, string_buffers, shader);
5370 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5372 return ret;
5375 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
5377 switch (mcs)
5379 case WINED3D_MCS_MATERIAL:
5380 return material;
5381 case WINED3D_MCS_COLOR1:
5382 return "ffp_attrib_diffuse";
5383 case WINED3D_MCS_COLOR2:
5384 return "ffp_attrib_specular";
5385 default:
5386 ERR("Invalid material color source %#x.\n", mcs);
5387 return "<invalid>";
5391 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
5392 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
5394 const char *diffuse, *specular, *emissive, *ambient;
5395 enum wined3d_light_type light_type;
5396 unsigned int i;
5398 if (!settings->lighting)
5400 shader_addline(buffer, "gl_FrontColor = ffp_attrib_diffuse;\n");
5401 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_attrib_specular;\n");
5402 return;
5405 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
5406 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
5407 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
5408 shader_addline(buffer, "vec3 dir, dst;\n");
5409 shader_addline(buffer, "float att, t;\n");
5411 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
5412 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
5413 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
5414 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
5416 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
5418 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
5419 switch (light_type)
5421 case WINED3D_LIGHT_POINT:
5422 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5423 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5424 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5425 shader_addline(buffer, "dst.x = 1.0;\n");
5426 if (legacy_lighting)
5428 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
5429 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
5431 else
5433 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5435 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5436 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", i, i, i);
5437 if (!legacy_lighting)
5438 shader_addline(buffer, "att = 1.0 / att;\n");
5439 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5440 if (!settings->normal)
5442 if (!legacy_lighting)
5443 shader_addline(buffer, "}\n");
5444 break;
5446 shader_addline(buffer, "dir = normalize(dir);\n");
5447 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5448 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
5449 if (settings->localviewer)
5450 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5451 else
5452 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5453 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5454 " * ffp_light[%u].specular) * att;\n", i);
5455 if (!legacy_lighting)
5456 shader_addline(buffer, "}\n");
5457 break;
5459 case WINED3D_LIGHT_SPOT:
5460 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5461 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5462 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5463 shader_addline(buffer, "dst.x = 1.0;\n");
5464 if (legacy_lighting)
5466 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
5467 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
5469 else
5471 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5473 shader_addline(buffer, "dir = normalize(dir);\n");
5474 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", i);
5475 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", i);
5476 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", i);
5477 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
5478 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
5479 i, i, i, i);
5480 if (legacy_lighting)
5481 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5482 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5483 i, i, i);
5484 else
5485 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5486 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5487 i, i, i);
5488 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5489 if (!settings->normal)
5491 if (!legacy_lighting)
5492 shader_addline(buffer, "}\n");
5493 break;
5495 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5496 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
5497 if (settings->localviewer)
5498 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5499 else
5500 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5501 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5502 " * ffp_light[%u].specular) * att;\n", i);
5503 if (!legacy_lighting)
5504 shader_addline(buffer, "}\n");
5505 break;
5507 case WINED3D_LIGHT_DIRECTIONAL:
5508 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
5509 if (!settings->normal)
5510 break;
5511 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", i);
5512 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5513 " * ffp_light[%u].diffuse.xyz;\n", i);
5514 /* TODO: In the non-local viewer case the halfvector is constant
5515 * and could be precomputed and stored in a uniform. */
5516 if (settings->localviewer)
5517 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5518 else
5519 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5520 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
5521 " * ffp_light[%u].specular;\n", i);
5522 break;
5524 case WINED3D_LIGHT_PARALLELPOINT:
5525 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
5526 if (!settings->normal)
5527 break;
5528 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", i);
5529 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5530 " * ffp_light[%u].diffuse.xyz;\n", i);
5531 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5532 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
5533 " * ffp_light[%u].specular;\n", i);
5534 break;
5536 default:
5537 if (light_type)
5538 FIXME("Unhandled light type %#x.\n", light_type);
5539 continue;
5543 shader_addline(buffer, "gl_FrontColor.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
5544 ambient, diffuse, emissive);
5545 shader_addline(buffer, "gl_FrontColor.w = %s.w;\n", diffuse);
5546 shader_addline(buffer, "gl_FrontSecondaryColor = %s * specular;\n", specular);
5549 /* Context activation is done by the caller. */
5550 static GLuint shader_glsl_generate_ffp_vertex_shader(struct wined3d_string_buffer *buffer,
5551 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info,
5552 BOOL legacy_lighting)
5554 static const struct attrib_info
5556 const char type[6];
5557 const char name[20];
5559 attrib_info[] =
5561 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
5562 /* TODO: Vertex blending */
5563 {"vec4", ""}, /* WINED3D_FFP_BLENDWEIGHT */
5564 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
5565 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
5566 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
5567 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
5568 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
5570 GLuint shader_obj;
5571 unsigned int i;
5573 string_buffer_clear(buffer);
5575 shader_addline(buffer, "#version 120\n");
5576 shader_addline(buffer, "\n");
5578 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
5580 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
5582 shader_addline(buffer, "attribute %s vs_in%u;\n", type, i);
5584 shader_addline(buffer, "\n");
5586 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix;\n");
5587 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
5588 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
5589 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
5591 shader_addline(buffer, "uniform struct\n{\n");
5592 shader_addline(buffer, " vec4 emissive;\n");
5593 shader_addline(buffer, " vec4 ambient;\n");
5594 shader_addline(buffer, " vec4 diffuse;\n");
5595 shader_addline(buffer, " vec4 specular;\n");
5596 shader_addline(buffer, " float shininess;\n");
5597 shader_addline(buffer, "} ffp_material;\n");
5599 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
5600 shader_addline(buffer, "uniform struct\n{\n");
5601 shader_addline(buffer, " vec4 diffuse;\n");
5602 shader_addline(buffer, " vec4 specular;\n");
5603 shader_addline(buffer, " vec4 ambient;\n");
5604 shader_addline(buffer, " vec4 position;\n");
5605 shader_addline(buffer, " vec3 direction;\n");
5606 shader_addline(buffer, " float range;\n");
5607 shader_addline(buffer, " float falloff;\n");
5608 shader_addline(buffer, " float c_att;\n");
5609 shader_addline(buffer, " float l_att;\n");
5610 shader_addline(buffer, " float q_att;\n");
5611 shader_addline(buffer, " float cos_htheta;\n");
5612 shader_addline(buffer, " float cos_hphi;\n");
5613 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
5615 if (settings->point_size)
5617 shader_addline(buffer, "uniform struct\n{\n");
5618 shader_addline(buffer, " float size;\n");
5619 shader_addline(buffer, " float size_min;\n");
5620 shader_addline(buffer, " float size_max;\n");
5621 shader_addline(buffer, " float c_att;\n");
5622 shader_addline(buffer, " float l_att;\n");
5623 shader_addline(buffer, " float q_att;\n");
5624 shader_addline(buffer, "} ffp_point;\n");
5627 shader_addline(buffer, "\nvoid main()\n{\n");
5628 shader_addline(buffer, "float m;\n");
5629 shader_addline(buffer, "vec3 r;\n");
5631 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
5633 if (attrib_info[i].name[0])
5634 shader_addline(buffer, "%s %s = vs_in%u;\n",
5635 attrib_info[i].type, attrib_info[i].name, i);
5637 for (i = 0; i < MAX_TEXTURES; ++i)
5639 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
5640 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU)
5642 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
5646 if (settings->transformed)
5648 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
5649 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5650 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
5652 else
5654 shader_addline(buffer, "vec4 ec_pos = ffp_modelview_matrix * ffp_attrib_position;\n");
5655 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5656 if (settings->clipping)
5657 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
5658 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
5661 if (!settings->normal)
5662 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
5663 else if (settings->normalize)
5664 shader_addline(buffer, "vec3 normal = normalize(ffp_normal_matrix * ffp_attrib_normal);\n");
5665 else
5666 shader_addline(buffer, "vec3 normal = ffp_normal_matrix * ffp_attrib_normal;\n");
5668 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
5670 for (i = 0; i < MAX_TEXTURES; ++i)
5672 switch (settings->texgen[i] & 0xffff0000)
5674 case WINED3DTSS_TCI_PASSTHRU:
5675 if (settings->texcoords & (1 << i))
5676 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
5677 i, i, i);
5678 break;
5680 case WINED3DTSS_TCI_CAMERASPACENORMAL:
5681 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
5682 break;
5684 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
5685 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
5686 break;
5688 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
5689 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u]"
5690 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
5691 break;
5693 case WINED3DTSS_TCI_SPHEREMAP:
5694 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
5695 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
5696 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u]"
5697 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
5698 break;
5700 default:
5701 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
5702 break;
5706 switch (settings->fog_mode)
5708 case WINED3D_FFP_VS_FOG_OFF:
5709 break;
5711 case WINED3D_FFP_VS_FOG_FOGCOORD:
5712 shader_addline(buffer, "gl_FogFragCoord = ffp_attrib_specular.w * 255.0;\n");
5713 break;
5715 case WINED3D_FFP_VS_FOG_RANGE:
5716 shader_addline(buffer, "gl_FogFragCoord = length(ec_pos.xyz);\n");
5717 break;
5719 case WINED3D_FFP_VS_FOG_DEPTH:
5720 if (settings->ortho_fog)
5721 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
5722 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z * 0.5 + 0.5;\n");
5723 else if (settings->transformed)
5724 shader_addline(buffer, "gl_FogFragCoord = ec_pos.z;\n");
5725 else
5726 shader_addline(buffer, "gl_FogFragCoord = abs(ec_pos.z);\n");
5727 break;
5729 default:
5730 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
5731 break;
5734 if (settings->point_size)
5736 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
5737 " + ffp_point.l_att * length(ec_pos.xyz)"
5738 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
5739 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
5740 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
5743 shader_addline(buffer, "}\n");
5745 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5746 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5748 return shader_obj;
5751 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
5752 DWORD argnum, unsigned int stage, DWORD arg)
5754 const char *ret;
5756 if (arg == ARG_UNUSED)
5757 return "<unused arg>";
5759 switch (arg & WINED3DTA_SELECTMASK)
5761 case WINED3DTA_DIFFUSE:
5762 ret = "gl_Color";
5763 break;
5765 case WINED3DTA_CURRENT:
5766 if (!stage)
5767 ret = "gl_Color";
5768 else
5769 ret = "ret";
5770 break;
5772 case WINED3DTA_TEXTURE:
5773 switch (stage)
5775 case 0: ret = "tex0"; break;
5776 case 1: ret = "tex1"; break;
5777 case 2: ret = "tex2"; break;
5778 case 3: ret = "tex3"; break;
5779 case 4: ret = "tex4"; break;
5780 case 5: ret = "tex5"; break;
5781 case 6: ret = "tex6"; break;
5782 case 7: ret = "tex7"; break;
5783 default:
5784 ret = "<invalid texture>";
5785 break;
5787 break;
5789 case WINED3DTA_TFACTOR:
5790 ret = "tex_factor";
5791 break;
5793 case WINED3DTA_SPECULAR:
5794 ret = "gl_SecondaryColor";
5795 break;
5797 case WINED3DTA_TEMP:
5798 ret = "temp_reg";
5799 break;
5801 case WINED3DTA_CONSTANT:
5802 switch (stage)
5804 case 0: ret = "tss_const0"; break;
5805 case 1: ret = "tss_const1"; break;
5806 case 2: ret = "tss_const2"; break;
5807 case 3: ret = "tss_const3"; break;
5808 case 4: ret = "tss_const4"; break;
5809 case 5: ret = "tss_const5"; break;
5810 case 6: ret = "tss_const6"; break;
5811 case 7: ret = "tss_const7"; break;
5812 default:
5813 ret = "<invalid constant>";
5814 break;
5816 break;
5818 default:
5819 return "<unhandled arg>";
5822 if (arg & WINED3DTA_COMPLEMENT)
5824 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
5825 if (argnum == 0)
5826 ret = "arg0";
5827 else if (argnum == 1)
5828 ret = "arg1";
5829 else if (argnum == 2)
5830 ret = "arg2";
5833 if (arg & WINED3DTA_ALPHAREPLICATE)
5835 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
5836 if (argnum == 0)
5837 ret = "arg0";
5838 else if (argnum == 1)
5839 ret = "arg1";
5840 else if (argnum == 2)
5841 ret = "arg2";
5844 return ret;
5847 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
5848 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
5850 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5852 if (color && alpha)
5853 dstmask = "";
5854 else if (color)
5855 dstmask = ".xyz";
5856 else
5857 dstmask = ".w";
5859 if (dst == tempreg)
5860 dstreg = "temp_reg";
5861 else
5862 dstreg = "ret";
5864 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
5865 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
5866 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
5868 switch (op)
5870 case WINED3D_TOP_DISABLE:
5871 if (!stage)
5872 shader_addline(buffer, "%s%s = gl_Color%s;\n", dstreg, dstmask, dstmask);
5873 break;
5875 case WINED3D_TOP_SELECT_ARG1:
5876 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
5877 break;
5879 case WINED3D_TOP_SELECT_ARG2:
5880 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
5881 break;
5883 case WINED3D_TOP_MODULATE:
5884 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5885 break;
5887 case WINED3D_TOP_MODULATE_4X:
5888 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
5889 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5890 break;
5892 case WINED3D_TOP_MODULATE_2X:
5893 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
5894 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5895 break;
5897 case WINED3D_TOP_ADD:
5898 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
5899 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5900 break;
5902 case WINED3D_TOP_ADD_SIGNED:
5903 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
5904 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5905 break;
5907 case WINED3D_TOP_ADD_SIGNED_2X:
5908 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
5909 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5910 break;
5912 case WINED3D_TOP_SUBTRACT:
5913 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
5914 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5915 break;
5917 case WINED3D_TOP_ADD_SMOOTH:
5918 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
5919 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
5920 break;
5922 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
5923 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5924 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5925 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5926 break;
5928 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5929 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5930 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5931 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5932 break;
5934 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5935 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
5936 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5937 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5938 break;
5940 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5941 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5942 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5943 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
5944 break;
5946 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
5947 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
5948 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5949 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5950 break;
5952 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
5953 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
5954 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5955 break;
5957 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
5958 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
5959 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5960 break;
5962 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
5963 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5964 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5965 break;
5966 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
5967 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
5968 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5969 break;
5971 case WINED3D_TOP_BUMPENVMAP:
5972 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5973 /* These are handled in the first pass, nothing to do. */
5974 break;
5976 case WINED3D_TOP_DOTPRODUCT3:
5977 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
5978 dstreg, dstmask, arg1, arg2, dstmask);
5979 break;
5981 case WINED3D_TOP_MULTIPLY_ADD:
5982 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
5983 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
5984 break;
5986 case WINED3D_TOP_LERP:
5987 /* MSDN isn't quite right here. */
5988 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
5989 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
5990 break;
5992 default:
5993 FIXME("Unhandled operation %#x.\n", op);
5994 break;
5998 /* Context activation is done by the caller. */
5999 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
6000 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
6002 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6003 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
6004 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
6005 const char *final_combiner_src = "ret";
6006 UINT lowest_disabled_stage;
6007 GLuint shader_id;
6008 DWORD arg0, arg1, arg2;
6009 unsigned int stage;
6010 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
6012 string_buffer_clear(buffer);
6014 /* Find out which textures are read */
6015 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6017 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6018 break;
6020 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
6021 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
6022 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
6024 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
6025 || (stage == 0 && settings->color_key_enabled))
6026 tex_map |= 1 << stage;
6027 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6028 tfactor_used = TRUE;
6029 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6030 tempreg_used = TRUE;
6031 if (settings->op[stage].dst == tempreg)
6032 tempreg_used = TRUE;
6033 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6034 tss_const_map |= 1 << stage;
6036 switch (settings->op[stage].cop)
6038 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6039 lum_map |= 1 << stage;
6040 /* fall through */
6041 case WINED3D_TOP_BUMPENVMAP:
6042 bump_map |= 1 << stage;
6043 /* fall through */
6044 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6045 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6046 tex_map |= 1 << stage;
6047 break;
6049 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6050 tfactor_used = TRUE;
6051 break;
6053 default:
6054 break;
6057 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6058 continue;
6060 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
6061 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
6062 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
6064 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
6065 tex_map |= 1 << stage;
6066 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6067 tfactor_used = TRUE;
6068 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6069 tempreg_used = TRUE;
6070 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6071 tss_const_map |= 1 << stage;
6073 lowest_disabled_stage = stage;
6075 shader_addline(buffer, "#version 120\n");
6077 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6078 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6080 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
6081 shader_addline(buffer, "vec4 ret;\n");
6082 if (tempreg_used || settings->sRGB_write)
6083 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
6084 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
6086 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6088 if (tss_const_map & (1 << stage))
6089 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
6091 if (!(tex_map & (1 << stage)))
6092 continue;
6094 switch (settings->op[stage].tex_type)
6096 case WINED3D_GL_RES_TYPE_TEX_1D:
6097 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
6098 break;
6099 case WINED3D_GL_RES_TYPE_TEX_2D:
6100 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
6101 break;
6102 case WINED3D_GL_RES_TYPE_TEX_3D:
6103 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
6104 break;
6105 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6106 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
6107 break;
6108 case WINED3D_GL_RES_TYPE_TEX_RECT:
6109 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
6110 break;
6111 default:
6112 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
6113 break;
6116 shader_addline(buffer, "vec4 tex%u;\n", stage);
6118 if (!(bump_map & (1 << stage)))
6119 continue;
6120 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
6122 if (!(lum_map & (1 << stage)))
6123 continue;
6124 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
6125 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
6127 if (tfactor_used)
6128 shader_addline(buffer, "uniform vec4 tex_factor;\n");
6129 if (settings->color_key_enabled)
6130 shader_addline(buffer, "uniform vec4 color_key;\n");
6131 shader_addline(buffer, "uniform vec4 specular_enable;\n");
6133 if (settings->sRGB_write)
6135 shader_addline(buffer, "const vec4 srgb_const0 = ");
6136 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
6137 shader_addline(buffer, ";\n");
6138 shader_addline(buffer, "const vec4 srgb_const1 = ");
6139 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
6140 shader_addline(buffer, ";\n");
6143 shader_addline(buffer, "uniform struct\n{\n");
6144 shader_addline(buffer, " vec4 color;\n");
6145 shader_addline(buffer, " float density;\n");
6146 shader_addline(buffer, " float end;\n");
6147 shader_addline(buffer, " float scale;\n");
6148 shader_addline(buffer, "} ffp_fog;\n");
6150 shader_addline(buffer, "void main()\n{\n");
6152 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
6153 shader_addline(buffer, "if (any(lessThan(gl_TexCoord[7], vec4(0.0)))) discard;\n");
6155 /* Generate texture sampling instructions) */
6156 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
6158 const char *texture_function, *coord_mask;
6159 BOOL proj;
6161 if (!(tex_map & (1 << stage)))
6162 continue;
6164 if (settings->op[stage].projected == proj_none)
6166 proj = FALSE;
6168 else if (settings->op[stage].projected == proj_count4
6169 || settings->op[stage].projected == proj_count3)
6171 proj = TRUE;
6173 else
6175 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
6176 proj = TRUE;
6179 switch (settings->op[stage].tex_type)
6181 case WINED3D_GL_RES_TYPE_TEX_1D:
6182 if (proj)
6184 texture_function = "texture1DProj";
6185 coord_mask = "xw";
6187 else
6189 texture_function = "texture1D";
6190 coord_mask = "x";
6192 break;
6193 case WINED3D_GL_RES_TYPE_TEX_2D:
6194 if (proj)
6196 texture_function = "texture2DProj";
6197 coord_mask = "xyw";
6199 else
6201 texture_function = "texture2D";
6202 coord_mask = "xy";
6204 break;
6205 case WINED3D_GL_RES_TYPE_TEX_3D:
6206 if (proj)
6208 texture_function = "texture3DProj";
6209 coord_mask = "xyzw";
6211 else
6213 texture_function = "texture3D";
6214 coord_mask = "xyz";
6216 break;
6217 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6218 texture_function = "textureCube";
6219 coord_mask = "xyz";
6220 break;
6221 case WINED3D_GL_RES_TYPE_TEX_RECT:
6222 if (proj)
6224 texture_function = "texture2DRectProj";
6225 coord_mask = "xyw";
6227 else
6229 texture_function = "texture2DRect";
6230 coord_mask = "xy";
6232 break;
6233 default:
6234 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
6235 texture_function = "";
6236 coord_mask = "xyzw";
6237 break;
6240 if (stage > 0
6241 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
6242 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
6244 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
6246 /* With projective textures, texbem only divides the static
6247 * texture coord, not the displacement, so multiply the
6248 * displacement with the dividing parameter before passing it to
6249 * TXP. */
6250 if (settings->op[stage].projected != proj_none)
6252 if (settings->op[stage].projected == proj_count4)
6254 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].w) + gl_TexCoord[%u].xy;\n",
6255 stage, stage);
6256 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].ww;\n", stage);
6258 else
6260 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].z) + gl_TexCoord[%u].xy;\n",
6261 stage, stage);
6262 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].zz;\n", stage);
6265 else
6267 shader_addline(buffer, "ret = gl_TexCoord[%u] + ret.xyxy;\n", stage);
6270 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
6271 stage, texture_function, stage, coord_mask);
6273 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6274 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
6275 stage, stage - 1, stage - 1, stage - 1);
6277 else if (settings->op[stage].projected == proj_count3)
6279 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].xyz);\n",
6280 stage, texture_function, stage, stage);
6282 else
6284 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].%s);\n",
6285 stage, texture_function, stage, stage, coord_mask);
6288 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
6289 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
6290 settings->op[stage].color_fixup);
6293 if (settings->color_key_enabled)
6294 shader_addline(buffer, "if (all(equal(tex0, color_key))) discard;\n");
6296 /* Generate the main shader */
6297 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6299 BOOL op_equal;
6301 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6303 if (!stage)
6304 final_combiner_src = "gl_Color";
6305 break;
6308 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6309 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6310 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
6311 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6312 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6313 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
6314 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6315 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6316 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
6317 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6318 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6319 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
6320 else
6321 op_equal = settings->op[stage].aop == settings->op[stage].cop
6322 && settings->op[stage].carg0 == settings->op[stage].aarg0
6323 && settings->op[stage].carg1 == settings->op[stage].aarg1
6324 && settings->op[stage].carg2 == settings->op[stage].aarg2;
6326 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6328 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6329 settings->op[stage].cop, settings->op[stage].carg0,
6330 settings->op[stage].carg1, settings->op[stage].carg2);
6331 if (!stage)
6332 shader_addline(buffer, "ret.w = gl_Color.w;\n");
6334 else if (op_equal)
6336 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
6337 settings->op[stage].cop, settings->op[stage].carg0,
6338 settings->op[stage].carg1, settings->op[stage].carg2);
6340 else
6342 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6343 settings->op[stage].cop, settings->op[stage].carg0,
6344 settings->op[stage].carg1, settings->op[stage].carg2);
6345 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
6346 settings->op[stage].aop, settings->op[stage].aarg0,
6347 settings->op[stage].aarg1, settings->op[stage].aarg2);
6351 shader_addline(buffer, "gl_FragData[0] = gl_SecondaryColor * specular_enable + %s;\n", final_combiner_src);
6353 if (settings->sRGB_write)
6354 shader_glsl_generate_srgb_write_correction(buffer);
6356 shader_glsl_generate_fog_code(buffer, settings->fog);
6358 shader_addline(buffer, "}\n");
6360 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6361 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6363 string_buffer_release(&priv->string_buffers, tex_reg_name);
6364 return shader_id;
6367 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
6368 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
6370 struct glsl_ffp_vertex_shader *shader;
6371 const struct wine_rb_entry *entry;
6373 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
6374 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
6376 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
6377 return NULL;
6379 shader->desc.settings = *settings;
6380 shader->id = shader_glsl_generate_ffp_vertex_shader(&priv->shader_buffer, settings, gl_info, priv->legacy_lighting);
6381 list_init(&shader->linked_programs);
6382 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
6383 ERR("Failed to insert ffp vertex shader.\n");
6385 return shader;
6388 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
6389 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
6391 struct glsl_ffp_fragment_shader *glsl_desc;
6392 const struct ffp_frag_desc *desc;
6394 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
6395 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
6397 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
6398 return NULL;
6400 glsl_desc->entry.settings = *args;
6401 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
6402 list_init(&glsl_desc->linked_programs);
6403 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
6405 return glsl_desc;
6409 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
6410 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
6412 unsigned int i;
6413 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6415 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
6416 sizeof(GLuint) * gl_info->limits.glsl_vs_float_constants);
6417 for (i = 0; i < vs_c_count; ++i)
6419 string_buffer_sprintf(name, "vs_c[%u]", i);
6420 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6422 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
6423 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLuint));
6425 for (i = 0; i < MAX_CONST_I; ++i)
6427 string_buffer_sprintf(name, "vs_i[%u]", i);
6428 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6431 for (i = 0; i < MAX_CONST_B; ++i)
6433 string_buffer_sprintf(name, "vs_b[%u]", i);
6434 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6437 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "posFixup"));
6439 vs->modelview_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_modelview_matrix"));
6440 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
6441 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
6442 for (i = 0; i < MAX_TEXTURES; ++i)
6444 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
6445 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6447 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
6448 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
6449 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
6450 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
6451 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
6452 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
6453 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
6455 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
6456 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6457 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
6458 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6459 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
6460 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6461 string_buffer_sprintf(name, "ffp_light[%u].position", i);
6462 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6463 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
6464 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6465 string_buffer_sprintf(name, "ffp_light[%u].range", i);
6466 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6467 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
6468 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6469 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
6470 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6471 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
6472 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6473 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
6474 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6475 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
6476 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6477 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
6478 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6480 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
6481 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
6482 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
6483 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
6484 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
6485 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
6487 string_buffer_release(&priv->string_buffers, name);
6490 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
6491 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
6493 unsigned int i;
6494 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6496 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
6497 sizeof(GLuint) * gl_info->limits.glsl_ps_float_constants);
6498 for (i = 0; i < ps_c_count; ++i)
6500 string_buffer_sprintf(name, "ps_c[%u]", i);
6501 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6503 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
6504 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLuint));
6506 for (i = 0; i < MAX_CONST_I; ++i)
6508 string_buffer_sprintf(name, "ps_i[%u]", i);
6509 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6512 for (i = 0; i < MAX_CONST_B; ++i)
6514 string_buffer_sprintf(name, "ps_b[%u]", i);
6515 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6518 for (i = 0; i < MAX_TEXTURES; ++i)
6520 string_buffer_sprintf(name, "bumpenv_mat%u", i);
6521 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6522 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
6523 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6524 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
6525 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6526 string_buffer_sprintf(name, "tss_const%u", i);
6527 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6530 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
6531 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
6533 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
6534 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
6535 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
6536 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
6538 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
6539 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
6540 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
6542 string_buffer_release(&priv->string_buffers, name);
6545 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
6546 struct shader_glsl_priv *priv, GLuint program_id,
6547 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
6549 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6550 GLuint block_idx;
6551 unsigned int i;
6552 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6554 for (i = 0; i < count; ++i)
6556 if (!reg_maps->cb_sizes[i])
6557 continue;
6559 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
6560 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
6561 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
6563 checkGLcall("glUniformBlockBinding");
6564 string_buffer_release(&priv->string_buffers, name);
6567 /* Context activation is done by the caller. */
6568 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
6569 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
6571 const struct wined3d_gl_info *gl_info = context->gl_info;
6572 const struct ps_np2fixup_info *np2fixup_info = NULL;
6573 struct glsl_shader_prog_link *entry = NULL;
6574 struct wined3d_shader *vshader = NULL;
6575 struct wined3d_shader *gshader = NULL;
6576 struct wined3d_shader *pshader = NULL;
6577 GLuint program_id = 0;
6578 GLuint reorder_shader_id = 0;
6579 unsigned int i;
6580 GLuint vs_id = 0;
6581 GLuint gs_id = 0;
6582 GLuint ps_id = 0;
6583 struct list *ps_list, *vs_list;
6584 WORD attribs_map;
6585 struct wined3d_string_buffer *tmp_name;
6587 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_VERTEX)))
6589 vs_id = ctx_data->glsl_program->vs.id;
6590 vs_list = &ctx_data->glsl_program->vs.shader_entry;
6592 if (use_vs(state))
6594 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
6595 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
6597 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_GEOMETRY))
6598 && ctx_data->glsl_program->gs.id)
6599 gs_id = ctx_data->glsl_program->gs.id;
6600 else if (gshader)
6601 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, &priv->string_buffers, gshader);
6604 else if (use_vs(state))
6606 struct vs_compile_args vs_compile_args;
6607 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
6609 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args);
6610 vs_id = find_glsl_vshader(context, &priv->shader_buffer, &priv->string_buffers, vshader, &vs_compile_args);
6611 vs_list = &vshader->linked_programs;
6613 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
6614 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, &priv->string_buffers, gshader);
6616 else if (priv->vertex_pipe == &glsl_vertex_pipe)
6618 struct glsl_ffp_vertex_shader *ffp_shader;
6619 struct wined3d_ffp_vs_settings settings;
6621 wined3d_ffp_get_vs_settings(state, &context->stream_info, &settings);
6622 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
6623 vs_id = ffp_shader->id;
6624 vs_list = &ffp_shader->linked_programs;
6627 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_PIXEL)))
6629 ps_id = ctx_data->glsl_program->ps.id;
6630 ps_list = &ctx_data->glsl_program->ps.shader_entry;
6632 if (use_ps(state))
6633 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
6635 else if (use_ps(state))
6637 struct ps_compile_args ps_compile_args;
6638 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
6639 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, gl_info);
6640 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
6641 pshader, &ps_compile_args, &np2fixup_info);
6642 ps_list = &pshader->linked_programs;
6644 else if (priv->fragment_pipe == &glsl_fragment_pipe)
6646 struct glsl_ffp_fragment_shader *ffp_shader;
6647 struct ffp_frag_settings settings;
6649 gen_ffp_frag_op(context, state, &settings, FALSE);
6650 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
6651 ps_id = ffp_shader->id;
6652 ps_list = &ffp_shader->linked_programs;
6655 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
6657 ctx_data->glsl_program = entry;
6658 return;
6661 /* If we get to this point, then no matching program exists, so we create one */
6662 program_id = GL_EXTCALL(glCreateProgram());
6663 TRACE("Created new GLSL shader program %u.\n", program_id);
6665 /* Create the entry */
6666 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
6667 entry->id = program_id;
6668 entry->vs.id = vs_id;
6669 entry->gs.id = gs_id;
6670 entry->ps.id = ps_id;
6671 entry->constant_version = 0;
6672 entry->ps.np2_fixup_info = np2fixup_info;
6673 /* Add the hash table entry */
6674 add_glsl_program_entry(priv, entry);
6676 /* Set the current program */
6677 ctx_data->glsl_program = entry;
6679 /* Attach GLSL vshader */
6680 if (vs_id)
6682 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
6683 GL_EXTCALL(glAttachShader(program_id, vs_id));
6684 checkGLcall("glAttachShader");
6686 list_add_head(vs_list, &entry->vs.shader_entry);
6689 if (vshader)
6691 attribs_map = vshader->reg_maps.input_registers;
6692 reorder_shader_id = generate_param_reorder_function(priv, vshader, pshader,
6693 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size, gl_info);
6694 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
6695 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
6696 checkGLcall("glAttachShader");
6697 /* Flag the reorder function for deletion, then it will be freed automatically when the program
6698 * is destroyed
6700 GL_EXTCALL(glDeleteShader(reorder_shader_id));
6702 else
6704 attribs_map = (1 << WINED3D_FFP_ATTRIBS_COUNT) - 1;
6707 /* Bind vertex attributes to a corresponding index number to match
6708 * the same index numbers as ARB_vertex_programs (makes loading
6709 * vertex attributes simpler). With this method, we can use the
6710 * exact same code to load the attributes later for both ARB and
6711 * GLSL shaders.
6713 * We have to do this here because we need to know the Program ID
6714 * in order to make the bindings work, and it has to be done prior
6715 * to linking the GLSL program. */
6716 tmp_name = string_buffer_get(&priv->string_buffers);
6717 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
6719 if (!(attribs_map & 1))
6720 continue;
6722 string_buffer_sprintf(tmp_name, "vs_in%u", i);
6723 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
6725 checkGLcall("glBindAttribLocation");
6726 string_buffer_release(&priv->string_buffers, tmp_name);
6728 if (gshader)
6730 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
6731 GL_EXTCALL(glAttachShader(program_id, gs_id));
6732 checkGLcall("glAttachShader");
6734 TRACE("input type %s, output type %s, vertices out %u.\n",
6735 debug_d3dprimitivetype(gshader->u.gs.input_type),
6736 debug_d3dprimitivetype(gshader->u.gs.output_type),
6737 gshader->u.gs.vertices_out);
6738 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
6739 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
6740 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
6741 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
6742 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
6743 gshader->u.gs.vertices_out));
6744 checkGLcall("glProgramParameteriARB");
6746 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
6749 /* Attach GLSL pshader */
6750 if (ps_id)
6752 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
6753 GL_EXTCALL(glAttachShader(program_id, ps_id));
6754 checkGLcall("glAttachShader");
6756 list_add_head(ps_list, &entry->ps.shader_entry);
6759 /* Link the program */
6760 TRACE("Linking GLSL shader program %u.\n", program_id);
6761 GL_EXTCALL(glLinkProgram(program_id));
6762 shader_glsl_validate_link(gl_info, program_id);
6764 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
6765 vshader ? min(vshader->limits->constant_float, gl_info->limits.glsl_vs_float_constants) : 0);
6766 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
6767 pshader ? min(pshader->limits->constant_float, gl_info->limits.glsl_ps_float_constants) : 0);
6768 checkGLcall("Find glsl program uniform locations");
6770 if (pshader && pshader->reg_maps.shader_version.major >= 3
6771 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
6773 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
6774 entry->vs.vertex_color_clamp = GL_FALSE;
6776 else
6778 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
6781 /* Set the shader to allow uniform loading on it */
6782 GL_EXTCALL(glUseProgram(program_id));
6783 checkGLcall("glUseProgram");
6785 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
6786 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
6787 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
6788 * vertex shader with fixed function pixel processing is used we make sure that the card
6789 * supports enough samplers to allow the max number of vertex samplers with all possible
6790 * fixed function fragment processing setups. So once the program is linked these samplers
6791 * won't change. */
6792 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
6794 entry->constant_update_mask = 0;
6795 if (vshader)
6797 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
6798 if (vshader->reg_maps.integer_constants)
6799 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
6800 if (vshader->reg_maps.boolean_constants)
6801 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
6802 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
6804 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps,
6805 0, gl_info->limits.vertex_uniform_blocks);
6807 else
6809 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
6810 | WINED3D_SHADER_CONST_FFP_PROJ;
6812 for (i = 0; i < MAX_TEXTURES; ++i)
6814 if (entry->vs.texture_matrix_location[i] != -1)
6816 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
6817 break;
6820 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
6821 || entry->vs.material_specular_location != -1
6822 || entry->vs.material_emissive_location != -1
6823 || entry->vs.material_shininess_location != -1)
6824 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
6825 if (entry->vs.light_ambient_location != -1)
6826 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
6828 if (entry->vs.pointsize_min_location != -1)
6829 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
6831 if (gshader)
6832 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps,
6833 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
6835 if (ps_id)
6837 if (pshader)
6839 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
6840 if (pshader->reg_maps.integer_constants)
6841 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
6842 if (pshader->reg_maps.boolean_constants)
6843 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
6844 if (entry->ps.ycorrection_location != -1)
6845 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
6847 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps,
6848 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
6849 gl_info->limits.fragment_uniform_blocks);
6851 else
6853 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
6856 for (i = 0; i < MAX_TEXTURES; ++i)
6858 if (entry->ps.bumpenv_mat_location[i] != -1)
6860 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
6861 break;
6865 if (entry->ps.fog_color_location != -1)
6866 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
6867 if (entry->ps.np2_fixup_location != -1)
6868 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
6869 if (entry->ps.color_key_location != -1)
6870 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
6874 /* Context activation is done by the caller. */
6875 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum wined3d_gl_resource_type tex_type,
6876 BOOL masked)
6878 GLuint program_id;
6879 GLuint vshader_id, pshader_id;
6880 const char *blt_pshader;
6882 static const char blt_vshader[] =
6883 "#version 120\n"
6884 "void main(void)\n"
6885 "{\n"
6886 " gl_Position = gl_Vertex;\n"
6887 " gl_FrontColor = vec4(1.0);\n"
6888 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
6889 "}\n";
6891 static const char * const blt_pshaders_full[WINED3D_GL_RES_TYPE_COUNT] =
6893 /* WINED3D_GL_RES_TYPE_TEX_1D */
6894 NULL,
6895 /* WINED3D_GL_RES_TYPE_TEX_2D */
6896 "#version 120\n"
6897 "uniform sampler2D sampler;\n"
6898 "void main(void)\n"
6899 "{\n"
6900 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6901 "}\n",
6902 /* WINED3D_GL_RES_TYPE_TEX_3D */
6903 NULL,
6904 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
6905 "#version 120\n"
6906 "uniform samplerCube sampler;\n"
6907 "void main(void)\n"
6908 "{\n"
6909 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6910 "}\n",
6911 /* WINED3D_GL_RES_TYPE_TEX_RECT */
6912 "#version 120\n"
6913 "#extension GL_ARB_texture_rectangle : enable\n"
6914 "uniform sampler2DRect sampler;\n"
6915 "void main(void)\n"
6916 "{\n"
6917 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6918 "}\n",
6919 /* WINED3D_GL_RES_TYPE_BUFFER */
6920 NULL,
6921 /* WINED3D_GL_RES_TYPE_RB */
6922 NULL,
6925 static const char * const blt_pshaders_masked[WINED3D_GL_RES_TYPE_COUNT] =
6927 /* WINED3D_GL_RES_TYPE_TEX_1D */
6928 NULL,
6929 /* WINED3D_GL_RES_TYPE_TEX_2D */
6930 "#version 120\n"
6931 "uniform sampler2D sampler;\n"
6932 "uniform vec4 mask;\n"
6933 "void main(void)\n"
6934 "{\n"
6935 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6936 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6937 "}\n",
6938 /* WINED3D_GL_RES_TYPE_TEX_3D */
6939 NULL,
6940 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
6941 "#version 120\n"
6942 "uniform samplerCube sampler;\n"
6943 "uniform vec4 mask;\n"
6944 "void main(void)\n"
6945 "{\n"
6946 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6947 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6948 "}\n",
6949 /* WINED3D_GL_RES_TYPE_TEX_RECT */
6950 "#version 120\n"
6951 "#extension GL_ARB_texture_rectangle : enable\n"
6952 "uniform sampler2DRect sampler;\n"
6953 "uniform vec4 mask;\n"
6954 "void main(void)\n"
6955 "{\n"
6956 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6957 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6958 "}\n",
6959 /* WINED3D_GL_RES_TYPE_BUFFER */
6960 NULL,
6961 /* WINED3D_GL_RES_TYPE_RB */
6962 NULL,
6965 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
6966 if (!blt_pshader)
6968 FIXME("tex_type %#x not supported\n", tex_type);
6969 return 0;
6972 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6973 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
6975 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6976 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
6978 program_id = GL_EXTCALL(glCreateProgram());
6979 GL_EXTCALL(glAttachShader(program_id, vshader_id));
6980 GL_EXTCALL(glAttachShader(program_id, pshader_id));
6981 GL_EXTCALL(glLinkProgram(program_id));
6983 shader_glsl_validate_link(gl_info, program_id);
6985 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
6986 * is destroyed
6988 GL_EXTCALL(glDeleteShader(vshader_id));
6989 GL_EXTCALL(glDeleteShader(pshader_id));
6990 return program_id;
6993 /* Context activation is done by the caller. */
6994 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
6995 const struct wined3d_state *state)
6997 struct glsl_context_data *ctx_data = context->shader_backend_data;
6998 const struct wined3d_gl_info *gl_info = context->gl_info;
6999 struct shader_glsl_priv *priv = shader_priv;
7000 GLuint program_id = 0, prev_id = 0;
7001 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
7003 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
7004 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
7006 if (ctx_data->glsl_program)
7008 prev_id = ctx_data->glsl_program->id;
7009 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7011 else
7013 prev_id = 0;
7014 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7017 set_glsl_shader_program(context, state, priv, ctx_data);
7019 if (ctx_data->glsl_program)
7021 program_id = ctx_data->glsl_program->id;
7022 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7024 else
7026 program_id = 0;
7027 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7030 if (old_vertex_color_clamp != current_vertex_color_clamp)
7032 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7034 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
7035 checkGLcall("glClampColorARB");
7037 else
7039 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
7043 TRACE("Using GLSL program %u.\n", program_id);
7045 if (prev_id != program_id)
7047 GL_EXTCALL(glUseProgram(program_id));
7048 checkGLcall("glUseProgram");
7050 if (program_id)
7051 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
7055 /* "context" is not necessarily the currently active context. */
7056 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
7058 struct glsl_context_data *ctx_data = context->shader_backend_data;
7060 ctx_data->glsl_program = NULL;
7061 context->shader_update_mask = (1 << WINED3D_SHADER_TYPE_PIXEL)
7062 | (1 << WINED3D_SHADER_TYPE_VERTEX)
7063 | (1 << WINED3D_SHADER_TYPE_GEOMETRY);
7066 /* Context activation is done by the caller. */
7067 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
7069 const struct wined3d_gl_info *gl_info = context->gl_info;
7070 struct shader_glsl_priv *priv = shader_priv;
7072 shader_glsl_invalidate_current_program(context);
7073 GL_EXTCALL(glUseProgram(0));
7074 checkGLcall("glUseProgram");
7076 priv->vertex_pipe->vp_enable(gl_info, FALSE);
7077 priv->fragment_pipe->enable_extension(gl_info, FALSE);
7079 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7081 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
7082 checkGLcall("glClampColorARB");
7086 /* Context activation is done by the caller. */
7087 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
7088 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size)
7090 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
7091 struct shader_glsl_priv *priv = shader_priv;
7092 GLuint *blt_program;
7093 GLint loc;
7095 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
7096 if (!*blt_program)
7098 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
7099 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
7100 GL_EXTCALL(glUseProgram(*blt_program));
7101 GL_EXTCALL(glUniform1i(loc, 0));
7103 else
7105 GL_EXTCALL(glUseProgram(*blt_program));
7108 if (masked)
7110 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
7111 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
7115 /* Context activation is done by the caller. */
7116 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
7118 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
7119 GLuint program_id;
7121 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
7122 if (program_id) TRACE("Using GLSL program %u\n", program_id);
7124 GL_EXTCALL(glUseProgram(program_id));
7125 checkGLcall("glUseProgram");
7128 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
7129 const struct glsl_shader_prog_link *program)
7131 const struct glsl_context_data *ctx_data;
7132 struct wined3d_context *context;
7133 unsigned int i;
7135 for (i = 0; i < device->context_count; ++i)
7137 context = device->contexts[i];
7138 ctx_data = context->shader_backend_data;
7140 if (ctx_data->glsl_program == program)
7141 shader_glsl_invalidate_current_program(context);
7145 static void shader_glsl_destroy(struct wined3d_shader *shader)
7147 struct glsl_shader_private *shader_data = shader->backend_data;
7148 struct wined3d_device *device = shader->device;
7149 struct shader_glsl_priv *priv = device->shader_priv;
7150 const struct wined3d_gl_info *gl_info;
7151 const struct list *linked_programs;
7152 struct wined3d_context *context;
7154 if (!shader_data || !shader_data->num_gl_shaders)
7156 HeapFree(GetProcessHeap(), 0, shader_data);
7157 shader->backend_data = NULL;
7158 return;
7161 context = context_acquire(device, NULL);
7162 gl_info = context->gl_info;
7164 TRACE("Deleting linked programs.\n");
7165 linked_programs = &shader->linked_programs;
7166 if (linked_programs->next)
7168 struct glsl_shader_prog_link *entry, *entry2;
7169 UINT i;
7171 switch (shader->reg_maps.shader_version.type)
7173 case WINED3D_SHADER_TYPE_PIXEL:
7175 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
7177 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7179 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
7180 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7181 checkGLcall("glDeleteShader");
7183 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
7185 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7186 struct glsl_shader_prog_link, ps.shader_entry)
7188 shader_glsl_invalidate_contexts_program(device, entry);
7189 delete_glsl_program_entry(priv, gl_info, entry);
7192 break;
7195 case WINED3D_SHADER_TYPE_VERTEX:
7197 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
7199 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7201 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
7202 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7203 checkGLcall("glDeleteShader");
7205 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
7207 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7208 struct glsl_shader_prog_link, vs.shader_entry)
7210 shader_glsl_invalidate_contexts_program(device, entry);
7211 delete_glsl_program_entry(priv, gl_info, entry);
7214 break;
7217 case WINED3D_SHADER_TYPE_GEOMETRY:
7219 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
7221 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7223 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
7224 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7225 checkGLcall("glDeleteShader");
7227 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
7229 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7230 struct glsl_shader_prog_link, gs.shader_entry)
7232 shader_glsl_invalidate_contexts_program(device, entry);
7233 delete_glsl_program_entry(priv, gl_info, entry);
7236 break;
7239 default:
7240 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
7241 break;
7245 HeapFree(GetProcessHeap(), 0, shader->backend_data);
7246 shader->backend_data = NULL;
7248 context_release(context);
7251 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
7253 const struct glsl_program_key *k = key;
7254 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
7255 const struct glsl_shader_prog_link, program_lookup_entry);
7257 if (k->vs_id > prog->vs.id) return 1;
7258 else if (k->vs_id < prog->vs.id) return -1;
7260 if (k->gs_id > prog->gs.id) return 1;
7261 else if (k->gs_id < prog->gs.id) return -1;
7263 if (k->ps_id > prog->ps.id) return 1;
7264 else if (k->ps_id < prog->ps.id) return -1;
7266 return 0;
7269 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
7271 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
7272 + constant_count * sizeof(*heap->contained)
7273 + constant_count * sizeof(*heap->positions);
7274 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
7276 if (!mem)
7278 ERR("Failed to allocate memory\n");
7279 return FALSE;
7282 heap->entries = mem;
7283 heap->entries[1].version = 0;
7284 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
7285 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
7286 heap->positions = (unsigned int *)(heap->contained + constant_count);
7287 heap->size = 1;
7289 return TRUE;
7292 static void constant_heap_free(struct constant_heap *heap)
7294 HeapFree(GetProcessHeap(), 0, heap->entries);
7297 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
7299 wined3d_rb_alloc,
7300 wined3d_rb_realloc,
7301 wined3d_rb_free,
7302 glsl_program_key_compare,
7305 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
7306 const struct fragment_pipeline *fragment_pipe)
7308 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7309 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
7310 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
7311 gl_info->limits.glsl_ps_float_constants)) + 1;
7312 struct fragment_caps fragment_caps;
7313 void *vertex_priv, *fragment_priv;
7315 string_buffer_list_init(&priv->string_buffers);
7317 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
7319 ERR("Failed to initialize vertex pipe.\n");
7320 HeapFree(GetProcessHeap(), 0, priv);
7321 return E_FAIL;
7324 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
7326 ERR("Failed to initialize fragment pipe.\n");
7327 vertex_pipe->vp_free(device);
7328 HeapFree(GetProcessHeap(), 0, priv);
7329 return E_FAIL;
7332 if (!string_buffer_init(&priv->shader_buffer))
7334 ERR("Failed to initialize shader buffer.\n");
7335 goto fail;
7338 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
7339 if (!priv->stack)
7341 ERR("Failed to allocate memory.\n");
7342 goto fail;
7345 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
7347 ERR("Failed to initialize vertex shader constant heap\n");
7348 goto fail;
7351 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
7353 ERR("Failed to initialize pixel shader constant heap\n");
7354 goto fail;
7357 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
7359 ERR("Failed to initialize rbtree.\n");
7360 goto fail;
7363 priv->next_constant_version = 1;
7364 priv->vertex_pipe = vertex_pipe;
7365 priv->fragment_pipe = fragment_pipe;
7366 fragment_pipe->get_caps(gl_info, &fragment_caps);
7367 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
7368 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
7370 device->vertex_priv = vertex_priv;
7371 device->fragment_priv = fragment_priv;
7372 device->shader_priv = priv;
7374 return WINED3D_OK;
7376 fail:
7377 constant_heap_free(&priv->pconst_heap);
7378 constant_heap_free(&priv->vconst_heap);
7379 HeapFree(GetProcessHeap(), 0, priv->stack);
7380 string_buffer_free(&priv->shader_buffer);
7381 fragment_pipe->free_private(device);
7382 vertex_pipe->vp_free(device);
7383 HeapFree(GetProcessHeap(), 0, priv);
7384 return E_OUTOFMEMORY;
7387 /* Context activation is done by the caller. */
7388 static void shader_glsl_free(struct wined3d_device *device)
7390 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7391 struct shader_glsl_priv *priv = device->shader_priv;
7392 int i;
7394 for (i = 0; i < WINED3D_GL_RES_TYPE_COUNT; ++i)
7396 if (priv->depth_blt_program_full[i])
7398 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
7400 if (priv->depth_blt_program_masked[i])
7402 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
7406 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
7407 constant_heap_free(&priv->pconst_heap);
7408 constant_heap_free(&priv->vconst_heap);
7409 HeapFree(GetProcessHeap(), 0, priv->stack);
7410 string_buffer_list_cleanup(&priv->string_buffers);
7411 string_buffer_free(&priv->shader_buffer);
7412 priv->fragment_pipe->free_private(device);
7413 priv->vertex_pipe->vp_free(device);
7415 HeapFree(GetProcessHeap(), 0, device->shader_priv);
7416 device->shader_priv = NULL;
7419 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
7421 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
7422 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
7425 static void shader_glsl_free_context_data(struct wined3d_context *context)
7427 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
7430 static void shader_glsl_init_context_state(struct wined3d_context *context)
7432 const struct wined3d_gl_info *gl_info = context->gl_info;
7434 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
7435 checkGLcall("GL_PROGRAM_POINT_SIZE");
7438 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
7440 UINT shader_model;
7442 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
7443 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
7444 && gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] && gl_info->supported[ARB_DRAW_INSTANCED]
7445 && gl_info->supported[ARB_TEXTURE_RG] && gl_info->supported[ARB_SAMPLER_OBJECTS])
7446 shader_model = 4;
7447 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
7448 * texldd and texldl instructions. */
7449 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
7450 shader_model = 3;
7451 else
7452 shader_model = 2;
7453 TRACE("Shader model %u.\n", shader_model);
7455 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
7456 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
7457 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
7459 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
7460 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
7462 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
7463 * Direct3D minimum requirement.
7465 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
7466 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
7468 * The problem is that the refrast clamps temporary results in the shader to
7469 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
7470 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
7471 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
7472 * offer a way to query this.
7474 if (shader_model >= 4)
7475 caps->ps_1x_max_value = FLT_MAX;
7476 else
7477 caps->ps_1x_max_value = 1024.0f;
7479 /* Ideally we'd only set caps like sRGB writes here if supported by both
7480 * the shader backend and the fragment pipe, but we can get called before
7481 * shader_glsl_alloc(). */
7482 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
7483 | WINED3D_SHADER_CAP_SRGB_WRITE;
7486 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
7488 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
7490 TRACE("Checking support for fixup:\n");
7491 dump_color_fixup_desc(fixup);
7494 /* We support everything except YUV conversions. */
7495 if (!is_complex_fixup(fixup))
7497 TRACE("[OK]\n");
7498 return TRUE;
7501 TRACE("[FAILED]\n");
7502 return FALSE;
7505 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
7507 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
7508 /* WINED3DSIH_ADD */ shader_glsl_binop,
7509 /* WINED3DSIH_AND */ shader_glsl_binop,
7510 /* WINED3DSIH_BEM */ shader_glsl_bem,
7511 /* WINED3DSIH_BREAK */ shader_glsl_break,
7512 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
7513 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
7514 /* WINED3DSIH_CALL */ shader_glsl_call,
7515 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
7516 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
7517 /* WINED3DSIH_CND */ shader_glsl_cnd,
7518 /* WINED3DSIH_CRS */ shader_glsl_cross,
7519 /* WINED3DSIH_CUT */ shader_glsl_cut,
7520 /* WINED3DSIH_DCL */ shader_glsl_nop,
7521 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
7522 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
7523 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
7524 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
7525 /* WINED3DSIH_DEF */ shader_glsl_nop,
7526 /* WINED3DSIH_DEFB */ shader_glsl_nop,
7527 /* WINED3DSIH_DEFI */ shader_glsl_nop,
7528 /* WINED3DSIH_DIV */ shader_glsl_binop,
7529 /* WINED3DSIH_DP2 */ shader_glsl_dot,
7530 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
7531 /* WINED3DSIH_DP3 */ shader_glsl_dot,
7532 /* WINED3DSIH_DP4 */ shader_glsl_dot,
7533 /* WINED3DSIH_DST */ shader_glsl_dst,
7534 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
7535 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
7536 /* WINED3DSIH_ELSE */ shader_glsl_else,
7537 /* WINED3DSIH_EMIT */ shader_glsl_emit,
7538 /* WINED3DSIH_ENDIF */ shader_glsl_end,
7539 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
7540 /* WINED3DSIH_ENDREP */ shader_glsl_end,
7541 /* WINED3DSIH_EQ */ shader_glsl_relop,
7542 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
7543 /* WINED3DSIH_EXPP */ shader_glsl_expp,
7544 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
7545 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
7546 /* WINED3DSIH_GE */ shader_glsl_relop,
7547 /* WINED3DSIH_IADD */ shader_glsl_binop,
7548 /* WINED3DSIH_IEQ */ NULL,
7549 /* WINED3DSIH_IF */ shader_glsl_if,
7550 /* WINED3DSIH_IFC */ shader_glsl_ifc,
7551 /* WINED3DSIH_IGE */ shader_glsl_relop,
7552 /* WINED3DSIH_IMUL */ shader_glsl_imul,
7553 /* WINED3DSIH_ISHL */ shader_glsl_binop,
7554 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
7555 /* WINED3DSIH_LABEL */ shader_glsl_label,
7556 /* WINED3DSIH_LD */ NULL,
7557 /* WINED3DSIH_LIT */ shader_glsl_lit,
7558 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
7559 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
7560 /* WINED3DSIH_LOOP */ shader_glsl_loop,
7561 /* WINED3DSIH_LRP */ shader_glsl_lrp,
7562 /* WINED3DSIH_LT */ shader_glsl_relop,
7563 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
7564 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
7565 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
7566 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
7567 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
7568 /* WINED3DSIH_MAD */ shader_glsl_mad,
7569 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
7570 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
7571 /* WINED3DSIH_MOV */ shader_glsl_mov,
7572 /* WINED3DSIH_MOVA */ shader_glsl_mov,
7573 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
7574 /* WINED3DSIH_MUL */ shader_glsl_binop,
7575 /* WINED3DSIH_NE */ shader_glsl_relop,
7576 /* WINED3DSIH_NOP */ shader_glsl_nop,
7577 /* WINED3DSIH_NRM */ shader_glsl_nrm,
7578 /* WINED3DSIH_OR */ shader_glsl_binop,
7579 /* WINED3DSIH_PHASE */ shader_glsl_nop,
7580 /* WINED3DSIH_POW */ shader_glsl_pow,
7581 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
7582 /* WINED3DSIH_REP */ shader_glsl_rep,
7583 /* WINED3DSIH_RET */ shader_glsl_ret,
7584 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
7585 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
7586 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
7587 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
7588 /* WINED3DSIH_SAMPLE_LOD */ NULL,
7589 /* WINED3DSIH_SETP */ NULL,
7590 /* WINED3DSIH_SGE */ shader_glsl_compare,
7591 /* WINED3DSIH_SGN */ shader_glsl_sgn,
7592 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
7593 /* WINED3DSIH_SLT */ shader_glsl_compare,
7594 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
7595 /* WINED3DSIH_SUB */ shader_glsl_binop,
7596 /* WINED3DSIH_TEX */ shader_glsl_tex,
7597 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
7598 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
7599 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
7600 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
7601 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
7602 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
7603 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
7604 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
7605 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
7606 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
7607 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
7608 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
7609 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
7610 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
7611 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
7612 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
7613 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
7614 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
7615 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
7616 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
7617 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
7618 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
7619 /* WINED3DSIH_UGE */ shader_glsl_relop,
7620 /* WINED3DSIH_USHR */ shader_glsl_binop,
7621 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
7622 /* WINED3DSIH_XOR */ shader_glsl_binop,
7625 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
7626 SHADER_HANDLER hw_fct;
7628 /* Select handler */
7629 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
7631 /* Unhandled opcode */
7632 if (!hw_fct)
7634 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
7635 return;
7637 hw_fct(ins);
7639 shader_glsl_add_instruction_modifiers(ins);
7642 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
7644 struct shader_glsl_priv *priv = shader_priv;
7646 return priv->ffp_proj_control;
7649 const struct wined3d_shader_backend_ops glsl_shader_backend =
7651 shader_glsl_handle_instruction,
7652 shader_glsl_select,
7653 shader_glsl_disable,
7654 shader_glsl_select_depth_blt,
7655 shader_glsl_deselect_depth_blt,
7656 shader_glsl_update_float_vertex_constants,
7657 shader_glsl_update_float_pixel_constants,
7658 shader_glsl_load_constants,
7659 shader_glsl_destroy,
7660 shader_glsl_alloc,
7661 shader_glsl_free,
7662 shader_glsl_allocate_context_data,
7663 shader_glsl_free_context_data,
7664 shader_glsl_init_context_state,
7665 shader_glsl_get_caps,
7666 shader_glsl_color_fixup_supported,
7667 shader_glsl_has_ffp_proj_control,
7670 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
7672 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
7674 caps->xyzrhw = TRUE;
7675 caps->ffp_generic_attributes = TRUE;
7676 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
7677 caps->max_vertex_blend_matrices = 1;
7678 caps->max_vertex_blend_matrix_index = 0;
7679 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
7680 | WINED3DVTXPCAPS_MATERIALSOURCE7
7681 | WINED3DVTXPCAPS_VERTEXFOG
7682 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
7683 | WINED3DVTXPCAPS_POSITIONALLIGHTS
7684 | WINED3DVTXPCAPS_LOCALVIEWER
7685 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
7686 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
7687 caps->max_user_clip_planes = gl_info->limits.clipplanes;
7688 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
7691 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7693 struct shader_glsl_priv *priv;
7695 if (shader_backend == &glsl_shader_backend)
7697 priv = shader_priv;
7699 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
7701 ERR("Failed to initialize rbtree.\n");
7702 return NULL;
7705 return priv;
7708 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
7710 return NULL;
7713 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
7715 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7716 struct glsl_ffp_vertex_shader, desc.entry);
7717 struct glsl_shader_prog_link *program, *program2;
7718 struct glsl_ffp_destroy_ctx *ctx = context;
7720 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7721 struct glsl_shader_prog_link, vs.shader_entry)
7723 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7725 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
7726 HeapFree(GetProcessHeap(), 0, shader);
7729 /* Context activation is done by the caller. */
7730 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
7732 struct shader_glsl_priv *priv = device->vertex_priv;
7733 struct glsl_ffp_destroy_ctx ctx;
7735 ctx.priv = priv;
7736 ctx.gl_info = &device->adapter->gl_info;
7737 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
7740 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
7741 const struct wined3d_state *state, DWORD state_id)
7743 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7746 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
7747 const struct wined3d_state *state, DWORD state_id)
7749 const struct wined3d_gl_info *gl_info = context->gl_info;
7750 BOOL transformed = context->stream_info.position_transformed;
7751 BOOL wasrhw = context->last_was_rhw;
7752 unsigned int i;
7754 context->last_was_rhw = transformed;
7756 /* If the vertex declaration contains a transformed position attribute,
7757 * the draw uses the fixed function vertex pipeline regardless of any
7758 * vertex shader set by the application. */
7759 if (transformed != wasrhw)
7760 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7762 if (!use_vs(state))
7764 if (context->last_was_vshader)
7766 for (i = 0; i < gl_info->limits.clipplanes; ++i)
7767 clipplane(context, state, STATE_CLIPPLANE(i));
7770 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7772 /* Because of settings->texcoords, we have to always regenerate the
7773 * vertex shader on a vdecl change.
7774 * TODO: Just always output all the texcoords when there are enough
7775 * varyings available to drop the dependency. */
7776 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7778 if (use_ps(state)
7779 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
7780 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
7781 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7783 else
7785 if (!context->last_was_vshader)
7787 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
7788 for (i = 0; i < gl_info->limits.clipplanes; ++i)
7789 clipplane(context, state, STATE_CLIPPLANE(i));
7793 context->last_was_vshader = use_vs(state);
7796 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
7797 const struct wined3d_state *state, DWORD state_id)
7799 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7800 /* Different vertex shaders potentially require a different vertex attributes setup. */
7801 if (!isStateDirty(context, STATE_VDECL))
7802 context_apply_state(context, state, STATE_VDECL);
7805 static void glsl_vertex_pipe_world(struct wined3d_context *context,
7806 const struct wined3d_state *state, DWORD state_id)
7808 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
7811 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
7813 const struct wined3d_gl_info *gl_info = context->gl_info;
7814 unsigned int k;
7816 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
7817 | WINED3D_SHADER_CONST_FFP_LIGHTS;
7819 for (k = 0; k < gl_info->limits.clipplanes; ++k)
7821 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
7822 clipplane(context, state, STATE_CLIPPLANE(k));
7825 if (context->swapchain->device->vertexBlendUsed)
7827 static int warned;
7829 if (!warned++)
7830 FIXME("Vertex blending emulation.\n");
7834 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
7835 const struct wined3d_state *state, DWORD state_id)
7837 /* Table fog behavior depends on the projection matrix. */
7838 if (state->render_states[WINED3D_RS_FOGENABLE]
7839 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
7840 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7841 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
7844 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
7845 const struct wined3d_state *state, DWORD state_id)
7847 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
7848 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
7849 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
7850 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
7851 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
7852 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
7855 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
7856 const struct wined3d_state *state, DWORD state_id)
7858 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7861 static void glsl_vertex_pipe_material(struct wined3d_context *context,
7862 const struct wined3d_state *state, DWORD state_id)
7864 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
7867 static void glsl_vertex_pipe_light(struct wined3d_context *context,
7868 const struct wined3d_state *state, DWORD state_id)
7870 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
7873 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
7874 const struct wined3d_state *state, DWORD state_id)
7876 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
7879 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
7880 const struct wined3d_state *state, DWORD state_id)
7882 if (!use_vs(state))
7883 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
7886 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
7888 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
7889 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
7890 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
7891 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
7892 /* Clip planes */
7893 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
7894 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
7895 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
7896 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
7897 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
7898 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
7899 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
7900 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
7901 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
7902 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
7903 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
7904 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
7905 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
7906 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
7907 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
7908 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
7909 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
7910 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
7911 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
7912 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
7913 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
7914 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
7915 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
7916 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
7917 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
7918 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
7919 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
7920 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
7921 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
7922 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
7923 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
7924 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
7925 /* Lights */
7926 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7927 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7928 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7929 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7930 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7931 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7932 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7933 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7934 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7935 /* Viewport */
7936 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
7937 /* Transform states */
7938 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
7939 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
7940 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7941 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7942 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7943 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7944 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7945 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7946 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7947 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7948 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
7949 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7950 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7951 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7952 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7953 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7954 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7955 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7956 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7957 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7958 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7959 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7960 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7961 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7962 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7963 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7964 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7965 /* Fog */
7966 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7967 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7968 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7969 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7970 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
7971 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
7972 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7973 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7974 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7975 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7976 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7977 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7978 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7979 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7980 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7981 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7982 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
7983 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
7984 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
7985 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
7986 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
7987 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7988 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7989 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7990 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
7991 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7992 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7993 /* NP2 texture matrix fixups. They are not needed if
7994 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
7995 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
7996 * matrix. */
7997 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7998 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7999 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8000 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8001 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8002 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8003 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8004 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8005 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8006 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8007 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8008 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8009 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8010 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8011 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8012 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8013 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8014 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8015 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8016 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8017 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8018 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8019 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8020 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8021 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8022 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
8025 /* TODO:
8026 * - This currently depends on GL fixed function functions to set things
8027 * like light parameters. Ideally we'd use regular uniforms for that.
8028 * - In part because of the previous point, much of this is modelled after
8029 * GL fixed function, and has much of the same limitations. For example,
8030 * D3D spot lights are slightly different from GL spot lights.
8031 * - We can now implement drawing transformed vertices using the GLSL pipe,
8032 * instead of using the immediate mode fallback.
8033 * - Similarly, we don't need the fallback for certain combinations of
8034 * material sources anymore.
8035 * - Implement vertex blending and vertex tweening.
8036 * - Handle WINED3D_TSS_TEXCOORD_INDEX in the shader, instead of duplicating
8037 * attribute arrays in load_tex_coords().
8038 * - Per-vertex point sizes. */
8039 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
8041 glsl_vertex_pipe_vp_enable,
8042 glsl_vertex_pipe_vp_get_caps,
8043 glsl_vertex_pipe_vp_alloc,
8044 glsl_vertex_pipe_vp_free,
8045 glsl_vertex_pipe_vp_states,
8048 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
8050 /* Nothing to do. */
8053 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
8055 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
8056 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
8057 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
8058 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
8059 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
8060 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
8061 | WINED3DTEXOPCAPS_SELECTARG1
8062 | WINED3DTEXOPCAPS_SELECTARG2
8063 | WINED3DTEXOPCAPS_MODULATE4X
8064 | WINED3DTEXOPCAPS_MODULATE2X
8065 | WINED3DTEXOPCAPS_MODULATE
8066 | WINED3DTEXOPCAPS_ADDSIGNED2X
8067 | WINED3DTEXOPCAPS_ADDSIGNED
8068 | WINED3DTEXOPCAPS_ADD
8069 | WINED3DTEXOPCAPS_SUBTRACT
8070 | WINED3DTEXOPCAPS_ADDSMOOTH
8071 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
8072 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
8073 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
8074 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
8075 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
8076 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
8077 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
8078 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
8079 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
8080 | WINED3DTEXOPCAPS_DOTPRODUCT3
8081 | WINED3DTEXOPCAPS_MULTIPLYADD
8082 | WINED3DTEXOPCAPS_LERP
8083 | WINED3DTEXOPCAPS_BUMPENVMAP
8084 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
8085 caps->MaxTextureBlendStages = 8;
8086 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
8089 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8091 struct shader_glsl_priv *priv;
8093 if (shader_backend == &glsl_shader_backend)
8095 priv = shader_priv;
8097 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
8099 ERR("Failed to initialize rbtree.\n");
8100 return NULL;
8103 return priv;
8106 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
8108 return NULL;
8111 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
8113 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8114 struct glsl_ffp_fragment_shader, entry.entry);
8115 struct glsl_shader_prog_link *program, *program2;
8116 struct glsl_ffp_destroy_ctx *ctx = context;
8118 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8119 struct glsl_shader_prog_link, ps.shader_entry)
8121 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8123 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8124 HeapFree(GetProcessHeap(), 0, shader);
8127 /* Context activation is done by the caller. */
8128 static void glsl_fragment_pipe_free(struct wined3d_device *device)
8130 struct shader_glsl_priv *priv = device->fragment_priv;
8131 struct glsl_ffp_destroy_ctx ctx;
8133 ctx.priv = priv;
8134 ctx.gl_info = &device->adapter->gl_info;
8135 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
8138 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
8139 const struct wined3d_state *state, DWORD state_id)
8141 context->last_was_pshader = use_ps(state);
8143 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
8146 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
8147 const struct wined3d_state *state, DWORD state_id)
8149 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8152 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
8153 const struct wined3d_state *state, DWORD state_id)
8155 BOOL use_vshader = use_vs(state);
8156 enum fogsource new_source;
8157 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
8158 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
8160 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
8162 if (!state->render_states[WINED3D_RS_FOGENABLE])
8163 return;
8165 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
8167 if (use_vshader)
8168 new_source = FOGSOURCE_VS;
8169 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
8170 new_source = FOGSOURCE_COORD;
8171 else
8172 new_source = FOGSOURCE_FFP;
8174 else
8176 new_source = FOGSOURCE_FFP;
8179 if (new_source != context->fog_source || fogstart == fogend)
8181 context->fog_source = new_source;
8182 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8186 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
8187 const struct wined3d_state *state, DWORD state_id)
8189 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
8190 glsl_fragment_pipe_fog(context, state, state_id);
8193 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
8194 const struct wined3d_state *state, DWORD state_id)
8196 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
8199 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
8200 const struct wined3d_state *state, DWORD state_id)
8202 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
8205 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
8206 const struct wined3d_state *state, DWORD state_id)
8208 const struct wined3d_gl_info *gl_info = context->gl_info;
8209 int glParm;
8210 float ref;
8212 TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
8214 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
8216 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
8217 checkGLcall("glEnable GL_ALPHA_TEST");
8219 else
8221 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
8222 checkGLcall("glDisable GL_ALPHA_TEST");
8223 return;
8226 ref = ((float)state->render_states[WINED3D_RS_ALPHAREF]) / 255.0f;
8227 glParm = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
8229 if (glParm)
8231 gl_info->gl_ops.gl.p_glAlphaFunc(glParm, ref);
8232 checkGLcall("glAlphaFunc");
8236 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
8237 const struct wined3d_state *state, DWORD state_id)
8239 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
8242 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
8244 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8245 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8246 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8247 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8248 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8249 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8250 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8251 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8252 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8253 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8254 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8255 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8256 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8257 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8258 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8259 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8260 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8261 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8262 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8263 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8264 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8265 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8266 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8267 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8268 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8269 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8270 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8271 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8272 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8273 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8274 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8275 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8276 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8277 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8278 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8279 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8280 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8281 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8282 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8283 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8284 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8285 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8286 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8287 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8288 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8289 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8290 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8291 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8292 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8293 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8294 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8295 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8296 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8297 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8298 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8299 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8300 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8301 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8302 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8303 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8304 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8305 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8306 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8307 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8308 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8309 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8310 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8311 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8312 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8313 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8314 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8315 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8316 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8317 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8318 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
8319 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
8320 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
8321 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_EXT_NONE },
8322 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8323 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
8324 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
8325 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8326 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8327 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
8328 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
8329 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
8330 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8331 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
8332 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
8333 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
8334 {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 },
8335 {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 },
8336 {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 },
8337 {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 },
8338 {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 },
8339 {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 },
8340 {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 },
8341 {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 },
8342 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8343 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8344 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8345 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8346 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8347 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8348 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8349 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8350 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8351 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
8352 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
8355 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
8357 return TRUE;
8360 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
8364 const struct fragment_pipeline glsl_fragment_pipe =
8366 glsl_fragment_pipe_enable,
8367 glsl_fragment_pipe_get_caps,
8368 glsl_fragment_pipe_alloc,
8369 glsl_fragment_pipe_free,
8370 glsl_fragment_pipe_alloc_context_data,
8371 glsl_fragment_pipe_free_context_data,
8372 shader_glsl_color_fixup_supported,
8373 glsl_fragment_pipe_state_template,