wined3d: Use pure integer vertex attributes for SM4+ shaders.
[wine.git] / dlls / wined3d / glsl_shader.c
blobc8c8cc2f423e7293025c32c6cd9764c6bb4f8177
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 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
1709 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
1710 unsigned int index)
1712 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
1713 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
1714 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
1715 index, scalar_type, scalar_type, index);
1718 /** Generate the variable & register declarations for the GLSL output target */
1719 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1720 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1721 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1723 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1724 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1725 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1726 const struct wined3d_gl_info *gl_info = context->gl_info;
1727 unsigned int i, extra_constants_needed = 0;
1728 const struct wined3d_shader_lconst *lconst;
1729 const char *prefix;
1730 DWORD map;
1732 prefix = shader_glsl_get_prefix(version->type);
1734 /* Prototype the subroutines */
1735 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1737 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1740 /* Declare the constants (aka uniforms) */
1741 if (shader->limits->constant_float > 0)
1743 unsigned max_constantsF;
1745 /* Unless the shader uses indirect addressing, always declare the
1746 * maximum array size and ignore that we need some uniforms privately.
1747 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1748 * and immediate values, still declare VC[256]. If the shader needs
1749 * more uniforms than we have it won't work in any case. If it uses
1750 * less, the compiler will figure out which uniforms are really used
1751 * and strip them out. This allows a shader to use c255 on a dx9 card,
1752 * as long as it doesn't also use all the other constants.
1754 * If the shader uses indirect addressing the compiler must assume
1755 * that all declared uniforms are used. In this case, declare only the
1756 * amount that we're assured to have.
1758 * Thus we run into problems in these two cases:
1759 * 1) The shader really uses more uniforms than supported.
1760 * 2) The shader uses indirect addressing, less constants than
1761 * supported, but uses a constant index > #supported consts. */
1762 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1764 /* No indirect addressing here. */
1765 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1767 else
1769 if (reg_maps->usesrelconstF)
1771 /* Subtract the other potential uniforms from the max
1772 * available (bools, ints, and 1 row of projection matrix).
1773 * Subtract another uniform for immediate values, which have
1774 * to be loaded via uniform by the driver as well. The shader
1775 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1776 * shader code, so one vec4 should be enough. (Unfortunately
1777 * the Nvidia driver doesn't store 128 and -128 in one float).
1779 * Writing gl_ClipVertex requires one uniform for each
1780 * clipplane as well. */
1781 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1782 if (vs_args->clip_enabled)
1783 max_constantsF -= gl_info->limits.clipplanes;
1784 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
1785 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1786 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1787 * for now take this into account when calculating the number of available constants
1789 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
1790 /* Set by driver quirks in directx.c */
1791 max_constantsF -= gl_info->reserved_glsl_constants;
1793 if (max_constantsF < shader->limits->constant_float)
1795 static unsigned int once;
1797 if (!once++)
1798 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1799 " it may not render correctly.\n");
1800 else
1801 WARN("The hardware does not support enough uniform components to run this shader.\n");
1804 else
1806 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1809 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1810 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1813 /* Always declare the full set of constants, the compiler can remove the
1814 * unused ones because d3d doesn't (yet) support indirect int and bool
1815 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1816 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1817 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1819 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1820 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1822 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1824 if (reg_maps->cb_sizes[i])
1825 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1826 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1829 /* Declare texture samplers */
1830 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1832 struct wined3d_shader_sampler_map_entry *entry;
1833 const char *sampler_type_prefix, *sampler_type;
1834 BOOL shadow_sampler, tex_rect;
1836 entry = &reg_maps->sampler_map.entries[i];
1838 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1840 ERR("Invalid resource index %u.\n", entry->resource_idx);
1841 continue;
1844 switch (reg_maps->resource_info[entry->resource_idx].data_type)
1846 case WINED3D_DATA_FLOAT:
1847 case WINED3D_DATA_UNORM:
1848 case WINED3D_DATA_SNORM:
1849 sampler_type_prefix = "";
1850 break;
1852 case WINED3D_DATA_INT:
1853 sampler_type_prefix = "i";
1854 break;
1856 case WINED3D_DATA_UINT:
1857 sampler_type_prefix = "u";
1858 break;
1860 default:
1861 sampler_type_prefix = "";
1862 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
1863 break;
1866 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
1867 switch (reg_maps->resource_info[entry->resource_idx].type)
1869 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1870 if (shadow_sampler)
1871 sampler_type = "sampler1DShadow";
1872 else
1873 sampler_type = "sampler1D";
1874 break;
1876 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1877 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1878 && (ps_args->np2_fixup & (1u << entry->resource_idx))
1879 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1880 if (shadow_sampler)
1882 if (tex_rect)
1883 sampler_type = "sampler2DRectShadow";
1884 else
1885 sampler_type = "sampler2DShadow";
1887 else
1889 if (tex_rect)
1890 sampler_type = "sampler2DRect";
1891 else
1892 sampler_type = "sampler2D";
1894 break;
1896 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1897 if (shadow_sampler)
1898 FIXME("Unsupported 3D shadow sampler.\n");
1899 sampler_type = "sampler3D";
1900 break;
1902 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1903 if (shadow_sampler)
1904 FIXME("Unsupported Cube shadow sampler.\n");
1905 sampler_type = "samplerCube";
1906 break;
1908 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
1909 if (shadow_sampler)
1910 sampler_type = "sampler2DArrayShadow";
1911 else
1912 sampler_type = "sampler2DArray";
1913 break;
1915 default:
1916 sampler_type = "unsupported_sampler";
1917 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
1918 break;
1920 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
1921 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
1924 /* Declare uniforms for NP2 texcoord fixup:
1925 * This is NOT done inside the loop that declares the texture samplers
1926 * since the NP2 fixup code is currently only used for the GeforceFX
1927 * series and when forcing the ARB_npot extension off. Modern cards just
1928 * skip the code anyway, so put it inside a separate loop. */
1929 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1931 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1932 UINT cur = 0;
1934 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1935 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1936 * samplerNP2Fixup stores texture dimensions and is updated through
1937 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1939 for (i = 0; i < shader->limits->sampler; ++i)
1941 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
1942 continue;
1944 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1946 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1947 continue;
1950 fixup->idx[i] = cur++;
1953 fixup->num_consts = (cur + 1) >> 1;
1954 fixup->active = ps_args->np2_fixup;
1955 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1958 /* Declare address variables */
1959 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1961 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1964 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1966 for (i = 0; i < shader->input_signature.element_count; ++i)
1968 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
1969 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
1971 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
1972 prefix, e->register_idx);
1974 else if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
1976 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1977 prefix, e->register_idx);
1979 else if (e->component_type == WINED3D_TYPE_UINT)
1981 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "uvec", "uint", e->register_idx);
1983 else if (e->component_type == WINED3D_TYPE_INT)
1985 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "ivec", "int", e->register_idx);
1987 else
1989 if (e->component_type && e->component_type != WINED3D_TYPE_FLOAT)
1990 FIXME("Unhandled type %#x.\n", e->component_type);
1991 shader_addline(buffer, "%s vec4 %s_in%u;\n",
1992 get_attribute_keyword(gl_info), prefix, e->register_idx);
1996 if (vs_args->point_size && !vs_args->per_vertex_point_size)
1998 shader_addline(buffer, "uniform struct\n{\n");
1999 shader_addline(buffer, " float size;\n");
2000 shader_addline(buffer, " float size_min;\n");
2001 shader_addline(buffer, " float size_max;\n");
2002 shader_addline(buffer, "} ffp_point;\n");
2005 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && version->major < 3)
2007 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2008 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_specular;\n");
2009 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2010 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2013 if (version->major < 4)
2014 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
2016 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2018 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2020 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
2022 else
2024 shader_addline(buffer, "layout(%s) in;\n", glsl_primitive_type_from_d3d(shader->u.gs.input_type));
2025 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
2026 glsl_primitive_type_from_d3d(shader->u.gs.output_type), shader->u.gs.vertices_out);
2027 shader_addline(buffer, "in vs_gs_iface { vec4 gs_in[%u]; } gs_in[];\n", shader->limits->packed_input);
2030 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2032 if (version->major < 3 || ps_args->vp_mode != vertexshader)
2034 shader_addline(buffer, "uniform struct\n{\n");
2035 shader_addline(buffer, " vec4 color;\n");
2036 shader_addline(buffer, " float density;\n");
2037 shader_addline(buffer, " float end;\n");
2038 shader_addline(buffer, " float scale;\n");
2039 shader_addline(buffer, "} ffp_fog;\n");
2041 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2043 if (glsl_is_color_reg_read(shader, 0))
2044 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
2045 if (glsl_is_color_reg_read(shader, 1))
2046 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
2047 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2048 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
2050 else
2052 if (glsl_is_color_reg_read(shader, 0))
2053 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2054 if (glsl_is_color_reg_read(shader, 1))
2055 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_specular;\n");
2056 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2057 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2058 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2062 if (version->major >= 3)
2064 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
2066 if (ps_args->vp_mode == vertexshader)
2067 declare_in_varying(gl_info, buffer, FALSE, "vec4 %s_link[%u];\n", prefix, in_count);
2068 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
2071 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
2073 if (!(map & 1))
2074 continue;
2076 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
2078 if (reg_maps->luminanceparams & (1u << i))
2080 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
2081 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
2082 extra_constants_needed++;
2085 extra_constants_needed++;
2088 if (ps_args->srgb_correction)
2090 shader_addline(buffer, "const vec4 srgb_const0 = ");
2091 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
2092 shader_addline(buffer, ";\n");
2093 shader_addline(buffer, "const vec4 srgb_const1 = ");
2094 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
2095 shader_addline(buffer, ";\n");
2097 if (reg_maps->vpos || reg_maps->usesdsy)
2099 ++extra_constants_needed;
2100 shader_addline(buffer, "uniform vec4 ycorrection;\n");
2101 shader_addline(buffer, "vec4 vpos;\n");
2104 if (ps_args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
2105 shader_addline(buffer, "uniform float alpha_test_ref;\n");
2107 if (!needs_legacy_glsl_syntax(gl_info))
2108 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
2110 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
2111 FIXME("Insufficient uniforms to run this shader.\n");
2114 /* Declare output register temporaries */
2115 if (shader->limits->packed_output)
2116 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2118 /* Declare temporary variables */
2119 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2121 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
2124 /* Declare loop registers aLx */
2125 if (version->major < 4)
2127 for (i = 0; i < reg_maps->loop_depth; ++i)
2129 shader_addline(buffer, "int aL%u;\n", i);
2130 shader_addline(buffer, "int tmpInt%u;\n", i);
2134 /* Temporary variables for matrix operations */
2135 shader_addline(buffer, "vec4 tmp0;\n");
2136 shader_addline(buffer, "vec4 tmp1;\n");
2138 if (!shader->load_local_constsF)
2140 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2142 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2143 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2144 shader_addline(buffer, ";\n");
2149 /*****************************************************************************
2150 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2152 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2153 ****************************************************************************/
2155 /* Prototypes */
2156 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2157 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2159 /** Used for opcode modifiers - They multiply the result by the specified amount */
2160 static const char * const shift_glsl_tab[] = {
2161 "", /* 0 (none) */
2162 "2.0 * ", /* 1 (x2) */
2163 "4.0 * ", /* 2 (x4) */
2164 "8.0 * ", /* 3 (x8) */
2165 "16.0 * ", /* 4 (x16) */
2166 "32.0 * ", /* 5 (x32) */
2167 "", /* 6 (x64) */
2168 "", /* 7 (x128) */
2169 "", /* 8 (d256) */
2170 "", /* 9 (d128) */
2171 "", /* 10 (d64) */
2172 "", /* 11 (d32) */
2173 "0.0625 * ", /* 12 (d16) */
2174 "0.125 * ", /* 13 (d8) */
2175 "0.25 * ", /* 14 (d4) */
2176 "0.5 * " /* 15 (d2) */
2179 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2180 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2181 const char *in_reg, const char *in_regswizzle, char *out_str)
2183 switch (src_modifier)
2185 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2186 case WINED3DSPSM_DW:
2187 case WINED3DSPSM_NONE:
2188 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2189 break;
2190 case WINED3DSPSM_NEG:
2191 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2192 break;
2193 case WINED3DSPSM_NOT:
2194 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2195 break;
2196 case WINED3DSPSM_BIAS:
2197 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2198 break;
2199 case WINED3DSPSM_BIASNEG:
2200 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2201 break;
2202 case WINED3DSPSM_SIGN:
2203 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2204 break;
2205 case WINED3DSPSM_SIGNNEG:
2206 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2207 break;
2208 case WINED3DSPSM_COMP:
2209 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2210 break;
2211 case WINED3DSPSM_X2:
2212 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2213 break;
2214 case WINED3DSPSM_X2NEG:
2215 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2216 break;
2217 case WINED3DSPSM_ABS:
2218 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2219 break;
2220 case WINED3DSPSM_ABSNEG:
2221 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2222 break;
2223 default:
2224 FIXME("Unhandled modifier %u\n", src_modifier);
2225 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2229 /** Writes the GLSL variable name that corresponds to the register that the
2230 * DX opcode parameter is trying to access */
2231 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2232 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2234 /* oPos, oFog and oPts in D3D */
2235 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2237 const struct wined3d_shader *shader = ins->ctx->shader;
2238 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2239 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2240 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2241 const char *prefix = shader_glsl_get_prefix(version->type);
2242 struct glsl_src_param rel_param0, rel_param1;
2243 char imm_str[4][17];
2245 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2246 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2247 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2248 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2249 *is_color = FALSE;
2251 switch (reg->type)
2253 case WINED3DSPR_TEMP:
2254 sprintf(register_name, "R%u", reg->idx[0].offset);
2255 break;
2257 case WINED3DSPR_INPUT:
2258 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2260 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2262 if (reg->idx[0].rel_addr)
2263 FIXME("VS3+ input registers relative addressing.\n");
2264 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2265 *is_color = TRUE;
2266 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2267 break;
2270 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2272 if (reg->idx[0].rel_addr)
2274 if (reg->idx[1].rel_addr)
2275 sprintf(register_name, "gs_in[%s + %u]%s[%s + %u]",
2276 rel_param0.param_str, reg->idx[0].offset,
2277 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2278 rel_param1.param_str, reg->idx[1].offset);
2279 else
2280 sprintf(register_name, "gs_in[%s + %u]%s[%u]",
2281 rel_param0.param_str, reg->idx[0].offset,
2282 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2283 reg->idx[1].offset);
2285 else if (reg->idx[1].rel_addr)
2286 sprintf(register_name, "gs_in[%u]%s[%s + %u]", reg->idx[0].offset,
2287 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2288 rel_param1.param_str, reg->idx[1].offset);
2289 else
2290 sprintf(register_name, "gs_in[%u]%s[%u]", reg->idx[0].offset,
2291 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2292 reg->idx[1].offset);
2293 break;
2296 /* pixel shaders >= 3.0 */
2297 if (version->major >= 3)
2299 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2300 unsigned int in_count = vec4_varyings(version->major, gl_info);
2302 if (reg->idx[0].rel_addr)
2304 /* Removing a + 0 would be an obvious optimization, but
2305 * OS X doesn't see the NOP operation there. */
2306 if (idx)
2308 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2309 && shader->u.ps.declared_in_count > in_count)
2311 sprintf(register_name,
2312 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2313 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2314 prefix, rel_param0.param_str, idx);
2316 else
2318 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2321 else
2323 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2324 && shader->u.ps.declared_in_count > in_count)
2326 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2327 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2328 prefix, rel_param0.param_str);
2330 else
2332 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2336 else
2338 if (idx == in_count) sprintf(register_name, "gl_Color");
2339 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2340 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2343 else
2345 if (!reg->idx[0].offset)
2346 strcpy(register_name, "ffp_varying_diffuse");
2347 else
2348 strcpy(register_name, "ffp_varying_specular");
2349 break;
2351 break;
2353 case WINED3DSPR_CONST:
2355 /* Relative addressing */
2356 if (reg->idx[0].rel_addr)
2358 if (wined3d_settings.check_float_constants)
2359 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2360 rel_param0.param_str, reg->idx[0].offset,
2361 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2362 prefix, rel_param0.param_str, reg->idx[0].offset);
2363 else if (reg->idx[0].offset)
2364 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2365 else
2366 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2368 else
2370 if (shader_constant_is_local(shader, reg->idx[0].offset))
2371 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2372 else
2373 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2376 break;
2378 case WINED3DSPR_CONSTINT:
2379 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2380 break;
2382 case WINED3DSPR_CONSTBOOL:
2383 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2384 break;
2386 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2387 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2388 sprintf(register_name, "T%u", reg->idx[0].offset);
2389 else
2390 sprintf(register_name, "A%u", reg->idx[0].offset);
2391 break;
2393 case WINED3DSPR_LOOP:
2394 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
2395 break;
2397 case WINED3DSPR_SAMPLER:
2398 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2399 break;
2401 case WINED3DSPR_COLOROUT:
2402 if (reg->idx[0].offset >= gl_info->limits.buffers)
2403 WARN("Write to render target %u, only %d supported.\n",
2404 reg->idx[0].offset, gl_info->limits.buffers);
2406 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2407 break;
2409 case WINED3DSPR_RASTOUT:
2410 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2411 break;
2413 case WINED3DSPR_DEPTHOUT:
2414 sprintf(register_name, "gl_FragDepth");
2415 break;
2417 case WINED3DSPR_ATTROUT:
2418 if (!reg->idx[0].offset)
2419 sprintf(register_name, "%s_out[8]", prefix);
2420 else
2421 sprintf(register_name, "%s_out[9]", prefix);
2422 break;
2424 case WINED3DSPR_TEXCRDOUT:
2425 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2426 if (reg->idx[0].rel_addr)
2427 FIXME("VS3 output registers relative addressing.\n");
2428 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2429 break;
2431 case WINED3DSPR_MISCTYPE:
2432 if (!reg->idx[0].offset)
2434 /* vPos */
2435 sprintf(register_name, "vpos");
2437 else if (reg->idx[0].offset == 1)
2439 /* Note that gl_FrontFacing is a bool, while vFace is
2440 * a float for which the sign determines front/back */
2441 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2443 else
2445 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2446 sprintf(register_name, "unrecognized_register");
2448 break;
2450 case WINED3DSPR_IMMCONST:
2451 switch (reg->immconst_type)
2453 case WINED3D_IMMCONST_SCALAR:
2454 switch (reg->data_type)
2456 case WINED3D_DATA_FLOAT:
2457 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
2458 break;
2459 case WINED3D_DATA_INT:
2460 sprintf(register_name, "%#x", reg->immconst_data[0]);
2461 break;
2462 case WINED3D_DATA_RESOURCE:
2463 case WINED3D_DATA_SAMPLER:
2464 case WINED3D_DATA_UINT:
2465 sprintf(register_name, "%#xu", reg->immconst_data[0]);
2466 break;
2467 default:
2468 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2469 break;
2471 break;
2473 case WINED3D_IMMCONST_VEC4:
2474 switch (reg->data_type)
2476 case WINED3D_DATA_FLOAT:
2477 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
2478 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
2479 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
2480 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
2481 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2482 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2483 break;
2484 case WINED3D_DATA_INT:
2485 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2486 reg->immconst_data[0], reg->immconst_data[1],
2487 reg->immconst_data[2], reg->immconst_data[3]);
2488 break;
2489 case WINED3D_DATA_RESOURCE:
2490 case WINED3D_DATA_SAMPLER:
2491 case WINED3D_DATA_UINT:
2492 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2493 reg->immconst_data[0], reg->immconst_data[1],
2494 reg->immconst_data[2], reg->immconst_data[3]);
2495 break;
2496 default:
2497 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2498 break;
2500 break;
2502 default:
2503 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2504 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2506 break;
2508 case WINED3DSPR_CONSTBUFFER:
2509 if (reg->idx[1].rel_addr)
2510 sprintf(register_name, "%s_cb%u[%s + %u]",
2511 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2512 else
2513 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2514 break;
2516 case WINED3DSPR_IMMCONSTBUFFER:
2517 if (reg->idx[0].rel_addr)
2518 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2519 else
2520 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2521 break;
2523 case WINED3DSPR_PRIMID:
2524 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2525 break;
2527 default:
2528 FIXME("Unhandled register type %#x.\n", reg->type);
2529 sprintf(register_name, "unrecognized_register");
2530 break;
2534 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2536 *str++ = '.';
2537 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2538 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2539 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2540 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2541 *str = '\0';
2544 /* Get the GLSL write mask for the destination register */
2545 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2547 DWORD mask = param->write_mask;
2549 if (shader_is_scalar(&param->reg))
2551 mask = WINED3DSP_WRITEMASK_0;
2552 *write_mask = '\0';
2554 else
2556 shader_glsl_write_mask_to_str(mask, write_mask);
2559 return mask;
2562 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
2563 unsigned int size = 0;
2565 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2566 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2567 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2568 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2570 return size;
2573 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2575 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2576 * but addressed as "rgba". To fix this we need to swap the register's x
2577 * and z components. */
2578 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2580 *str++ = '.';
2581 /* swizzle bits fields: wwzzyyxx */
2582 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
2583 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
2584 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
2585 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
2586 *str = '\0';
2589 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2590 BOOL fixup, DWORD mask, char *swizzle_str)
2592 if (shader_is_scalar(&param->reg))
2593 *swizzle_str = '\0';
2594 else
2595 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2598 /* From a given parameter token, generate the corresponding GLSL string.
2599 * Also, return the actual register name and swizzle in case the
2600 * caller needs this information as well. */
2601 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2602 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
2604 BOOL is_color = FALSE;
2605 char swizzle_str[6];
2607 glsl_src->reg_name[0] = '\0';
2608 glsl_src->param_str[0] = '\0';
2609 swizzle_str[0] = '\0';
2611 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
2612 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
2614 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
2616 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
2618 else
2620 char reg_name[200];
2622 switch (wined3d_src->reg.data_type)
2624 case WINED3D_DATA_FLOAT:
2625 sprintf(reg_name, "%s", glsl_src->reg_name);
2626 break;
2627 case WINED3D_DATA_INT:
2628 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
2629 break;
2630 case WINED3D_DATA_RESOURCE:
2631 case WINED3D_DATA_SAMPLER:
2632 case WINED3D_DATA_UINT:
2633 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
2634 break;
2635 default:
2636 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
2637 sprintf(reg_name, "%s", glsl_src->reg_name);
2638 break;
2641 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
2645 /* From a given parameter token, generate the corresponding GLSL string.
2646 * Also, return the actual register name and swizzle in case the
2647 * caller needs this information as well. */
2648 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
2649 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
2651 BOOL is_color = FALSE;
2653 glsl_dst->mask_str[0] = '\0';
2654 glsl_dst->reg_name[0] = '\0';
2656 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
2657 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
2660 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2661 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
2662 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
2663 enum wined3d_data_type data_type)
2665 struct glsl_dst_param glsl_dst;
2666 DWORD mask;
2668 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
2670 switch (data_type)
2672 case WINED3D_DATA_FLOAT:
2673 shader_addline(buffer, "%s%s = %s(",
2674 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2675 break;
2676 case WINED3D_DATA_INT:
2677 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
2678 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2679 break;
2680 case WINED3D_DATA_RESOURCE:
2681 case WINED3D_DATA_SAMPLER:
2682 case WINED3D_DATA_UINT:
2683 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
2684 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2685 break;
2686 default:
2687 FIXME("Unhandled data type %#x.\n", data_type);
2688 shader_addline(buffer, "%s%s = %s(",
2689 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2690 break;
2694 return mask;
2697 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2698 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
2700 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2703 /** Process GLSL instruction modifiers */
2704 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
2706 struct glsl_dst_param dst_param;
2707 DWORD modifiers;
2709 if (!ins->dst_count) return;
2711 modifiers = ins->dst[0].modifiers;
2712 if (!modifiers) return;
2714 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2716 if (modifiers & WINED3DSPDM_SATURATE)
2718 /* _SAT means to clamp the value of the register to between 0 and 1 */
2719 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
2720 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
2723 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
2725 FIXME("_centroid modifier not handled\n");
2728 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
2730 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2734 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
2736 switch (op)
2738 case WINED3D_SHADER_REL_OP_GT: return ">";
2739 case WINED3D_SHADER_REL_OP_EQ: return "==";
2740 case WINED3D_SHADER_REL_OP_GE: return ">=";
2741 case WINED3D_SHADER_REL_OP_LT: return "<";
2742 case WINED3D_SHADER_REL_OP_NE: return "!=";
2743 case WINED3D_SHADER_REL_OP_LE: return "<=";
2744 default:
2745 FIXME("Unrecognized operator %#x.\n", op);
2746 return "(\?\?)";
2750 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
2751 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
2753 static const struct
2755 unsigned int coord_size;
2756 unsigned int offset_size;
2757 const char *type_part;
2759 resource_types[] =
2761 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
2762 {1, 0, ""}, /* WINED3D_SHADER_RESOURCE_BUFFER */
2763 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
2764 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
2765 {2, 0, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
2766 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
2767 {3, 0, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
2768 {2, 1, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
2769 {3, 2, "2DArray"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
2770 {3, 0, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
2772 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2773 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
2774 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2775 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
2776 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
2777 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
2778 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
2779 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2780 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
2781 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
2782 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
2783 const char *base = "texture", *type_part = "", *suffix = "";
2784 unsigned int coord_size;
2786 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
2788 if (resource_type >= ARRAY_SIZE(resource_types))
2790 ERR("Unexpected resource type %#x.\n", resource_type);
2791 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
2794 /* Note that there's no such thing as a projected cube texture. */
2795 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
2796 projected = FALSE;
2798 if (needs_legacy_glsl_syntax(gl_info))
2800 if (shadow)
2801 base = "shadow";
2803 type_part = resource_types[resource_type].type_part;
2804 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
2805 type_part = "2DRect";
2806 if (!type_part[0])
2807 FIXME("Unhandled resource type %#x.\n", resource_type);
2809 if (!lod && grad && !gl_info->supported[EXT_GPU_SHADER4])
2811 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2812 suffix = "ARB";
2813 else
2814 FIXME("Unsupported grad function.\n");
2818 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
2820 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
2821 if (flags & ~texel_fetch_flags)
2822 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
2824 base = "texelFetch";
2825 type_part = "";
2828 sample_function->offset_size = offset ? resource_types[resource_type].offset_size : 0;
2829 if (offset && !sample_function->offset_size)
2831 FIXME("Offset not supported for resource type %#x.\n", resource_type);
2832 offset = FALSE;
2835 sample_function->name = string_buffer_get(priv->string_buffers);
2836 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
2837 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
2839 coord_size = resource_types[resource_type].coord_size;
2840 if (shadow)
2841 ++coord_size;
2842 sample_function->coord_mask = (1u << coord_size) - 1;
2843 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
2846 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
2847 struct glsl_sample_function *sample_function)
2849 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2851 string_buffer_release(priv->string_buffers, sample_function->name);
2854 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2855 BOOL sign_fixup, enum fixup_channel_source channel_source)
2857 switch(channel_source)
2859 case CHANNEL_SOURCE_ZERO:
2860 strcat(arguments, "0.0");
2861 break;
2863 case CHANNEL_SOURCE_ONE:
2864 strcat(arguments, "1.0");
2865 break;
2867 case CHANNEL_SOURCE_X:
2868 strcat(arguments, reg_name);
2869 strcat(arguments, ".x");
2870 break;
2872 case CHANNEL_SOURCE_Y:
2873 strcat(arguments, reg_name);
2874 strcat(arguments, ".y");
2875 break;
2877 case CHANNEL_SOURCE_Z:
2878 strcat(arguments, reg_name);
2879 strcat(arguments, ".z");
2880 break;
2882 case CHANNEL_SOURCE_W:
2883 strcat(arguments, reg_name);
2884 strcat(arguments, ".w");
2885 break;
2887 default:
2888 FIXME("Unhandled channel source %#x\n", channel_source);
2889 strcat(arguments, "undefined");
2890 break;
2893 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2896 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
2897 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2899 unsigned int mask_size, remaining;
2900 DWORD fixup_mask = 0;
2901 char arguments[256];
2902 char mask_str[6];
2904 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2905 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2906 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2907 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2908 if (!(mask &= fixup_mask))
2909 return;
2911 if (is_complex_fixup(fixup))
2913 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2914 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2915 return;
2918 shader_glsl_write_mask_to_str(mask, mask_str);
2919 mask_size = shader_glsl_get_write_mask_size(mask);
2921 arguments[0] = '\0';
2922 remaining = mask_size;
2923 if (mask & WINED3DSP_WRITEMASK_0)
2925 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2926 if (--remaining) strcat(arguments, ", ");
2928 if (mask & WINED3DSP_WRITEMASK_1)
2930 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2931 if (--remaining) strcat(arguments, ", ");
2933 if (mask & WINED3DSP_WRITEMASK_2)
2935 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2936 if (--remaining) strcat(arguments, ", ");
2938 if (mask & WINED3DSP_WRITEMASK_3)
2940 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2941 if (--remaining) strcat(arguments, ", ");
2944 if (mask_size > 1)
2945 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2946 else
2947 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2950 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2952 char reg_name[256];
2953 BOOL is_color;
2955 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2956 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2959 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2960 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
2961 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
2962 const char *coord_reg_fmt, ...)
2964 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2965 char dst_swizzle[6];
2966 struct color_fixup_desc fixup;
2967 BOOL np2_fixup = FALSE;
2968 va_list args;
2969 int ret;
2971 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2973 /* If ARB_texture_swizzle is supported we don't need to do anything here.
2974 * We actually rely on it for vertex shaders and SM4+. */
2975 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
2977 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2978 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
2980 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
2981 np2_fixup = TRUE;
2983 else
2985 fixup = COLOR_FIXUP_IDENTITY;
2988 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2990 if (sample_function->output_single_component)
2991 shader_addline(ins->ctx->buffer, "vec4(");
2993 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2994 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
2996 for (;;)
2998 va_start(args, coord_reg_fmt);
2999 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3000 va_end(args);
3001 if (!ret)
3002 break;
3003 if (!string_buffer_resize(ins->ctx->buffer, ret))
3004 break;
3007 if (np2_fixup)
3009 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3010 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3012 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3014 case 1:
3015 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3016 idx >> 1, (idx % 2) ? "z" : "x");
3017 break;
3018 case 2:
3019 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3020 idx >> 1, (idx % 2) ? "zw" : "xy");
3021 break;
3022 case 3:
3023 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3024 idx >> 1, (idx % 2) ? "zw" : "xy");
3025 break;
3026 case 4:
3027 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3028 idx >> 1, (idx % 2) ? "zw" : "xy");
3029 break;
3032 if (dx && dy)
3033 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3034 else if (bias)
3035 shader_addline(ins->ctx->buffer, ", %s", bias);
3036 if (sample_function->offset_size)
3038 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3039 shader_addline(ins->ctx->buffer, ", ");
3040 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3042 shader_addline(ins->ctx->buffer, ")");
3044 if (sample_function->output_single_component)
3045 shader_addline(ins->ctx->buffer, ")");
3047 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3049 if (!is_identity_fixup(fixup))
3050 shader_glsl_color_correction(ins, fixup);
3053 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer)
3055 /* Write the final position.
3057 * OpenGL coordinates specify the center of the pixel while D3D coords
3058 * specify the corner. The offsets are stored in z and w in
3059 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3060 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3061 * a MAD. */
3062 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3063 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3065 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3066 * in utils.c
3068 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3069 * shaders are run before the homogeneous divide, so we have to take the w
3070 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3071 * z = z * 2 - w. */
3072 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3075 /*****************************************************************************
3076 * Begin processing individual instruction opcodes
3077 ****************************************************************************/
3079 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3081 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3082 struct glsl_src_param src0_param;
3083 struct glsl_src_param src1_param;
3084 DWORD write_mask;
3085 const char *op;
3087 /* Determine the GLSL operator to use based on the opcode */
3088 switch (ins->handler_idx)
3090 case WINED3DSIH_ADD: op = "+"; break;
3091 case WINED3DSIH_AND: op = "&"; break;
3092 case WINED3DSIH_DIV: op = "/"; break;
3093 case WINED3DSIH_IADD: op = "+"; break;
3094 case WINED3DSIH_ISHL: op = "<<"; break;
3095 case WINED3DSIH_MUL: op = "*"; break;
3096 case WINED3DSIH_OR: op = "|"; break;
3097 case WINED3DSIH_SUB: op = "-"; break;
3098 case WINED3DSIH_USHR: op = ">>"; break;
3099 case WINED3DSIH_XOR: op = "^"; break;
3100 default:
3101 op = "<unhandled operator>";
3102 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3103 break;
3106 write_mask = shader_glsl_append_dst(buffer, ins);
3107 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3108 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3109 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3112 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3114 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3115 struct glsl_src_param src0_param;
3116 struct glsl_src_param src1_param;
3117 unsigned int mask_size;
3118 DWORD write_mask;
3119 const char *op;
3121 write_mask = shader_glsl_append_dst(buffer, ins);
3122 mask_size = shader_glsl_get_write_mask_size(write_mask);
3123 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3124 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3126 if (mask_size > 1)
3128 switch (ins->handler_idx)
3130 case WINED3DSIH_EQ: op = "equal"; break;
3131 case WINED3DSIH_IEQ: op = "equal"; break;
3132 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3133 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3134 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3135 case WINED3DSIH_LT: op = "lessThan"; break;
3136 case WINED3DSIH_ILT: op = "lessThan"; break;
3137 case WINED3DSIH_ULT: op = "lessThan"; break;
3138 case WINED3DSIH_NE: op = "notEqual"; break;
3139 case WINED3DSIH_INE: op = "notEqual"; break;
3140 default:
3141 op = "<unhandled operator>";
3142 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3143 break;
3146 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3147 mask_size, op, src0_param.param_str, src1_param.param_str);
3149 else
3151 switch (ins->handler_idx)
3153 case WINED3DSIH_EQ: op = "=="; break;
3154 case WINED3DSIH_IEQ: op = "=="; break;
3155 case WINED3DSIH_GE: op = ">="; break;
3156 case WINED3DSIH_IGE: op = ">="; break;
3157 case WINED3DSIH_UGE: op = ">="; break;
3158 case WINED3DSIH_LT: op = "<"; break;
3159 case WINED3DSIH_ILT: op = "<"; break;
3160 case WINED3DSIH_ULT: op = "<"; break;
3161 case WINED3DSIH_NE: op = "!="; break;
3162 case WINED3DSIH_INE: op = "!="; break;
3163 default:
3164 op = "<unhandled operator>";
3165 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3166 break;
3169 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3170 src0_param.param_str, op, src1_param.param_str);
3174 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3176 struct glsl_src_param src_param;
3177 DWORD write_mask;
3178 const char *op;
3180 switch (ins->handler_idx)
3182 case WINED3DSIH_INEG: op = "-"; break;
3183 case WINED3DSIH_NOT: op = "~"; break;
3184 default:
3185 op = "<unhandled operator>";
3186 ERR("Unhandled opcode %s.\n",
3187 debug_d3dshaderinstructionhandler(ins->handler_idx));
3188 break;
3191 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3192 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3193 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3196 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
3198 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3199 struct glsl_src_param src0_param;
3200 struct glsl_src_param src1_param;
3201 DWORD write_mask;
3203 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
3204 * not, we can emulate it. */
3205 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3206 FIXME("64-bit integer multiplies not implemented.\n");
3208 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3210 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3211 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3212 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3214 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3215 src0_param.param_str, src1_param.param_str);
3219 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3221 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3222 struct glsl_src_param src0_param, src1_param;
3223 DWORD write_mask;
3225 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3227 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3229 char dst_mask[6];
3231 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3232 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3233 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3234 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3235 dst_mask, src0_param.param_str, src1_param.param_str);
3237 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3238 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3239 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3240 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3242 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3243 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3245 else
3247 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3248 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3249 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3250 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3253 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3255 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3256 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3257 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3258 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3262 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3263 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3265 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3266 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3267 struct glsl_src_param src0_param;
3268 DWORD write_mask;
3270 write_mask = shader_glsl_append_dst(buffer, ins);
3271 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3273 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3274 * shader versions WINED3DSIO_MOVA is used for this. */
3275 if (ins->ctx->reg_maps->shader_version.major == 1
3276 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3277 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3279 /* This is a simple floor() */
3280 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3281 if (mask_size > 1) {
3282 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3283 } else {
3284 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3287 else if(ins->handler_idx == WINED3DSIH_MOVA)
3289 /* We need to *round* to the nearest int here. */
3290 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3292 if (gl_info->supported[EXT_GPU_SHADER4])
3294 if (mask_size > 1)
3295 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3296 else
3297 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3299 else
3301 if (mask_size > 1)
3302 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3303 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3304 else
3305 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3306 src0_param.param_str, src0_param.param_str);
3309 else
3311 shader_addline(buffer, "%s);\n", src0_param.param_str);
3315 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3316 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3318 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3319 struct glsl_src_param src0_param;
3320 struct glsl_src_param src1_param;
3321 DWORD dst_write_mask, src_write_mask;
3322 unsigned int dst_size;
3324 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3325 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3327 /* dp4 works on vec4, dp3 on vec3, etc. */
3328 if (ins->handler_idx == WINED3DSIH_DP4)
3329 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3330 else if (ins->handler_idx == WINED3DSIH_DP3)
3331 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3332 else
3333 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3335 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3336 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3338 if (dst_size > 1) {
3339 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3340 } else {
3341 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3345 /* Note that this instruction has some restrictions. The destination write mask
3346 * can't contain the w component, and the source swizzles have to be .xyzw */
3347 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3349 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3350 struct glsl_src_param src0_param;
3351 struct glsl_src_param src1_param;
3352 char dst_mask[6];
3354 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3355 shader_glsl_append_dst(ins->ctx->buffer, ins);
3356 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3357 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3358 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3361 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3363 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3366 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3367 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3368 * GLSL uses the value as-is. */
3369 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3371 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3372 struct glsl_src_param src0_param;
3373 struct glsl_src_param src1_param;
3374 DWORD dst_write_mask;
3375 unsigned int dst_size;
3377 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3378 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3380 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3381 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3383 if (dst_size > 1)
3385 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3386 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3388 else
3390 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3391 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3395 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3396 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3398 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3399 struct glsl_src_param src_param;
3400 const char *instruction;
3401 DWORD write_mask;
3402 unsigned i;
3404 /* Determine the GLSL function to use based on the opcode */
3405 /* TODO: Possibly make this a table for faster lookups */
3406 switch (ins->handler_idx)
3408 case WINED3DSIH_ABS: instruction = "abs"; break;
3409 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3410 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3411 case WINED3DSIH_FRC: instruction = "fract"; break;
3412 case WINED3DSIH_IMAX: instruction = "max"; break;
3413 case WINED3DSIH_IMIN: instruction = "min"; break;
3414 case WINED3DSIH_MAX: instruction = "max"; break;
3415 case WINED3DSIH_MIN: instruction = "min"; break;
3416 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
3417 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3418 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
3419 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
3420 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3421 default: instruction = "";
3422 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3423 break;
3426 write_mask = shader_glsl_append_dst(buffer, ins);
3428 shader_addline(buffer, "%s(", instruction);
3430 if (ins->src_count)
3432 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3433 shader_addline(buffer, "%s", src_param.param_str);
3434 for (i = 1; i < ins->src_count; ++i)
3436 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3437 shader_addline(buffer, ", %s", src_param.param_str);
3441 shader_addline(buffer, "));\n");
3444 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3446 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3448 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3449 struct glsl_src_param src_param;
3450 unsigned int mask_size;
3451 DWORD write_mask;
3452 char dst_mask[6];
3454 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3455 mask_size = shader_glsl_get_write_mask_size(write_mask);
3456 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3458 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3459 src_param.param_str, src_param.param_str);
3460 shader_glsl_append_dst(buffer, ins);
3462 if (mask_size > 1)
3464 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3465 mask_size, src_param.param_str);
3467 else
3469 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3470 src_param.param_str);
3474 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3476 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3477 ins->ctx->reg_maps->shader_version.minor);
3478 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3479 struct glsl_src_param src0_param;
3480 const char *prefix, *suffix;
3481 unsigned int dst_size;
3482 DWORD dst_write_mask;
3484 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3485 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3487 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
3488 dst_write_mask = WINED3DSP_WRITEMASK_3;
3490 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
3492 switch (ins->handler_idx)
3494 case WINED3DSIH_EXP:
3495 case WINED3DSIH_EXPP:
3496 prefix = "exp2(";
3497 suffix = ")";
3498 break;
3500 case WINED3DSIH_LOG:
3501 case WINED3DSIH_LOGP:
3502 prefix = "log2(abs(";
3503 suffix = "))";
3504 break;
3506 case WINED3DSIH_RCP:
3507 prefix = "1.0 / ";
3508 suffix = "";
3509 break;
3511 case WINED3DSIH_RSQ:
3512 prefix = "inversesqrt(abs(";
3513 suffix = "))";
3514 break;
3516 default:
3517 prefix = "";
3518 suffix = "";
3519 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3520 break;
3523 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
3524 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3525 else
3526 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3529 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3530 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3531 * dst.x = 2^(floor(src))
3532 * dst.y = src - floor(src)
3533 * dst.z = 2^src (partial precision is allowed, but optional)
3534 * dst.w = 1.0;
3535 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3536 * dst = 2^src; (partial precision is allowed, but optional)
3538 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
3540 if (ins->ctx->reg_maps->shader_version.major < 2)
3542 struct glsl_src_param src_param;
3543 char dst_mask[6];
3545 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
3547 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
3548 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
3549 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
3550 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
3552 shader_glsl_append_dst(ins->ctx->buffer, ins);
3553 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3554 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
3555 return;
3558 shader_glsl_scalar_op(ins);
3561 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
3562 const char *vector_constructor, const char *scalar_constructor)
3564 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3565 struct glsl_src_param src_param;
3566 unsigned int mask_size;
3567 DWORD write_mask;
3569 write_mask = shader_glsl_append_dst(buffer, ins);
3570 mask_size = shader_glsl_get_write_mask_size(write_mask);
3571 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3573 if (mask_size > 1)
3574 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
3575 else
3576 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
3579 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
3581 shader_glsl_cast(ins, "ivec", "int");
3584 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
3586 shader_glsl_cast(ins, "uvec", "uint");
3589 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
3591 shader_glsl_cast(ins, "vec", "float");
3594 /** Process signed comparison opcodes in GLSL. */
3595 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
3597 struct glsl_src_param src0_param;
3598 struct glsl_src_param src1_param;
3599 DWORD write_mask;
3600 unsigned int mask_size;
3602 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3603 mask_size = shader_glsl_get_write_mask_size(write_mask);
3604 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3605 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3607 if (mask_size > 1) {
3608 const char *compare;
3610 switch(ins->handler_idx)
3612 case WINED3DSIH_SLT: compare = "lessThan"; break;
3613 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
3614 default: compare = "";
3615 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3618 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
3619 src0_param.param_str, src1_param.param_str);
3620 } else {
3621 switch(ins->handler_idx)
3623 case WINED3DSIH_SLT:
3624 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3625 * to return 0.0 but step returns 1.0 because step is not < x
3626 * An alternative is a bvec compare padded with an unused second component.
3627 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3628 * issue. Playing with not() is not possible either because not() does not accept
3629 * a scalar.
3631 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
3632 src0_param.param_str, src1_param.param_str);
3633 break;
3634 case WINED3DSIH_SGE:
3635 /* Here we can use the step() function and safe a conditional */
3636 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
3637 break;
3638 default:
3639 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3645 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
3647 const char *condition_prefix, *condition_suffix;
3648 struct wined3d_shader_dst_param dst;
3649 struct glsl_src_param src0_param;
3650 struct glsl_src_param src1_param;
3651 struct glsl_src_param src2_param;
3652 BOOL temp_destination = FALSE;
3653 DWORD cmp_channel = 0;
3654 unsigned int i, j;
3655 char mask_char[6];
3656 DWORD write_mask;
3658 switch (ins->handler_idx)
3660 case WINED3DSIH_CMP:
3661 condition_prefix = "";
3662 condition_suffix = " >= 0.0";
3663 break;
3665 case WINED3DSIH_CND:
3666 condition_prefix = "";
3667 condition_suffix = " > 0.5";
3668 break;
3670 case WINED3DSIH_MOVC:
3671 condition_prefix = "bool(";
3672 condition_suffix = ")";
3673 break;
3675 default:
3676 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3677 condition_prefix = "<unhandled prefix>";
3678 condition_suffix = "<unhandled suffix>";
3679 break;
3682 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
3684 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3685 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3686 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3687 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3689 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3690 condition_prefix, src0_param.param_str, condition_suffix,
3691 src1_param.param_str, src2_param.param_str);
3692 return;
3695 dst = ins->dst[0];
3697 /* Splitting the instruction up in multiple lines imposes a problem:
3698 * The first lines may overwrite source parameters of the following lines.
3699 * Deal with that by using a temporary destination register if needed. */
3700 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
3701 && ins->src[0].reg.type == dst.reg.type)
3702 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
3703 && ins->src[1].reg.type == dst.reg.type)
3704 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
3705 && ins->src[2].reg.type == dst.reg.type))
3706 temp_destination = TRUE;
3708 /* Cycle through all source0 channels. */
3709 for (i = 0; i < 4; ++i)
3711 write_mask = 0;
3712 /* Find the destination channels which use the current source0 channel. */
3713 for (j = 0; j < 4; ++j)
3715 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
3717 write_mask |= WINED3DSP_WRITEMASK_0 << j;
3718 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
3721 dst.write_mask = ins->dst[0].write_mask & write_mask;
3723 if (temp_destination)
3725 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
3726 continue;
3727 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
3729 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
3730 continue;
3732 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
3733 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3734 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3736 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3737 condition_prefix, src0_param.param_str, condition_suffix,
3738 src1_param.param_str, src2_param.param_str);
3741 if (temp_destination)
3743 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3744 shader_glsl_append_dst(ins->ctx->buffer, ins);
3745 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3749 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3750 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3751 * the compare is done per component of src0. */
3752 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3754 struct glsl_src_param src0_param;
3755 struct glsl_src_param src1_param;
3756 struct glsl_src_param src2_param;
3757 DWORD write_mask;
3758 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3759 ins->ctx->reg_maps->shader_version.minor);
3761 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3763 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3764 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3765 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3766 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3768 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
3769 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3770 else
3771 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3772 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3773 return;
3776 shader_glsl_conditional_move(ins);
3779 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3780 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3782 struct glsl_src_param src0_param;
3783 struct glsl_src_param src1_param;
3784 struct glsl_src_param src2_param;
3785 DWORD write_mask;
3787 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3788 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3789 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3790 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3791 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3792 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3795 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3796 Vertex shaders to GLSL codes */
3797 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3799 int i;
3800 int nComponents = 0;
3801 struct wined3d_shader_dst_param tmp_dst = {{0}};
3802 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3803 struct wined3d_shader_instruction tmp_ins;
3805 memset(&tmp_ins, 0, sizeof(tmp_ins));
3807 /* Set constants for the temporary argument */
3808 tmp_ins.ctx = ins->ctx;
3809 tmp_ins.dst_count = 1;
3810 tmp_ins.dst = &tmp_dst;
3811 tmp_ins.src_count = 2;
3812 tmp_ins.src = tmp_src;
3814 switch(ins->handler_idx)
3816 case WINED3DSIH_M4x4:
3817 nComponents = 4;
3818 tmp_ins.handler_idx = WINED3DSIH_DP4;
3819 break;
3820 case WINED3DSIH_M4x3:
3821 nComponents = 3;
3822 tmp_ins.handler_idx = WINED3DSIH_DP4;
3823 break;
3824 case WINED3DSIH_M3x4:
3825 nComponents = 4;
3826 tmp_ins.handler_idx = WINED3DSIH_DP3;
3827 break;
3828 case WINED3DSIH_M3x3:
3829 nComponents = 3;
3830 tmp_ins.handler_idx = WINED3DSIH_DP3;
3831 break;
3832 case WINED3DSIH_M3x2:
3833 nComponents = 2;
3834 tmp_ins.handler_idx = WINED3DSIH_DP3;
3835 break;
3836 default:
3837 break;
3840 tmp_dst = ins->dst[0];
3841 tmp_src[0] = ins->src[0];
3842 tmp_src[1] = ins->src[1];
3843 for (i = 0; i < nComponents; ++i)
3845 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3846 shader_glsl_dot(&tmp_ins);
3847 ++tmp_src[1].reg.idx[0].offset;
3852 The LRP instruction performs a component-wise linear interpolation
3853 between the second and third operands using the first operand as the
3854 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3855 This is equivalent to mix(src2, src1, src0);
3857 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3859 struct glsl_src_param src0_param;
3860 struct glsl_src_param src1_param;
3861 struct glsl_src_param src2_param;
3862 DWORD write_mask;
3864 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3866 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3867 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3868 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3870 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3871 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3874 /** Process the WINED3DSIO_LIT instruction in GLSL:
3875 * dst.x = dst.w = 1.0
3876 * dst.y = (src0.x > 0) ? src0.x
3877 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3878 * where src.w is clamped at +- 128
3880 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3882 struct glsl_src_param src0_param;
3883 struct glsl_src_param src1_param;
3884 struct glsl_src_param src3_param;
3885 char dst_mask[6];
3887 shader_glsl_append_dst(ins->ctx->buffer, ins);
3888 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3890 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3891 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3892 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3894 /* The sdk specifies the instruction like this
3895 * dst.x = 1.0;
3896 * if(src.x > 0.0) dst.y = src.x
3897 * else dst.y = 0.0.
3898 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3899 * else dst.z = 0.0;
3900 * dst.w = 1.0;
3901 * (where power = src.w clamped between -128 and 128)
3903 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3904 * dst.x = 1.0 ... No further explanation needed
3905 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3906 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3907 * dst.w = 1.0. ... Nothing fancy.
3909 * So we still have one conditional in there. So do this:
3910 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3912 * 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),
3913 * 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.
3914 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3916 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3917 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3918 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3920 shader_addline(ins->ctx->buffer,
3921 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3922 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3923 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3924 src0_param.param_str, src3_param.param_str, dst_mask);
3927 /** Process the WINED3DSIO_DST instruction in GLSL:
3928 * dst.x = 1.0
3929 * dst.y = src0.x * src0.y
3930 * dst.z = src0.z
3931 * dst.w = src1.w
3933 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3935 struct glsl_src_param src0y_param;
3936 struct glsl_src_param src0z_param;
3937 struct glsl_src_param src1y_param;
3938 struct glsl_src_param src1w_param;
3939 char dst_mask[6];
3941 shader_glsl_append_dst(ins->ctx->buffer, ins);
3942 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3944 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3945 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3946 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3947 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3949 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3950 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3953 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3954 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3955 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3957 * dst.x = cos(src0.?)
3958 * dst.y = sin(src0.?)
3959 * dst.z = dst.z
3960 * dst.w = dst.w
3962 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3964 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3965 struct glsl_src_param src0_param;
3966 DWORD write_mask;
3968 if (ins->ctx->reg_maps->shader_version.major < 4)
3970 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3972 write_mask = shader_glsl_append_dst(buffer, ins);
3973 switch (write_mask)
3975 case WINED3DSP_WRITEMASK_0:
3976 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3977 break;
3979 case WINED3DSP_WRITEMASK_1:
3980 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3981 break;
3983 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3984 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3985 src0_param.param_str, src0_param.param_str);
3986 break;
3988 default:
3989 ERR("Write mask should be .x, .y or .xy\n");
3990 break;
3993 return;
3996 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3999 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4001 char dst_mask[6];
4003 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4004 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4005 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4007 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4008 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4009 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4011 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4012 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4014 else
4016 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4017 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4018 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4021 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4023 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4024 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4025 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4029 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4030 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4031 * generate invalid code
4033 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4035 struct glsl_src_param src0_param;
4036 DWORD write_mask;
4038 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4039 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4041 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4044 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4045 * Start a for() loop where src1.y is the initial value of aL,
4046 * increment aL by src1.z for a total of src1.x iterations.
4047 * Need to use a temporary variable for this operation.
4049 /* FIXME: I don't think nested loops will work correctly this way. */
4050 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4052 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4053 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4054 const struct wined3d_shader *shader = ins->ctx->shader;
4055 const struct wined3d_shader_lconst *constant;
4056 struct glsl_src_param src1_param;
4057 const DWORD *control_values = NULL;
4059 if (ins->ctx->reg_maps->shader_version.major < 4)
4061 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
4063 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4064 * class hardware doesn't support real varying indexing, but Microsoft
4065 * designed this feature for Shader model 2.x+. If the loop control is
4066 * known at compile time, the GLSL compiler can unroll the loop, and
4067 * replace indirect addressing with direct addressing. */
4068 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4070 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4072 if (constant->idx == ins->src[1].reg.idx[0].offset)
4074 control_values = constant->value;
4075 break;
4080 if (control_values)
4082 struct wined3d_shader_loop_control loop_control;
4083 loop_control.count = control_values[0];
4084 loop_control.start = control_values[1];
4085 loop_control.step = (int)control_values[2];
4087 if (loop_control.step > 0)
4089 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4090 loop_state->current_depth, loop_control.start,
4091 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
4092 loop_state->current_depth, loop_control.step);
4094 else if (loop_control.step < 0)
4096 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4097 loop_state->current_depth, loop_control.start,
4098 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
4099 loop_state->current_depth, loop_control.step);
4101 else
4103 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4104 loop_state->current_depth, loop_control.start, loop_state->current_depth,
4105 loop_state->current_depth, loop_control.count,
4106 loop_state->current_depth);
4109 else
4111 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4112 loop_state->current_depth, loop_state->current_reg,
4113 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
4114 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
4117 ++loop_state->current_reg;
4119 else
4121 shader_addline(buffer, "for (;;)\n{\n");
4124 ++loop_state->current_depth;
4127 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4129 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4131 shader_addline(ins->ctx->buffer, "}\n");
4133 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4135 --loop_state->current_depth;
4136 --loop_state->current_reg;
4139 if (ins->handler_idx == WINED3DSIH_ENDREP)
4141 --loop_state->current_depth;
4145 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4147 const struct wined3d_shader *shader = ins->ctx->shader;
4148 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4149 const struct wined3d_shader_lconst *constant;
4150 struct glsl_src_param src0_param;
4151 const DWORD *control_values = NULL;
4153 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4154 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4156 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4158 if (constant->idx == ins->src[0].reg.idx[0].offset)
4160 control_values = constant->value;
4161 break;
4166 if (control_values)
4168 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4169 loop_state->current_depth, loop_state->current_depth,
4170 control_values[0], loop_state->current_depth);
4172 else
4174 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4175 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4176 loop_state->current_depth, loop_state->current_depth,
4177 src0_param.param_str, loop_state->current_depth);
4180 ++loop_state->current_depth;
4183 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
4185 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4186 struct glsl_src_param src0_param;
4188 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4189 shader_addline(ins->ctx->buffer, "if (%s(%s)) {\n", condition, src0_param.param_str);
4192 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
4194 struct glsl_src_param src0_param;
4195 struct glsl_src_param src1_param;
4197 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4198 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4200 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
4201 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4204 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
4206 shader_addline(ins->ctx->buffer, "} else {\n");
4209 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
4211 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
4212 shader_glsl_fixup_position(ins->ctx->buffer);
4213 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
4216 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
4218 shader_addline(ins->ctx->buffer, "break;\n");
4221 /* FIXME: According to MSDN the compare is done per component. */
4222 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
4224 struct glsl_src_param src0_param;
4225 struct glsl_src_param src1_param;
4227 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4228 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4230 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
4231 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4234 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
4236 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4237 struct glsl_src_param src_param;
4239 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4240 shader_addline(ins->ctx->buffer, "if (%s(%s)) break;\n", condition, src_param.param_str);
4243 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
4245 shader_addline(ins->ctx->buffer, "}\n");
4246 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
4249 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
4251 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
4254 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
4256 struct glsl_src_param src1_param;
4258 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4259 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
4260 src1_param.param_str, ins->src[0].reg.idx[0].offset);
4263 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
4265 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
4266 * function only suppresses the unhandled instruction warning
4270 /*********************************************
4271 * Pixel Shader Specific Code begins here
4272 ********************************************/
4273 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
4275 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4276 ins->ctx->reg_maps->shader_version.minor);
4277 struct glsl_sample_function sample_function;
4278 DWORD sample_flags = 0;
4279 DWORD resource_idx;
4280 DWORD mask = 0, swizzle;
4281 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4283 /* 1.0-1.4: Use destination register as sampler source.
4284 * 2.0+: Use provided sampler source. */
4285 if (shader_version < WINED3D_SHADER_VERSION(2,0))
4286 resource_idx = ins->dst[0].reg.idx[0].offset;
4287 else
4288 resource_idx = ins->src[1].reg.idx[0].offset;
4290 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4292 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4293 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4294 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4296 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4297 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4299 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4300 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4302 case WINED3D_TTFF_COUNT1:
4303 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4304 break;
4305 case WINED3D_TTFF_COUNT2:
4306 mask = WINED3DSP_WRITEMASK_1;
4307 break;
4308 case WINED3D_TTFF_COUNT3:
4309 mask = WINED3DSP_WRITEMASK_2;
4310 break;
4311 case WINED3D_TTFF_COUNT4:
4312 case WINED3D_TTFF_DISABLE:
4313 mask = WINED3DSP_WRITEMASK_3;
4314 break;
4318 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4320 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4322 if (src_mod == WINED3DSPSM_DZ) {
4323 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4324 mask = WINED3DSP_WRITEMASK_2;
4325 } else if (src_mod == WINED3DSPSM_DW) {
4326 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4327 mask = WINED3DSP_WRITEMASK_3;
4330 else
4332 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4333 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4335 /* ps 2.0 texldp instruction always divides by the fourth component. */
4336 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4337 mask = WINED3DSP_WRITEMASK_3;
4341 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
4342 mask |= sample_function.coord_mask;
4343 sample_function.coord_mask = mask;
4345 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4346 else swizzle = ins->src[1].swizzle;
4348 /* 1.0-1.3: Use destination register as coordinate source.
4349 1.4+: Use provided coordinate source register. */
4350 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4352 char coord_mask[6];
4353 shader_glsl_write_mask_to_str(mask, coord_mask);
4354 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4355 "T%u%s", resource_idx, coord_mask);
4357 else
4359 struct glsl_src_param coord_param;
4360 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4361 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4363 struct glsl_src_param bias;
4364 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4365 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4366 NULL, "%s", coord_param.param_str);
4367 } else {
4368 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4369 "%s", coord_param.param_str);
4372 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4375 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4377 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4378 struct glsl_src_param coord_param, dx_param, dy_param;
4379 struct glsl_sample_function sample_function;
4380 DWORD sampler_idx;
4381 DWORD swizzle = ins->src[1].swizzle;
4383 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
4385 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4386 shader_glsl_tex(ins);
4387 return;
4390 sampler_idx = ins->src[1].reg.idx[0].offset;
4392 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
4393 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4394 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
4395 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
4397 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
4398 NULL, NULL, "%s", coord_param.param_str);
4399 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4402 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4404 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4405 struct glsl_src_param coord_param, lod_param;
4406 struct glsl_sample_function sample_function;
4407 DWORD sampler_idx;
4408 DWORD swizzle = ins->src[1].swizzle;
4410 sampler_idx = ins->src[1].reg.idx[0].offset;
4412 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
4413 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4415 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4417 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
4418 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
4420 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4421 * However, the NVIDIA drivers allow them in fragment shaders as well,
4422 * even without the appropriate extension. */
4423 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4425 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
4426 "%s", coord_param.param_str);
4427 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4430 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4431 unsigned int resource_idx, unsigned int sampler_idx)
4433 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4434 unsigned int i;
4436 for (i = 0; i < sampler_map->count; ++i)
4438 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4439 return entries[i].bind_idx;
4442 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4444 return ~0u;
4447 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
4449 static const unsigned int texture_size_component_count[] =
4451 0, /* WINED3D_SHADER_RESOURCE_NONE */
4452 1, /* WINED3D_SHADER_RESOURCE_BUFFER */
4453 1, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
4454 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
4455 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
4456 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
4457 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
4458 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
4459 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
4460 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
4463 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
4464 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4465 enum wined3d_shader_resource_type resource_type;
4466 unsigned int resource_idx, sampler_bind_idx, i;
4467 enum wined3d_data_type dst_data_type;
4468 struct glsl_src_param lod_param;
4469 char dst_swizzle[6];
4470 DWORD write_mask;
4472 dst_data_type = ins->dst[0].reg.data_type;
4473 if (ins->flags == WINED3DSI_RESINFO_UINT)
4474 dst_data_type = WINED3D_DATA_UINT;
4475 else if (ins->flags)
4476 FIXME("Unhandled flags %#x.\n", ins->flags);
4478 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
4479 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
4481 resource_idx = ins->src[1].reg.idx[0].offset;
4482 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4483 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
4484 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4485 resource_idx, WINED3D_SAMPLER_DEFAULT);
4487 if (resource_type >= ARRAY_SIZE(texture_size_component_count))
4489 ERR("Unexpected resource type %#x.\n", resource_type);
4490 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
4493 if (dst_data_type == WINED3D_DATA_UINT)
4494 shader_addline(ins->ctx->buffer, "uvec4(");
4495 else
4496 shader_addline(ins->ctx->buffer, "vec4(");
4498 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
4499 shader_glsl_get_prefix(version->type), sampler_bind_idx, lod_param.param_str);
4501 for (i = 0; i < 3 - texture_size_component_count[resource_type]; ++i)
4502 shader_addline(ins->ctx->buffer, "0, ");
4504 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
4506 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
4507 shader_glsl_get_prefix(version->type), sampler_bind_idx);
4509 else
4511 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
4512 shader_addline(ins->ctx->buffer, "1");
4515 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
4518 /* FIXME: The current implementation does not handle multisample textures correctly. */
4519 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
4521 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4522 struct glsl_src_param coord_param, lod_param;
4523 struct glsl_sample_function sample_function;
4524 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
4526 if (wined3d_shader_instruction_has_texel_offset(ins))
4527 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4529 resource_idx = ins->src[1].reg.idx[0].offset;
4530 sampler_idx = WINED3D_SAMPLER_DEFAULT;
4532 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4533 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4534 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4535 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4536 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
4537 NULL, NULL, lod_param.param_str, &ins->texel_offset, "%s", coord_param.param_str);
4538 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4541 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
4543 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
4544 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
4545 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4546 struct glsl_sample_function sample_function;
4547 DWORD flags = 0;
4549 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
4550 flags |= WINED3D_GLSL_SAMPLE_GRAD;
4551 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
4552 flags |= WINED3D_GLSL_SAMPLE_LOD;
4553 if (wined3d_shader_instruction_has_texel_offset(ins))
4554 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4556 resource_idx = ins->src[1].reg.idx[0].offset;
4557 sampler_idx = ins->src[2].reg.idx[0].offset;
4559 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4560 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4562 switch (ins->handler_idx)
4564 case WINED3DSIH_SAMPLE:
4565 break;
4566 case WINED3DSIH_SAMPLE_B:
4567 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4568 lod_param_str = lod_param.param_str;
4569 break;
4570 case WINED3DSIH_SAMPLE_GRAD:
4571 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dx_param);
4572 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.coord_mask, &dy_param);
4573 dx_param_str = dx_param.param_str;
4574 dy_param_str = dy_param.param_str;
4575 break;
4576 case WINED3DSIH_SAMPLE_LOD:
4577 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4578 lod_param_str = lod_param.param_str;
4579 break;
4580 default:
4581 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4582 break;
4585 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4586 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
4587 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
4588 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4591 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
4593 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4594 struct glsl_src_param coord_param, compare_param;
4595 struct glsl_sample_function sample_function;
4596 const char *lod_param = NULL;
4597 DWORD flags = 0;
4598 UINT coord_size;
4600 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
4602 lod_param = "0";
4603 flags |= WINED3D_GLSL_SAMPLE_LOD;
4606 if (wined3d_shader_instruction_has_texel_offset(ins))
4607 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4609 resource_idx = ins->src[1].reg.idx[0].offset;
4610 sampler_idx = ins->src[2].reg.idx[0].offset;
4612 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4613 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4614 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
4615 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
4616 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4617 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
4618 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
4619 coord_size, coord_param.param_str, compare_param.param_str);
4620 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4623 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
4625 /* FIXME: Make this work for more than just 2D textures */
4626 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4627 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4629 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
4631 char dst_mask[6];
4633 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4634 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
4635 ins->dst[0].reg.idx[0].offset, dst_mask);
4637 else
4639 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4640 DWORD reg = ins->src[0].reg.idx[0].offset;
4641 char dst_swizzle[6];
4643 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
4645 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
4647 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4648 struct glsl_src_param div_param;
4649 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
4651 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
4653 if (mask_size > 1)
4654 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4655 else
4656 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4658 else
4660 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
4665 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4666 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4667 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4668 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
4670 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4671 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4672 struct glsl_sample_function sample_function;
4673 struct glsl_src_param src0_param;
4674 UINT mask_size;
4676 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4678 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4679 * scalar, and projected sampling would require 4.
4681 * It is a dependent read - not valid with conditional NP2 textures
4683 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4684 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4686 switch(mask_size)
4688 case 1:
4689 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4690 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
4691 break;
4693 case 2:
4694 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4695 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
4696 break;
4698 case 3:
4699 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4700 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
4701 break;
4703 default:
4704 FIXME("Unexpected mask size %u\n", mask_size);
4705 break;
4707 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4710 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4711 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4712 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
4714 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4715 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4716 struct glsl_src_param src0_param;
4717 DWORD dst_mask;
4718 unsigned int mask_size;
4720 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4721 mask_size = shader_glsl_get_write_mask_size(dst_mask);
4722 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4724 if (mask_size > 1) {
4725 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
4726 } else {
4727 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
4731 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4732 * Calculate the depth as dst.x / dst.y */
4733 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
4735 struct glsl_dst_param dst_param;
4737 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4739 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4740 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4741 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4742 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4743 * >= 1.0 or < 0.0
4745 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4746 dst_param.reg_name, dst_param.reg_name);
4749 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4750 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4751 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4752 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4754 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
4756 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4757 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4758 struct glsl_src_param src0_param;
4760 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4762 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
4763 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4766 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4767 * Calculate the 1st of a 2-row matrix multiplication. */
4768 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
4770 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4771 DWORD reg = ins->dst[0].reg.idx[0].offset;
4772 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4773 struct glsl_src_param src0_param;
4775 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4776 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4779 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4780 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4781 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
4783 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4784 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4785 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4786 DWORD reg = ins->dst[0].reg.idx[0].offset;
4787 struct glsl_src_param src0_param;
4789 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4790 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
4791 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
4794 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
4796 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4797 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4798 struct glsl_sample_function sample_function;
4799 DWORD reg = ins->dst[0].reg.idx[0].offset;
4800 struct glsl_src_param src0_param;
4802 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4803 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4805 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4807 /* Sample the texture using the calculated coordinates */
4808 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
4809 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4812 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
4813 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
4814 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
4816 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4817 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4818 struct glsl_sample_function sample_function;
4819 DWORD reg = ins->dst[0].reg.idx[0].offset;
4820 struct glsl_src_param src0_param;
4822 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4823 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4825 /* Dependent read, not valid with conditional NP2 */
4826 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4828 /* Sample the texture using the calculated coordinates */
4829 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
4830 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4832 tex_mx->current_row = 0;
4835 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4836 * Perform the 3rd row of a 3x3 matrix multiply */
4837 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
4839 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4840 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4841 DWORD reg = ins->dst[0].reg.idx[0].offset;
4842 struct glsl_src_param src0_param;
4843 char dst_mask[6];
4845 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4847 shader_glsl_append_dst(ins->ctx->buffer, ins);
4848 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4849 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
4851 tex_mx->current_row = 0;
4854 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4855 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4856 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
4858 struct glsl_src_param src0_param;
4859 struct glsl_src_param src1_param;
4860 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4861 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4862 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4863 struct glsl_sample_function sample_function;
4864 DWORD reg = ins->dst[0].reg.idx[0].offset;
4865 char coord_mask[6];
4867 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4868 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4870 /* Perform the last matrix multiply operation */
4871 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4872 /* Reflection calculation */
4873 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
4875 /* Dependent read, not valid with conditional NP2 */
4876 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4877 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4879 /* Sample the texture */
4880 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4881 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
4882 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4884 tex_mx->current_row = 0;
4887 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4888 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4889 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
4891 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4892 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4893 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4894 struct glsl_sample_function sample_function;
4895 DWORD reg = ins->dst[0].reg.idx[0].offset;
4896 struct glsl_src_param src0_param;
4897 char coord_mask[6];
4899 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4901 /* Perform the last matrix multiply operation */
4902 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
4904 /* Construct the eye-ray vector from w coordinates */
4905 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
4906 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
4907 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4909 /* Dependent read, not valid with conditional NP2 */
4910 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4911 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4913 /* Sample the texture using the calculated coordinates */
4914 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4915 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
4916 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4918 tex_mx->current_row = 0;
4921 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4922 * Apply a fake bump map transform.
4923 * texbem is pshader <= 1.3 only, this saves a few version checks
4925 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
4927 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4928 struct glsl_sample_function sample_function;
4929 struct glsl_src_param coord_param;
4930 DWORD sampler_idx;
4931 DWORD mask;
4932 DWORD flags;
4933 char coord_mask[6];
4935 sampler_idx = ins->dst[0].reg.idx[0].offset;
4936 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4937 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4939 /* Dependent read, not valid with conditional NP2 */
4940 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4941 mask = sample_function.coord_mask;
4943 shader_glsl_write_mask_to_str(mask, coord_mask);
4945 /* With projected textures, texbem only divides the static texture coord,
4946 * not the displacement, so we can't let GL handle this. */
4947 if (flags & WINED3D_PSARGS_PROJECTED)
4949 DWORD div_mask=0;
4950 char coord_div_mask[3];
4951 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4953 case WINED3D_TTFF_COUNT1:
4954 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4955 break;
4956 case WINED3D_TTFF_COUNT2:
4957 div_mask = WINED3DSP_WRITEMASK_1;
4958 break;
4959 case WINED3D_TTFF_COUNT3:
4960 div_mask = WINED3DSP_WRITEMASK_2;
4961 break;
4962 case WINED3D_TTFF_COUNT4:
4963 case WINED3D_TTFF_DISABLE:
4964 div_mask = WINED3DSP_WRITEMASK_3;
4965 break;
4967 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4968 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4971 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4973 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4974 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4975 coord_param.param_str, coord_mask);
4977 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4979 struct glsl_src_param luminance_param;
4980 struct glsl_dst_param dst_param;
4982 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4983 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4985 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4986 dst_param.reg_name, dst_param.mask_str,
4987 luminance_param.param_str, sampler_idx, sampler_idx);
4989 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4992 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4994 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4995 struct glsl_src_param src0_param, src1_param;
4997 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4998 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
5000 shader_glsl_append_dst(ins->ctx->buffer, ins);
5001 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
5002 src0_param.param_str, sampler_idx, src1_param.param_str);
5005 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
5006 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
5007 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
5009 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5010 struct glsl_sample_function sample_function;
5011 struct glsl_src_param src0_param;
5013 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
5015 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5016 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5017 "%s.wx", src0_param.reg_name);
5018 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5021 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
5022 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
5023 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
5025 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5026 struct glsl_sample_function sample_function;
5027 struct glsl_src_param src0_param;
5029 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
5031 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5032 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5033 "%s.yz", src0_param.reg_name);
5034 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5037 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
5038 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
5039 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
5041 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5042 struct glsl_sample_function sample_function;
5043 struct glsl_src_param src0_param;
5045 /* Dependent read, not valid with conditional NP2 */
5046 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5047 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
5049 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5050 "%s", src0_param.param_str);
5051 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5054 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
5055 * If any of the first 3 components are < 0, discard this pixel */
5056 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
5058 if (ins->ctx->reg_maps->shader_version.major >= 4)
5060 struct glsl_src_param src_param;
5062 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5063 shader_addline(ins->ctx->buffer, "if (bool(floatBitsToUint(%s))) discard;\n", src_param.param_str);
5065 else
5067 struct glsl_dst_param dst_param;
5069 /* The argument is a destination parameter, and no writemasks are allowed */
5070 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5072 /* 2.0 shaders compare all 4 components in texkill. */
5073 if (ins->ctx->reg_maps->shader_version.major >= 2)
5074 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
5075 /* 1.x shaders only compare the first 3 components, probably due to
5076 * the nature of the texkill instruction as a tex* instruction, and
5077 * phase, which kills all .w components. Even if all 4 components are
5078 * defined, only the first 3 are used. */
5079 else
5080 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
5084 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
5085 * dst = dot2(src0, src1) + src2 */
5086 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
5088 struct glsl_src_param src0_param;
5089 struct glsl_src_param src1_param;
5090 struct glsl_src_param src2_param;
5091 DWORD write_mask;
5092 unsigned int mask_size;
5094 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5095 mask_size = shader_glsl_get_write_mask_size(write_mask);
5097 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
5098 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
5099 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
5101 if (mask_size > 1) {
5102 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
5103 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
5104 } else {
5105 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
5106 src0_param.param_str, src1_param.param_str, src2_param.param_str);
5110 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
5111 const struct wined3d_shader_signature *input_signature,
5112 const struct wined3d_shader_reg_maps *reg_maps,
5113 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
5115 unsigned int i;
5117 for (i = 0; i < input_signature->element_count; ++i)
5119 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5120 const char *semantic_name;
5121 UINT semantic_idx;
5122 char reg_mask[6];
5124 /* Unused */
5125 if (!(reg_maps->input_registers & (1u << input->register_idx)))
5126 continue;
5128 semantic_name = input->semantic_name;
5129 semantic_idx = input->semantic_idx;
5130 shader_glsl_write_mask_to_str(input->mask, reg_mask);
5132 if (args->vp_mode == vertexshader)
5134 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
5135 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
5136 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5137 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5138 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
5139 else
5140 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
5141 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5142 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
5144 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5146 if (args->pointsprite)
5147 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
5148 shader->u.ps.input_reg_map[input->register_idx]);
5149 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
5150 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
5151 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5152 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
5153 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
5154 else
5155 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5156 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5158 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
5160 if (!semantic_idx)
5161 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
5162 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5163 else if (semantic_idx == 1)
5164 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
5165 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5166 else
5167 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5168 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5170 else
5172 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5173 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5178 /*********************************************
5179 * Vertex Shader Specific Code begins here
5180 ********************************************/
5182 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
5184 struct glsl_program_key key;
5186 key.vs_id = entry->vs.id;
5187 key.gs_id = entry->gs.id;
5188 key.ps_id = entry->ps.id;
5190 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
5192 ERR("Failed to insert program entry.\n");
5196 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
5197 GLuint vs_id, GLuint gs_id, GLuint ps_id)
5199 struct wine_rb_entry *entry;
5200 struct glsl_program_key key;
5202 key.vs_id = vs_id;
5203 key.gs_id = gs_id;
5204 key.ps_id = ps_id;
5206 entry = wine_rb_get(&priv->program_lookup, &key);
5207 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
5210 /* Context activation is done by the caller. */
5211 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
5212 struct glsl_shader_prog_link *entry)
5214 struct glsl_program_key key;
5216 key.vs_id = entry->vs.id;
5217 key.gs_id = entry->gs.id;
5218 key.ps_id = entry->ps.id;
5219 wine_rb_remove(&priv->program_lookup, &key);
5221 GL_EXTCALL(glDeleteProgram(entry->id));
5222 if (entry->vs.id)
5223 list_remove(&entry->vs.shader_entry);
5224 if (entry->gs.id)
5225 list_remove(&entry->gs.shader_entry);
5226 if (entry->ps.id)
5227 list_remove(&entry->ps.shader_entry);
5228 HeapFree(GetProcessHeap(), 0, entry);
5231 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
5232 const struct wined3d_gl_info *gl_info, const DWORD *map,
5233 const struct wined3d_shader_signature *input_signature,
5234 const struct wined3d_shader_reg_maps *reg_maps_in,
5235 const struct wined3d_shader_signature *output_signature,
5236 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
5238 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5239 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5240 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5241 unsigned int in_count = vec4_varyings(3, gl_info);
5242 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
5243 DWORD in_idx, *set = NULL;
5244 unsigned int i, j;
5245 char reg_mask[6];
5247 set = wined3d_calloc(max_varyings, sizeof(*set));
5249 for (i = 0; i < input_signature->element_count; ++i)
5251 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5253 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
5254 continue;
5256 in_idx = map[input->register_idx];
5257 /* Declared, but not read register */
5258 if (in_idx == ~0u)
5259 continue;
5260 if (in_idx >= max_varyings)
5262 FIXME("More input varyings declared than supported, expect issues.\n");
5263 continue;
5266 if (in_idx == in_count)
5267 string_buffer_sprintf(destination, "gl_FrontColor");
5268 else if (in_idx == in_count + 1)
5269 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5270 else
5271 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
5273 if (!set[in_idx])
5274 set[in_idx] = ~0u;
5276 for (j = 0; j < output_signature->element_count; ++j)
5278 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
5279 DWORD mask;
5281 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
5282 || input->semantic_idx != output->semantic_idx
5283 || strcmp(input->semantic_name, output->semantic_name)
5284 || !(mask = input->mask & output->mask))
5285 continue;
5287 if (set[in_idx] == ~0u)
5288 set[in_idx] = 0;
5289 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
5290 shader_glsl_write_mask_to_str(mask, reg_mask);
5292 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
5293 destination->buffer, reg_mask, output->register_idx, reg_mask);
5297 for (i = 0; i < max_varyings; ++i)
5299 unsigned int size;
5301 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
5302 continue;
5304 if (set[i] == ~0u)
5305 set[i] = 0;
5307 size = 0;
5308 if (!(set[i] & WINED3DSP_WRITEMASK_0))
5309 reg_mask[size++] = 'x';
5310 if (!(set[i] & WINED3DSP_WRITEMASK_1))
5311 reg_mask[size++] = 'y';
5312 if (!(set[i] & WINED3DSP_WRITEMASK_2))
5313 reg_mask[size++] = 'z';
5314 if (!(set[i] & WINED3DSP_WRITEMASK_3))
5315 reg_mask[size++] = 'w';
5316 reg_mask[size] = '\0';
5318 if (i == in_count)
5319 string_buffer_sprintf(destination, "gl_FrontColor");
5320 else if (i == in_count + 1)
5321 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5322 else
5323 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
5325 if (size == 1)
5326 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
5327 else
5328 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
5331 HeapFree(GetProcessHeap(), 0, set);
5332 string_buffer_release(&priv->string_buffers, destination);
5335 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
5336 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
5337 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
5339 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5340 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5341 char reg_mask[6];
5342 unsigned int i;
5344 for (i = 0; i < output_signature->element_count; ++i)
5346 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
5348 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
5349 continue;
5351 if (output->register_idx >= input_count)
5352 continue;
5354 string_buffer_sprintf(destination, "%s[%u]", out_array_name, output->register_idx);
5356 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5358 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
5359 destination->buffer, reg_mask, output->register_idx, reg_mask);
5362 string_buffer_release(&priv->string_buffers, destination);
5365 /* Context activation is done by the caller. */
5366 static void shader_glsl_generate_vs_gs_setup(struct shader_glsl_priv *priv,
5367 const struct wined3d_shader *vs, unsigned int input_count,
5368 const struct wined3d_gl_info *gl_info)
5370 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5371 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5373 if (legacy_context)
5374 shader_addline(buffer, "varying out vec4 gs_in[%u];\n", input_count);
5375 else
5376 shader_addline(buffer, "out vs_gs_iface { vec4 gs_in[%u]; } gs_in;\n", input_count);
5377 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
5379 shader_glsl_setup_sm4_shader_output(priv, input_count, &vs->output_signature, &vs->reg_maps,
5380 legacy_context ? "gs_in" : "gs_in.gs_in");
5382 shader_addline(buffer, "}\n");
5385 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
5386 const struct wined3d_gl_info *gl_info, const DWORD *map,
5387 const struct wined3d_shader_signature *input_signature,
5388 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
5389 const struct wined3d_shader_signature *output_signature,
5390 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
5392 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5393 const char *semantic_name;
5394 UINT semantic_idx;
5395 char reg_mask[6];
5396 unsigned int i;
5398 /* First, sort out position and point size system values. */
5399 for (i = 0; i < output_signature->element_count; ++i)
5401 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
5403 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
5404 continue;
5406 semantic_name = output->semantic_name;
5407 semantic_idx = output->semantic_idx;
5408 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5410 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
5412 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
5413 reg_mask, output->register_idx, reg_mask);
5415 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5417 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
5418 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
5422 /* Then, setup the pixel shader input. */
5423 if (reg_maps_out->shader_version.major < 4)
5424 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
5425 output_signature, reg_maps_out, "ps_link");
5426 else
5427 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "ps_link");
5430 /* Context activation is done by the caller. */
5431 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
5432 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
5433 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
5435 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5436 GLuint ret;
5437 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
5438 unsigned int i;
5439 const char *semantic_name;
5440 UINT semantic_idx;
5441 char reg_mask[6];
5442 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5444 string_buffer_clear(buffer);
5446 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &vs->reg_maps.shader_version));
5448 if (per_vertex_point_size)
5450 shader_addline(buffer, "uniform struct\n{\n");
5451 shader_addline(buffer, " float size_min;\n");
5452 shader_addline(buffer, " float size_max;\n");
5453 shader_addline(buffer, "} ffp_point;\n");
5456 if (ps_major < 3)
5458 DWORD colors_written_mask[2] = {0};
5459 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
5461 if (!legacy_context)
5463 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
5464 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
5465 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
5466 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
5469 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
5471 for (i = 0; i < vs->output_signature.element_count; ++i)
5473 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5474 DWORD write_mask;
5476 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5477 continue;
5479 semantic_name = output->semantic_name;
5480 semantic_idx = output->semantic_idx;
5481 write_mask = output->mask;
5482 shader_glsl_write_mask_to_str(write_mask, reg_mask);
5484 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
5486 if (legacy_context)
5487 shader_addline(buffer, "gl_Front%sColor%s = shader_out[%u]%s;\n",
5488 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
5489 else
5490 shader_addline(buffer, "ffp_varying_%s%s = clamp(shader_out[%u]%s, 0.0, 1.0);\n",
5491 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
5493 colors_written_mask[semantic_idx] = write_mask;
5495 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5497 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
5498 reg_mask, output->register_idx, reg_mask);
5500 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5502 if (semantic_idx < MAX_TEXTURES)
5504 shader_addline(buffer, "%s[%u]%s = shader_out[%u]%s;\n",
5505 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
5506 semantic_idx, reg_mask, output->register_idx, reg_mask);
5507 texcoords_written_mask[semantic_idx] = write_mask;
5510 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5512 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
5513 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
5515 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
5517 shader_addline(buffer, "%s = clamp(shader_out[%u].%c, 0.0, 1.0);\n",
5518 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
5519 output->register_idx, reg_mask[1]);
5523 for (i = 0; i < 2; ++i)
5525 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5527 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5528 if (!i)
5529 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
5530 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
5531 reg_mask, reg_mask);
5532 else
5533 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
5534 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
5535 reg_mask, reg_mask);
5538 for (i = 0; i < MAX_TEXTURES; ++i)
5540 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
5541 continue;
5543 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5545 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
5546 && !texcoords_written_mask[i])
5547 continue;
5549 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5550 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
5551 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
5555 else
5557 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
5559 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
5560 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
5561 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
5562 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
5565 shader_addline(buffer, "}\n");
5567 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5568 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
5569 shader_glsl_compile(gl_info, ret, buffer->buffer);
5571 return ret;
5574 static void shader_glsl_generate_sm4_rasterizer_input_setup(struct shader_glsl_priv *priv,
5575 const struct wined3d_shader *shader, unsigned int input_count,
5576 const struct wined3d_gl_info *gl_info)
5578 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5580 if (input_count)
5581 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", min(vec4_varyings(4, gl_info), input_count));
5583 shader_addline(buffer, "void setup_%s_output(in vec4 shader_out[%u])\n{\n",
5584 shader_glsl_get_prefix(shader->reg_maps.shader_version.type), shader->limits->packed_output);
5586 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
5587 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
5589 shader_addline(buffer, "}\n");
5592 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
5593 const struct wined3d_gl_info *gl_info)
5595 const char *output = get_fragment_output(gl_info);
5597 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
5598 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
5599 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
5600 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
5601 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
5602 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
5605 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
5606 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
5608 const char *output = get_fragment_output(gl_info);
5610 switch (mode)
5612 case WINED3D_FFP_PS_FOG_OFF:
5613 return;
5615 case WINED3D_FFP_PS_FOG_LINEAR:
5616 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
5617 break;
5619 case WINED3D_FFP_PS_FOG_EXP:
5620 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
5621 break;
5623 case WINED3D_FFP_PS_FOG_EXP2:
5624 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
5625 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
5626 break;
5628 default:
5629 ERR("Invalid fog mode %#x.\n", mode);
5630 return;
5633 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
5634 output, output);
5637 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
5638 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
5640 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
5641 * flipping all the operators here, just negate the comparison below. */
5642 static const char * const comparison_operator[] =
5644 "", /* WINED3D_CMP_NEVER */
5645 "<", /* WINED3D_CMP_LESS */
5646 "==", /* WINED3D_CMP_EQUAL */
5647 "<=", /* WINED3D_CMP_LESSEQUAL */
5648 ">", /* WINED3D_CMP_GREATER */
5649 "!=", /* WINED3D_CMP_NOTEQUAL */
5650 ">=", /* WINED3D_CMP_GREATEREQUAL */
5651 "" /* WINED3D_CMP_ALWAYS */
5654 if (alpha_func == WINED3D_CMP_ALWAYS)
5655 return;
5657 if (alpha_func != WINED3D_CMP_NEVER)
5658 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
5659 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
5660 shader_addline(buffer, " discard;\n");
5663 /* Context activation is done by the caller. */
5664 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
5665 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5666 const struct wined3d_shader *shader,
5667 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
5669 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5670 const struct wined3d_gl_info *gl_info = context->gl_info;
5671 const DWORD *function = shader->function;
5672 struct shader_glsl_ctx_priv priv_ctx;
5673 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5675 /* Create the hw GLSL shader object and assign it as the shader->prgId */
5676 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5678 memset(&priv_ctx, 0, sizeof(priv_ctx));
5679 priv_ctx.cur_ps_args = args;
5680 priv_ctx.cur_np2fixup_info = np2fixup_info;
5681 priv_ctx.string_buffers = string_buffers;
5683 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5685 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5686 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5687 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5688 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
5689 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5690 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5691 /* The spec says that it doesn't have to be explicitly enabled, but the
5692 * nvidia drivers write a warning if we don't do so. */
5693 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5694 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5695 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5696 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5697 if (gl_info->supported[EXT_GPU_SHADER4])
5698 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5699 if (gl_info->supported[EXT_TEXTURE_ARRAY])
5700 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
5702 /* Base Declarations */
5703 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5705 shader_addline(buffer, "void main()\n{\n");
5707 /* Direct3D applications expect integer vPos values, while OpenGL drivers
5708 * add approximately 0.5. This causes off-by-one problems as spotted by
5709 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
5710 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
5711 * causes precision troubles when we just subtract 0.5.
5713 * To deal with that, just floor() the position. This will eliminate the
5714 * fraction on all cards.
5716 * TODO: Test how this behaves with multisampling.
5718 * An advantage of floor is that it works even if the driver doesn't add
5719 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
5720 * to return in gl_FragCoord, even though coordinates specify the pixel
5721 * centers instead of the pixel corners. This code will behave correctly
5722 * on drivers that returns integer values. */
5723 if (reg_maps->vpos)
5725 if (shader->device->wined3d->flags & WINED3D_PIXEL_CENTER_INTEGER)
5726 shader_addline(buffer,
5727 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
5728 else
5729 shader_addline(buffer,
5730 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
5733 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
5735 unsigned int i;
5736 WORD map = reg_maps->texcoord;
5738 if (legacy_context)
5740 if (glsl_is_color_reg_read(shader, 0))
5741 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
5742 if (glsl_is_color_reg_read(shader, 1))
5743 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
5746 for (i = 0; map; map >>= 1, ++i)
5748 if (map & 1)
5750 if (args->pointsprite)
5751 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
5752 else if (args->texcoords_initialized & (1u << i))
5753 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
5754 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
5755 else
5756 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
5757 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
5761 if (legacy_context)
5762 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
5765 /* Pack 3.0 inputs */
5766 if (reg_maps->shader_version.major >= 3)
5767 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
5769 /* Base Shader Body */
5770 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5772 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
5773 if (reg_maps->shader_version.major < 2)
5774 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
5776 if (args->srgb_correction)
5777 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
5779 /* SM < 3 does not replace the fog stage. */
5780 if (reg_maps->shader_version.major < 3)
5781 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
5783 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
5785 shader_addline(buffer, "}\n");
5787 TRACE("Compiling shader object %u.\n", shader_id);
5788 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5790 return shader_id;
5793 /* Context activation is done by the caller. */
5794 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
5795 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
5797 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
5798 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5799 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5800 const struct wined3d_gl_info *gl_info = context->gl_info;
5801 const DWORD *function = shader->function;
5802 struct shader_glsl_ctx_priv priv_ctx;
5803 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5805 /* Create the hw GLSL shader program and assign it as the shader->prgId */
5806 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5808 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5810 if (gl_info->supported[ARB_DRAW_INSTANCED])
5811 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
5812 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5813 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5814 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5815 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5816 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5817 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5818 if (gl_info->supported[EXT_GPU_SHADER4])
5819 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5820 if (gl_info->supported[EXT_TEXTURE_ARRAY])
5821 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
5823 memset(&priv_ctx, 0, sizeof(priv_ctx));
5824 priv_ctx.cur_vs_args = args;
5825 priv_ctx.string_buffers = string_buffers;
5827 /* Base Declarations */
5828 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5830 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL)
5831 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
5833 if (reg_maps->shader_version.major >= 4)
5835 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL)
5836 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->next_shader_input_count, gl_info);
5837 else if (args->next_shader_type == WINED3D_SHADER_TYPE_GEOMETRY)
5838 shader_glsl_generate_vs_gs_setup(priv, shader, args->next_shader_input_count, gl_info);
5841 shader_addline(buffer, "void main()\n{\n");
5843 /* Base Shader Body */
5844 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5846 /* Unpack outputs */
5847 shader_addline(buffer, "setup_vs_output(vs_out);\n");
5849 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
5850 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
5851 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
5852 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
5854 if (reg_maps->shader_version.major < 3)
5856 if (args->fog_src == VS_FOG_Z)
5857 shader_addline(buffer, "%s = gl_Position.z;\n",
5858 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5859 else if (!reg_maps->fog)
5860 shader_addline(buffer, "%s = 0.0;\n",
5861 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5864 /* We always store the clipplanes without y inversion */
5865 if (args->clip_enabled)
5866 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
5868 if (args->point_size && !args->per_vertex_point_size)
5869 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
5871 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL)
5872 shader_glsl_fixup_position(buffer);
5874 shader_addline(buffer, "}\n");
5876 TRACE("Compiling shader object %u.\n", shader_id);
5877 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5879 return shader_id;
5882 /* Context activation is done by the caller. */
5883 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
5884 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
5886 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
5887 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5888 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5889 const struct wined3d_gl_info *gl_info = context->gl_info;
5890 const DWORD *function = shader->function;
5891 struct shader_glsl_ctx_priv priv_ctx;
5892 GLuint shader_id;
5894 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
5896 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5898 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
5899 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
5900 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5901 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5902 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5903 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5904 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5905 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5906 if (gl_info->supported[EXT_GPU_SHADER4])
5907 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5908 if (gl_info->supported[EXT_TEXTURE_ARRAY])
5909 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
5911 memset(&priv_ctx, 0, sizeof(priv_ctx));
5912 priv_ctx.string_buffers = string_buffers;
5913 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5914 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
5915 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->ps_input_count, gl_info);
5916 shader_addline(buffer, "void main()\n{\n");
5917 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5918 shader_addline(buffer, "}\n");
5920 TRACE("Compiling shader object %u.\n", shader_id);
5921 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5923 return shader_id;
5926 static GLuint find_glsl_pshader(const struct wined3d_context *context,
5927 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5928 struct wined3d_shader *shader,
5929 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
5931 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
5932 struct glsl_shader_private *shader_data;
5933 struct ps_np2fixup_info *np2fixup;
5934 UINT i;
5935 DWORD new_size;
5936 GLuint ret;
5938 if (!shader->backend_data)
5940 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5941 if (!shader->backend_data)
5943 ERR("Failed to allocate backend data.\n");
5944 return 0;
5947 shader_data = shader->backend_data;
5948 gl_shaders = shader_data->gl_shaders.ps;
5950 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5951 * so a linear search is more performant than a hashmap or a binary search
5952 * (cache coherency etc)
5954 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5956 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
5958 if (args->np2_fixup)
5959 *np2fixup_info = &gl_shaders[i].np2fixup;
5960 return gl_shaders[i].id;
5964 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5965 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5966 if (shader_data->num_gl_shaders)
5968 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5969 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
5970 new_size * sizeof(*gl_shaders));
5972 else
5974 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5975 new_size = 1;
5978 if(!new_array) {
5979 ERR("Out of memory\n");
5980 return 0;
5982 shader_data->gl_shaders.ps = new_array;
5983 shader_data->shader_array_size = new_size;
5984 gl_shaders = new_array;
5987 gl_shaders[shader_data->num_gl_shaders].args = *args;
5989 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
5990 memset(np2fixup, 0, sizeof(*np2fixup));
5991 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
5993 pixelshader_update_resource_types(shader, args->tex_types);
5995 string_buffer_clear(buffer);
5996 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
5997 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5999 return ret;
6002 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
6003 const DWORD use_map)
6005 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
6006 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
6007 if (stored->point_size != new->point_size)
6008 return FALSE;
6009 if (stored->per_vertex_point_size != new->per_vertex_point_size)
6010 return FALSE;
6011 if (stored->flatshading != new->flatshading)
6012 return FALSE;
6013 if (stored->next_shader_type != new->next_shader_type)
6014 return FALSE;
6015 if (stored->next_shader_input_count != new->next_shader_input_count)
6016 return FALSE;
6017 return stored->fog_src == new->fog_src;
6020 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
6021 struct wined3d_shader *shader, const struct vs_compile_args *args)
6023 UINT i;
6024 DWORD new_size;
6025 DWORD use_map = context->stream_info.use_map;
6026 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
6027 struct glsl_shader_private *shader_data;
6028 GLuint ret;
6030 if (!shader->backend_data)
6032 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
6033 if (!shader->backend_data)
6035 ERR("Failed to allocate backend data.\n");
6036 return 0;
6039 shader_data = shader->backend_data;
6040 gl_shaders = shader_data->gl_shaders.vs;
6042 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
6043 * so a linear search is more performant than a hashmap or a binary search
6044 * (cache coherency etc)
6046 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6048 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
6049 return gl_shaders[i].id;
6052 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6054 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
6055 if (shader_data->num_gl_shaders)
6057 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
6058 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
6059 new_size * sizeof(*gl_shaders));
6061 else
6063 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
6064 new_size = 1;
6067 if(!new_array) {
6068 ERR("Out of memory\n");
6069 return 0;
6071 shader_data->gl_shaders.vs = new_array;
6072 shader_data->shader_array_size = new_size;
6073 gl_shaders = new_array;
6076 gl_shaders[shader_data->num_gl_shaders].args = *args;
6078 string_buffer_clear(&priv->shader_buffer);
6079 ret = shader_glsl_generate_vshader(context, priv, shader, args);
6080 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6082 return ret;
6085 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
6086 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
6088 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
6089 struct glsl_shader_private *shader_data;
6090 unsigned int i, new_size;
6091 GLuint ret;
6093 if (!shader->backend_data)
6095 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
6097 ERR("Failed to allocate backend data.\n");
6098 return 0;
6101 shader_data = shader->backend_data;
6102 gl_shaders = shader_data->gl_shaders.gs;
6104 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6106 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
6107 return gl_shaders[i].id;
6110 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6112 if (shader_data->num_gl_shaders)
6114 new_size = shader_data->shader_array_size + 1;
6115 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.gs,
6116 new_size * sizeof(*new_array));
6118 else
6120 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
6121 new_size = 1;
6124 if (!new_array)
6126 ERR("Failed to allocate GL shaders array.\n");
6127 return 0;
6129 shader_data->gl_shaders.gs = new_array;
6130 shader_data->shader_array_size = new_size;
6131 gl_shaders = new_array;
6133 string_buffer_clear(&priv->shader_buffer);
6134 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
6135 gl_shaders[shader_data->num_gl_shaders].args = *args;
6136 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6138 return ret;
6141 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
6143 switch (mcs)
6145 case WINED3D_MCS_MATERIAL:
6146 return material;
6147 case WINED3D_MCS_COLOR1:
6148 return "ffp_attrib_diffuse";
6149 case WINED3D_MCS_COLOR2:
6150 return "ffp_attrib_specular";
6151 default:
6152 ERR("Invalid material color source %#x.\n", mcs);
6153 return "<invalid>";
6157 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
6158 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
6160 const char *diffuse, *specular, *emissive, *ambient;
6161 enum wined3d_light_type light_type;
6162 unsigned int i;
6164 if (!settings->lighting)
6166 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
6167 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
6168 return;
6171 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
6172 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
6173 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
6174 shader_addline(buffer, "vec3 dir, dst;\n");
6175 shader_addline(buffer, "float att, t;\n");
6177 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
6178 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
6179 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
6180 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
6182 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
6184 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
6185 switch (light_type)
6187 case WINED3D_LIGHT_POINT:
6188 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
6189 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
6190 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
6191 shader_addline(buffer, "dst.x = 1.0;\n");
6192 if (legacy_lighting)
6194 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
6195 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
6197 else
6199 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
6201 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6202 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", i, i, i);
6203 if (!legacy_lighting)
6204 shader_addline(buffer, "att = 1.0 / att;\n");
6205 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
6206 if (!settings->normal)
6208 if (!legacy_lighting)
6209 shader_addline(buffer, "}\n");
6210 break;
6212 shader_addline(buffer, "dir = normalize(dir);\n");
6213 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
6214 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
6215 if (settings->localviewer)
6216 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6217 else
6218 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6219 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
6220 " * ffp_light[%u].specular) * att;\n", i);
6221 if (!legacy_lighting)
6222 shader_addline(buffer, "}\n");
6223 break;
6225 case WINED3D_LIGHT_SPOT:
6226 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
6227 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
6228 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
6229 shader_addline(buffer, "dst.x = 1.0;\n");
6230 if (legacy_lighting)
6232 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
6233 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
6235 else
6237 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
6239 shader_addline(buffer, "dir = normalize(dir);\n");
6240 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", i);
6241 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", i);
6242 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", i);
6243 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
6244 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
6245 i, i, i, i);
6246 if (legacy_lighting)
6247 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6248 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
6249 i, i, i);
6250 else
6251 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6252 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
6253 i, i, i);
6254 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
6255 if (!settings->normal)
6257 if (!legacy_lighting)
6258 shader_addline(buffer, "}\n");
6259 break;
6261 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
6262 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
6263 if (settings->localviewer)
6264 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6265 else
6266 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6267 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
6268 " * ffp_light[%u].specular) * att;\n", i);
6269 if (!legacy_lighting)
6270 shader_addline(buffer, "}\n");
6271 break;
6273 case WINED3D_LIGHT_DIRECTIONAL:
6274 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
6275 if (!settings->normal)
6276 break;
6277 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", i);
6278 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6279 " * ffp_light[%u].diffuse.xyz;\n", i);
6280 /* TODO: In the non-local viewer case the halfvector is constant
6281 * and could be precomputed and stored in a uniform. */
6282 if (settings->localviewer)
6283 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6284 else
6285 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6286 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6287 " * ffp_light[%u].specular;\n", i);
6288 break;
6290 case WINED3D_LIGHT_PARALLELPOINT:
6291 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
6292 if (!settings->normal)
6293 break;
6294 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", i);
6295 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6296 " * ffp_light[%u].diffuse.xyz;\n", i);
6297 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6298 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6299 " * ffp_light[%u].specular;\n", i);
6300 break;
6302 default:
6303 if (light_type)
6304 FIXME("Unhandled light type %#x.\n", light_type);
6305 continue;
6309 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
6310 ambient, diffuse, emissive);
6311 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
6312 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
6315 /* Context activation is done by the caller. */
6316 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
6317 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
6319 static const struct attrib_info
6321 const char type[6];
6322 const char name[24];
6324 attrib_info[] =
6326 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
6327 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
6328 /* TODO: Indexed vertex blending */
6329 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
6330 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
6331 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
6332 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
6333 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
6335 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6336 BOOL legacy_lighting = priv->legacy_lighting;
6337 GLuint shader_obj;
6338 unsigned int i;
6339 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6340 BOOL output_legacy_fogcoord = legacy_context;
6342 string_buffer_clear(buffer);
6344 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6346 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
6348 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
6350 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
6352 shader_addline(buffer, "\n");
6354 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
6355 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
6356 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
6357 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
6359 shader_addline(buffer, "uniform struct\n{\n");
6360 shader_addline(buffer, " vec4 emissive;\n");
6361 shader_addline(buffer, " vec4 ambient;\n");
6362 shader_addline(buffer, " vec4 diffuse;\n");
6363 shader_addline(buffer, " vec4 specular;\n");
6364 shader_addline(buffer, " float shininess;\n");
6365 shader_addline(buffer, "} ffp_material;\n");
6367 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
6368 shader_addline(buffer, "uniform struct\n{\n");
6369 shader_addline(buffer, " vec4 diffuse;\n");
6370 shader_addline(buffer, " vec4 specular;\n");
6371 shader_addline(buffer, " vec4 ambient;\n");
6372 shader_addline(buffer, " vec4 position;\n");
6373 shader_addline(buffer, " vec3 direction;\n");
6374 shader_addline(buffer, " float range;\n");
6375 shader_addline(buffer, " float falloff;\n");
6376 shader_addline(buffer, " float c_att;\n");
6377 shader_addline(buffer, " float l_att;\n");
6378 shader_addline(buffer, " float q_att;\n");
6379 shader_addline(buffer, " float cos_htheta;\n");
6380 shader_addline(buffer, " float cos_hphi;\n");
6381 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
6383 if (settings->point_size)
6385 shader_addline(buffer, "uniform struct\n{\n");
6386 shader_addline(buffer, " float size;\n");
6387 shader_addline(buffer, " float size_min;\n");
6388 shader_addline(buffer, " float size_max;\n");
6389 shader_addline(buffer, " float c_att;\n");
6390 shader_addline(buffer, " float l_att;\n");
6391 shader_addline(buffer, " float q_att;\n");
6392 shader_addline(buffer, "} ffp_point;\n");
6395 if (legacy_context)
6397 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6398 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6399 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6400 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6402 else
6404 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
6405 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
6406 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6407 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6410 shader_addline(buffer, "\nvoid main()\n{\n");
6411 shader_addline(buffer, "float m;\n");
6412 shader_addline(buffer, "vec3 r;\n");
6414 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
6416 if (attrib_info[i].name[0])
6417 shader_addline(buffer, "%s %s = vs_in%u;\n",
6418 attrib_info[i].type, attrib_info[i].name, i);
6420 for (i = 0; i < MAX_TEXTURES; ++i)
6422 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
6423 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
6424 && settings->texcoords & (1u << i))
6425 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
6428 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
6430 if (settings->transformed)
6432 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
6433 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6434 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
6436 else
6438 for (i = 0; i < settings->vertexblends; ++i)
6439 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
6441 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
6442 for (i = 0; i < settings->vertexblends + 1; ++i)
6443 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
6445 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6446 if (settings->clipping)
6447 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
6448 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
6451 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
6452 if (settings->normal)
6454 if (!settings->vertexblends)
6456 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
6458 else
6460 for (i = 0; i < settings->vertexblends + 1; ++i)
6461 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
6464 if (settings->normalize)
6465 shader_addline(buffer, "normal = normalize(normal);\n");
6468 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
6469 if (legacy_context)
6471 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
6472 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
6474 else
6476 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
6477 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
6480 for (i = 0; i < MAX_TEXTURES; ++i)
6482 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6484 switch (settings->texgen[i] & 0xffff0000)
6486 case WINED3DTSS_TCI_PASSTHRU:
6487 if (settings->texcoords & (1u << i))
6488 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
6489 i, i, i);
6490 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
6491 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
6492 else
6493 output_legacy_texcoord = FALSE;
6494 break;
6496 case WINED3DTSS_TCI_CAMERASPACENORMAL:
6497 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
6498 break;
6500 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
6501 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
6502 break;
6504 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
6505 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6506 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
6507 break;
6509 case WINED3DTSS_TCI_SPHEREMAP:
6510 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
6511 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
6512 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6513 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
6514 break;
6516 default:
6517 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
6518 break;
6520 if (output_legacy_texcoord)
6521 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
6524 switch (settings->fog_mode)
6526 case WINED3D_FFP_VS_FOG_OFF:
6527 output_legacy_fogcoord = FALSE;
6528 break;
6530 case WINED3D_FFP_VS_FOG_FOGCOORD:
6531 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
6532 break;
6534 case WINED3D_FFP_VS_FOG_RANGE:
6535 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
6536 break;
6538 case WINED3D_FFP_VS_FOG_DEPTH:
6539 if (settings->ortho_fog)
6540 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
6541 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
6542 else if (settings->transformed)
6543 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
6544 else
6545 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
6546 break;
6548 default:
6549 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
6550 break;
6552 if (output_legacy_fogcoord)
6553 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
6555 if (settings->point_size)
6557 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
6558 " + ffp_point.l_att * length(ec_pos.xyz)"
6559 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
6560 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
6561 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
6564 shader_addline(buffer, "}\n");
6566 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6567 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
6569 return shader_obj;
6572 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
6573 DWORD argnum, unsigned int stage, DWORD arg)
6575 const char *ret;
6577 if (arg == ARG_UNUSED)
6578 return "<unused arg>";
6580 switch (arg & WINED3DTA_SELECTMASK)
6582 case WINED3DTA_DIFFUSE:
6583 ret = "ffp_varying_diffuse";
6584 break;
6586 case WINED3DTA_CURRENT:
6587 ret = "ret";
6588 break;
6590 case WINED3DTA_TEXTURE:
6591 switch (stage)
6593 case 0: ret = "tex0"; break;
6594 case 1: ret = "tex1"; break;
6595 case 2: ret = "tex2"; break;
6596 case 3: ret = "tex3"; break;
6597 case 4: ret = "tex4"; break;
6598 case 5: ret = "tex5"; break;
6599 case 6: ret = "tex6"; break;
6600 case 7: ret = "tex7"; break;
6601 default:
6602 ret = "<invalid texture>";
6603 break;
6605 break;
6607 case WINED3DTA_TFACTOR:
6608 ret = "tex_factor";
6609 break;
6611 case WINED3DTA_SPECULAR:
6612 ret = "ffp_varying_specular";
6613 break;
6615 case WINED3DTA_TEMP:
6616 ret = "temp_reg";
6617 break;
6619 case WINED3DTA_CONSTANT:
6620 switch (stage)
6622 case 0: ret = "tss_const0"; break;
6623 case 1: ret = "tss_const1"; break;
6624 case 2: ret = "tss_const2"; break;
6625 case 3: ret = "tss_const3"; break;
6626 case 4: ret = "tss_const4"; break;
6627 case 5: ret = "tss_const5"; break;
6628 case 6: ret = "tss_const6"; break;
6629 case 7: ret = "tss_const7"; break;
6630 default:
6631 ret = "<invalid constant>";
6632 break;
6634 break;
6636 default:
6637 return "<unhandled arg>";
6640 if (arg & WINED3DTA_COMPLEMENT)
6642 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
6643 if (argnum == 0)
6644 ret = "arg0";
6645 else if (argnum == 1)
6646 ret = "arg1";
6647 else if (argnum == 2)
6648 ret = "arg2";
6651 if (arg & WINED3DTA_ALPHAREPLICATE)
6653 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
6654 if (argnum == 0)
6655 ret = "arg0";
6656 else if (argnum == 1)
6657 ret = "arg1";
6658 else if (argnum == 2)
6659 ret = "arg2";
6662 return ret;
6665 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
6666 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
6668 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
6670 if (color && alpha)
6671 dstmask = "";
6672 else if (color)
6673 dstmask = ".xyz";
6674 else
6675 dstmask = ".w";
6677 if (dst == tempreg)
6678 dstreg = "temp_reg";
6679 else
6680 dstreg = "ret";
6682 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
6683 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
6684 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
6686 switch (op)
6688 case WINED3D_TOP_DISABLE:
6689 break;
6691 case WINED3D_TOP_SELECT_ARG1:
6692 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
6693 break;
6695 case WINED3D_TOP_SELECT_ARG2:
6696 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
6697 break;
6699 case WINED3D_TOP_MODULATE:
6700 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6701 break;
6703 case WINED3D_TOP_MODULATE_4X:
6704 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
6705 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6706 break;
6708 case WINED3D_TOP_MODULATE_2X:
6709 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
6710 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6711 break;
6713 case WINED3D_TOP_ADD:
6714 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
6715 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6716 break;
6718 case WINED3D_TOP_ADD_SIGNED:
6719 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
6720 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6721 break;
6723 case WINED3D_TOP_ADD_SIGNED_2X:
6724 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
6725 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6726 break;
6728 case WINED3D_TOP_SUBTRACT:
6729 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
6730 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6731 break;
6733 case WINED3D_TOP_ADD_SMOOTH:
6734 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
6735 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
6736 break;
6738 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
6739 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
6740 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6741 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6742 break;
6744 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6745 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6746 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6747 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6748 break;
6750 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6751 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
6752 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6753 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6754 break;
6756 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6757 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6758 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6759 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
6760 break;
6762 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
6763 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
6764 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6765 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6766 break;
6768 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
6769 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
6770 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6771 break;
6773 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
6774 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
6775 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6776 break;
6778 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
6779 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6780 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6781 break;
6782 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
6783 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
6784 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6785 break;
6787 case WINED3D_TOP_BUMPENVMAP:
6788 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6789 /* These are handled in the first pass, nothing to do. */
6790 break;
6792 case WINED3D_TOP_DOTPRODUCT3:
6793 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
6794 dstreg, dstmask, arg1, arg2, dstmask);
6795 break;
6797 case WINED3D_TOP_MULTIPLY_ADD:
6798 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
6799 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
6800 break;
6802 case WINED3D_TOP_LERP:
6803 /* MSDN isn't quite right here. */
6804 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
6805 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
6806 break;
6808 default:
6809 FIXME("Unhandled operation %#x.\n", op);
6810 break;
6814 /* Context activation is done by the caller. */
6815 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
6816 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
6818 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
6819 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
6820 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6821 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6822 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
6823 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
6824 UINT lowest_disabled_stage;
6825 GLuint shader_id;
6826 DWORD arg0, arg1, arg2;
6827 unsigned int stage;
6829 string_buffer_clear(buffer);
6831 /* Find out which textures are read */
6832 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6834 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6835 break;
6837 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
6838 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
6839 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
6841 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
6842 || (stage == 0 && settings->color_key_enabled))
6843 tex_map |= 1u << stage;
6844 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6845 tfactor_used = TRUE;
6846 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6847 tempreg_used = TRUE;
6848 if (settings->op[stage].dst == tempreg)
6849 tempreg_used = TRUE;
6850 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6851 tss_const_map |= 1u << stage;
6853 switch (settings->op[stage].cop)
6855 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6856 lum_map |= 1u << stage;
6857 /* fall through */
6858 case WINED3D_TOP_BUMPENVMAP:
6859 bump_map |= 1u << stage;
6860 /* fall through */
6861 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6862 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6863 tex_map |= 1u << stage;
6864 break;
6866 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6867 tfactor_used = TRUE;
6868 break;
6870 default:
6871 break;
6874 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6875 continue;
6877 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
6878 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
6879 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
6881 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
6882 tex_map |= 1u << stage;
6883 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6884 tfactor_used = TRUE;
6885 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6886 tempreg_used = TRUE;
6887 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6888 tss_const_map |= 1u << stage;
6890 lowest_disabled_stage = stage;
6892 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6894 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6895 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6897 if (!needs_legacy_glsl_syntax(gl_info))
6898 shader_addline(buffer, "out vec4 ps_out[1];\n");
6900 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
6901 shader_addline(buffer, "vec4 ret;\n");
6902 if (tempreg_used || settings->sRGB_write)
6903 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
6904 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
6906 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6908 if (tss_const_map & (1u << stage))
6909 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
6911 if (!(tex_map & (1u << stage)))
6912 continue;
6914 switch (settings->op[stage].tex_type)
6916 case WINED3D_GL_RES_TYPE_TEX_1D:
6917 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
6918 break;
6919 case WINED3D_GL_RES_TYPE_TEX_2D:
6920 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
6921 break;
6922 case WINED3D_GL_RES_TYPE_TEX_3D:
6923 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
6924 break;
6925 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6926 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
6927 break;
6928 case WINED3D_GL_RES_TYPE_TEX_RECT:
6929 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
6930 break;
6931 default:
6932 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
6933 break;
6936 shader_addline(buffer, "vec4 tex%u;\n", stage);
6938 if (!(bump_map & (1u << stage)))
6939 continue;
6940 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
6942 if (!(lum_map & (1u << stage)))
6943 continue;
6944 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
6945 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
6947 if (tfactor_used)
6948 shader_addline(buffer, "uniform vec4 tex_factor;\n");
6949 if (settings->color_key_enabled)
6950 shader_addline(buffer, "uniform vec4 color_key[2];\n");
6951 shader_addline(buffer, "uniform vec4 specular_enable;\n");
6953 if (settings->sRGB_write)
6955 shader_addline(buffer, "const vec4 srgb_const0 = ");
6956 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
6957 shader_addline(buffer, ";\n");
6958 shader_addline(buffer, "const vec4 srgb_const1 = ");
6959 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
6960 shader_addline(buffer, ";\n");
6963 shader_addline(buffer, "uniform struct\n{\n");
6964 shader_addline(buffer, " vec4 color;\n");
6965 shader_addline(buffer, " float density;\n");
6966 shader_addline(buffer, " float end;\n");
6967 shader_addline(buffer, " float scale;\n");
6968 shader_addline(buffer, "} ffp_fog;\n");
6970 if (alpha_test_func != WINED3D_CMP_ALWAYS)
6971 shader_addline(buffer, "uniform float alpha_test_ref;\n");
6973 if (legacy_context)
6975 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6976 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6977 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6978 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6979 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6981 else
6983 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
6984 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
6985 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6986 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6987 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6990 shader_addline(buffer, "void main()\n{\n");
6992 if (legacy_context)
6994 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
6995 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
6998 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7000 if (tex_map & (1u << stage))
7002 if (settings->pointsprite)
7003 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
7004 else if (settings->texcoords_initialized & (1u << stage))
7005 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
7006 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
7007 else
7008 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
7012 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
7013 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7015 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
7016 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
7018 /* Generate texture sampling instructions */
7019 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
7021 const char *texture_function, *coord_mask;
7022 BOOL proj;
7024 if (!(tex_map & (1u << stage)))
7025 continue;
7027 if (settings->op[stage].projected == proj_none)
7029 proj = FALSE;
7031 else if (settings->op[stage].projected == proj_count4
7032 || settings->op[stage].projected == proj_count3)
7034 proj = TRUE;
7036 else
7038 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
7039 proj = TRUE;
7042 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
7043 proj = FALSE;
7045 switch (settings->op[stage].tex_type)
7047 case WINED3D_GL_RES_TYPE_TEX_1D:
7048 if (proj)
7050 texture_function = "texture1DProj";
7051 coord_mask = "xw";
7053 else
7055 texture_function = "texture1D";
7056 coord_mask = "x";
7058 break;
7059 case WINED3D_GL_RES_TYPE_TEX_2D:
7060 if (proj)
7062 texture_function = "texture2DProj";
7063 coord_mask = "xyw";
7065 else
7067 texture_function = "texture2D";
7068 coord_mask = "xy";
7070 break;
7071 case WINED3D_GL_RES_TYPE_TEX_3D:
7072 if (proj)
7074 texture_function = "texture3DProj";
7075 coord_mask = "xyzw";
7077 else
7079 texture_function = "texture3D";
7080 coord_mask = "xyz";
7082 break;
7083 case WINED3D_GL_RES_TYPE_TEX_CUBE:
7084 texture_function = "textureCube";
7085 coord_mask = "xyz";
7086 break;
7087 case WINED3D_GL_RES_TYPE_TEX_RECT:
7088 if (proj)
7090 texture_function = "texture2DRectProj";
7091 coord_mask = "xyw";
7093 else
7095 texture_function = "texture2DRect";
7096 coord_mask = "xy";
7098 break;
7099 default:
7100 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
7101 texture_function = "";
7102 coord_mask = "xyzw";
7103 break;
7105 if (!needs_legacy_glsl_syntax(gl_info))
7106 texture_function = proj ? "textureProj" : "texture";
7108 if (stage > 0
7109 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
7110 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
7112 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
7114 /* With projective textures, texbem only divides the static
7115 * texture coord, not the displacement, so multiply the
7116 * displacement with the dividing parameter before passing it to
7117 * TXP. */
7118 if (settings->op[stage].projected != proj_none)
7120 if (settings->op[stage].projected == proj_count4)
7122 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
7123 stage, stage);
7124 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
7126 else
7128 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
7129 stage, stage);
7130 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
7133 else
7135 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
7138 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
7139 stage, texture_function, stage, coord_mask);
7141 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
7142 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
7143 stage, stage - 1, stage - 1, stage - 1);
7145 else if (settings->op[stage].projected == proj_count3)
7147 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
7148 stage, texture_function, stage, stage);
7150 else
7152 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
7153 stage, texture_function, stage, stage, coord_mask);
7156 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
7157 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
7158 settings->op[stage].color_fixup);
7161 if (settings->color_key_enabled)
7163 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
7164 shader_addline(buffer, " discard;\n");
7167 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
7169 /* Generate the main shader */
7170 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7172 BOOL op_equal;
7174 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
7175 break;
7177 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
7178 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
7179 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
7180 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
7181 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
7182 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
7183 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
7184 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
7185 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
7186 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
7187 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
7188 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
7189 else
7190 op_equal = settings->op[stage].aop == settings->op[stage].cop
7191 && settings->op[stage].carg0 == settings->op[stage].aarg0
7192 && settings->op[stage].carg1 == settings->op[stage].aarg1
7193 && settings->op[stage].carg2 == settings->op[stage].aarg2;
7195 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
7197 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
7198 settings->op[stage].cop, settings->op[stage].carg0,
7199 settings->op[stage].carg1, settings->op[stage].carg2);
7201 else if (op_equal)
7203 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
7204 settings->op[stage].cop, settings->op[stage].carg0,
7205 settings->op[stage].carg1, settings->op[stage].carg2);
7207 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
7208 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
7210 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
7211 settings->op[stage].cop, settings->op[stage].carg0,
7212 settings->op[stage].carg1, settings->op[stage].carg2);
7213 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
7214 settings->op[stage].aop, settings->op[stage].aarg0,
7215 settings->op[stage].aarg1, settings->op[stage].aarg2);
7219 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
7220 get_fragment_output(gl_info));
7222 if (settings->sRGB_write)
7223 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7225 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
7227 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
7229 shader_addline(buffer, "}\n");
7231 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7232 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7234 string_buffer_release(&priv->string_buffers, tex_reg_name);
7235 return shader_id;
7238 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
7239 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
7241 struct glsl_ffp_vertex_shader *shader;
7242 const struct wine_rb_entry *entry;
7244 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
7245 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
7247 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
7248 return NULL;
7250 shader->desc.settings = *settings;
7251 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
7252 list_init(&shader->linked_programs);
7253 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
7254 ERR("Failed to insert ffp vertex shader.\n");
7256 return shader;
7259 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
7260 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
7262 struct glsl_ffp_fragment_shader *glsl_desc;
7263 const struct ffp_frag_desc *desc;
7265 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
7266 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
7268 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
7269 return NULL;
7271 glsl_desc->entry.settings = *args;
7272 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
7273 list_init(&glsl_desc->linked_programs);
7274 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
7276 return glsl_desc;
7280 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
7281 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
7283 unsigned int i;
7284 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7286 for (i = 0; i < vs_c_count; ++i)
7288 string_buffer_sprintf(name, "vs_c[%u]", i);
7289 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7291 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
7293 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
7295 string_buffer_sprintf(name, "vs_i[%u]", i);
7296 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7299 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
7301 string_buffer_sprintf(name, "vs_b[%u]", i);
7302 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7305 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
7307 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
7309 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
7310 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7312 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
7313 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
7314 for (i = 0; i < MAX_TEXTURES; ++i)
7316 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
7317 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7319 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
7320 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
7321 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
7322 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
7323 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
7324 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
7325 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
7327 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
7328 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7329 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
7330 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7331 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
7332 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7333 string_buffer_sprintf(name, "ffp_light[%u].position", i);
7334 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7335 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
7336 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7337 string_buffer_sprintf(name, "ffp_light[%u].range", i);
7338 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7339 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
7340 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7341 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
7342 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7343 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
7344 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7345 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
7346 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7347 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
7348 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7349 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
7350 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7352 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
7353 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
7354 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
7355 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
7356 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
7357 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
7359 string_buffer_release(&priv->string_buffers, name);
7362 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
7363 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
7365 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
7368 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
7369 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
7371 unsigned int i;
7372 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7374 for (i = 0; i < ps_c_count; ++i)
7376 string_buffer_sprintf(name, "ps_c[%u]", i);
7377 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7379 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
7381 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
7383 string_buffer_sprintf(name, "ps_i[%u]", i);
7384 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7387 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
7389 string_buffer_sprintf(name, "ps_b[%u]", i);
7390 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7393 for (i = 0; i < MAX_TEXTURES; ++i)
7395 string_buffer_sprintf(name, "bumpenv_mat%u", i);
7396 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7397 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
7398 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7399 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
7400 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7401 string_buffer_sprintf(name, "tss_const%u", i);
7402 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7405 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
7406 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
7408 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
7409 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
7410 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
7411 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
7413 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
7415 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
7416 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
7417 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
7419 string_buffer_release(&priv->string_buffers, name);
7422 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
7423 struct shader_glsl_priv *priv, GLuint program_id,
7424 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
7426 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
7427 GLuint block_idx;
7428 unsigned int i;
7429 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7431 for (i = 0; i < count; ++i)
7433 if (!reg_maps->cb_sizes[i])
7434 continue;
7436 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
7437 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
7438 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
7440 checkGLcall("glUniformBlockBinding");
7441 string_buffer_release(&priv->string_buffers, name);
7444 /* Context activation is done by the caller. */
7445 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
7446 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
7448 const struct wined3d_gl_info *gl_info = context->gl_info;
7449 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
7450 const struct ps_np2fixup_info *np2fixup_info = NULL;
7451 struct glsl_shader_prog_link *entry = NULL;
7452 struct wined3d_shader *vshader = NULL;
7453 struct wined3d_shader *gshader = NULL;
7454 struct wined3d_shader *pshader = NULL;
7455 GLuint program_id;
7456 GLuint reorder_shader_id = 0;
7457 unsigned int i;
7458 GLuint vs_id = 0;
7459 GLuint gs_id = 0;
7460 GLuint ps_id = 0;
7461 struct list *ps_list, *vs_list;
7462 WORD attribs_map;
7463 struct wined3d_string_buffer *tmp_name;
7465 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
7467 vs_id = ctx_data->glsl_program->vs.id;
7468 vs_list = &ctx_data->glsl_program->vs.shader_entry;
7470 if (use_vs(state))
7472 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7473 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
7475 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
7476 && ctx_data->glsl_program->gs.id)
7478 gs_id = ctx_data->glsl_program->gs.id;
7480 else if (gshader)
7482 struct gs_compile_args args;
7484 find_gs_compile_args(state, gshader, &args);
7485 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
7489 else if (use_vs(state))
7491 struct vs_compile_args vs_compile_args;
7493 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7494 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
7496 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, d3d_info);
7497 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
7498 vs_list = &vshader->linked_programs;
7500 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
7501 && ctx_data->glsl_program->gs.id)
7503 gs_id = ctx_data->glsl_program->gs.id;
7505 else if (gshader)
7507 struct gs_compile_args gs_compile_args;
7509 find_gs_compile_args(state, gshader, &gs_compile_args);
7510 gs_id = find_glsl_geometry_shader(context, priv, gshader, &gs_compile_args);
7513 else if (priv->vertex_pipe == &glsl_vertex_pipe)
7515 struct glsl_ffp_vertex_shader *ffp_shader;
7516 struct wined3d_ffp_vs_settings settings;
7518 wined3d_ffp_get_vs_settings(context, state, &settings);
7519 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
7520 vs_id = ffp_shader->id;
7521 vs_list = &ffp_shader->linked_programs;
7524 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
7526 ps_id = ctx_data->glsl_program->ps.id;
7527 ps_list = &ctx_data->glsl_program->ps.shader_entry;
7529 if (use_ps(state))
7530 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7532 else if (use_ps(state))
7534 struct ps_compile_args ps_compile_args;
7535 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7536 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
7537 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
7538 pshader, &ps_compile_args, &np2fixup_info);
7539 ps_list = &pshader->linked_programs;
7541 else if (priv->fragment_pipe == &glsl_fragment_pipe)
7543 struct glsl_ffp_fragment_shader *ffp_shader;
7544 struct ffp_frag_settings settings;
7546 gen_ffp_frag_op(context, state, &settings, FALSE);
7547 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
7548 ps_id = ffp_shader->id;
7549 ps_list = &ffp_shader->linked_programs;
7552 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
7554 ctx_data->glsl_program = entry;
7555 return;
7558 /* If we get to this point, then no matching program exists, so we create one */
7559 program_id = GL_EXTCALL(glCreateProgram());
7560 TRACE("Created new GLSL shader program %u.\n", program_id);
7562 /* Create the entry */
7563 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
7564 entry->id = program_id;
7565 entry->vs.id = vs_id;
7566 entry->gs.id = gs_id;
7567 entry->ps.id = ps_id;
7568 entry->constant_version = 0;
7569 entry->ps.np2_fixup_info = np2fixup_info;
7570 /* Add the hash table entry */
7571 add_glsl_program_entry(priv, entry);
7573 /* Set the current program */
7574 ctx_data->glsl_program = entry;
7576 /* Attach GLSL vshader */
7577 if (vs_id)
7579 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
7580 GL_EXTCALL(glAttachShader(program_id, vs_id));
7581 checkGLcall("glAttachShader");
7583 list_add_head(vs_list, &entry->vs.shader_entry);
7586 if (vshader)
7588 attribs_map = vshader->reg_maps.input_registers;
7589 if (vshader->reg_maps.shader_version.major < 4)
7591 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
7592 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
7593 d3d_info->emulated_flatshading
7594 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
7595 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
7596 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
7597 checkGLcall("glAttachShader");
7598 /* Flag the reorder function for deletion, it will be freed
7599 * automatically when the program is destroyed. */
7600 GL_EXTCALL(glDeleteShader(reorder_shader_id));
7603 else
7605 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
7608 /* Bind vertex attributes to a corresponding index number to match
7609 * the same index numbers as ARB_vertex_programs (makes loading
7610 * vertex attributes simpler). With this method, we can use the
7611 * exact same code to load the attributes later for both ARB and
7612 * GLSL shaders.
7614 * We have to do this here because we need to know the Program ID
7615 * in order to make the bindings work, and it has to be done prior
7616 * to linking the GLSL program. */
7617 tmp_name = string_buffer_get(&priv->string_buffers);
7618 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
7620 if (!(attribs_map & 1))
7621 continue;
7623 string_buffer_sprintf(tmp_name, "vs_in%u", i);
7624 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
7625 if (vshader && vshader->reg_maps.shader_version.major >= 4)
7627 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
7628 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
7629 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
7630 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
7633 checkGLcall("glBindAttribLocation");
7634 string_buffer_release(&priv->string_buffers, tmp_name);
7636 if (gshader)
7638 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
7639 GL_EXTCALL(glAttachShader(program_id, gs_id));
7640 checkGLcall("glAttachShader");
7642 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
7644 TRACE("input type %s, output type %s, vertices out %u.\n",
7645 debug_d3dprimitivetype(gshader->u.gs.input_type),
7646 debug_d3dprimitivetype(gshader->u.gs.output_type),
7647 gshader->u.gs.vertices_out);
7648 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
7649 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
7650 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
7651 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
7652 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
7653 gshader->u.gs.vertices_out));
7654 checkGLcall("glProgramParameteriARB");
7657 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
7660 /* Attach GLSL pshader */
7661 if (ps_id)
7663 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
7664 GL_EXTCALL(glAttachShader(program_id, ps_id));
7665 checkGLcall("glAttachShader");
7667 list_add_head(ps_list, &entry->ps.shader_entry);
7670 /* Link the program */
7671 TRACE("Linking GLSL shader program %u.\n", program_id);
7672 GL_EXTCALL(glLinkProgram(program_id));
7673 shader_glsl_validate_link(gl_info, program_id);
7675 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
7676 vshader ? vshader->limits->constant_float : 0);
7677 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
7678 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
7679 pshader ? pshader->limits->constant_float : 0);
7680 checkGLcall("Find glsl program uniform locations");
7682 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
7684 if (pshader && pshader->reg_maps.shader_version.major >= 3
7685 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
7687 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
7688 entry->vs.vertex_color_clamp = GL_FALSE;
7690 else
7692 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7695 else
7697 /* With core profile we never change vertex_color_clamp from
7698 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
7699 * glClampColorARB(). */
7700 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7703 /* Set the shader to allow uniform loading on it */
7704 GL_EXTCALL(glUseProgram(program_id));
7705 checkGLcall("glUseProgram");
7707 /* Texture unit mapping is set up to be the same each time the shader
7708 * program is used so we can hardcode the sampler uniform values. */
7709 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
7711 entry->constant_update_mask = 0;
7712 if (vshader)
7714 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
7715 if (vshader->reg_maps.integer_constants)
7716 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
7717 if (vshader->reg_maps.boolean_constants)
7718 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
7719 if (entry->vs.pos_fixup_location != -1)
7720 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
7722 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps,
7723 0, gl_info->limits.vertex_uniform_blocks);
7725 else
7727 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
7728 | WINED3D_SHADER_CONST_FFP_PROJ;
7730 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
7732 if (entry->vs.modelview_matrix_location[i] != -1)
7734 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
7735 break;
7739 for (i = 0; i < MAX_TEXTURES; ++i)
7741 if (entry->vs.texture_matrix_location[i] != -1)
7743 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7744 break;
7747 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
7748 || entry->vs.material_specular_location != -1
7749 || entry->vs.material_emissive_location != -1
7750 || entry->vs.material_shininess_location != -1)
7751 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
7752 if (entry->vs.light_ambient_location != -1)
7753 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
7755 if (entry->vs.pointsize_min_location != -1)
7756 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
7758 if (gshader)
7760 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
7761 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps,
7762 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
7765 if (ps_id)
7767 if (pshader)
7769 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
7770 if (pshader->reg_maps.integer_constants)
7771 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
7772 if (pshader->reg_maps.boolean_constants)
7773 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
7774 if (entry->ps.ycorrection_location != -1)
7775 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
7777 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps,
7778 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
7779 gl_info->limits.fragment_uniform_blocks);
7781 else
7783 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7786 for (i = 0; i < MAX_TEXTURES; ++i)
7788 if (entry->ps.bumpenv_mat_location[i] != -1)
7790 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
7791 break;
7795 if (entry->ps.fog_color_location != -1)
7796 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
7797 if (entry->ps.alpha_test_ref_location != -1)
7798 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
7799 if (entry->ps.np2_fixup_location != -1)
7800 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
7801 if (entry->ps.color_key_location != -1)
7802 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
7806 /* Context activation is done by the caller. */
7807 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum wined3d_gl_resource_type tex_type,
7808 BOOL masked)
7810 GLuint program_id;
7811 GLuint vshader_id, pshader_id;
7812 const char *blt_pshader;
7814 static const char blt_vshader[] =
7815 "#version 120\n"
7816 "void main(void)\n"
7817 "{\n"
7818 " gl_Position = gl_Vertex;\n"
7819 " gl_FrontColor = vec4(1.0);\n"
7820 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
7821 "}\n";
7823 static const char * const blt_pshaders_full[WINED3D_GL_RES_TYPE_COUNT] =
7825 /* WINED3D_GL_RES_TYPE_TEX_1D */
7826 NULL,
7827 /* WINED3D_GL_RES_TYPE_TEX_2D */
7828 "#version 120\n"
7829 "uniform sampler2D sampler;\n"
7830 "void main(void)\n"
7831 "{\n"
7832 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7833 "}\n",
7834 /* WINED3D_GL_RES_TYPE_TEX_3D */
7835 NULL,
7836 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7837 "#version 120\n"
7838 "uniform samplerCube sampler;\n"
7839 "void main(void)\n"
7840 "{\n"
7841 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7842 "}\n",
7843 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7844 "#version 120\n"
7845 "#extension GL_ARB_texture_rectangle : enable\n"
7846 "uniform sampler2DRect sampler;\n"
7847 "void main(void)\n"
7848 "{\n"
7849 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7850 "}\n",
7851 /* WINED3D_GL_RES_TYPE_BUFFER */
7852 NULL,
7853 /* WINED3D_GL_RES_TYPE_RB */
7854 NULL,
7857 static const char * const blt_pshaders_masked[WINED3D_GL_RES_TYPE_COUNT] =
7859 /* WINED3D_GL_RES_TYPE_TEX_1D */
7860 NULL,
7861 /* WINED3D_GL_RES_TYPE_TEX_2D */
7862 "#version 120\n"
7863 "uniform sampler2D sampler;\n"
7864 "uniform vec4 mask;\n"
7865 "void main(void)\n"
7866 "{\n"
7867 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7868 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7869 "}\n",
7870 /* WINED3D_GL_RES_TYPE_TEX_3D */
7871 NULL,
7872 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7873 "#version 120\n"
7874 "uniform samplerCube sampler;\n"
7875 "uniform vec4 mask;\n"
7876 "void main(void)\n"
7877 "{\n"
7878 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7879 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7880 "}\n",
7881 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7882 "#version 120\n"
7883 "#extension GL_ARB_texture_rectangle : enable\n"
7884 "uniform sampler2DRect sampler;\n"
7885 "uniform vec4 mask;\n"
7886 "void main(void)\n"
7887 "{\n"
7888 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7889 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7890 "}\n",
7891 /* WINED3D_GL_RES_TYPE_BUFFER */
7892 NULL,
7893 /* WINED3D_GL_RES_TYPE_RB */
7894 NULL,
7897 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
7898 if (!blt_pshader)
7900 FIXME("tex_type %#x not supported\n", tex_type);
7901 return 0;
7904 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7905 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
7907 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7908 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
7910 program_id = GL_EXTCALL(glCreateProgram());
7911 GL_EXTCALL(glAttachShader(program_id, vshader_id));
7912 GL_EXTCALL(glAttachShader(program_id, pshader_id));
7913 GL_EXTCALL(glLinkProgram(program_id));
7915 shader_glsl_validate_link(gl_info, program_id);
7917 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
7918 * is destroyed
7920 GL_EXTCALL(glDeleteShader(vshader_id));
7921 GL_EXTCALL(glDeleteShader(pshader_id));
7922 return program_id;
7925 /* Context activation is done by the caller. */
7926 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
7927 const struct wined3d_state *state)
7929 struct glsl_context_data *ctx_data = context->shader_backend_data;
7930 const struct wined3d_gl_info *gl_info = context->gl_info;
7931 struct shader_glsl_priv *priv = shader_priv;
7932 GLuint program_id = 0, prev_id = 0;
7933 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
7935 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
7936 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
7938 if (ctx_data->glsl_program)
7940 prev_id = ctx_data->glsl_program->id;
7941 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7943 else
7945 prev_id = 0;
7946 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7949 set_glsl_shader_program(context, state, priv, ctx_data);
7951 if (ctx_data->glsl_program)
7953 program_id = ctx_data->glsl_program->id;
7954 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7956 else
7958 program_id = 0;
7959 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7962 if (old_vertex_color_clamp != current_vertex_color_clamp)
7964 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7966 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
7967 checkGLcall("glClampColorARB");
7969 else
7971 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
7975 TRACE("Using GLSL program %u.\n", program_id);
7977 if (prev_id != program_id)
7979 GL_EXTCALL(glUseProgram(program_id));
7980 checkGLcall("glUseProgram");
7982 if (program_id)
7983 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
7987 /* "context" is not necessarily the currently active context. */
7988 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
7990 struct glsl_context_data *ctx_data = context->shader_backend_data;
7992 ctx_data->glsl_program = NULL;
7993 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
7994 | (1u << WINED3D_SHADER_TYPE_VERTEX)
7995 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
7996 | (1u << WINED3D_SHADER_TYPE_HULL)
7997 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
8000 /* Context activation is done by the caller. */
8001 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
8003 const struct wined3d_gl_info *gl_info = context->gl_info;
8004 struct shader_glsl_priv *priv = shader_priv;
8006 shader_glsl_invalidate_current_program(context);
8007 GL_EXTCALL(glUseProgram(0));
8008 checkGLcall("glUseProgram");
8010 priv->vertex_pipe->vp_enable(gl_info, FALSE);
8011 priv->fragment_pipe->enable_extension(gl_info, FALSE);
8013 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
8015 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
8016 checkGLcall("glClampColorARB");
8020 /* Context activation is done by the caller. */
8021 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
8022 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size)
8024 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
8025 struct shader_glsl_priv *priv = shader_priv;
8026 GLuint *blt_program;
8027 GLint loc;
8029 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
8030 if (!*blt_program)
8032 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
8033 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
8034 GL_EXTCALL(glUseProgram(*blt_program));
8035 GL_EXTCALL(glUniform1i(loc, 0));
8037 else
8039 GL_EXTCALL(glUseProgram(*blt_program));
8042 if (masked)
8044 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
8045 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
8049 /* Context activation is done by the caller. */
8050 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
8052 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
8053 GLuint program_id;
8055 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
8056 if (program_id) TRACE("Using GLSL program %u\n", program_id);
8058 GL_EXTCALL(glUseProgram(program_id));
8059 checkGLcall("glUseProgram");
8062 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
8063 const struct glsl_shader_prog_link *program)
8065 const struct glsl_context_data *ctx_data;
8066 struct wined3d_context *context;
8067 unsigned int i;
8069 for (i = 0; i < device->context_count; ++i)
8071 context = device->contexts[i];
8072 ctx_data = context->shader_backend_data;
8074 if (ctx_data->glsl_program == program)
8075 shader_glsl_invalidate_current_program(context);
8079 static void shader_glsl_destroy(struct wined3d_shader *shader)
8081 struct glsl_shader_private *shader_data = shader->backend_data;
8082 struct wined3d_device *device = shader->device;
8083 struct shader_glsl_priv *priv = device->shader_priv;
8084 const struct wined3d_gl_info *gl_info;
8085 const struct list *linked_programs;
8086 struct wined3d_context *context;
8088 if (!shader_data || !shader_data->num_gl_shaders)
8090 HeapFree(GetProcessHeap(), 0, shader_data);
8091 shader->backend_data = NULL;
8092 return;
8095 context = context_acquire(device, NULL);
8096 gl_info = context->gl_info;
8098 TRACE("Deleting linked programs.\n");
8099 linked_programs = &shader->linked_programs;
8100 if (linked_programs->next)
8102 struct glsl_shader_prog_link *entry, *entry2;
8103 UINT i;
8105 switch (shader->reg_maps.shader_version.type)
8107 case WINED3D_SHADER_TYPE_PIXEL:
8109 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
8111 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8113 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
8114 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8115 checkGLcall("glDeleteShader");
8117 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
8119 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8120 struct glsl_shader_prog_link, ps.shader_entry)
8122 shader_glsl_invalidate_contexts_program(device, entry);
8123 delete_glsl_program_entry(priv, gl_info, entry);
8126 break;
8129 case WINED3D_SHADER_TYPE_VERTEX:
8131 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
8133 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8135 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
8136 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8137 checkGLcall("glDeleteShader");
8139 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
8141 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8142 struct glsl_shader_prog_link, vs.shader_entry)
8144 shader_glsl_invalidate_contexts_program(device, entry);
8145 delete_glsl_program_entry(priv, gl_info, entry);
8148 break;
8151 case WINED3D_SHADER_TYPE_GEOMETRY:
8153 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
8155 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8157 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
8158 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8159 checkGLcall("glDeleteShader");
8161 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
8163 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8164 struct glsl_shader_prog_link, gs.shader_entry)
8166 shader_glsl_invalidate_contexts_program(device, entry);
8167 delete_glsl_program_entry(priv, gl_info, entry);
8170 break;
8173 default:
8174 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8175 break;
8179 HeapFree(GetProcessHeap(), 0, shader->backend_data);
8180 shader->backend_data = NULL;
8182 context_release(context);
8185 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
8187 const struct glsl_program_key *k = key;
8188 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
8189 const struct glsl_shader_prog_link, program_lookup_entry);
8191 if (k->vs_id > prog->vs.id) return 1;
8192 else if (k->vs_id < prog->vs.id) return -1;
8194 if (k->gs_id > prog->gs.id) return 1;
8195 else if (k->gs_id < prog->gs.id) return -1;
8197 if (k->ps_id > prog->ps.id) return 1;
8198 else if (k->ps_id < prog->ps.id) return -1;
8200 return 0;
8203 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
8205 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
8206 + constant_count * sizeof(*heap->contained)
8207 + constant_count * sizeof(*heap->positions);
8208 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
8210 if (!mem)
8212 ERR("Failed to allocate memory\n");
8213 return FALSE;
8216 heap->entries = mem;
8217 heap->entries[1].version = 0;
8218 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
8219 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
8220 heap->positions = (unsigned int *)(heap->contained + constant_count);
8221 heap->size = 1;
8223 return TRUE;
8226 static void constant_heap_free(struct constant_heap *heap)
8228 HeapFree(GetProcessHeap(), 0, heap->entries);
8231 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
8233 wined3d_rb_alloc,
8234 wined3d_rb_realloc,
8235 wined3d_rb_free,
8236 glsl_program_key_compare,
8239 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
8240 const struct fragment_pipeline *fragment_pipe)
8242 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
8243 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
8244 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
8245 struct fragment_caps fragment_caps;
8246 void *vertex_priv, *fragment_priv;
8248 string_buffer_list_init(&priv->string_buffers);
8250 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
8252 ERR("Failed to initialize vertex pipe.\n");
8253 HeapFree(GetProcessHeap(), 0, priv);
8254 return E_FAIL;
8257 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
8259 ERR("Failed to initialize fragment pipe.\n");
8260 vertex_pipe->vp_free(device);
8261 HeapFree(GetProcessHeap(), 0, priv);
8262 return E_FAIL;
8265 if (!string_buffer_init(&priv->shader_buffer))
8267 ERR("Failed to initialize shader buffer.\n");
8268 goto fail;
8271 if (!(priv->stack = wined3d_calloc(stack_size, sizeof(*priv->stack))))
8273 ERR("Failed to allocate memory.\n");
8274 goto fail;
8277 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
8279 ERR("Failed to initialize vertex shader constant heap\n");
8280 goto fail;
8283 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
8285 ERR("Failed to initialize pixel shader constant heap\n");
8286 goto fail;
8289 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
8291 ERR("Failed to initialize rbtree.\n");
8292 goto fail;
8295 priv->next_constant_version = 1;
8296 priv->vertex_pipe = vertex_pipe;
8297 priv->fragment_pipe = fragment_pipe;
8298 fragment_pipe->get_caps(gl_info, &fragment_caps);
8299 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
8300 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
8302 device->vertex_priv = vertex_priv;
8303 device->fragment_priv = fragment_priv;
8304 device->shader_priv = priv;
8306 return WINED3D_OK;
8308 fail:
8309 constant_heap_free(&priv->pconst_heap);
8310 constant_heap_free(&priv->vconst_heap);
8311 HeapFree(GetProcessHeap(), 0, priv->stack);
8312 string_buffer_free(&priv->shader_buffer);
8313 fragment_pipe->free_private(device);
8314 vertex_pipe->vp_free(device);
8315 HeapFree(GetProcessHeap(), 0, priv);
8316 return E_OUTOFMEMORY;
8319 /* Context activation is done by the caller. */
8320 static void shader_glsl_free(struct wined3d_device *device)
8322 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
8323 struct shader_glsl_priv *priv = device->shader_priv;
8324 int i;
8326 for (i = 0; i < WINED3D_GL_RES_TYPE_COUNT; ++i)
8328 if (priv->depth_blt_program_full[i])
8330 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
8332 if (priv->depth_blt_program_masked[i])
8334 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
8338 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
8339 constant_heap_free(&priv->pconst_heap);
8340 constant_heap_free(&priv->vconst_heap);
8341 HeapFree(GetProcessHeap(), 0, priv->stack);
8342 string_buffer_list_cleanup(&priv->string_buffers);
8343 string_buffer_free(&priv->shader_buffer);
8344 priv->fragment_pipe->free_private(device);
8345 priv->vertex_pipe->vp_free(device);
8347 HeapFree(GetProcessHeap(), 0, device->shader_priv);
8348 device->shader_priv = NULL;
8351 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
8353 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
8354 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
8357 static void shader_glsl_free_context_data(struct wined3d_context *context)
8359 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
8362 static void shader_glsl_init_context_state(struct wined3d_context *context)
8364 const struct wined3d_gl_info *gl_info = context->gl_info;
8366 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
8367 checkGLcall("GL_PROGRAM_POINT_SIZE");
8370 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
8372 UINT shader_model;
8374 /* FIXME: Check for the specific extensions required for SM5 support
8375 * (ARB_compute_shader, ARB_tessellation_shader, ARB_gpu_shader5, ...) as
8376 * soon as we introduce them, adjusting the GL / GLSL version checks
8377 * accordingly. */
8378 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 30) && gl_info->supported[WINED3D_GL_VERSION_4_3])
8379 shader_model = 5;
8380 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50) && gl_info->supported[WINED3D_GL_VERSION_3_2]
8381 && gl_info->supported[ARB_SHADER_BIT_ENCODING] && gl_info->supported[ARB_SAMPLER_OBJECTS]
8382 && gl_info->supported[ARB_TEXTURE_SWIZZLE])
8383 shader_model = 4;
8384 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
8385 * texldd and texldl instructions. */
8386 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
8387 shader_model = 3;
8388 else
8389 shader_model = 2;
8390 TRACE("Shader model %u.\n", shader_model);
8392 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
8393 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
8394 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
8395 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
8396 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
8398 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
8399 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
8400 caps->varying_count = gl_info->limits.glsl_varyings;
8402 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
8403 * Direct3D minimum requirement.
8405 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
8406 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
8408 * The problem is that the refrast clamps temporary results in the shader to
8409 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
8410 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
8411 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
8412 * offer a way to query this.
8414 if (shader_model >= 4)
8415 caps->ps_1x_max_value = FLT_MAX;
8416 else
8417 caps->ps_1x_max_value = 1024.0f;
8419 /* Ideally we'd only set caps like sRGB writes here if supported by both
8420 * the shader backend and the fragment pipe, but we can get called before
8421 * shader_glsl_alloc(). */
8422 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
8423 | WINED3D_SHADER_CAP_SRGB_WRITE;
8426 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
8428 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
8430 TRACE("Checking support for fixup:\n");
8431 dump_color_fixup_desc(fixup);
8434 /* We support everything except YUV conversions. */
8435 if (!is_complex_fixup(fixup))
8437 TRACE("[OK]\n");
8438 return TRUE;
8441 TRACE("[FAILED]\n");
8442 return FALSE;
8445 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
8447 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
8448 /* WINED3DSIH_ADD */ shader_glsl_binop,
8449 /* WINED3DSIH_AND */ shader_glsl_binop,
8450 /* WINED3DSIH_BEM */ shader_glsl_bem,
8451 /* WINED3DSIH_BREAK */ shader_glsl_break,
8452 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
8453 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
8454 /* WINED3DSIH_CALL */ shader_glsl_call,
8455 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
8456 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
8457 /* WINED3DSIH_CND */ shader_glsl_cnd,
8458 /* WINED3DSIH_CRS */ shader_glsl_cross,
8459 /* WINED3DSIH_CUT */ shader_glsl_cut,
8460 /* WINED3DSIH_DCL */ shader_glsl_nop,
8461 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
8462 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
8463 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ NULL,
8464 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ NULL,
8465 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ NULL,
8466 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
8467 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ NULL,
8468 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
8469 /* WINED3DSIH_DCL_INPUT_PS */ NULL,
8470 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
8471 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
8472 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
8473 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
8474 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
8475 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ NULL,
8476 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
8477 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
8478 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ NULL,
8479 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
8480 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
8481 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ NULL,
8482 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ NULL,
8483 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ NULL,
8484 /* WINED3DSIH_DCL_UAV_TYPED */ NULL,
8485 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
8486 /* WINED3DSIH_DEF */ shader_glsl_nop,
8487 /* WINED3DSIH_DEFB */ shader_glsl_nop,
8488 /* WINED3DSIH_DEFI */ shader_glsl_nop,
8489 /* WINED3DSIH_DIV */ shader_glsl_binop,
8490 /* WINED3DSIH_DP2 */ shader_glsl_dot,
8491 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
8492 /* WINED3DSIH_DP3 */ shader_glsl_dot,
8493 /* WINED3DSIH_DP4 */ shader_glsl_dot,
8494 /* WINED3DSIH_DST */ shader_glsl_dst,
8495 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
8496 /* WINED3DSIH_DSX_COARSE */ NULL,
8497 /* WINED3DSIH_DSX_FINE */ NULL,
8498 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
8499 /* WINED3DSIH_DSY_COARSE */ NULL,
8500 /* WINED3DSIH_DSY_FINE */ NULL,
8501 /* WINED3DSIH_ELSE */ shader_glsl_else,
8502 /* WINED3DSIH_EMIT */ shader_glsl_emit,
8503 /* WINED3DSIH_ENDIF */ shader_glsl_end,
8504 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
8505 /* WINED3DSIH_ENDREP */ shader_glsl_end,
8506 /* WINED3DSIH_EQ */ shader_glsl_relop,
8507 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
8508 /* WINED3DSIH_EXPP */ shader_glsl_expp,
8509 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
8510 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
8511 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
8512 /* WINED3DSIH_GE */ shader_glsl_relop,
8513 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ NULL,
8514 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
8515 /* WINED3DSIH_HS_FORK_PHASE */ NULL,
8516 /* WINED3DSIH_HS_JOIN_PHASE */ NULL,
8517 /* WINED3DSIH_IADD */ shader_glsl_binop,
8518 /* WINED3DSIH_IEQ */ shader_glsl_relop,
8519 /* WINED3DSIH_IF */ shader_glsl_if,
8520 /* WINED3DSIH_IFC */ shader_glsl_ifc,
8521 /* WINED3DSIH_IGE */ shader_glsl_relop,
8522 /* WINED3DSIH_ILT */ shader_glsl_relop,
8523 /* WINED3DSIH_IMAD */ shader_glsl_mad,
8524 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
8525 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
8526 /* WINED3DSIH_IMUL */ shader_glsl_imul,
8527 /* WINED3DSIH_INE */ shader_glsl_relop,
8528 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
8529 /* WINED3DSIH_ISHL */ shader_glsl_binop,
8530 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
8531 /* WINED3DSIH_LABEL */ shader_glsl_label,
8532 /* WINED3DSIH_LD */ shader_glsl_ld,
8533 /* WINED3DSIH_LD2DMS */ NULL,
8534 /* WINED3DSIH_LD_STRUCTURED */ NULL,
8535 /* WINED3DSIH_LIT */ shader_glsl_lit,
8536 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
8537 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
8538 /* WINED3DSIH_LOOP */ shader_glsl_loop,
8539 /* WINED3DSIH_LRP */ shader_glsl_lrp,
8540 /* WINED3DSIH_LT */ shader_glsl_relop,
8541 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
8542 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
8543 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
8544 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
8545 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
8546 /* WINED3DSIH_MAD */ shader_glsl_mad,
8547 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
8548 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
8549 /* WINED3DSIH_MOV */ shader_glsl_mov,
8550 /* WINED3DSIH_MOVA */ shader_glsl_mov,
8551 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
8552 /* WINED3DSIH_MUL */ shader_glsl_binop,
8553 /* WINED3DSIH_NE */ shader_glsl_relop,
8554 /* WINED3DSIH_NOP */ shader_glsl_nop,
8555 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
8556 /* WINED3DSIH_NRM */ shader_glsl_nrm,
8557 /* WINED3DSIH_OR */ shader_glsl_binop,
8558 /* WINED3DSIH_PHASE */ shader_glsl_nop,
8559 /* WINED3DSIH_POW */ shader_glsl_pow,
8560 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
8561 /* WINED3DSIH_REP */ shader_glsl_rep,
8562 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
8563 /* WINED3DSIH_RET */ shader_glsl_ret,
8564 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
8565 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
8566 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
8567 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
8568 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
8569 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
8570 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
8571 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
8572 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
8573 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
8574 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
8575 /* WINED3DSIH_SETP */ NULL,
8576 /* WINED3DSIH_SGE */ shader_glsl_compare,
8577 /* WINED3DSIH_SGN */ shader_glsl_sgn,
8578 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
8579 /* WINED3DSIH_SLT */ shader_glsl_compare,
8580 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
8581 /* WINED3DSIH_STORE_UAV_TYPED */ NULL,
8582 /* WINED3DSIH_SUB */ shader_glsl_binop,
8583 /* WINED3DSIH_TEX */ shader_glsl_tex,
8584 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
8585 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
8586 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
8587 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
8588 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
8589 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
8590 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
8591 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
8592 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
8593 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
8594 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
8595 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
8596 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
8597 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
8598 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
8599 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
8600 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
8601 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
8602 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
8603 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
8604 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
8605 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
8606 /* WINED3DSIH_UGE */ shader_glsl_relop,
8607 /* WINED3DSIH_ULT */ shader_glsl_relop,
8608 /* WINED3DSIH_USHR */ shader_glsl_binop,
8609 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
8610 /* WINED3DSIH_XOR */ shader_glsl_binop,
8613 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
8614 SHADER_HANDLER hw_fct;
8616 /* Select handler */
8617 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
8619 /* Unhandled opcode */
8620 if (!hw_fct)
8622 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
8623 return;
8625 hw_fct(ins);
8627 shader_glsl_add_instruction_modifiers(ins);
8630 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
8632 struct shader_glsl_priv *priv = shader_priv;
8634 return priv->ffp_proj_control;
8637 const struct wined3d_shader_backend_ops glsl_shader_backend =
8639 shader_glsl_handle_instruction,
8640 shader_glsl_select,
8641 shader_glsl_disable,
8642 shader_glsl_select_depth_blt,
8643 shader_glsl_deselect_depth_blt,
8644 shader_glsl_update_float_vertex_constants,
8645 shader_glsl_update_float_pixel_constants,
8646 shader_glsl_load_constants,
8647 shader_glsl_destroy,
8648 shader_glsl_alloc,
8649 shader_glsl_free,
8650 shader_glsl_allocate_context_data,
8651 shader_glsl_free_context_data,
8652 shader_glsl_init_context_state,
8653 shader_glsl_get_caps,
8654 shader_glsl_color_fixup_supported,
8655 shader_glsl_has_ffp_proj_control,
8658 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
8660 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
8662 caps->xyzrhw = TRUE;
8663 caps->emulated_flatshading = !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
8664 caps->ffp_generic_attributes = TRUE;
8665 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
8666 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
8667 caps->max_vertex_blend_matrix_index = 0;
8668 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
8669 | WINED3DVTXPCAPS_MATERIALSOURCE7
8670 | WINED3DVTXPCAPS_VERTEXFOG
8671 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
8672 | WINED3DVTXPCAPS_POSITIONALLIGHTS
8673 | WINED3DVTXPCAPS_LOCALVIEWER
8674 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
8675 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
8676 caps->max_user_clip_planes = gl_info->limits.clipplanes;
8677 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
8680 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
8682 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8683 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8684 return 0;
8687 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8689 struct shader_glsl_priv *priv;
8691 if (shader_backend == &glsl_shader_backend)
8693 priv = shader_priv;
8695 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
8697 ERR("Failed to initialize rbtree.\n");
8698 return NULL;
8701 return priv;
8704 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
8706 return NULL;
8709 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
8711 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8712 struct glsl_ffp_vertex_shader, desc.entry);
8713 struct glsl_shader_prog_link *program, *program2;
8714 struct glsl_ffp_destroy_ctx *ctx = context;
8716 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8717 struct glsl_shader_prog_link, vs.shader_entry)
8719 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8721 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8722 HeapFree(GetProcessHeap(), 0, shader);
8725 /* Context activation is done by the caller. */
8726 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
8728 struct shader_glsl_priv *priv = device->vertex_priv;
8729 struct glsl_ffp_destroy_ctx ctx;
8731 ctx.priv = priv;
8732 ctx.gl_info = &device->adapter->gl_info;
8733 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
8736 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
8737 const struct wined3d_state *state, DWORD state_id) {}
8739 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
8740 const struct wined3d_state *state, DWORD state_id)
8742 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8745 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
8746 const struct wined3d_state *state, DWORD state_id)
8748 const struct wined3d_gl_info *gl_info = context->gl_info;
8749 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
8750 BOOL transformed = context->stream_info.position_transformed;
8751 BOOL wasrhw = context->last_was_rhw;
8752 unsigned int i;
8754 context->last_was_rhw = transformed;
8756 /* If the vertex declaration contains a transformed position attribute,
8757 * the draw uses the fixed function vertex pipeline regardless of any
8758 * vertex shader set by the application. */
8759 if (transformed != wasrhw)
8760 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8762 if (!use_vs(state))
8764 if (context->last_was_vshader)
8766 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8767 clipplane(context, state, STATE_CLIPPLANE(i));
8770 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8772 /* Because of settings->texcoords, we have to regenerate the vertex
8773 * shader on a vdecl change if there aren't enough varyings to just
8774 * always output all the texture coordinates. */
8775 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
8776 || normal != context->last_was_normal)
8777 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8779 if (use_ps(state)
8780 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
8781 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
8782 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8784 else
8786 if (!context->last_was_vshader)
8788 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
8789 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8790 clipplane(context, state, STATE_CLIPPLANE(i));
8794 context->last_was_vshader = use_vs(state);
8795 context->last_was_normal = normal;
8798 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
8799 const struct wined3d_state *state, DWORD state_id)
8801 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8802 /* Different vertex shaders potentially require a different vertex attributes setup. */
8803 if (!isStateDirty(context, STATE_VDECL))
8804 context_apply_state(context, state, STATE_VDECL);
8807 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
8808 const struct wined3d_state *state, DWORD state_id)
8810 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
8811 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
8812 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8815 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
8816 const struct wined3d_state *state, DWORD state_id)
8818 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
8819 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
8820 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
8821 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
8822 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8825 static void glsl_vertex_pipe_world(struct wined3d_context *context,
8826 const struct wined3d_state *state, DWORD state_id)
8828 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
8831 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
8832 const struct wined3d_state *state, DWORD state_id)
8834 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8837 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
8839 const struct wined3d_gl_info *gl_info = context->gl_info;
8840 unsigned int k;
8842 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
8843 | WINED3D_SHADER_CONST_FFP_LIGHTS
8844 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8846 for (k = 0; k < gl_info->limits.clipplanes; ++k)
8848 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
8849 clipplane(context, state, STATE_CLIPPLANE(k));
8853 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
8854 const struct wined3d_state *state, DWORD state_id)
8856 /* Table fog behavior depends on the projection matrix. */
8857 if (state->render_states[WINED3D_RS_FOGENABLE]
8858 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
8859 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8860 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
8863 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
8864 const struct wined3d_state *state, DWORD state_id)
8866 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
8867 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
8868 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
8869 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
8870 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8871 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
8874 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
8875 const struct wined3d_state *state, DWORD state_id)
8877 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8880 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
8881 const struct wined3d_state *state, DWORD state_id)
8883 DWORD sampler = state_id - STATE_SAMPLER(0);
8884 const struct wined3d_texture *texture = state->textures[sampler];
8885 BOOL np2;
8887 if (!texture)
8888 return;
8890 if (sampler >= MAX_TEXTURES)
8891 return;
8893 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
8894 || context->lastWasPow2Texture & (1u << sampler))
8896 if (np2)
8897 context->lastWasPow2Texture |= 1u << sampler;
8898 else
8899 context->lastWasPow2Texture &= ~(1u << sampler);
8901 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8905 static void glsl_vertex_pipe_material(struct wined3d_context *context,
8906 const struct wined3d_state *state, DWORD state_id)
8908 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
8911 static void glsl_vertex_pipe_light(struct wined3d_context *context,
8912 const struct wined3d_state *state, DWORD state_id)
8914 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
8917 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
8918 const struct wined3d_state *state, DWORD state_id)
8920 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8923 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
8924 const struct wined3d_state *state, DWORD state_id)
8926 if (!use_vs(state))
8927 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8930 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
8931 const struct wined3d_state *state, DWORD state_id)
8933 static unsigned int once;
8935 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
8936 FIXME("Non-point sprite points not supported in core profile.\n");
8939 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
8940 const struct wined3d_state *state, DWORD state_id)
8942 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
8945 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
8947 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8948 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
8949 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
8950 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
8951 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
8952 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
8953 /* Clip planes */
8954 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
8955 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
8956 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
8957 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
8958 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
8959 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
8960 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
8961 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
8962 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
8963 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
8964 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
8965 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
8966 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
8967 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
8968 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
8969 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
8970 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
8971 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
8972 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
8973 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
8974 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
8975 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
8976 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
8977 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
8978 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
8979 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
8980 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
8981 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
8982 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
8983 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
8984 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
8985 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
8986 /* Lights */
8987 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8988 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8989 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8990 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8991 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8992 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8993 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8994 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8995 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8996 /* Viewport */
8997 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
8998 /* Transform states */
8999 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
9000 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
9001 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9002 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9003 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9004 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9005 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9006 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9007 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9008 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9009 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
9010 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9011 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9012 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9013 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9014 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9015 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9016 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9017 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9018 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9019 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9020 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9021 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9022 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9023 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9024 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9025 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9026 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9027 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9028 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9029 /* Fog */
9030 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
9031 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9032 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9033 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9034 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
9035 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
9036 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9037 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9038 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
9039 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9040 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9041 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9042 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9043 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9044 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9045 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9046 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
9047 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
9048 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
9049 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
9050 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
9051 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
9052 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9053 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9054 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9055 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
9056 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9057 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9058 /* NP2 texture matrix fixups. They are not needed if
9059 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
9060 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
9061 * matrix. */
9062 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9063 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9064 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9065 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9066 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9067 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9068 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9069 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9070 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9071 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9072 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9073 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9074 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9075 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9076 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9077 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9078 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9079 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9080 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9081 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9082 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9083 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9084 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9085 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9086 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
9087 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_LEGACY_CONTEXT },
9088 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GL_EXT_NONE },
9089 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
9092 /* TODO:
9093 * - Implement vertex tweening. */
9094 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
9096 glsl_vertex_pipe_vp_enable,
9097 glsl_vertex_pipe_vp_get_caps,
9098 glsl_vertex_pipe_vp_get_emul_mask,
9099 glsl_vertex_pipe_vp_alloc,
9100 glsl_vertex_pipe_vp_free,
9101 glsl_vertex_pipe_vp_states,
9104 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
9106 /* Nothing to do. */
9109 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
9111 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
9112 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
9113 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
9114 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
9115 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
9116 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
9117 | WINED3DTEXOPCAPS_SELECTARG1
9118 | WINED3DTEXOPCAPS_SELECTARG2
9119 | WINED3DTEXOPCAPS_MODULATE4X
9120 | WINED3DTEXOPCAPS_MODULATE2X
9121 | WINED3DTEXOPCAPS_MODULATE
9122 | WINED3DTEXOPCAPS_ADDSIGNED2X
9123 | WINED3DTEXOPCAPS_ADDSIGNED
9124 | WINED3DTEXOPCAPS_ADD
9125 | WINED3DTEXOPCAPS_SUBTRACT
9126 | WINED3DTEXOPCAPS_ADDSMOOTH
9127 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
9128 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
9129 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
9130 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
9131 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
9132 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
9133 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
9134 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
9135 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
9136 | WINED3DTEXOPCAPS_DOTPRODUCT3
9137 | WINED3DTEXOPCAPS_MULTIPLYADD
9138 | WINED3DTEXOPCAPS_LERP
9139 | WINED3DTEXOPCAPS_BUMPENVMAP
9140 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
9141 caps->MaxTextureBlendStages = 8;
9142 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
9145 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
9147 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9148 return GL_EXT_EMUL_ARB_MULTITEXTURE;
9149 return 0;
9152 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
9154 struct shader_glsl_priv *priv;
9156 if (shader_backend == &glsl_shader_backend)
9158 priv = shader_priv;
9160 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
9162 ERR("Failed to initialize rbtree.\n");
9163 return NULL;
9166 return priv;
9169 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
9171 return NULL;
9174 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
9176 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
9177 struct glsl_ffp_fragment_shader, entry.entry);
9178 struct glsl_shader_prog_link *program, *program2;
9179 struct glsl_ffp_destroy_ctx *ctx = context;
9181 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
9182 struct glsl_shader_prog_link, ps.shader_entry)
9184 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
9186 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
9187 HeapFree(GetProcessHeap(), 0, shader);
9190 /* Context activation is done by the caller. */
9191 static void glsl_fragment_pipe_free(struct wined3d_device *device)
9193 struct shader_glsl_priv *priv = device->fragment_priv;
9194 struct glsl_ffp_destroy_ctx ctx;
9196 ctx.priv = priv;
9197 ctx.gl_info = &device->adapter->gl_info;
9198 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
9201 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
9202 const struct wined3d_state *state, DWORD state_id)
9204 context->last_was_pshader = use_ps(state);
9206 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9209 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
9210 const struct wined3d_state *state, DWORD state_id)
9212 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
9215 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
9216 const struct wined3d_state *state, DWORD state_id)
9218 BOOL use_vshader = use_vs(state);
9219 enum fogsource new_source;
9220 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
9221 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
9223 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9225 if (!state->render_states[WINED3D_RS_FOGENABLE])
9226 return;
9228 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
9230 if (use_vshader)
9231 new_source = FOGSOURCE_VS;
9232 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
9233 new_source = FOGSOURCE_COORD;
9234 else
9235 new_source = FOGSOURCE_FFP;
9237 else
9239 new_source = FOGSOURCE_FFP;
9242 if (new_source != context->fog_source || fogstart == fogend)
9244 context->fog_source = new_source;
9245 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
9249 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
9250 const struct wined3d_state *state, DWORD state_id)
9252 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
9253 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
9254 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9256 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
9257 glsl_fragment_pipe_fog(context, state, state_id);
9260 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
9261 const struct wined3d_state *state, DWORD state_id)
9263 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
9264 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
9265 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9268 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
9269 const struct wined3d_state *state, DWORD state_id)
9271 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9274 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
9275 const struct wined3d_state *state, DWORD state_id)
9277 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
9280 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
9281 const struct wined3d_state *state, DWORD state_id)
9283 const struct wined3d_gl_info *gl_info = context->gl_info;
9284 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
9285 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
9287 if (func)
9289 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
9290 checkGLcall("glAlphaFunc");
9294 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
9295 const struct wined3d_state *state, DWORD state_id)
9297 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9300 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
9301 const struct wined3d_state *state, DWORD state_id)
9303 const struct wined3d_gl_info *gl_info = context->gl_info;
9305 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
9307 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
9308 checkGLcall("glEnable(GL_ALPHA_TEST)");
9310 else
9312 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
9313 checkGLcall("glDisable(GL_ALPHA_TEST)");
9317 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
9318 const struct wined3d_state *state, DWORD state_id)
9320 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
9323 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
9324 const struct wined3d_state *state, DWORD state_id)
9326 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
9329 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
9330 const struct wined3d_state *state, DWORD state_id)
9332 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
9335 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
9337 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
9338 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
9339 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9340 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9341 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9342 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9343 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9344 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9345 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9346 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9347 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9348 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9349 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9350 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9351 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9352 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9353 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9354 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9355 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9356 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9357 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9358 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9359 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9360 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9361 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9362 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9363 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9364 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9365 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9366 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9367 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9368 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9369 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9370 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9371 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9372 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9373 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9374 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9375 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9376 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9377 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9378 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9379 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9380 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9381 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9382 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9383 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9384 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9385 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9386 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9387 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9388 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9389 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9390 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9391 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9392 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9393 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9394 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9395 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9396 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9397 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9398 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9399 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9400 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9401 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9402 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9403 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9404 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9405 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9406 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9407 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9408 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9409 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9410 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9411 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9412 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9413 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
9414 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
9415 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
9416 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
9417 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
9418 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
9419 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9420 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
9421 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
9422 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9423 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9424 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9425 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
9426 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
9427 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9428 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9429 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9430 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
9431 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
9432 {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 },
9433 {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 },
9434 {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 },
9435 {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 },
9436 {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 },
9437 {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 },
9438 {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 },
9439 {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 },
9440 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9441 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9442 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9443 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9444 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9445 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9446 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9447 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9448 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9449 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9450 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_LEGACY_CONTEXT},
9451 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GL_EXT_NONE },
9452 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
9455 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
9457 return TRUE;
9460 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
9464 const struct fragment_pipeline glsl_fragment_pipe =
9466 glsl_fragment_pipe_enable,
9467 glsl_fragment_pipe_get_caps,
9468 glsl_fragment_pipe_get_emul_mask,
9469 glsl_fragment_pipe_alloc,
9470 glsl_fragment_pipe_free,
9471 glsl_fragment_pipe_alloc_context_data,
9472 glsl_fragment_pipe_free_context_data,
9473 shader_glsl_color_fixup_supported,
9474 glsl_fragment_pipe_state_template,