wined3d: Handle GS inputs and outputs.
[wine.git] / dlls / wined3d / glsl_shader.c
blobec6517c629653dd037d551578097eca6a927c1f9
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;
164 struct glsl_ps_program
166 struct list shader_entry;
167 GLuint id;
168 GLint uniform_f_locations[WINED3D_MAX_PS_CONSTS_F];
169 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
170 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
171 GLint bumpenv_mat_location[MAX_TEXTURES];
172 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
173 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
174 GLint tss_constant_location[MAX_TEXTURES];
175 GLint tex_factor_location;
176 GLint specular_enable_location;
177 GLint fog_color_location;
178 GLint fog_density_location;
179 GLint fog_end_location;
180 GLint fog_scale_location;
181 GLint alpha_test_ref_location;
182 GLint ycorrection_location;
183 GLint np2_fixup_location;
184 GLint color_key_location;
185 const struct ps_np2fixup_info *np2_fixup_info;
188 /* Struct to maintain data about a linked GLSL program */
189 struct glsl_shader_prog_link
191 struct wine_rb_entry program_lookup_entry;
192 struct glsl_vs_program vs;
193 struct glsl_gs_program gs;
194 struct glsl_ps_program ps;
195 GLuint id;
196 DWORD constant_update_mask;
197 UINT constant_version;
200 struct glsl_program_key
202 GLuint vs_id;
203 GLuint gs_id;
204 GLuint ps_id;
207 struct shader_glsl_ctx_priv {
208 const struct vs_compile_args *cur_vs_args;
209 const struct ps_compile_args *cur_ps_args;
210 struct ps_np2fixup_info *cur_np2fixup_info;
211 struct wined3d_string_buffer_list *string_buffers;
214 struct glsl_context_data
216 struct glsl_shader_prog_link *glsl_program;
219 struct glsl_ps_compiled_shader
221 struct ps_compile_args args;
222 struct ps_np2fixup_info np2fixup;
223 GLuint id;
226 struct glsl_vs_compiled_shader
228 struct vs_compile_args args;
229 GLuint id;
232 struct glsl_gs_compiled_shader
234 struct gs_compile_args args;
235 GLuint id;
238 struct glsl_shader_private
240 union
242 struct glsl_vs_compiled_shader *vs;
243 struct glsl_gs_compiled_shader *gs;
244 struct glsl_ps_compiled_shader *ps;
245 } gl_shaders;
246 UINT num_gl_shaders, shader_array_size;
249 struct glsl_ffp_vertex_shader
251 struct wined3d_ffp_vs_desc desc;
252 GLuint id;
253 struct list linked_programs;
256 struct glsl_ffp_fragment_shader
258 struct ffp_frag_desc entry;
259 GLuint id;
260 struct list linked_programs;
263 struct glsl_ffp_destroy_ctx
265 struct shader_glsl_priv *priv;
266 const struct wined3d_gl_info *gl_info;
269 static const char *debug_gl_shader_type(GLenum type)
271 switch (type)
273 #define WINED3D_TO_STR(u) case u: return #u
274 WINED3D_TO_STR(GL_VERTEX_SHADER);
275 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
276 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
277 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
278 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
279 #undef WINED3D_TO_STR
280 default:
281 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
285 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
287 switch (type)
289 case WINED3D_SHADER_TYPE_VERTEX:
290 return "vs";
292 case WINED3D_SHADER_TYPE_HULL:
293 return "hs";
295 case WINED3D_SHADER_TYPE_DOMAIN:
296 return "ds";
298 case WINED3D_SHADER_TYPE_GEOMETRY:
299 return "gs";
301 case WINED3D_SHADER_TYPE_PIXEL:
302 return "ps";
304 default:
305 FIXME("Unhandled shader type %#x.\n", type);
306 return "unknown";
310 static const char *shader_glsl_get_version(const struct wined3d_gl_info *gl_info,
311 const struct wined3d_shader_version *version)
313 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
314 return "#version 150";
315 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30) && version && version->major >= 4)
316 return "#version 130";
317 else
318 return "#version 120";
321 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
323 char str[4][17];
325 wined3d_ftoa(values[0], str[0]);
326 wined3d_ftoa(values[1], str[1]);
327 wined3d_ftoa(values[2], str[2]);
328 wined3d_ftoa(values[3], str[3]);
329 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
332 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
333 const int *values, unsigned int size)
335 int i;
337 if (!size || size > 4)
339 ERR("Invalid vector size %u.\n", size);
340 return;
343 if (size > 1)
344 shader_addline(buffer, "ivec%u(", size);
346 for (i = 0; i < size; ++i)
347 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
349 if (size > 1)
350 shader_addline(buffer, ")");
353 static const char *get_info_log_line(const char **ptr)
355 const char *p, *q;
357 p = *ptr;
358 if (!(q = strstr(p, "\n")))
360 if (!*p) return NULL;
361 *ptr += strlen(p);
362 return p;
364 *ptr = q + 1;
366 return p;
369 /* Context activation is done by the caller. */
370 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
372 int length = 0;
373 char *log;
375 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
376 return;
378 if (program)
379 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
380 else
381 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
383 /* A size of 1 is just a null-terminated string, so the log should be bigger than
384 * that if there are errors. */
385 if (length > 1)
387 const char *ptr, *line;
389 log = HeapAlloc(GetProcessHeap(), 0, length);
390 /* The info log is supposed to be zero-terminated, but at least some
391 * versions of fglrx don't terminate the string properly. The reported
392 * length does include the terminator, so explicitly set it to zero
393 * here. */
394 log[length - 1] = 0;
395 if (program)
396 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
397 else
398 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
400 ptr = log;
401 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
403 WARN("Info log received from GLSL shader #%u:\n", id);
404 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
406 else
408 FIXME("Info log received from GLSL shader #%u:\n", id);
409 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
411 HeapFree(GetProcessHeap(), 0, log);
415 /* Context activation is done by the caller. */
416 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
418 const char *ptr, *line;
420 TRACE("Compiling shader object %u.\n", shader);
422 if (TRACE_ON(d3d_shader))
424 ptr = src;
425 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
428 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
429 checkGLcall("glShaderSource");
430 GL_EXTCALL(glCompileShader(shader));
431 checkGLcall("glCompileShader");
432 print_glsl_info_log(gl_info, shader, FALSE);
435 /* Context activation is done by the caller. */
436 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
438 GLint i, shader_count, source_size = -1;
439 GLuint *shaders;
440 char *source = NULL;
442 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
443 shaders = HeapAlloc(GetProcessHeap(), 0, shader_count * sizeof(*shaders));
444 if (!shaders)
446 ERR("Failed to allocate shader array memory.\n");
447 return;
450 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
451 for (i = 0; i < shader_count; ++i)
453 const char *ptr, *line;
454 GLint tmp;
456 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
458 if (source_size < tmp)
460 HeapFree(GetProcessHeap(), 0, source);
462 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
463 if (!source)
465 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
466 HeapFree(GetProcessHeap(), 0, shaders);
467 return;
469 source_size = tmp;
472 FIXME("Shader %u:\n", shaders[i]);
473 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
474 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
475 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
476 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
477 FIXME("\n");
479 ptr = source;
480 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
481 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
482 FIXME("\n");
485 HeapFree(GetProcessHeap(), 0, source);
486 HeapFree(GetProcessHeap(), 0, shaders);
489 /* Context activation is done by the caller. */
490 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
492 GLint tmp;
494 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
495 return;
497 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
498 if (!tmp)
500 FIXME("Program %u link status invalid.\n", program);
501 shader_glsl_dump_program_source(gl_info, program);
504 print_glsl_info_log(gl_info, program, TRUE);
507 /* Context activation is done by the caller. */
508 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
509 struct shader_glsl_priv *priv, const DWORD *tex_unit_map, GLuint program_id)
511 unsigned int mapped_unit;
512 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
513 const char *prefix;
514 unsigned int i, j;
515 GLint name_loc;
517 static const struct
519 enum wined3d_shader_type type;
520 unsigned int base_idx;
521 unsigned int count;
523 sampler_info[] =
525 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
526 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
529 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
531 prefix = shader_glsl_get_prefix(sampler_info[i].type);
533 for (j = 0; j < sampler_info[i].count; ++j)
535 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, j);
536 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
537 if (name_loc == -1)
538 continue;
540 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
541 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
543 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
544 continue;
547 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
548 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
551 checkGLcall("glUniform1i");
552 string_buffer_release(&priv->string_buffers, sampler_name);
555 /* Context activation is done by the caller. */
556 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
557 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
559 unsigned int start = ~0U, end = 0;
560 int stack_idx = 0;
561 unsigned int heap_idx = 1;
562 unsigned int idx;
564 if (heap->entries[heap_idx].version <= version) return;
566 idx = heap->entries[heap_idx].idx;
567 if (constant_locations[idx] != -1)
568 start = end = idx;
569 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
571 while (stack_idx >= 0)
573 /* Note that we fall through to the next case statement. */
574 switch(stack[stack_idx])
576 case HEAP_NODE_TRAVERSE_LEFT:
578 unsigned int left_idx = heap_idx << 1;
579 if (left_idx < heap->size && heap->entries[left_idx].version > version)
581 heap_idx = left_idx;
582 idx = heap->entries[heap_idx].idx;
583 if (constant_locations[idx] != -1)
585 if (start > idx)
586 start = idx;
587 if (end < idx)
588 end = idx;
591 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
592 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
593 break;
597 case HEAP_NODE_TRAVERSE_RIGHT:
599 unsigned int right_idx = (heap_idx << 1) + 1;
600 if (right_idx < heap->size && heap->entries[right_idx].version > version)
602 heap_idx = right_idx;
603 idx = heap->entries[heap_idx].idx;
604 if (constant_locations[idx] != -1)
606 if (start > idx)
607 start = idx;
608 if (end < idx)
609 end = idx;
612 stack[stack_idx++] = HEAP_NODE_POP;
613 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
614 break;
618 case HEAP_NODE_POP:
619 heap_idx >>= 1;
620 --stack_idx;
621 break;
624 if (start <= end)
625 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
626 checkGLcall("walk_constant_heap()");
629 /* Context activation is done by the caller. */
630 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
631 GLint location, const struct wined3d_vec4 *data)
633 GLfloat clamped_constant[4];
635 if (location == -1) return;
637 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
638 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
639 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
640 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
642 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
645 /* Context activation is done by the caller. */
646 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
647 const struct wined3d_vec4 *constants, const GLint *constant_locations,
648 const struct constant_heap *heap, unsigned char *stack, DWORD version)
650 int stack_idx = 0;
651 unsigned int heap_idx = 1;
652 unsigned int idx;
654 if (heap->entries[heap_idx].version <= version) return;
656 idx = heap->entries[heap_idx].idx;
657 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
658 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
660 while (stack_idx >= 0)
662 /* Note that we fall through to the next case statement. */
663 switch(stack[stack_idx])
665 case HEAP_NODE_TRAVERSE_LEFT:
667 unsigned int left_idx = heap_idx << 1;
668 if (left_idx < heap->size && heap->entries[left_idx].version > version)
670 heap_idx = left_idx;
671 idx = heap->entries[heap_idx].idx;
672 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
674 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
675 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
676 break;
680 case HEAP_NODE_TRAVERSE_RIGHT:
682 unsigned int right_idx = (heap_idx << 1) + 1;
683 if (right_idx < heap->size && heap->entries[right_idx].version > version)
685 heap_idx = right_idx;
686 idx = heap->entries[heap_idx].idx;
687 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
689 stack[stack_idx++] = HEAP_NODE_POP;
690 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
691 break;
695 case HEAP_NODE_POP:
696 heap_idx >>= 1;
697 --stack_idx;
698 break;
701 checkGLcall("walk_constant_heap_clamped()");
704 /* Context activation is done by the caller. */
705 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
706 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
707 unsigned char *stack, unsigned int version)
709 const struct wined3d_shader_lconst *lconst;
711 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
712 if (shader->reg_maps.shader_version.major == 1
713 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
714 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
715 else
716 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
718 if (!shader->load_local_constsF)
720 TRACE("No need to load local float constants for this shader\n");
721 return;
724 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
725 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
727 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
729 checkGLcall("glUniform4fv()");
732 /* Context activation is done by the caller. */
733 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
734 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
736 unsigned int i;
737 struct list* ptr;
739 for (i = 0; constants_set; constants_set >>= 1, ++i)
741 if (!(constants_set & 1)) continue;
743 /* We found this uniform name in the program - go ahead and send the data */
744 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
747 /* Load immediate constants */
748 ptr = list_head(&shader->constantsI);
749 while (ptr)
751 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
752 unsigned int idx = lconst->idx;
753 const GLint *values = (const GLint *)lconst->value;
755 /* We found this uniform name in the program - go ahead and send the data */
756 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
757 ptr = list_next(&shader->constantsI, ptr);
759 checkGLcall("glUniform4iv()");
762 /* Context activation is done by the caller. */
763 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
764 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
766 unsigned int i;
767 struct list* ptr;
769 for (i = 0; constants_set; constants_set >>= 1, ++i)
771 if (!(constants_set & 1)) continue;
773 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
776 /* Load immediate constants */
777 ptr = list_head(&shader->constantsB);
778 while (ptr)
780 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
781 unsigned int idx = lconst->idx;
782 const GLint *values = (const GLint *)lconst->value;
784 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
785 ptr = list_next(&shader->constantsB, ptr);
787 checkGLcall("glUniform1iv()");
790 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
792 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
795 /* Context activation is done by the caller (state handler). */
796 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
797 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
799 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
800 UINT fixup = ps->np2_fixup_info->active;
801 UINT i;
803 for (i = 0; fixup; fixup >>= 1, ++i)
805 const struct wined3d_texture *tex = state->textures[i];
806 unsigned char idx = ps->np2_fixup_info->idx[i];
807 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
809 if (!tex)
811 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
812 continue;
815 if (idx % 2)
817 tex_dim[2] = tex->pow2_matrix[0];
818 tex_dim[3] = tex->pow2_matrix[5];
820 else
822 tex_dim[0] = tex->pow2_matrix[0];
823 tex_dim[1] = tex->pow2_matrix[5];
827 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
830 /* Taken and adapted from Mesa. */
831 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
833 float pos, neg, t, det;
834 struct wined3d_matrix temp;
836 /* Calculate the determinant of upper left 3x3 submatrix and
837 * determine if the matrix is singular. */
838 pos = neg = 0.0f;
839 t = in->_11 * in->_22 * in->_33;
840 if (t >= 0.0f)
841 pos += t;
842 else
843 neg += t;
845 t = in->_21 * in->_32 * in->_13;
846 if (t >= 0.0f)
847 pos += t;
848 else
849 neg += t;
850 t = in->_31 * in->_12 * in->_23;
851 if (t >= 0.0f)
852 pos += t;
853 else
854 neg += t;
856 t = -in->_31 * in->_22 * in->_13;
857 if (t >= 0.0f)
858 pos += t;
859 else
860 neg += t;
861 t = -in->_21 * in->_12 * in->_33;
862 if (t >= 0.0f)
863 pos += t;
864 else
865 neg += t;
867 t = -in->_11 * in->_32 * in->_23;
868 if (t >= 0.0f)
869 pos += t;
870 else
871 neg += t;
873 det = pos + neg;
875 if (fabsf(det) < 1e-25f)
876 return FALSE;
878 det = 1.0f / det;
879 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
880 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
881 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
882 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
883 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
884 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
885 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
886 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
887 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
889 *out = temp;
890 return TRUE;
893 static void swap_rows(float **a, float **b)
895 float *tmp = *a;
897 *a = *b;
898 *b = tmp;
901 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
903 float wtmp[4][8];
904 float m0, m1, m2, m3, s;
905 float *r0, *r1, *r2, *r3;
907 r0 = wtmp[0];
908 r1 = wtmp[1];
909 r2 = wtmp[2];
910 r3 = wtmp[3];
912 r0[0] = m->_11;
913 r0[1] = m->_12;
914 r0[2] = m->_13;
915 r0[3] = m->_14;
916 r0[4] = 1.0f;
917 r0[5] = r0[6] = r0[7] = 0.0f;
919 r1[0] = m->_21;
920 r1[1] = m->_22;
921 r1[2] = m->_23;
922 r1[3] = m->_24;
923 r1[5] = 1.0f;
924 r1[4] = r1[6] = r1[7] = 0.0f;
926 r2[0] = m->_31;
927 r2[1] = m->_32;
928 r2[2] = m->_33;
929 r2[3] = m->_34;
930 r2[6] = 1.0f;
931 r2[4] = r2[5] = r2[7] = 0.0f;
933 r3[0] = m->_41;
934 r3[1] = m->_42;
935 r3[2] = m->_43;
936 r3[3] = m->_44;
937 r3[7] = 1.0f;
938 r3[4] = r3[5] = r3[6] = 0.0f;
940 /* Choose pivot - or die. */
941 if (fabsf(r3[0]) > fabsf(r2[0]))
942 swap_rows(&r3, &r2);
943 if (fabsf(r2[0]) > fabsf(r1[0]))
944 swap_rows(&r2, &r1);
945 if (fabsf(r1[0]) > fabsf(r0[0]))
946 swap_rows(&r1, &r0);
947 if (r0[0] == 0.0f)
948 return FALSE;
950 /* Eliminate first variable. */
951 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
952 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
953 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
954 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
955 s = r0[4];
956 if (s != 0.0f)
958 r1[4] -= m1 * s;
959 r2[4] -= m2 * s;
960 r3[4] -= m3 * s;
962 s = r0[5];
963 if (s != 0.0f)
965 r1[5] -= m1 * s;
966 r2[5] -= m2 * s;
967 r3[5] -= m3 * s;
969 s = r0[6];
970 if (s != 0.0f)
972 r1[6] -= m1 * s;
973 r2[6] -= m2 * s;
974 r3[6] -= m3 * s;
976 s = r0[7];
977 if (s != 0.0f)
979 r1[7] -= m1 * s;
980 r2[7] -= m2 * s;
981 r3[7] -= m3 * s;
984 /* Choose pivot - or die. */
985 if (fabsf(r3[1]) > fabsf(r2[1]))
986 swap_rows(&r3, &r2);
987 if (fabsf(r2[1]) > fabsf(r1[1]))
988 swap_rows(&r2, &r1);
989 if (r1[1] == 0.0f)
990 return FALSE;
992 /* Eliminate second variable. */
993 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
994 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
995 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
996 s = r1[4];
997 if (s != 0.0f)
999 r2[4] -= m2 * s;
1000 r3[4] -= m3 * s;
1002 s = r1[5];
1003 if (s != 0.0f)
1005 r2[5] -= m2 * s;
1006 r3[5] -= m3 * s;
1008 s = r1[6];
1009 if (s != 0.0f)
1011 r2[6] -= m2 * s;
1012 r3[6] -= m3 * s;
1014 s = r1[7];
1015 if (s != 0.0f)
1017 r2[7] -= m2 * s;
1018 r3[7] -= m3 * s;
1021 /* Choose pivot - or die. */
1022 if (fabsf(r3[2]) > fabsf(r2[2]))
1023 swap_rows(&r3, &r2);
1024 if (r2[2] == 0.0f)
1025 return FALSE;
1027 /* Eliminate third variable. */
1028 m3 = r3[2] / r2[2];
1029 r3[3] -= m3 * r2[3];
1030 r3[4] -= m3 * r2[4];
1031 r3[5] -= m3 * r2[5];
1032 r3[6] -= m3 * r2[6];
1033 r3[7] -= m3 * r2[7];
1035 /* Last check. */
1036 if (r3[3] == 0.0f)
1037 return FALSE;
1039 /* Back substitute row 3. */
1040 s = 1.0f / r3[3];
1041 r3[4] *= s;
1042 r3[5] *= s;
1043 r3[6] *= s;
1044 r3[7] *= s;
1046 /* Back substitute row 2. */
1047 m2 = r2[3];
1048 s = 1.0f / r2[2];
1049 r2[4] = s * (r2[4] - r3[4] * m2);
1050 r2[5] = s * (r2[5] - r3[5] * m2);
1051 r2[6] = s * (r2[6] - r3[6] * m2);
1052 r2[7] = s * (r2[7] - r3[7] * m2);
1053 m1 = r1[3];
1054 r1[4] -= r3[4] * m1;
1055 r1[5] -= r3[5] * m1;
1056 r1[6] -= r3[6] * m1;
1057 r1[7] -= r3[7] * m1;
1058 m0 = r0[3];
1059 r0[4] -= r3[4] * m0;
1060 r0[5] -= r3[5] * m0;
1061 r0[6] -= r3[6] * m0;
1062 r0[7] -= r3[7] * m0;
1064 /* Back substitute row 1. */
1065 m1 = r1[2];
1066 s = 1.0f / r1[1];
1067 r1[4] = s * (r1[4] - r2[4] * m1);
1068 r1[5] = s * (r1[5] - r2[5] * m1);
1069 r1[6] = s * (r1[6] - r2[6] * m1);
1070 r1[7] = s * (r1[7] - r2[7] * m1);
1071 m0 = r0[2];
1072 r0[4] -= r2[4] * m0;
1073 r0[5] -= r2[5] * m0;
1074 r0[6] -= r2[6] * m0;
1075 r0[7] -= r2[7] * m0;
1077 /* Back substitute row 0. */
1078 m0 = r0[1];
1079 s = 1.0f / r0[0];
1080 r0[4] = s * (r0[4] - r1[4] * m0);
1081 r0[5] = s * (r0[5] - r1[5] * m0);
1082 r0[6] = s * (r0[6] - r1[6] * m0);
1083 r0[7] = s * (r0[7] - r1[7] * m0);
1085 out->_11 = r0[4];
1086 out->_12 = r0[5];
1087 out->_13 = r0[6];
1088 out->_14 = r0[7];
1089 out->_21 = r1[4];
1090 out->_22 = r1[5];
1091 out->_23 = r1[6];
1092 out->_24 = r1[7];
1093 out->_31 = r2[4];
1094 out->_32 = r2[5];
1095 out->_33 = r2[6];
1096 out->_34 = r2[7];
1097 out->_41 = r3[4];
1098 out->_42 = r3[5];
1099 out->_43 = r3[6];
1100 out->_44 = r3[7];
1102 return TRUE;
1105 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1106 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1108 const struct wined3d_gl_info *gl_info = context->gl_info;
1109 float mat[3 * 3];
1110 struct wined3d_matrix mv;
1111 unsigned int i, j;
1113 if (prog->vs.normal_matrix_location == -1)
1114 return;
1116 get_modelview_matrix(context, state, 0, &mv);
1117 if (context->swapchain->device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING)
1118 invert_matrix_3d(&mv, &mv);
1119 else
1120 invert_matrix(&mv, &mv);
1121 /* Tests show that singular modelview matrices are used unchanged as normal
1122 * matrices on D3D3 and older. There seems to be no clearly consistent
1123 * behavior on newer D3D versions so always follow older ddraw behavior. */
1124 for (i = 0; i < 3; ++i)
1125 for (j = 0; j < 3; ++j)
1126 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1128 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1129 checkGLcall("glUniformMatrix3fv");
1132 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1133 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1135 const struct wined3d_gl_info *gl_info = context->gl_info;
1136 struct wined3d_matrix mat;
1138 if (tex >= MAX_TEXTURES)
1139 return;
1140 if (prog->vs.texture_matrix_location[tex] == -1)
1141 return;
1143 get_texture_matrix(context, state, tex, &mat);
1144 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1145 checkGLcall("glUniformMatrix4fv");
1148 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1149 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1151 const struct wined3d_gl_info *gl_info = context->gl_info;
1153 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1155 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1156 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1158 else
1160 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1162 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1164 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1165 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1166 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1167 checkGLcall("setting FFP material uniforms");
1170 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1171 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1173 const struct wined3d_gl_info *gl_info = context->gl_info;
1174 struct wined3d_color color;
1176 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1177 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1178 checkGLcall("glUniform3fv");
1181 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1182 const struct wined3d_matrix *src2)
1184 struct wined3d_vec4 temp;
1186 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1187 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1188 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1189 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1191 *dest = temp;
1194 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1195 const struct wined3d_state *state, unsigned int light, struct glsl_shader_prog_link *prog)
1197 const struct wined3d_gl_info *gl_info = context->gl_info;
1198 const struct wined3d_light_info *light_info = state->lights[light];
1199 struct wined3d_vec4 vec4;
1200 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1202 if (!light_info)
1203 return;
1205 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1206 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1207 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1209 switch (light_info->OriginalParms.type)
1211 case WINED3D_LIGHT_POINT:
1212 multiply_vector_matrix(&vec4, &light_info->position, view);
1213 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1214 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1215 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1216 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1217 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1218 break;
1220 case WINED3D_LIGHT_SPOT:
1221 multiply_vector_matrix(&vec4, &light_info->position, view);
1222 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1224 multiply_vector_matrix(&vec4, &light_info->direction, view);
1225 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1227 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1228 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1229 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1230 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1231 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1232 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1233 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1234 break;
1236 case WINED3D_LIGHT_DIRECTIONAL:
1237 multiply_vector_matrix(&vec4, &light_info->direction, view);
1238 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1239 break;
1241 case WINED3D_LIGHT_PARALLELPOINT:
1242 multiply_vector_matrix(&vec4, &light_info->position, view);
1243 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1244 break;
1246 default:
1247 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1249 checkGLcall("setting FFP lights uniforms");
1252 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1253 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1255 const struct wined3d_gl_info *gl_info = context->gl_info;
1256 float min, max;
1257 float size, att[3];
1259 get_pointsize_minmax(context, state, &min, &max);
1261 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1262 checkGLcall("glUniform1f");
1263 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1264 checkGLcall("glUniform1f");
1266 get_pointsize(context, state, &size, att);
1268 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1269 checkGLcall("glUniform1f");
1270 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1271 checkGLcall("glUniform1f");
1272 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1273 checkGLcall("glUniform1f");
1274 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1275 checkGLcall("glUniform1f");
1278 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1279 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1281 const struct wined3d_gl_info *gl_info = context->gl_info;
1282 struct wined3d_color color;
1283 float start, end, scale;
1284 union
1286 DWORD d;
1287 float f;
1288 } tmpvalue;
1290 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1291 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1292 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1293 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1294 get_fog_start_end(context, state, &start, &end);
1295 scale = 1.0f / (end - start);
1296 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1297 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1298 checkGLcall("fog emulation uniforms");
1301 /* Context activation is done by the caller (state handler). */
1302 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1303 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1305 struct wined3d_color float_key[2];
1306 const struct wined3d_texture *texture = state->textures[0];
1308 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1309 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1312 /* Context activation is done by the caller (state handler). */
1313 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1314 const struct wined3d_state *state)
1316 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1317 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1318 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1319 const struct wined3d_gl_info *gl_info = context->gl_info;
1320 struct shader_glsl_priv *priv = shader_priv;
1321 float position_fixup[4];
1322 DWORD update_mask;
1324 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1325 UINT constant_version;
1326 int i;
1328 if (!prog) {
1329 /* No GLSL program set - nothing to do. */
1330 return;
1332 constant_version = prog->constant_version;
1333 update_mask = context->constant_update_mask & prog->constant_update_mask;
1335 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1336 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1337 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1339 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1340 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1341 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1343 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1344 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1345 vshader->reg_maps.boolean_constants);
1347 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1348 shader_glsl_pointsize_uniform(context, state, prog);
1350 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
1352 shader_get_position_fixup(context, state, position_fixup);
1353 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1354 checkGLcall("glUniform4fv");
1357 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1359 struct wined3d_matrix mat;
1361 get_modelview_matrix(context, state, 0, &mat);
1362 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1363 checkGLcall("glUniformMatrix4fv");
1365 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1368 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1370 struct wined3d_matrix mat;
1372 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1374 if (prog->vs.modelview_matrix_location[i] == -1)
1375 break;
1377 get_modelview_matrix(context, state, i, &mat);
1378 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1379 checkGLcall("glUniformMatrix4fv");
1383 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1385 struct wined3d_matrix projection;
1387 get_projection_matrix(context, state, &projection);
1388 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1389 checkGLcall("glUniformMatrix4fv");
1392 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1394 for (i = 0; i < MAX_TEXTURES; ++i)
1395 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1398 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1399 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1401 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1403 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1404 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1405 shader_glsl_ffp_vertex_light_uniform(context, state, i, prog);
1408 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1409 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1410 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1412 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1413 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1414 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1416 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1417 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1418 pshader->reg_maps.boolean_constants);
1420 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1422 for (i = 0; i < MAX_TEXTURES; ++i)
1424 if (prog->ps.bumpenv_mat_location[i] == -1)
1425 continue;
1427 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1428 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1430 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1432 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1433 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1434 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1435 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1439 checkGLcall("bump env uniforms");
1442 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1444 const struct wined3d_vec4 correction_params =
1446 /* Position is relative to the framebuffer, not the viewport. */
1447 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1448 context->render_offscreen ? 1.0f : -1.0f,
1449 0.0f,
1450 0.0f,
1453 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1456 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1457 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1458 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1459 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1461 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1463 struct wined3d_color color;
1465 if (prog->ps.tex_factor_location != -1)
1467 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1468 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1471 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1472 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1473 else
1474 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1476 for (i = 0; i < MAX_TEXTURES; ++i)
1478 if (prog->ps.tss_constant_location[i] == -1)
1479 continue;
1481 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1482 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1485 checkGLcall("fixed function uniforms");
1488 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1489 shader_glsl_load_fog_uniform(context, state, prog);
1491 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1493 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
1495 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1496 checkGLcall("alpha test emulation uniform");
1499 if (priv->next_constant_version == UINT_MAX)
1501 TRACE("Max constant version reached, resetting to 0.\n");
1502 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1503 priv->next_constant_version = 1;
1505 else
1507 prog->constant_version = priv->next_constant_version++;
1511 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1513 struct constant_entry *entries = heap->entries;
1514 unsigned int *positions = heap->positions;
1515 unsigned int heap_idx, parent_idx;
1517 if (!heap->contained[idx])
1519 heap_idx = heap->size++;
1520 heap->contained[idx] = TRUE;
1522 else
1524 heap_idx = positions[idx];
1527 while (heap_idx > 1)
1529 parent_idx = heap_idx >> 1;
1531 if (new_version <= entries[parent_idx].version) break;
1533 entries[heap_idx] = entries[parent_idx];
1534 positions[entries[parent_idx].idx] = heap_idx;
1535 heap_idx = parent_idx;
1538 entries[heap_idx].version = new_version;
1539 entries[heap_idx].idx = idx;
1540 positions[idx] = heap_idx;
1543 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1545 struct shader_glsl_priv *priv = device->shader_priv;
1546 struct constant_heap *heap = &priv->vconst_heap;
1547 UINT i;
1549 for (i = start; i < count + start; ++i)
1551 update_heap_entry(heap, i, priv->next_constant_version);
1555 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1557 struct shader_glsl_priv *priv = device->shader_priv;
1558 struct constant_heap *heap = &priv->pconst_heap;
1559 UINT i;
1561 for (i = start; i < count + start; ++i)
1563 update_heap_entry(heap, i, priv->next_constant_version);
1567 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1569 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1570 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1571 if(shader_major > 3) return ret;
1573 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1574 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1575 return ret;
1578 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1580 return gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
1583 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
1585 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
1588 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1589 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1591 va_list args;
1592 int ret;
1594 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1595 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1596 for (;;)
1598 va_start(args, format);
1599 ret = shader_vaddline(buffer, format, args);
1600 va_end(args);
1601 if (!ret)
1602 return;
1603 if (!string_buffer_resize(buffer, ret))
1604 return;
1608 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1609 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1611 va_list args;
1612 int ret;
1614 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1615 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1616 for (;;)
1618 va_start(args, format);
1619 ret = shader_vaddline(buffer, format, args);
1620 va_end(args);
1621 if (!ret)
1622 return;
1623 if (!string_buffer_resize(buffer, ret))
1624 return;
1628 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
1630 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
1633 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
1635 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
1636 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1637 const BOOL *input_reg_used = shader->u.ps.input_reg_used;
1638 unsigned int i;
1640 if (reg_maps->shader_version.major < 3)
1641 return input_reg_used[idx];
1643 for (i = 0; i < input_signature->element_count; ++i)
1645 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
1647 if (!(reg_maps->input_registers & (1u << input->register_idx)))
1648 continue;
1650 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
1651 && input->semantic_idx == idx)
1653 if (input_reg_used[input->register_idx])
1654 return TRUE;
1655 else
1656 return FALSE;
1659 return FALSE;
1662 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
1663 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
1665 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
1667 if (version->major >= 4)
1668 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
1669 else
1670 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
1673 /** Generate the variable & register declarations for the GLSL output target */
1674 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1675 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1676 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1678 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1679 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1680 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1681 const struct wined3d_gl_info *gl_info = context->gl_info;
1682 unsigned int i, extra_constants_needed = 0;
1683 const struct wined3d_shader_lconst *lconst;
1684 const char *prefix;
1685 DWORD map;
1687 prefix = shader_glsl_get_prefix(version->type);
1689 /* Prototype the subroutines */
1690 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1692 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1695 /* Declare the constants (aka uniforms) */
1696 if (shader->limits->constant_float > 0)
1698 unsigned max_constantsF;
1700 /* Unless the shader uses indirect addressing, always declare the
1701 * maximum array size and ignore that we need some uniforms privately.
1702 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1703 * and immediate values, still declare VC[256]. If the shader needs
1704 * more uniforms than we have it won't work in any case. If it uses
1705 * less, the compiler will figure out which uniforms are really used
1706 * and strip them out. This allows a shader to use c255 on a dx9 card,
1707 * as long as it doesn't also use all the other constants.
1709 * If the shader uses indirect addressing the compiler must assume
1710 * that all declared uniforms are used. In this case, declare only the
1711 * amount that we're assured to have.
1713 * Thus we run into problems in these two cases:
1714 * 1) The shader really uses more uniforms than supported.
1715 * 2) The shader uses indirect addressing, less constants than
1716 * supported, but uses a constant index > #supported consts. */
1717 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1719 /* No indirect addressing here. */
1720 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1722 else
1724 if (reg_maps->usesrelconstF)
1726 /* Subtract the other potential uniforms from the max
1727 * available (bools, ints, and 1 row of projection matrix).
1728 * Subtract another uniform for immediate values, which have
1729 * to be loaded via uniform by the driver as well. The shader
1730 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1731 * shader code, so one vec4 should be enough. (Unfortunately
1732 * the Nvidia driver doesn't store 128 and -128 in one float).
1734 * Writing gl_ClipVertex requires one uniform for each
1735 * clipplane as well. */
1736 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1737 if (vs_args->clip_enabled)
1738 max_constantsF -= gl_info->limits.clipplanes;
1739 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
1740 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1741 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1742 * for now take this into account when calculating the number of available constants
1744 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
1745 /* Set by driver quirks in directx.c */
1746 max_constantsF -= gl_info->reserved_glsl_constants;
1748 if (max_constantsF < shader->limits->constant_float)
1750 static unsigned int once;
1752 if (!once++)
1753 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1754 " it may not render correctly.\n");
1755 else
1756 WARN("The hardware does not support enough uniform components to run this shader.\n");
1759 else
1761 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1764 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1765 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1768 /* Always declare the full set of constants, the compiler can remove the
1769 * unused ones because d3d doesn't (yet) support indirect int and bool
1770 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1771 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1772 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1774 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1775 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1777 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1779 if (reg_maps->cb_sizes[i])
1780 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1781 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1784 /* Declare texture samplers */
1785 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1787 struct wined3d_shader_sampler_map_entry *entry;
1788 const char *sampler_type_prefix, *sampler_type;
1789 BOOL shadow_sampler, tex_rect;
1791 entry = &reg_maps->sampler_map.entries[i];
1793 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1795 ERR("Invalid resource index %u.\n", entry->resource_idx);
1796 continue;
1799 switch (reg_maps->resource_info[entry->resource_idx].data_type)
1801 case WINED3D_DATA_FLOAT:
1802 case WINED3D_DATA_UNORM:
1803 case WINED3D_DATA_SNORM:
1804 sampler_type_prefix = "";
1805 break;
1807 case WINED3D_DATA_INT:
1808 sampler_type_prefix = "i";
1809 break;
1811 case WINED3D_DATA_UINT:
1812 sampler_type_prefix = "u";
1813 break;
1815 default:
1816 sampler_type_prefix = "";
1817 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
1818 break;
1821 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
1822 switch (reg_maps->resource_info[entry->resource_idx].type)
1824 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1825 if (shadow_sampler)
1826 sampler_type = "sampler1DShadow";
1827 else
1828 sampler_type = "sampler1D";
1829 break;
1831 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1832 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1833 && (ps_args->np2_fixup & (1u << entry->resource_idx))
1834 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1835 if (shadow_sampler)
1837 if (tex_rect)
1838 sampler_type = "sampler2DRectShadow";
1839 else
1840 sampler_type = "sampler2DShadow";
1842 else
1844 if (tex_rect)
1845 sampler_type = "sampler2DRect";
1846 else
1847 sampler_type = "sampler2D";
1849 break;
1851 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1852 if (shadow_sampler)
1853 FIXME("Unsupported 3D shadow sampler.\n");
1854 sampler_type = "sampler3D";
1855 break;
1857 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1858 if (shadow_sampler)
1859 FIXME("Unsupported Cube shadow sampler.\n");
1860 sampler_type = "samplerCube";
1861 break;
1863 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
1864 if (shadow_sampler)
1865 sampler_type = "sampler2DArrayShadow";
1866 else
1867 sampler_type = "sampler2DArray";
1868 break;
1870 default:
1871 sampler_type = "unsupported_sampler";
1872 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
1873 break;
1875 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
1876 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
1879 /* Declare uniforms for NP2 texcoord fixup:
1880 * This is NOT done inside the loop that declares the texture samplers
1881 * since the NP2 fixup code is currently only used for the GeforceFX
1882 * series and when forcing the ARB_npot extension off. Modern cards just
1883 * skip the code anyway, so put it inside a separate loop. */
1884 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1886 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1887 UINT cur = 0;
1889 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1890 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1891 * samplerNP2Fixup stores texture dimensions and is updated through
1892 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1894 for (i = 0; i < shader->limits->sampler; ++i)
1896 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
1897 continue;
1899 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1901 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1902 continue;
1905 fixup->idx[i] = cur++;
1908 fixup->num_consts = (cur + 1) >> 1;
1909 fixup->active = ps_args->np2_fixup;
1910 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1913 /* Declare address variables */
1914 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1916 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1919 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1921 for (i = 0; i < shader->input_signature.element_count; ++i)
1923 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
1924 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
1925 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
1926 prefix, e->register_idx);
1927 else if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
1928 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1929 prefix, e->register_idx);
1930 else
1931 shader_addline(buffer, "%s vec4 %s_in%u;\n",
1932 get_attribute_keyword(gl_info), prefix, e->register_idx);
1935 if (vs_args->point_size && !vs_args->per_vertex_point_size)
1937 shader_addline(buffer, "uniform struct\n{\n");
1938 shader_addline(buffer, " float size;\n");
1939 shader_addline(buffer, " float size_min;\n");
1940 shader_addline(buffer, " float size_max;\n");
1941 shader_addline(buffer, "} ffp_point;\n");
1944 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && version->major < 3)
1946 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_diffuse;\n");
1947 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_specular;\n");
1948 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
1949 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
1952 shader_addline(buffer, "uniform vec4 posFixup;\n");
1953 if (version->major < 4)
1954 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
1956 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1958 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
1960 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1962 if (version->major < 3 || ps_args->vp_mode != vertexshader)
1964 shader_addline(buffer, "uniform struct\n{\n");
1965 shader_addline(buffer, " vec4 color;\n");
1966 shader_addline(buffer, " float density;\n");
1967 shader_addline(buffer, " float end;\n");
1968 shader_addline(buffer, " float scale;\n");
1969 shader_addline(buffer, "} ffp_fog;\n");
1971 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
1973 if (glsl_is_color_reg_read(shader, 0))
1974 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
1975 if (glsl_is_color_reg_read(shader, 1))
1976 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
1977 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
1978 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
1980 else
1982 if (glsl_is_color_reg_read(shader, 0))
1983 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_diffuse;\n");
1984 if (glsl_is_color_reg_read(shader, 1))
1985 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_specular;\n");
1986 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
1987 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
1988 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
1992 if (version->major >= 3)
1994 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
1996 if (ps_args->vp_mode == vertexshader)
1997 declare_in_varying(gl_info, buffer, FALSE, "vec4 %s_link[%u];\n", prefix, in_count);
1998 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
2001 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
2003 if (!(map & 1))
2004 continue;
2006 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
2008 if (reg_maps->luminanceparams & (1u << i))
2010 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
2011 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
2012 extra_constants_needed++;
2015 extra_constants_needed++;
2018 if (ps_args->srgb_correction)
2020 shader_addline(buffer, "const vec4 srgb_const0 = ");
2021 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
2022 shader_addline(buffer, ";\n");
2023 shader_addline(buffer, "const vec4 srgb_const1 = ");
2024 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
2025 shader_addline(buffer, ";\n");
2027 if (reg_maps->vpos || reg_maps->usesdsy)
2029 ++extra_constants_needed;
2030 shader_addline(buffer, "uniform vec4 ycorrection;\n");
2031 shader_addline(buffer, "vec4 vpos;\n");
2034 if (ps_args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
2035 shader_addline(buffer, "uniform float alpha_test_ref;\n");
2037 if (!needs_legacy_glsl_syntax(gl_info))
2038 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
2040 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
2041 FIXME("Insufficient uniforms to run this shader.\n");
2044 /* Declare output register temporaries */
2045 if (shader->limits->packed_output)
2046 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2048 /* Declare temporary variables */
2049 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2051 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
2054 /* Declare loop registers aLx */
2055 if (version->major < 4)
2057 for (i = 0; i < reg_maps->loop_depth; ++i)
2059 shader_addline(buffer, "int aL%u;\n", i);
2060 shader_addline(buffer, "int tmpInt%u;\n", i);
2064 /* Temporary variables for matrix operations */
2065 shader_addline(buffer, "vec4 tmp0;\n");
2066 shader_addline(buffer, "vec4 tmp1;\n");
2068 if (!shader->load_local_constsF)
2070 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2072 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2073 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2074 shader_addline(buffer, ";\n");
2079 /*****************************************************************************
2080 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2082 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2083 ****************************************************************************/
2085 /* Prototypes */
2086 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2087 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2089 /** Used for opcode modifiers - They multiply the result by the specified amount */
2090 static const char * const shift_glsl_tab[] = {
2091 "", /* 0 (none) */
2092 "2.0 * ", /* 1 (x2) */
2093 "4.0 * ", /* 2 (x4) */
2094 "8.0 * ", /* 3 (x8) */
2095 "16.0 * ", /* 4 (x16) */
2096 "32.0 * ", /* 5 (x32) */
2097 "", /* 6 (x64) */
2098 "", /* 7 (x128) */
2099 "", /* 8 (d256) */
2100 "", /* 9 (d128) */
2101 "", /* 10 (d64) */
2102 "", /* 11 (d32) */
2103 "0.0625 * ", /* 12 (d16) */
2104 "0.125 * ", /* 13 (d8) */
2105 "0.25 * ", /* 14 (d4) */
2106 "0.5 * " /* 15 (d2) */
2109 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2110 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2111 const char *in_reg, const char *in_regswizzle, char *out_str)
2113 switch (src_modifier)
2115 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2116 case WINED3DSPSM_DW:
2117 case WINED3DSPSM_NONE:
2118 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2119 break;
2120 case WINED3DSPSM_NEG:
2121 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2122 break;
2123 case WINED3DSPSM_NOT:
2124 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2125 break;
2126 case WINED3DSPSM_BIAS:
2127 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2128 break;
2129 case WINED3DSPSM_BIASNEG:
2130 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2131 break;
2132 case WINED3DSPSM_SIGN:
2133 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2134 break;
2135 case WINED3DSPSM_SIGNNEG:
2136 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2137 break;
2138 case WINED3DSPSM_COMP:
2139 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2140 break;
2141 case WINED3DSPSM_X2:
2142 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2143 break;
2144 case WINED3DSPSM_X2NEG:
2145 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2146 break;
2147 case WINED3DSPSM_ABS:
2148 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2149 break;
2150 case WINED3DSPSM_ABSNEG:
2151 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2152 break;
2153 default:
2154 FIXME("Unhandled modifier %u\n", src_modifier);
2155 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2159 /** Writes the GLSL variable name that corresponds to the register that the
2160 * DX opcode parameter is trying to access */
2161 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2162 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2164 /* oPos, oFog and oPts in D3D */
2165 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2167 const struct wined3d_shader *shader = ins->ctx->shader;
2168 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2169 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2170 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2171 const char *prefix = shader_glsl_get_prefix(version->type);
2172 struct glsl_src_param rel_param0, rel_param1;
2173 char imm_str[4][17];
2175 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2176 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2177 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2178 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2179 *is_color = FALSE;
2181 switch (reg->type)
2183 case WINED3DSPR_TEMP:
2184 sprintf(register_name, "R%u", reg->idx[0].offset);
2185 break;
2187 case WINED3DSPR_INPUT:
2188 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2190 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2192 if (reg->idx[0].rel_addr)
2193 FIXME("VS3+ input registers relative addressing.\n");
2194 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2195 *is_color = TRUE;
2196 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2197 break;
2200 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2202 if (reg->idx[0].rel_addr)
2204 if (reg->idx[1].rel_addr)
2205 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
2206 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2207 else
2208 sprintf(register_name, "gs_in[%s + %u][%u]",
2209 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
2211 else if (reg->idx[1].rel_addr)
2212 sprintf(register_name, "gs_in[%u][%s + %u]",
2213 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2214 else
2215 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
2216 break;
2219 /* pixel shaders >= 3.0 */
2220 if (version->major >= 3)
2222 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2223 unsigned int in_count = vec4_varyings(version->major, gl_info);
2225 if (reg->idx[0].rel_addr)
2227 /* Removing a + 0 would be an obvious optimization, but
2228 * OS X doesn't see the NOP operation there. */
2229 if (idx)
2231 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2232 && shader->u.ps.declared_in_count > in_count)
2234 sprintf(register_name,
2235 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2236 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2237 prefix, rel_param0.param_str, idx);
2239 else
2241 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2244 else
2246 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2247 && shader->u.ps.declared_in_count > in_count)
2249 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2250 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2251 prefix, rel_param0.param_str);
2253 else
2255 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2259 else
2261 if (idx == in_count) sprintf(register_name, "gl_Color");
2262 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2263 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2266 else
2268 if (!reg->idx[0].offset)
2269 strcpy(register_name, "ffp_varying_diffuse");
2270 else
2271 strcpy(register_name, "ffp_varying_specular");
2272 break;
2274 break;
2276 case WINED3DSPR_CONST:
2278 /* Relative addressing */
2279 if (reg->idx[0].rel_addr)
2281 if (wined3d_settings.check_float_constants)
2282 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2283 rel_param0.param_str, reg->idx[0].offset,
2284 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2285 prefix, rel_param0.param_str, reg->idx[0].offset);
2286 else if (reg->idx[0].offset)
2287 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2288 else
2289 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2291 else
2293 if (shader_constant_is_local(shader, reg->idx[0].offset))
2294 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2295 else
2296 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2299 break;
2301 case WINED3DSPR_CONSTINT:
2302 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2303 break;
2305 case WINED3DSPR_CONSTBOOL:
2306 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2307 break;
2309 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2310 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2311 sprintf(register_name, "T%u", reg->idx[0].offset);
2312 else
2313 sprintf(register_name, "A%u", reg->idx[0].offset);
2314 break;
2316 case WINED3DSPR_LOOP:
2317 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
2318 break;
2320 case WINED3DSPR_SAMPLER:
2321 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2322 break;
2324 case WINED3DSPR_COLOROUT:
2325 if (reg->idx[0].offset >= gl_info->limits.buffers)
2326 WARN("Write to render target %u, only %d supported.\n",
2327 reg->idx[0].offset, gl_info->limits.buffers);
2329 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2330 break;
2332 case WINED3DSPR_RASTOUT:
2333 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2334 break;
2336 case WINED3DSPR_DEPTHOUT:
2337 sprintf(register_name, "gl_FragDepth");
2338 break;
2340 case WINED3DSPR_ATTROUT:
2341 if (!reg->idx[0].offset)
2342 sprintf(register_name, "%s_out[8]", prefix);
2343 else
2344 sprintf(register_name, "%s_out[9]", prefix);
2345 break;
2347 case WINED3DSPR_TEXCRDOUT:
2348 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2349 if (reg->idx[0].rel_addr)
2350 FIXME("VS3 output registers relative addressing.\n");
2351 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2352 break;
2354 case WINED3DSPR_MISCTYPE:
2355 if (!reg->idx[0].offset)
2357 /* vPos */
2358 sprintf(register_name, "vpos");
2360 else if (reg->idx[0].offset == 1)
2362 /* Note that gl_FrontFacing is a bool, while vFace is
2363 * a float for which the sign determines front/back */
2364 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2366 else
2368 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2369 sprintf(register_name, "unrecognized_register");
2371 break;
2373 case WINED3DSPR_IMMCONST:
2374 switch (reg->immconst_type)
2376 case WINED3D_IMMCONST_SCALAR:
2377 switch (reg->data_type)
2379 case WINED3D_DATA_FLOAT:
2380 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
2381 break;
2382 case WINED3D_DATA_INT:
2383 sprintf(register_name, "%#x", reg->immconst_data[0]);
2384 break;
2385 case WINED3D_DATA_RESOURCE:
2386 case WINED3D_DATA_SAMPLER:
2387 case WINED3D_DATA_UINT:
2388 sprintf(register_name, "%#xu", reg->immconst_data[0]);
2389 break;
2390 default:
2391 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2392 break;
2394 break;
2396 case WINED3D_IMMCONST_VEC4:
2397 switch (reg->data_type)
2399 case WINED3D_DATA_FLOAT:
2400 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
2401 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
2402 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
2403 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
2404 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2405 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2406 break;
2407 case WINED3D_DATA_INT:
2408 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2409 reg->immconst_data[0], reg->immconst_data[1],
2410 reg->immconst_data[2], reg->immconst_data[3]);
2411 break;
2412 case WINED3D_DATA_RESOURCE:
2413 case WINED3D_DATA_SAMPLER:
2414 case WINED3D_DATA_UINT:
2415 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2416 reg->immconst_data[0], reg->immconst_data[1],
2417 reg->immconst_data[2], reg->immconst_data[3]);
2418 break;
2419 default:
2420 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2421 break;
2423 break;
2425 default:
2426 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2427 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2429 break;
2431 case WINED3DSPR_CONSTBUFFER:
2432 if (reg->idx[1].rel_addr)
2433 sprintf(register_name, "%s_cb%u[%s + %u]",
2434 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2435 else
2436 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2437 break;
2439 case WINED3DSPR_IMMCONSTBUFFER:
2440 if (reg->idx[0].rel_addr)
2441 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2442 else
2443 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2444 break;
2446 case WINED3DSPR_PRIMID:
2447 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2448 break;
2450 default:
2451 FIXME("Unhandled register type %#x.\n", reg->type);
2452 sprintf(register_name, "unrecognized_register");
2453 break;
2457 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2459 *str++ = '.';
2460 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2461 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2462 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2463 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2464 *str = '\0';
2467 /* Get the GLSL write mask for the destination register */
2468 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2470 DWORD mask = param->write_mask;
2472 if (shader_is_scalar(&param->reg))
2474 mask = WINED3DSP_WRITEMASK_0;
2475 *write_mask = '\0';
2477 else
2479 shader_glsl_write_mask_to_str(mask, write_mask);
2482 return mask;
2485 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
2486 unsigned int size = 0;
2488 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2489 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2490 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2491 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2493 return size;
2496 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2498 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2499 * but addressed as "rgba". To fix this we need to swap the register's x
2500 * and z components. */
2501 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2503 *str++ = '.';
2504 /* swizzle bits fields: wwzzyyxx */
2505 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
2506 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
2507 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
2508 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
2509 *str = '\0';
2512 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2513 BOOL fixup, DWORD mask, char *swizzle_str)
2515 if (shader_is_scalar(&param->reg))
2516 *swizzle_str = '\0';
2517 else
2518 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2521 /* From a given parameter token, generate the corresponding GLSL string.
2522 * Also, return the actual register name and swizzle in case the
2523 * caller needs this information as well. */
2524 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2525 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
2527 BOOL is_color = FALSE;
2528 char swizzle_str[6];
2530 glsl_src->reg_name[0] = '\0';
2531 glsl_src->param_str[0] = '\0';
2532 swizzle_str[0] = '\0';
2534 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
2535 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
2537 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
2539 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
2541 else
2543 char reg_name[200];
2545 switch (wined3d_src->reg.data_type)
2547 case WINED3D_DATA_FLOAT:
2548 sprintf(reg_name, "%s", glsl_src->reg_name);
2549 break;
2550 case WINED3D_DATA_INT:
2551 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
2552 break;
2553 case WINED3D_DATA_RESOURCE:
2554 case WINED3D_DATA_SAMPLER:
2555 case WINED3D_DATA_UINT:
2556 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
2557 break;
2558 default:
2559 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
2560 sprintf(reg_name, "%s", glsl_src->reg_name);
2561 break;
2564 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
2568 /* From a given parameter token, generate the corresponding GLSL string.
2569 * Also, return the actual register name and swizzle in case the
2570 * caller needs this information as well. */
2571 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
2572 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
2574 BOOL is_color = FALSE;
2576 glsl_dst->mask_str[0] = '\0';
2577 glsl_dst->reg_name[0] = '\0';
2579 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
2580 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
2583 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2584 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
2585 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
2586 enum wined3d_data_type data_type)
2588 struct glsl_dst_param glsl_dst;
2589 DWORD mask;
2591 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
2593 switch (data_type)
2595 case WINED3D_DATA_FLOAT:
2596 shader_addline(buffer, "%s%s = %s(",
2597 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2598 break;
2599 case WINED3D_DATA_INT:
2600 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
2601 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2602 break;
2603 case WINED3D_DATA_RESOURCE:
2604 case WINED3D_DATA_SAMPLER:
2605 case WINED3D_DATA_UINT:
2606 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
2607 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2608 break;
2609 default:
2610 FIXME("Unhandled data type %#x.\n", data_type);
2611 shader_addline(buffer, "%s%s = %s(",
2612 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2613 break;
2617 return mask;
2620 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2621 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
2623 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2626 /** Process GLSL instruction modifiers */
2627 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
2629 struct glsl_dst_param dst_param;
2630 DWORD modifiers;
2632 if (!ins->dst_count) return;
2634 modifiers = ins->dst[0].modifiers;
2635 if (!modifiers) return;
2637 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2639 if (modifiers & WINED3DSPDM_SATURATE)
2641 /* _SAT means to clamp the value of the register to between 0 and 1 */
2642 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
2643 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
2646 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
2648 FIXME("_centroid modifier not handled\n");
2651 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
2653 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2657 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
2659 switch (op)
2661 case WINED3D_SHADER_REL_OP_GT: return ">";
2662 case WINED3D_SHADER_REL_OP_EQ: return "==";
2663 case WINED3D_SHADER_REL_OP_GE: return ">=";
2664 case WINED3D_SHADER_REL_OP_LT: return "<";
2665 case WINED3D_SHADER_REL_OP_NE: return "!=";
2666 case WINED3D_SHADER_REL_OP_LE: return "<=";
2667 default:
2668 FIXME("Unrecognized operator %#x.\n", op);
2669 return "(\?\?)";
2673 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
2674 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
2676 static const struct
2678 unsigned int coord_size;
2679 unsigned int offset_size;
2680 const char *type_part;
2682 resource_types[] =
2684 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
2685 {1, 0, ""}, /* WINED3D_SHADER_RESOURCE_BUFFER */
2686 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
2687 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
2688 {2, 0, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
2689 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
2690 {3, 0, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
2691 {2, 1, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
2692 {3, 2, "2DArray"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
2693 {3, 0, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
2695 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2696 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
2697 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2698 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
2699 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
2700 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
2701 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
2702 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2703 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
2704 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
2705 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
2706 const char *base = "texture", *type_part = "", *suffix = "";
2707 unsigned int coord_size;
2709 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
2711 if (resource_type >= ARRAY_SIZE(resource_types))
2713 ERR("Unexpected resource type %#x.\n", resource_type);
2714 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
2717 /* Note that there's no such thing as a projected cube texture. */
2718 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
2719 projected = FALSE;
2721 if (needs_legacy_glsl_syntax(gl_info))
2723 if (shadow)
2724 base = "shadow";
2726 type_part = resource_types[resource_type].type_part;
2727 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
2728 type_part = "2DRect";
2729 if (!type_part[0])
2730 FIXME("Unhandled resource type %#x.\n", resource_type);
2732 if (!lod && grad && !gl_info->supported[EXT_GPU_SHADER4])
2734 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2735 suffix = "ARB";
2736 else
2737 FIXME("Unsupported grad function.\n");
2741 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
2743 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
2744 if (flags & ~texel_fetch_flags)
2745 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
2747 base = "texelFetch";
2748 type_part = "";
2751 sample_function->offset_size = offset ? resource_types[resource_type].offset_size : 0;
2752 if (offset && !sample_function->offset_size)
2754 FIXME("Offset not supported for resource type %#x.\n", resource_type);
2755 offset = FALSE;
2758 sample_function->name = string_buffer_get(priv->string_buffers);
2759 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
2760 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
2762 coord_size = resource_types[resource_type].coord_size;
2763 if (shadow)
2764 ++coord_size;
2765 sample_function->coord_mask = (1u << coord_size) - 1;
2766 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
2769 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
2770 struct glsl_sample_function *sample_function)
2772 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2774 string_buffer_release(priv->string_buffers, sample_function->name);
2777 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2778 BOOL sign_fixup, enum fixup_channel_source channel_source)
2780 switch(channel_source)
2782 case CHANNEL_SOURCE_ZERO:
2783 strcat(arguments, "0.0");
2784 break;
2786 case CHANNEL_SOURCE_ONE:
2787 strcat(arguments, "1.0");
2788 break;
2790 case CHANNEL_SOURCE_X:
2791 strcat(arguments, reg_name);
2792 strcat(arguments, ".x");
2793 break;
2795 case CHANNEL_SOURCE_Y:
2796 strcat(arguments, reg_name);
2797 strcat(arguments, ".y");
2798 break;
2800 case CHANNEL_SOURCE_Z:
2801 strcat(arguments, reg_name);
2802 strcat(arguments, ".z");
2803 break;
2805 case CHANNEL_SOURCE_W:
2806 strcat(arguments, reg_name);
2807 strcat(arguments, ".w");
2808 break;
2810 default:
2811 FIXME("Unhandled channel source %#x\n", channel_source);
2812 strcat(arguments, "undefined");
2813 break;
2816 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2819 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
2820 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2822 unsigned int mask_size, remaining;
2823 DWORD fixup_mask = 0;
2824 char arguments[256];
2825 char mask_str[6];
2827 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2828 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2829 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2830 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2831 if (!(mask &= fixup_mask))
2832 return;
2834 if (is_complex_fixup(fixup))
2836 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2837 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2838 return;
2841 shader_glsl_write_mask_to_str(mask, mask_str);
2842 mask_size = shader_glsl_get_write_mask_size(mask);
2844 arguments[0] = '\0';
2845 remaining = mask_size;
2846 if (mask & WINED3DSP_WRITEMASK_0)
2848 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2849 if (--remaining) strcat(arguments, ", ");
2851 if (mask & WINED3DSP_WRITEMASK_1)
2853 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2854 if (--remaining) strcat(arguments, ", ");
2856 if (mask & WINED3DSP_WRITEMASK_2)
2858 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2859 if (--remaining) strcat(arguments, ", ");
2861 if (mask & WINED3DSP_WRITEMASK_3)
2863 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2864 if (--remaining) strcat(arguments, ", ");
2867 if (mask_size > 1)
2868 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2869 else
2870 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2873 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2875 char reg_name[256];
2876 BOOL is_color;
2878 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2879 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2882 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2883 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
2884 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
2885 const char *coord_reg_fmt, ...)
2887 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2888 char dst_swizzle[6];
2889 struct color_fixup_desc fixup;
2890 BOOL np2_fixup = FALSE;
2891 va_list args;
2892 int ret;
2894 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2896 /* If ARB_texture_swizzle is supported we don't need to do anything here.
2897 * We actually rely on it for vertex shaders and SM4+. */
2898 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
2900 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2901 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
2903 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
2904 np2_fixup = TRUE;
2906 else
2908 fixup = COLOR_FIXUP_IDENTITY;
2911 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2913 if (sample_function->output_single_component)
2914 shader_addline(ins->ctx->buffer, "vec4(");
2916 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2917 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
2919 for (;;)
2921 va_start(args, coord_reg_fmt);
2922 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2923 va_end(args);
2924 if (!ret)
2925 break;
2926 if (!string_buffer_resize(ins->ctx->buffer, ret))
2927 break;
2930 if (np2_fixup)
2932 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2933 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
2935 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
2937 case 1:
2938 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2939 idx >> 1, (idx % 2) ? "z" : "x");
2940 break;
2941 case 2:
2942 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2943 idx >> 1, (idx % 2) ? "zw" : "xy");
2944 break;
2945 case 3:
2946 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
2947 idx >> 1, (idx % 2) ? "zw" : "xy");
2948 break;
2949 case 4:
2950 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
2951 idx >> 1, (idx % 2) ? "zw" : "xy");
2952 break;
2955 if (dx && dy)
2956 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
2957 else if (bias)
2958 shader_addline(ins->ctx->buffer, ", %s", bias);
2959 if (sample_function->offset_size)
2961 int offset_immdata[4] = {offset->u, offset->v, offset->w};
2962 shader_addline(ins->ctx->buffer, ", ");
2963 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
2965 shader_addline(ins->ctx->buffer, ")");
2967 if (sample_function->output_single_component)
2968 shader_addline(ins->ctx->buffer, ")");
2970 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
2972 if (!is_identity_fixup(fixup))
2973 shader_glsl_color_correction(ins, fixup);
2976 /*****************************************************************************
2977 * Begin processing individual instruction opcodes
2978 ****************************************************************************/
2980 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2982 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2983 struct glsl_src_param src0_param;
2984 struct glsl_src_param src1_param;
2985 DWORD write_mask;
2986 const char *op;
2988 /* Determine the GLSL operator to use based on the opcode */
2989 switch (ins->handler_idx)
2991 case WINED3DSIH_ADD: op = "+"; break;
2992 case WINED3DSIH_AND: op = "&"; break;
2993 case WINED3DSIH_DIV: op = "/"; break;
2994 case WINED3DSIH_IADD: op = "+"; break;
2995 case WINED3DSIH_ISHL: op = "<<"; break;
2996 case WINED3DSIH_MUL: op = "*"; break;
2997 case WINED3DSIH_OR: op = "|"; break;
2998 case WINED3DSIH_SUB: op = "-"; break;
2999 case WINED3DSIH_USHR: op = ">>"; break;
3000 case WINED3DSIH_XOR: op = "^"; break;
3001 default:
3002 op = "<unhandled operator>";
3003 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3004 break;
3007 write_mask = shader_glsl_append_dst(buffer, ins);
3008 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3009 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3010 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3013 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3015 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3016 struct glsl_src_param src0_param;
3017 struct glsl_src_param src1_param;
3018 unsigned int mask_size;
3019 DWORD write_mask;
3020 const char *op;
3022 write_mask = shader_glsl_append_dst(buffer, ins);
3023 mask_size = shader_glsl_get_write_mask_size(write_mask);
3024 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3025 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3027 if (mask_size > 1)
3029 switch (ins->handler_idx)
3031 case WINED3DSIH_EQ: op = "equal"; break;
3032 case WINED3DSIH_IEQ: op = "equal"; break;
3033 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3034 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3035 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3036 case WINED3DSIH_LT: op = "lessThan"; break;
3037 case WINED3DSIH_ILT: op = "lessThan"; break;
3038 case WINED3DSIH_ULT: op = "lessThan"; break;
3039 case WINED3DSIH_NE: op = "notEqual"; break;
3040 case WINED3DSIH_INE: op = "notEqual"; break;
3041 default:
3042 op = "<unhandled operator>";
3043 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3044 break;
3047 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3048 mask_size, op, src0_param.param_str, src1_param.param_str);
3050 else
3052 switch (ins->handler_idx)
3054 case WINED3DSIH_EQ: op = "=="; break;
3055 case WINED3DSIH_IEQ: op = "=="; break;
3056 case WINED3DSIH_GE: op = ">="; break;
3057 case WINED3DSIH_IGE: op = ">="; break;
3058 case WINED3DSIH_UGE: op = ">="; break;
3059 case WINED3DSIH_LT: op = "<"; break;
3060 case WINED3DSIH_ILT: op = "<"; break;
3061 case WINED3DSIH_ULT: op = "<"; break;
3062 case WINED3DSIH_NE: op = "!="; break;
3063 case WINED3DSIH_INE: op = "!="; break;
3064 default:
3065 op = "<unhandled operator>";
3066 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3067 break;
3070 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3071 src0_param.param_str, op, src1_param.param_str);
3075 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3077 struct glsl_src_param src_param;
3078 DWORD write_mask;
3079 const char *op;
3081 switch (ins->handler_idx)
3083 case WINED3DSIH_INEG: op = "-"; break;
3084 case WINED3DSIH_NOT: op = "~"; break;
3085 default:
3086 op = "<unhandled operator>";
3087 ERR("Unhandled opcode %s.\n",
3088 debug_d3dshaderinstructionhandler(ins->handler_idx));
3089 break;
3092 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3093 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3094 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3097 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
3099 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3100 struct glsl_src_param src0_param;
3101 struct glsl_src_param src1_param;
3102 DWORD write_mask;
3104 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
3105 * not, we can emulate it. */
3106 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3107 FIXME("64-bit integer multiplies not implemented.\n");
3109 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3111 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3112 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3113 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3115 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3116 src0_param.param_str, src1_param.param_str);
3120 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3122 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3123 struct glsl_src_param src0_param, src1_param;
3124 DWORD write_mask;
3126 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3128 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3130 char dst_mask[6];
3132 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3133 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3134 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3135 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3136 dst_mask, src0_param.param_str, src1_param.param_str);
3138 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3139 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3140 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3141 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3143 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3144 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3146 else
3148 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3149 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3150 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3151 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3154 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3156 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3157 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3158 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3159 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3163 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3164 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3166 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3167 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3168 struct glsl_src_param src0_param;
3169 DWORD write_mask;
3171 write_mask = shader_glsl_append_dst(buffer, ins);
3172 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3174 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3175 * shader versions WINED3DSIO_MOVA is used for this. */
3176 if (ins->ctx->reg_maps->shader_version.major == 1
3177 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3178 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3180 /* This is a simple floor() */
3181 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3182 if (mask_size > 1) {
3183 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3184 } else {
3185 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3188 else if(ins->handler_idx == WINED3DSIH_MOVA)
3190 /* We need to *round* to the nearest int here. */
3191 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3193 if (gl_info->supported[EXT_GPU_SHADER4])
3195 if (mask_size > 1)
3196 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3197 else
3198 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3200 else
3202 if (mask_size > 1)
3203 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3204 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3205 else
3206 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3207 src0_param.param_str, src0_param.param_str);
3210 else
3212 shader_addline(buffer, "%s);\n", src0_param.param_str);
3216 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3217 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3219 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3220 struct glsl_src_param src0_param;
3221 struct glsl_src_param src1_param;
3222 DWORD dst_write_mask, src_write_mask;
3223 unsigned int dst_size;
3225 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3226 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3228 /* dp4 works on vec4, dp3 on vec3, etc. */
3229 if (ins->handler_idx == WINED3DSIH_DP4)
3230 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3231 else if (ins->handler_idx == WINED3DSIH_DP3)
3232 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3233 else
3234 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3236 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3237 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3239 if (dst_size > 1) {
3240 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3241 } else {
3242 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3246 /* Note that this instruction has some restrictions. The destination write mask
3247 * can't contain the w component, and the source swizzles have to be .xyzw */
3248 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3250 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3251 struct glsl_src_param src0_param;
3252 struct glsl_src_param src1_param;
3253 char dst_mask[6];
3255 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3256 shader_glsl_append_dst(ins->ctx->buffer, ins);
3257 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3258 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3259 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3262 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3264 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3267 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3268 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3269 * GLSL uses the value as-is. */
3270 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3272 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3273 struct glsl_src_param src0_param;
3274 struct glsl_src_param src1_param;
3275 DWORD dst_write_mask;
3276 unsigned int dst_size;
3278 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3279 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3281 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3282 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3284 if (dst_size > 1)
3286 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3287 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3289 else
3291 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3292 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3296 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3297 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3299 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3300 struct glsl_src_param src_param;
3301 const char *instruction;
3302 DWORD write_mask;
3303 unsigned i;
3305 /* Determine the GLSL function to use based on the opcode */
3306 /* TODO: Possibly make this a table for faster lookups */
3307 switch (ins->handler_idx)
3309 case WINED3DSIH_ABS: instruction = "abs"; break;
3310 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3311 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3312 case WINED3DSIH_FRC: instruction = "fract"; break;
3313 case WINED3DSIH_IMAX: instruction = "max"; break;
3314 case WINED3DSIH_IMIN: instruction = "min"; break;
3315 case WINED3DSIH_MAX: instruction = "max"; break;
3316 case WINED3DSIH_MIN: instruction = "min"; break;
3317 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
3318 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3319 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
3320 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
3321 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3322 default: instruction = "";
3323 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3324 break;
3327 write_mask = shader_glsl_append_dst(buffer, ins);
3329 shader_addline(buffer, "%s(", instruction);
3331 if (ins->src_count)
3333 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3334 shader_addline(buffer, "%s", src_param.param_str);
3335 for (i = 1; i < ins->src_count; ++i)
3337 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3338 shader_addline(buffer, ", %s", src_param.param_str);
3342 shader_addline(buffer, "));\n");
3345 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3347 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3349 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3350 struct glsl_src_param src_param;
3351 unsigned int mask_size;
3352 DWORD write_mask;
3353 char dst_mask[6];
3355 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3356 mask_size = shader_glsl_get_write_mask_size(write_mask);
3357 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3359 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3360 src_param.param_str, src_param.param_str);
3361 shader_glsl_append_dst(buffer, ins);
3363 if (mask_size > 1)
3365 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3366 mask_size, src_param.param_str);
3368 else
3370 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3371 src_param.param_str);
3375 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3377 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3378 ins->ctx->reg_maps->shader_version.minor);
3379 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3380 struct glsl_src_param src0_param;
3381 const char *prefix, *suffix;
3382 unsigned int dst_size;
3383 DWORD dst_write_mask;
3385 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3386 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3388 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
3389 dst_write_mask = WINED3DSP_WRITEMASK_3;
3391 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
3393 switch (ins->handler_idx)
3395 case WINED3DSIH_EXP:
3396 case WINED3DSIH_EXPP:
3397 prefix = "exp2(";
3398 suffix = ")";
3399 break;
3401 case WINED3DSIH_LOG:
3402 case WINED3DSIH_LOGP:
3403 prefix = "log2(abs(";
3404 suffix = "))";
3405 break;
3407 case WINED3DSIH_RCP:
3408 prefix = "1.0 / ";
3409 suffix = "";
3410 break;
3412 case WINED3DSIH_RSQ:
3413 prefix = "inversesqrt(abs(";
3414 suffix = "))";
3415 break;
3417 default:
3418 prefix = "";
3419 suffix = "";
3420 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3421 break;
3424 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
3425 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3426 else
3427 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3430 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3431 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3432 * dst.x = 2^(floor(src))
3433 * dst.y = src - floor(src)
3434 * dst.z = 2^src (partial precision is allowed, but optional)
3435 * dst.w = 1.0;
3436 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3437 * dst = 2^src; (partial precision is allowed, but optional)
3439 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
3441 if (ins->ctx->reg_maps->shader_version.major < 2)
3443 struct glsl_src_param src_param;
3444 char dst_mask[6];
3446 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
3448 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
3449 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
3450 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
3451 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
3453 shader_glsl_append_dst(ins->ctx->buffer, ins);
3454 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3455 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
3456 return;
3459 shader_glsl_scalar_op(ins);
3462 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
3463 const char *vector_constructor, const char *scalar_constructor)
3465 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3466 struct glsl_src_param src_param;
3467 unsigned int mask_size;
3468 DWORD write_mask;
3470 write_mask = shader_glsl_append_dst(buffer, ins);
3471 mask_size = shader_glsl_get_write_mask_size(write_mask);
3472 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3474 if (mask_size > 1)
3475 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
3476 else
3477 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
3480 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
3482 shader_glsl_cast(ins, "ivec", "int");
3485 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
3487 shader_glsl_cast(ins, "uvec", "uint");
3490 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
3492 shader_glsl_cast(ins, "vec", "float");
3495 /** Process signed comparison opcodes in GLSL. */
3496 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
3498 struct glsl_src_param src0_param;
3499 struct glsl_src_param src1_param;
3500 DWORD write_mask;
3501 unsigned int mask_size;
3503 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3504 mask_size = shader_glsl_get_write_mask_size(write_mask);
3505 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3506 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3508 if (mask_size > 1) {
3509 const char *compare;
3511 switch(ins->handler_idx)
3513 case WINED3DSIH_SLT: compare = "lessThan"; break;
3514 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
3515 default: compare = "";
3516 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3519 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
3520 src0_param.param_str, src1_param.param_str);
3521 } else {
3522 switch(ins->handler_idx)
3524 case WINED3DSIH_SLT:
3525 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3526 * to return 0.0 but step returns 1.0 because step is not < x
3527 * An alternative is a bvec compare padded with an unused second component.
3528 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3529 * issue. Playing with not() is not possible either because not() does not accept
3530 * a scalar.
3532 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
3533 src0_param.param_str, src1_param.param_str);
3534 break;
3535 case WINED3DSIH_SGE:
3536 /* Here we can use the step() function and safe a conditional */
3537 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
3538 break;
3539 default:
3540 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3546 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
3548 const char *condition_prefix, *condition_suffix;
3549 struct wined3d_shader_dst_param dst;
3550 struct glsl_src_param src0_param;
3551 struct glsl_src_param src1_param;
3552 struct glsl_src_param src2_param;
3553 BOOL temp_destination = FALSE;
3554 DWORD cmp_channel = 0;
3555 unsigned int i, j;
3556 char mask_char[6];
3557 DWORD write_mask;
3559 switch (ins->handler_idx)
3561 case WINED3DSIH_CMP:
3562 condition_prefix = "";
3563 condition_suffix = " >= 0.0";
3564 break;
3566 case WINED3DSIH_CND:
3567 condition_prefix = "";
3568 condition_suffix = " > 0.5";
3569 break;
3571 case WINED3DSIH_MOVC:
3572 condition_prefix = "bool(";
3573 condition_suffix = ")";
3574 break;
3576 default:
3577 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3578 condition_prefix = "<unhandled prefix>";
3579 condition_suffix = "<unhandled suffix>";
3580 break;
3583 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
3585 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3586 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3587 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3588 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3590 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3591 condition_prefix, src0_param.param_str, condition_suffix,
3592 src1_param.param_str, src2_param.param_str);
3593 return;
3596 dst = ins->dst[0];
3598 /* Splitting the instruction up in multiple lines imposes a problem:
3599 * The first lines may overwrite source parameters of the following lines.
3600 * Deal with that by using a temporary destination register if needed. */
3601 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
3602 && ins->src[0].reg.type == dst.reg.type)
3603 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
3604 && ins->src[1].reg.type == dst.reg.type)
3605 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
3606 && ins->src[2].reg.type == dst.reg.type))
3607 temp_destination = TRUE;
3609 /* Cycle through all source0 channels. */
3610 for (i = 0; i < 4; ++i)
3612 write_mask = 0;
3613 /* Find the destination channels which use the current source0 channel. */
3614 for (j = 0; j < 4; ++j)
3616 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
3618 write_mask |= WINED3DSP_WRITEMASK_0 << j;
3619 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
3622 dst.write_mask = ins->dst[0].write_mask & write_mask;
3624 if (temp_destination)
3626 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
3627 continue;
3628 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
3630 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
3631 continue;
3633 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
3634 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3635 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3637 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3638 condition_prefix, src0_param.param_str, condition_suffix,
3639 src1_param.param_str, src2_param.param_str);
3642 if (temp_destination)
3644 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3645 shader_glsl_append_dst(ins->ctx->buffer, ins);
3646 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3650 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3651 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3652 * the compare is done per component of src0. */
3653 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3655 struct glsl_src_param src0_param;
3656 struct glsl_src_param src1_param;
3657 struct glsl_src_param src2_param;
3658 DWORD write_mask;
3659 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3660 ins->ctx->reg_maps->shader_version.minor);
3662 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3664 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3665 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3666 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3667 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3669 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
3670 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3671 else
3672 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3673 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3674 return;
3677 shader_glsl_conditional_move(ins);
3680 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3681 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3683 struct glsl_src_param src0_param;
3684 struct glsl_src_param src1_param;
3685 struct glsl_src_param src2_param;
3686 DWORD write_mask;
3688 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3689 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3690 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3691 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3692 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3693 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3696 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3697 Vertex shaders to GLSL codes */
3698 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3700 int i;
3701 int nComponents = 0;
3702 struct wined3d_shader_dst_param tmp_dst = {{0}};
3703 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3704 struct wined3d_shader_instruction tmp_ins;
3706 memset(&tmp_ins, 0, sizeof(tmp_ins));
3708 /* Set constants for the temporary argument */
3709 tmp_ins.ctx = ins->ctx;
3710 tmp_ins.dst_count = 1;
3711 tmp_ins.dst = &tmp_dst;
3712 tmp_ins.src_count = 2;
3713 tmp_ins.src = tmp_src;
3715 switch(ins->handler_idx)
3717 case WINED3DSIH_M4x4:
3718 nComponents = 4;
3719 tmp_ins.handler_idx = WINED3DSIH_DP4;
3720 break;
3721 case WINED3DSIH_M4x3:
3722 nComponents = 3;
3723 tmp_ins.handler_idx = WINED3DSIH_DP4;
3724 break;
3725 case WINED3DSIH_M3x4:
3726 nComponents = 4;
3727 tmp_ins.handler_idx = WINED3DSIH_DP3;
3728 break;
3729 case WINED3DSIH_M3x3:
3730 nComponents = 3;
3731 tmp_ins.handler_idx = WINED3DSIH_DP3;
3732 break;
3733 case WINED3DSIH_M3x2:
3734 nComponents = 2;
3735 tmp_ins.handler_idx = WINED3DSIH_DP3;
3736 break;
3737 default:
3738 break;
3741 tmp_dst = ins->dst[0];
3742 tmp_src[0] = ins->src[0];
3743 tmp_src[1] = ins->src[1];
3744 for (i = 0; i < nComponents; ++i)
3746 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3747 shader_glsl_dot(&tmp_ins);
3748 ++tmp_src[1].reg.idx[0].offset;
3753 The LRP instruction performs a component-wise linear interpolation
3754 between the second and third operands using the first operand as the
3755 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3756 This is equivalent to mix(src2, src1, src0);
3758 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3760 struct glsl_src_param src0_param;
3761 struct glsl_src_param src1_param;
3762 struct glsl_src_param src2_param;
3763 DWORD write_mask;
3765 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3767 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3768 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3769 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3771 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3772 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3775 /** Process the WINED3DSIO_LIT instruction in GLSL:
3776 * dst.x = dst.w = 1.0
3777 * dst.y = (src0.x > 0) ? src0.x
3778 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3779 * where src.w is clamped at +- 128
3781 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3783 struct glsl_src_param src0_param;
3784 struct glsl_src_param src1_param;
3785 struct glsl_src_param src3_param;
3786 char dst_mask[6];
3788 shader_glsl_append_dst(ins->ctx->buffer, ins);
3789 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3791 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3792 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3793 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3795 /* The sdk specifies the instruction like this
3796 * dst.x = 1.0;
3797 * if(src.x > 0.0) dst.y = src.x
3798 * else dst.y = 0.0.
3799 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3800 * else dst.z = 0.0;
3801 * dst.w = 1.0;
3802 * (where power = src.w clamped between -128 and 128)
3804 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3805 * dst.x = 1.0 ... No further explanation needed
3806 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3807 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3808 * dst.w = 1.0. ... Nothing fancy.
3810 * So we still have one conditional in there. So do this:
3811 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3813 * 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),
3814 * 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.
3815 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3817 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3818 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3819 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3821 shader_addline(ins->ctx->buffer,
3822 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3823 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3824 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3825 src0_param.param_str, src3_param.param_str, dst_mask);
3828 /** Process the WINED3DSIO_DST instruction in GLSL:
3829 * dst.x = 1.0
3830 * dst.y = src0.x * src0.y
3831 * dst.z = src0.z
3832 * dst.w = src1.w
3834 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3836 struct glsl_src_param src0y_param;
3837 struct glsl_src_param src0z_param;
3838 struct glsl_src_param src1y_param;
3839 struct glsl_src_param src1w_param;
3840 char dst_mask[6];
3842 shader_glsl_append_dst(ins->ctx->buffer, ins);
3843 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3845 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3846 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3847 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3848 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3850 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3851 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3854 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3855 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3856 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3858 * dst.x = cos(src0.?)
3859 * dst.y = sin(src0.?)
3860 * dst.z = dst.z
3861 * dst.w = dst.w
3863 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3865 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3866 struct glsl_src_param src0_param;
3867 DWORD write_mask;
3869 if (ins->ctx->reg_maps->shader_version.major < 4)
3871 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3873 write_mask = shader_glsl_append_dst(buffer, ins);
3874 switch (write_mask)
3876 case WINED3DSP_WRITEMASK_0:
3877 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3878 break;
3880 case WINED3DSP_WRITEMASK_1:
3881 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3882 break;
3884 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3885 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3886 src0_param.param_str, src0_param.param_str);
3887 break;
3889 default:
3890 ERR("Write mask should be .x, .y or .xy\n");
3891 break;
3894 return;
3897 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3900 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3902 char dst_mask[6];
3904 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3905 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3906 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3908 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3909 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3910 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3912 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3913 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3915 else
3917 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3918 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3919 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3922 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3924 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3925 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3926 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3930 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3931 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3932 * generate invalid code
3934 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3936 struct glsl_src_param src0_param;
3937 DWORD write_mask;
3939 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3940 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3942 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3945 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3946 * Start a for() loop where src1.y is the initial value of aL,
3947 * increment aL by src1.z for a total of src1.x iterations.
3948 * Need to use a temporary variable for this operation.
3950 /* FIXME: I don't think nested loops will work correctly this way. */
3951 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3953 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3954 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3955 const struct wined3d_shader *shader = ins->ctx->shader;
3956 const struct wined3d_shader_lconst *constant;
3957 struct glsl_src_param src1_param;
3958 const DWORD *control_values = NULL;
3960 if (ins->ctx->reg_maps->shader_version.major < 4)
3962 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3964 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3965 * class hardware doesn't support real varying indexing, but Microsoft
3966 * designed this feature for Shader model 2.x+. If the loop control is
3967 * known at compile time, the GLSL compiler can unroll the loop, and
3968 * replace indirect addressing with direct addressing. */
3969 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3971 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3973 if (constant->idx == ins->src[1].reg.idx[0].offset)
3975 control_values = constant->value;
3976 break;
3981 if (control_values)
3983 struct wined3d_shader_loop_control loop_control;
3984 loop_control.count = control_values[0];
3985 loop_control.start = control_values[1];
3986 loop_control.step = (int)control_values[2];
3988 if (loop_control.step > 0)
3990 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3991 loop_state->current_depth, loop_control.start,
3992 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3993 loop_state->current_depth, loop_control.step);
3995 else if (loop_control.step < 0)
3997 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3998 loop_state->current_depth, loop_control.start,
3999 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
4000 loop_state->current_depth, loop_control.step);
4002 else
4004 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4005 loop_state->current_depth, loop_control.start, loop_state->current_depth,
4006 loop_state->current_depth, loop_control.count,
4007 loop_state->current_depth);
4010 else
4012 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4013 loop_state->current_depth, loop_state->current_reg,
4014 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
4015 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
4018 ++loop_state->current_reg;
4020 else
4022 shader_addline(buffer, "for (;;)\n{\n");
4025 ++loop_state->current_depth;
4028 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4030 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4032 shader_addline(ins->ctx->buffer, "}\n");
4034 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4036 --loop_state->current_depth;
4037 --loop_state->current_reg;
4040 if (ins->handler_idx == WINED3DSIH_ENDREP)
4042 --loop_state->current_depth;
4046 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4048 const struct wined3d_shader *shader = ins->ctx->shader;
4049 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4050 const struct wined3d_shader_lconst *constant;
4051 struct glsl_src_param src0_param;
4052 const DWORD *control_values = NULL;
4054 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4055 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4057 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4059 if (constant->idx == ins->src[0].reg.idx[0].offset)
4061 control_values = constant->value;
4062 break;
4067 if (control_values)
4069 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4070 loop_state->current_depth, loop_state->current_depth,
4071 control_values[0], loop_state->current_depth);
4073 else
4075 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4076 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4077 loop_state->current_depth, loop_state->current_depth,
4078 src0_param.param_str, loop_state->current_depth);
4081 ++loop_state->current_depth;
4084 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
4086 struct glsl_src_param src0_param;
4088 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4089 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
4092 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
4094 struct glsl_src_param src0_param;
4095 struct glsl_src_param src1_param;
4097 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4098 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4100 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
4101 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4104 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
4106 shader_addline(ins->ctx->buffer, "} else {\n");
4109 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
4111 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
4112 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
4115 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
4117 shader_addline(ins->ctx->buffer, "break;\n");
4120 /* FIXME: According to MSDN the compare is done per component. */
4121 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
4123 struct glsl_src_param src0_param;
4124 struct glsl_src_param src1_param;
4126 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4127 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4129 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
4130 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4133 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
4135 struct glsl_src_param src_param;
4137 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4138 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
4141 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
4143 shader_addline(ins->ctx->buffer, "}\n");
4144 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
4147 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
4149 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
4152 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
4154 struct glsl_src_param src1_param;
4156 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4157 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
4158 src1_param.param_str, ins->src[0].reg.idx[0].offset);
4161 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
4163 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
4164 * function only suppresses the unhandled instruction warning
4168 /*********************************************
4169 * Pixel Shader Specific Code begins here
4170 ********************************************/
4171 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
4173 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4174 ins->ctx->reg_maps->shader_version.minor);
4175 struct glsl_sample_function sample_function;
4176 DWORD sample_flags = 0;
4177 DWORD resource_idx;
4178 DWORD mask = 0, swizzle;
4179 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4181 /* 1.0-1.4: Use destination register as sampler source.
4182 * 2.0+: Use provided sampler source. */
4183 if (shader_version < WINED3D_SHADER_VERSION(2,0))
4184 resource_idx = ins->dst[0].reg.idx[0].offset;
4185 else
4186 resource_idx = ins->src[1].reg.idx[0].offset;
4188 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4190 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4191 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4192 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4194 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4195 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4197 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4198 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4200 case WINED3D_TTFF_COUNT1:
4201 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4202 break;
4203 case WINED3D_TTFF_COUNT2:
4204 mask = WINED3DSP_WRITEMASK_1;
4205 break;
4206 case WINED3D_TTFF_COUNT3:
4207 mask = WINED3DSP_WRITEMASK_2;
4208 break;
4209 case WINED3D_TTFF_COUNT4:
4210 case WINED3D_TTFF_DISABLE:
4211 mask = WINED3DSP_WRITEMASK_3;
4212 break;
4216 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4218 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4220 if (src_mod == WINED3DSPSM_DZ) {
4221 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4222 mask = WINED3DSP_WRITEMASK_2;
4223 } else if (src_mod == WINED3DSPSM_DW) {
4224 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4225 mask = WINED3DSP_WRITEMASK_3;
4228 else
4230 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4231 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4233 /* ps 2.0 texldp instruction always divides by the fourth component. */
4234 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4235 mask = WINED3DSP_WRITEMASK_3;
4239 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
4240 mask |= sample_function.coord_mask;
4241 sample_function.coord_mask = mask;
4243 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4244 else swizzle = ins->src[1].swizzle;
4246 /* 1.0-1.3: Use destination register as coordinate source.
4247 1.4+: Use provided coordinate source register. */
4248 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4250 char coord_mask[6];
4251 shader_glsl_write_mask_to_str(mask, coord_mask);
4252 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4253 "T%u%s", resource_idx, coord_mask);
4255 else
4257 struct glsl_src_param coord_param;
4258 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4259 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4261 struct glsl_src_param bias;
4262 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4263 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4264 NULL, "%s", coord_param.param_str);
4265 } else {
4266 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4267 "%s", coord_param.param_str);
4270 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4273 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4275 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4276 struct glsl_src_param coord_param, dx_param, dy_param;
4277 struct glsl_sample_function sample_function;
4278 DWORD sampler_idx;
4279 DWORD swizzle = ins->src[1].swizzle;
4281 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
4283 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4284 shader_glsl_tex(ins);
4285 return;
4288 sampler_idx = ins->src[1].reg.idx[0].offset;
4290 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
4291 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4292 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
4293 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
4295 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
4296 NULL, NULL, "%s", coord_param.param_str);
4297 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4300 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4302 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4303 struct glsl_src_param coord_param, lod_param;
4304 struct glsl_sample_function sample_function;
4305 DWORD sampler_idx;
4306 DWORD swizzle = ins->src[1].swizzle;
4308 sampler_idx = ins->src[1].reg.idx[0].offset;
4310 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
4311 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4313 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4315 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
4316 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
4318 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4319 * However, the NVIDIA drivers allow them in fragment shaders as well,
4320 * even without the appropriate extension. */
4321 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4323 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
4324 "%s", coord_param.param_str);
4325 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4328 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4329 unsigned int resource_idx, unsigned int sampler_idx)
4331 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4332 unsigned int i;
4334 for (i = 0; i < sampler_map->count; ++i)
4336 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4337 return entries[i].bind_idx;
4340 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4342 return ~0u;
4345 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
4347 static const unsigned int texture_size_component_count[] =
4349 0, /* WINED3D_SHADER_RESOURCE_NONE */
4350 1, /* WINED3D_SHADER_RESOURCE_BUFFER */
4351 1, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
4352 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
4353 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
4354 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
4355 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
4356 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
4357 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
4358 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
4361 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
4362 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4363 enum wined3d_shader_resource_type resource_type;
4364 unsigned int resource_idx, sampler_bind_idx, i;
4365 enum wined3d_data_type dst_data_type;
4366 struct glsl_src_param lod_param;
4367 char dst_swizzle[6];
4368 DWORD write_mask;
4370 dst_data_type = ins->dst[0].reg.data_type;
4371 if (ins->flags == WINED3DSI_RESINFO_UINT)
4372 dst_data_type = WINED3D_DATA_UINT;
4373 else if (ins->flags)
4374 FIXME("Unhandled flags %#x.\n", ins->flags);
4376 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
4377 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
4379 resource_idx = ins->src[1].reg.idx[0].offset;
4380 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4381 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
4382 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4383 resource_idx, WINED3D_SAMPLER_DEFAULT);
4385 if (resource_type >= ARRAY_SIZE(texture_size_component_count))
4387 ERR("Unexpected resource type %#x.\n", resource_type);
4388 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
4391 if (dst_data_type == WINED3D_DATA_UINT)
4392 shader_addline(ins->ctx->buffer, "uvec4(");
4393 else
4394 shader_addline(ins->ctx->buffer, "vec4(");
4396 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
4397 shader_glsl_get_prefix(version->type), sampler_bind_idx, lod_param.param_str);
4399 for (i = 0; i < 3 - texture_size_component_count[resource_type]; ++i)
4400 shader_addline(ins->ctx->buffer, "0, ");
4402 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
4404 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
4405 shader_glsl_get_prefix(version->type), sampler_bind_idx);
4407 else
4409 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
4410 shader_addline(ins->ctx->buffer, "1");
4413 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
4416 /* FIXME: The current implementation does not handle multisample textures correctly. */
4417 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
4419 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4420 struct glsl_src_param coord_param, lod_param;
4421 struct glsl_sample_function sample_function;
4422 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
4424 if (wined3d_shader_instruction_has_texel_offset(ins))
4425 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4427 resource_idx = ins->src[1].reg.idx[0].offset;
4428 sampler_idx = WINED3D_SAMPLER_DEFAULT;
4430 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4431 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4432 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4433 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4434 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
4435 NULL, NULL, lod_param.param_str, &ins->texel_offset, "%s", coord_param.param_str);
4436 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4439 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
4441 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
4442 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
4443 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4444 struct glsl_sample_function sample_function;
4445 DWORD flags = 0;
4447 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
4448 flags |= WINED3D_GLSL_SAMPLE_GRAD;
4449 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
4450 flags |= WINED3D_GLSL_SAMPLE_LOD;
4451 if (wined3d_shader_instruction_has_texel_offset(ins))
4452 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4454 resource_idx = ins->src[1].reg.idx[0].offset;
4455 sampler_idx = ins->src[2].reg.idx[0].offset;
4457 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4458 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4460 switch (ins->handler_idx)
4462 case WINED3DSIH_SAMPLE:
4463 break;
4464 case WINED3DSIH_SAMPLE_B:
4465 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4466 lod_param_str = lod_param.param_str;
4467 break;
4468 case WINED3DSIH_SAMPLE_GRAD:
4469 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dx_param);
4470 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.coord_mask, &dy_param);
4471 dx_param_str = dx_param.param_str;
4472 dy_param_str = dy_param.param_str;
4473 break;
4474 case WINED3DSIH_SAMPLE_LOD:
4475 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4476 lod_param_str = lod_param.param_str;
4477 break;
4478 default:
4479 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4480 break;
4483 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4484 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
4485 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
4486 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4489 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
4491 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4492 struct glsl_src_param coord_param, compare_param;
4493 struct glsl_sample_function sample_function;
4494 const char *lod_param = NULL;
4495 DWORD flags = 0;
4496 UINT coord_size;
4498 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
4500 lod_param = "0";
4501 flags |= WINED3D_GLSL_SAMPLE_LOD;
4504 if (wined3d_shader_instruction_has_texel_offset(ins))
4505 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4507 resource_idx = ins->src[1].reg.idx[0].offset;
4508 sampler_idx = ins->src[2].reg.idx[0].offset;
4510 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4511 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4512 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
4513 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
4514 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4515 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
4516 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
4517 coord_size, coord_param.param_str, compare_param.param_str);
4518 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4521 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
4523 /* FIXME: Make this work for more than just 2D textures */
4524 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4525 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4527 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
4529 char dst_mask[6];
4531 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4532 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
4533 ins->dst[0].reg.idx[0].offset, dst_mask);
4535 else
4537 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4538 DWORD reg = ins->src[0].reg.idx[0].offset;
4539 char dst_swizzle[6];
4541 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
4543 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
4545 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4546 struct glsl_src_param div_param;
4547 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
4549 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
4551 if (mask_size > 1)
4552 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4553 else
4554 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4556 else
4558 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
4563 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4564 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4565 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4566 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
4568 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4569 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4570 struct glsl_sample_function sample_function;
4571 struct glsl_src_param src0_param;
4572 UINT mask_size;
4574 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4576 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4577 * scalar, and projected sampling would require 4.
4579 * It is a dependent read - not valid with conditional NP2 textures
4581 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4582 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4584 switch(mask_size)
4586 case 1:
4587 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4588 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
4589 break;
4591 case 2:
4592 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4593 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
4594 break;
4596 case 3:
4597 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4598 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
4599 break;
4601 default:
4602 FIXME("Unexpected mask size %u\n", mask_size);
4603 break;
4605 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4608 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4609 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4610 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
4612 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4613 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4614 struct glsl_src_param src0_param;
4615 DWORD dst_mask;
4616 unsigned int mask_size;
4618 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4619 mask_size = shader_glsl_get_write_mask_size(dst_mask);
4620 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4622 if (mask_size > 1) {
4623 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
4624 } else {
4625 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
4629 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4630 * Calculate the depth as dst.x / dst.y */
4631 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
4633 struct glsl_dst_param dst_param;
4635 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4637 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4638 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4639 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4640 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4641 * >= 1.0 or < 0.0
4643 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4644 dst_param.reg_name, dst_param.reg_name);
4647 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4648 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4649 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4650 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4652 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
4654 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4655 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4656 struct glsl_src_param src0_param;
4658 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4660 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
4661 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4664 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4665 * Calculate the 1st of a 2-row matrix multiplication. */
4666 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
4668 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4669 DWORD reg = ins->dst[0].reg.idx[0].offset;
4670 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4671 struct glsl_src_param src0_param;
4673 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4674 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4677 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4678 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4679 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
4681 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4682 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4683 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4684 DWORD reg = ins->dst[0].reg.idx[0].offset;
4685 struct glsl_src_param src0_param;
4687 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4688 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
4689 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
4692 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
4694 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4695 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4696 struct glsl_sample_function sample_function;
4697 DWORD reg = ins->dst[0].reg.idx[0].offset;
4698 struct glsl_src_param src0_param;
4700 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4701 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4703 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4705 /* Sample the texture using the calculated coordinates */
4706 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
4707 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4710 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
4711 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
4712 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
4714 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4715 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4716 struct glsl_sample_function sample_function;
4717 DWORD reg = ins->dst[0].reg.idx[0].offset;
4718 struct glsl_src_param src0_param;
4720 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4721 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4723 /* Dependent read, not valid with conditional NP2 */
4724 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4726 /* Sample the texture using the calculated coordinates */
4727 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
4728 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4730 tex_mx->current_row = 0;
4733 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4734 * Perform the 3rd row of a 3x3 matrix multiply */
4735 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
4737 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4738 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4739 DWORD reg = ins->dst[0].reg.idx[0].offset;
4740 struct glsl_src_param src0_param;
4741 char dst_mask[6];
4743 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4745 shader_glsl_append_dst(ins->ctx->buffer, ins);
4746 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4747 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
4749 tex_mx->current_row = 0;
4752 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4753 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4754 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
4756 struct glsl_src_param src0_param;
4757 struct glsl_src_param src1_param;
4758 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4759 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4760 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4761 struct glsl_sample_function sample_function;
4762 DWORD reg = ins->dst[0].reg.idx[0].offset;
4763 char coord_mask[6];
4765 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4766 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4768 /* Perform the last matrix multiply operation */
4769 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4770 /* Reflection calculation */
4771 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
4773 /* Dependent read, not valid with conditional NP2 */
4774 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4775 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4777 /* Sample the texture */
4778 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4779 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
4780 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4782 tex_mx->current_row = 0;
4785 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4786 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4787 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
4789 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4790 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4791 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4792 struct glsl_sample_function sample_function;
4793 DWORD reg = ins->dst[0].reg.idx[0].offset;
4794 struct glsl_src_param src0_param;
4795 char coord_mask[6];
4797 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4799 /* Perform the last matrix multiply operation */
4800 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
4802 /* Construct the eye-ray vector from w coordinates */
4803 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
4804 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
4805 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4807 /* Dependent read, not valid with conditional NP2 */
4808 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4809 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4811 /* Sample the texture using the calculated coordinates */
4812 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4813 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
4814 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4816 tex_mx->current_row = 0;
4819 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4820 * Apply a fake bump map transform.
4821 * texbem is pshader <= 1.3 only, this saves a few version checks
4823 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
4825 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4826 struct glsl_sample_function sample_function;
4827 struct glsl_src_param coord_param;
4828 DWORD sampler_idx;
4829 DWORD mask;
4830 DWORD flags;
4831 char coord_mask[6];
4833 sampler_idx = ins->dst[0].reg.idx[0].offset;
4834 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4835 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4837 /* Dependent read, not valid with conditional NP2 */
4838 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4839 mask = sample_function.coord_mask;
4841 shader_glsl_write_mask_to_str(mask, coord_mask);
4843 /* With projected textures, texbem only divides the static texture coord,
4844 * not the displacement, so we can't let GL handle this. */
4845 if (flags & WINED3D_PSARGS_PROJECTED)
4847 DWORD div_mask=0;
4848 char coord_div_mask[3];
4849 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4851 case WINED3D_TTFF_COUNT1:
4852 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4853 break;
4854 case WINED3D_TTFF_COUNT2:
4855 div_mask = WINED3DSP_WRITEMASK_1;
4856 break;
4857 case WINED3D_TTFF_COUNT3:
4858 div_mask = WINED3DSP_WRITEMASK_2;
4859 break;
4860 case WINED3D_TTFF_COUNT4:
4861 case WINED3D_TTFF_DISABLE:
4862 div_mask = WINED3DSP_WRITEMASK_3;
4863 break;
4865 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4866 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4869 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4871 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4872 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4873 coord_param.param_str, coord_mask);
4875 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4877 struct glsl_src_param luminance_param;
4878 struct glsl_dst_param dst_param;
4880 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4881 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4883 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4884 dst_param.reg_name, dst_param.mask_str,
4885 luminance_param.param_str, sampler_idx, sampler_idx);
4887 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4890 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4892 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4893 struct glsl_src_param src0_param, src1_param;
4895 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4896 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4898 shader_glsl_append_dst(ins->ctx->buffer, ins);
4899 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4900 src0_param.param_str, sampler_idx, src1_param.param_str);
4903 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4904 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4905 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4907 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4908 struct glsl_sample_function sample_function;
4909 struct glsl_src_param src0_param;
4911 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4913 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4914 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4915 "%s.wx", src0_param.reg_name);
4916 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4919 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4920 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4921 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4923 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4924 struct glsl_sample_function sample_function;
4925 struct glsl_src_param src0_param;
4927 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4929 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4930 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4931 "%s.yz", src0_param.reg_name);
4932 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4935 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4936 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4937 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4939 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4940 struct glsl_sample_function sample_function;
4941 struct glsl_src_param src0_param;
4943 /* Dependent read, not valid with conditional NP2 */
4944 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4945 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4947 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4948 "%s", src0_param.param_str);
4949 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4952 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4953 * If any of the first 3 components are < 0, discard this pixel */
4954 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4956 if (ins->ctx->reg_maps->shader_version.major >= 4)
4958 struct glsl_src_param src_param;
4960 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4961 shader_addline(ins->ctx->buffer, "if (bool(floatBitsToUint(%s))) discard;\n", src_param.param_str);
4963 else
4965 struct glsl_dst_param dst_param;
4967 /* The argument is a destination parameter, and no writemasks are allowed */
4968 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4970 /* 2.0 shaders compare all 4 components in texkill. */
4971 if (ins->ctx->reg_maps->shader_version.major >= 2)
4972 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4973 /* 1.x shaders only compare the first 3 components, probably due to
4974 * the nature of the texkill instruction as a tex* instruction, and
4975 * phase, which kills all .w components. Even if all 4 components are
4976 * defined, only the first 3 are used. */
4977 else
4978 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4982 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4983 * dst = dot2(src0, src1) + src2 */
4984 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4986 struct glsl_src_param src0_param;
4987 struct glsl_src_param src1_param;
4988 struct glsl_src_param src2_param;
4989 DWORD write_mask;
4990 unsigned int mask_size;
4992 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4993 mask_size = shader_glsl_get_write_mask_size(write_mask);
4995 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4996 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4997 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4999 if (mask_size > 1) {
5000 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
5001 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
5002 } else {
5003 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
5004 src0_param.param_str, src1_param.param_str, src2_param.param_str);
5008 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
5009 const struct wined3d_shader_signature *input_signature,
5010 const struct wined3d_shader_reg_maps *reg_maps,
5011 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
5013 unsigned int i;
5015 for (i = 0; i < input_signature->element_count; ++i)
5017 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5018 const char *semantic_name;
5019 UINT semantic_idx;
5020 char reg_mask[6];
5022 /* Unused */
5023 if (!(reg_maps->input_registers & (1u << input->register_idx)))
5024 continue;
5026 semantic_name = input->semantic_name;
5027 semantic_idx = input->semantic_idx;
5028 shader_glsl_write_mask_to_str(input->mask, reg_mask);
5030 if (args->vp_mode == vertexshader)
5032 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
5033 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
5034 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5035 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5036 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
5037 else
5038 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
5039 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5040 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
5042 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5044 if (args->pointsprite)
5045 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
5046 shader->u.ps.input_reg_map[input->register_idx]);
5047 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
5048 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
5049 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5050 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
5051 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
5052 else
5053 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5054 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5056 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
5058 if (!semantic_idx)
5059 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
5060 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5061 else if (semantic_idx == 1)
5062 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
5063 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5064 else
5065 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5066 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5068 else
5070 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5071 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5076 /*********************************************
5077 * Vertex Shader Specific Code begins here
5078 ********************************************/
5080 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
5082 struct glsl_program_key key;
5084 key.vs_id = entry->vs.id;
5085 key.gs_id = entry->gs.id;
5086 key.ps_id = entry->ps.id;
5088 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
5090 ERR("Failed to insert program entry.\n");
5094 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
5095 GLuint vs_id, GLuint gs_id, GLuint ps_id)
5097 struct wine_rb_entry *entry;
5098 struct glsl_program_key key;
5100 key.vs_id = vs_id;
5101 key.gs_id = gs_id;
5102 key.ps_id = ps_id;
5104 entry = wine_rb_get(&priv->program_lookup, &key);
5105 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
5108 /* Context activation is done by the caller. */
5109 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
5110 struct glsl_shader_prog_link *entry)
5112 struct glsl_program_key key;
5114 key.vs_id = entry->vs.id;
5115 key.gs_id = entry->gs.id;
5116 key.ps_id = entry->ps.id;
5117 wine_rb_remove(&priv->program_lookup, &key);
5119 GL_EXTCALL(glDeleteProgram(entry->id));
5120 if (entry->vs.id)
5121 list_remove(&entry->vs.shader_entry);
5122 if (entry->gs.id)
5123 list_remove(&entry->gs.shader_entry);
5124 if (entry->ps.id)
5125 list_remove(&entry->ps.shader_entry);
5126 HeapFree(GetProcessHeap(), 0, entry);
5129 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
5130 const struct wined3d_gl_info *gl_info, const DWORD *map,
5131 const struct wined3d_shader_signature *input_signature,
5132 const struct wined3d_shader_reg_maps *reg_maps_in,
5133 const struct wined3d_shader_signature *output_signature,
5134 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
5136 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5137 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5138 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5139 unsigned int in_count = vec4_varyings(3, gl_info);
5140 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
5141 DWORD in_idx, *set = NULL;
5142 unsigned int i, j;
5143 char reg_mask[6];
5145 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * max_varyings);
5147 for (i = 0; i < input_signature->element_count; ++i)
5149 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5151 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
5152 continue;
5154 in_idx = map[input->register_idx];
5155 /* Declared, but not read register */
5156 if (in_idx == ~0u)
5157 continue;
5158 if (in_idx >= max_varyings)
5160 FIXME("More input varyings declared than supported, expect issues.\n");
5161 continue;
5164 if (in_idx == in_count)
5165 string_buffer_sprintf(destination, "gl_FrontColor");
5166 else if (in_idx == in_count + 1)
5167 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5168 else
5169 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
5171 if (!set[in_idx])
5172 set[in_idx] = ~0u;
5174 for (j = 0; j < output_signature->element_count; ++j)
5176 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
5177 DWORD mask;
5179 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
5180 || input->semantic_idx != output->semantic_idx
5181 || strcmp(input->semantic_name, output->semantic_name)
5182 || !(mask = input->mask & output->mask))
5183 continue;
5185 if (set[in_idx] == ~0u)
5186 set[in_idx] = 0;
5187 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
5188 shader_glsl_write_mask_to_str(mask, reg_mask);
5190 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
5191 destination->buffer, reg_mask, output->register_idx, reg_mask);
5195 for (i = 0; i < max_varyings; ++i)
5197 unsigned int size;
5199 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
5200 continue;
5202 if (set[i] == ~0u)
5203 set[i] = 0;
5205 size = 0;
5206 if (!(set[i] & WINED3DSP_WRITEMASK_0))
5207 reg_mask[size++] = 'x';
5208 if (!(set[i] & WINED3DSP_WRITEMASK_1))
5209 reg_mask[size++] = 'y';
5210 if (!(set[i] & WINED3DSP_WRITEMASK_2))
5211 reg_mask[size++] = 'z';
5212 if (!(set[i] & WINED3DSP_WRITEMASK_3))
5213 reg_mask[size++] = 'w';
5214 reg_mask[size] = '\0';
5216 if (i == in_count)
5217 string_buffer_sprintf(destination, "gl_FrontColor");
5218 else if (i == in_count + 1)
5219 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5220 else
5221 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
5223 if (size == 1)
5224 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
5225 else
5226 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
5229 HeapFree(GetProcessHeap(), 0, set);
5230 string_buffer_release(&priv->string_buffers, destination);
5233 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
5234 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
5235 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
5237 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5238 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5239 char reg_mask[6];
5240 unsigned int i;
5242 for (i = 0; i < output_signature->element_count; ++i)
5244 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
5246 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
5247 continue;
5249 if (output->register_idx >= input_count)
5250 continue;
5252 string_buffer_sprintf(destination, "%s[%u]", out_array_name, output->register_idx);
5254 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5256 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
5257 destination->buffer, reg_mask, output->register_idx, reg_mask);
5260 string_buffer_release(&priv->string_buffers, destination);
5263 /* Context activation is done by the caller. */
5264 static void shader_glsl_generate_vs_gs_setup(struct shader_glsl_priv *priv,
5265 const struct wined3d_shader *vs, unsigned int input_count,
5266 const struct wined3d_gl_info *gl_info)
5268 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5270 shader_addline(buffer, "varying out vec4 gs_in[%u];\n", input_count);
5271 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
5273 shader_glsl_setup_sm4_shader_output(priv, input_count, &vs->output_signature, &vs->reg_maps, "gs_in");
5275 shader_addline(buffer, "}\n");
5278 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
5279 const struct wined3d_gl_info *gl_info, const DWORD *map,
5280 const struct wined3d_shader_signature *input_signature,
5281 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
5282 const struct wined3d_shader_signature *output_signature,
5283 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
5285 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5286 const char *semantic_name;
5287 UINT semantic_idx;
5288 char reg_mask[6];
5289 unsigned int i;
5291 /* First, sort out position and point size system values. */
5292 for (i = 0; i < output_signature->element_count; ++i)
5294 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
5296 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
5297 continue;
5299 semantic_name = output->semantic_name;
5300 semantic_idx = output->semantic_idx;
5301 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5303 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
5305 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
5306 reg_mask, output->register_idx, reg_mask);
5308 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5310 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
5311 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
5315 /* Then, setup the pixel shader input. */
5316 if (reg_maps_out->shader_version.major < 4)
5317 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
5318 output_signature, reg_maps_out, "ps_link");
5319 else
5320 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "ps_link");
5323 /* Context activation is done by the caller. */
5324 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
5325 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
5326 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
5328 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5329 GLuint ret;
5330 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
5331 unsigned int i;
5332 const char *semantic_name;
5333 UINT semantic_idx;
5334 char reg_mask[6];
5335 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5337 string_buffer_clear(buffer);
5339 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &vs->reg_maps.shader_version));
5341 if (per_vertex_point_size)
5343 shader_addline(buffer, "uniform struct\n{\n");
5344 shader_addline(buffer, " float size_min;\n");
5345 shader_addline(buffer, " float size_max;\n");
5346 shader_addline(buffer, "} ffp_point;\n");
5349 if (ps_major < 3)
5351 DWORD colors_written_mask[2] = {0};
5352 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
5354 if (!legacy_context)
5356 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
5357 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
5358 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
5359 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
5362 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
5364 for (i = 0; i < vs->output_signature.element_count; ++i)
5366 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5367 DWORD write_mask;
5369 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5370 continue;
5372 semantic_name = output->semantic_name;
5373 semantic_idx = output->semantic_idx;
5374 write_mask = output->mask;
5375 shader_glsl_write_mask_to_str(write_mask, reg_mask);
5377 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
5379 if (legacy_context)
5380 shader_addline(buffer, "gl_Front%sColor%s = shader_out[%u]%s;\n",
5381 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
5382 else
5383 shader_addline(buffer, "ffp_varying_%s%s = clamp(shader_out[%u]%s, 0.0, 1.0);\n",
5384 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
5386 colors_written_mask[semantic_idx] = write_mask;
5388 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5390 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
5391 reg_mask, output->register_idx, reg_mask);
5393 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5395 if (semantic_idx < MAX_TEXTURES)
5397 shader_addline(buffer, "%s[%u]%s = shader_out[%u]%s;\n",
5398 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
5399 semantic_idx, reg_mask, output->register_idx, reg_mask);
5400 texcoords_written_mask[semantic_idx] = write_mask;
5403 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5405 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
5406 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
5408 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
5410 shader_addline(buffer, "%s = clamp(shader_out[%u].%c, 0.0, 1.0);\n",
5411 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
5412 output->register_idx, reg_mask[1]);
5416 for (i = 0; i < 2; ++i)
5418 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5420 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5421 if (!i)
5422 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
5423 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
5424 reg_mask, reg_mask);
5425 else
5426 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
5427 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
5428 reg_mask, reg_mask);
5431 for (i = 0; i < MAX_TEXTURES; ++i)
5433 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
5434 continue;
5436 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5438 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
5439 && !texcoords_written_mask[i])
5440 continue;
5442 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5443 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
5444 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
5448 else
5450 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
5452 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
5453 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
5454 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
5455 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
5458 shader_addline(buffer, "}\n");
5460 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5461 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
5462 shader_glsl_compile(gl_info, ret, buffer->buffer);
5464 return ret;
5467 static void shader_glsl_generate_sm4_rasterizer_input_setup(struct shader_glsl_priv *priv,
5468 const struct wined3d_shader *shader, unsigned int input_count,
5469 const struct wined3d_gl_info *gl_info)
5471 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5473 if (input_count)
5474 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", min(vec4_varyings(4, gl_info), input_count));
5476 shader_addline(buffer, "void setup_%s_output(in vec4 shader_out[%u])\n{\n",
5477 shader_glsl_get_prefix(shader->reg_maps.shader_version.type), shader->limits->packed_output);
5479 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
5480 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
5482 shader_addline(buffer, "}\n");
5485 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
5486 const struct wined3d_gl_info *gl_info)
5488 const char *output = get_fragment_output(gl_info);
5490 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
5491 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
5492 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
5493 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
5494 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
5495 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
5498 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
5499 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
5501 const char *output = get_fragment_output(gl_info);
5503 switch (mode)
5505 case WINED3D_FFP_PS_FOG_OFF:
5506 return;
5508 case WINED3D_FFP_PS_FOG_LINEAR:
5509 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
5510 break;
5512 case WINED3D_FFP_PS_FOG_EXP:
5513 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
5514 break;
5516 case WINED3D_FFP_PS_FOG_EXP2:
5517 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
5518 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
5519 break;
5521 default:
5522 ERR("Invalid fog mode %#x.\n", mode);
5523 return;
5526 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
5527 output, output);
5530 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
5531 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
5533 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
5534 * flipping all the operators here, just negate the comparison below. */
5535 static const char * const comparison_operator[] =
5537 "", /* WINED3D_CMP_NEVER */
5538 "<", /* WINED3D_CMP_LESS */
5539 "==", /* WINED3D_CMP_EQUAL */
5540 "<=", /* WINED3D_CMP_LESSEQUAL */
5541 ">", /* WINED3D_CMP_GREATER */
5542 "!=", /* WINED3D_CMP_NOTEQUAL */
5543 ">=", /* WINED3D_CMP_GREATEREQUAL */
5544 "" /* WINED3D_CMP_ALWAYS */
5547 if (alpha_func == WINED3D_CMP_ALWAYS)
5548 return;
5550 if (alpha_func != WINED3D_CMP_NEVER)
5551 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
5552 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
5553 shader_addline(buffer, " discard;\n");
5556 /* Context activation is done by the caller. */
5557 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
5558 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5559 const struct wined3d_shader *shader,
5560 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
5562 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5563 const struct wined3d_gl_info *gl_info = context->gl_info;
5564 const DWORD *function = shader->function;
5565 struct shader_glsl_ctx_priv priv_ctx;
5566 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5568 /* Create the hw GLSL shader object and assign it as the shader->prgId */
5569 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5571 memset(&priv_ctx, 0, sizeof(priv_ctx));
5572 priv_ctx.cur_ps_args = args;
5573 priv_ctx.cur_np2fixup_info = np2fixup_info;
5574 priv_ctx.string_buffers = string_buffers;
5576 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5578 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5579 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5580 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5581 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
5582 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5583 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5584 /* The spec says that it doesn't have to be explicitly enabled, but the
5585 * nvidia drivers write a warning if we don't do so. */
5586 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5587 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5588 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5589 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5590 if (gl_info->supported[EXT_GPU_SHADER4])
5591 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5592 if (gl_info->supported[EXT_TEXTURE_ARRAY])
5593 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
5595 /* Base Declarations */
5596 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5598 shader_addline(buffer, "void main()\n{\n");
5600 /* Direct3D applications expect integer vPos values, while OpenGL drivers
5601 * add approximately 0.5. This causes off-by-one problems as spotted by
5602 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
5603 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
5604 * causes precision troubles when we just subtract 0.5.
5606 * To deal with that, just floor() the position. This will eliminate the
5607 * fraction on all cards.
5609 * TODO: Test how this behaves with multisampling.
5611 * An advantage of floor is that it works even if the driver doesn't add
5612 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
5613 * to return in gl_FragCoord, even though coordinates specify the pixel
5614 * centers instead of the pixel corners. This code will behave correctly
5615 * on drivers that returns integer values. */
5616 if (reg_maps->vpos)
5618 if (shader->device->wined3d->flags & WINED3D_PIXEL_CENTER_INTEGER)
5619 shader_addline(buffer,
5620 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
5621 else
5622 shader_addline(buffer,
5623 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
5626 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
5628 unsigned int i;
5629 WORD map = reg_maps->texcoord;
5631 if (legacy_context)
5633 if (glsl_is_color_reg_read(shader, 0))
5634 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
5635 if (glsl_is_color_reg_read(shader, 1))
5636 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
5639 for (i = 0; map; map >>= 1, ++i)
5641 if (map & 1)
5643 if (args->pointsprite)
5644 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
5645 else if (args->texcoords_initialized & (1u << i))
5646 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
5647 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
5648 else
5649 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
5650 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
5654 if (legacy_context)
5655 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
5658 /* Pack 3.0 inputs */
5659 if (reg_maps->shader_version.major >= 3)
5660 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
5662 /* Base Shader Body */
5663 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5665 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
5666 if (reg_maps->shader_version.major < 2)
5667 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
5669 if (args->srgb_correction)
5670 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
5672 /* SM < 3 does not replace the fog stage. */
5673 if (reg_maps->shader_version.major < 3)
5674 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
5676 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
5678 shader_addline(buffer, "}\n");
5680 TRACE("Compiling shader object %u.\n", shader_id);
5681 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5683 return shader_id;
5686 /* Context activation is done by the caller. */
5687 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
5688 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
5690 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
5691 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5692 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5693 const struct wined3d_gl_info *gl_info = context->gl_info;
5694 const DWORD *function = shader->function;
5695 struct shader_glsl_ctx_priv priv_ctx;
5696 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5698 /* Create the hw GLSL shader program and assign it as the shader->prgId */
5699 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5701 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5703 if (gl_info->supported[ARB_DRAW_INSTANCED])
5704 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
5705 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5706 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5707 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5708 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5709 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5710 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5711 if (gl_info->supported[EXT_GPU_SHADER4])
5712 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5713 if (gl_info->supported[EXT_TEXTURE_ARRAY])
5714 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
5716 memset(&priv_ctx, 0, sizeof(priv_ctx));
5717 priv_ctx.cur_vs_args = args;
5718 priv_ctx.string_buffers = string_buffers;
5720 /* Base Declarations */
5721 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5723 if (reg_maps->shader_version.major >= 4)
5725 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL)
5726 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->next_shader_input_count, gl_info);
5727 else if (args->next_shader_type == WINED3D_SHADER_TYPE_GEOMETRY)
5728 shader_glsl_generate_vs_gs_setup(priv, shader, args->next_shader_input_count, gl_info);
5731 shader_addline(buffer, "void main()\n{\n");
5733 /* Base Shader Body */
5734 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5736 /* Unpack outputs */
5737 shader_addline(buffer, "setup_vs_output(vs_out);\n");
5739 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
5740 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
5741 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
5742 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
5744 if (reg_maps->shader_version.major < 3)
5746 if (args->fog_src == VS_FOG_Z)
5747 shader_addline(buffer, "%s = gl_Position.z;\n",
5748 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5749 else if (!reg_maps->fog)
5750 shader_addline(buffer, "%s = 0.0;\n",
5751 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5754 /* We always store the clipplanes without y inversion */
5755 if (args->clip_enabled)
5756 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
5758 if (args->point_size && !args->per_vertex_point_size)
5759 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
5761 /* Write the final position.
5763 * OpenGL coordinates specify the center of the pixel while d3d coords specify
5764 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
5765 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
5766 * contains 1.0 to allow a mad.
5768 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
5769 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
5771 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
5773 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
5774 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
5775 * which is the same as z = z * 2 - w.
5777 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
5779 shader_addline(buffer, "}\n");
5781 TRACE("Compiling shader object %u.\n", shader_id);
5782 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5784 return shader_id;
5787 /* Context activation is done by the caller. */
5788 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
5789 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
5791 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
5792 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5793 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5794 const struct wined3d_gl_info *gl_info = context->gl_info;
5795 const DWORD *function = shader->function;
5796 struct shader_glsl_ctx_priv priv_ctx;
5797 GLuint shader_id;
5799 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
5801 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5803 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
5804 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
5805 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5806 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5807 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5808 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5809 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5810 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5811 if (gl_info->supported[EXT_GPU_SHADER4])
5812 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5813 if (gl_info->supported[EXT_TEXTURE_ARRAY])
5814 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
5816 memset(&priv_ctx, 0, sizeof(priv_ctx));
5817 priv_ctx.string_buffers = string_buffers;
5818 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5819 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->ps_input_count, gl_info);
5820 shader_addline(buffer, "void main()\n{\n");
5821 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5822 shader_addline(buffer, "}\n");
5824 TRACE("Compiling shader object %u.\n", shader_id);
5825 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5827 return shader_id;
5830 static GLuint find_glsl_pshader(const struct wined3d_context *context,
5831 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5832 struct wined3d_shader *shader,
5833 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
5835 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
5836 struct glsl_shader_private *shader_data;
5837 struct ps_np2fixup_info *np2fixup;
5838 UINT i;
5839 DWORD new_size;
5840 GLuint ret;
5842 if (!shader->backend_data)
5844 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5845 if (!shader->backend_data)
5847 ERR("Failed to allocate backend data.\n");
5848 return 0;
5851 shader_data = shader->backend_data;
5852 gl_shaders = shader_data->gl_shaders.ps;
5854 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5855 * so a linear search is more performant than a hashmap or a binary search
5856 * (cache coherency etc)
5858 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5860 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
5862 if (args->np2_fixup)
5863 *np2fixup_info = &gl_shaders[i].np2fixup;
5864 return gl_shaders[i].id;
5868 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5869 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5870 if (shader_data->num_gl_shaders)
5872 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5873 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
5874 new_size * sizeof(*gl_shaders));
5876 else
5878 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5879 new_size = 1;
5882 if(!new_array) {
5883 ERR("Out of memory\n");
5884 return 0;
5886 shader_data->gl_shaders.ps = new_array;
5887 shader_data->shader_array_size = new_size;
5888 gl_shaders = new_array;
5891 gl_shaders[shader_data->num_gl_shaders].args = *args;
5893 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
5894 memset(np2fixup, 0, sizeof(*np2fixup));
5895 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
5897 pixelshader_update_resource_types(shader, args->tex_types);
5899 string_buffer_clear(buffer);
5900 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
5901 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5903 return ret;
5906 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
5907 const DWORD use_map)
5909 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
5910 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
5911 if (stored->point_size != new->point_size)
5912 return FALSE;
5913 if (stored->per_vertex_point_size != new->per_vertex_point_size)
5914 return FALSE;
5915 if (stored->flatshading != new->flatshading)
5916 return FALSE;
5917 if (stored->next_shader_type != new->next_shader_type)
5918 return FALSE;
5919 if (stored->next_shader_input_count != new->next_shader_input_count)
5920 return FALSE;
5921 return stored->fog_src == new->fog_src;
5924 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
5925 struct wined3d_shader *shader, const struct vs_compile_args *args)
5927 UINT i;
5928 DWORD new_size;
5929 DWORD use_map = context->stream_info.use_map;
5930 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
5931 struct glsl_shader_private *shader_data;
5932 GLuint ret;
5934 if (!shader->backend_data)
5936 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5937 if (!shader->backend_data)
5939 ERR("Failed to allocate backend data.\n");
5940 return 0;
5943 shader_data = shader->backend_data;
5944 gl_shaders = shader_data->gl_shaders.vs;
5946 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5947 * so a linear search is more performant than a hashmap or a binary search
5948 * (cache coherency etc)
5950 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5952 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
5953 return gl_shaders[i].id;
5956 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5958 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5959 if (shader_data->num_gl_shaders)
5961 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5962 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
5963 new_size * sizeof(*gl_shaders));
5965 else
5967 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5968 new_size = 1;
5971 if(!new_array) {
5972 ERR("Out of memory\n");
5973 return 0;
5975 shader_data->gl_shaders.vs = new_array;
5976 shader_data->shader_array_size = new_size;
5977 gl_shaders = new_array;
5980 gl_shaders[shader_data->num_gl_shaders].args = *args;
5982 string_buffer_clear(&priv->shader_buffer);
5983 ret = shader_glsl_generate_vshader(context, priv, shader, args);
5984 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5986 return ret;
5989 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
5990 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
5992 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
5993 struct glsl_shader_private *shader_data;
5994 unsigned int i, new_size;
5995 GLuint ret;
5997 if (!shader->backend_data)
5999 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
6001 ERR("Failed to allocate backend data.\n");
6002 return 0;
6005 shader_data = shader->backend_data;
6006 gl_shaders = shader_data->gl_shaders.gs;
6008 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6010 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
6011 return gl_shaders[i].id;
6014 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6016 if (shader_data->num_gl_shaders)
6018 new_size = shader_data->shader_array_size + 1;
6019 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.gs,
6020 new_size * sizeof(*new_array));
6022 else
6024 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
6025 new_size = 1;
6028 if (!new_array)
6030 ERR("Failed to allocate GL shaders array.\n");
6031 return 0;
6033 shader_data->gl_shaders.gs = new_array;
6034 shader_data->shader_array_size = new_size;
6035 gl_shaders = new_array;
6037 string_buffer_clear(&priv->shader_buffer);
6038 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
6039 gl_shaders[shader_data->num_gl_shaders].args = *args;
6040 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6042 return ret;
6045 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
6047 switch (mcs)
6049 case WINED3D_MCS_MATERIAL:
6050 return material;
6051 case WINED3D_MCS_COLOR1:
6052 return "ffp_attrib_diffuse";
6053 case WINED3D_MCS_COLOR2:
6054 return "ffp_attrib_specular";
6055 default:
6056 ERR("Invalid material color source %#x.\n", mcs);
6057 return "<invalid>";
6061 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
6062 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
6064 const char *diffuse, *specular, *emissive, *ambient;
6065 enum wined3d_light_type light_type;
6066 unsigned int i;
6068 if (!settings->lighting)
6070 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
6071 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
6072 return;
6075 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
6076 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
6077 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
6078 shader_addline(buffer, "vec3 dir, dst;\n");
6079 shader_addline(buffer, "float att, t;\n");
6081 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
6082 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
6083 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
6084 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
6086 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
6088 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
6089 switch (light_type)
6091 case WINED3D_LIGHT_POINT:
6092 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
6093 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
6094 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
6095 shader_addline(buffer, "dst.x = 1.0;\n");
6096 if (legacy_lighting)
6098 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
6099 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
6101 else
6103 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
6105 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6106 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", i, i, i);
6107 if (!legacy_lighting)
6108 shader_addline(buffer, "att = 1.0 / att;\n");
6109 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
6110 if (!settings->normal)
6112 if (!legacy_lighting)
6113 shader_addline(buffer, "}\n");
6114 break;
6116 shader_addline(buffer, "dir = normalize(dir);\n");
6117 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
6118 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
6119 if (settings->localviewer)
6120 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6121 else
6122 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6123 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
6124 " * ffp_light[%u].specular) * att;\n", i);
6125 if (!legacy_lighting)
6126 shader_addline(buffer, "}\n");
6127 break;
6129 case WINED3D_LIGHT_SPOT:
6130 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
6131 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
6132 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
6133 shader_addline(buffer, "dst.x = 1.0;\n");
6134 if (legacy_lighting)
6136 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
6137 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
6139 else
6141 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
6143 shader_addline(buffer, "dir = normalize(dir);\n");
6144 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", i);
6145 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", i);
6146 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", i);
6147 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
6148 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
6149 i, i, i, i);
6150 if (legacy_lighting)
6151 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6152 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
6153 i, i, i);
6154 else
6155 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6156 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
6157 i, i, i);
6158 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
6159 if (!settings->normal)
6161 if (!legacy_lighting)
6162 shader_addline(buffer, "}\n");
6163 break;
6165 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
6166 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
6167 if (settings->localviewer)
6168 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6169 else
6170 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6171 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
6172 " * ffp_light[%u].specular) * att;\n", i);
6173 if (!legacy_lighting)
6174 shader_addline(buffer, "}\n");
6175 break;
6177 case WINED3D_LIGHT_DIRECTIONAL:
6178 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
6179 if (!settings->normal)
6180 break;
6181 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", i);
6182 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6183 " * ffp_light[%u].diffuse.xyz;\n", i);
6184 /* TODO: In the non-local viewer case the halfvector is constant
6185 * and could be precomputed and stored in a uniform. */
6186 if (settings->localviewer)
6187 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6188 else
6189 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6190 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6191 " * ffp_light[%u].specular;\n", i);
6192 break;
6194 case WINED3D_LIGHT_PARALLELPOINT:
6195 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
6196 if (!settings->normal)
6197 break;
6198 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", i);
6199 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6200 " * ffp_light[%u].diffuse.xyz;\n", i);
6201 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6202 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6203 " * ffp_light[%u].specular;\n", i);
6204 break;
6206 default:
6207 if (light_type)
6208 FIXME("Unhandled light type %#x.\n", light_type);
6209 continue;
6213 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
6214 ambient, diffuse, emissive);
6215 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
6216 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
6219 /* Context activation is done by the caller. */
6220 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
6221 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
6223 static const struct attrib_info
6225 const char type[6];
6226 const char name[24];
6228 attrib_info[] =
6230 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
6231 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
6232 /* TODO: Indexed vertex blending */
6233 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
6234 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
6235 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
6236 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
6237 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
6239 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6240 BOOL legacy_lighting = priv->legacy_lighting;
6241 GLuint shader_obj;
6242 unsigned int i;
6243 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6244 BOOL output_legacy_fogcoord = legacy_context;
6246 string_buffer_clear(buffer);
6248 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6250 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
6252 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
6254 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
6256 shader_addline(buffer, "\n");
6258 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
6259 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
6260 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
6261 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
6263 shader_addline(buffer, "uniform struct\n{\n");
6264 shader_addline(buffer, " vec4 emissive;\n");
6265 shader_addline(buffer, " vec4 ambient;\n");
6266 shader_addline(buffer, " vec4 diffuse;\n");
6267 shader_addline(buffer, " vec4 specular;\n");
6268 shader_addline(buffer, " float shininess;\n");
6269 shader_addline(buffer, "} ffp_material;\n");
6271 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
6272 shader_addline(buffer, "uniform struct\n{\n");
6273 shader_addline(buffer, " vec4 diffuse;\n");
6274 shader_addline(buffer, " vec4 specular;\n");
6275 shader_addline(buffer, " vec4 ambient;\n");
6276 shader_addline(buffer, " vec4 position;\n");
6277 shader_addline(buffer, " vec3 direction;\n");
6278 shader_addline(buffer, " float range;\n");
6279 shader_addline(buffer, " float falloff;\n");
6280 shader_addline(buffer, " float c_att;\n");
6281 shader_addline(buffer, " float l_att;\n");
6282 shader_addline(buffer, " float q_att;\n");
6283 shader_addline(buffer, " float cos_htheta;\n");
6284 shader_addline(buffer, " float cos_hphi;\n");
6285 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
6287 if (settings->point_size)
6289 shader_addline(buffer, "uniform struct\n{\n");
6290 shader_addline(buffer, " float size;\n");
6291 shader_addline(buffer, " float size_min;\n");
6292 shader_addline(buffer, " float size_max;\n");
6293 shader_addline(buffer, " float c_att;\n");
6294 shader_addline(buffer, " float l_att;\n");
6295 shader_addline(buffer, " float q_att;\n");
6296 shader_addline(buffer, "} ffp_point;\n");
6299 if (legacy_context)
6301 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6302 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6303 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6304 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6306 else
6308 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
6309 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
6310 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6311 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6314 shader_addline(buffer, "\nvoid main()\n{\n");
6315 shader_addline(buffer, "float m;\n");
6316 shader_addline(buffer, "vec3 r;\n");
6318 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
6320 if (attrib_info[i].name[0])
6321 shader_addline(buffer, "%s %s = vs_in%u;\n",
6322 attrib_info[i].type, attrib_info[i].name, i);
6324 for (i = 0; i < MAX_TEXTURES; ++i)
6326 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
6327 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
6328 && settings->texcoords & (1u << i))
6329 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
6332 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
6334 if (settings->transformed)
6336 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
6337 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6338 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
6340 else
6342 for (i = 0; i < settings->vertexblends; ++i)
6343 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
6345 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
6346 for (i = 0; i < settings->vertexblends + 1; ++i)
6347 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
6349 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6350 if (settings->clipping)
6351 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
6352 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
6355 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
6356 if (settings->normal)
6358 if (!settings->vertexblends)
6360 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
6362 else
6364 for (i = 0; i < settings->vertexblends + 1; ++i)
6365 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
6368 if (settings->normalize)
6369 shader_addline(buffer, "normal = normalize(normal);\n");
6372 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
6373 if (legacy_context)
6375 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
6376 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
6378 else
6380 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
6381 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
6384 for (i = 0; i < MAX_TEXTURES; ++i)
6386 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6388 switch (settings->texgen[i] & 0xffff0000)
6390 case WINED3DTSS_TCI_PASSTHRU:
6391 if (settings->texcoords & (1u << i))
6392 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
6393 i, i, i);
6394 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
6395 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
6396 else
6397 output_legacy_texcoord = FALSE;
6398 break;
6400 case WINED3DTSS_TCI_CAMERASPACENORMAL:
6401 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
6402 break;
6404 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
6405 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
6406 break;
6408 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
6409 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6410 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
6411 break;
6413 case WINED3DTSS_TCI_SPHEREMAP:
6414 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
6415 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
6416 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6417 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
6418 break;
6420 default:
6421 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
6422 break;
6424 if (output_legacy_texcoord)
6425 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
6428 switch (settings->fog_mode)
6430 case WINED3D_FFP_VS_FOG_OFF:
6431 output_legacy_fogcoord = FALSE;
6432 break;
6434 case WINED3D_FFP_VS_FOG_FOGCOORD:
6435 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
6436 break;
6438 case WINED3D_FFP_VS_FOG_RANGE:
6439 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
6440 break;
6442 case WINED3D_FFP_VS_FOG_DEPTH:
6443 if (settings->ortho_fog)
6444 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
6445 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
6446 else if (settings->transformed)
6447 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
6448 else
6449 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
6450 break;
6452 default:
6453 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
6454 break;
6456 if (output_legacy_fogcoord)
6457 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
6459 if (settings->point_size)
6461 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
6462 " + ffp_point.l_att * length(ec_pos.xyz)"
6463 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
6464 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
6465 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
6468 shader_addline(buffer, "}\n");
6470 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6471 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
6473 return shader_obj;
6476 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
6477 DWORD argnum, unsigned int stage, DWORD arg)
6479 const char *ret;
6481 if (arg == ARG_UNUSED)
6482 return "<unused arg>";
6484 switch (arg & WINED3DTA_SELECTMASK)
6486 case WINED3DTA_DIFFUSE:
6487 ret = "ffp_varying_diffuse";
6488 break;
6490 case WINED3DTA_CURRENT:
6491 ret = "ret";
6492 break;
6494 case WINED3DTA_TEXTURE:
6495 switch (stage)
6497 case 0: ret = "tex0"; break;
6498 case 1: ret = "tex1"; break;
6499 case 2: ret = "tex2"; break;
6500 case 3: ret = "tex3"; break;
6501 case 4: ret = "tex4"; break;
6502 case 5: ret = "tex5"; break;
6503 case 6: ret = "tex6"; break;
6504 case 7: ret = "tex7"; break;
6505 default:
6506 ret = "<invalid texture>";
6507 break;
6509 break;
6511 case WINED3DTA_TFACTOR:
6512 ret = "tex_factor";
6513 break;
6515 case WINED3DTA_SPECULAR:
6516 ret = "ffp_varying_specular";
6517 break;
6519 case WINED3DTA_TEMP:
6520 ret = "temp_reg";
6521 break;
6523 case WINED3DTA_CONSTANT:
6524 switch (stage)
6526 case 0: ret = "tss_const0"; break;
6527 case 1: ret = "tss_const1"; break;
6528 case 2: ret = "tss_const2"; break;
6529 case 3: ret = "tss_const3"; break;
6530 case 4: ret = "tss_const4"; break;
6531 case 5: ret = "tss_const5"; break;
6532 case 6: ret = "tss_const6"; break;
6533 case 7: ret = "tss_const7"; break;
6534 default:
6535 ret = "<invalid constant>";
6536 break;
6538 break;
6540 default:
6541 return "<unhandled arg>";
6544 if (arg & WINED3DTA_COMPLEMENT)
6546 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
6547 if (argnum == 0)
6548 ret = "arg0";
6549 else if (argnum == 1)
6550 ret = "arg1";
6551 else if (argnum == 2)
6552 ret = "arg2";
6555 if (arg & WINED3DTA_ALPHAREPLICATE)
6557 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
6558 if (argnum == 0)
6559 ret = "arg0";
6560 else if (argnum == 1)
6561 ret = "arg1";
6562 else if (argnum == 2)
6563 ret = "arg2";
6566 return ret;
6569 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
6570 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
6572 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
6574 if (color && alpha)
6575 dstmask = "";
6576 else if (color)
6577 dstmask = ".xyz";
6578 else
6579 dstmask = ".w";
6581 if (dst == tempreg)
6582 dstreg = "temp_reg";
6583 else
6584 dstreg = "ret";
6586 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
6587 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
6588 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
6590 switch (op)
6592 case WINED3D_TOP_DISABLE:
6593 break;
6595 case WINED3D_TOP_SELECT_ARG1:
6596 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
6597 break;
6599 case WINED3D_TOP_SELECT_ARG2:
6600 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
6601 break;
6603 case WINED3D_TOP_MODULATE:
6604 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6605 break;
6607 case WINED3D_TOP_MODULATE_4X:
6608 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
6609 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6610 break;
6612 case WINED3D_TOP_MODULATE_2X:
6613 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
6614 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6615 break;
6617 case WINED3D_TOP_ADD:
6618 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
6619 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6620 break;
6622 case WINED3D_TOP_ADD_SIGNED:
6623 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
6624 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6625 break;
6627 case WINED3D_TOP_ADD_SIGNED_2X:
6628 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
6629 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6630 break;
6632 case WINED3D_TOP_SUBTRACT:
6633 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
6634 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6635 break;
6637 case WINED3D_TOP_ADD_SMOOTH:
6638 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
6639 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
6640 break;
6642 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
6643 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
6644 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6645 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6646 break;
6648 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6649 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6650 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6651 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6652 break;
6654 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6655 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
6656 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6657 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6658 break;
6660 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6661 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6662 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6663 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
6664 break;
6666 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
6667 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
6668 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6669 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6670 break;
6672 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
6673 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
6674 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6675 break;
6677 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
6678 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
6679 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6680 break;
6682 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
6683 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6684 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6685 break;
6686 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
6687 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
6688 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6689 break;
6691 case WINED3D_TOP_BUMPENVMAP:
6692 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6693 /* These are handled in the first pass, nothing to do. */
6694 break;
6696 case WINED3D_TOP_DOTPRODUCT3:
6697 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
6698 dstreg, dstmask, arg1, arg2, dstmask);
6699 break;
6701 case WINED3D_TOP_MULTIPLY_ADD:
6702 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
6703 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
6704 break;
6706 case WINED3D_TOP_LERP:
6707 /* MSDN isn't quite right here. */
6708 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
6709 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
6710 break;
6712 default:
6713 FIXME("Unhandled operation %#x.\n", op);
6714 break;
6718 /* Context activation is done by the caller. */
6719 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
6720 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
6722 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
6723 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
6724 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6725 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6726 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
6727 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
6728 UINT lowest_disabled_stage;
6729 GLuint shader_id;
6730 DWORD arg0, arg1, arg2;
6731 unsigned int stage;
6733 string_buffer_clear(buffer);
6735 /* Find out which textures are read */
6736 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6738 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6739 break;
6741 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
6742 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
6743 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
6745 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
6746 || (stage == 0 && settings->color_key_enabled))
6747 tex_map |= 1u << stage;
6748 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6749 tfactor_used = TRUE;
6750 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6751 tempreg_used = TRUE;
6752 if (settings->op[stage].dst == tempreg)
6753 tempreg_used = TRUE;
6754 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6755 tss_const_map |= 1u << stage;
6757 switch (settings->op[stage].cop)
6759 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6760 lum_map |= 1u << stage;
6761 /* fall through */
6762 case WINED3D_TOP_BUMPENVMAP:
6763 bump_map |= 1u << stage;
6764 /* fall through */
6765 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6766 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6767 tex_map |= 1u << stage;
6768 break;
6770 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6771 tfactor_used = TRUE;
6772 break;
6774 default:
6775 break;
6778 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6779 continue;
6781 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
6782 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
6783 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
6785 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
6786 tex_map |= 1u << stage;
6787 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6788 tfactor_used = TRUE;
6789 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6790 tempreg_used = TRUE;
6791 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6792 tss_const_map |= 1u << stage;
6794 lowest_disabled_stage = stage;
6796 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6798 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6799 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6801 if (!needs_legacy_glsl_syntax(gl_info))
6802 shader_addline(buffer, "out vec4 ps_out[1];\n");
6804 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
6805 shader_addline(buffer, "vec4 ret;\n");
6806 if (tempreg_used || settings->sRGB_write)
6807 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
6808 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
6810 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6812 if (tss_const_map & (1u << stage))
6813 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
6815 if (!(tex_map & (1u << stage)))
6816 continue;
6818 switch (settings->op[stage].tex_type)
6820 case WINED3D_GL_RES_TYPE_TEX_1D:
6821 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
6822 break;
6823 case WINED3D_GL_RES_TYPE_TEX_2D:
6824 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
6825 break;
6826 case WINED3D_GL_RES_TYPE_TEX_3D:
6827 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
6828 break;
6829 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6830 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
6831 break;
6832 case WINED3D_GL_RES_TYPE_TEX_RECT:
6833 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
6834 break;
6835 default:
6836 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
6837 break;
6840 shader_addline(buffer, "vec4 tex%u;\n", stage);
6842 if (!(bump_map & (1u << stage)))
6843 continue;
6844 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
6846 if (!(lum_map & (1u << stage)))
6847 continue;
6848 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
6849 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
6851 if (tfactor_used)
6852 shader_addline(buffer, "uniform vec4 tex_factor;\n");
6853 if (settings->color_key_enabled)
6854 shader_addline(buffer, "uniform vec4 color_key[2];\n");
6855 shader_addline(buffer, "uniform vec4 specular_enable;\n");
6857 if (settings->sRGB_write)
6859 shader_addline(buffer, "const vec4 srgb_const0 = ");
6860 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
6861 shader_addline(buffer, ";\n");
6862 shader_addline(buffer, "const vec4 srgb_const1 = ");
6863 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
6864 shader_addline(buffer, ";\n");
6867 shader_addline(buffer, "uniform struct\n{\n");
6868 shader_addline(buffer, " vec4 color;\n");
6869 shader_addline(buffer, " float density;\n");
6870 shader_addline(buffer, " float end;\n");
6871 shader_addline(buffer, " float scale;\n");
6872 shader_addline(buffer, "} ffp_fog;\n");
6874 if (alpha_test_func != WINED3D_CMP_ALWAYS)
6875 shader_addline(buffer, "uniform float alpha_test_ref;\n");
6877 if (legacy_context)
6879 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6880 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6881 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6882 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6883 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6885 else
6887 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
6888 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
6889 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6890 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6891 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6894 shader_addline(buffer, "void main()\n{\n");
6896 if (legacy_context)
6898 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
6899 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
6902 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6904 if (tex_map & (1u << stage))
6906 if (settings->pointsprite)
6907 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
6908 else if (settings->texcoords_initialized & (1u << stage))
6909 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
6910 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
6911 else
6912 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
6916 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
6917 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6919 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
6920 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
6922 /* Generate texture sampling instructions */
6923 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
6925 const char *texture_function, *coord_mask;
6926 BOOL proj;
6928 if (!(tex_map & (1u << stage)))
6929 continue;
6931 if (settings->op[stage].projected == proj_none)
6933 proj = FALSE;
6935 else if (settings->op[stage].projected == proj_count4
6936 || settings->op[stage].projected == proj_count3)
6938 proj = TRUE;
6940 else
6942 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
6943 proj = TRUE;
6946 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
6947 proj = FALSE;
6949 switch (settings->op[stage].tex_type)
6951 case WINED3D_GL_RES_TYPE_TEX_1D:
6952 if (proj)
6954 texture_function = "texture1DProj";
6955 coord_mask = "xw";
6957 else
6959 texture_function = "texture1D";
6960 coord_mask = "x";
6962 break;
6963 case WINED3D_GL_RES_TYPE_TEX_2D:
6964 if (proj)
6966 texture_function = "texture2DProj";
6967 coord_mask = "xyw";
6969 else
6971 texture_function = "texture2D";
6972 coord_mask = "xy";
6974 break;
6975 case WINED3D_GL_RES_TYPE_TEX_3D:
6976 if (proj)
6978 texture_function = "texture3DProj";
6979 coord_mask = "xyzw";
6981 else
6983 texture_function = "texture3D";
6984 coord_mask = "xyz";
6986 break;
6987 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6988 texture_function = "textureCube";
6989 coord_mask = "xyz";
6990 break;
6991 case WINED3D_GL_RES_TYPE_TEX_RECT:
6992 if (proj)
6994 texture_function = "texture2DRectProj";
6995 coord_mask = "xyw";
6997 else
6999 texture_function = "texture2DRect";
7000 coord_mask = "xy";
7002 break;
7003 default:
7004 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
7005 texture_function = "";
7006 coord_mask = "xyzw";
7007 break;
7009 if (!needs_legacy_glsl_syntax(gl_info))
7010 texture_function = proj ? "textureProj" : "texture";
7012 if (stage > 0
7013 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
7014 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
7016 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
7018 /* With projective textures, texbem only divides the static
7019 * texture coord, not the displacement, so multiply the
7020 * displacement with the dividing parameter before passing it to
7021 * TXP. */
7022 if (settings->op[stage].projected != proj_none)
7024 if (settings->op[stage].projected == proj_count4)
7026 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
7027 stage, stage);
7028 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
7030 else
7032 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
7033 stage, stage);
7034 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
7037 else
7039 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
7042 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
7043 stage, texture_function, stage, coord_mask);
7045 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
7046 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
7047 stage, stage - 1, stage - 1, stage - 1);
7049 else if (settings->op[stage].projected == proj_count3)
7051 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
7052 stage, texture_function, stage, stage);
7054 else
7056 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
7057 stage, texture_function, stage, stage, coord_mask);
7060 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
7061 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
7062 settings->op[stage].color_fixup);
7065 if (settings->color_key_enabled)
7067 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
7068 shader_addline(buffer, " discard;\n");
7071 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
7073 /* Generate the main shader */
7074 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7076 BOOL op_equal;
7078 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
7079 break;
7081 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
7082 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
7083 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
7084 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
7085 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
7086 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
7087 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
7088 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
7089 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
7090 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
7091 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
7092 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
7093 else
7094 op_equal = settings->op[stage].aop == settings->op[stage].cop
7095 && settings->op[stage].carg0 == settings->op[stage].aarg0
7096 && settings->op[stage].carg1 == settings->op[stage].aarg1
7097 && settings->op[stage].carg2 == settings->op[stage].aarg2;
7099 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
7101 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
7102 settings->op[stage].cop, settings->op[stage].carg0,
7103 settings->op[stage].carg1, settings->op[stage].carg2);
7105 else if (op_equal)
7107 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
7108 settings->op[stage].cop, settings->op[stage].carg0,
7109 settings->op[stage].carg1, settings->op[stage].carg2);
7111 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
7112 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
7114 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
7115 settings->op[stage].cop, settings->op[stage].carg0,
7116 settings->op[stage].carg1, settings->op[stage].carg2);
7117 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
7118 settings->op[stage].aop, settings->op[stage].aarg0,
7119 settings->op[stage].aarg1, settings->op[stage].aarg2);
7123 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
7124 get_fragment_output(gl_info));
7126 if (settings->sRGB_write)
7127 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7129 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
7131 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
7133 shader_addline(buffer, "}\n");
7135 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7136 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7138 string_buffer_release(&priv->string_buffers, tex_reg_name);
7139 return shader_id;
7142 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
7143 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
7145 struct glsl_ffp_vertex_shader *shader;
7146 const struct wine_rb_entry *entry;
7148 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
7149 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
7151 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
7152 return NULL;
7154 shader->desc.settings = *settings;
7155 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
7156 list_init(&shader->linked_programs);
7157 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
7158 ERR("Failed to insert ffp vertex shader.\n");
7160 return shader;
7163 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
7164 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
7166 struct glsl_ffp_fragment_shader *glsl_desc;
7167 const struct ffp_frag_desc *desc;
7169 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
7170 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
7172 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
7173 return NULL;
7175 glsl_desc->entry.settings = *args;
7176 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
7177 list_init(&glsl_desc->linked_programs);
7178 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
7180 return glsl_desc;
7184 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
7185 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
7187 unsigned int i;
7188 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7190 for (i = 0; i < vs_c_count; ++i)
7192 string_buffer_sprintf(name, "vs_c[%u]", i);
7193 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7195 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
7197 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
7199 string_buffer_sprintf(name, "vs_i[%u]", i);
7200 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7203 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
7205 string_buffer_sprintf(name, "vs_b[%u]", i);
7206 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7209 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "posFixup"));
7211 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
7213 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
7214 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7216 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
7217 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
7218 for (i = 0; i < MAX_TEXTURES; ++i)
7220 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
7221 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7223 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
7224 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
7225 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
7226 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
7227 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
7228 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
7229 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
7231 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
7232 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7233 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
7234 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7235 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
7236 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7237 string_buffer_sprintf(name, "ffp_light[%u].position", i);
7238 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7239 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
7240 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7241 string_buffer_sprintf(name, "ffp_light[%u].range", i);
7242 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7243 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
7244 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7245 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
7246 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7247 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
7248 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7249 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
7250 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7251 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
7252 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7253 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
7254 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7256 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
7257 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
7258 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
7259 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
7260 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
7261 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
7263 string_buffer_release(&priv->string_buffers, name);
7266 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
7267 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
7269 unsigned int i;
7270 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7272 for (i = 0; i < ps_c_count; ++i)
7274 string_buffer_sprintf(name, "ps_c[%u]", i);
7275 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7277 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
7279 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
7281 string_buffer_sprintf(name, "ps_i[%u]", i);
7282 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7285 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
7287 string_buffer_sprintf(name, "ps_b[%u]", i);
7288 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7291 for (i = 0; i < MAX_TEXTURES; ++i)
7293 string_buffer_sprintf(name, "bumpenv_mat%u", i);
7294 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7295 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
7296 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7297 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
7298 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7299 string_buffer_sprintf(name, "tss_const%u", i);
7300 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7303 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
7304 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
7306 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
7307 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
7308 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
7309 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
7311 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
7313 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
7314 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
7315 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
7317 string_buffer_release(&priv->string_buffers, name);
7320 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
7321 struct shader_glsl_priv *priv, GLuint program_id,
7322 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
7324 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
7325 GLuint block_idx;
7326 unsigned int i;
7327 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7329 for (i = 0; i < count; ++i)
7331 if (!reg_maps->cb_sizes[i])
7332 continue;
7334 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
7335 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
7336 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
7338 checkGLcall("glUniformBlockBinding");
7339 string_buffer_release(&priv->string_buffers, name);
7342 /* Context activation is done by the caller. */
7343 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
7344 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
7346 const struct wined3d_gl_info *gl_info = context->gl_info;
7347 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
7348 const struct ps_np2fixup_info *np2fixup_info = NULL;
7349 struct glsl_shader_prog_link *entry = NULL;
7350 struct wined3d_shader *vshader = NULL;
7351 struct wined3d_shader *gshader = NULL;
7352 struct wined3d_shader *pshader = NULL;
7353 GLuint program_id;
7354 GLuint reorder_shader_id = 0;
7355 unsigned int i;
7356 GLuint vs_id = 0;
7357 GLuint gs_id = 0;
7358 GLuint ps_id = 0;
7359 struct list *ps_list, *vs_list;
7360 WORD attribs_map;
7361 struct wined3d_string_buffer *tmp_name;
7363 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
7365 vs_id = ctx_data->glsl_program->vs.id;
7366 vs_list = &ctx_data->glsl_program->vs.shader_entry;
7368 if (use_vs(state))
7370 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7371 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
7373 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
7374 && ctx_data->glsl_program->gs.id)
7375 gs_id = ctx_data->glsl_program->gs.id;
7376 else if (gshader)
7378 struct gs_compile_args args;
7380 find_gs_compile_args(state, gshader, &args);
7381 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
7385 else if (use_vs(state))
7387 struct vs_compile_args vs_compile_args;
7389 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7391 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, d3d_info);
7392 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
7393 vs_list = &vshader->linked_programs;
7395 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
7397 struct gs_compile_args gs_compile_args;
7399 find_gs_compile_args(state, gshader, &gs_compile_args);
7400 gs_id = find_glsl_geometry_shader(context, priv, gshader, &gs_compile_args);
7403 else if (priv->vertex_pipe == &glsl_vertex_pipe)
7405 struct glsl_ffp_vertex_shader *ffp_shader;
7406 struct wined3d_ffp_vs_settings settings;
7408 wined3d_ffp_get_vs_settings(context, state, &settings);
7409 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
7410 vs_id = ffp_shader->id;
7411 vs_list = &ffp_shader->linked_programs;
7414 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
7416 ps_id = ctx_data->glsl_program->ps.id;
7417 ps_list = &ctx_data->glsl_program->ps.shader_entry;
7419 if (use_ps(state))
7420 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7422 else if (use_ps(state))
7424 struct ps_compile_args ps_compile_args;
7425 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7426 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
7427 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
7428 pshader, &ps_compile_args, &np2fixup_info);
7429 ps_list = &pshader->linked_programs;
7431 else if (priv->fragment_pipe == &glsl_fragment_pipe)
7433 struct glsl_ffp_fragment_shader *ffp_shader;
7434 struct ffp_frag_settings settings;
7436 gen_ffp_frag_op(context, state, &settings, FALSE);
7437 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
7438 ps_id = ffp_shader->id;
7439 ps_list = &ffp_shader->linked_programs;
7442 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
7444 ctx_data->glsl_program = entry;
7445 return;
7448 /* If we get to this point, then no matching program exists, so we create one */
7449 program_id = GL_EXTCALL(glCreateProgram());
7450 TRACE("Created new GLSL shader program %u.\n", program_id);
7452 /* Create the entry */
7453 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
7454 entry->id = program_id;
7455 entry->vs.id = vs_id;
7456 entry->gs.id = gs_id;
7457 entry->ps.id = ps_id;
7458 entry->constant_version = 0;
7459 entry->ps.np2_fixup_info = np2fixup_info;
7460 /* Add the hash table entry */
7461 add_glsl_program_entry(priv, entry);
7463 /* Set the current program */
7464 ctx_data->glsl_program = entry;
7466 /* Attach GLSL vshader */
7467 if (vs_id)
7469 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
7470 GL_EXTCALL(glAttachShader(program_id, vs_id));
7471 checkGLcall("glAttachShader");
7473 list_add_head(vs_list, &entry->vs.shader_entry);
7476 if (vshader)
7478 attribs_map = vshader->reg_maps.input_registers;
7479 if (vshader->reg_maps.shader_version.major < 4)
7481 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
7482 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
7483 d3d_info->emulated_flatshading
7484 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
7485 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
7486 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
7487 checkGLcall("glAttachShader");
7488 /* Flag the reorder function for deletion, it will be freed
7489 * automatically when the program is destroyed. */
7490 GL_EXTCALL(glDeleteShader(reorder_shader_id));
7493 else
7495 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
7498 /* Bind vertex attributes to a corresponding index number to match
7499 * the same index numbers as ARB_vertex_programs (makes loading
7500 * vertex attributes simpler). With this method, we can use the
7501 * exact same code to load the attributes later for both ARB and
7502 * GLSL shaders.
7504 * We have to do this here because we need to know the Program ID
7505 * in order to make the bindings work, and it has to be done prior
7506 * to linking the GLSL program. */
7507 tmp_name = string_buffer_get(&priv->string_buffers);
7508 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
7510 if (!(attribs_map & 1))
7511 continue;
7513 string_buffer_sprintf(tmp_name, "vs_in%u", i);
7514 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
7516 checkGLcall("glBindAttribLocation");
7517 string_buffer_release(&priv->string_buffers, tmp_name);
7519 if (gshader)
7521 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
7522 GL_EXTCALL(glAttachShader(program_id, gs_id));
7523 checkGLcall("glAttachShader");
7525 TRACE("input type %s, output type %s, vertices out %u.\n",
7526 debug_d3dprimitivetype(gshader->u.gs.input_type),
7527 debug_d3dprimitivetype(gshader->u.gs.output_type),
7528 gshader->u.gs.vertices_out);
7529 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
7530 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
7531 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
7532 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
7533 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
7534 gshader->u.gs.vertices_out));
7535 checkGLcall("glProgramParameteriARB");
7537 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
7540 /* Attach GLSL pshader */
7541 if (ps_id)
7543 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
7544 GL_EXTCALL(glAttachShader(program_id, ps_id));
7545 checkGLcall("glAttachShader");
7547 list_add_head(ps_list, &entry->ps.shader_entry);
7550 /* Link the program */
7551 TRACE("Linking GLSL shader program %u.\n", program_id);
7552 GL_EXTCALL(glLinkProgram(program_id));
7553 shader_glsl_validate_link(gl_info, program_id);
7555 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
7556 vshader ? vshader->limits->constant_float : 0);
7557 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
7558 pshader ? pshader->limits->constant_float : 0);
7559 checkGLcall("Find glsl program uniform locations");
7561 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
7563 if (pshader && pshader->reg_maps.shader_version.major >= 3
7564 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
7566 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
7567 entry->vs.vertex_color_clamp = GL_FALSE;
7569 else
7571 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7574 else
7576 /* With core profile we never change vertex_color_clamp from
7577 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
7578 * glClampColorARB(). */
7579 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7582 /* Set the shader to allow uniform loading on it */
7583 GL_EXTCALL(glUseProgram(program_id));
7584 checkGLcall("glUseProgram");
7586 /* Texture unit mapping is set up to be the same each time the shader
7587 * program is used so we can hardcode the sampler uniform values. */
7588 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
7590 entry->constant_update_mask = 0;
7591 if (vshader)
7593 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
7594 if (vshader->reg_maps.integer_constants)
7595 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
7596 if (vshader->reg_maps.boolean_constants)
7597 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
7598 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
7600 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps,
7601 0, gl_info->limits.vertex_uniform_blocks);
7603 else
7605 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
7606 | WINED3D_SHADER_CONST_FFP_PROJ;
7608 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
7610 if (entry->vs.modelview_matrix_location[i] != -1)
7612 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
7613 break;
7617 for (i = 0; i < MAX_TEXTURES; ++i)
7619 if (entry->vs.texture_matrix_location[i] != -1)
7621 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7622 break;
7625 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
7626 || entry->vs.material_specular_location != -1
7627 || entry->vs.material_emissive_location != -1
7628 || entry->vs.material_shininess_location != -1)
7629 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
7630 if (entry->vs.light_ambient_location != -1)
7631 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
7633 if (entry->vs.pointsize_min_location != -1)
7634 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
7636 if (gshader)
7637 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps,
7638 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
7640 if (ps_id)
7642 if (pshader)
7644 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
7645 if (pshader->reg_maps.integer_constants)
7646 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
7647 if (pshader->reg_maps.boolean_constants)
7648 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
7649 if (entry->ps.ycorrection_location != -1)
7650 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
7652 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps,
7653 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
7654 gl_info->limits.fragment_uniform_blocks);
7656 else
7658 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7661 for (i = 0; i < MAX_TEXTURES; ++i)
7663 if (entry->ps.bumpenv_mat_location[i] != -1)
7665 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
7666 break;
7670 if (entry->ps.fog_color_location != -1)
7671 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
7672 if (entry->ps.alpha_test_ref_location != -1)
7673 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
7674 if (entry->ps.np2_fixup_location != -1)
7675 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
7676 if (entry->ps.color_key_location != -1)
7677 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
7681 /* Context activation is done by the caller. */
7682 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum wined3d_gl_resource_type tex_type,
7683 BOOL masked)
7685 GLuint program_id;
7686 GLuint vshader_id, pshader_id;
7687 const char *blt_pshader;
7689 static const char blt_vshader[] =
7690 "#version 120\n"
7691 "void main(void)\n"
7692 "{\n"
7693 " gl_Position = gl_Vertex;\n"
7694 " gl_FrontColor = vec4(1.0);\n"
7695 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
7696 "}\n";
7698 static const char * const blt_pshaders_full[WINED3D_GL_RES_TYPE_COUNT] =
7700 /* WINED3D_GL_RES_TYPE_TEX_1D */
7701 NULL,
7702 /* WINED3D_GL_RES_TYPE_TEX_2D */
7703 "#version 120\n"
7704 "uniform sampler2D sampler;\n"
7705 "void main(void)\n"
7706 "{\n"
7707 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7708 "}\n",
7709 /* WINED3D_GL_RES_TYPE_TEX_3D */
7710 NULL,
7711 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7712 "#version 120\n"
7713 "uniform samplerCube sampler;\n"
7714 "void main(void)\n"
7715 "{\n"
7716 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7717 "}\n",
7718 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7719 "#version 120\n"
7720 "#extension GL_ARB_texture_rectangle : enable\n"
7721 "uniform sampler2DRect sampler;\n"
7722 "void main(void)\n"
7723 "{\n"
7724 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7725 "}\n",
7726 /* WINED3D_GL_RES_TYPE_BUFFER */
7727 NULL,
7728 /* WINED3D_GL_RES_TYPE_RB */
7729 NULL,
7732 static const char * const blt_pshaders_masked[WINED3D_GL_RES_TYPE_COUNT] =
7734 /* WINED3D_GL_RES_TYPE_TEX_1D */
7735 NULL,
7736 /* WINED3D_GL_RES_TYPE_TEX_2D */
7737 "#version 120\n"
7738 "uniform sampler2D sampler;\n"
7739 "uniform vec4 mask;\n"
7740 "void main(void)\n"
7741 "{\n"
7742 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7743 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7744 "}\n",
7745 /* WINED3D_GL_RES_TYPE_TEX_3D */
7746 NULL,
7747 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7748 "#version 120\n"
7749 "uniform samplerCube sampler;\n"
7750 "uniform vec4 mask;\n"
7751 "void main(void)\n"
7752 "{\n"
7753 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7754 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7755 "}\n",
7756 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7757 "#version 120\n"
7758 "#extension GL_ARB_texture_rectangle : enable\n"
7759 "uniform sampler2DRect sampler;\n"
7760 "uniform vec4 mask;\n"
7761 "void main(void)\n"
7762 "{\n"
7763 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7764 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7765 "}\n",
7766 /* WINED3D_GL_RES_TYPE_BUFFER */
7767 NULL,
7768 /* WINED3D_GL_RES_TYPE_RB */
7769 NULL,
7772 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
7773 if (!blt_pshader)
7775 FIXME("tex_type %#x not supported\n", tex_type);
7776 return 0;
7779 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7780 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
7782 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7783 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
7785 program_id = GL_EXTCALL(glCreateProgram());
7786 GL_EXTCALL(glAttachShader(program_id, vshader_id));
7787 GL_EXTCALL(glAttachShader(program_id, pshader_id));
7788 GL_EXTCALL(glLinkProgram(program_id));
7790 shader_glsl_validate_link(gl_info, program_id);
7792 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
7793 * is destroyed
7795 GL_EXTCALL(glDeleteShader(vshader_id));
7796 GL_EXTCALL(glDeleteShader(pshader_id));
7797 return program_id;
7800 /* Context activation is done by the caller. */
7801 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
7802 const struct wined3d_state *state)
7804 struct glsl_context_data *ctx_data = context->shader_backend_data;
7805 const struct wined3d_gl_info *gl_info = context->gl_info;
7806 struct shader_glsl_priv *priv = shader_priv;
7807 GLuint program_id = 0, prev_id = 0;
7808 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
7810 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
7811 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
7813 if (ctx_data->glsl_program)
7815 prev_id = ctx_data->glsl_program->id;
7816 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7818 else
7820 prev_id = 0;
7821 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7824 set_glsl_shader_program(context, state, priv, ctx_data);
7826 if (ctx_data->glsl_program)
7828 program_id = ctx_data->glsl_program->id;
7829 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7831 else
7833 program_id = 0;
7834 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7837 if (old_vertex_color_clamp != current_vertex_color_clamp)
7839 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7841 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
7842 checkGLcall("glClampColorARB");
7844 else
7846 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
7850 TRACE("Using GLSL program %u.\n", program_id);
7852 if (prev_id != program_id)
7854 GL_EXTCALL(glUseProgram(program_id));
7855 checkGLcall("glUseProgram");
7857 if (program_id)
7858 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
7862 /* "context" is not necessarily the currently active context. */
7863 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
7865 struct glsl_context_data *ctx_data = context->shader_backend_data;
7867 ctx_data->glsl_program = NULL;
7868 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
7869 | (1u << WINED3D_SHADER_TYPE_VERTEX)
7870 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
7871 | (1u << WINED3D_SHADER_TYPE_HULL)
7872 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
7875 /* Context activation is done by the caller. */
7876 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
7878 const struct wined3d_gl_info *gl_info = context->gl_info;
7879 struct shader_glsl_priv *priv = shader_priv;
7881 shader_glsl_invalidate_current_program(context);
7882 GL_EXTCALL(glUseProgram(0));
7883 checkGLcall("glUseProgram");
7885 priv->vertex_pipe->vp_enable(gl_info, FALSE);
7886 priv->fragment_pipe->enable_extension(gl_info, FALSE);
7888 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7890 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
7891 checkGLcall("glClampColorARB");
7895 /* Context activation is done by the caller. */
7896 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
7897 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size)
7899 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
7900 struct shader_glsl_priv *priv = shader_priv;
7901 GLuint *blt_program;
7902 GLint loc;
7904 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
7905 if (!*blt_program)
7907 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
7908 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
7909 GL_EXTCALL(glUseProgram(*blt_program));
7910 GL_EXTCALL(glUniform1i(loc, 0));
7912 else
7914 GL_EXTCALL(glUseProgram(*blt_program));
7917 if (masked)
7919 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
7920 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
7924 /* Context activation is done by the caller. */
7925 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
7927 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
7928 GLuint program_id;
7930 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
7931 if (program_id) TRACE("Using GLSL program %u\n", program_id);
7933 GL_EXTCALL(glUseProgram(program_id));
7934 checkGLcall("glUseProgram");
7937 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
7938 const struct glsl_shader_prog_link *program)
7940 const struct glsl_context_data *ctx_data;
7941 struct wined3d_context *context;
7942 unsigned int i;
7944 for (i = 0; i < device->context_count; ++i)
7946 context = device->contexts[i];
7947 ctx_data = context->shader_backend_data;
7949 if (ctx_data->glsl_program == program)
7950 shader_glsl_invalidate_current_program(context);
7954 static void shader_glsl_destroy(struct wined3d_shader *shader)
7956 struct glsl_shader_private *shader_data = shader->backend_data;
7957 struct wined3d_device *device = shader->device;
7958 struct shader_glsl_priv *priv = device->shader_priv;
7959 const struct wined3d_gl_info *gl_info;
7960 const struct list *linked_programs;
7961 struct wined3d_context *context;
7963 if (!shader_data || !shader_data->num_gl_shaders)
7965 HeapFree(GetProcessHeap(), 0, shader_data);
7966 shader->backend_data = NULL;
7967 return;
7970 context = context_acquire(device, NULL);
7971 gl_info = context->gl_info;
7973 TRACE("Deleting linked programs.\n");
7974 linked_programs = &shader->linked_programs;
7975 if (linked_programs->next)
7977 struct glsl_shader_prog_link *entry, *entry2;
7978 UINT i;
7980 switch (shader->reg_maps.shader_version.type)
7982 case WINED3D_SHADER_TYPE_PIXEL:
7984 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
7986 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7988 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
7989 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7990 checkGLcall("glDeleteShader");
7992 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
7994 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7995 struct glsl_shader_prog_link, ps.shader_entry)
7997 shader_glsl_invalidate_contexts_program(device, entry);
7998 delete_glsl_program_entry(priv, gl_info, entry);
8001 break;
8004 case WINED3D_SHADER_TYPE_VERTEX:
8006 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
8008 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8010 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
8011 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8012 checkGLcall("glDeleteShader");
8014 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
8016 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8017 struct glsl_shader_prog_link, vs.shader_entry)
8019 shader_glsl_invalidate_contexts_program(device, entry);
8020 delete_glsl_program_entry(priv, gl_info, entry);
8023 break;
8026 case WINED3D_SHADER_TYPE_GEOMETRY:
8028 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
8030 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8032 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
8033 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8034 checkGLcall("glDeleteShader");
8036 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
8038 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8039 struct glsl_shader_prog_link, gs.shader_entry)
8041 shader_glsl_invalidate_contexts_program(device, entry);
8042 delete_glsl_program_entry(priv, gl_info, entry);
8045 break;
8048 default:
8049 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8050 break;
8054 HeapFree(GetProcessHeap(), 0, shader->backend_data);
8055 shader->backend_data = NULL;
8057 context_release(context);
8060 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
8062 const struct glsl_program_key *k = key;
8063 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
8064 const struct glsl_shader_prog_link, program_lookup_entry);
8066 if (k->vs_id > prog->vs.id) return 1;
8067 else if (k->vs_id < prog->vs.id) return -1;
8069 if (k->gs_id > prog->gs.id) return 1;
8070 else if (k->gs_id < prog->gs.id) return -1;
8072 if (k->ps_id > prog->ps.id) return 1;
8073 else if (k->ps_id < prog->ps.id) return -1;
8075 return 0;
8078 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
8080 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
8081 + constant_count * sizeof(*heap->contained)
8082 + constant_count * sizeof(*heap->positions);
8083 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
8085 if (!mem)
8087 ERR("Failed to allocate memory\n");
8088 return FALSE;
8091 heap->entries = mem;
8092 heap->entries[1].version = 0;
8093 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
8094 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
8095 heap->positions = (unsigned int *)(heap->contained + constant_count);
8096 heap->size = 1;
8098 return TRUE;
8101 static void constant_heap_free(struct constant_heap *heap)
8103 HeapFree(GetProcessHeap(), 0, heap->entries);
8106 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
8108 wined3d_rb_alloc,
8109 wined3d_rb_realloc,
8110 wined3d_rb_free,
8111 glsl_program_key_compare,
8114 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
8115 const struct fragment_pipeline *fragment_pipe)
8117 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
8118 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
8119 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
8120 struct fragment_caps fragment_caps;
8121 void *vertex_priv, *fragment_priv;
8123 string_buffer_list_init(&priv->string_buffers);
8125 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
8127 ERR("Failed to initialize vertex pipe.\n");
8128 HeapFree(GetProcessHeap(), 0, priv);
8129 return E_FAIL;
8132 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
8134 ERR("Failed to initialize fragment pipe.\n");
8135 vertex_pipe->vp_free(device);
8136 HeapFree(GetProcessHeap(), 0, priv);
8137 return E_FAIL;
8140 if (!string_buffer_init(&priv->shader_buffer))
8142 ERR("Failed to initialize shader buffer.\n");
8143 goto fail;
8146 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
8147 if (!priv->stack)
8149 ERR("Failed to allocate memory.\n");
8150 goto fail;
8153 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
8155 ERR("Failed to initialize vertex shader constant heap\n");
8156 goto fail;
8159 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
8161 ERR("Failed to initialize pixel shader constant heap\n");
8162 goto fail;
8165 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
8167 ERR("Failed to initialize rbtree.\n");
8168 goto fail;
8171 priv->next_constant_version = 1;
8172 priv->vertex_pipe = vertex_pipe;
8173 priv->fragment_pipe = fragment_pipe;
8174 fragment_pipe->get_caps(gl_info, &fragment_caps);
8175 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
8176 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
8178 device->vertex_priv = vertex_priv;
8179 device->fragment_priv = fragment_priv;
8180 device->shader_priv = priv;
8182 return WINED3D_OK;
8184 fail:
8185 constant_heap_free(&priv->pconst_heap);
8186 constant_heap_free(&priv->vconst_heap);
8187 HeapFree(GetProcessHeap(), 0, priv->stack);
8188 string_buffer_free(&priv->shader_buffer);
8189 fragment_pipe->free_private(device);
8190 vertex_pipe->vp_free(device);
8191 HeapFree(GetProcessHeap(), 0, priv);
8192 return E_OUTOFMEMORY;
8195 /* Context activation is done by the caller. */
8196 static void shader_glsl_free(struct wined3d_device *device)
8198 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
8199 struct shader_glsl_priv *priv = device->shader_priv;
8200 int i;
8202 for (i = 0; i < WINED3D_GL_RES_TYPE_COUNT; ++i)
8204 if (priv->depth_blt_program_full[i])
8206 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
8208 if (priv->depth_blt_program_masked[i])
8210 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
8214 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
8215 constant_heap_free(&priv->pconst_heap);
8216 constant_heap_free(&priv->vconst_heap);
8217 HeapFree(GetProcessHeap(), 0, priv->stack);
8218 string_buffer_list_cleanup(&priv->string_buffers);
8219 string_buffer_free(&priv->shader_buffer);
8220 priv->fragment_pipe->free_private(device);
8221 priv->vertex_pipe->vp_free(device);
8223 HeapFree(GetProcessHeap(), 0, device->shader_priv);
8224 device->shader_priv = NULL;
8227 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
8229 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
8230 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
8233 static void shader_glsl_free_context_data(struct wined3d_context *context)
8235 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
8238 static void shader_glsl_init_context_state(struct wined3d_context *context)
8240 const struct wined3d_gl_info *gl_info = context->gl_info;
8242 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
8243 checkGLcall("GL_PROGRAM_POINT_SIZE");
8246 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
8248 UINT shader_model;
8250 /* FIXME: Check for the specific extensions required for SM5 support
8251 * (ARB_compute_shader, ARB_tessellation_shader, ARB_gpu_shader5, ...) as
8252 * soon as we introduce them, adjusting the GL / GLSL version checks
8253 * accordingly. */
8254 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 30) && gl_info->supported[WINED3D_GL_VERSION_4_3])
8255 shader_model = 5;
8256 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50) && gl_info->supported[WINED3D_GL_VERSION_3_2]
8257 && gl_info->supported[ARB_SHADER_BIT_ENCODING] && gl_info->supported[ARB_SAMPLER_OBJECTS]
8258 && gl_info->supported[ARB_TEXTURE_SWIZZLE])
8259 shader_model = 4;
8260 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
8261 * texldd and texldl instructions. */
8262 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
8263 shader_model = 3;
8264 else
8265 shader_model = 2;
8266 TRACE("Shader model %u.\n", shader_model);
8268 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
8269 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
8270 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
8271 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
8272 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
8274 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
8275 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
8276 caps->varying_count = gl_info->limits.glsl_varyings;
8278 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
8279 * Direct3D minimum requirement.
8281 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
8282 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
8284 * The problem is that the refrast clamps temporary results in the shader to
8285 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
8286 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
8287 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
8288 * offer a way to query this.
8290 if (shader_model >= 4)
8291 caps->ps_1x_max_value = FLT_MAX;
8292 else
8293 caps->ps_1x_max_value = 1024.0f;
8295 /* Ideally we'd only set caps like sRGB writes here if supported by both
8296 * the shader backend and the fragment pipe, but we can get called before
8297 * shader_glsl_alloc(). */
8298 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
8299 | WINED3D_SHADER_CAP_SRGB_WRITE;
8302 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
8304 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
8306 TRACE("Checking support for fixup:\n");
8307 dump_color_fixup_desc(fixup);
8310 /* We support everything except YUV conversions. */
8311 if (!is_complex_fixup(fixup))
8313 TRACE("[OK]\n");
8314 return TRUE;
8317 TRACE("[FAILED]\n");
8318 return FALSE;
8321 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
8323 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
8324 /* WINED3DSIH_ADD */ shader_glsl_binop,
8325 /* WINED3DSIH_AND */ shader_glsl_binop,
8326 /* WINED3DSIH_BEM */ shader_glsl_bem,
8327 /* WINED3DSIH_BREAK */ shader_glsl_break,
8328 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
8329 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
8330 /* WINED3DSIH_CALL */ shader_glsl_call,
8331 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
8332 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
8333 /* WINED3DSIH_CND */ shader_glsl_cnd,
8334 /* WINED3DSIH_CRS */ shader_glsl_cross,
8335 /* WINED3DSIH_CUT */ shader_glsl_cut,
8336 /* WINED3DSIH_DCL */ shader_glsl_nop,
8337 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
8338 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
8339 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ NULL,
8340 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ NULL,
8341 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ NULL,
8342 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
8343 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ NULL,
8344 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
8345 /* WINED3DSIH_DCL_INPUT_PS */ NULL,
8346 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
8347 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
8348 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
8349 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
8350 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
8351 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ NULL,
8352 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
8353 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
8354 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ NULL,
8355 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
8356 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
8357 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ NULL,
8358 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ NULL,
8359 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ NULL,
8360 /* WINED3DSIH_DCL_UAV_TYPED */ NULL,
8361 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
8362 /* WINED3DSIH_DEF */ shader_glsl_nop,
8363 /* WINED3DSIH_DEFB */ shader_glsl_nop,
8364 /* WINED3DSIH_DEFI */ shader_glsl_nop,
8365 /* WINED3DSIH_DIV */ shader_glsl_binop,
8366 /* WINED3DSIH_DP2 */ shader_glsl_dot,
8367 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
8368 /* WINED3DSIH_DP3 */ shader_glsl_dot,
8369 /* WINED3DSIH_DP4 */ shader_glsl_dot,
8370 /* WINED3DSIH_DST */ shader_glsl_dst,
8371 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
8372 /* WINED3DSIH_DSX_COARSE */ NULL,
8373 /* WINED3DSIH_DSX_FINE */ NULL,
8374 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
8375 /* WINED3DSIH_DSY_COARSE */ NULL,
8376 /* WINED3DSIH_DSY_FINE */ NULL,
8377 /* WINED3DSIH_ELSE */ shader_glsl_else,
8378 /* WINED3DSIH_EMIT */ shader_glsl_emit,
8379 /* WINED3DSIH_ENDIF */ shader_glsl_end,
8380 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
8381 /* WINED3DSIH_ENDREP */ shader_glsl_end,
8382 /* WINED3DSIH_EQ */ shader_glsl_relop,
8383 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
8384 /* WINED3DSIH_EXPP */ shader_glsl_expp,
8385 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
8386 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
8387 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
8388 /* WINED3DSIH_GE */ shader_glsl_relop,
8389 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ NULL,
8390 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
8391 /* WINED3DSIH_HS_FORK_PHASE */ NULL,
8392 /* WINED3DSIH_HS_JOIN_PHASE */ NULL,
8393 /* WINED3DSIH_IADD */ shader_glsl_binop,
8394 /* WINED3DSIH_IEQ */ shader_glsl_relop,
8395 /* WINED3DSIH_IF */ shader_glsl_if,
8396 /* WINED3DSIH_IFC */ shader_glsl_ifc,
8397 /* WINED3DSIH_IGE */ shader_glsl_relop,
8398 /* WINED3DSIH_ILT */ shader_glsl_relop,
8399 /* WINED3DSIH_IMAD */ shader_glsl_mad,
8400 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
8401 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
8402 /* WINED3DSIH_IMUL */ shader_glsl_imul,
8403 /* WINED3DSIH_INE */ shader_glsl_relop,
8404 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
8405 /* WINED3DSIH_ISHL */ shader_glsl_binop,
8406 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
8407 /* WINED3DSIH_LABEL */ shader_glsl_label,
8408 /* WINED3DSIH_LD */ shader_glsl_ld,
8409 /* WINED3DSIH_LD2DMS */ NULL,
8410 /* WINED3DSIH_LD_STRUCTURED */ NULL,
8411 /* WINED3DSIH_LIT */ shader_glsl_lit,
8412 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
8413 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
8414 /* WINED3DSIH_LOOP */ shader_glsl_loop,
8415 /* WINED3DSIH_LRP */ shader_glsl_lrp,
8416 /* WINED3DSIH_LT */ shader_glsl_relop,
8417 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
8418 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
8419 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
8420 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
8421 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
8422 /* WINED3DSIH_MAD */ shader_glsl_mad,
8423 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
8424 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
8425 /* WINED3DSIH_MOV */ shader_glsl_mov,
8426 /* WINED3DSIH_MOVA */ shader_glsl_mov,
8427 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
8428 /* WINED3DSIH_MUL */ shader_glsl_binop,
8429 /* WINED3DSIH_NE */ shader_glsl_relop,
8430 /* WINED3DSIH_NOP */ shader_glsl_nop,
8431 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
8432 /* WINED3DSIH_NRM */ shader_glsl_nrm,
8433 /* WINED3DSIH_OR */ shader_glsl_binop,
8434 /* WINED3DSIH_PHASE */ shader_glsl_nop,
8435 /* WINED3DSIH_POW */ shader_glsl_pow,
8436 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
8437 /* WINED3DSIH_REP */ shader_glsl_rep,
8438 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
8439 /* WINED3DSIH_RET */ shader_glsl_ret,
8440 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
8441 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
8442 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
8443 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
8444 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
8445 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
8446 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
8447 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
8448 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
8449 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
8450 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
8451 /* WINED3DSIH_SETP */ NULL,
8452 /* WINED3DSIH_SGE */ shader_glsl_compare,
8453 /* WINED3DSIH_SGN */ shader_glsl_sgn,
8454 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
8455 /* WINED3DSIH_SLT */ shader_glsl_compare,
8456 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
8457 /* WINED3DSIH_STORE_UAV_TYPED */ NULL,
8458 /* WINED3DSIH_SUB */ shader_glsl_binop,
8459 /* WINED3DSIH_TEX */ shader_glsl_tex,
8460 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
8461 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
8462 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
8463 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
8464 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
8465 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
8466 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
8467 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
8468 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
8469 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
8470 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
8471 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
8472 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
8473 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
8474 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
8475 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
8476 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
8477 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
8478 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
8479 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
8480 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
8481 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
8482 /* WINED3DSIH_UGE */ shader_glsl_relop,
8483 /* WINED3DSIH_ULT */ shader_glsl_relop,
8484 /* WINED3DSIH_USHR */ shader_glsl_binop,
8485 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
8486 /* WINED3DSIH_XOR */ shader_glsl_binop,
8489 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
8490 SHADER_HANDLER hw_fct;
8492 /* Select handler */
8493 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
8495 /* Unhandled opcode */
8496 if (!hw_fct)
8498 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
8499 return;
8501 hw_fct(ins);
8503 shader_glsl_add_instruction_modifiers(ins);
8506 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
8508 struct shader_glsl_priv *priv = shader_priv;
8510 return priv->ffp_proj_control;
8513 const struct wined3d_shader_backend_ops glsl_shader_backend =
8515 shader_glsl_handle_instruction,
8516 shader_glsl_select,
8517 shader_glsl_disable,
8518 shader_glsl_select_depth_blt,
8519 shader_glsl_deselect_depth_blt,
8520 shader_glsl_update_float_vertex_constants,
8521 shader_glsl_update_float_pixel_constants,
8522 shader_glsl_load_constants,
8523 shader_glsl_destroy,
8524 shader_glsl_alloc,
8525 shader_glsl_free,
8526 shader_glsl_allocate_context_data,
8527 shader_glsl_free_context_data,
8528 shader_glsl_init_context_state,
8529 shader_glsl_get_caps,
8530 shader_glsl_color_fixup_supported,
8531 shader_glsl_has_ffp_proj_control,
8534 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
8536 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
8538 caps->xyzrhw = TRUE;
8539 caps->emulated_flatshading = !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
8540 caps->ffp_generic_attributes = TRUE;
8541 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
8542 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
8543 caps->max_vertex_blend_matrix_index = 0;
8544 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
8545 | WINED3DVTXPCAPS_MATERIALSOURCE7
8546 | WINED3DVTXPCAPS_VERTEXFOG
8547 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
8548 | WINED3DVTXPCAPS_POSITIONALLIGHTS
8549 | WINED3DVTXPCAPS_LOCALVIEWER
8550 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
8551 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
8552 caps->max_user_clip_planes = gl_info->limits.clipplanes;
8553 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
8556 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
8558 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8559 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8560 return 0;
8563 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8565 struct shader_glsl_priv *priv;
8567 if (shader_backend == &glsl_shader_backend)
8569 priv = shader_priv;
8571 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
8573 ERR("Failed to initialize rbtree.\n");
8574 return NULL;
8577 return priv;
8580 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
8582 return NULL;
8585 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
8587 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8588 struct glsl_ffp_vertex_shader, desc.entry);
8589 struct glsl_shader_prog_link *program, *program2;
8590 struct glsl_ffp_destroy_ctx *ctx = context;
8592 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8593 struct glsl_shader_prog_link, vs.shader_entry)
8595 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8597 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8598 HeapFree(GetProcessHeap(), 0, shader);
8601 /* Context activation is done by the caller. */
8602 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
8604 struct shader_glsl_priv *priv = device->vertex_priv;
8605 struct glsl_ffp_destroy_ctx ctx;
8607 ctx.priv = priv;
8608 ctx.gl_info = &device->adapter->gl_info;
8609 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
8612 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
8613 const struct wined3d_state *state, DWORD state_id) {}
8615 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
8616 const struct wined3d_state *state, DWORD state_id)
8618 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8621 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
8622 const struct wined3d_state *state, DWORD state_id)
8624 const struct wined3d_gl_info *gl_info = context->gl_info;
8625 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
8626 BOOL transformed = context->stream_info.position_transformed;
8627 BOOL wasrhw = context->last_was_rhw;
8628 unsigned int i;
8630 context->last_was_rhw = transformed;
8632 /* If the vertex declaration contains a transformed position attribute,
8633 * the draw uses the fixed function vertex pipeline regardless of any
8634 * vertex shader set by the application. */
8635 if (transformed != wasrhw)
8636 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8638 if (!use_vs(state))
8640 if (context->last_was_vshader)
8642 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8643 clipplane(context, state, STATE_CLIPPLANE(i));
8646 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8648 /* Because of settings->texcoords, we have to regenerate the vertex
8649 * shader on a vdecl change if there aren't enough varyings to just
8650 * always output all the texture coordinates. */
8651 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
8652 || normal != context->last_was_normal)
8653 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8655 if (use_ps(state)
8656 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
8657 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
8658 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8660 else
8662 if (!context->last_was_vshader)
8664 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
8665 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8666 clipplane(context, state, STATE_CLIPPLANE(i));
8670 context->last_was_vshader = use_vs(state);
8671 context->last_was_normal = normal;
8674 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
8675 const struct wined3d_state *state, DWORD state_id)
8677 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8678 /* Different vertex shaders potentially require a different vertex attributes setup. */
8679 if (!isStateDirty(context, STATE_VDECL))
8680 context_apply_state(context, state, STATE_VDECL);
8683 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
8684 const struct wined3d_state *state, DWORD state_id)
8686 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
8687 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
8688 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8691 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
8692 const struct wined3d_state *state, DWORD state_id)
8694 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
8695 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
8696 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
8697 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
8698 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8701 static void glsl_vertex_pipe_world(struct wined3d_context *context,
8702 const struct wined3d_state *state, DWORD state_id)
8704 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
8707 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
8708 const struct wined3d_state *state, DWORD state_id)
8710 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8713 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
8715 const struct wined3d_gl_info *gl_info = context->gl_info;
8716 unsigned int k;
8718 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
8719 | WINED3D_SHADER_CONST_FFP_LIGHTS
8720 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8722 for (k = 0; k < gl_info->limits.clipplanes; ++k)
8724 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
8725 clipplane(context, state, STATE_CLIPPLANE(k));
8729 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
8730 const struct wined3d_state *state, DWORD state_id)
8732 /* Table fog behavior depends on the projection matrix. */
8733 if (state->render_states[WINED3D_RS_FOGENABLE]
8734 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
8735 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8736 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
8739 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
8740 const struct wined3d_state *state, DWORD state_id)
8742 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
8743 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
8744 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
8745 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
8746 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8747 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
8750 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
8751 const struct wined3d_state *state, DWORD state_id)
8753 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8756 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
8757 const struct wined3d_state *state, DWORD state_id)
8759 DWORD sampler = state_id - STATE_SAMPLER(0);
8760 const struct wined3d_texture *texture = state->textures[sampler];
8761 BOOL np2;
8763 if (!texture)
8764 return;
8766 if (sampler >= MAX_TEXTURES)
8767 return;
8769 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
8770 || context->lastWasPow2Texture & (1u << sampler))
8772 if (np2)
8773 context->lastWasPow2Texture |= 1u << sampler;
8774 else
8775 context->lastWasPow2Texture &= ~(1u << sampler);
8777 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8781 static void glsl_vertex_pipe_material(struct wined3d_context *context,
8782 const struct wined3d_state *state, DWORD state_id)
8784 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
8787 static void glsl_vertex_pipe_light(struct wined3d_context *context,
8788 const struct wined3d_state *state, DWORD state_id)
8790 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
8793 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
8794 const struct wined3d_state *state, DWORD state_id)
8796 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8799 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
8800 const struct wined3d_state *state, DWORD state_id)
8802 if (!use_vs(state))
8803 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8806 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
8807 const struct wined3d_state *state, DWORD state_id)
8809 static unsigned int once;
8811 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
8812 FIXME("Non-point sprite points not supported in core profile.\n");
8815 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
8816 const struct wined3d_state *state, DWORD state_id)
8818 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
8821 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
8823 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8824 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
8825 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
8826 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
8827 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
8828 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
8829 /* Clip planes */
8830 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
8831 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
8832 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
8833 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
8834 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
8835 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
8836 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
8837 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
8838 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
8839 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
8840 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
8841 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
8842 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
8843 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
8844 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
8845 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
8846 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
8847 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
8848 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
8849 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
8850 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
8851 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
8852 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
8853 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
8854 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
8855 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
8856 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
8857 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
8858 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
8859 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
8860 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
8861 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
8862 /* Lights */
8863 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8864 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8865 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8866 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8867 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8868 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8869 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8870 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8871 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8872 /* Viewport */
8873 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
8874 /* Transform states */
8875 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
8876 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
8877 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8878 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8879 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8880 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8881 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8882 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8883 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8884 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8885 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
8886 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8887 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8888 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8889 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8890 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8891 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8892 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8893 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8894 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8895 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8896 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8897 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8898 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8899 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8900 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8901 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8902 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8903 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8904 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8905 /* Fog */
8906 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8907 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8908 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8909 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8910 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
8911 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
8912 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8913 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8914 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8915 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8916 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8917 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8918 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8919 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8920 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8921 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8922 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
8923 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
8924 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
8925 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
8926 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
8927 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
8928 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8929 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8930 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8931 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
8932 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8933 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8934 /* NP2 texture matrix fixups. They are not needed if
8935 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
8936 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
8937 * matrix. */
8938 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8939 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8940 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8941 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8942 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8943 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8944 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8945 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8946 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8947 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8948 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8949 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8950 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8951 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8952 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8953 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8954 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8955 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8956 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8957 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8958 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8959 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8960 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8961 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8962 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8963 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_LEGACY_CONTEXT },
8964 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GL_EXT_NONE },
8965 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
8968 /* TODO:
8969 * - Implement vertex tweening. */
8970 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
8972 glsl_vertex_pipe_vp_enable,
8973 glsl_vertex_pipe_vp_get_caps,
8974 glsl_vertex_pipe_vp_get_emul_mask,
8975 glsl_vertex_pipe_vp_alloc,
8976 glsl_vertex_pipe_vp_free,
8977 glsl_vertex_pipe_vp_states,
8980 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
8982 /* Nothing to do. */
8985 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
8987 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
8988 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
8989 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
8990 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
8991 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
8992 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
8993 | WINED3DTEXOPCAPS_SELECTARG1
8994 | WINED3DTEXOPCAPS_SELECTARG2
8995 | WINED3DTEXOPCAPS_MODULATE4X
8996 | WINED3DTEXOPCAPS_MODULATE2X
8997 | WINED3DTEXOPCAPS_MODULATE
8998 | WINED3DTEXOPCAPS_ADDSIGNED2X
8999 | WINED3DTEXOPCAPS_ADDSIGNED
9000 | WINED3DTEXOPCAPS_ADD
9001 | WINED3DTEXOPCAPS_SUBTRACT
9002 | WINED3DTEXOPCAPS_ADDSMOOTH
9003 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
9004 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
9005 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
9006 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
9007 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
9008 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
9009 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
9010 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
9011 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
9012 | WINED3DTEXOPCAPS_DOTPRODUCT3
9013 | WINED3DTEXOPCAPS_MULTIPLYADD
9014 | WINED3DTEXOPCAPS_LERP
9015 | WINED3DTEXOPCAPS_BUMPENVMAP
9016 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
9017 caps->MaxTextureBlendStages = 8;
9018 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
9021 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
9023 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9024 return GL_EXT_EMUL_ARB_MULTITEXTURE;
9025 return 0;
9028 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
9030 struct shader_glsl_priv *priv;
9032 if (shader_backend == &glsl_shader_backend)
9034 priv = shader_priv;
9036 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
9038 ERR("Failed to initialize rbtree.\n");
9039 return NULL;
9042 return priv;
9045 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
9047 return NULL;
9050 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
9052 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
9053 struct glsl_ffp_fragment_shader, entry.entry);
9054 struct glsl_shader_prog_link *program, *program2;
9055 struct glsl_ffp_destroy_ctx *ctx = context;
9057 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
9058 struct glsl_shader_prog_link, ps.shader_entry)
9060 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
9062 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
9063 HeapFree(GetProcessHeap(), 0, shader);
9066 /* Context activation is done by the caller. */
9067 static void glsl_fragment_pipe_free(struct wined3d_device *device)
9069 struct shader_glsl_priv *priv = device->fragment_priv;
9070 struct glsl_ffp_destroy_ctx ctx;
9072 ctx.priv = priv;
9073 ctx.gl_info = &device->adapter->gl_info;
9074 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
9077 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
9078 const struct wined3d_state *state, DWORD state_id)
9080 context->last_was_pshader = use_ps(state);
9082 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9085 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
9086 const struct wined3d_state *state, DWORD state_id)
9088 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
9091 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
9092 const struct wined3d_state *state, DWORD state_id)
9094 BOOL use_vshader = use_vs(state);
9095 enum fogsource new_source;
9096 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
9097 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
9099 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9101 if (!state->render_states[WINED3D_RS_FOGENABLE])
9102 return;
9104 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
9106 if (use_vshader)
9107 new_source = FOGSOURCE_VS;
9108 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
9109 new_source = FOGSOURCE_COORD;
9110 else
9111 new_source = FOGSOURCE_FFP;
9113 else
9115 new_source = FOGSOURCE_FFP;
9118 if (new_source != context->fog_source || fogstart == fogend)
9120 context->fog_source = new_source;
9121 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
9125 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
9126 const struct wined3d_state *state, DWORD state_id)
9128 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
9129 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
9130 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9132 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
9133 glsl_fragment_pipe_fog(context, state, state_id);
9136 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
9137 const struct wined3d_state *state, DWORD state_id)
9139 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
9140 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
9141 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9144 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
9145 const struct wined3d_state *state, DWORD state_id)
9147 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9150 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
9151 const struct wined3d_state *state, DWORD state_id)
9153 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
9156 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
9157 const struct wined3d_state *state, DWORD state_id)
9159 const struct wined3d_gl_info *gl_info = context->gl_info;
9160 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
9161 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
9163 if (func)
9165 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
9166 checkGLcall("glAlphaFunc");
9170 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
9171 const struct wined3d_state *state, DWORD state_id)
9173 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9176 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
9177 const struct wined3d_state *state, DWORD state_id)
9179 const struct wined3d_gl_info *gl_info = context->gl_info;
9181 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
9183 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
9184 checkGLcall("glEnable(GL_ALPHA_TEST)");
9186 else
9188 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
9189 checkGLcall("glDisable(GL_ALPHA_TEST)");
9193 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
9194 const struct wined3d_state *state, DWORD state_id)
9196 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
9199 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
9200 const struct wined3d_state *state, DWORD state_id)
9202 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
9205 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
9206 const struct wined3d_state *state, DWORD state_id)
9208 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
9211 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
9213 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
9214 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
9215 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9216 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9217 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9218 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9219 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9220 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9221 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9222 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9223 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9224 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9225 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9226 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9227 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9228 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9229 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9230 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9231 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9232 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9233 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9234 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9235 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9236 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9237 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9238 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9239 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9240 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9241 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9242 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9243 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9244 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9245 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9246 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9247 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9248 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9249 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9250 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9251 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9252 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9253 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9254 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9255 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9256 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9257 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9258 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9259 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9260 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9261 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9262 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9263 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9264 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9265 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9266 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9267 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9268 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9269 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9270 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9271 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9272 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9273 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9274 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9275 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9276 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9277 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9278 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9279 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9280 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9281 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9282 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9283 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9284 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9285 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9286 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9287 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9288 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9289 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
9290 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
9291 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
9292 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
9293 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
9294 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
9295 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9296 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
9297 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
9298 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9299 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9300 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9301 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
9302 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
9303 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9304 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9305 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9306 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
9307 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
9308 {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 },
9309 {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 },
9310 {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 },
9311 {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 },
9312 {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 },
9313 {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 },
9314 {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 },
9315 {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 },
9316 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9317 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9318 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9319 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9320 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9321 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9322 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9323 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9324 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9325 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9326 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_LEGACY_CONTEXT},
9327 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GL_EXT_NONE },
9328 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
9331 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
9333 return TRUE;
9336 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
9340 const struct fragment_pipeline glsl_fragment_pipe =
9342 glsl_fragment_pipe_enable,
9343 glsl_fragment_pipe_get_caps,
9344 glsl_fragment_pipe_get_emul_mask,
9345 glsl_fragment_pipe_alloc,
9346 glsl_fragment_pipe_free,
9347 glsl_fragment_pipe_alloc_context_data,
9348 glsl_fragment_pipe_free_context_data,
9349 shader_glsl_color_fixup_supported,
9350 glsl_fragment_pipe_state_template,