wined3d: Introduce a helper function to allocate arrays.
[wine.git] / dlls / wined3d / glsl_shader.c
blob8a6215f65dc81e6e6bb1999cbffa17efb8c2750e
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 0x01
48 #define WINED3D_GLSL_SAMPLE_LOD 0x02
49 #define WINED3D_GLSL_SAMPLE_GRAD 0x04
50 #define WINED3D_GLSL_SAMPLE_LOAD 0x08
51 #define WINED3D_GLSL_SAMPLE_OFFSET 0x10
53 struct glsl_dst_param
55 char reg_name[150];
56 char mask_str[6];
59 struct glsl_src_param
61 char reg_name[150];
62 char param_str[200];
65 struct glsl_sample_function
67 struct wined3d_string_buffer *name;
68 DWORD coord_mask;
69 enum wined3d_data_type data_type;
70 BOOL output_single_component;
71 unsigned int offset_size;
74 enum heap_node_op
76 HEAP_NODE_TRAVERSE_LEFT,
77 HEAP_NODE_TRAVERSE_RIGHT,
78 HEAP_NODE_POP,
81 struct constant_entry
83 unsigned int idx;
84 unsigned int version;
87 struct constant_heap
89 struct constant_entry *entries;
90 BOOL *contained;
91 unsigned int *positions;
92 unsigned int size;
95 /* GLSL shader private data */
96 struct shader_glsl_priv {
97 struct wined3d_string_buffer shader_buffer;
98 struct wined3d_string_buffer_list string_buffers;
99 struct wine_rb_tree program_lookup;
100 struct constant_heap vconst_heap;
101 struct constant_heap pconst_heap;
102 unsigned char *stack;
103 GLuint depth_blt_program_full[WINED3D_GL_RES_TYPE_COUNT];
104 GLuint depth_blt_program_masked[WINED3D_GL_RES_TYPE_COUNT];
105 UINT next_constant_version;
107 const struct wined3d_vertex_pipe_ops *vertex_pipe;
108 const struct fragment_pipeline *fragment_pipe;
109 struct wine_rb_tree ffp_vertex_shaders;
110 struct wine_rb_tree ffp_fragment_shaders;
111 BOOL ffp_proj_control;
112 BOOL legacy_lighting;
115 struct glsl_vs_program
117 struct list shader_entry;
118 GLuint id;
119 GLenum vertex_color_clamp;
120 GLint uniform_f_locations[WINED3D_MAX_VS_CONSTS_F];
121 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
122 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
123 GLint pos_fixup_location;
125 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
126 GLint projection_matrix_location;
127 GLint normal_matrix_location;
128 GLint texture_matrix_location[MAX_TEXTURES];
129 GLint material_ambient_location;
130 GLint material_diffuse_location;
131 GLint material_specular_location;
132 GLint material_emissive_location;
133 GLint material_shininess_location;
134 GLint light_ambient_location;
135 struct
137 GLint diffuse;
138 GLint specular;
139 GLint ambient;
140 GLint position;
141 GLint direction;
142 GLint range;
143 GLint falloff;
144 GLint c_att;
145 GLint l_att;
146 GLint q_att;
147 GLint cos_htheta;
148 GLint cos_hphi;
149 } light_location[MAX_ACTIVE_LIGHTS];
150 GLint pointsize_location;
151 GLint pointsize_min_location;
152 GLint pointsize_max_location;
153 GLint pointsize_c_att_location;
154 GLint pointsize_l_att_location;
155 GLint pointsize_q_att_location;
158 struct glsl_gs_program
160 struct list shader_entry;
161 GLuint id;
163 GLint pos_fixup_location;
166 struct glsl_ps_program
168 struct list shader_entry;
169 GLuint id;
170 GLint uniform_f_locations[WINED3D_MAX_PS_CONSTS_F];
171 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
172 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
173 GLint bumpenv_mat_location[MAX_TEXTURES];
174 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
175 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
176 GLint tss_constant_location[MAX_TEXTURES];
177 GLint tex_factor_location;
178 GLint specular_enable_location;
179 GLint fog_color_location;
180 GLint fog_density_location;
181 GLint fog_end_location;
182 GLint fog_scale_location;
183 GLint alpha_test_ref_location;
184 GLint ycorrection_location;
185 GLint np2_fixup_location;
186 GLint color_key_location;
187 const struct ps_np2fixup_info *np2_fixup_info;
190 /* Struct to maintain data about a linked GLSL program */
191 struct glsl_shader_prog_link
193 struct wine_rb_entry program_lookup_entry;
194 struct glsl_vs_program vs;
195 struct glsl_gs_program gs;
196 struct glsl_ps_program ps;
197 GLuint id;
198 DWORD constant_update_mask;
199 UINT constant_version;
202 struct glsl_program_key
204 GLuint vs_id;
205 GLuint gs_id;
206 GLuint ps_id;
209 struct shader_glsl_ctx_priv {
210 const struct vs_compile_args *cur_vs_args;
211 const struct ps_compile_args *cur_ps_args;
212 struct ps_np2fixup_info *cur_np2fixup_info;
213 struct wined3d_string_buffer_list *string_buffers;
216 struct glsl_context_data
218 struct glsl_shader_prog_link *glsl_program;
221 struct glsl_ps_compiled_shader
223 struct ps_compile_args args;
224 struct ps_np2fixup_info np2fixup;
225 GLuint id;
228 struct glsl_vs_compiled_shader
230 struct vs_compile_args args;
231 GLuint id;
234 struct glsl_gs_compiled_shader
236 struct gs_compile_args args;
237 GLuint id;
240 struct glsl_shader_private
242 union
244 struct glsl_vs_compiled_shader *vs;
245 struct glsl_gs_compiled_shader *gs;
246 struct glsl_ps_compiled_shader *ps;
247 } gl_shaders;
248 UINT num_gl_shaders, shader_array_size;
251 struct glsl_ffp_vertex_shader
253 struct wined3d_ffp_vs_desc desc;
254 GLuint id;
255 struct list linked_programs;
258 struct glsl_ffp_fragment_shader
260 struct ffp_frag_desc entry;
261 GLuint id;
262 struct list linked_programs;
265 struct glsl_ffp_destroy_ctx
267 struct shader_glsl_priv *priv;
268 const struct wined3d_gl_info *gl_info;
271 static const char *debug_gl_shader_type(GLenum type)
273 switch (type)
275 #define WINED3D_TO_STR(u) case u: return #u
276 WINED3D_TO_STR(GL_VERTEX_SHADER);
277 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
278 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
279 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
280 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
281 #undef WINED3D_TO_STR
282 default:
283 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
287 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
289 switch (type)
291 case WINED3D_SHADER_TYPE_VERTEX:
292 return "vs";
294 case WINED3D_SHADER_TYPE_HULL:
295 return "hs";
297 case WINED3D_SHADER_TYPE_DOMAIN:
298 return "ds";
300 case WINED3D_SHADER_TYPE_GEOMETRY:
301 return "gs";
303 case WINED3D_SHADER_TYPE_PIXEL:
304 return "ps";
306 default:
307 FIXME("Unhandled shader type %#x.\n", type);
308 return "unknown";
312 static const char *shader_glsl_get_version(const struct wined3d_gl_info *gl_info,
313 const struct wined3d_shader_version *version)
315 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
316 return "#version 150";
317 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30) && version && version->major >= 4)
318 return "#version 130";
319 else
320 return "#version 120";
323 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
325 char str[4][17];
327 wined3d_ftoa(values[0], str[0]);
328 wined3d_ftoa(values[1], str[1]);
329 wined3d_ftoa(values[2], str[2]);
330 wined3d_ftoa(values[3], str[3]);
331 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
334 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
335 const int *values, unsigned int size)
337 int i;
339 if (!size || size > 4)
341 ERR("Invalid vector size %u.\n", size);
342 return;
345 if (size > 1)
346 shader_addline(buffer, "ivec%u(", size);
348 for (i = 0; i < size; ++i)
349 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
351 if (size > 1)
352 shader_addline(buffer, ")");
355 static const char *get_info_log_line(const char **ptr)
357 const char *p, *q;
359 p = *ptr;
360 if (!(q = strstr(p, "\n")))
362 if (!*p) return NULL;
363 *ptr += strlen(p);
364 return p;
366 *ptr = q + 1;
368 return p;
371 /* Context activation is done by the caller. */
372 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
374 int length = 0;
375 char *log;
377 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
378 return;
380 if (program)
381 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
382 else
383 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
385 /* A size of 1 is just a null-terminated string, so the log should be bigger than
386 * that if there are errors. */
387 if (length > 1)
389 const char *ptr, *line;
391 log = HeapAlloc(GetProcessHeap(), 0, length);
392 /* The info log is supposed to be zero-terminated, but at least some
393 * versions of fglrx don't terminate the string properly. The reported
394 * length does include the terminator, so explicitly set it to zero
395 * here. */
396 log[length - 1] = 0;
397 if (program)
398 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
399 else
400 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
402 ptr = log;
403 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
405 WARN("Info log received from GLSL shader #%u:\n", id);
406 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
408 else
410 FIXME("Info log received from GLSL shader #%u:\n", id);
411 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
413 HeapFree(GetProcessHeap(), 0, log);
417 /* Context activation is done by the caller. */
418 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
420 const char *ptr, *line;
422 TRACE("Compiling shader object %u.\n", shader);
424 if (TRACE_ON(d3d_shader))
426 ptr = src;
427 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
430 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
431 checkGLcall("glShaderSource");
432 GL_EXTCALL(glCompileShader(shader));
433 checkGLcall("glCompileShader");
434 print_glsl_info_log(gl_info, shader, FALSE);
437 /* Context activation is done by the caller. */
438 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
440 GLint i, shader_count, source_size = -1;
441 GLuint *shaders;
442 char *source = NULL;
444 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
445 if (!(shaders = wined3d_calloc(shader_count, sizeof(*shaders))))
447 ERR("Failed to allocate shader array memory.\n");
448 return;
451 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
452 for (i = 0; i < shader_count; ++i)
454 const char *ptr, *line;
455 GLint tmp;
457 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
459 if (source_size < tmp)
461 HeapFree(GetProcessHeap(), 0, source);
463 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
464 if (!source)
466 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
467 HeapFree(GetProcessHeap(), 0, shaders);
468 return;
470 source_size = tmp;
473 FIXME("Shader %u:\n", shaders[i]);
474 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
475 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
476 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
477 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
478 FIXME("\n");
480 ptr = source;
481 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
482 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
483 FIXME("\n");
486 HeapFree(GetProcessHeap(), 0, source);
487 HeapFree(GetProcessHeap(), 0, shaders);
490 /* Context activation is done by the caller. */
491 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
493 GLint tmp;
495 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
496 return;
498 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
499 if (!tmp)
501 FIXME("Program %u link status invalid.\n", program);
502 shader_glsl_dump_program_source(gl_info, program);
505 print_glsl_info_log(gl_info, program, TRUE);
508 /* Context activation is done by the caller. */
509 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
510 struct shader_glsl_priv *priv, const DWORD *tex_unit_map, GLuint program_id)
512 unsigned int mapped_unit;
513 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
514 const char *prefix;
515 unsigned int i, j;
516 GLint name_loc;
518 static const struct
520 enum wined3d_shader_type type;
521 unsigned int base_idx;
522 unsigned int count;
524 sampler_info[] =
526 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
527 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
530 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
532 prefix = shader_glsl_get_prefix(sampler_info[i].type);
534 for (j = 0; j < sampler_info[i].count; ++j)
536 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, j);
537 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
538 if (name_loc == -1)
539 continue;
541 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
542 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
544 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
545 continue;
548 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
549 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
552 checkGLcall("glUniform1i");
553 string_buffer_release(&priv->string_buffers, sampler_name);
556 /* Context activation is done by the caller. */
557 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
558 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
560 unsigned int start = ~0U, end = 0;
561 int stack_idx = 0;
562 unsigned int heap_idx = 1;
563 unsigned int idx;
565 if (heap->entries[heap_idx].version <= version) return;
567 idx = heap->entries[heap_idx].idx;
568 if (constant_locations[idx] != -1)
569 start = end = idx;
570 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
572 while (stack_idx >= 0)
574 /* Note that we fall through to the next case statement. */
575 switch(stack[stack_idx])
577 case HEAP_NODE_TRAVERSE_LEFT:
579 unsigned int left_idx = heap_idx << 1;
580 if (left_idx < heap->size && heap->entries[left_idx].version > version)
582 heap_idx = left_idx;
583 idx = heap->entries[heap_idx].idx;
584 if (constant_locations[idx] != -1)
586 if (start > idx)
587 start = idx;
588 if (end < idx)
589 end = idx;
592 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
593 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
594 break;
598 case HEAP_NODE_TRAVERSE_RIGHT:
600 unsigned int right_idx = (heap_idx << 1) + 1;
601 if (right_idx < heap->size && heap->entries[right_idx].version > version)
603 heap_idx = right_idx;
604 idx = heap->entries[heap_idx].idx;
605 if (constant_locations[idx] != -1)
607 if (start > idx)
608 start = idx;
609 if (end < idx)
610 end = idx;
613 stack[stack_idx++] = HEAP_NODE_POP;
614 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
615 break;
619 case HEAP_NODE_POP:
620 heap_idx >>= 1;
621 --stack_idx;
622 break;
625 if (start <= end)
626 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
627 checkGLcall("walk_constant_heap()");
630 /* Context activation is done by the caller. */
631 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
632 GLint location, const struct wined3d_vec4 *data)
634 GLfloat clamped_constant[4];
636 if (location == -1) return;
638 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
639 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
640 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
641 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
643 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
646 /* Context activation is done by the caller. */
647 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
648 const struct wined3d_vec4 *constants, const GLint *constant_locations,
649 const struct constant_heap *heap, unsigned char *stack, DWORD version)
651 int stack_idx = 0;
652 unsigned int heap_idx = 1;
653 unsigned int idx;
655 if (heap->entries[heap_idx].version <= version) return;
657 idx = heap->entries[heap_idx].idx;
658 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
659 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
661 while (stack_idx >= 0)
663 /* Note that we fall through to the next case statement. */
664 switch(stack[stack_idx])
666 case HEAP_NODE_TRAVERSE_LEFT:
668 unsigned int left_idx = heap_idx << 1;
669 if (left_idx < heap->size && heap->entries[left_idx].version > version)
671 heap_idx = left_idx;
672 idx = heap->entries[heap_idx].idx;
673 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
675 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
676 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
677 break;
681 case HEAP_NODE_TRAVERSE_RIGHT:
683 unsigned int right_idx = (heap_idx << 1) + 1;
684 if (right_idx < heap->size && heap->entries[right_idx].version > version)
686 heap_idx = right_idx;
687 idx = heap->entries[heap_idx].idx;
688 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
690 stack[stack_idx++] = HEAP_NODE_POP;
691 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
692 break;
696 case HEAP_NODE_POP:
697 heap_idx >>= 1;
698 --stack_idx;
699 break;
702 checkGLcall("walk_constant_heap_clamped()");
705 /* Context activation is done by the caller. */
706 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
707 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
708 unsigned char *stack, unsigned int version)
710 const struct wined3d_shader_lconst *lconst;
712 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
713 if (shader->reg_maps.shader_version.major == 1
714 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
715 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
716 else
717 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
719 if (!shader->load_local_constsF)
721 TRACE("No need to load local float constants for this shader\n");
722 return;
725 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
726 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
728 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
730 checkGLcall("glUniform4fv()");
733 /* Context activation is done by the caller. */
734 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
735 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
737 unsigned int i;
738 struct list* ptr;
740 for (i = 0; constants_set; constants_set >>= 1, ++i)
742 if (!(constants_set & 1)) continue;
744 /* We found this uniform name in the program - go ahead and send the data */
745 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
748 /* Load immediate constants */
749 ptr = list_head(&shader->constantsI);
750 while (ptr)
752 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
753 unsigned int idx = lconst->idx;
754 const GLint *values = (const GLint *)lconst->value;
756 /* We found this uniform name in the program - go ahead and send the data */
757 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
758 ptr = list_next(&shader->constantsI, ptr);
760 checkGLcall("glUniform4iv()");
763 /* Context activation is done by the caller. */
764 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
765 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
767 unsigned int i;
768 struct list* ptr;
770 for (i = 0; constants_set; constants_set >>= 1, ++i)
772 if (!(constants_set & 1)) continue;
774 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
777 /* Load immediate constants */
778 ptr = list_head(&shader->constantsB);
779 while (ptr)
781 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
782 unsigned int idx = lconst->idx;
783 const GLint *values = (const GLint *)lconst->value;
785 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
786 ptr = list_next(&shader->constantsB, ptr);
788 checkGLcall("glUniform1iv()");
791 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
793 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
796 /* Context activation is done by the caller (state handler). */
797 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
798 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
800 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
801 UINT fixup = ps->np2_fixup_info->active;
802 UINT i;
804 for (i = 0; fixup; fixup >>= 1, ++i)
806 const struct wined3d_texture *tex = state->textures[i];
807 unsigned char idx = ps->np2_fixup_info->idx[i];
808 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
810 if (!tex)
812 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
813 continue;
816 if (idx % 2)
818 tex_dim[2] = tex->pow2_matrix[0];
819 tex_dim[3] = tex->pow2_matrix[5];
821 else
823 tex_dim[0] = tex->pow2_matrix[0];
824 tex_dim[1] = tex->pow2_matrix[5];
828 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
831 /* Taken and adapted from Mesa. */
832 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
834 float pos, neg, t, det;
835 struct wined3d_matrix temp;
837 /* Calculate the determinant of upper left 3x3 submatrix and
838 * determine if the matrix is singular. */
839 pos = neg = 0.0f;
840 t = in->_11 * in->_22 * in->_33;
841 if (t >= 0.0f)
842 pos += t;
843 else
844 neg += t;
846 t = in->_21 * in->_32 * in->_13;
847 if (t >= 0.0f)
848 pos += t;
849 else
850 neg += t;
851 t = in->_31 * in->_12 * in->_23;
852 if (t >= 0.0f)
853 pos += t;
854 else
855 neg += t;
857 t = -in->_31 * in->_22 * in->_13;
858 if (t >= 0.0f)
859 pos += t;
860 else
861 neg += t;
862 t = -in->_21 * in->_12 * in->_33;
863 if (t >= 0.0f)
864 pos += t;
865 else
866 neg += t;
868 t = -in->_11 * in->_32 * in->_23;
869 if (t >= 0.0f)
870 pos += t;
871 else
872 neg += t;
874 det = pos + neg;
876 if (fabsf(det) < 1e-25f)
877 return FALSE;
879 det = 1.0f / det;
880 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
881 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
882 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
883 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
884 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
885 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
886 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
887 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
888 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
890 *out = temp;
891 return TRUE;
894 static void swap_rows(float **a, float **b)
896 float *tmp = *a;
898 *a = *b;
899 *b = tmp;
902 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
904 float wtmp[4][8];
905 float m0, m1, m2, m3, s;
906 float *r0, *r1, *r2, *r3;
908 r0 = wtmp[0];
909 r1 = wtmp[1];
910 r2 = wtmp[2];
911 r3 = wtmp[3];
913 r0[0] = m->_11;
914 r0[1] = m->_12;
915 r0[2] = m->_13;
916 r0[3] = m->_14;
917 r0[4] = 1.0f;
918 r0[5] = r0[6] = r0[7] = 0.0f;
920 r1[0] = m->_21;
921 r1[1] = m->_22;
922 r1[2] = m->_23;
923 r1[3] = m->_24;
924 r1[5] = 1.0f;
925 r1[4] = r1[6] = r1[7] = 0.0f;
927 r2[0] = m->_31;
928 r2[1] = m->_32;
929 r2[2] = m->_33;
930 r2[3] = m->_34;
931 r2[6] = 1.0f;
932 r2[4] = r2[5] = r2[7] = 0.0f;
934 r3[0] = m->_41;
935 r3[1] = m->_42;
936 r3[2] = m->_43;
937 r3[3] = m->_44;
938 r3[7] = 1.0f;
939 r3[4] = r3[5] = r3[6] = 0.0f;
941 /* Choose pivot - or die. */
942 if (fabsf(r3[0]) > fabsf(r2[0]))
943 swap_rows(&r3, &r2);
944 if (fabsf(r2[0]) > fabsf(r1[0]))
945 swap_rows(&r2, &r1);
946 if (fabsf(r1[0]) > fabsf(r0[0]))
947 swap_rows(&r1, &r0);
948 if (r0[0] == 0.0f)
949 return FALSE;
951 /* Eliminate first variable. */
952 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
953 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
954 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
955 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
956 s = r0[4];
957 if (s != 0.0f)
959 r1[4] -= m1 * s;
960 r2[4] -= m2 * s;
961 r3[4] -= m3 * s;
963 s = r0[5];
964 if (s != 0.0f)
966 r1[5] -= m1 * s;
967 r2[5] -= m2 * s;
968 r3[5] -= m3 * s;
970 s = r0[6];
971 if (s != 0.0f)
973 r1[6] -= m1 * s;
974 r2[6] -= m2 * s;
975 r3[6] -= m3 * s;
977 s = r0[7];
978 if (s != 0.0f)
980 r1[7] -= m1 * s;
981 r2[7] -= m2 * s;
982 r3[7] -= m3 * s;
985 /* Choose pivot - or die. */
986 if (fabsf(r3[1]) > fabsf(r2[1]))
987 swap_rows(&r3, &r2);
988 if (fabsf(r2[1]) > fabsf(r1[1]))
989 swap_rows(&r2, &r1);
990 if (r1[1] == 0.0f)
991 return FALSE;
993 /* Eliminate second variable. */
994 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
995 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
996 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
997 s = r1[4];
998 if (s != 0.0f)
1000 r2[4] -= m2 * s;
1001 r3[4] -= m3 * s;
1003 s = r1[5];
1004 if (s != 0.0f)
1006 r2[5] -= m2 * s;
1007 r3[5] -= m3 * s;
1009 s = r1[6];
1010 if (s != 0.0f)
1012 r2[6] -= m2 * s;
1013 r3[6] -= m3 * s;
1015 s = r1[7];
1016 if (s != 0.0f)
1018 r2[7] -= m2 * s;
1019 r3[7] -= m3 * s;
1022 /* Choose pivot - or die. */
1023 if (fabsf(r3[2]) > fabsf(r2[2]))
1024 swap_rows(&r3, &r2);
1025 if (r2[2] == 0.0f)
1026 return FALSE;
1028 /* Eliminate third variable. */
1029 m3 = r3[2] / r2[2];
1030 r3[3] -= m3 * r2[3];
1031 r3[4] -= m3 * r2[4];
1032 r3[5] -= m3 * r2[5];
1033 r3[6] -= m3 * r2[6];
1034 r3[7] -= m3 * r2[7];
1036 /* Last check. */
1037 if (r3[3] == 0.0f)
1038 return FALSE;
1040 /* Back substitute row 3. */
1041 s = 1.0f / r3[3];
1042 r3[4] *= s;
1043 r3[5] *= s;
1044 r3[6] *= s;
1045 r3[7] *= s;
1047 /* Back substitute row 2. */
1048 m2 = r2[3];
1049 s = 1.0f / r2[2];
1050 r2[4] = s * (r2[4] - r3[4] * m2);
1051 r2[5] = s * (r2[5] - r3[5] * m2);
1052 r2[6] = s * (r2[6] - r3[6] * m2);
1053 r2[7] = s * (r2[7] - r3[7] * m2);
1054 m1 = r1[3];
1055 r1[4] -= r3[4] * m1;
1056 r1[5] -= r3[5] * m1;
1057 r1[6] -= r3[6] * m1;
1058 r1[7] -= r3[7] * m1;
1059 m0 = r0[3];
1060 r0[4] -= r3[4] * m0;
1061 r0[5] -= r3[5] * m0;
1062 r0[6] -= r3[6] * m0;
1063 r0[7] -= r3[7] * m0;
1065 /* Back substitute row 1. */
1066 m1 = r1[2];
1067 s = 1.0f / r1[1];
1068 r1[4] = s * (r1[4] - r2[4] * m1);
1069 r1[5] = s * (r1[5] - r2[5] * m1);
1070 r1[6] = s * (r1[6] - r2[6] * m1);
1071 r1[7] = s * (r1[7] - r2[7] * m1);
1072 m0 = r0[2];
1073 r0[4] -= r2[4] * m0;
1074 r0[5] -= r2[5] * m0;
1075 r0[6] -= r2[6] * m0;
1076 r0[7] -= r2[7] * m0;
1078 /* Back substitute row 0. */
1079 m0 = r0[1];
1080 s = 1.0f / r0[0];
1081 r0[4] = s * (r0[4] - r1[4] * m0);
1082 r0[5] = s * (r0[5] - r1[5] * m0);
1083 r0[6] = s * (r0[6] - r1[6] * m0);
1084 r0[7] = s * (r0[7] - r1[7] * m0);
1086 out->_11 = r0[4];
1087 out->_12 = r0[5];
1088 out->_13 = r0[6];
1089 out->_14 = r0[7];
1090 out->_21 = r1[4];
1091 out->_22 = r1[5];
1092 out->_23 = r1[6];
1093 out->_24 = r1[7];
1094 out->_31 = r2[4];
1095 out->_32 = r2[5];
1096 out->_33 = r2[6];
1097 out->_34 = r2[7];
1098 out->_41 = r3[4];
1099 out->_42 = r3[5];
1100 out->_43 = r3[6];
1101 out->_44 = r3[7];
1103 return TRUE;
1106 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1107 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1109 const struct wined3d_gl_info *gl_info = context->gl_info;
1110 float mat[3 * 3];
1111 struct wined3d_matrix mv;
1112 unsigned int i, j;
1114 if (prog->vs.normal_matrix_location == -1)
1115 return;
1117 get_modelview_matrix(context, state, 0, &mv);
1118 if (context->swapchain->device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING)
1119 invert_matrix_3d(&mv, &mv);
1120 else
1121 invert_matrix(&mv, &mv);
1122 /* Tests show that singular modelview matrices are used unchanged as normal
1123 * matrices on D3D3 and older. There seems to be no clearly consistent
1124 * behavior on newer D3D versions so always follow older ddraw behavior. */
1125 for (i = 0; i < 3; ++i)
1126 for (j = 0; j < 3; ++j)
1127 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1129 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1130 checkGLcall("glUniformMatrix3fv");
1133 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1134 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1136 const struct wined3d_gl_info *gl_info = context->gl_info;
1137 struct wined3d_matrix mat;
1139 if (tex >= MAX_TEXTURES)
1140 return;
1141 if (prog->vs.texture_matrix_location[tex] == -1)
1142 return;
1144 get_texture_matrix(context, state, tex, &mat);
1145 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1146 checkGLcall("glUniformMatrix4fv");
1149 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1150 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1152 const struct wined3d_gl_info *gl_info = context->gl_info;
1154 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1156 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1157 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1159 else
1161 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1163 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1165 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1166 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1167 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1168 checkGLcall("setting FFP material uniforms");
1171 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1172 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1174 const struct wined3d_gl_info *gl_info = context->gl_info;
1175 struct wined3d_color color;
1177 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1178 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1179 checkGLcall("glUniform3fv");
1182 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1183 const struct wined3d_matrix *src2)
1185 struct wined3d_vec4 temp;
1187 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1188 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1189 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1190 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1192 *dest = temp;
1195 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1196 const struct wined3d_state *state, unsigned int light, struct glsl_shader_prog_link *prog)
1198 const struct wined3d_gl_info *gl_info = context->gl_info;
1199 const struct wined3d_light_info *light_info = state->lights[light];
1200 struct wined3d_vec4 vec4;
1201 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1203 if (!light_info)
1204 return;
1206 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1207 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1208 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1210 switch (light_info->OriginalParms.type)
1212 case WINED3D_LIGHT_POINT:
1213 multiply_vector_matrix(&vec4, &light_info->position, view);
1214 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1215 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1216 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1217 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1218 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1219 break;
1221 case WINED3D_LIGHT_SPOT:
1222 multiply_vector_matrix(&vec4, &light_info->position, view);
1223 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1225 multiply_vector_matrix(&vec4, &light_info->direction, view);
1226 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1228 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1229 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1230 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1231 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1232 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1233 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1234 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1235 break;
1237 case WINED3D_LIGHT_DIRECTIONAL:
1238 multiply_vector_matrix(&vec4, &light_info->direction, view);
1239 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1240 break;
1242 case WINED3D_LIGHT_PARALLELPOINT:
1243 multiply_vector_matrix(&vec4, &light_info->position, view);
1244 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1245 break;
1247 default:
1248 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1250 checkGLcall("setting FFP lights uniforms");
1253 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1254 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1256 const struct wined3d_gl_info *gl_info = context->gl_info;
1257 float min, max;
1258 float size, att[3];
1260 get_pointsize_minmax(context, state, &min, &max);
1262 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1263 checkGLcall("glUniform1f");
1264 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1265 checkGLcall("glUniform1f");
1267 get_pointsize(context, state, &size, att);
1269 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1270 checkGLcall("glUniform1f");
1271 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1272 checkGLcall("glUniform1f");
1273 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1274 checkGLcall("glUniform1f");
1275 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1276 checkGLcall("glUniform1f");
1279 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1280 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1282 const struct wined3d_gl_info *gl_info = context->gl_info;
1283 struct wined3d_color color;
1284 float start, end, scale;
1285 union
1287 DWORD d;
1288 float f;
1289 } tmpvalue;
1291 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1292 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1293 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1294 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1295 get_fog_start_end(context, state, &start, &end);
1296 scale = 1.0f / (end - start);
1297 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1298 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1299 checkGLcall("fog emulation uniforms");
1302 /* Context activation is done by the caller (state handler). */
1303 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1304 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1306 struct wined3d_color float_key[2];
1307 const struct wined3d_texture *texture = state->textures[0];
1309 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1310 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1313 /* Context activation is done by the caller (state handler). */
1314 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1315 const struct wined3d_state *state)
1317 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1318 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1319 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1320 const struct wined3d_gl_info *gl_info = context->gl_info;
1321 struct shader_glsl_priv *priv = shader_priv;
1322 float position_fixup[4];
1323 DWORD update_mask;
1325 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1326 UINT constant_version;
1327 int i;
1329 if (!prog) {
1330 /* No GLSL program set - nothing to do. */
1331 return;
1333 constant_version = prog->constant_version;
1334 update_mask = context->constant_update_mask & prog->constant_update_mask;
1336 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1337 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1338 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1340 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1341 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1342 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1344 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1345 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1346 vshader->reg_maps.boolean_constants);
1348 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1349 shader_glsl_pointsize_uniform(context, state, prog);
1351 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1353 shader_get_position_fixup(context, state, position_fixup);
1354 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1355 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, 1, position_fixup));
1356 else
1357 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1358 checkGLcall("glUniform4fv");
1361 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1363 struct wined3d_matrix mat;
1365 get_modelview_matrix(context, state, 0, &mat);
1366 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1367 checkGLcall("glUniformMatrix4fv");
1369 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1372 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1374 struct wined3d_matrix mat;
1376 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1378 if (prog->vs.modelview_matrix_location[i] == -1)
1379 break;
1381 get_modelview_matrix(context, state, i, &mat);
1382 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1383 checkGLcall("glUniformMatrix4fv");
1387 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1389 struct wined3d_matrix projection;
1391 get_projection_matrix(context, state, &projection);
1392 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1393 checkGLcall("glUniformMatrix4fv");
1396 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1398 for (i = 0; i < MAX_TEXTURES; ++i)
1399 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1402 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1403 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1405 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1407 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1408 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1409 shader_glsl_ffp_vertex_light_uniform(context, state, i, prog);
1412 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1413 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1414 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1416 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1417 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1418 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1420 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1421 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1422 pshader->reg_maps.boolean_constants);
1424 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1426 for (i = 0; i < MAX_TEXTURES; ++i)
1428 if (prog->ps.bumpenv_mat_location[i] == -1)
1429 continue;
1431 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1432 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1434 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1436 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1437 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1438 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1439 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1443 checkGLcall("bump env uniforms");
1446 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1448 const struct wined3d_vec4 correction_params =
1450 /* Position is relative to the framebuffer, not the viewport. */
1451 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1452 context->render_offscreen ? 1.0f : -1.0f,
1453 0.0f,
1454 0.0f,
1457 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1460 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1461 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1462 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1463 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1465 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1467 struct wined3d_color color;
1469 if (prog->ps.tex_factor_location != -1)
1471 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1472 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1475 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1476 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1477 else
1478 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1480 for (i = 0; i < MAX_TEXTURES; ++i)
1482 if (prog->ps.tss_constant_location[i] == -1)
1483 continue;
1485 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1486 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1489 checkGLcall("fixed function uniforms");
1492 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1493 shader_glsl_load_fog_uniform(context, state, prog);
1495 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1497 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
1499 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1500 checkGLcall("alpha test emulation uniform");
1503 if (priv->next_constant_version == UINT_MAX)
1505 TRACE("Max constant version reached, resetting to 0.\n");
1506 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1507 priv->next_constant_version = 1;
1509 else
1511 prog->constant_version = priv->next_constant_version++;
1515 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1517 struct constant_entry *entries = heap->entries;
1518 unsigned int *positions = heap->positions;
1519 unsigned int heap_idx, parent_idx;
1521 if (!heap->contained[idx])
1523 heap_idx = heap->size++;
1524 heap->contained[idx] = TRUE;
1526 else
1528 heap_idx = positions[idx];
1531 while (heap_idx > 1)
1533 parent_idx = heap_idx >> 1;
1535 if (new_version <= entries[parent_idx].version) break;
1537 entries[heap_idx] = entries[parent_idx];
1538 positions[entries[parent_idx].idx] = heap_idx;
1539 heap_idx = parent_idx;
1542 entries[heap_idx].version = new_version;
1543 entries[heap_idx].idx = idx;
1544 positions[idx] = heap_idx;
1547 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1549 struct shader_glsl_priv *priv = device->shader_priv;
1550 struct constant_heap *heap = &priv->vconst_heap;
1551 UINT i;
1553 for (i = start; i < count + start; ++i)
1555 update_heap_entry(heap, i, priv->next_constant_version);
1559 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1561 struct shader_glsl_priv *priv = device->shader_priv;
1562 struct constant_heap *heap = &priv->pconst_heap;
1563 UINT i;
1565 for (i = start; i < count + start; ++i)
1567 update_heap_entry(heap, i, priv->next_constant_version);
1571 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1573 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1574 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1575 if(shader_major > 3) return ret;
1577 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1578 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1579 return ret;
1582 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1584 return gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
1587 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
1589 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
1592 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1593 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1595 va_list args;
1596 int ret;
1598 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1599 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1600 for (;;)
1602 va_start(args, format);
1603 ret = shader_vaddline(buffer, format, args);
1604 va_end(args);
1605 if (!ret)
1606 return;
1607 if (!string_buffer_resize(buffer, ret))
1608 return;
1612 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1613 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1615 va_list args;
1616 int ret;
1618 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1619 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1620 for (;;)
1622 va_start(args, format);
1623 ret = shader_vaddline(buffer, format, args);
1624 va_end(args);
1625 if (!ret)
1626 return;
1627 if (!string_buffer_resize(buffer, ret))
1628 return;
1632 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
1634 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
1637 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
1639 switch (primitive_type)
1641 case WINED3D_PT_POINTLIST:
1642 return "points";
1644 case WINED3D_PT_LINELIST:
1645 return "lines";
1647 case WINED3D_PT_LINESTRIP:
1648 return "line_strip";
1650 case WINED3D_PT_TRIANGLELIST:
1651 return "triangles";
1653 case WINED3D_PT_TRIANGLESTRIP:
1654 return "triangle_strip";
1656 case WINED3D_PT_LINELIST_ADJ:
1657 return "lines_adjacency";
1659 case WINED3D_PT_TRIANGLELIST_ADJ:
1660 return "triangles_adjacency";
1662 default:
1663 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
1664 return "";
1668 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
1670 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
1671 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1672 const BOOL *input_reg_used = shader->u.ps.input_reg_used;
1673 unsigned int i;
1675 if (reg_maps->shader_version.major < 3)
1676 return input_reg_used[idx];
1678 for (i = 0; i < input_signature->element_count; ++i)
1680 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
1682 if (!(reg_maps->input_registers & (1u << input->register_idx)))
1683 continue;
1685 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
1686 && input->semantic_idx == idx)
1688 if (input_reg_used[input->register_idx])
1689 return TRUE;
1690 else
1691 return FALSE;
1694 return FALSE;
1697 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
1698 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
1700 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
1702 if (version->major >= 4)
1703 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
1704 else
1705 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
1708 /** Generate the variable & register declarations for the GLSL output target */
1709 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1710 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1711 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1713 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1714 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1715 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1716 const struct wined3d_gl_info *gl_info = context->gl_info;
1717 unsigned int i, extra_constants_needed = 0;
1718 const struct wined3d_shader_lconst *lconst;
1719 const char *prefix;
1720 DWORD map;
1722 prefix = shader_glsl_get_prefix(version->type);
1724 /* Prototype the subroutines */
1725 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1727 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1730 /* Declare the constants (aka uniforms) */
1731 if (shader->limits->constant_float > 0)
1733 unsigned max_constantsF;
1735 /* Unless the shader uses indirect addressing, always declare the
1736 * maximum array size and ignore that we need some uniforms privately.
1737 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1738 * and immediate values, still declare VC[256]. If the shader needs
1739 * more uniforms than we have it won't work in any case. If it uses
1740 * less, the compiler will figure out which uniforms are really used
1741 * and strip them out. This allows a shader to use c255 on a dx9 card,
1742 * as long as it doesn't also use all the other constants.
1744 * If the shader uses indirect addressing the compiler must assume
1745 * that all declared uniforms are used. In this case, declare only the
1746 * amount that we're assured to have.
1748 * Thus we run into problems in these two cases:
1749 * 1) The shader really uses more uniforms than supported.
1750 * 2) The shader uses indirect addressing, less constants than
1751 * supported, but uses a constant index > #supported consts. */
1752 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1754 /* No indirect addressing here. */
1755 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1757 else
1759 if (reg_maps->usesrelconstF)
1761 /* Subtract the other potential uniforms from the max
1762 * available (bools, ints, and 1 row of projection matrix).
1763 * Subtract another uniform for immediate values, which have
1764 * to be loaded via uniform by the driver as well. The shader
1765 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1766 * shader code, so one vec4 should be enough. (Unfortunately
1767 * the Nvidia driver doesn't store 128 and -128 in one float).
1769 * Writing gl_ClipVertex requires one uniform for each
1770 * clipplane as well. */
1771 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1772 if (vs_args->clip_enabled)
1773 max_constantsF -= gl_info->limits.clipplanes;
1774 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
1775 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1776 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1777 * for now take this into account when calculating the number of available constants
1779 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
1780 /* Set by driver quirks in directx.c */
1781 max_constantsF -= gl_info->reserved_glsl_constants;
1783 if (max_constantsF < shader->limits->constant_float)
1785 static unsigned int once;
1787 if (!once++)
1788 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1789 " it may not render correctly.\n");
1790 else
1791 WARN("The hardware does not support enough uniform components to run this shader.\n");
1794 else
1796 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1799 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1800 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1803 /* Always declare the full set of constants, the compiler can remove the
1804 * unused ones because d3d doesn't (yet) support indirect int and bool
1805 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1806 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1807 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1809 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1810 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1812 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1814 if (reg_maps->cb_sizes[i])
1815 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1816 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1819 /* Declare texture samplers */
1820 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1822 struct wined3d_shader_sampler_map_entry *entry;
1823 const char *sampler_type_prefix, *sampler_type;
1824 BOOL shadow_sampler, tex_rect;
1826 entry = &reg_maps->sampler_map.entries[i];
1828 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1830 ERR("Invalid resource index %u.\n", entry->resource_idx);
1831 continue;
1834 switch (reg_maps->resource_info[entry->resource_idx].data_type)
1836 case WINED3D_DATA_FLOAT:
1837 case WINED3D_DATA_UNORM:
1838 case WINED3D_DATA_SNORM:
1839 sampler_type_prefix = "";
1840 break;
1842 case WINED3D_DATA_INT:
1843 sampler_type_prefix = "i";
1844 break;
1846 case WINED3D_DATA_UINT:
1847 sampler_type_prefix = "u";
1848 break;
1850 default:
1851 sampler_type_prefix = "";
1852 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
1853 break;
1856 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
1857 switch (reg_maps->resource_info[entry->resource_idx].type)
1859 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1860 if (shadow_sampler)
1861 sampler_type = "sampler1DShadow";
1862 else
1863 sampler_type = "sampler1D";
1864 break;
1866 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1867 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1868 && (ps_args->np2_fixup & (1u << entry->resource_idx))
1869 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1870 if (shadow_sampler)
1872 if (tex_rect)
1873 sampler_type = "sampler2DRectShadow";
1874 else
1875 sampler_type = "sampler2DShadow";
1877 else
1879 if (tex_rect)
1880 sampler_type = "sampler2DRect";
1881 else
1882 sampler_type = "sampler2D";
1884 break;
1886 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1887 if (shadow_sampler)
1888 FIXME("Unsupported 3D shadow sampler.\n");
1889 sampler_type = "sampler3D";
1890 break;
1892 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1893 if (shadow_sampler)
1894 FIXME("Unsupported Cube shadow sampler.\n");
1895 sampler_type = "samplerCube";
1896 break;
1898 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
1899 if (shadow_sampler)
1900 sampler_type = "sampler2DArrayShadow";
1901 else
1902 sampler_type = "sampler2DArray";
1903 break;
1905 default:
1906 sampler_type = "unsupported_sampler";
1907 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
1908 break;
1910 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
1911 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
1914 /* Declare uniforms for NP2 texcoord fixup:
1915 * This is NOT done inside the loop that declares the texture samplers
1916 * since the NP2 fixup code is currently only used for the GeforceFX
1917 * series and when forcing the ARB_npot extension off. Modern cards just
1918 * skip the code anyway, so put it inside a separate loop. */
1919 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1921 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1922 UINT cur = 0;
1924 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1925 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1926 * samplerNP2Fixup stores texture dimensions and is updated through
1927 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1929 for (i = 0; i < shader->limits->sampler; ++i)
1931 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
1932 continue;
1934 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1936 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1937 continue;
1940 fixup->idx[i] = cur++;
1943 fixup->num_consts = (cur + 1) >> 1;
1944 fixup->active = ps_args->np2_fixup;
1945 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1948 /* Declare address variables */
1949 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1951 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1954 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1956 for (i = 0; i < shader->input_signature.element_count; ++i)
1958 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
1959 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
1960 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
1961 prefix, e->register_idx);
1962 else if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
1963 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1964 prefix, e->register_idx);
1965 else
1966 shader_addline(buffer, "%s vec4 %s_in%u;\n",
1967 get_attribute_keyword(gl_info), prefix, e->register_idx);
1970 if (vs_args->point_size && !vs_args->per_vertex_point_size)
1972 shader_addline(buffer, "uniform struct\n{\n");
1973 shader_addline(buffer, " float size;\n");
1974 shader_addline(buffer, " float size_min;\n");
1975 shader_addline(buffer, " float size_max;\n");
1976 shader_addline(buffer, "} ffp_point;\n");
1979 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && version->major < 3)
1981 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_diffuse;\n");
1982 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_specular;\n");
1983 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
1984 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
1987 if (version->major < 4)
1988 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
1990 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1992 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
1994 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
1996 else
1998 shader_addline(buffer, "layout(%s) in;\n", glsl_primitive_type_from_d3d(shader->u.gs.input_type));
1999 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
2000 glsl_primitive_type_from_d3d(shader->u.gs.output_type), shader->u.gs.vertices_out);
2001 shader_addline(buffer, "in vs_gs_iface { vec4 gs_in[%u]; } gs_in[];\n", shader->limits->packed_input);
2004 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2006 if (version->major < 3 || ps_args->vp_mode != vertexshader)
2008 shader_addline(buffer, "uniform struct\n{\n");
2009 shader_addline(buffer, " vec4 color;\n");
2010 shader_addline(buffer, " float density;\n");
2011 shader_addline(buffer, " float end;\n");
2012 shader_addline(buffer, " float scale;\n");
2013 shader_addline(buffer, "} ffp_fog;\n");
2015 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2017 if (glsl_is_color_reg_read(shader, 0))
2018 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
2019 if (glsl_is_color_reg_read(shader, 1))
2020 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
2021 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2022 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
2024 else
2026 if (glsl_is_color_reg_read(shader, 0))
2027 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2028 if (glsl_is_color_reg_read(shader, 1))
2029 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_specular;\n");
2030 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2031 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2032 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2036 if (version->major >= 3)
2038 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
2040 if (ps_args->vp_mode == vertexshader)
2041 declare_in_varying(gl_info, buffer, FALSE, "vec4 %s_link[%u];\n", prefix, in_count);
2042 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
2045 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
2047 if (!(map & 1))
2048 continue;
2050 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
2052 if (reg_maps->luminanceparams & (1u << i))
2054 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
2055 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
2056 extra_constants_needed++;
2059 extra_constants_needed++;
2062 if (ps_args->srgb_correction)
2064 shader_addline(buffer, "const vec4 srgb_const0 = ");
2065 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
2066 shader_addline(buffer, ";\n");
2067 shader_addline(buffer, "const vec4 srgb_const1 = ");
2068 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
2069 shader_addline(buffer, ";\n");
2071 if (reg_maps->vpos || reg_maps->usesdsy)
2073 ++extra_constants_needed;
2074 shader_addline(buffer, "uniform vec4 ycorrection;\n");
2075 shader_addline(buffer, "vec4 vpos;\n");
2078 if (ps_args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
2079 shader_addline(buffer, "uniform float alpha_test_ref;\n");
2081 if (!needs_legacy_glsl_syntax(gl_info))
2082 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
2084 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
2085 FIXME("Insufficient uniforms to run this shader.\n");
2088 /* Declare output register temporaries */
2089 if (shader->limits->packed_output)
2090 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2092 /* Declare temporary variables */
2093 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2095 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
2098 /* Declare loop registers aLx */
2099 if (version->major < 4)
2101 for (i = 0; i < reg_maps->loop_depth; ++i)
2103 shader_addline(buffer, "int aL%u;\n", i);
2104 shader_addline(buffer, "int tmpInt%u;\n", i);
2108 /* Temporary variables for matrix operations */
2109 shader_addline(buffer, "vec4 tmp0;\n");
2110 shader_addline(buffer, "vec4 tmp1;\n");
2112 if (!shader->load_local_constsF)
2114 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2116 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2117 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2118 shader_addline(buffer, ";\n");
2123 /*****************************************************************************
2124 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2126 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2127 ****************************************************************************/
2129 /* Prototypes */
2130 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2131 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2133 /** Used for opcode modifiers - They multiply the result by the specified amount */
2134 static const char * const shift_glsl_tab[] = {
2135 "", /* 0 (none) */
2136 "2.0 * ", /* 1 (x2) */
2137 "4.0 * ", /* 2 (x4) */
2138 "8.0 * ", /* 3 (x8) */
2139 "16.0 * ", /* 4 (x16) */
2140 "32.0 * ", /* 5 (x32) */
2141 "", /* 6 (x64) */
2142 "", /* 7 (x128) */
2143 "", /* 8 (d256) */
2144 "", /* 9 (d128) */
2145 "", /* 10 (d64) */
2146 "", /* 11 (d32) */
2147 "0.0625 * ", /* 12 (d16) */
2148 "0.125 * ", /* 13 (d8) */
2149 "0.25 * ", /* 14 (d4) */
2150 "0.5 * " /* 15 (d2) */
2153 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2154 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2155 const char *in_reg, const char *in_regswizzle, char *out_str)
2157 switch (src_modifier)
2159 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2160 case WINED3DSPSM_DW:
2161 case WINED3DSPSM_NONE:
2162 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2163 break;
2164 case WINED3DSPSM_NEG:
2165 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2166 break;
2167 case WINED3DSPSM_NOT:
2168 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2169 break;
2170 case WINED3DSPSM_BIAS:
2171 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2172 break;
2173 case WINED3DSPSM_BIASNEG:
2174 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2175 break;
2176 case WINED3DSPSM_SIGN:
2177 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2178 break;
2179 case WINED3DSPSM_SIGNNEG:
2180 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2181 break;
2182 case WINED3DSPSM_COMP:
2183 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2184 break;
2185 case WINED3DSPSM_X2:
2186 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2187 break;
2188 case WINED3DSPSM_X2NEG:
2189 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2190 break;
2191 case WINED3DSPSM_ABS:
2192 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2193 break;
2194 case WINED3DSPSM_ABSNEG:
2195 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2196 break;
2197 default:
2198 FIXME("Unhandled modifier %u\n", src_modifier);
2199 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2203 /** Writes the GLSL variable name that corresponds to the register that the
2204 * DX opcode parameter is trying to access */
2205 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2206 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2208 /* oPos, oFog and oPts in D3D */
2209 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2211 const struct wined3d_shader *shader = ins->ctx->shader;
2212 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2213 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2214 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2215 const char *prefix = shader_glsl_get_prefix(version->type);
2216 struct glsl_src_param rel_param0, rel_param1;
2217 char imm_str[4][17];
2219 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2220 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2221 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2222 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2223 *is_color = FALSE;
2225 switch (reg->type)
2227 case WINED3DSPR_TEMP:
2228 sprintf(register_name, "R%u", reg->idx[0].offset);
2229 break;
2231 case WINED3DSPR_INPUT:
2232 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2234 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2236 if (reg->idx[0].rel_addr)
2237 FIXME("VS3+ input registers relative addressing.\n");
2238 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2239 *is_color = TRUE;
2240 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2241 break;
2244 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2246 if (reg->idx[0].rel_addr)
2248 if (reg->idx[1].rel_addr)
2249 sprintf(register_name, "gs_in[%s + %u]%s[%s + %u]",
2250 rel_param0.param_str, reg->idx[0].offset,
2251 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2252 rel_param1.param_str, reg->idx[1].offset);
2253 else
2254 sprintf(register_name, "gs_in[%s + %u]%s[%u]",
2255 rel_param0.param_str, reg->idx[0].offset,
2256 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2257 reg->idx[1].offset);
2259 else if (reg->idx[1].rel_addr)
2260 sprintf(register_name, "gs_in[%u]%s[%s + %u]", reg->idx[0].offset,
2261 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2262 rel_param1.param_str, reg->idx[1].offset);
2263 else
2264 sprintf(register_name, "gs_in[%u]%s[%u]", reg->idx[0].offset,
2265 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2266 reg->idx[1].offset);
2267 break;
2270 /* pixel shaders >= 3.0 */
2271 if (version->major >= 3)
2273 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2274 unsigned int in_count = vec4_varyings(version->major, gl_info);
2276 if (reg->idx[0].rel_addr)
2278 /* Removing a + 0 would be an obvious optimization, but
2279 * OS X doesn't see the NOP operation there. */
2280 if (idx)
2282 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2283 && shader->u.ps.declared_in_count > in_count)
2285 sprintf(register_name,
2286 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2287 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2288 prefix, rel_param0.param_str, idx);
2290 else
2292 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2295 else
2297 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2298 && shader->u.ps.declared_in_count > in_count)
2300 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2301 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2302 prefix, rel_param0.param_str);
2304 else
2306 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2310 else
2312 if (idx == in_count) sprintf(register_name, "gl_Color");
2313 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2314 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2317 else
2319 if (!reg->idx[0].offset)
2320 strcpy(register_name, "ffp_varying_diffuse");
2321 else
2322 strcpy(register_name, "ffp_varying_specular");
2323 break;
2325 break;
2327 case WINED3DSPR_CONST:
2329 /* Relative addressing */
2330 if (reg->idx[0].rel_addr)
2332 if (wined3d_settings.check_float_constants)
2333 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2334 rel_param0.param_str, reg->idx[0].offset,
2335 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2336 prefix, rel_param0.param_str, reg->idx[0].offset);
2337 else if (reg->idx[0].offset)
2338 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2339 else
2340 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2342 else
2344 if (shader_constant_is_local(shader, reg->idx[0].offset))
2345 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2346 else
2347 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2350 break;
2352 case WINED3DSPR_CONSTINT:
2353 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2354 break;
2356 case WINED3DSPR_CONSTBOOL:
2357 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2358 break;
2360 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2361 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2362 sprintf(register_name, "T%u", reg->idx[0].offset);
2363 else
2364 sprintf(register_name, "A%u", reg->idx[0].offset);
2365 break;
2367 case WINED3DSPR_LOOP:
2368 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
2369 break;
2371 case WINED3DSPR_SAMPLER:
2372 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2373 break;
2375 case WINED3DSPR_COLOROUT:
2376 if (reg->idx[0].offset >= gl_info->limits.buffers)
2377 WARN("Write to render target %u, only %d supported.\n",
2378 reg->idx[0].offset, gl_info->limits.buffers);
2380 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2381 break;
2383 case WINED3DSPR_RASTOUT:
2384 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2385 break;
2387 case WINED3DSPR_DEPTHOUT:
2388 sprintf(register_name, "gl_FragDepth");
2389 break;
2391 case WINED3DSPR_ATTROUT:
2392 if (!reg->idx[0].offset)
2393 sprintf(register_name, "%s_out[8]", prefix);
2394 else
2395 sprintf(register_name, "%s_out[9]", prefix);
2396 break;
2398 case WINED3DSPR_TEXCRDOUT:
2399 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2400 if (reg->idx[0].rel_addr)
2401 FIXME("VS3 output registers relative addressing.\n");
2402 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2403 break;
2405 case WINED3DSPR_MISCTYPE:
2406 if (!reg->idx[0].offset)
2408 /* vPos */
2409 sprintf(register_name, "vpos");
2411 else if (reg->idx[0].offset == 1)
2413 /* Note that gl_FrontFacing is a bool, while vFace is
2414 * a float for which the sign determines front/back */
2415 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2417 else
2419 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2420 sprintf(register_name, "unrecognized_register");
2422 break;
2424 case WINED3DSPR_IMMCONST:
2425 switch (reg->immconst_type)
2427 case WINED3D_IMMCONST_SCALAR:
2428 switch (reg->data_type)
2430 case WINED3D_DATA_FLOAT:
2431 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
2432 break;
2433 case WINED3D_DATA_INT:
2434 sprintf(register_name, "%#x", reg->immconst_data[0]);
2435 break;
2436 case WINED3D_DATA_RESOURCE:
2437 case WINED3D_DATA_SAMPLER:
2438 case WINED3D_DATA_UINT:
2439 sprintf(register_name, "%#xu", reg->immconst_data[0]);
2440 break;
2441 default:
2442 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2443 break;
2445 break;
2447 case WINED3D_IMMCONST_VEC4:
2448 switch (reg->data_type)
2450 case WINED3D_DATA_FLOAT:
2451 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
2452 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
2453 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
2454 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
2455 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2456 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2457 break;
2458 case WINED3D_DATA_INT:
2459 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2460 reg->immconst_data[0], reg->immconst_data[1],
2461 reg->immconst_data[2], reg->immconst_data[3]);
2462 break;
2463 case WINED3D_DATA_RESOURCE:
2464 case WINED3D_DATA_SAMPLER:
2465 case WINED3D_DATA_UINT:
2466 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2467 reg->immconst_data[0], reg->immconst_data[1],
2468 reg->immconst_data[2], reg->immconst_data[3]);
2469 break;
2470 default:
2471 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2472 break;
2474 break;
2476 default:
2477 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2478 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2480 break;
2482 case WINED3DSPR_CONSTBUFFER:
2483 if (reg->idx[1].rel_addr)
2484 sprintf(register_name, "%s_cb%u[%s + %u]",
2485 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2486 else
2487 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2488 break;
2490 case WINED3DSPR_IMMCONSTBUFFER:
2491 if (reg->idx[0].rel_addr)
2492 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2493 else
2494 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2495 break;
2497 case WINED3DSPR_PRIMID:
2498 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2499 break;
2501 default:
2502 FIXME("Unhandled register type %#x.\n", reg->type);
2503 sprintf(register_name, "unrecognized_register");
2504 break;
2508 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2510 *str++ = '.';
2511 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2512 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2513 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2514 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2515 *str = '\0';
2518 /* Get the GLSL write mask for the destination register */
2519 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2521 DWORD mask = param->write_mask;
2523 if (shader_is_scalar(&param->reg))
2525 mask = WINED3DSP_WRITEMASK_0;
2526 *write_mask = '\0';
2528 else
2530 shader_glsl_write_mask_to_str(mask, write_mask);
2533 return mask;
2536 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
2537 unsigned int size = 0;
2539 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2540 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2541 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2542 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2544 return size;
2547 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2549 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2550 * but addressed as "rgba". To fix this we need to swap the register's x
2551 * and z components. */
2552 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2554 *str++ = '.';
2555 /* swizzle bits fields: wwzzyyxx */
2556 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
2557 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
2558 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
2559 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
2560 *str = '\0';
2563 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2564 BOOL fixup, DWORD mask, char *swizzle_str)
2566 if (shader_is_scalar(&param->reg))
2567 *swizzle_str = '\0';
2568 else
2569 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2572 /* From a given parameter token, generate the corresponding GLSL string.
2573 * Also, return the actual register name and swizzle in case the
2574 * caller needs this information as well. */
2575 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2576 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
2578 BOOL is_color = FALSE;
2579 char swizzle_str[6];
2581 glsl_src->reg_name[0] = '\0';
2582 glsl_src->param_str[0] = '\0';
2583 swizzle_str[0] = '\0';
2585 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
2586 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
2588 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
2590 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
2592 else
2594 char reg_name[200];
2596 switch (wined3d_src->reg.data_type)
2598 case WINED3D_DATA_FLOAT:
2599 sprintf(reg_name, "%s", glsl_src->reg_name);
2600 break;
2601 case WINED3D_DATA_INT:
2602 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
2603 break;
2604 case WINED3D_DATA_RESOURCE:
2605 case WINED3D_DATA_SAMPLER:
2606 case WINED3D_DATA_UINT:
2607 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
2608 break;
2609 default:
2610 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
2611 sprintf(reg_name, "%s", glsl_src->reg_name);
2612 break;
2615 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
2619 /* From a given parameter token, generate the corresponding GLSL string.
2620 * Also, return the actual register name and swizzle in case the
2621 * caller needs this information as well. */
2622 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
2623 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
2625 BOOL is_color = FALSE;
2627 glsl_dst->mask_str[0] = '\0';
2628 glsl_dst->reg_name[0] = '\0';
2630 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
2631 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
2634 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2635 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
2636 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
2637 enum wined3d_data_type data_type)
2639 struct glsl_dst_param glsl_dst;
2640 DWORD mask;
2642 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
2644 switch (data_type)
2646 case WINED3D_DATA_FLOAT:
2647 shader_addline(buffer, "%s%s = %s(",
2648 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2649 break;
2650 case WINED3D_DATA_INT:
2651 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
2652 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2653 break;
2654 case WINED3D_DATA_RESOURCE:
2655 case WINED3D_DATA_SAMPLER:
2656 case WINED3D_DATA_UINT:
2657 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
2658 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2659 break;
2660 default:
2661 FIXME("Unhandled data type %#x.\n", data_type);
2662 shader_addline(buffer, "%s%s = %s(",
2663 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2664 break;
2668 return mask;
2671 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2672 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
2674 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2677 /** Process GLSL instruction modifiers */
2678 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
2680 struct glsl_dst_param dst_param;
2681 DWORD modifiers;
2683 if (!ins->dst_count) return;
2685 modifiers = ins->dst[0].modifiers;
2686 if (!modifiers) return;
2688 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2690 if (modifiers & WINED3DSPDM_SATURATE)
2692 /* _SAT means to clamp the value of the register to between 0 and 1 */
2693 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
2694 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
2697 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
2699 FIXME("_centroid modifier not handled\n");
2702 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
2704 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2708 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
2710 switch (op)
2712 case WINED3D_SHADER_REL_OP_GT: return ">";
2713 case WINED3D_SHADER_REL_OP_EQ: return "==";
2714 case WINED3D_SHADER_REL_OP_GE: return ">=";
2715 case WINED3D_SHADER_REL_OP_LT: return "<";
2716 case WINED3D_SHADER_REL_OP_NE: return "!=";
2717 case WINED3D_SHADER_REL_OP_LE: return "<=";
2718 default:
2719 FIXME("Unrecognized operator %#x.\n", op);
2720 return "(\?\?)";
2724 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
2725 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
2727 static const struct
2729 unsigned int coord_size;
2730 unsigned int offset_size;
2731 const char *type_part;
2733 resource_types[] =
2735 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
2736 {1, 0, ""}, /* WINED3D_SHADER_RESOURCE_BUFFER */
2737 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
2738 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
2739 {2, 0, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
2740 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
2741 {3, 0, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
2742 {2, 1, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
2743 {3, 2, "2DArray"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
2744 {3, 0, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
2746 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2747 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
2748 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2749 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
2750 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
2751 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
2752 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
2753 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2754 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
2755 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
2756 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
2757 const char *base = "texture", *type_part = "", *suffix = "";
2758 unsigned int coord_size;
2760 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
2762 if (resource_type >= ARRAY_SIZE(resource_types))
2764 ERR("Unexpected resource type %#x.\n", resource_type);
2765 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
2768 /* Note that there's no such thing as a projected cube texture. */
2769 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
2770 projected = FALSE;
2772 if (needs_legacy_glsl_syntax(gl_info))
2774 if (shadow)
2775 base = "shadow";
2777 type_part = resource_types[resource_type].type_part;
2778 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
2779 type_part = "2DRect";
2780 if (!type_part[0])
2781 FIXME("Unhandled resource type %#x.\n", resource_type);
2783 if (!lod && grad && !gl_info->supported[EXT_GPU_SHADER4])
2785 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2786 suffix = "ARB";
2787 else
2788 FIXME("Unsupported grad function.\n");
2792 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
2794 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
2795 if (flags & ~texel_fetch_flags)
2796 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
2798 base = "texelFetch";
2799 type_part = "";
2802 sample_function->offset_size = offset ? resource_types[resource_type].offset_size : 0;
2803 if (offset && !sample_function->offset_size)
2805 FIXME("Offset not supported for resource type %#x.\n", resource_type);
2806 offset = FALSE;
2809 sample_function->name = string_buffer_get(priv->string_buffers);
2810 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
2811 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
2813 coord_size = resource_types[resource_type].coord_size;
2814 if (shadow)
2815 ++coord_size;
2816 sample_function->coord_mask = (1u << coord_size) - 1;
2817 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
2820 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
2821 struct glsl_sample_function *sample_function)
2823 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2825 string_buffer_release(priv->string_buffers, sample_function->name);
2828 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2829 BOOL sign_fixup, enum fixup_channel_source channel_source)
2831 switch(channel_source)
2833 case CHANNEL_SOURCE_ZERO:
2834 strcat(arguments, "0.0");
2835 break;
2837 case CHANNEL_SOURCE_ONE:
2838 strcat(arguments, "1.0");
2839 break;
2841 case CHANNEL_SOURCE_X:
2842 strcat(arguments, reg_name);
2843 strcat(arguments, ".x");
2844 break;
2846 case CHANNEL_SOURCE_Y:
2847 strcat(arguments, reg_name);
2848 strcat(arguments, ".y");
2849 break;
2851 case CHANNEL_SOURCE_Z:
2852 strcat(arguments, reg_name);
2853 strcat(arguments, ".z");
2854 break;
2856 case CHANNEL_SOURCE_W:
2857 strcat(arguments, reg_name);
2858 strcat(arguments, ".w");
2859 break;
2861 default:
2862 FIXME("Unhandled channel source %#x\n", channel_source);
2863 strcat(arguments, "undefined");
2864 break;
2867 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2870 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
2871 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2873 unsigned int mask_size, remaining;
2874 DWORD fixup_mask = 0;
2875 char arguments[256];
2876 char mask_str[6];
2878 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2879 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2880 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2881 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2882 if (!(mask &= fixup_mask))
2883 return;
2885 if (is_complex_fixup(fixup))
2887 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2888 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2889 return;
2892 shader_glsl_write_mask_to_str(mask, mask_str);
2893 mask_size = shader_glsl_get_write_mask_size(mask);
2895 arguments[0] = '\0';
2896 remaining = mask_size;
2897 if (mask & WINED3DSP_WRITEMASK_0)
2899 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2900 if (--remaining) strcat(arguments, ", ");
2902 if (mask & WINED3DSP_WRITEMASK_1)
2904 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2905 if (--remaining) strcat(arguments, ", ");
2907 if (mask & WINED3DSP_WRITEMASK_2)
2909 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2910 if (--remaining) strcat(arguments, ", ");
2912 if (mask & WINED3DSP_WRITEMASK_3)
2914 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2915 if (--remaining) strcat(arguments, ", ");
2918 if (mask_size > 1)
2919 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2920 else
2921 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2924 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2926 char reg_name[256];
2927 BOOL is_color;
2929 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2930 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2933 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2934 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
2935 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
2936 const char *coord_reg_fmt, ...)
2938 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2939 char dst_swizzle[6];
2940 struct color_fixup_desc fixup;
2941 BOOL np2_fixup = FALSE;
2942 va_list args;
2943 int ret;
2945 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2947 /* If ARB_texture_swizzle is supported we don't need to do anything here.
2948 * We actually rely on it for vertex shaders and SM4+. */
2949 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
2951 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2952 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
2954 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
2955 np2_fixup = TRUE;
2957 else
2959 fixup = COLOR_FIXUP_IDENTITY;
2962 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2964 if (sample_function->output_single_component)
2965 shader_addline(ins->ctx->buffer, "vec4(");
2967 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2968 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
2970 for (;;)
2972 va_start(args, coord_reg_fmt);
2973 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2974 va_end(args);
2975 if (!ret)
2976 break;
2977 if (!string_buffer_resize(ins->ctx->buffer, ret))
2978 break;
2981 if (np2_fixup)
2983 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2984 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
2986 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
2988 case 1:
2989 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2990 idx >> 1, (idx % 2) ? "z" : "x");
2991 break;
2992 case 2:
2993 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2994 idx >> 1, (idx % 2) ? "zw" : "xy");
2995 break;
2996 case 3:
2997 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
2998 idx >> 1, (idx % 2) ? "zw" : "xy");
2999 break;
3000 case 4:
3001 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3002 idx >> 1, (idx % 2) ? "zw" : "xy");
3003 break;
3006 if (dx && dy)
3007 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3008 else if (bias)
3009 shader_addline(ins->ctx->buffer, ", %s", bias);
3010 if (sample_function->offset_size)
3012 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3013 shader_addline(ins->ctx->buffer, ", ");
3014 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3016 shader_addline(ins->ctx->buffer, ")");
3018 if (sample_function->output_single_component)
3019 shader_addline(ins->ctx->buffer, ")");
3021 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3023 if (!is_identity_fixup(fixup))
3024 shader_glsl_color_correction(ins, fixup);
3027 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer)
3029 /* Write the final position.
3031 * OpenGL coordinates specify the center of the pixel while D3D coords
3032 * specify the corner. The offsets are stored in z and w in
3033 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3034 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3035 * a MAD. */
3036 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3037 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3039 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3040 * in utils.c
3042 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3043 * shaders are run before the homogeneous divide, so we have to take the w
3044 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3045 * z = z * 2 - w. */
3046 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3049 /*****************************************************************************
3050 * Begin processing individual instruction opcodes
3051 ****************************************************************************/
3053 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3055 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3056 struct glsl_src_param src0_param;
3057 struct glsl_src_param src1_param;
3058 DWORD write_mask;
3059 const char *op;
3061 /* Determine the GLSL operator to use based on the opcode */
3062 switch (ins->handler_idx)
3064 case WINED3DSIH_ADD: op = "+"; break;
3065 case WINED3DSIH_AND: op = "&"; break;
3066 case WINED3DSIH_DIV: op = "/"; break;
3067 case WINED3DSIH_IADD: op = "+"; break;
3068 case WINED3DSIH_ISHL: op = "<<"; break;
3069 case WINED3DSIH_MUL: op = "*"; break;
3070 case WINED3DSIH_OR: op = "|"; break;
3071 case WINED3DSIH_SUB: op = "-"; break;
3072 case WINED3DSIH_USHR: op = ">>"; break;
3073 case WINED3DSIH_XOR: op = "^"; break;
3074 default:
3075 op = "<unhandled operator>";
3076 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3077 break;
3080 write_mask = shader_glsl_append_dst(buffer, ins);
3081 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3082 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3083 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3086 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3088 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3089 struct glsl_src_param src0_param;
3090 struct glsl_src_param src1_param;
3091 unsigned int mask_size;
3092 DWORD write_mask;
3093 const char *op;
3095 write_mask = shader_glsl_append_dst(buffer, ins);
3096 mask_size = shader_glsl_get_write_mask_size(write_mask);
3097 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3098 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3100 if (mask_size > 1)
3102 switch (ins->handler_idx)
3104 case WINED3DSIH_EQ: op = "equal"; break;
3105 case WINED3DSIH_IEQ: op = "equal"; break;
3106 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3107 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3108 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3109 case WINED3DSIH_LT: op = "lessThan"; break;
3110 case WINED3DSIH_ILT: op = "lessThan"; break;
3111 case WINED3DSIH_ULT: op = "lessThan"; break;
3112 case WINED3DSIH_NE: op = "notEqual"; break;
3113 case WINED3DSIH_INE: op = "notEqual"; break;
3114 default:
3115 op = "<unhandled operator>";
3116 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3117 break;
3120 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3121 mask_size, op, src0_param.param_str, src1_param.param_str);
3123 else
3125 switch (ins->handler_idx)
3127 case WINED3DSIH_EQ: op = "=="; break;
3128 case WINED3DSIH_IEQ: op = "=="; break;
3129 case WINED3DSIH_GE: op = ">="; break;
3130 case WINED3DSIH_IGE: op = ">="; break;
3131 case WINED3DSIH_UGE: op = ">="; break;
3132 case WINED3DSIH_LT: op = "<"; break;
3133 case WINED3DSIH_ILT: op = "<"; break;
3134 case WINED3DSIH_ULT: op = "<"; break;
3135 case WINED3DSIH_NE: op = "!="; break;
3136 case WINED3DSIH_INE: op = "!="; break;
3137 default:
3138 op = "<unhandled operator>";
3139 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3140 break;
3143 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3144 src0_param.param_str, op, src1_param.param_str);
3148 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3150 struct glsl_src_param src_param;
3151 DWORD write_mask;
3152 const char *op;
3154 switch (ins->handler_idx)
3156 case WINED3DSIH_INEG: op = "-"; break;
3157 case WINED3DSIH_NOT: op = "~"; break;
3158 default:
3159 op = "<unhandled operator>";
3160 ERR("Unhandled opcode %s.\n",
3161 debug_d3dshaderinstructionhandler(ins->handler_idx));
3162 break;
3165 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3166 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3167 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3170 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
3172 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3173 struct glsl_src_param src0_param;
3174 struct glsl_src_param src1_param;
3175 DWORD write_mask;
3177 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
3178 * not, we can emulate it. */
3179 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3180 FIXME("64-bit integer multiplies not implemented.\n");
3182 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3184 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3185 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3186 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3188 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3189 src0_param.param_str, src1_param.param_str);
3193 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3195 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3196 struct glsl_src_param src0_param, src1_param;
3197 DWORD write_mask;
3199 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3201 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3203 char dst_mask[6];
3205 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3206 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3207 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3208 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3209 dst_mask, src0_param.param_str, src1_param.param_str);
3211 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3212 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3213 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3214 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3216 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3217 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3219 else
3221 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3222 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3223 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3224 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3227 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3229 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3230 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3231 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3232 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3236 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3237 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3239 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3240 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3241 struct glsl_src_param src0_param;
3242 DWORD write_mask;
3244 write_mask = shader_glsl_append_dst(buffer, ins);
3245 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3247 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3248 * shader versions WINED3DSIO_MOVA is used for this. */
3249 if (ins->ctx->reg_maps->shader_version.major == 1
3250 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3251 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3253 /* This is a simple floor() */
3254 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3255 if (mask_size > 1) {
3256 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3257 } else {
3258 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3261 else if(ins->handler_idx == WINED3DSIH_MOVA)
3263 /* We need to *round* to the nearest int here. */
3264 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3266 if (gl_info->supported[EXT_GPU_SHADER4])
3268 if (mask_size > 1)
3269 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3270 else
3271 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3273 else
3275 if (mask_size > 1)
3276 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3277 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3278 else
3279 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3280 src0_param.param_str, src0_param.param_str);
3283 else
3285 shader_addline(buffer, "%s);\n", src0_param.param_str);
3289 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3290 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3292 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3293 struct glsl_src_param src0_param;
3294 struct glsl_src_param src1_param;
3295 DWORD dst_write_mask, src_write_mask;
3296 unsigned int dst_size;
3298 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3299 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3301 /* dp4 works on vec4, dp3 on vec3, etc. */
3302 if (ins->handler_idx == WINED3DSIH_DP4)
3303 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3304 else if (ins->handler_idx == WINED3DSIH_DP3)
3305 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3306 else
3307 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3309 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3310 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3312 if (dst_size > 1) {
3313 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3314 } else {
3315 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3319 /* Note that this instruction has some restrictions. The destination write mask
3320 * can't contain the w component, and the source swizzles have to be .xyzw */
3321 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3323 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3324 struct glsl_src_param src0_param;
3325 struct glsl_src_param src1_param;
3326 char dst_mask[6];
3328 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3329 shader_glsl_append_dst(ins->ctx->buffer, ins);
3330 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3331 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3332 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3335 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3337 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3340 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3341 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3342 * GLSL uses the value as-is. */
3343 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3345 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3346 struct glsl_src_param src0_param;
3347 struct glsl_src_param src1_param;
3348 DWORD dst_write_mask;
3349 unsigned int dst_size;
3351 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3352 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3354 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3355 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3357 if (dst_size > 1)
3359 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3360 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3362 else
3364 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3365 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3369 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3370 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3372 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3373 struct glsl_src_param src_param;
3374 const char *instruction;
3375 DWORD write_mask;
3376 unsigned i;
3378 /* Determine the GLSL function to use based on the opcode */
3379 /* TODO: Possibly make this a table for faster lookups */
3380 switch (ins->handler_idx)
3382 case WINED3DSIH_ABS: instruction = "abs"; break;
3383 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3384 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3385 case WINED3DSIH_FRC: instruction = "fract"; break;
3386 case WINED3DSIH_IMAX: instruction = "max"; break;
3387 case WINED3DSIH_IMIN: instruction = "min"; break;
3388 case WINED3DSIH_MAX: instruction = "max"; break;
3389 case WINED3DSIH_MIN: instruction = "min"; break;
3390 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
3391 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3392 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
3393 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
3394 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3395 default: instruction = "";
3396 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3397 break;
3400 write_mask = shader_glsl_append_dst(buffer, ins);
3402 shader_addline(buffer, "%s(", instruction);
3404 if (ins->src_count)
3406 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3407 shader_addline(buffer, "%s", src_param.param_str);
3408 for (i = 1; i < ins->src_count; ++i)
3410 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3411 shader_addline(buffer, ", %s", src_param.param_str);
3415 shader_addline(buffer, "));\n");
3418 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3420 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3422 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3423 struct glsl_src_param src_param;
3424 unsigned int mask_size;
3425 DWORD write_mask;
3426 char dst_mask[6];
3428 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3429 mask_size = shader_glsl_get_write_mask_size(write_mask);
3430 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3432 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3433 src_param.param_str, src_param.param_str);
3434 shader_glsl_append_dst(buffer, ins);
3436 if (mask_size > 1)
3438 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3439 mask_size, src_param.param_str);
3441 else
3443 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3444 src_param.param_str);
3448 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3450 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3451 ins->ctx->reg_maps->shader_version.minor);
3452 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3453 struct glsl_src_param src0_param;
3454 const char *prefix, *suffix;
3455 unsigned int dst_size;
3456 DWORD dst_write_mask;
3458 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3459 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3461 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
3462 dst_write_mask = WINED3DSP_WRITEMASK_3;
3464 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
3466 switch (ins->handler_idx)
3468 case WINED3DSIH_EXP:
3469 case WINED3DSIH_EXPP:
3470 prefix = "exp2(";
3471 suffix = ")";
3472 break;
3474 case WINED3DSIH_LOG:
3475 case WINED3DSIH_LOGP:
3476 prefix = "log2(abs(";
3477 suffix = "))";
3478 break;
3480 case WINED3DSIH_RCP:
3481 prefix = "1.0 / ";
3482 suffix = "";
3483 break;
3485 case WINED3DSIH_RSQ:
3486 prefix = "inversesqrt(abs(";
3487 suffix = "))";
3488 break;
3490 default:
3491 prefix = "";
3492 suffix = "";
3493 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3494 break;
3497 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
3498 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3499 else
3500 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3503 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3504 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3505 * dst.x = 2^(floor(src))
3506 * dst.y = src - floor(src)
3507 * dst.z = 2^src (partial precision is allowed, but optional)
3508 * dst.w = 1.0;
3509 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3510 * dst = 2^src; (partial precision is allowed, but optional)
3512 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
3514 if (ins->ctx->reg_maps->shader_version.major < 2)
3516 struct glsl_src_param src_param;
3517 char dst_mask[6];
3519 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
3521 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
3522 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
3523 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
3524 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
3526 shader_glsl_append_dst(ins->ctx->buffer, ins);
3527 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3528 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
3529 return;
3532 shader_glsl_scalar_op(ins);
3535 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
3536 const char *vector_constructor, const char *scalar_constructor)
3538 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3539 struct glsl_src_param src_param;
3540 unsigned int mask_size;
3541 DWORD write_mask;
3543 write_mask = shader_glsl_append_dst(buffer, ins);
3544 mask_size = shader_glsl_get_write_mask_size(write_mask);
3545 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3547 if (mask_size > 1)
3548 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
3549 else
3550 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
3553 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
3555 shader_glsl_cast(ins, "ivec", "int");
3558 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
3560 shader_glsl_cast(ins, "uvec", "uint");
3563 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
3565 shader_glsl_cast(ins, "vec", "float");
3568 /** Process signed comparison opcodes in GLSL. */
3569 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
3571 struct glsl_src_param src0_param;
3572 struct glsl_src_param src1_param;
3573 DWORD write_mask;
3574 unsigned int mask_size;
3576 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3577 mask_size = shader_glsl_get_write_mask_size(write_mask);
3578 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3579 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3581 if (mask_size > 1) {
3582 const char *compare;
3584 switch(ins->handler_idx)
3586 case WINED3DSIH_SLT: compare = "lessThan"; break;
3587 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
3588 default: compare = "";
3589 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3592 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
3593 src0_param.param_str, src1_param.param_str);
3594 } else {
3595 switch(ins->handler_idx)
3597 case WINED3DSIH_SLT:
3598 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3599 * to return 0.0 but step returns 1.0 because step is not < x
3600 * An alternative is a bvec compare padded with an unused second component.
3601 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3602 * issue. Playing with not() is not possible either because not() does not accept
3603 * a scalar.
3605 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
3606 src0_param.param_str, src1_param.param_str);
3607 break;
3608 case WINED3DSIH_SGE:
3609 /* Here we can use the step() function and safe a conditional */
3610 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
3611 break;
3612 default:
3613 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3619 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
3621 const char *condition_prefix, *condition_suffix;
3622 struct wined3d_shader_dst_param dst;
3623 struct glsl_src_param src0_param;
3624 struct glsl_src_param src1_param;
3625 struct glsl_src_param src2_param;
3626 BOOL temp_destination = FALSE;
3627 DWORD cmp_channel = 0;
3628 unsigned int i, j;
3629 char mask_char[6];
3630 DWORD write_mask;
3632 switch (ins->handler_idx)
3634 case WINED3DSIH_CMP:
3635 condition_prefix = "";
3636 condition_suffix = " >= 0.0";
3637 break;
3639 case WINED3DSIH_CND:
3640 condition_prefix = "";
3641 condition_suffix = " > 0.5";
3642 break;
3644 case WINED3DSIH_MOVC:
3645 condition_prefix = "bool(";
3646 condition_suffix = ")";
3647 break;
3649 default:
3650 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3651 condition_prefix = "<unhandled prefix>";
3652 condition_suffix = "<unhandled suffix>";
3653 break;
3656 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
3658 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3659 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3660 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3661 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3663 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3664 condition_prefix, src0_param.param_str, condition_suffix,
3665 src1_param.param_str, src2_param.param_str);
3666 return;
3669 dst = ins->dst[0];
3671 /* Splitting the instruction up in multiple lines imposes a problem:
3672 * The first lines may overwrite source parameters of the following lines.
3673 * Deal with that by using a temporary destination register if needed. */
3674 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
3675 && ins->src[0].reg.type == dst.reg.type)
3676 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
3677 && ins->src[1].reg.type == dst.reg.type)
3678 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
3679 && ins->src[2].reg.type == dst.reg.type))
3680 temp_destination = TRUE;
3682 /* Cycle through all source0 channels. */
3683 for (i = 0; i < 4; ++i)
3685 write_mask = 0;
3686 /* Find the destination channels which use the current source0 channel. */
3687 for (j = 0; j < 4; ++j)
3689 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
3691 write_mask |= WINED3DSP_WRITEMASK_0 << j;
3692 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
3695 dst.write_mask = ins->dst[0].write_mask & write_mask;
3697 if (temp_destination)
3699 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
3700 continue;
3701 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
3703 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
3704 continue;
3706 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
3707 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3708 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3710 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3711 condition_prefix, src0_param.param_str, condition_suffix,
3712 src1_param.param_str, src2_param.param_str);
3715 if (temp_destination)
3717 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3718 shader_glsl_append_dst(ins->ctx->buffer, ins);
3719 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3723 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3724 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3725 * the compare is done per component of src0. */
3726 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3728 struct glsl_src_param src0_param;
3729 struct glsl_src_param src1_param;
3730 struct glsl_src_param src2_param;
3731 DWORD write_mask;
3732 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3733 ins->ctx->reg_maps->shader_version.minor);
3735 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3737 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3738 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3739 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3740 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3742 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
3743 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3744 else
3745 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3746 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3747 return;
3750 shader_glsl_conditional_move(ins);
3753 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3754 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3756 struct glsl_src_param src0_param;
3757 struct glsl_src_param src1_param;
3758 struct glsl_src_param src2_param;
3759 DWORD write_mask;
3761 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3762 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3763 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3764 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3765 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3766 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3769 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3770 Vertex shaders to GLSL codes */
3771 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3773 int i;
3774 int nComponents = 0;
3775 struct wined3d_shader_dst_param tmp_dst = {{0}};
3776 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3777 struct wined3d_shader_instruction tmp_ins;
3779 memset(&tmp_ins, 0, sizeof(tmp_ins));
3781 /* Set constants for the temporary argument */
3782 tmp_ins.ctx = ins->ctx;
3783 tmp_ins.dst_count = 1;
3784 tmp_ins.dst = &tmp_dst;
3785 tmp_ins.src_count = 2;
3786 tmp_ins.src = tmp_src;
3788 switch(ins->handler_idx)
3790 case WINED3DSIH_M4x4:
3791 nComponents = 4;
3792 tmp_ins.handler_idx = WINED3DSIH_DP4;
3793 break;
3794 case WINED3DSIH_M4x3:
3795 nComponents = 3;
3796 tmp_ins.handler_idx = WINED3DSIH_DP4;
3797 break;
3798 case WINED3DSIH_M3x4:
3799 nComponents = 4;
3800 tmp_ins.handler_idx = WINED3DSIH_DP3;
3801 break;
3802 case WINED3DSIH_M3x3:
3803 nComponents = 3;
3804 tmp_ins.handler_idx = WINED3DSIH_DP3;
3805 break;
3806 case WINED3DSIH_M3x2:
3807 nComponents = 2;
3808 tmp_ins.handler_idx = WINED3DSIH_DP3;
3809 break;
3810 default:
3811 break;
3814 tmp_dst = ins->dst[0];
3815 tmp_src[0] = ins->src[0];
3816 tmp_src[1] = ins->src[1];
3817 for (i = 0; i < nComponents; ++i)
3819 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3820 shader_glsl_dot(&tmp_ins);
3821 ++tmp_src[1].reg.idx[0].offset;
3826 The LRP instruction performs a component-wise linear interpolation
3827 between the second and third operands using the first operand as the
3828 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3829 This is equivalent to mix(src2, src1, src0);
3831 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3833 struct glsl_src_param src0_param;
3834 struct glsl_src_param src1_param;
3835 struct glsl_src_param src2_param;
3836 DWORD write_mask;
3838 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3840 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3841 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3842 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3844 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3845 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3848 /** Process the WINED3DSIO_LIT instruction in GLSL:
3849 * dst.x = dst.w = 1.0
3850 * dst.y = (src0.x > 0) ? src0.x
3851 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3852 * where src.w is clamped at +- 128
3854 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3856 struct glsl_src_param src0_param;
3857 struct glsl_src_param src1_param;
3858 struct glsl_src_param src3_param;
3859 char dst_mask[6];
3861 shader_glsl_append_dst(ins->ctx->buffer, ins);
3862 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3864 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3865 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3866 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3868 /* The sdk specifies the instruction like this
3869 * dst.x = 1.0;
3870 * if(src.x > 0.0) dst.y = src.x
3871 * else dst.y = 0.0.
3872 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3873 * else dst.z = 0.0;
3874 * dst.w = 1.0;
3875 * (where power = src.w clamped between -128 and 128)
3877 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3878 * dst.x = 1.0 ... No further explanation needed
3879 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3880 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3881 * dst.w = 1.0. ... Nothing fancy.
3883 * So we still have one conditional in there. So do this:
3884 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3886 * 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),
3887 * 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.
3888 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3890 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3891 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3892 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3894 shader_addline(ins->ctx->buffer,
3895 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3896 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3897 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3898 src0_param.param_str, src3_param.param_str, dst_mask);
3901 /** Process the WINED3DSIO_DST instruction in GLSL:
3902 * dst.x = 1.0
3903 * dst.y = src0.x * src0.y
3904 * dst.z = src0.z
3905 * dst.w = src1.w
3907 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3909 struct glsl_src_param src0y_param;
3910 struct glsl_src_param src0z_param;
3911 struct glsl_src_param src1y_param;
3912 struct glsl_src_param src1w_param;
3913 char dst_mask[6];
3915 shader_glsl_append_dst(ins->ctx->buffer, ins);
3916 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3918 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3919 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3920 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3921 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3923 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3924 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3927 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3928 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3929 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3931 * dst.x = cos(src0.?)
3932 * dst.y = sin(src0.?)
3933 * dst.z = dst.z
3934 * dst.w = dst.w
3936 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3938 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3939 struct glsl_src_param src0_param;
3940 DWORD write_mask;
3942 if (ins->ctx->reg_maps->shader_version.major < 4)
3944 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3946 write_mask = shader_glsl_append_dst(buffer, ins);
3947 switch (write_mask)
3949 case WINED3DSP_WRITEMASK_0:
3950 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3951 break;
3953 case WINED3DSP_WRITEMASK_1:
3954 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3955 break;
3957 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3958 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3959 src0_param.param_str, src0_param.param_str);
3960 break;
3962 default:
3963 ERR("Write mask should be .x, .y or .xy\n");
3964 break;
3967 return;
3970 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3973 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3975 char dst_mask[6];
3977 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3978 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3979 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3981 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3982 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3983 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3985 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3986 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3988 else
3990 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3991 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3992 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3995 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3997 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3998 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3999 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4003 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4004 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4005 * generate invalid code
4007 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4009 struct glsl_src_param src0_param;
4010 DWORD write_mask;
4012 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4013 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4015 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4018 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4019 * Start a for() loop where src1.y is the initial value of aL,
4020 * increment aL by src1.z for a total of src1.x iterations.
4021 * Need to use a temporary variable for this operation.
4023 /* FIXME: I don't think nested loops will work correctly this way. */
4024 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4026 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4027 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4028 const struct wined3d_shader *shader = ins->ctx->shader;
4029 const struct wined3d_shader_lconst *constant;
4030 struct glsl_src_param src1_param;
4031 const DWORD *control_values = NULL;
4033 if (ins->ctx->reg_maps->shader_version.major < 4)
4035 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
4037 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4038 * class hardware doesn't support real varying indexing, but Microsoft
4039 * designed this feature for Shader model 2.x+. If the loop control is
4040 * known at compile time, the GLSL compiler can unroll the loop, and
4041 * replace indirect addressing with direct addressing. */
4042 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4044 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4046 if (constant->idx == ins->src[1].reg.idx[0].offset)
4048 control_values = constant->value;
4049 break;
4054 if (control_values)
4056 struct wined3d_shader_loop_control loop_control;
4057 loop_control.count = control_values[0];
4058 loop_control.start = control_values[1];
4059 loop_control.step = (int)control_values[2];
4061 if (loop_control.step > 0)
4063 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4064 loop_state->current_depth, loop_control.start,
4065 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
4066 loop_state->current_depth, loop_control.step);
4068 else if (loop_control.step < 0)
4070 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4071 loop_state->current_depth, loop_control.start,
4072 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
4073 loop_state->current_depth, loop_control.step);
4075 else
4077 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4078 loop_state->current_depth, loop_control.start, loop_state->current_depth,
4079 loop_state->current_depth, loop_control.count,
4080 loop_state->current_depth);
4083 else
4085 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4086 loop_state->current_depth, loop_state->current_reg,
4087 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
4088 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
4091 ++loop_state->current_reg;
4093 else
4095 shader_addline(buffer, "for (;;)\n{\n");
4098 ++loop_state->current_depth;
4101 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4103 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4105 shader_addline(ins->ctx->buffer, "}\n");
4107 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4109 --loop_state->current_depth;
4110 --loop_state->current_reg;
4113 if (ins->handler_idx == WINED3DSIH_ENDREP)
4115 --loop_state->current_depth;
4119 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4121 const struct wined3d_shader *shader = ins->ctx->shader;
4122 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4123 const struct wined3d_shader_lconst *constant;
4124 struct glsl_src_param src0_param;
4125 const DWORD *control_values = NULL;
4127 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4128 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4130 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4132 if (constant->idx == ins->src[0].reg.idx[0].offset)
4134 control_values = constant->value;
4135 break;
4140 if (control_values)
4142 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4143 loop_state->current_depth, loop_state->current_depth,
4144 control_values[0], loop_state->current_depth);
4146 else
4148 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4149 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4150 loop_state->current_depth, loop_state->current_depth,
4151 src0_param.param_str, loop_state->current_depth);
4154 ++loop_state->current_depth;
4157 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
4159 struct glsl_src_param src0_param;
4161 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4162 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
4165 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
4167 struct glsl_src_param src0_param;
4168 struct glsl_src_param src1_param;
4170 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4171 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4173 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
4174 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4177 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
4179 shader_addline(ins->ctx->buffer, "} else {\n");
4182 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
4184 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
4185 shader_glsl_fixup_position(ins->ctx->buffer);
4186 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
4189 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
4191 shader_addline(ins->ctx->buffer, "break;\n");
4194 /* FIXME: According to MSDN the compare is done per component. */
4195 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
4197 struct glsl_src_param src0_param;
4198 struct glsl_src_param src1_param;
4200 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4201 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4203 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
4204 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4207 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
4209 struct glsl_src_param src_param;
4211 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4212 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
4215 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
4217 shader_addline(ins->ctx->buffer, "}\n");
4218 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
4221 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
4223 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
4226 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
4228 struct glsl_src_param src1_param;
4230 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4231 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
4232 src1_param.param_str, ins->src[0].reg.idx[0].offset);
4235 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
4237 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
4238 * function only suppresses the unhandled instruction warning
4242 /*********************************************
4243 * Pixel Shader Specific Code begins here
4244 ********************************************/
4245 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
4247 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4248 ins->ctx->reg_maps->shader_version.minor);
4249 struct glsl_sample_function sample_function;
4250 DWORD sample_flags = 0;
4251 DWORD resource_idx;
4252 DWORD mask = 0, swizzle;
4253 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4255 /* 1.0-1.4: Use destination register as sampler source.
4256 * 2.0+: Use provided sampler source. */
4257 if (shader_version < WINED3D_SHADER_VERSION(2,0))
4258 resource_idx = ins->dst[0].reg.idx[0].offset;
4259 else
4260 resource_idx = ins->src[1].reg.idx[0].offset;
4262 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4264 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4265 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4266 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4268 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4269 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4271 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4272 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4274 case WINED3D_TTFF_COUNT1:
4275 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4276 break;
4277 case WINED3D_TTFF_COUNT2:
4278 mask = WINED3DSP_WRITEMASK_1;
4279 break;
4280 case WINED3D_TTFF_COUNT3:
4281 mask = WINED3DSP_WRITEMASK_2;
4282 break;
4283 case WINED3D_TTFF_COUNT4:
4284 case WINED3D_TTFF_DISABLE:
4285 mask = WINED3DSP_WRITEMASK_3;
4286 break;
4290 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4292 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4294 if (src_mod == WINED3DSPSM_DZ) {
4295 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4296 mask = WINED3DSP_WRITEMASK_2;
4297 } else if (src_mod == WINED3DSPSM_DW) {
4298 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4299 mask = WINED3DSP_WRITEMASK_3;
4302 else
4304 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4305 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4307 /* ps 2.0 texldp instruction always divides by the fourth component. */
4308 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4309 mask = WINED3DSP_WRITEMASK_3;
4313 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
4314 mask |= sample_function.coord_mask;
4315 sample_function.coord_mask = mask;
4317 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4318 else swizzle = ins->src[1].swizzle;
4320 /* 1.0-1.3: Use destination register as coordinate source.
4321 1.4+: Use provided coordinate source register. */
4322 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4324 char coord_mask[6];
4325 shader_glsl_write_mask_to_str(mask, coord_mask);
4326 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4327 "T%u%s", resource_idx, coord_mask);
4329 else
4331 struct glsl_src_param coord_param;
4332 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4333 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4335 struct glsl_src_param bias;
4336 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4337 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4338 NULL, "%s", coord_param.param_str);
4339 } else {
4340 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4341 "%s", coord_param.param_str);
4344 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4347 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4349 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4350 struct glsl_src_param coord_param, dx_param, dy_param;
4351 struct glsl_sample_function sample_function;
4352 DWORD sampler_idx;
4353 DWORD swizzle = ins->src[1].swizzle;
4355 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
4357 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4358 shader_glsl_tex(ins);
4359 return;
4362 sampler_idx = ins->src[1].reg.idx[0].offset;
4364 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
4365 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4366 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
4367 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
4369 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
4370 NULL, NULL, "%s", coord_param.param_str);
4371 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4374 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4376 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4377 struct glsl_src_param coord_param, lod_param;
4378 struct glsl_sample_function sample_function;
4379 DWORD sampler_idx;
4380 DWORD swizzle = ins->src[1].swizzle;
4382 sampler_idx = ins->src[1].reg.idx[0].offset;
4384 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
4385 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4387 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4389 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
4390 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
4392 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4393 * However, the NVIDIA drivers allow them in fragment shaders as well,
4394 * even without the appropriate extension. */
4395 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4397 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
4398 "%s", coord_param.param_str);
4399 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4402 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4403 unsigned int resource_idx, unsigned int sampler_idx)
4405 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4406 unsigned int i;
4408 for (i = 0; i < sampler_map->count; ++i)
4410 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4411 return entries[i].bind_idx;
4414 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4416 return ~0u;
4419 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
4421 static const unsigned int texture_size_component_count[] =
4423 0, /* WINED3D_SHADER_RESOURCE_NONE */
4424 1, /* WINED3D_SHADER_RESOURCE_BUFFER */
4425 1, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
4426 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
4427 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
4428 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
4429 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
4430 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
4431 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
4432 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
4435 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
4436 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4437 enum wined3d_shader_resource_type resource_type;
4438 unsigned int resource_idx, sampler_bind_idx, i;
4439 enum wined3d_data_type dst_data_type;
4440 struct glsl_src_param lod_param;
4441 char dst_swizzle[6];
4442 DWORD write_mask;
4444 dst_data_type = ins->dst[0].reg.data_type;
4445 if (ins->flags == WINED3DSI_RESINFO_UINT)
4446 dst_data_type = WINED3D_DATA_UINT;
4447 else if (ins->flags)
4448 FIXME("Unhandled flags %#x.\n", ins->flags);
4450 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
4451 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
4453 resource_idx = ins->src[1].reg.idx[0].offset;
4454 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4455 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
4456 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4457 resource_idx, WINED3D_SAMPLER_DEFAULT);
4459 if (resource_type >= ARRAY_SIZE(texture_size_component_count))
4461 ERR("Unexpected resource type %#x.\n", resource_type);
4462 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
4465 if (dst_data_type == WINED3D_DATA_UINT)
4466 shader_addline(ins->ctx->buffer, "uvec4(");
4467 else
4468 shader_addline(ins->ctx->buffer, "vec4(");
4470 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
4471 shader_glsl_get_prefix(version->type), sampler_bind_idx, lod_param.param_str);
4473 for (i = 0; i < 3 - texture_size_component_count[resource_type]; ++i)
4474 shader_addline(ins->ctx->buffer, "0, ");
4476 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
4478 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
4479 shader_glsl_get_prefix(version->type), sampler_bind_idx);
4481 else
4483 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
4484 shader_addline(ins->ctx->buffer, "1");
4487 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
4490 /* FIXME: The current implementation does not handle multisample textures correctly. */
4491 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
4493 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4494 struct glsl_src_param coord_param, lod_param;
4495 struct glsl_sample_function sample_function;
4496 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
4498 if (wined3d_shader_instruction_has_texel_offset(ins))
4499 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4501 resource_idx = ins->src[1].reg.idx[0].offset;
4502 sampler_idx = WINED3D_SAMPLER_DEFAULT;
4504 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4505 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4506 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4507 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4508 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
4509 NULL, NULL, lod_param.param_str, &ins->texel_offset, "%s", coord_param.param_str);
4510 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4513 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
4515 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
4516 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
4517 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4518 struct glsl_sample_function sample_function;
4519 DWORD flags = 0;
4521 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
4522 flags |= WINED3D_GLSL_SAMPLE_GRAD;
4523 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
4524 flags |= WINED3D_GLSL_SAMPLE_LOD;
4525 if (wined3d_shader_instruction_has_texel_offset(ins))
4526 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4528 resource_idx = ins->src[1].reg.idx[0].offset;
4529 sampler_idx = ins->src[2].reg.idx[0].offset;
4531 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4532 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4534 switch (ins->handler_idx)
4536 case WINED3DSIH_SAMPLE:
4537 break;
4538 case WINED3DSIH_SAMPLE_B:
4539 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4540 lod_param_str = lod_param.param_str;
4541 break;
4542 case WINED3DSIH_SAMPLE_GRAD:
4543 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dx_param);
4544 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.coord_mask, &dy_param);
4545 dx_param_str = dx_param.param_str;
4546 dy_param_str = dy_param.param_str;
4547 break;
4548 case WINED3DSIH_SAMPLE_LOD:
4549 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4550 lod_param_str = lod_param.param_str;
4551 break;
4552 default:
4553 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4554 break;
4557 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4558 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
4559 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
4560 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4563 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
4565 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4566 struct glsl_src_param coord_param, compare_param;
4567 struct glsl_sample_function sample_function;
4568 const char *lod_param = NULL;
4569 DWORD flags = 0;
4570 UINT coord_size;
4572 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
4574 lod_param = "0";
4575 flags |= WINED3D_GLSL_SAMPLE_LOD;
4578 if (wined3d_shader_instruction_has_texel_offset(ins))
4579 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4581 resource_idx = ins->src[1].reg.idx[0].offset;
4582 sampler_idx = ins->src[2].reg.idx[0].offset;
4584 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4585 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4586 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
4587 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
4588 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4589 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
4590 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
4591 coord_size, coord_param.param_str, compare_param.param_str);
4592 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4595 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
4597 /* FIXME: Make this work for more than just 2D textures */
4598 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4599 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4601 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
4603 char dst_mask[6];
4605 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4606 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
4607 ins->dst[0].reg.idx[0].offset, dst_mask);
4609 else
4611 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4612 DWORD reg = ins->src[0].reg.idx[0].offset;
4613 char dst_swizzle[6];
4615 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
4617 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
4619 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4620 struct glsl_src_param div_param;
4621 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
4623 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
4625 if (mask_size > 1)
4626 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4627 else
4628 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4630 else
4632 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
4637 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4638 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4639 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4640 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
4642 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4643 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4644 struct glsl_sample_function sample_function;
4645 struct glsl_src_param src0_param;
4646 UINT mask_size;
4648 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4650 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4651 * scalar, and projected sampling would require 4.
4653 * It is a dependent read - not valid with conditional NP2 textures
4655 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4656 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4658 switch(mask_size)
4660 case 1:
4661 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4662 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
4663 break;
4665 case 2:
4666 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4667 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
4668 break;
4670 case 3:
4671 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4672 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
4673 break;
4675 default:
4676 FIXME("Unexpected mask size %u\n", mask_size);
4677 break;
4679 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4682 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4683 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4684 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
4686 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4687 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4688 struct glsl_src_param src0_param;
4689 DWORD dst_mask;
4690 unsigned int mask_size;
4692 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4693 mask_size = shader_glsl_get_write_mask_size(dst_mask);
4694 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4696 if (mask_size > 1) {
4697 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
4698 } else {
4699 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
4703 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4704 * Calculate the depth as dst.x / dst.y */
4705 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
4707 struct glsl_dst_param dst_param;
4709 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4711 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4712 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4713 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4714 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4715 * >= 1.0 or < 0.0
4717 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4718 dst_param.reg_name, dst_param.reg_name);
4721 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4722 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4723 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4724 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4726 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
4728 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4729 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4730 struct glsl_src_param src0_param;
4732 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4734 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
4735 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4738 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4739 * Calculate the 1st of a 2-row matrix multiplication. */
4740 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
4742 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4743 DWORD reg = ins->dst[0].reg.idx[0].offset;
4744 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4745 struct glsl_src_param src0_param;
4747 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4748 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4751 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4752 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4753 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
4755 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4756 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4757 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4758 DWORD reg = ins->dst[0].reg.idx[0].offset;
4759 struct glsl_src_param src0_param;
4761 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4762 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
4763 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
4766 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
4768 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4769 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4770 struct glsl_sample_function sample_function;
4771 DWORD reg = ins->dst[0].reg.idx[0].offset;
4772 struct glsl_src_param src0_param;
4774 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4775 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4777 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4779 /* Sample the texture using the calculated coordinates */
4780 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
4781 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4784 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
4785 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
4786 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
4788 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4789 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4790 struct glsl_sample_function sample_function;
4791 DWORD reg = ins->dst[0].reg.idx[0].offset;
4792 struct glsl_src_param src0_param;
4794 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4795 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4797 /* Dependent read, not valid with conditional NP2 */
4798 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4800 /* Sample the texture using the calculated coordinates */
4801 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
4802 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4804 tex_mx->current_row = 0;
4807 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4808 * Perform the 3rd row of a 3x3 matrix multiply */
4809 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
4811 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4812 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4813 DWORD reg = ins->dst[0].reg.idx[0].offset;
4814 struct glsl_src_param src0_param;
4815 char dst_mask[6];
4817 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4819 shader_glsl_append_dst(ins->ctx->buffer, ins);
4820 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4821 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
4823 tex_mx->current_row = 0;
4826 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4827 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4828 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
4830 struct glsl_src_param src0_param;
4831 struct glsl_src_param src1_param;
4832 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4833 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4834 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4835 struct glsl_sample_function sample_function;
4836 DWORD reg = ins->dst[0].reg.idx[0].offset;
4837 char coord_mask[6];
4839 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4840 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4842 /* Perform the last matrix multiply operation */
4843 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4844 /* Reflection calculation */
4845 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
4847 /* Dependent read, not valid with conditional NP2 */
4848 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4849 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4851 /* Sample the texture */
4852 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4853 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
4854 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4856 tex_mx->current_row = 0;
4859 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4860 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4861 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
4863 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4864 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4865 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4866 struct glsl_sample_function sample_function;
4867 DWORD reg = ins->dst[0].reg.idx[0].offset;
4868 struct glsl_src_param src0_param;
4869 char coord_mask[6];
4871 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4873 /* Perform the last matrix multiply operation */
4874 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
4876 /* Construct the eye-ray vector from w coordinates */
4877 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
4878 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
4879 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4881 /* Dependent read, not valid with conditional NP2 */
4882 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4883 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4885 /* Sample the texture using the calculated coordinates */
4886 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4887 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
4888 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4890 tex_mx->current_row = 0;
4893 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4894 * Apply a fake bump map transform.
4895 * texbem is pshader <= 1.3 only, this saves a few version checks
4897 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
4899 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4900 struct glsl_sample_function sample_function;
4901 struct glsl_src_param coord_param;
4902 DWORD sampler_idx;
4903 DWORD mask;
4904 DWORD flags;
4905 char coord_mask[6];
4907 sampler_idx = ins->dst[0].reg.idx[0].offset;
4908 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4909 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4911 /* Dependent read, not valid with conditional NP2 */
4912 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4913 mask = sample_function.coord_mask;
4915 shader_glsl_write_mask_to_str(mask, coord_mask);
4917 /* With projected textures, texbem only divides the static texture coord,
4918 * not the displacement, so we can't let GL handle this. */
4919 if (flags & WINED3D_PSARGS_PROJECTED)
4921 DWORD div_mask=0;
4922 char coord_div_mask[3];
4923 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4925 case WINED3D_TTFF_COUNT1:
4926 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4927 break;
4928 case WINED3D_TTFF_COUNT2:
4929 div_mask = WINED3DSP_WRITEMASK_1;
4930 break;
4931 case WINED3D_TTFF_COUNT3:
4932 div_mask = WINED3DSP_WRITEMASK_2;
4933 break;
4934 case WINED3D_TTFF_COUNT4:
4935 case WINED3D_TTFF_DISABLE:
4936 div_mask = WINED3DSP_WRITEMASK_3;
4937 break;
4939 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4940 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4943 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4945 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4946 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4947 coord_param.param_str, coord_mask);
4949 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4951 struct glsl_src_param luminance_param;
4952 struct glsl_dst_param dst_param;
4954 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4955 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4957 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4958 dst_param.reg_name, dst_param.mask_str,
4959 luminance_param.param_str, sampler_idx, sampler_idx);
4961 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4964 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4966 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4967 struct glsl_src_param src0_param, src1_param;
4969 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4970 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4972 shader_glsl_append_dst(ins->ctx->buffer, ins);
4973 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4974 src0_param.param_str, sampler_idx, src1_param.param_str);
4977 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4978 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4979 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4981 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4982 struct glsl_sample_function sample_function;
4983 struct glsl_src_param src0_param;
4985 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4987 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4988 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4989 "%s.wx", src0_param.reg_name);
4990 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4993 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4994 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4995 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4997 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4998 struct glsl_sample_function sample_function;
4999 struct glsl_src_param src0_param;
5001 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
5003 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5004 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5005 "%s.yz", src0_param.reg_name);
5006 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5009 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
5010 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
5011 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
5013 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5014 struct glsl_sample_function sample_function;
5015 struct glsl_src_param src0_param;
5017 /* Dependent read, not valid with conditional NP2 */
5018 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5019 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
5021 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5022 "%s", src0_param.param_str);
5023 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5026 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
5027 * If any of the first 3 components are < 0, discard this pixel */
5028 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
5030 if (ins->ctx->reg_maps->shader_version.major >= 4)
5032 struct glsl_src_param src_param;
5034 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5035 shader_addline(ins->ctx->buffer, "if (bool(floatBitsToUint(%s))) discard;\n", src_param.param_str);
5037 else
5039 struct glsl_dst_param dst_param;
5041 /* The argument is a destination parameter, and no writemasks are allowed */
5042 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5044 /* 2.0 shaders compare all 4 components in texkill. */
5045 if (ins->ctx->reg_maps->shader_version.major >= 2)
5046 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
5047 /* 1.x shaders only compare the first 3 components, probably due to
5048 * the nature of the texkill instruction as a tex* instruction, and
5049 * phase, which kills all .w components. Even if all 4 components are
5050 * defined, only the first 3 are used. */
5051 else
5052 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
5056 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
5057 * dst = dot2(src0, src1) + src2 */
5058 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
5060 struct glsl_src_param src0_param;
5061 struct glsl_src_param src1_param;
5062 struct glsl_src_param src2_param;
5063 DWORD write_mask;
5064 unsigned int mask_size;
5066 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5067 mask_size = shader_glsl_get_write_mask_size(write_mask);
5069 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
5070 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
5071 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
5073 if (mask_size > 1) {
5074 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
5075 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
5076 } else {
5077 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
5078 src0_param.param_str, src1_param.param_str, src2_param.param_str);
5082 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
5083 const struct wined3d_shader_signature *input_signature,
5084 const struct wined3d_shader_reg_maps *reg_maps,
5085 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
5087 unsigned int i;
5089 for (i = 0; i < input_signature->element_count; ++i)
5091 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5092 const char *semantic_name;
5093 UINT semantic_idx;
5094 char reg_mask[6];
5096 /* Unused */
5097 if (!(reg_maps->input_registers & (1u << input->register_idx)))
5098 continue;
5100 semantic_name = input->semantic_name;
5101 semantic_idx = input->semantic_idx;
5102 shader_glsl_write_mask_to_str(input->mask, reg_mask);
5104 if (args->vp_mode == vertexshader)
5106 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
5107 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
5108 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5109 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5110 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
5111 else
5112 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
5113 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5114 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
5116 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5118 if (args->pointsprite)
5119 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
5120 shader->u.ps.input_reg_map[input->register_idx]);
5121 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
5122 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
5123 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5124 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
5125 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
5126 else
5127 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5128 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5130 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
5132 if (!semantic_idx)
5133 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
5134 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5135 else if (semantic_idx == 1)
5136 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
5137 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5138 else
5139 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5140 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5142 else
5144 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5145 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5150 /*********************************************
5151 * Vertex Shader Specific Code begins here
5152 ********************************************/
5154 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
5156 struct glsl_program_key key;
5158 key.vs_id = entry->vs.id;
5159 key.gs_id = entry->gs.id;
5160 key.ps_id = entry->ps.id;
5162 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
5164 ERR("Failed to insert program entry.\n");
5168 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
5169 GLuint vs_id, GLuint gs_id, GLuint ps_id)
5171 struct wine_rb_entry *entry;
5172 struct glsl_program_key key;
5174 key.vs_id = vs_id;
5175 key.gs_id = gs_id;
5176 key.ps_id = ps_id;
5178 entry = wine_rb_get(&priv->program_lookup, &key);
5179 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
5182 /* Context activation is done by the caller. */
5183 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
5184 struct glsl_shader_prog_link *entry)
5186 struct glsl_program_key key;
5188 key.vs_id = entry->vs.id;
5189 key.gs_id = entry->gs.id;
5190 key.ps_id = entry->ps.id;
5191 wine_rb_remove(&priv->program_lookup, &key);
5193 GL_EXTCALL(glDeleteProgram(entry->id));
5194 if (entry->vs.id)
5195 list_remove(&entry->vs.shader_entry);
5196 if (entry->gs.id)
5197 list_remove(&entry->gs.shader_entry);
5198 if (entry->ps.id)
5199 list_remove(&entry->ps.shader_entry);
5200 HeapFree(GetProcessHeap(), 0, entry);
5203 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
5204 const struct wined3d_gl_info *gl_info, const DWORD *map,
5205 const struct wined3d_shader_signature *input_signature,
5206 const struct wined3d_shader_reg_maps *reg_maps_in,
5207 const struct wined3d_shader_signature *output_signature,
5208 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
5210 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5211 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5212 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5213 unsigned int in_count = vec4_varyings(3, gl_info);
5214 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
5215 DWORD in_idx, *set = NULL;
5216 unsigned int i, j;
5217 char reg_mask[6];
5219 set = wined3d_calloc(max_varyings, sizeof(*set));
5221 for (i = 0; i < input_signature->element_count; ++i)
5223 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5225 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
5226 continue;
5228 in_idx = map[input->register_idx];
5229 /* Declared, but not read register */
5230 if (in_idx == ~0u)
5231 continue;
5232 if (in_idx >= max_varyings)
5234 FIXME("More input varyings declared than supported, expect issues.\n");
5235 continue;
5238 if (in_idx == in_count)
5239 string_buffer_sprintf(destination, "gl_FrontColor");
5240 else if (in_idx == in_count + 1)
5241 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5242 else
5243 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
5245 if (!set[in_idx])
5246 set[in_idx] = ~0u;
5248 for (j = 0; j < output_signature->element_count; ++j)
5250 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
5251 DWORD mask;
5253 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
5254 || input->semantic_idx != output->semantic_idx
5255 || strcmp(input->semantic_name, output->semantic_name)
5256 || !(mask = input->mask & output->mask))
5257 continue;
5259 if (set[in_idx] == ~0u)
5260 set[in_idx] = 0;
5261 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
5262 shader_glsl_write_mask_to_str(mask, reg_mask);
5264 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
5265 destination->buffer, reg_mask, output->register_idx, reg_mask);
5269 for (i = 0; i < max_varyings; ++i)
5271 unsigned int size;
5273 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
5274 continue;
5276 if (set[i] == ~0u)
5277 set[i] = 0;
5279 size = 0;
5280 if (!(set[i] & WINED3DSP_WRITEMASK_0))
5281 reg_mask[size++] = 'x';
5282 if (!(set[i] & WINED3DSP_WRITEMASK_1))
5283 reg_mask[size++] = 'y';
5284 if (!(set[i] & WINED3DSP_WRITEMASK_2))
5285 reg_mask[size++] = 'z';
5286 if (!(set[i] & WINED3DSP_WRITEMASK_3))
5287 reg_mask[size++] = 'w';
5288 reg_mask[size] = '\0';
5290 if (i == in_count)
5291 string_buffer_sprintf(destination, "gl_FrontColor");
5292 else if (i == in_count + 1)
5293 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5294 else
5295 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
5297 if (size == 1)
5298 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
5299 else
5300 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
5303 HeapFree(GetProcessHeap(), 0, set);
5304 string_buffer_release(&priv->string_buffers, destination);
5307 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
5308 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
5309 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
5311 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5312 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5313 char reg_mask[6];
5314 unsigned int i;
5316 for (i = 0; i < output_signature->element_count; ++i)
5318 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
5320 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
5321 continue;
5323 if (output->register_idx >= input_count)
5324 continue;
5326 string_buffer_sprintf(destination, "%s[%u]", out_array_name, output->register_idx);
5328 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5330 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
5331 destination->buffer, reg_mask, output->register_idx, reg_mask);
5334 string_buffer_release(&priv->string_buffers, destination);
5337 /* Context activation is done by the caller. */
5338 static void shader_glsl_generate_vs_gs_setup(struct shader_glsl_priv *priv,
5339 const struct wined3d_shader *vs, unsigned int input_count,
5340 const struct wined3d_gl_info *gl_info)
5342 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5343 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5345 if (legacy_context)
5346 shader_addline(buffer, "varying out vec4 gs_in[%u];\n", input_count);
5347 else
5348 shader_addline(buffer, "out vs_gs_iface { vec4 gs_in[%u]; } gs_in;\n", input_count);
5349 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
5351 shader_glsl_setup_sm4_shader_output(priv, input_count, &vs->output_signature, &vs->reg_maps,
5352 legacy_context ? "gs_in" : "gs_in.gs_in");
5354 shader_addline(buffer, "}\n");
5357 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
5358 const struct wined3d_gl_info *gl_info, const DWORD *map,
5359 const struct wined3d_shader_signature *input_signature,
5360 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
5361 const struct wined3d_shader_signature *output_signature,
5362 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
5364 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5365 const char *semantic_name;
5366 UINT semantic_idx;
5367 char reg_mask[6];
5368 unsigned int i;
5370 /* First, sort out position and point size system values. */
5371 for (i = 0; i < output_signature->element_count; ++i)
5373 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
5375 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
5376 continue;
5378 semantic_name = output->semantic_name;
5379 semantic_idx = output->semantic_idx;
5380 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5382 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
5384 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
5385 reg_mask, output->register_idx, reg_mask);
5387 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5389 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
5390 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
5394 /* Then, setup the pixel shader input. */
5395 if (reg_maps_out->shader_version.major < 4)
5396 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
5397 output_signature, reg_maps_out, "ps_link");
5398 else
5399 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "ps_link");
5402 /* Context activation is done by the caller. */
5403 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
5404 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
5405 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
5407 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5408 GLuint ret;
5409 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
5410 unsigned int i;
5411 const char *semantic_name;
5412 UINT semantic_idx;
5413 char reg_mask[6];
5414 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5416 string_buffer_clear(buffer);
5418 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &vs->reg_maps.shader_version));
5420 if (per_vertex_point_size)
5422 shader_addline(buffer, "uniform struct\n{\n");
5423 shader_addline(buffer, " float size_min;\n");
5424 shader_addline(buffer, " float size_max;\n");
5425 shader_addline(buffer, "} ffp_point;\n");
5428 if (ps_major < 3)
5430 DWORD colors_written_mask[2] = {0};
5431 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
5433 if (!legacy_context)
5435 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
5436 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
5437 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
5438 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
5441 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
5443 for (i = 0; i < vs->output_signature.element_count; ++i)
5445 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5446 DWORD write_mask;
5448 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5449 continue;
5451 semantic_name = output->semantic_name;
5452 semantic_idx = output->semantic_idx;
5453 write_mask = output->mask;
5454 shader_glsl_write_mask_to_str(write_mask, reg_mask);
5456 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
5458 if (legacy_context)
5459 shader_addline(buffer, "gl_Front%sColor%s = shader_out[%u]%s;\n",
5460 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
5461 else
5462 shader_addline(buffer, "ffp_varying_%s%s = clamp(shader_out[%u]%s, 0.0, 1.0);\n",
5463 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
5465 colors_written_mask[semantic_idx] = write_mask;
5467 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5469 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
5470 reg_mask, output->register_idx, reg_mask);
5472 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5474 if (semantic_idx < MAX_TEXTURES)
5476 shader_addline(buffer, "%s[%u]%s = shader_out[%u]%s;\n",
5477 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
5478 semantic_idx, reg_mask, output->register_idx, reg_mask);
5479 texcoords_written_mask[semantic_idx] = write_mask;
5482 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5484 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
5485 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
5487 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
5489 shader_addline(buffer, "%s = clamp(shader_out[%u].%c, 0.0, 1.0);\n",
5490 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
5491 output->register_idx, reg_mask[1]);
5495 for (i = 0; i < 2; ++i)
5497 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5499 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5500 if (!i)
5501 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
5502 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
5503 reg_mask, reg_mask);
5504 else
5505 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
5506 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
5507 reg_mask, reg_mask);
5510 for (i = 0; i < MAX_TEXTURES; ++i)
5512 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
5513 continue;
5515 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5517 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
5518 && !texcoords_written_mask[i])
5519 continue;
5521 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5522 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
5523 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
5527 else
5529 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
5531 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
5532 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
5533 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
5534 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
5537 shader_addline(buffer, "}\n");
5539 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5540 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
5541 shader_glsl_compile(gl_info, ret, buffer->buffer);
5543 return ret;
5546 static void shader_glsl_generate_sm4_rasterizer_input_setup(struct shader_glsl_priv *priv,
5547 const struct wined3d_shader *shader, unsigned int input_count,
5548 const struct wined3d_gl_info *gl_info)
5550 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5552 if (input_count)
5553 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", min(vec4_varyings(4, gl_info), input_count));
5555 shader_addline(buffer, "void setup_%s_output(in vec4 shader_out[%u])\n{\n",
5556 shader_glsl_get_prefix(shader->reg_maps.shader_version.type), shader->limits->packed_output);
5558 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
5559 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
5561 shader_addline(buffer, "}\n");
5564 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
5565 const struct wined3d_gl_info *gl_info)
5567 const char *output = get_fragment_output(gl_info);
5569 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
5570 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
5571 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
5572 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
5573 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
5574 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
5577 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
5578 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
5580 const char *output = get_fragment_output(gl_info);
5582 switch (mode)
5584 case WINED3D_FFP_PS_FOG_OFF:
5585 return;
5587 case WINED3D_FFP_PS_FOG_LINEAR:
5588 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
5589 break;
5591 case WINED3D_FFP_PS_FOG_EXP:
5592 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
5593 break;
5595 case WINED3D_FFP_PS_FOG_EXP2:
5596 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
5597 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
5598 break;
5600 default:
5601 ERR("Invalid fog mode %#x.\n", mode);
5602 return;
5605 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
5606 output, output);
5609 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
5610 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
5612 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
5613 * flipping all the operators here, just negate the comparison below. */
5614 static const char * const comparison_operator[] =
5616 "", /* WINED3D_CMP_NEVER */
5617 "<", /* WINED3D_CMP_LESS */
5618 "==", /* WINED3D_CMP_EQUAL */
5619 "<=", /* WINED3D_CMP_LESSEQUAL */
5620 ">", /* WINED3D_CMP_GREATER */
5621 "!=", /* WINED3D_CMP_NOTEQUAL */
5622 ">=", /* WINED3D_CMP_GREATEREQUAL */
5623 "" /* WINED3D_CMP_ALWAYS */
5626 if (alpha_func == WINED3D_CMP_ALWAYS)
5627 return;
5629 if (alpha_func != WINED3D_CMP_NEVER)
5630 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
5631 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
5632 shader_addline(buffer, " discard;\n");
5635 /* Context activation is done by the caller. */
5636 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
5637 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5638 const struct wined3d_shader *shader,
5639 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
5641 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5642 const struct wined3d_gl_info *gl_info = context->gl_info;
5643 const DWORD *function = shader->function;
5644 struct shader_glsl_ctx_priv priv_ctx;
5645 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5647 /* Create the hw GLSL shader object and assign it as the shader->prgId */
5648 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5650 memset(&priv_ctx, 0, sizeof(priv_ctx));
5651 priv_ctx.cur_ps_args = args;
5652 priv_ctx.cur_np2fixup_info = np2fixup_info;
5653 priv_ctx.string_buffers = string_buffers;
5655 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5657 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5658 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5659 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5660 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
5661 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5662 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5663 /* The spec says that it doesn't have to be explicitly enabled, but the
5664 * nvidia drivers write a warning if we don't do so. */
5665 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5666 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5667 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5668 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5669 if (gl_info->supported[EXT_GPU_SHADER4])
5670 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5671 if (gl_info->supported[EXT_TEXTURE_ARRAY])
5672 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
5674 /* Base Declarations */
5675 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5677 shader_addline(buffer, "void main()\n{\n");
5679 /* Direct3D applications expect integer vPos values, while OpenGL drivers
5680 * add approximately 0.5. This causes off-by-one problems as spotted by
5681 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
5682 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
5683 * causes precision troubles when we just subtract 0.5.
5685 * To deal with that, just floor() the position. This will eliminate the
5686 * fraction on all cards.
5688 * TODO: Test how this behaves with multisampling.
5690 * An advantage of floor is that it works even if the driver doesn't add
5691 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
5692 * to return in gl_FragCoord, even though coordinates specify the pixel
5693 * centers instead of the pixel corners. This code will behave correctly
5694 * on drivers that returns integer values. */
5695 if (reg_maps->vpos)
5697 if (shader->device->wined3d->flags & WINED3D_PIXEL_CENTER_INTEGER)
5698 shader_addline(buffer,
5699 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
5700 else
5701 shader_addline(buffer,
5702 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
5705 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
5707 unsigned int i;
5708 WORD map = reg_maps->texcoord;
5710 if (legacy_context)
5712 if (glsl_is_color_reg_read(shader, 0))
5713 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
5714 if (glsl_is_color_reg_read(shader, 1))
5715 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
5718 for (i = 0; map; map >>= 1, ++i)
5720 if (map & 1)
5722 if (args->pointsprite)
5723 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
5724 else if (args->texcoords_initialized & (1u << i))
5725 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
5726 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
5727 else
5728 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
5729 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
5733 if (legacy_context)
5734 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
5737 /* Pack 3.0 inputs */
5738 if (reg_maps->shader_version.major >= 3)
5739 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
5741 /* Base Shader Body */
5742 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5744 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
5745 if (reg_maps->shader_version.major < 2)
5746 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
5748 if (args->srgb_correction)
5749 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
5751 /* SM < 3 does not replace the fog stage. */
5752 if (reg_maps->shader_version.major < 3)
5753 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
5755 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
5757 shader_addline(buffer, "}\n");
5759 TRACE("Compiling shader object %u.\n", shader_id);
5760 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5762 return shader_id;
5765 /* Context activation is done by the caller. */
5766 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
5767 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
5769 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
5770 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5771 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5772 const struct wined3d_gl_info *gl_info = context->gl_info;
5773 const DWORD *function = shader->function;
5774 struct shader_glsl_ctx_priv priv_ctx;
5775 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5777 /* Create the hw GLSL shader program and assign it as the shader->prgId */
5778 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5780 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5782 if (gl_info->supported[ARB_DRAW_INSTANCED])
5783 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
5784 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5785 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5786 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5787 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5788 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5789 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5790 if (gl_info->supported[EXT_GPU_SHADER4])
5791 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5792 if (gl_info->supported[EXT_TEXTURE_ARRAY])
5793 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
5795 memset(&priv_ctx, 0, sizeof(priv_ctx));
5796 priv_ctx.cur_vs_args = args;
5797 priv_ctx.string_buffers = string_buffers;
5799 /* Base Declarations */
5800 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5802 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL)
5803 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
5805 if (reg_maps->shader_version.major >= 4)
5807 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL)
5808 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->next_shader_input_count, gl_info);
5809 else if (args->next_shader_type == WINED3D_SHADER_TYPE_GEOMETRY)
5810 shader_glsl_generate_vs_gs_setup(priv, shader, args->next_shader_input_count, gl_info);
5813 shader_addline(buffer, "void main()\n{\n");
5815 /* Base Shader Body */
5816 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5818 /* Unpack outputs */
5819 shader_addline(buffer, "setup_vs_output(vs_out);\n");
5821 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
5822 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
5823 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
5824 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
5826 if (reg_maps->shader_version.major < 3)
5828 if (args->fog_src == VS_FOG_Z)
5829 shader_addline(buffer, "%s = gl_Position.z;\n",
5830 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5831 else if (!reg_maps->fog)
5832 shader_addline(buffer, "%s = 0.0;\n",
5833 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5836 /* We always store the clipplanes without y inversion */
5837 if (args->clip_enabled)
5838 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
5840 if (args->point_size && !args->per_vertex_point_size)
5841 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
5843 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL)
5844 shader_glsl_fixup_position(buffer);
5846 shader_addline(buffer, "}\n");
5848 TRACE("Compiling shader object %u.\n", shader_id);
5849 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5851 return shader_id;
5854 /* Context activation is done by the caller. */
5855 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
5856 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
5858 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
5859 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5860 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5861 const struct wined3d_gl_info *gl_info = context->gl_info;
5862 const DWORD *function = shader->function;
5863 struct shader_glsl_ctx_priv priv_ctx;
5864 GLuint shader_id;
5866 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
5868 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5870 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
5871 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
5872 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5873 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5874 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5875 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5876 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5877 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5878 if (gl_info->supported[EXT_GPU_SHADER4])
5879 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5880 if (gl_info->supported[EXT_TEXTURE_ARRAY])
5881 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
5883 memset(&priv_ctx, 0, sizeof(priv_ctx));
5884 priv_ctx.string_buffers = string_buffers;
5885 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5886 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
5887 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->ps_input_count, gl_info);
5888 shader_addline(buffer, "void main()\n{\n");
5889 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5890 shader_addline(buffer, "}\n");
5892 TRACE("Compiling shader object %u.\n", shader_id);
5893 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5895 return shader_id;
5898 static GLuint find_glsl_pshader(const struct wined3d_context *context,
5899 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5900 struct wined3d_shader *shader,
5901 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
5903 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
5904 struct glsl_shader_private *shader_data;
5905 struct ps_np2fixup_info *np2fixup;
5906 UINT i;
5907 DWORD new_size;
5908 GLuint ret;
5910 if (!shader->backend_data)
5912 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5913 if (!shader->backend_data)
5915 ERR("Failed to allocate backend data.\n");
5916 return 0;
5919 shader_data = shader->backend_data;
5920 gl_shaders = shader_data->gl_shaders.ps;
5922 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5923 * so a linear search is more performant than a hashmap or a binary search
5924 * (cache coherency etc)
5926 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5928 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
5930 if (args->np2_fixup)
5931 *np2fixup_info = &gl_shaders[i].np2fixup;
5932 return gl_shaders[i].id;
5936 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5937 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5938 if (shader_data->num_gl_shaders)
5940 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5941 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
5942 new_size * sizeof(*gl_shaders));
5944 else
5946 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5947 new_size = 1;
5950 if(!new_array) {
5951 ERR("Out of memory\n");
5952 return 0;
5954 shader_data->gl_shaders.ps = new_array;
5955 shader_data->shader_array_size = new_size;
5956 gl_shaders = new_array;
5959 gl_shaders[shader_data->num_gl_shaders].args = *args;
5961 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
5962 memset(np2fixup, 0, sizeof(*np2fixup));
5963 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
5965 pixelshader_update_resource_types(shader, args->tex_types);
5967 string_buffer_clear(buffer);
5968 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
5969 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5971 return ret;
5974 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
5975 const DWORD use_map)
5977 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
5978 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
5979 if (stored->point_size != new->point_size)
5980 return FALSE;
5981 if (stored->per_vertex_point_size != new->per_vertex_point_size)
5982 return FALSE;
5983 if (stored->flatshading != new->flatshading)
5984 return FALSE;
5985 if (stored->next_shader_type != new->next_shader_type)
5986 return FALSE;
5987 if (stored->next_shader_input_count != new->next_shader_input_count)
5988 return FALSE;
5989 return stored->fog_src == new->fog_src;
5992 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
5993 struct wined3d_shader *shader, const struct vs_compile_args *args)
5995 UINT i;
5996 DWORD new_size;
5997 DWORD use_map = context->stream_info.use_map;
5998 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
5999 struct glsl_shader_private *shader_data;
6000 GLuint ret;
6002 if (!shader->backend_data)
6004 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
6005 if (!shader->backend_data)
6007 ERR("Failed to allocate backend data.\n");
6008 return 0;
6011 shader_data = shader->backend_data;
6012 gl_shaders = shader_data->gl_shaders.vs;
6014 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
6015 * so a linear search is more performant than a hashmap or a binary search
6016 * (cache coherency etc)
6018 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6020 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
6021 return gl_shaders[i].id;
6024 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6026 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
6027 if (shader_data->num_gl_shaders)
6029 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
6030 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
6031 new_size * sizeof(*gl_shaders));
6033 else
6035 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
6036 new_size = 1;
6039 if(!new_array) {
6040 ERR("Out of memory\n");
6041 return 0;
6043 shader_data->gl_shaders.vs = new_array;
6044 shader_data->shader_array_size = new_size;
6045 gl_shaders = new_array;
6048 gl_shaders[shader_data->num_gl_shaders].args = *args;
6050 string_buffer_clear(&priv->shader_buffer);
6051 ret = shader_glsl_generate_vshader(context, priv, shader, args);
6052 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6054 return ret;
6057 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
6058 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
6060 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
6061 struct glsl_shader_private *shader_data;
6062 unsigned int i, new_size;
6063 GLuint ret;
6065 if (!shader->backend_data)
6067 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
6069 ERR("Failed to allocate backend data.\n");
6070 return 0;
6073 shader_data = shader->backend_data;
6074 gl_shaders = shader_data->gl_shaders.gs;
6076 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6078 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
6079 return gl_shaders[i].id;
6082 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6084 if (shader_data->num_gl_shaders)
6086 new_size = shader_data->shader_array_size + 1;
6087 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.gs,
6088 new_size * sizeof(*new_array));
6090 else
6092 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
6093 new_size = 1;
6096 if (!new_array)
6098 ERR("Failed to allocate GL shaders array.\n");
6099 return 0;
6101 shader_data->gl_shaders.gs = new_array;
6102 shader_data->shader_array_size = new_size;
6103 gl_shaders = new_array;
6105 string_buffer_clear(&priv->shader_buffer);
6106 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
6107 gl_shaders[shader_data->num_gl_shaders].args = *args;
6108 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6110 return ret;
6113 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
6115 switch (mcs)
6117 case WINED3D_MCS_MATERIAL:
6118 return material;
6119 case WINED3D_MCS_COLOR1:
6120 return "ffp_attrib_diffuse";
6121 case WINED3D_MCS_COLOR2:
6122 return "ffp_attrib_specular";
6123 default:
6124 ERR("Invalid material color source %#x.\n", mcs);
6125 return "<invalid>";
6129 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
6130 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
6132 const char *diffuse, *specular, *emissive, *ambient;
6133 enum wined3d_light_type light_type;
6134 unsigned int i;
6136 if (!settings->lighting)
6138 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
6139 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
6140 return;
6143 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
6144 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
6145 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
6146 shader_addline(buffer, "vec3 dir, dst;\n");
6147 shader_addline(buffer, "float att, t;\n");
6149 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
6150 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
6151 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
6152 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
6154 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
6156 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
6157 switch (light_type)
6159 case WINED3D_LIGHT_POINT:
6160 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
6161 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
6162 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
6163 shader_addline(buffer, "dst.x = 1.0;\n");
6164 if (legacy_lighting)
6166 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
6167 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
6169 else
6171 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
6173 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6174 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", i, i, i);
6175 if (!legacy_lighting)
6176 shader_addline(buffer, "att = 1.0 / att;\n");
6177 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
6178 if (!settings->normal)
6180 if (!legacy_lighting)
6181 shader_addline(buffer, "}\n");
6182 break;
6184 shader_addline(buffer, "dir = normalize(dir);\n");
6185 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
6186 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
6187 if (settings->localviewer)
6188 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6189 else
6190 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6191 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
6192 " * ffp_light[%u].specular) * att;\n", i);
6193 if (!legacy_lighting)
6194 shader_addline(buffer, "}\n");
6195 break;
6197 case WINED3D_LIGHT_SPOT:
6198 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
6199 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
6200 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
6201 shader_addline(buffer, "dst.x = 1.0;\n");
6202 if (legacy_lighting)
6204 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
6205 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
6207 else
6209 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
6211 shader_addline(buffer, "dir = normalize(dir);\n");
6212 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", i);
6213 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", i);
6214 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", i);
6215 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
6216 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
6217 i, i, i, i);
6218 if (legacy_lighting)
6219 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6220 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
6221 i, i, i);
6222 else
6223 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6224 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
6225 i, i, i);
6226 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
6227 if (!settings->normal)
6229 if (!legacy_lighting)
6230 shader_addline(buffer, "}\n");
6231 break;
6233 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
6234 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
6235 if (settings->localviewer)
6236 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6237 else
6238 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6239 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
6240 " * ffp_light[%u].specular) * att;\n", i);
6241 if (!legacy_lighting)
6242 shader_addline(buffer, "}\n");
6243 break;
6245 case WINED3D_LIGHT_DIRECTIONAL:
6246 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
6247 if (!settings->normal)
6248 break;
6249 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", i);
6250 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6251 " * ffp_light[%u].diffuse.xyz;\n", i);
6252 /* TODO: In the non-local viewer case the halfvector is constant
6253 * and could be precomputed and stored in a uniform. */
6254 if (settings->localviewer)
6255 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6256 else
6257 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6258 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6259 " * ffp_light[%u].specular;\n", i);
6260 break;
6262 case WINED3D_LIGHT_PARALLELPOINT:
6263 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
6264 if (!settings->normal)
6265 break;
6266 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", i);
6267 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6268 " * ffp_light[%u].diffuse.xyz;\n", i);
6269 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6270 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6271 " * ffp_light[%u].specular;\n", i);
6272 break;
6274 default:
6275 if (light_type)
6276 FIXME("Unhandled light type %#x.\n", light_type);
6277 continue;
6281 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
6282 ambient, diffuse, emissive);
6283 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
6284 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
6287 /* Context activation is done by the caller. */
6288 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
6289 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
6291 static const struct attrib_info
6293 const char type[6];
6294 const char name[24];
6296 attrib_info[] =
6298 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
6299 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
6300 /* TODO: Indexed vertex blending */
6301 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
6302 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
6303 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
6304 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
6305 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
6307 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6308 BOOL legacy_lighting = priv->legacy_lighting;
6309 GLuint shader_obj;
6310 unsigned int i;
6311 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6312 BOOL output_legacy_fogcoord = legacy_context;
6314 string_buffer_clear(buffer);
6316 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6318 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
6320 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
6322 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
6324 shader_addline(buffer, "\n");
6326 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
6327 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
6328 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
6329 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
6331 shader_addline(buffer, "uniform struct\n{\n");
6332 shader_addline(buffer, " vec4 emissive;\n");
6333 shader_addline(buffer, " vec4 ambient;\n");
6334 shader_addline(buffer, " vec4 diffuse;\n");
6335 shader_addline(buffer, " vec4 specular;\n");
6336 shader_addline(buffer, " float shininess;\n");
6337 shader_addline(buffer, "} ffp_material;\n");
6339 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
6340 shader_addline(buffer, "uniform struct\n{\n");
6341 shader_addline(buffer, " vec4 diffuse;\n");
6342 shader_addline(buffer, " vec4 specular;\n");
6343 shader_addline(buffer, " vec4 ambient;\n");
6344 shader_addline(buffer, " vec4 position;\n");
6345 shader_addline(buffer, " vec3 direction;\n");
6346 shader_addline(buffer, " float range;\n");
6347 shader_addline(buffer, " float falloff;\n");
6348 shader_addline(buffer, " float c_att;\n");
6349 shader_addline(buffer, " float l_att;\n");
6350 shader_addline(buffer, " float q_att;\n");
6351 shader_addline(buffer, " float cos_htheta;\n");
6352 shader_addline(buffer, " float cos_hphi;\n");
6353 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
6355 if (settings->point_size)
6357 shader_addline(buffer, "uniform struct\n{\n");
6358 shader_addline(buffer, " float size;\n");
6359 shader_addline(buffer, " float size_min;\n");
6360 shader_addline(buffer, " float size_max;\n");
6361 shader_addline(buffer, " float c_att;\n");
6362 shader_addline(buffer, " float l_att;\n");
6363 shader_addline(buffer, " float q_att;\n");
6364 shader_addline(buffer, "} ffp_point;\n");
6367 if (legacy_context)
6369 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6370 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6371 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6372 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6374 else
6376 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
6377 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
6378 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6379 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6382 shader_addline(buffer, "\nvoid main()\n{\n");
6383 shader_addline(buffer, "float m;\n");
6384 shader_addline(buffer, "vec3 r;\n");
6386 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
6388 if (attrib_info[i].name[0])
6389 shader_addline(buffer, "%s %s = vs_in%u;\n",
6390 attrib_info[i].type, attrib_info[i].name, i);
6392 for (i = 0; i < MAX_TEXTURES; ++i)
6394 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
6395 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
6396 && settings->texcoords & (1u << i))
6397 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
6400 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
6402 if (settings->transformed)
6404 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
6405 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6406 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
6408 else
6410 for (i = 0; i < settings->vertexblends; ++i)
6411 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
6413 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
6414 for (i = 0; i < settings->vertexblends + 1; ++i)
6415 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
6417 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6418 if (settings->clipping)
6419 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
6420 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
6423 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
6424 if (settings->normal)
6426 if (!settings->vertexblends)
6428 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
6430 else
6432 for (i = 0; i < settings->vertexblends + 1; ++i)
6433 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
6436 if (settings->normalize)
6437 shader_addline(buffer, "normal = normalize(normal);\n");
6440 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
6441 if (legacy_context)
6443 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
6444 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
6446 else
6448 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
6449 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
6452 for (i = 0; i < MAX_TEXTURES; ++i)
6454 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6456 switch (settings->texgen[i] & 0xffff0000)
6458 case WINED3DTSS_TCI_PASSTHRU:
6459 if (settings->texcoords & (1u << i))
6460 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
6461 i, i, i);
6462 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
6463 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
6464 else
6465 output_legacy_texcoord = FALSE;
6466 break;
6468 case WINED3DTSS_TCI_CAMERASPACENORMAL:
6469 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
6470 break;
6472 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
6473 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
6474 break;
6476 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
6477 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6478 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
6479 break;
6481 case WINED3DTSS_TCI_SPHEREMAP:
6482 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
6483 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
6484 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6485 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
6486 break;
6488 default:
6489 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
6490 break;
6492 if (output_legacy_texcoord)
6493 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
6496 switch (settings->fog_mode)
6498 case WINED3D_FFP_VS_FOG_OFF:
6499 output_legacy_fogcoord = FALSE;
6500 break;
6502 case WINED3D_FFP_VS_FOG_FOGCOORD:
6503 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
6504 break;
6506 case WINED3D_FFP_VS_FOG_RANGE:
6507 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
6508 break;
6510 case WINED3D_FFP_VS_FOG_DEPTH:
6511 if (settings->ortho_fog)
6512 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
6513 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
6514 else if (settings->transformed)
6515 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
6516 else
6517 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
6518 break;
6520 default:
6521 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
6522 break;
6524 if (output_legacy_fogcoord)
6525 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
6527 if (settings->point_size)
6529 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
6530 " + ffp_point.l_att * length(ec_pos.xyz)"
6531 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
6532 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
6533 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
6536 shader_addline(buffer, "}\n");
6538 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6539 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
6541 return shader_obj;
6544 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
6545 DWORD argnum, unsigned int stage, DWORD arg)
6547 const char *ret;
6549 if (arg == ARG_UNUSED)
6550 return "<unused arg>";
6552 switch (arg & WINED3DTA_SELECTMASK)
6554 case WINED3DTA_DIFFUSE:
6555 ret = "ffp_varying_diffuse";
6556 break;
6558 case WINED3DTA_CURRENT:
6559 ret = "ret";
6560 break;
6562 case WINED3DTA_TEXTURE:
6563 switch (stage)
6565 case 0: ret = "tex0"; break;
6566 case 1: ret = "tex1"; break;
6567 case 2: ret = "tex2"; break;
6568 case 3: ret = "tex3"; break;
6569 case 4: ret = "tex4"; break;
6570 case 5: ret = "tex5"; break;
6571 case 6: ret = "tex6"; break;
6572 case 7: ret = "tex7"; break;
6573 default:
6574 ret = "<invalid texture>";
6575 break;
6577 break;
6579 case WINED3DTA_TFACTOR:
6580 ret = "tex_factor";
6581 break;
6583 case WINED3DTA_SPECULAR:
6584 ret = "ffp_varying_specular";
6585 break;
6587 case WINED3DTA_TEMP:
6588 ret = "temp_reg";
6589 break;
6591 case WINED3DTA_CONSTANT:
6592 switch (stage)
6594 case 0: ret = "tss_const0"; break;
6595 case 1: ret = "tss_const1"; break;
6596 case 2: ret = "tss_const2"; break;
6597 case 3: ret = "tss_const3"; break;
6598 case 4: ret = "tss_const4"; break;
6599 case 5: ret = "tss_const5"; break;
6600 case 6: ret = "tss_const6"; break;
6601 case 7: ret = "tss_const7"; break;
6602 default:
6603 ret = "<invalid constant>";
6604 break;
6606 break;
6608 default:
6609 return "<unhandled arg>";
6612 if (arg & WINED3DTA_COMPLEMENT)
6614 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
6615 if (argnum == 0)
6616 ret = "arg0";
6617 else if (argnum == 1)
6618 ret = "arg1";
6619 else if (argnum == 2)
6620 ret = "arg2";
6623 if (arg & WINED3DTA_ALPHAREPLICATE)
6625 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
6626 if (argnum == 0)
6627 ret = "arg0";
6628 else if (argnum == 1)
6629 ret = "arg1";
6630 else if (argnum == 2)
6631 ret = "arg2";
6634 return ret;
6637 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
6638 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
6640 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
6642 if (color && alpha)
6643 dstmask = "";
6644 else if (color)
6645 dstmask = ".xyz";
6646 else
6647 dstmask = ".w";
6649 if (dst == tempreg)
6650 dstreg = "temp_reg";
6651 else
6652 dstreg = "ret";
6654 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
6655 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
6656 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
6658 switch (op)
6660 case WINED3D_TOP_DISABLE:
6661 break;
6663 case WINED3D_TOP_SELECT_ARG1:
6664 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
6665 break;
6667 case WINED3D_TOP_SELECT_ARG2:
6668 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
6669 break;
6671 case WINED3D_TOP_MODULATE:
6672 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6673 break;
6675 case WINED3D_TOP_MODULATE_4X:
6676 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
6677 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6678 break;
6680 case WINED3D_TOP_MODULATE_2X:
6681 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
6682 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6683 break;
6685 case WINED3D_TOP_ADD:
6686 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
6687 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6688 break;
6690 case WINED3D_TOP_ADD_SIGNED:
6691 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
6692 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6693 break;
6695 case WINED3D_TOP_ADD_SIGNED_2X:
6696 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
6697 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6698 break;
6700 case WINED3D_TOP_SUBTRACT:
6701 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
6702 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6703 break;
6705 case WINED3D_TOP_ADD_SMOOTH:
6706 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
6707 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
6708 break;
6710 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
6711 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
6712 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6713 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6714 break;
6716 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6717 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6718 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6719 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6720 break;
6722 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6723 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
6724 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6725 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6726 break;
6728 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6729 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6730 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6731 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
6732 break;
6734 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
6735 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
6736 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6737 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6738 break;
6740 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
6741 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
6742 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6743 break;
6745 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
6746 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
6747 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6748 break;
6750 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
6751 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6752 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6753 break;
6754 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
6755 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
6756 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6757 break;
6759 case WINED3D_TOP_BUMPENVMAP:
6760 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6761 /* These are handled in the first pass, nothing to do. */
6762 break;
6764 case WINED3D_TOP_DOTPRODUCT3:
6765 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
6766 dstreg, dstmask, arg1, arg2, dstmask);
6767 break;
6769 case WINED3D_TOP_MULTIPLY_ADD:
6770 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
6771 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
6772 break;
6774 case WINED3D_TOP_LERP:
6775 /* MSDN isn't quite right here. */
6776 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
6777 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
6778 break;
6780 default:
6781 FIXME("Unhandled operation %#x.\n", op);
6782 break;
6786 /* Context activation is done by the caller. */
6787 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
6788 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
6790 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
6791 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
6792 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6793 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6794 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
6795 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
6796 UINT lowest_disabled_stage;
6797 GLuint shader_id;
6798 DWORD arg0, arg1, arg2;
6799 unsigned int stage;
6801 string_buffer_clear(buffer);
6803 /* Find out which textures are read */
6804 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6806 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6807 break;
6809 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
6810 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
6811 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
6813 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
6814 || (stage == 0 && settings->color_key_enabled))
6815 tex_map |= 1u << stage;
6816 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6817 tfactor_used = TRUE;
6818 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6819 tempreg_used = TRUE;
6820 if (settings->op[stage].dst == tempreg)
6821 tempreg_used = TRUE;
6822 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6823 tss_const_map |= 1u << stage;
6825 switch (settings->op[stage].cop)
6827 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6828 lum_map |= 1u << stage;
6829 /* fall through */
6830 case WINED3D_TOP_BUMPENVMAP:
6831 bump_map |= 1u << stage;
6832 /* fall through */
6833 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6834 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6835 tex_map |= 1u << stage;
6836 break;
6838 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6839 tfactor_used = TRUE;
6840 break;
6842 default:
6843 break;
6846 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6847 continue;
6849 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
6850 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
6851 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
6853 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
6854 tex_map |= 1u << stage;
6855 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6856 tfactor_used = TRUE;
6857 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6858 tempreg_used = TRUE;
6859 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6860 tss_const_map |= 1u << stage;
6862 lowest_disabled_stage = stage;
6864 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6866 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6867 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6869 if (!needs_legacy_glsl_syntax(gl_info))
6870 shader_addline(buffer, "out vec4 ps_out[1];\n");
6872 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
6873 shader_addline(buffer, "vec4 ret;\n");
6874 if (tempreg_used || settings->sRGB_write)
6875 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
6876 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
6878 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6880 if (tss_const_map & (1u << stage))
6881 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
6883 if (!(tex_map & (1u << stage)))
6884 continue;
6886 switch (settings->op[stage].tex_type)
6888 case WINED3D_GL_RES_TYPE_TEX_1D:
6889 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
6890 break;
6891 case WINED3D_GL_RES_TYPE_TEX_2D:
6892 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
6893 break;
6894 case WINED3D_GL_RES_TYPE_TEX_3D:
6895 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
6896 break;
6897 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6898 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
6899 break;
6900 case WINED3D_GL_RES_TYPE_TEX_RECT:
6901 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
6902 break;
6903 default:
6904 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
6905 break;
6908 shader_addline(buffer, "vec4 tex%u;\n", stage);
6910 if (!(bump_map & (1u << stage)))
6911 continue;
6912 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
6914 if (!(lum_map & (1u << stage)))
6915 continue;
6916 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
6917 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
6919 if (tfactor_used)
6920 shader_addline(buffer, "uniform vec4 tex_factor;\n");
6921 if (settings->color_key_enabled)
6922 shader_addline(buffer, "uniform vec4 color_key[2];\n");
6923 shader_addline(buffer, "uniform vec4 specular_enable;\n");
6925 if (settings->sRGB_write)
6927 shader_addline(buffer, "const vec4 srgb_const0 = ");
6928 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
6929 shader_addline(buffer, ";\n");
6930 shader_addline(buffer, "const vec4 srgb_const1 = ");
6931 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
6932 shader_addline(buffer, ";\n");
6935 shader_addline(buffer, "uniform struct\n{\n");
6936 shader_addline(buffer, " vec4 color;\n");
6937 shader_addline(buffer, " float density;\n");
6938 shader_addline(buffer, " float end;\n");
6939 shader_addline(buffer, " float scale;\n");
6940 shader_addline(buffer, "} ffp_fog;\n");
6942 if (alpha_test_func != WINED3D_CMP_ALWAYS)
6943 shader_addline(buffer, "uniform float alpha_test_ref;\n");
6945 if (legacy_context)
6947 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6948 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6949 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6950 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6951 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6953 else
6955 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
6956 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
6957 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6958 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6959 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6962 shader_addline(buffer, "void main()\n{\n");
6964 if (legacy_context)
6966 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
6967 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
6970 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6972 if (tex_map & (1u << stage))
6974 if (settings->pointsprite)
6975 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
6976 else if (settings->texcoords_initialized & (1u << stage))
6977 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
6978 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
6979 else
6980 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
6984 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
6985 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6987 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
6988 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
6990 /* Generate texture sampling instructions */
6991 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
6993 const char *texture_function, *coord_mask;
6994 BOOL proj;
6996 if (!(tex_map & (1u << stage)))
6997 continue;
6999 if (settings->op[stage].projected == proj_none)
7001 proj = FALSE;
7003 else if (settings->op[stage].projected == proj_count4
7004 || settings->op[stage].projected == proj_count3)
7006 proj = TRUE;
7008 else
7010 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
7011 proj = TRUE;
7014 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
7015 proj = FALSE;
7017 switch (settings->op[stage].tex_type)
7019 case WINED3D_GL_RES_TYPE_TEX_1D:
7020 if (proj)
7022 texture_function = "texture1DProj";
7023 coord_mask = "xw";
7025 else
7027 texture_function = "texture1D";
7028 coord_mask = "x";
7030 break;
7031 case WINED3D_GL_RES_TYPE_TEX_2D:
7032 if (proj)
7034 texture_function = "texture2DProj";
7035 coord_mask = "xyw";
7037 else
7039 texture_function = "texture2D";
7040 coord_mask = "xy";
7042 break;
7043 case WINED3D_GL_RES_TYPE_TEX_3D:
7044 if (proj)
7046 texture_function = "texture3DProj";
7047 coord_mask = "xyzw";
7049 else
7051 texture_function = "texture3D";
7052 coord_mask = "xyz";
7054 break;
7055 case WINED3D_GL_RES_TYPE_TEX_CUBE:
7056 texture_function = "textureCube";
7057 coord_mask = "xyz";
7058 break;
7059 case WINED3D_GL_RES_TYPE_TEX_RECT:
7060 if (proj)
7062 texture_function = "texture2DRectProj";
7063 coord_mask = "xyw";
7065 else
7067 texture_function = "texture2DRect";
7068 coord_mask = "xy";
7070 break;
7071 default:
7072 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
7073 texture_function = "";
7074 coord_mask = "xyzw";
7075 break;
7077 if (!needs_legacy_glsl_syntax(gl_info))
7078 texture_function = proj ? "textureProj" : "texture";
7080 if (stage > 0
7081 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
7082 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
7084 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
7086 /* With projective textures, texbem only divides the static
7087 * texture coord, not the displacement, so multiply the
7088 * displacement with the dividing parameter before passing it to
7089 * TXP. */
7090 if (settings->op[stage].projected != proj_none)
7092 if (settings->op[stage].projected == proj_count4)
7094 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
7095 stage, stage);
7096 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
7098 else
7100 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
7101 stage, stage);
7102 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
7105 else
7107 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
7110 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
7111 stage, texture_function, stage, coord_mask);
7113 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
7114 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
7115 stage, stage - 1, stage - 1, stage - 1);
7117 else if (settings->op[stage].projected == proj_count3)
7119 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
7120 stage, texture_function, stage, stage);
7122 else
7124 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
7125 stage, texture_function, stage, stage, coord_mask);
7128 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
7129 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
7130 settings->op[stage].color_fixup);
7133 if (settings->color_key_enabled)
7135 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
7136 shader_addline(buffer, " discard;\n");
7139 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
7141 /* Generate the main shader */
7142 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7144 BOOL op_equal;
7146 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
7147 break;
7149 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
7150 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
7151 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
7152 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
7153 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
7154 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
7155 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
7156 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
7157 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
7158 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
7159 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
7160 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
7161 else
7162 op_equal = settings->op[stage].aop == settings->op[stage].cop
7163 && settings->op[stage].carg0 == settings->op[stage].aarg0
7164 && settings->op[stage].carg1 == settings->op[stage].aarg1
7165 && settings->op[stage].carg2 == settings->op[stage].aarg2;
7167 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
7169 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
7170 settings->op[stage].cop, settings->op[stage].carg0,
7171 settings->op[stage].carg1, settings->op[stage].carg2);
7173 else if (op_equal)
7175 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
7176 settings->op[stage].cop, settings->op[stage].carg0,
7177 settings->op[stage].carg1, settings->op[stage].carg2);
7179 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
7180 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
7182 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
7183 settings->op[stage].cop, settings->op[stage].carg0,
7184 settings->op[stage].carg1, settings->op[stage].carg2);
7185 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
7186 settings->op[stage].aop, settings->op[stage].aarg0,
7187 settings->op[stage].aarg1, settings->op[stage].aarg2);
7191 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
7192 get_fragment_output(gl_info));
7194 if (settings->sRGB_write)
7195 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7197 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
7199 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
7201 shader_addline(buffer, "}\n");
7203 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7204 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7206 string_buffer_release(&priv->string_buffers, tex_reg_name);
7207 return shader_id;
7210 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
7211 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
7213 struct glsl_ffp_vertex_shader *shader;
7214 const struct wine_rb_entry *entry;
7216 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
7217 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
7219 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
7220 return NULL;
7222 shader->desc.settings = *settings;
7223 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
7224 list_init(&shader->linked_programs);
7225 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
7226 ERR("Failed to insert ffp vertex shader.\n");
7228 return shader;
7231 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
7232 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
7234 struct glsl_ffp_fragment_shader *glsl_desc;
7235 const struct ffp_frag_desc *desc;
7237 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
7238 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
7240 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
7241 return NULL;
7243 glsl_desc->entry.settings = *args;
7244 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
7245 list_init(&glsl_desc->linked_programs);
7246 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
7248 return glsl_desc;
7252 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
7253 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
7255 unsigned int i;
7256 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7258 for (i = 0; i < vs_c_count; ++i)
7260 string_buffer_sprintf(name, "vs_c[%u]", i);
7261 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7263 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
7265 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
7267 string_buffer_sprintf(name, "vs_i[%u]", i);
7268 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7271 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
7273 string_buffer_sprintf(name, "vs_b[%u]", i);
7274 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7277 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
7279 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
7281 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
7282 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7284 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
7285 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
7286 for (i = 0; i < MAX_TEXTURES; ++i)
7288 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
7289 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7291 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
7292 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
7293 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
7294 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
7295 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
7296 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
7297 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
7299 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
7300 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7301 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
7302 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7303 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
7304 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7305 string_buffer_sprintf(name, "ffp_light[%u].position", i);
7306 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7307 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
7308 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7309 string_buffer_sprintf(name, "ffp_light[%u].range", i);
7310 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7311 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
7312 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7313 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
7314 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7315 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
7316 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7317 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
7318 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7319 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
7320 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7321 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
7322 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7324 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
7325 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
7326 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
7327 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
7328 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
7329 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
7331 string_buffer_release(&priv->string_buffers, name);
7334 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
7335 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
7337 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
7340 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
7341 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
7343 unsigned int i;
7344 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7346 for (i = 0; i < ps_c_count; ++i)
7348 string_buffer_sprintf(name, "ps_c[%u]", i);
7349 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7351 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
7353 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
7355 string_buffer_sprintf(name, "ps_i[%u]", i);
7356 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7359 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
7361 string_buffer_sprintf(name, "ps_b[%u]", i);
7362 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7365 for (i = 0; i < MAX_TEXTURES; ++i)
7367 string_buffer_sprintf(name, "bumpenv_mat%u", i);
7368 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7369 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
7370 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7371 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
7372 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7373 string_buffer_sprintf(name, "tss_const%u", i);
7374 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7377 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
7378 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
7380 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
7381 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
7382 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
7383 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
7385 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
7387 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
7388 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
7389 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
7391 string_buffer_release(&priv->string_buffers, name);
7394 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
7395 struct shader_glsl_priv *priv, GLuint program_id,
7396 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
7398 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
7399 GLuint block_idx;
7400 unsigned int i;
7401 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7403 for (i = 0; i < count; ++i)
7405 if (!reg_maps->cb_sizes[i])
7406 continue;
7408 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
7409 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
7410 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
7412 checkGLcall("glUniformBlockBinding");
7413 string_buffer_release(&priv->string_buffers, name);
7416 /* Context activation is done by the caller. */
7417 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
7418 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
7420 const struct wined3d_gl_info *gl_info = context->gl_info;
7421 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
7422 const struct ps_np2fixup_info *np2fixup_info = NULL;
7423 struct glsl_shader_prog_link *entry = NULL;
7424 struct wined3d_shader *vshader = NULL;
7425 struct wined3d_shader *gshader = NULL;
7426 struct wined3d_shader *pshader = NULL;
7427 GLuint program_id;
7428 GLuint reorder_shader_id = 0;
7429 unsigned int i;
7430 GLuint vs_id = 0;
7431 GLuint gs_id = 0;
7432 GLuint ps_id = 0;
7433 struct list *ps_list, *vs_list;
7434 WORD attribs_map;
7435 struct wined3d_string_buffer *tmp_name;
7437 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
7439 vs_id = ctx_data->glsl_program->vs.id;
7440 vs_list = &ctx_data->glsl_program->vs.shader_entry;
7442 if (use_vs(state))
7444 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7445 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
7447 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
7448 && ctx_data->glsl_program->gs.id)
7450 gs_id = ctx_data->glsl_program->gs.id;
7452 else if (gshader)
7454 struct gs_compile_args args;
7456 find_gs_compile_args(state, gshader, &args);
7457 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
7461 else if (use_vs(state))
7463 struct vs_compile_args vs_compile_args;
7465 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7466 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
7468 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, d3d_info);
7469 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
7470 vs_list = &vshader->linked_programs;
7472 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
7473 && ctx_data->glsl_program->gs.id)
7475 gs_id = ctx_data->glsl_program->gs.id;
7477 else if (gshader)
7479 struct gs_compile_args gs_compile_args;
7481 find_gs_compile_args(state, gshader, &gs_compile_args);
7482 gs_id = find_glsl_geometry_shader(context, priv, gshader, &gs_compile_args);
7485 else if (priv->vertex_pipe == &glsl_vertex_pipe)
7487 struct glsl_ffp_vertex_shader *ffp_shader;
7488 struct wined3d_ffp_vs_settings settings;
7490 wined3d_ffp_get_vs_settings(context, state, &settings);
7491 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
7492 vs_id = ffp_shader->id;
7493 vs_list = &ffp_shader->linked_programs;
7496 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
7498 ps_id = ctx_data->glsl_program->ps.id;
7499 ps_list = &ctx_data->glsl_program->ps.shader_entry;
7501 if (use_ps(state))
7502 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7504 else if (use_ps(state))
7506 struct ps_compile_args ps_compile_args;
7507 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7508 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
7509 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
7510 pshader, &ps_compile_args, &np2fixup_info);
7511 ps_list = &pshader->linked_programs;
7513 else if (priv->fragment_pipe == &glsl_fragment_pipe)
7515 struct glsl_ffp_fragment_shader *ffp_shader;
7516 struct ffp_frag_settings settings;
7518 gen_ffp_frag_op(context, state, &settings, FALSE);
7519 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
7520 ps_id = ffp_shader->id;
7521 ps_list = &ffp_shader->linked_programs;
7524 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
7526 ctx_data->glsl_program = entry;
7527 return;
7530 /* If we get to this point, then no matching program exists, so we create one */
7531 program_id = GL_EXTCALL(glCreateProgram());
7532 TRACE("Created new GLSL shader program %u.\n", program_id);
7534 /* Create the entry */
7535 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
7536 entry->id = program_id;
7537 entry->vs.id = vs_id;
7538 entry->gs.id = gs_id;
7539 entry->ps.id = ps_id;
7540 entry->constant_version = 0;
7541 entry->ps.np2_fixup_info = np2fixup_info;
7542 /* Add the hash table entry */
7543 add_glsl_program_entry(priv, entry);
7545 /* Set the current program */
7546 ctx_data->glsl_program = entry;
7548 /* Attach GLSL vshader */
7549 if (vs_id)
7551 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
7552 GL_EXTCALL(glAttachShader(program_id, vs_id));
7553 checkGLcall("glAttachShader");
7555 list_add_head(vs_list, &entry->vs.shader_entry);
7558 if (vshader)
7560 attribs_map = vshader->reg_maps.input_registers;
7561 if (vshader->reg_maps.shader_version.major < 4)
7563 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
7564 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
7565 d3d_info->emulated_flatshading
7566 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
7567 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
7568 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
7569 checkGLcall("glAttachShader");
7570 /* Flag the reorder function for deletion, it will be freed
7571 * automatically when the program is destroyed. */
7572 GL_EXTCALL(glDeleteShader(reorder_shader_id));
7575 else
7577 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
7580 /* Bind vertex attributes to a corresponding index number to match
7581 * the same index numbers as ARB_vertex_programs (makes loading
7582 * vertex attributes simpler). With this method, we can use the
7583 * exact same code to load the attributes later for both ARB and
7584 * GLSL shaders.
7586 * We have to do this here because we need to know the Program ID
7587 * in order to make the bindings work, and it has to be done prior
7588 * to linking the GLSL program. */
7589 tmp_name = string_buffer_get(&priv->string_buffers);
7590 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
7592 if (!(attribs_map & 1))
7593 continue;
7595 string_buffer_sprintf(tmp_name, "vs_in%u", i);
7596 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
7598 checkGLcall("glBindAttribLocation");
7599 string_buffer_release(&priv->string_buffers, tmp_name);
7601 if (gshader)
7603 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
7604 GL_EXTCALL(glAttachShader(program_id, gs_id));
7605 checkGLcall("glAttachShader");
7607 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
7609 TRACE("input type %s, output type %s, vertices out %u.\n",
7610 debug_d3dprimitivetype(gshader->u.gs.input_type),
7611 debug_d3dprimitivetype(gshader->u.gs.output_type),
7612 gshader->u.gs.vertices_out);
7613 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
7614 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
7615 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
7616 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
7617 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
7618 gshader->u.gs.vertices_out));
7619 checkGLcall("glProgramParameteriARB");
7622 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
7625 /* Attach GLSL pshader */
7626 if (ps_id)
7628 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
7629 GL_EXTCALL(glAttachShader(program_id, ps_id));
7630 checkGLcall("glAttachShader");
7632 list_add_head(ps_list, &entry->ps.shader_entry);
7635 /* Link the program */
7636 TRACE("Linking GLSL shader program %u.\n", program_id);
7637 GL_EXTCALL(glLinkProgram(program_id));
7638 shader_glsl_validate_link(gl_info, program_id);
7640 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
7641 vshader ? vshader->limits->constant_float : 0);
7642 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
7643 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
7644 pshader ? pshader->limits->constant_float : 0);
7645 checkGLcall("Find glsl program uniform locations");
7647 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
7649 if (pshader && pshader->reg_maps.shader_version.major >= 3
7650 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
7652 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
7653 entry->vs.vertex_color_clamp = GL_FALSE;
7655 else
7657 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7660 else
7662 /* With core profile we never change vertex_color_clamp from
7663 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
7664 * glClampColorARB(). */
7665 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7668 /* Set the shader to allow uniform loading on it */
7669 GL_EXTCALL(glUseProgram(program_id));
7670 checkGLcall("glUseProgram");
7672 /* Texture unit mapping is set up to be the same each time the shader
7673 * program is used so we can hardcode the sampler uniform values. */
7674 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
7676 entry->constant_update_mask = 0;
7677 if (vshader)
7679 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
7680 if (vshader->reg_maps.integer_constants)
7681 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
7682 if (vshader->reg_maps.boolean_constants)
7683 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
7684 if (entry->vs.pos_fixup_location != -1)
7685 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
7687 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps,
7688 0, gl_info->limits.vertex_uniform_blocks);
7690 else
7692 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
7693 | WINED3D_SHADER_CONST_FFP_PROJ;
7695 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
7697 if (entry->vs.modelview_matrix_location[i] != -1)
7699 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
7700 break;
7704 for (i = 0; i < MAX_TEXTURES; ++i)
7706 if (entry->vs.texture_matrix_location[i] != -1)
7708 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7709 break;
7712 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
7713 || entry->vs.material_specular_location != -1
7714 || entry->vs.material_emissive_location != -1
7715 || entry->vs.material_shininess_location != -1)
7716 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
7717 if (entry->vs.light_ambient_location != -1)
7718 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
7720 if (entry->vs.pointsize_min_location != -1)
7721 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
7723 if (gshader)
7725 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
7726 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps,
7727 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
7730 if (ps_id)
7732 if (pshader)
7734 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
7735 if (pshader->reg_maps.integer_constants)
7736 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
7737 if (pshader->reg_maps.boolean_constants)
7738 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
7739 if (entry->ps.ycorrection_location != -1)
7740 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
7742 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps,
7743 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
7744 gl_info->limits.fragment_uniform_blocks);
7746 else
7748 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7751 for (i = 0; i < MAX_TEXTURES; ++i)
7753 if (entry->ps.bumpenv_mat_location[i] != -1)
7755 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
7756 break;
7760 if (entry->ps.fog_color_location != -1)
7761 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
7762 if (entry->ps.alpha_test_ref_location != -1)
7763 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
7764 if (entry->ps.np2_fixup_location != -1)
7765 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
7766 if (entry->ps.color_key_location != -1)
7767 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
7771 /* Context activation is done by the caller. */
7772 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum wined3d_gl_resource_type tex_type,
7773 BOOL masked)
7775 GLuint program_id;
7776 GLuint vshader_id, pshader_id;
7777 const char *blt_pshader;
7779 static const char blt_vshader[] =
7780 "#version 120\n"
7781 "void main(void)\n"
7782 "{\n"
7783 " gl_Position = gl_Vertex;\n"
7784 " gl_FrontColor = vec4(1.0);\n"
7785 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
7786 "}\n";
7788 static const char * const blt_pshaders_full[WINED3D_GL_RES_TYPE_COUNT] =
7790 /* WINED3D_GL_RES_TYPE_TEX_1D */
7791 NULL,
7792 /* WINED3D_GL_RES_TYPE_TEX_2D */
7793 "#version 120\n"
7794 "uniform sampler2D sampler;\n"
7795 "void main(void)\n"
7796 "{\n"
7797 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7798 "}\n",
7799 /* WINED3D_GL_RES_TYPE_TEX_3D */
7800 NULL,
7801 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7802 "#version 120\n"
7803 "uniform samplerCube sampler;\n"
7804 "void main(void)\n"
7805 "{\n"
7806 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7807 "}\n",
7808 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7809 "#version 120\n"
7810 "#extension GL_ARB_texture_rectangle : enable\n"
7811 "uniform sampler2DRect sampler;\n"
7812 "void main(void)\n"
7813 "{\n"
7814 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7815 "}\n",
7816 /* WINED3D_GL_RES_TYPE_BUFFER */
7817 NULL,
7818 /* WINED3D_GL_RES_TYPE_RB */
7819 NULL,
7822 static const char * const blt_pshaders_masked[WINED3D_GL_RES_TYPE_COUNT] =
7824 /* WINED3D_GL_RES_TYPE_TEX_1D */
7825 NULL,
7826 /* WINED3D_GL_RES_TYPE_TEX_2D */
7827 "#version 120\n"
7828 "uniform sampler2D sampler;\n"
7829 "uniform vec4 mask;\n"
7830 "void main(void)\n"
7831 "{\n"
7832 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7833 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7834 "}\n",
7835 /* WINED3D_GL_RES_TYPE_TEX_3D */
7836 NULL,
7837 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7838 "#version 120\n"
7839 "uniform samplerCube sampler;\n"
7840 "uniform vec4 mask;\n"
7841 "void main(void)\n"
7842 "{\n"
7843 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7844 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7845 "}\n",
7846 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7847 "#version 120\n"
7848 "#extension GL_ARB_texture_rectangle : enable\n"
7849 "uniform sampler2DRect sampler;\n"
7850 "uniform vec4 mask;\n"
7851 "void main(void)\n"
7852 "{\n"
7853 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7854 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7855 "}\n",
7856 /* WINED3D_GL_RES_TYPE_BUFFER */
7857 NULL,
7858 /* WINED3D_GL_RES_TYPE_RB */
7859 NULL,
7862 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
7863 if (!blt_pshader)
7865 FIXME("tex_type %#x not supported\n", tex_type);
7866 return 0;
7869 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7870 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
7872 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7873 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
7875 program_id = GL_EXTCALL(glCreateProgram());
7876 GL_EXTCALL(glAttachShader(program_id, vshader_id));
7877 GL_EXTCALL(glAttachShader(program_id, pshader_id));
7878 GL_EXTCALL(glLinkProgram(program_id));
7880 shader_glsl_validate_link(gl_info, program_id);
7882 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
7883 * is destroyed
7885 GL_EXTCALL(glDeleteShader(vshader_id));
7886 GL_EXTCALL(glDeleteShader(pshader_id));
7887 return program_id;
7890 /* Context activation is done by the caller. */
7891 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
7892 const struct wined3d_state *state)
7894 struct glsl_context_data *ctx_data = context->shader_backend_data;
7895 const struct wined3d_gl_info *gl_info = context->gl_info;
7896 struct shader_glsl_priv *priv = shader_priv;
7897 GLuint program_id = 0, prev_id = 0;
7898 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
7900 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
7901 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
7903 if (ctx_data->glsl_program)
7905 prev_id = ctx_data->glsl_program->id;
7906 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7908 else
7910 prev_id = 0;
7911 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7914 set_glsl_shader_program(context, state, priv, ctx_data);
7916 if (ctx_data->glsl_program)
7918 program_id = ctx_data->glsl_program->id;
7919 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7921 else
7923 program_id = 0;
7924 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7927 if (old_vertex_color_clamp != current_vertex_color_clamp)
7929 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7931 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
7932 checkGLcall("glClampColorARB");
7934 else
7936 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
7940 TRACE("Using GLSL program %u.\n", program_id);
7942 if (prev_id != program_id)
7944 GL_EXTCALL(glUseProgram(program_id));
7945 checkGLcall("glUseProgram");
7947 if (program_id)
7948 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
7952 /* "context" is not necessarily the currently active context. */
7953 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
7955 struct glsl_context_data *ctx_data = context->shader_backend_data;
7957 ctx_data->glsl_program = NULL;
7958 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
7959 | (1u << WINED3D_SHADER_TYPE_VERTEX)
7960 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
7961 | (1u << WINED3D_SHADER_TYPE_HULL)
7962 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
7965 /* Context activation is done by the caller. */
7966 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
7968 const struct wined3d_gl_info *gl_info = context->gl_info;
7969 struct shader_glsl_priv *priv = shader_priv;
7971 shader_glsl_invalidate_current_program(context);
7972 GL_EXTCALL(glUseProgram(0));
7973 checkGLcall("glUseProgram");
7975 priv->vertex_pipe->vp_enable(gl_info, FALSE);
7976 priv->fragment_pipe->enable_extension(gl_info, FALSE);
7978 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7980 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
7981 checkGLcall("glClampColorARB");
7985 /* Context activation is done by the caller. */
7986 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
7987 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size)
7989 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
7990 struct shader_glsl_priv *priv = shader_priv;
7991 GLuint *blt_program;
7992 GLint loc;
7994 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
7995 if (!*blt_program)
7997 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
7998 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
7999 GL_EXTCALL(glUseProgram(*blt_program));
8000 GL_EXTCALL(glUniform1i(loc, 0));
8002 else
8004 GL_EXTCALL(glUseProgram(*blt_program));
8007 if (masked)
8009 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
8010 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
8014 /* Context activation is done by the caller. */
8015 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
8017 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
8018 GLuint program_id;
8020 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
8021 if (program_id) TRACE("Using GLSL program %u\n", program_id);
8023 GL_EXTCALL(glUseProgram(program_id));
8024 checkGLcall("glUseProgram");
8027 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
8028 const struct glsl_shader_prog_link *program)
8030 const struct glsl_context_data *ctx_data;
8031 struct wined3d_context *context;
8032 unsigned int i;
8034 for (i = 0; i < device->context_count; ++i)
8036 context = device->contexts[i];
8037 ctx_data = context->shader_backend_data;
8039 if (ctx_data->glsl_program == program)
8040 shader_glsl_invalidate_current_program(context);
8044 static void shader_glsl_destroy(struct wined3d_shader *shader)
8046 struct glsl_shader_private *shader_data = shader->backend_data;
8047 struct wined3d_device *device = shader->device;
8048 struct shader_glsl_priv *priv = device->shader_priv;
8049 const struct wined3d_gl_info *gl_info;
8050 const struct list *linked_programs;
8051 struct wined3d_context *context;
8053 if (!shader_data || !shader_data->num_gl_shaders)
8055 HeapFree(GetProcessHeap(), 0, shader_data);
8056 shader->backend_data = NULL;
8057 return;
8060 context = context_acquire(device, NULL);
8061 gl_info = context->gl_info;
8063 TRACE("Deleting linked programs.\n");
8064 linked_programs = &shader->linked_programs;
8065 if (linked_programs->next)
8067 struct glsl_shader_prog_link *entry, *entry2;
8068 UINT i;
8070 switch (shader->reg_maps.shader_version.type)
8072 case WINED3D_SHADER_TYPE_PIXEL:
8074 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
8076 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8078 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
8079 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8080 checkGLcall("glDeleteShader");
8082 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
8084 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8085 struct glsl_shader_prog_link, ps.shader_entry)
8087 shader_glsl_invalidate_contexts_program(device, entry);
8088 delete_glsl_program_entry(priv, gl_info, entry);
8091 break;
8094 case WINED3D_SHADER_TYPE_VERTEX:
8096 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
8098 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8100 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
8101 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8102 checkGLcall("glDeleteShader");
8104 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
8106 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8107 struct glsl_shader_prog_link, vs.shader_entry)
8109 shader_glsl_invalidate_contexts_program(device, entry);
8110 delete_glsl_program_entry(priv, gl_info, entry);
8113 break;
8116 case WINED3D_SHADER_TYPE_GEOMETRY:
8118 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
8120 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8122 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
8123 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8124 checkGLcall("glDeleteShader");
8126 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
8128 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8129 struct glsl_shader_prog_link, gs.shader_entry)
8131 shader_glsl_invalidate_contexts_program(device, entry);
8132 delete_glsl_program_entry(priv, gl_info, entry);
8135 break;
8138 default:
8139 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8140 break;
8144 HeapFree(GetProcessHeap(), 0, shader->backend_data);
8145 shader->backend_data = NULL;
8147 context_release(context);
8150 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
8152 const struct glsl_program_key *k = key;
8153 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
8154 const struct glsl_shader_prog_link, program_lookup_entry);
8156 if (k->vs_id > prog->vs.id) return 1;
8157 else if (k->vs_id < prog->vs.id) return -1;
8159 if (k->gs_id > prog->gs.id) return 1;
8160 else if (k->gs_id < prog->gs.id) return -1;
8162 if (k->ps_id > prog->ps.id) return 1;
8163 else if (k->ps_id < prog->ps.id) return -1;
8165 return 0;
8168 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
8170 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
8171 + constant_count * sizeof(*heap->contained)
8172 + constant_count * sizeof(*heap->positions);
8173 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
8175 if (!mem)
8177 ERR("Failed to allocate memory\n");
8178 return FALSE;
8181 heap->entries = mem;
8182 heap->entries[1].version = 0;
8183 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
8184 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
8185 heap->positions = (unsigned int *)(heap->contained + constant_count);
8186 heap->size = 1;
8188 return TRUE;
8191 static void constant_heap_free(struct constant_heap *heap)
8193 HeapFree(GetProcessHeap(), 0, heap->entries);
8196 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
8198 wined3d_rb_alloc,
8199 wined3d_rb_realloc,
8200 wined3d_rb_free,
8201 glsl_program_key_compare,
8204 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
8205 const struct fragment_pipeline *fragment_pipe)
8207 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
8208 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
8209 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
8210 struct fragment_caps fragment_caps;
8211 void *vertex_priv, *fragment_priv;
8213 string_buffer_list_init(&priv->string_buffers);
8215 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
8217 ERR("Failed to initialize vertex pipe.\n");
8218 HeapFree(GetProcessHeap(), 0, priv);
8219 return E_FAIL;
8222 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
8224 ERR("Failed to initialize fragment pipe.\n");
8225 vertex_pipe->vp_free(device);
8226 HeapFree(GetProcessHeap(), 0, priv);
8227 return E_FAIL;
8230 if (!string_buffer_init(&priv->shader_buffer))
8232 ERR("Failed to initialize shader buffer.\n");
8233 goto fail;
8236 if (!(priv->stack = wined3d_calloc(stack_size, sizeof(*priv->stack))))
8238 ERR("Failed to allocate memory.\n");
8239 goto fail;
8242 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
8244 ERR("Failed to initialize vertex shader constant heap\n");
8245 goto fail;
8248 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
8250 ERR("Failed to initialize pixel shader constant heap\n");
8251 goto fail;
8254 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
8256 ERR("Failed to initialize rbtree.\n");
8257 goto fail;
8260 priv->next_constant_version = 1;
8261 priv->vertex_pipe = vertex_pipe;
8262 priv->fragment_pipe = fragment_pipe;
8263 fragment_pipe->get_caps(gl_info, &fragment_caps);
8264 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
8265 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
8267 device->vertex_priv = vertex_priv;
8268 device->fragment_priv = fragment_priv;
8269 device->shader_priv = priv;
8271 return WINED3D_OK;
8273 fail:
8274 constant_heap_free(&priv->pconst_heap);
8275 constant_heap_free(&priv->vconst_heap);
8276 HeapFree(GetProcessHeap(), 0, priv->stack);
8277 string_buffer_free(&priv->shader_buffer);
8278 fragment_pipe->free_private(device);
8279 vertex_pipe->vp_free(device);
8280 HeapFree(GetProcessHeap(), 0, priv);
8281 return E_OUTOFMEMORY;
8284 /* Context activation is done by the caller. */
8285 static void shader_glsl_free(struct wined3d_device *device)
8287 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
8288 struct shader_glsl_priv *priv = device->shader_priv;
8289 int i;
8291 for (i = 0; i < WINED3D_GL_RES_TYPE_COUNT; ++i)
8293 if (priv->depth_blt_program_full[i])
8295 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
8297 if (priv->depth_blt_program_masked[i])
8299 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
8303 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
8304 constant_heap_free(&priv->pconst_heap);
8305 constant_heap_free(&priv->vconst_heap);
8306 HeapFree(GetProcessHeap(), 0, priv->stack);
8307 string_buffer_list_cleanup(&priv->string_buffers);
8308 string_buffer_free(&priv->shader_buffer);
8309 priv->fragment_pipe->free_private(device);
8310 priv->vertex_pipe->vp_free(device);
8312 HeapFree(GetProcessHeap(), 0, device->shader_priv);
8313 device->shader_priv = NULL;
8316 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
8318 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
8319 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
8322 static void shader_glsl_free_context_data(struct wined3d_context *context)
8324 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
8327 static void shader_glsl_init_context_state(struct wined3d_context *context)
8329 const struct wined3d_gl_info *gl_info = context->gl_info;
8331 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
8332 checkGLcall("GL_PROGRAM_POINT_SIZE");
8335 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
8337 UINT shader_model;
8339 /* FIXME: Check for the specific extensions required for SM5 support
8340 * (ARB_compute_shader, ARB_tessellation_shader, ARB_gpu_shader5, ...) as
8341 * soon as we introduce them, adjusting the GL / GLSL version checks
8342 * accordingly. */
8343 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 30) && gl_info->supported[WINED3D_GL_VERSION_4_3])
8344 shader_model = 5;
8345 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50) && gl_info->supported[WINED3D_GL_VERSION_3_2]
8346 && gl_info->supported[ARB_SHADER_BIT_ENCODING] && gl_info->supported[ARB_SAMPLER_OBJECTS]
8347 && gl_info->supported[ARB_TEXTURE_SWIZZLE])
8348 shader_model = 4;
8349 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
8350 * texldd and texldl instructions. */
8351 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
8352 shader_model = 3;
8353 else
8354 shader_model = 2;
8355 TRACE("Shader model %u.\n", shader_model);
8357 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
8358 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
8359 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
8360 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
8361 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
8363 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
8364 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
8365 caps->varying_count = gl_info->limits.glsl_varyings;
8367 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
8368 * Direct3D minimum requirement.
8370 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
8371 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
8373 * The problem is that the refrast clamps temporary results in the shader to
8374 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
8375 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
8376 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
8377 * offer a way to query this.
8379 if (shader_model >= 4)
8380 caps->ps_1x_max_value = FLT_MAX;
8381 else
8382 caps->ps_1x_max_value = 1024.0f;
8384 /* Ideally we'd only set caps like sRGB writes here if supported by both
8385 * the shader backend and the fragment pipe, but we can get called before
8386 * shader_glsl_alloc(). */
8387 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
8388 | WINED3D_SHADER_CAP_SRGB_WRITE;
8391 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
8393 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
8395 TRACE("Checking support for fixup:\n");
8396 dump_color_fixup_desc(fixup);
8399 /* We support everything except YUV conversions. */
8400 if (!is_complex_fixup(fixup))
8402 TRACE("[OK]\n");
8403 return TRUE;
8406 TRACE("[FAILED]\n");
8407 return FALSE;
8410 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
8412 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
8413 /* WINED3DSIH_ADD */ shader_glsl_binop,
8414 /* WINED3DSIH_AND */ shader_glsl_binop,
8415 /* WINED3DSIH_BEM */ shader_glsl_bem,
8416 /* WINED3DSIH_BREAK */ shader_glsl_break,
8417 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
8418 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
8419 /* WINED3DSIH_CALL */ shader_glsl_call,
8420 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
8421 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
8422 /* WINED3DSIH_CND */ shader_glsl_cnd,
8423 /* WINED3DSIH_CRS */ shader_glsl_cross,
8424 /* WINED3DSIH_CUT */ shader_glsl_cut,
8425 /* WINED3DSIH_DCL */ shader_glsl_nop,
8426 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
8427 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
8428 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ NULL,
8429 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ NULL,
8430 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ NULL,
8431 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
8432 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ NULL,
8433 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
8434 /* WINED3DSIH_DCL_INPUT_PS */ NULL,
8435 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
8436 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
8437 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
8438 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
8439 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
8440 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ NULL,
8441 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
8442 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
8443 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ NULL,
8444 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
8445 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
8446 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ NULL,
8447 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ NULL,
8448 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ NULL,
8449 /* WINED3DSIH_DCL_UAV_TYPED */ NULL,
8450 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
8451 /* WINED3DSIH_DEF */ shader_glsl_nop,
8452 /* WINED3DSIH_DEFB */ shader_glsl_nop,
8453 /* WINED3DSIH_DEFI */ shader_glsl_nop,
8454 /* WINED3DSIH_DIV */ shader_glsl_binop,
8455 /* WINED3DSIH_DP2 */ shader_glsl_dot,
8456 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
8457 /* WINED3DSIH_DP3 */ shader_glsl_dot,
8458 /* WINED3DSIH_DP4 */ shader_glsl_dot,
8459 /* WINED3DSIH_DST */ shader_glsl_dst,
8460 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
8461 /* WINED3DSIH_DSX_COARSE */ NULL,
8462 /* WINED3DSIH_DSX_FINE */ NULL,
8463 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
8464 /* WINED3DSIH_DSY_COARSE */ NULL,
8465 /* WINED3DSIH_DSY_FINE */ NULL,
8466 /* WINED3DSIH_ELSE */ shader_glsl_else,
8467 /* WINED3DSIH_EMIT */ shader_glsl_emit,
8468 /* WINED3DSIH_ENDIF */ shader_glsl_end,
8469 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
8470 /* WINED3DSIH_ENDREP */ shader_glsl_end,
8471 /* WINED3DSIH_EQ */ shader_glsl_relop,
8472 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
8473 /* WINED3DSIH_EXPP */ shader_glsl_expp,
8474 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
8475 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
8476 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
8477 /* WINED3DSIH_GE */ shader_glsl_relop,
8478 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ NULL,
8479 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
8480 /* WINED3DSIH_HS_FORK_PHASE */ NULL,
8481 /* WINED3DSIH_HS_JOIN_PHASE */ NULL,
8482 /* WINED3DSIH_IADD */ shader_glsl_binop,
8483 /* WINED3DSIH_IEQ */ shader_glsl_relop,
8484 /* WINED3DSIH_IF */ shader_glsl_if,
8485 /* WINED3DSIH_IFC */ shader_glsl_ifc,
8486 /* WINED3DSIH_IGE */ shader_glsl_relop,
8487 /* WINED3DSIH_ILT */ shader_glsl_relop,
8488 /* WINED3DSIH_IMAD */ shader_glsl_mad,
8489 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
8490 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
8491 /* WINED3DSIH_IMUL */ shader_glsl_imul,
8492 /* WINED3DSIH_INE */ shader_glsl_relop,
8493 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
8494 /* WINED3DSIH_ISHL */ shader_glsl_binop,
8495 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
8496 /* WINED3DSIH_LABEL */ shader_glsl_label,
8497 /* WINED3DSIH_LD */ shader_glsl_ld,
8498 /* WINED3DSIH_LD2DMS */ NULL,
8499 /* WINED3DSIH_LD_STRUCTURED */ NULL,
8500 /* WINED3DSIH_LIT */ shader_glsl_lit,
8501 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
8502 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
8503 /* WINED3DSIH_LOOP */ shader_glsl_loop,
8504 /* WINED3DSIH_LRP */ shader_glsl_lrp,
8505 /* WINED3DSIH_LT */ shader_glsl_relop,
8506 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
8507 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
8508 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
8509 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
8510 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
8511 /* WINED3DSIH_MAD */ shader_glsl_mad,
8512 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
8513 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
8514 /* WINED3DSIH_MOV */ shader_glsl_mov,
8515 /* WINED3DSIH_MOVA */ shader_glsl_mov,
8516 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
8517 /* WINED3DSIH_MUL */ shader_glsl_binop,
8518 /* WINED3DSIH_NE */ shader_glsl_relop,
8519 /* WINED3DSIH_NOP */ shader_glsl_nop,
8520 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
8521 /* WINED3DSIH_NRM */ shader_glsl_nrm,
8522 /* WINED3DSIH_OR */ shader_glsl_binop,
8523 /* WINED3DSIH_PHASE */ shader_glsl_nop,
8524 /* WINED3DSIH_POW */ shader_glsl_pow,
8525 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
8526 /* WINED3DSIH_REP */ shader_glsl_rep,
8527 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
8528 /* WINED3DSIH_RET */ shader_glsl_ret,
8529 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
8530 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
8531 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
8532 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
8533 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
8534 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
8535 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
8536 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
8537 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
8538 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
8539 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
8540 /* WINED3DSIH_SETP */ NULL,
8541 /* WINED3DSIH_SGE */ shader_glsl_compare,
8542 /* WINED3DSIH_SGN */ shader_glsl_sgn,
8543 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
8544 /* WINED3DSIH_SLT */ shader_glsl_compare,
8545 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
8546 /* WINED3DSIH_STORE_UAV_TYPED */ NULL,
8547 /* WINED3DSIH_SUB */ shader_glsl_binop,
8548 /* WINED3DSIH_TEX */ shader_glsl_tex,
8549 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
8550 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
8551 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
8552 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
8553 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
8554 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
8555 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
8556 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
8557 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
8558 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
8559 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
8560 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
8561 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
8562 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
8563 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
8564 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
8565 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
8566 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
8567 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
8568 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
8569 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
8570 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
8571 /* WINED3DSIH_UGE */ shader_glsl_relop,
8572 /* WINED3DSIH_ULT */ shader_glsl_relop,
8573 /* WINED3DSIH_USHR */ shader_glsl_binop,
8574 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
8575 /* WINED3DSIH_XOR */ shader_glsl_binop,
8578 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
8579 SHADER_HANDLER hw_fct;
8581 /* Select handler */
8582 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
8584 /* Unhandled opcode */
8585 if (!hw_fct)
8587 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
8588 return;
8590 hw_fct(ins);
8592 shader_glsl_add_instruction_modifiers(ins);
8595 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
8597 struct shader_glsl_priv *priv = shader_priv;
8599 return priv->ffp_proj_control;
8602 const struct wined3d_shader_backend_ops glsl_shader_backend =
8604 shader_glsl_handle_instruction,
8605 shader_glsl_select,
8606 shader_glsl_disable,
8607 shader_glsl_select_depth_blt,
8608 shader_glsl_deselect_depth_blt,
8609 shader_glsl_update_float_vertex_constants,
8610 shader_glsl_update_float_pixel_constants,
8611 shader_glsl_load_constants,
8612 shader_glsl_destroy,
8613 shader_glsl_alloc,
8614 shader_glsl_free,
8615 shader_glsl_allocate_context_data,
8616 shader_glsl_free_context_data,
8617 shader_glsl_init_context_state,
8618 shader_glsl_get_caps,
8619 shader_glsl_color_fixup_supported,
8620 shader_glsl_has_ffp_proj_control,
8623 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
8625 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
8627 caps->xyzrhw = TRUE;
8628 caps->emulated_flatshading = !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
8629 caps->ffp_generic_attributes = TRUE;
8630 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
8631 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
8632 caps->max_vertex_blend_matrix_index = 0;
8633 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
8634 | WINED3DVTXPCAPS_MATERIALSOURCE7
8635 | WINED3DVTXPCAPS_VERTEXFOG
8636 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
8637 | WINED3DVTXPCAPS_POSITIONALLIGHTS
8638 | WINED3DVTXPCAPS_LOCALVIEWER
8639 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
8640 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
8641 caps->max_user_clip_planes = gl_info->limits.clipplanes;
8642 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
8645 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
8647 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8648 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8649 return 0;
8652 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8654 struct shader_glsl_priv *priv;
8656 if (shader_backend == &glsl_shader_backend)
8658 priv = shader_priv;
8660 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
8662 ERR("Failed to initialize rbtree.\n");
8663 return NULL;
8666 return priv;
8669 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
8671 return NULL;
8674 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
8676 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8677 struct glsl_ffp_vertex_shader, desc.entry);
8678 struct glsl_shader_prog_link *program, *program2;
8679 struct glsl_ffp_destroy_ctx *ctx = context;
8681 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8682 struct glsl_shader_prog_link, vs.shader_entry)
8684 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8686 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8687 HeapFree(GetProcessHeap(), 0, shader);
8690 /* Context activation is done by the caller. */
8691 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
8693 struct shader_glsl_priv *priv = device->vertex_priv;
8694 struct glsl_ffp_destroy_ctx ctx;
8696 ctx.priv = priv;
8697 ctx.gl_info = &device->adapter->gl_info;
8698 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
8701 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
8702 const struct wined3d_state *state, DWORD state_id) {}
8704 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
8705 const struct wined3d_state *state, DWORD state_id)
8707 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8710 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
8711 const struct wined3d_state *state, DWORD state_id)
8713 const struct wined3d_gl_info *gl_info = context->gl_info;
8714 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
8715 BOOL transformed = context->stream_info.position_transformed;
8716 BOOL wasrhw = context->last_was_rhw;
8717 unsigned int i;
8719 context->last_was_rhw = transformed;
8721 /* If the vertex declaration contains a transformed position attribute,
8722 * the draw uses the fixed function vertex pipeline regardless of any
8723 * vertex shader set by the application. */
8724 if (transformed != wasrhw)
8725 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8727 if (!use_vs(state))
8729 if (context->last_was_vshader)
8731 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8732 clipplane(context, state, STATE_CLIPPLANE(i));
8735 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8737 /* Because of settings->texcoords, we have to regenerate the vertex
8738 * shader on a vdecl change if there aren't enough varyings to just
8739 * always output all the texture coordinates. */
8740 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
8741 || normal != context->last_was_normal)
8742 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8744 if (use_ps(state)
8745 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
8746 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
8747 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8749 else
8751 if (!context->last_was_vshader)
8753 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
8754 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8755 clipplane(context, state, STATE_CLIPPLANE(i));
8759 context->last_was_vshader = use_vs(state);
8760 context->last_was_normal = normal;
8763 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
8764 const struct wined3d_state *state, DWORD state_id)
8766 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8767 /* Different vertex shaders potentially require a different vertex attributes setup. */
8768 if (!isStateDirty(context, STATE_VDECL))
8769 context_apply_state(context, state, STATE_VDECL);
8772 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
8773 const struct wined3d_state *state, DWORD state_id)
8775 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
8776 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
8777 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8780 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
8781 const struct wined3d_state *state, DWORD state_id)
8783 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
8784 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
8785 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
8786 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
8787 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8790 static void glsl_vertex_pipe_world(struct wined3d_context *context,
8791 const struct wined3d_state *state, DWORD state_id)
8793 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
8796 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
8797 const struct wined3d_state *state, DWORD state_id)
8799 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8802 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
8804 const struct wined3d_gl_info *gl_info = context->gl_info;
8805 unsigned int k;
8807 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
8808 | WINED3D_SHADER_CONST_FFP_LIGHTS
8809 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8811 for (k = 0; k < gl_info->limits.clipplanes; ++k)
8813 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
8814 clipplane(context, state, STATE_CLIPPLANE(k));
8818 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
8819 const struct wined3d_state *state, DWORD state_id)
8821 /* Table fog behavior depends on the projection matrix. */
8822 if (state->render_states[WINED3D_RS_FOGENABLE]
8823 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
8824 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8825 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
8828 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
8829 const struct wined3d_state *state, DWORD state_id)
8831 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
8832 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
8833 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
8834 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
8835 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8836 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
8839 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
8840 const struct wined3d_state *state, DWORD state_id)
8842 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8845 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
8846 const struct wined3d_state *state, DWORD state_id)
8848 DWORD sampler = state_id - STATE_SAMPLER(0);
8849 const struct wined3d_texture *texture = state->textures[sampler];
8850 BOOL np2;
8852 if (!texture)
8853 return;
8855 if (sampler >= MAX_TEXTURES)
8856 return;
8858 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
8859 || context->lastWasPow2Texture & (1u << sampler))
8861 if (np2)
8862 context->lastWasPow2Texture |= 1u << sampler;
8863 else
8864 context->lastWasPow2Texture &= ~(1u << sampler);
8866 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8870 static void glsl_vertex_pipe_material(struct wined3d_context *context,
8871 const struct wined3d_state *state, DWORD state_id)
8873 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
8876 static void glsl_vertex_pipe_light(struct wined3d_context *context,
8877 const struct wined3d_state *state, DWORD state_id)
8879 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
8882 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
8883 const struct wined3d_state *state, DWORD state_id)
8885 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8888 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
8889 const struct wined3d_state *state, DWORD state_id)
8891 if (!use_vs(state))
8892 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8895 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
8896 const struct wined3d_state *state, DWORD state_id)
8898 static unsigned int once;
8900 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
8901 FIXME("Non-point sprite points not supported in core profile.\n");
8904 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
8905 const struct wined3d_state *state, DWORD state_id)
8907 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
8910 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
8912 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8913 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
8914 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
8915 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
8916 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
8917 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
8918 /* Clip planes */
8919 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
8920 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
8921 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
8922 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
8923 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
8924 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
8925 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
8926 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
8927 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
8928 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
8929 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
8930 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
8931 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
8932 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
8933 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
8934 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
8935 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
8936 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
8937 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
8938 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
8939 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
8940 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
8941 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
8942 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
8943 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
8944 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
8945 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
8946 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
8947 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
8948 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
8949 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
8950 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
8951 /* Lights */
8952 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8953 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8954 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8955 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8956 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8957 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8958 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8959 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8960 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8961 /* Viewport */
8962 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
8963 /* Transform states */
8964 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
8965 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
8966 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8967 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8968 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8969 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8970 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8971 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8972 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8973 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8974 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
8975 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8976 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8977 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8978 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8979 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8980 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8981 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8982 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8983 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8984 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8985 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8986 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8987 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8988 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8989 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8990 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8991 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8992 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8993 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8994 /* Fog */
8995 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8996 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8997 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8998 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8999 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
9000 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
9001 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9002 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9003 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
9004 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9005 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9006 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9007 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9008 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9009 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9010 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9011 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
9012 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
9013 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
9014 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
9015 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
9016 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
9017 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9018 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9019 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9020 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
9021 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9022 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9023 /* NP2 texture matrix fixups. They are not needed if
9024 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
9025 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
9026 * matrix. */
9027 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9028 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9029 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9030 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9031 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9032 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9033 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9034 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9035 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9036 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9037 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9038 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9039 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9040 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9041 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9042 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9043 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9044 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9045 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9046 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9047 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9048 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9049 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9050 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9051 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
9052 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_LEGACY_CONTEXT },
9053 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GL_EXT_NONE },
9054 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
9057 /* TODO:
9058 * - Implement vertex tweening. */
9059 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
9061 glsl_vertex_pipe_vp_enable,
9062 glsl_vertex_pipe_vp_get_caps,
9063 glsl_vertex_pipe_vp_get_emul_mask,
9064 glsl_vertex_pipe_vp_alloc,
9065 glsl_vertex_pipe_vp_free,
9066 glsl_vertex_pipe_vp_states,
9069 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
9071 /* Nothing to do. */
9074 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
9076 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
9077 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
9078 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
9079 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
9080 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
9081 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
9082 | WINED3DTEXOPCAPS_SELECTARG1
9083 | WINED3DTEXOPCAPS_SELECTARG2
9084 | WINED3DTEXOPCAPS_MODULATE4X
9085 | WINED3DTEXOPCAPS_MODULATE2X
9086 | WINED3DTEXOPCAPS_MODULATE
9087 | WINED3DTEXOPCAPS_ADDSIGNED2X
9088 | WINED3DTEXOPCAPS_ADDSIGNED
9089 | WINED3DTEXOPCAPS_ADD
9090 | WINED3DTEXOPCAPS_SUBTRACT
9091 | WINED3DTEXOPCAPS_ADDSMOOTH
9092 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
9093 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
9094 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
9095 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
9096 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
9097 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
9098 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
9099 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
9100 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
9101 | WINED3DTEXOPCAPS_DOTPRODUCT3
9102 | WINED3DTEXOPCAPS_MULTIPLYADD
9103 | WINED3DTEXOPCAPS_LERP
9104 | WINED3DTEXOPCAPS_BUMPENVMAP
9105 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
9106 caps->MaxTextureBlendStages = 8;
9107 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
9110 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
9112 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9113 return GL_EXT_EMUL_ARB_MULTITEXTURE;
9114 return 0;
9117 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
9119 struct shader_glsl_priv *priv;
9121 if (shader_backend == &glsl_shader_backend)
9123 priv = shader_priv;
9125 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
9127 ERR("Failed to initialize rbtree.\n");
9128 return NULL;
9131 return priv;
9134 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
9136 return NULL;
9139 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
9141 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
9142 struct glsl_ffp_fragment_shader, entry.entry);
9143 struct glsl_shader_prog_link *program, *program2;
9144 struct glsl_ffp_destroy_ctx *ctx = context;
9146 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
9147 struct glsl_shader_prog_link, ps.shader_entry)
9149 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
9151 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
9152 HeapFree(GetProcessHeap(), 0, shader);
9155 /* Context activation is done by the caller. */
9156 static void glsl_fragment_pipe_free(struct wined3d_device *device)
9158 struct shader_glsl_priv *priv = device->fragment_priv;
9159 struct glsl_ffp_destroy_ctx ctx;
9161 ctx.priv = priv;
9162 ctx.gl_info = &device->adapter->gl_info;
9163 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
9166 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
9167 const struct wined3d_state *state, DWORD state_id)
9169 context->last_was_pshader = use_ps(state);
9171 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9174 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
9175 const struct wined3d_state *state, DWORD state_id)
9177 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
9180 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
9181 const struct wined3d_state *state, DWORD state_id)
9183 BOOL use_vshader = use_vs(state);
9184 enum fogsource new_source;
9185 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
9186 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
9188 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9190 if (!state->render_states[WINED3D_RS_FOGENABLE])
9191 return;
9193 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
9195 if (use_vshader)
9196 new_source = FOGSOURCE_VS;
9197 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
9198 new_source = FOGSOURCE_COORD;
9199 else
9200 new_source = FOGSOURCE_FFP;
9202 else
9204 new_source = FOGSOURCE_FFP;
9207 if (new_source != context->fog_source || fogstart == fogend)
9209 context->fog_source = new_source;
9210 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
9214 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
9215 const struct wined3d_state *state, DWORD state_id)
9217 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
9218 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
9219 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9221 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
9222 glsl_fragment_pipe_fog(context, state, state_id);
9225 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
9226 const struct wined3d_state *state, DWORD state_id)
9228 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
9229 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
9230 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9233 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
9234 const struct wined3d_state *state, DWORD state_id)
9236 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9239 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
9240 const struct wined3d_state *state, DWORD state_id)
9242 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
9245 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
9246 const struct wined3d_state *state, DWORD state_id)
9248 const struct wined3d_gl_info *gl_info = context->gl_info;
9249 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
9250 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
9252 if (func)
9254 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
9255 checkGLcall("glAlphaFunc");
9259 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
9260 const struct wined3d_state *state, DWORD state_id)
9262 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9265 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
9266 const struct wined3d_state *state, DWORD state_id)
9268 const struct wined3d_gl_info *gl_info = context->gl_info;
9270 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
9272 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
9273 checkGLcall("glEnable(GL_ALPHA_TEST)");
9275 else
9277 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
9278 checkGLcall("glDisable(GL_ALPHA_TEST)");
9282 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
9283 const struct wined3d_state *state, DWORD state_id)
9285 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
9288 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
9289 const struct wined3d_state *state, DWORD state_id)
9291 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
9294 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
9295 const struct wined3d_state *state, DWORD state_id)
9297 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
9300 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
9302 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
9303 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
9304 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9305 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9306 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9307 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9308 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9309 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9310 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9311 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9312 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9313 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9314 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9315 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9316 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9317 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9318 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9319 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9320 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9321 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9322 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9323 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9324 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9325 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9326 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9327 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9328 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9329 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9330 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9331 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9332 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9333 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9334 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9335 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9336 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9337 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9338 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9339 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9340 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9341 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9342 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9343 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9344 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9345 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9346 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9347 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9348 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9349 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9350 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9351 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9352 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9353 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9354 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9355 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9356 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9357 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9358 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9359 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9360 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9361 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9362 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9363 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9364 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9365 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9366 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9367 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9368 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9369 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9370 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9371 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9372 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9373 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9374 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9375 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9376 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9377 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9378 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
9379 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
9380 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
9381 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
9382 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
9383 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
9384 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9385 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
9386 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
9387 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9388 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9389 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9390 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
9391 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
9392 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9393 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9394 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9395 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
9396 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
9397 {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 },
9398 {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 },
9399 {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 },
9400 {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 },
9401 {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 },
9402 {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 },
9403 {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 },
9404 {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 },
9405 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9406 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9407 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9408 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9409 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9410 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9411 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9412 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9413 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9414 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9415 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_LEGACY_CONTEXT},
9416 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GL_EXT_NONE },
9417 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
9420 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
9422 return TRUE;
9425 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
9429 const struct fragment_pipeline glsl_fragment_pipe =
9431 glsl_fragment_pipe_enable,
9432 glsl_fragment_pipe_get_caps,
9433 glsl_fragment_pipe_get_emul_mask,
9434 glsl_fragment_pipe_alloc,
9435 glsl_fragment_pipe_free,
9436 glsl_fragment_pipe_alloc_context_data,
9437 glsl_fragment_pipe_free_context_data,
9438 shader_glsl_color_fixup_supported,
9439 glsl_fragment_pipe_state_template,