wined3d: Pass a wined3d_vec4 structure to apply_clamped_constant().
[wine.git] / dlls / wined3d / glsl_shader.c
blob8c3958db052ee44946a931e7cc0439e4a17ffd2d
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;
121 GLint uniform_i_locations[MAX_CONST_I];
122 GLint uniform_b_locations[MAX_CONST_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;
169 GLint uniform_i_locations[MAX_CONST_I];
170 GLint uniform_b_locations[MAX_CONST_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 ycorrection_location;
182 GLint np2_fixup_location;
183 GLint color_key_location;
184 const struct ps_np2fixup_info *np2_fixup_info;
187 /* Struct to maintain data about a linked GLSL program */
188 struct glsl_shader_prog_link
190 struct wine_rb_entry program_lookup_entry;
191 struct glsl_vs_program vs;
192 struct glsl_gs_program gs;
193 struct glsl_ps_program ps;
194 GLuint id;
195 DWORD constant_update_mask;
196 UINT constant_version;
199 struct glsl_program_key
201 GLuint vs_id;
202 GLuint gs_id;
203 GLuint ps_id;
206 struct shader_glsl_ctx_priv {
207 const struct vs_compile_args *cur_vs_args;
208 const struct ps_compile_args *cur_ps_args;
209 struct ps_np2fixup_info *cur_np2fixup_info;
210 struct wined3d_string_buffer_list *string_buffers;
213 struct glsl_context_data
215 struct glsl_shader_prog_link *glsl_program;
218 struct glsl_ps_compiled_shader
220 struct ps_compile_args args;
221 struct ps_np2fixup_info np2fixup;
222 GLuint id;
225 struct glsl_vs_compiled_shader
227 struct vs_compile_args args;
228 GLuint id;
231 struct glsl_gs_compiled_shader
233 GLuint id;
236 struct glsl_shader_private
238 union
240 struct glsl_vs_compiled_shader *vs;
241 struct glsl_gs_compiled_shader *gs;
242 struct glsl_ps_compiled_shader *ps;
243 } gl_shaders;
244 UINT num_gl_shaders, shader_array_size;
247 struct glsl_ffp_vertex_shader
249 struct wined3d_ffp_vs_desc desc;
250 GLuint id;
251 struct list linked_programs;
254 struct glsl_ffp_fragment_shader
256 struct ffp_frag_desc entry;
257 GLuint id;
258 struct list linked_programs;
261 struct glsl_ffp_destroy_ctx
263 struct shader_glsl_priv *priv;
264 const struct wined3d_gl_info *gl_info;
267 static const char *debug_gl_shader_type(GLenum type)
269 switch (type)
271 #define WINED3D_TO_STR(u) case u: return #u
272 WINED3D_TO_STR(GL_VERTEX_SHADER);
273 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
274 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
275 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
276 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
277 #undef WINED3D_TO_STR
278 default:
279 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
283 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
285 switch (type)
287 case WINED3D_SHADER_TYPE_VERTEX:
288 return "vs";
290 case WINED3D_SHADER_TYPE_HULL:
291 return "hs";
293 case WINED3D_SHADER_TYPE_DOMAIN:
294 return "ds";
296 case WINED3D_SHADER_TYPE_GEOMETRY:
297 return "gs";
299 case WINED3D_SHADER_TYPE_PIXEL:
300 return "ps";
302 default:
303 FIXME("Unhandled shader type %#x.\n", type);
304 return "unknown";
308 static const char *shader_glsl_get_version(const struct wined3d_gl_info *gl_info,
309 const struct wined3d_shader_version *version)
311 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
312 return "#version 150";
313 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30) && version && version->major >= 4)
314 return "#version 130";
315 else
316 return "#version 120";
319 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
321 char str[4][17];
323 wined3d_ftoa(values[0], str[0]);
324 wined3d_ftoa(values[1], str[1]);
325 wined3d_ftoa(values[2], str[2]);
326 wined3d_ftoa(values[3], str[3]);
327 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
330 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
331 const int *values, unsigned int size)
333 int i;
335 if (!size || size > 4)
337 ERR("Invalid vector size %u.\n", size);
338 return;
341 if (size > 1)
342 shader_addline(buffer, "ivec%u(", size);
344 for (i = 0; i < size; ++i)
345 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
347 if (size > 1)
348 shader_addline(buffer, ")");
351 static const char *get_info_log_line(const char **ptr)
353 const char *p, *q;
355 p = *ptr;
356 if (!(q = strstr(p, "\n")))
358 if (!*p) return NULL;
359 *ptr += strlen(p);
360 return p;
362 *ptr = q + 1;
364 return p;
367 /* Context activation is done by the caller. */
368 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
370 int length = 0;
371 char *log;
373 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
374 return;
376 if (program)
377 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
378 else
379 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
381 /* A size of 1 is just a null-terminated string, so the log should be bigger than
382 * that if there are errors. */
383 if (length > 1)
385 const char *ptr, *line;
387 log = HeapAlloc(GetProcessHeap(), 0, length);
388 /* The info log is supposed to be zero-terminated, but at least some
389 * versions of fglrx don't terminate the string properly. The reported
390 * length does include the terminator, so explicitly set it to zero
391 * here. */
392 log[length - 1] = 0;
393 if (program)
394 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
395 else
396 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
398 ptr = log;
399 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
401 WARN("Info log received from GLSL shader #%u:\n", id);
402 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
404 else
406 FIXME("Info log received from GLSL shader #%u:\n", id);
407 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
409 HeapFree(GetProcessHeap(), 0, log);
413 /* Context activation is done by the caller. */
414 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
416 const char *ptr, *line;
418 TRACE("Compiling shader object %u.\n", shader);
420 if (TRACE_ON(d3d_shader))
422 ptr = src;
423 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
426 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
427 checkGLcall("glShaderSource");
428 GL_EXTCALL(glCompileShader(shader));
429 checkGLcall("glCompileShader");
430 print_glsl_info_log(gl_info, shader, FALSE);
433 /* Context activation is done by the caller. */
434 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
436 GLint i, shader_count, source_size = -1;
437 GLuint *shaders;
438 char *source = NULL;
440 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
441 shaders = HeapAlloc(GetProcessHeap(), 0, shader_count * sizeof(*shaders));
442 if (!shaders)
444 ERR("Failed to allocate shader array memory.\n");
445 return;
448 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
449 for (i = 0; i < shader_count; ++i)
451 const char *ptr, *line;
452 GLint tmp;
454 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
456 if (source_size < tmp)
458 HeapFree(GetProcessHeap(), 0, source);
460 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
461 if (!source)
463 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
464 HeapFree(GetProcessHeap(), 0, shaders);
465 return;
467 source_size = tmp;
470 FIXME("Shader %u:\n", shaders[i]);
471 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
472 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
473 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
474 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
475 FIXME("\n");
477 ptr = source;
478 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
479 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
480 FIXME("\n");
483 HeapFree(GetProcessHeap(), 0, source);
484 HeapFree(GetProcessHeap(), 0, shaders);
487 /* Context activation is done by the caller. */
488 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
490 GLint tmp;
492 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
493 return;
495 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
496 if (!tmp)
498 FIXME("Program %u link status invalid.\n", program);
499 shader_glsl_dump_program_source(gl_info, program);
502 print_glsl_info_log(gl_info, program, TRUE);
505 /* Context activation is done by the caller. */
506 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
507 struct shader_glsl_priv *priv, const DWORD *tex_unit_map, GLuint program_id)
509 unsigned int mapped_unit;
510 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
511 const char *prefix;
512 unsigned int i, j;
513 GLint name_loc;
515 static const struct
517 enum wined3d_shader_type type;
518 unsigned int base_idx;
519 unsigned int count;
521 sampler_info[] =
523 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
524 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
527 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
529 prefix = shader_glsl_get_prefix(sampler_info[i].type);
531 for (j = 0; j < sampler_info[i].count; ++j)
533 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, j);
534 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
535 if (name_loc == -1)
536 continue;
538 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
539 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
541 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
542 continue;
545 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
546 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
549 checkGLcall("glUniform1i");
550 string_buffer_release(&priv->string_buffers, sampler_name);
553 /* Context activation is done by the caller. */
554 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
555 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
557 unsigned int start = ~0U, end = 0;
558 int stack_idx = 0;
559 unsigned int heap_idx = 1;
560 unsigned int idx;
562 if (heap->entries[heap_idx].version <= version) return;
564 idx = heap->entries[heap_idx].idx;
565 if (constant_locations[idx] != -1)
566 start = end = idx;
567 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
569 while (stack_idx >= 0)
571 /* Note that we fall through to the next case statement. */
572 switch(stack[stack_idx])
574 case HEAP_NODE_TRAVERSE_LEFT:
576 unsigned int left_idx = heap_idx << 1;
577 if (left_idx < heap->size && heap->entries[left_idx].version > version)
579 heap_idx = left_idx;
580 idx = heap->entries[heap_idx].idx;
581 if (constant_locations[idx] != -1)
583 if (start > idx)
584 start = idx;
585 if (end < idx)
586 end = idx;
589 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
590 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
591 break;
595 case HEAP_NODE_TRAVERSE_RIGHT:
597 unsigned int right_idx = (heap_idx << 1) + 1;
598 if (right_idx < heap->size && heap->entries[right_idx].version > version)
600 heap_idx = right_idx;
601 idx = heap->entries[heap_idx].idx;
602 if (constant_locations[idx] != -1)
604 if (start > idx)
605 start = idx;
606 if (end < idx)
607 end = idx;
610 stack[stack_idx++] = HEAP_NODE_POP;
611 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
612 break;
616 case HEAP_NODE_POP:
617 heap_idx >>= 1;
618 --stack_idx;
619 break;
622 if (start <= end)
623 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start * 4]));
624 checkGLcall("walk_constant_heap()");
627 /* Context activation is done by the caller. */
628 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
629 GLint location, const struct wined3d_vec4 *data)
631 GLfloat clamped_constant[4];
633 if (location == -1) return;
635 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
636 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
637 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
638 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
640 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
643 /* Context activation is done by the caller. */
644 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
645 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
647 int stack_idx = 0;
648 unsigned int heap_idx = 1;
649 unsigned int idx;
651 if (heap->entries[heap_idx].version <= version) return;
653 idx = heap->entries[heap_idx].idx;
654 apply_clamped_constant(gl_info, constant_locations[idx], (const struct wined3d_vec4 *)&constants[idx * 4]);
655 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
657 while (stack_idx >= 0)
659 /* Note that we fall through to the next case statement. */
660 switch(stack[stack_idx])
662 case HEAP_NODE_TRAVERSE_LEFT:
664 unsigned int left_idx = heap_idx << 1;
665 if (left_idx < heap->size && heap->entries[left_idx].version > version)
667 heap_idx = left_idx;
668 idx = heap->entries[heap_idx].idx;
669 apply_clamped_constant(gl_info, constant_locations[idx],
670 (const struct wined3d_vec4 *)&constants[idx * 4]);
672 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
673 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
674 break;
678 case HEAP_NODE_TRAVERSE_RIGHT:
680 unsigned int right_idx = (heap_idx << 1) + 1;
681 if (right_idx < heap->size && heap->entries[right_idx].version > version)
683 heap_idx = right_idx;
684 idx = heap->entries[heap_idx].idx;
685 apply_clamped_constant(gl_info, constant_locations[idx],
686 (const struct wined3d_vec4 *)&constants[idx * 4]);
688 stack[stack_idx++] = HEAP_NODE_POP;
689 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
690 break;
694 case HEAP_NODE_POP:
695 heap_idx >>= 1;
696 --stack_idx;
697 break;
700 checkGLcall("walk_constant_heap_clamped()");
703 /* Context activation is done by the caller. */
704 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
705 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
706 unsigned char *stack, UINT version)
708 const struct wined3d_shader_lconst *lconst;
710 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
711 if (shader->reg_maps.shader_version.major == 1
712 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
713 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
714 else
715 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
717 if (!shader->load_local_constsF)
719 TRACE("No need to load local float constants for this shader\n");
720 return;
723 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
724 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
726 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
728 checkGLcall("glUniform4fv()");
731 /* Context activation is done by the caller. */
732 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
733 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
735 unsigned int i;
736 struct list* ptr;
738 for (i = 0; constants_set; constants_set >>= 1, ++i)
740 if (!(constants_set & 1)) continue;
742 /* We found this uniform name in the program - go ahead and send the data */
743 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i * 4]));
746 /* Load immediate constants */
747 ptr = list_head(&shader->constantsI);
748 while (ptr)
750 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
751 unsigned int idx = lconst->idx;
752 const GLint *values = (const GLint *)lconst->value;
754 /* We found this uniform name in the program - go ahead and send the data */
755 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
756 ptr = list_next(&shader->constantsI, ptr);
758 checkGLcall("glUniform4iv()");
761 /* Context activation is done by the caller. */
762 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
763 const GLint locations[MAX_CONST_B], const BOOL *constants, WORD constants_set)
765 unsigned int i;
766 struct list* ptr;
768 for (i = 0; constants_set; constants_set >>= 1, ++i)
770 if (!(constants_set & 1)) continue;
772 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
775 /* Load immediate constants */
776 ptr = list_head(&shader->constantsB);
777 while (ptr)
779 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
780 unsigned int idx = lconst->idx;
781 const GLint *values = (const GLint *)lconst->value;
783 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
784 ptr = list_next(&shader->constantsB, ptr);
786 checkGLcall("glUniform1iv()");
789 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
791 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
794 /* Context activation is done by the caller (state handler). */
795 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
796 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
798 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
799 UINT fixup = ps->np2_fixup_info->active;
800 UINT i;
802 for (i = 0; fixup; fixup >>= 1, ++i)
804 const struct wined3d_texture *tex = state->textures[i];
805 unsigned char idx = ps->np2_fixup_info->idx[i];
806 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
808 if (!tex)
810 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
811 continue;
814 if (idx % 2)
816 tex_dim[2] = tex->pow2_matrix[0];
817 tex_dim[3] = tex->pow2_matrix[5];
819 else
821 tex_dim[0] = tex->pow2_matrix[0];
822 tex_dim[1] = tex->pow2_matrix[5];
826 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
829 /* Taken and adapted from Mesa. */
830 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
832 float pos, neg, t, det;
833 struct wined3d_matrix temp;
835 /* Calculate the determinant of upper left 3x3 submatrix and
836 * determine if the matrix is singular. */
837 pos = neg = 0.0f;
838 t = in->_11 * in->_22 * in->_33;
839 if (t >= 0.0f)
840 pos += t;
841 else
842 neg += t;
844 t = in->_21 * in->_32 * in->_13;
845 if (t >= 0.0f)
846 pos += t;
847 else
848 neg += t;
849 t = in->_31 * in->_12 * in->_23;
850 if (t >= 0.0f)
851 pos += t;
852 else
853 neg += t;
855 t = -in->_31 * in->_22 * in->_13;
856 if (t >= 0.0f)
857 pos += t;
858 else
859 neg += t;
860 t = -in->_21 * in->_12 * in->_33;
861 if (t >= 0.0f)
862 pos += t;
863 else
864 neg += t;
866 t = -in->_11 * in->_32 * in->_23;
867 if (t >= 0.0f)
868 pos += t;
869 else
870 neg += t;
872 det = pos + neg;
874 if (fabsf(det) < 1e-25f)
875 return FALSE;
877 det = 1.0f / det;
878 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
879 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
880 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
881 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
882 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
883 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
884 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
885 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
886 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
888 *out = temp;
889 return TRUE;
892 static void swap_rows(float **a, float **b)
894 float *tmp = *a;
896 *a = *b;
897 *b = tmp;
900 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
902 float wtmp[4][8];
903 float m0, m1, m2, m3, s;
904 float *r0, *r1, *r2, *r3;
906 r0 = wtmp[0];
907 r1 = wtmp[1];
908 r2 = wtmp[2];
909 r3 = wtmp[3];
911 r0[0] = m->_11;
912 r0[1] = m->_12;
913 r0[2] = m->_13;
914 r0[3] = m->_14;
915 r0[4] = 1.0f;
916 r0[5] = r0[6] = r0[7] = 0.0f;
918 r1[0] = m->_21;
919 r1[1] = m->_22;
920 r1[2] = m->_23;
921 r1[3] = m->_24;
922 r1[5] = 1.0f;
923 r1[4] = r1[6] = r1[7] = 0.0f;
925 r2[0] = m->_31;
926 r2[1] = m->_32;
927 r2[2] = m->_33;
928 r2[3] = m->_34;
929 r2[6] = 1.0f;
930 r2[4] = r2[5] = r2[7] = 0.0f;
932 r3[0] = m->_41;
933 r3[1] = m->_42;
934 r3[2] = m->_43;
935 r3[3] = m->_44;
936 r3[7] = 1.0f;
937 r3[4] = r3[5] = r3[6] = 0.0f;
939 /* Choose pivot - or die. */
940 if (fabsf(r3[0]) > fabsf(r2[0]))
941 swap_rows(&r3, &r2);
942 if (fabsf(r2[0]) > fabsf(r1[0]))
943 swap_rows(&r2, &r1);
944 if (fabsf(r1[0]) > fabsf(r0[0]))
945 swap_rows(&r1, &r0);
946 if (r0[0] == 0.0f)
947 return FALSE;
949 /* Eliminate first variable. */
950 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
951 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
952 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
953 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
954 s = r0[4];
955 if (s != 0.0f)
957 r1[4] -= m1 * s;
958 r2[4] -= m2 * s;
959 r3[4] -= m3 * s;
961 s = r0[5];
962 if (s != 0.0f)
964 r1[5] -= m1 * s;
965 r2[5] -= m2 * s;
966 r3[5] -= m3 * s;
968 s = r0[6];
969 if (s != 0.0f)
971 r1[6] -= m1 * s;
972 r2[6] -= m2 * s;
973 r3[6] -= m3 * s;
975 s = r0[7];
976 if (s != 0.0f)
978 r1[7] -= m1 * s;
979 r2[7] -= m2 * s;
980 r3[7] -= m3 * s;
983 /* Choose pivot - or die. */
984 if (fabsf(r3[1]) > fabsf(r2[1]))
985 swap_rows(&r3, &r2);
986 if (fabsf(r2[1]) > fabsf(r1[1]))
987 swap_rows(&r2, &r1);
988 if (r1[1] == 0.0f)
989 return FALSE;
991 /* Eliminate second variable. */
992 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
993 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
994 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
995 s = r1[4];
996 if (s != 0.0f)
998 r2[4] -= m2 * s;
999 r3[4] -= m3 * s;
1001 s = r1[5];
1002 if (s != 0.0f)
1004 r2[5] -= m2 * s;
1005 r3[5] -= m3 * s;
1007 s = r1[6];
1008 if (s != 0.0f)
1010 r2[6] -= m2 * s;
1011 r3[6] -= m3 * s;
1013 s = r1[7];
1014 if (s != 0.0f)
1016 r2[7] -= m2 * s;
1017 r3[7] -= m3 * s;
1020 /* Choose pivot - or die. */
1021 if (fabsf(r3[2]) > fabsf(r2[2]))
1022 swap_rows(&r3, &r2);
1023 if (r2[2] == 0.0f)
1024 return FALSE;
1026 /* Eliminate third variable. */
1027 m3 = r3[2] / r2[2];
1028 r3[3] -= m3 * r2[3];
1029 r3[4] -= m3 * r2[4];
1030 r3[5] -= m3 * r2[5];
1031 r3[6] -= m3 * r2[6];
1032 r3[7] -= m3 * r2[7];
1034 /* Last check. */
1035 if (r3[3] == 0.0f)
1036 return FALSE;
1038 /* Back substitute row 3. */
1039 s = 1.0f / r3[3];
1040 r3[4] *= s;
1041 r3[5] *= s;
1042 r3[6] *= s;
1043 r3[7] *= s;
1045 /* Back substitute row 2. */
1046 m2 = r2[3];
1047 s = 1.0f / r2[2];
1048 r2[4] = s * (r2[4] - r3[4] * m2);
1049 r2[5] = s * (r2[5] - r3[5] * m2);
1050 r2[6] = s * (r2[6] - r3[6] * m2);
1051 r2[7] = s * (r2[7] - r3[7] * m2);
1052 m1 = r1[3];
1053 r1[4] -= r3[4] * m1;
1054 r1[5] -= r3[5] * m1;
1055 r1[6] -= r3[6] * m1;
1056 r1[7] -= r3[7] * m1;
1057 m0 = r0[3];
1058 r0[4] -= r3[4] * m0;
1059 r0[5] -= r3[5] * m0;
1060 r0[6] -= r3[6] * m0;
1061 r0[7] -= r3[7] * m0;
1063 /* Back substitute row 1. */
1064 m1 = r1[2];
1065 s = 1.0f / r1[1];
1066 r1[4] = s * (r1[4] - r2[4] * m1);
1067 r1[5] = s * (r1[5] - r2[5] * m1);
1068 r1[6] = s * (r1[6] - r2[6] * m1);
1069 r1[7] = s * (r1[7] - r2[7] * m1);
1070 m0 = r0[2];
1071 r0[4] -= r2[4] * m0;
1072 r0[5] -= r2[5] * m0;
1073 r0[6] -= r2[6] * m0;
1074 r0[7] -= r2[7] * m0;
1076 /* Back substitute row 0. */
1077 m0 = r0[1];
1078 s = 1.0f / r0[0];
1079 r0[4] = s * (r0[4] - r1[4] * m0);
1080 r0[5] = s * (r0[5] - r1[5] * m0);
1081 r0[6] = s * (r0[6] - r1[6] * m0);
1082 r0[7] = s * (r0[7] - r1[7] * m0);
1084 out->_11 = r0[4];
1085 out->_12 = r0[5];
1086 out->_13 = r0[6];
1087 out->_14 = r0[7];
1088 out->_21 = r1[4];
1089 out->_22 = r1[5];
1090 out->_23 = r1[6];
1091 out->_24 = r1[7];
1092 out->_31 = r2[4];
1093 out->_32 = r2[5];
1094 out->_33 = r2[6];
1095 out->_34 = r2[7];
1096 out->_41 = r3[4];
1097 out->_42 = r3[5];
1098 out->_43 = r3[6];
1099 out->_44 = r3[7];
1101 return TRUE;
1104 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1105 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1107 const struct wined3d_gl_info *gl_info = context->gl_info;
1108 float mat[3 * 3];
1109 struct wined3d_matrix mv;
1110 unsigned int i, j;
1112 if (prog->vs.normal_matrix_location == -1)
1113 return;
1115 get_modelview_matrix(context, state, 0, &mv);
1116 if (context->swapchain->device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING)
1117 invert_matrix_3d(&mv, &mv);
1118 else
1119 invert_matrix(&mv, &mv);
1120 /* Tests show that singular modelview matrices are used unchanged as normal
1121 * matrices on D3D3 and older. There seems to be no clearly consistent
1122 * behavior on newer D3D versions so always follow older ddraw behavior. */
1123 for (i = 0; i < 3; ++i)
1124 for (j = 0; j < 3; ++j)
1125 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1127 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1128 checkGLcall("glUniformMatrix3fv");
1131 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1132 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1134 const struct wined3d_gl_info *gl_info = context->gl_info;
1135 struct wined3d_matrix mat;
1137 if (tex >= MAX_TEXTURES)
1138 return;
1139 if (prog->vs.texture_matrix_location[tex] == -1)
1140 return;
1142 get_texture_matrix(context, state, tex, &mat);
1143 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1144 checkGLcall("glUniformMatrix4fv");
1147 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1148 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1150 const struct wined3d_gl_info *gl_info = context->gl_info;
1152 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1154 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1155 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1157 else
1159 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1161 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1163 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1164 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1165 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1166 checkGLcall("setting FFP material uniforms");
1169 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1170 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1172 const struct wined3d_gl_info *gl_info = context->gl_info;
1173 struct wined3d_color color;
1175 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1176 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1177 checkGLcall("glUniform3fv");
1180 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1181 const struct wined3d_matrix *src2)
1183 struct wined3d_vec4 temp;
1185 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1186 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1187 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1188 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1190 *dest = temp;
1193 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1194 const struct wined3d_state *state, unsigned int light, struct glsl_shader_prog_link *prog)
1196 const struct wined3d_gl_info *gl_info = context->gl_info;
1197 const struct wined3d_light_info *light_info = state->lights[light];
1198 struct wined3d_vec4 vec4;
1199 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1201 if (!light_info)
1202 return;
1204 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1205 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1206 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1208 switch (light_info->OriginalParms.type)
1210 case WINED3D_LIGHT_POINT:
1211 multiply_vector_matrix(&vec4, &light_info->position, view);
1212 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1213 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1214 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1215 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1216 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1217 break;
1219 case WINED3D_LIGHT_SPOT:
1220 multiply_vector_matrix(&vec4, &light_info->position, view);
1221 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1223 multiply_vector_matrix(&vec4, &light_info->direction, view);
1224 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1226 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1227 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1228 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1229 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1230 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1231 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1232 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1233 break;
1235 case WINED3D_LIGHT_DIRECTIONAL:
1236 multiply_vector_matrix(&vec4, &light_info->direction, view);
1237 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1238 break;
1240 case WINED3D_LIGHT_PARALLELPOINT:
1241 multiply_vector_matrix(&vec4, &light_info->position, view);
1242 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1243 break;
1245 default:
1246 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1248 checkGLcall("setting FFP lights uniforms");
1251 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1252 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1254 const struct wined3d_gl_info *gl_info = context->gl_info;
1255 float min, max;
1256 float size, att[3];
1258 get_pointsize_minmax(context, state, &min, &max);
1260 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1261 checkGLcall("glUniform1f");
1262 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1263 checkGLcall("glUniform1f");
1265 get_pointsize(context, state, &size, att);
1267 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1268 checkGLcall("glUniform1f");
1269 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1270 checkGLcall("glUniform1f");
1271 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1272 checkGLcall("glUniform1f");
1273 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1274 checkGLcall("glUniform1f");
1277 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1278 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1280 const struct wined3d_gl_info *gl_info = context->gl_info;
1281 struct wined3d_color color;
1282 float start, end, scale;
1283 union
1285 DWORD d;
1286 float f;
1287 } tmpvalue;
1289 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1290 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1291 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1292 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1293 get_fog_start_end(context, state, &start, &end);
1294 scale = 1.0f / (end - start);
1295 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1296 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1297 checkGLcall("fog emulation uniforms");
1300 /* Context activation is done by the caller (state handler). */
1301 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1302 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1304 struct wined3d_color float_key[2];
1305 const struct wined3d_texture *texture = state->textures[0];
1307 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1308 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1311 /* Context activation is done by the caller (state handler). */
1312 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1313 const struct wined3d_state *state)
1315 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1316 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1317 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1318 const struct wined3d_gl_info *gl_info = context->gl_info;
1319 struct shader_glsl_priv *priv = shader_priv;
1320 float position_fixup[4];
1321 DWORD update_mask;
1323 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1324 UINT constant_version;
1325 int i;
1327 if (!prog) {
1328 /* No GLSL program set - nothing to do. */
1329 return;
1331 constant_version = prog->constant_version;
1332 update_mask = context->constant_update_mask & prog->constant_update_mask;
1334 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1335 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
1336 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1338 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1339 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
1340 vshader->reg_maps.integer_constants);
1342 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1343 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1344 vshader->reg_maps.boolean_constants);
1346 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1347 shader_glsl_pointsize_uniform(context, state, prog);
1349 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
1351 shader_get_position_fixup(context, state, position_fixup);
1352 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1353 checkGLcall("glUniform4fv");
1356 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1358 struct wined3d_matrix mat;
1360 get_modelview_matrix(context, state, 0, &mat);
1361 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1362 checkGLcall("glUniformMatrix4fv");
1364 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1367 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1369 struct wined3d_matrix mat;
1371 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1373 if (prog->vs.modelview_matrix_location[i] == -1)
1374 break;
1376 get_modelview_matrix(context, state, i, &mat);
1377 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1378 checkGLcall("glUniformMatrix4fv");
1382 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1384 struct wined3d_matrix projection;
1386 get_projection_matrix(context, state, &projection);
1387 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1388 checkGLcall("glUniformMatrix4fv");
1391 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1393 for (i = 0; i < MAX_TEXTURES; ++i)
1394 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1397 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1398 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1400 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1402 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1403 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1404 shader_glsl_ffp_vertex_light_uniform(context, state, i, prog);
1407 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1408 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
1409 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1411 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1412 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
1413 pshader->reg_maps.integer_constants);
1415 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1416 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1417 pshader->reg_maps.boolean_constants);
1419 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1421 for (i = 0; i < MAX_TEXTURES; ++i)
1423 if (prog->ps.bumpenv_mat_location[i] == -1)
1424 continue;
1426 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1427 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1429 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1431 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1432 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1433 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1434 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1438 checkGLcall("bump env uniforms");
1441 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1443 const struct wined3d_vec4 correction_params =
1445 /* Position is relative to the framebuffer, not the viewport. */
1446 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1447 context->render_offscreen ? 1.0f : -1.0f,
1448 0.0f,
1449 0.0f,
1452 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1455 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1456 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1457 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1458 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1460 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1462 struct wined3d_color color;
1464 if (prog->ps.tex_factor_location != -1)
1466 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1467 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1470 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1471 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1472 else
1473 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1475 for (i = 0; i < MAX_TEXTURES; ++i)
1477 if (prog->ps.tss_constant_location[i] == -1)
1478 continue;
1480 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1481 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1484 checkGLcall("fixed function uniforms");
1487 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1488 shader_glsl_load_fog_uniform(context, state, prog);
1490 if (priv->next_constant_version == UINT_MAX)
1492 TRACE("Max constant version reached, resetting to 0.\n");
1493 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1494 priv->next_constant_version = 1;
1496 else
1498 prog->constant_version = priv->next_constant_version++;
1502 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1504 struct constant_entry *entries = heap->entries;
1505 unsigned int *positions = heap->positions;
1506 unsigned int heap_idx, parent_idx;
1508 if (!heap->contained[idx])
1510 heap_idx = heap->size++;
1511 heap->contained[idx] = TRUE;
1513 else
1515 heap_idx = positions[idx];
1518 while (heap_idx > 1)
1520 parent_idx = heap_idx >> 1;
1522 if (new_version <= entries[parent_idx].version) break;
1524 entries[heap_idx] = entries[parent_idx];
1525 positions[entries[parent_idx].idx] = heap_idx;
1526 heap_idx = parent_idx;
1529 entries[heap_idx].version = new_version;
1530 entries[heap_idx].idx = idx;
1531 positions[idx] = heap_idx;
1534 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1536 struct shader_glsl_priv *priv = device->shader_priv;
1537 struct constant_heap *heap = &priv->vconst_heap;
1538 UINT i;
1540 for (i = start; i < count + start; ++i)
1542 update_heap_entry(heap, i, priv->next_constant_version);
1545 for (i = 0; i < device->context_count; ++i)
1547 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
1551 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1553 struct shader_glsl_priv *priv = device->shader_priv;
1554 struct constant_heap *heap = &priv->pconst_heap;
1555 UINT i;
1557 for (i = start; i < count + start; ++i)
1559 update_heap_entry(heap, i, priv->next_constant_version);
1562 for (i = 0; i < device->context_count; ++i)
1564 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
1568 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1570 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1571 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1572 if(shader_major > 3) return ret;
1574 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1575 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1576 return ret;
1579 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1581 return gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
1584 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
1586 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
1589 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1590 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1592 va_list args;
1593 int ret;
1595 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1596 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1597 for (;;)
1599 va_start(args, format);
1600 ret = shader_vaddline(buffer, format, args);
1601 va_end(args);
1602 if (!ret)
1603 return;
1604 if (!string_buffer_resize(buffer, ret))
1605 return;
1609 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1610 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1612 va_list args;
1613 int ret;
1615 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1616 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1617 for (;;)
1619 va_start(args, format);
1620 ret = shader_vaddline(buffer, format, args);
1621 va_end(args);
1622 if (!ret)
1623 return;
1624 if (!string_buffer_resize(buffer, ret))
1625 return;
1629 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
1631 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
1634 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
1636 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
1637 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1638 const BOOL *input_reg_used = shader->u.ps.input_reg_used;
1639 unsigned int i;
1641 if (reg_maps->shader_version.major < 3)
1642 return input_reg_used[idx];
1644 for (i = 0; i < input_signature->element_count; ++i)
1646 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
1648 if (!(reg_maps->input_registers & (1u << input->register_idx)))
1649 continue;
1651 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
1652 && input->semantic_idx == idx)
1654 if (input_reg_used[input->register_idx])
1655 return TRUE;
1656 else
1657 return FALSE;
1660 return FALSE;
1663 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
1664 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
1666 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
1668 if (version->major >= 4)
1669 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
1670 else
1671 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
1674 /** Generate the variable & register declarations for the GLSL output target */
1675 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1676 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1677 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1679 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1680 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1681 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1682 const struct wined3d_gl_info *gl_info = context->gl_info;
1683 unsigned int i, extra_constants_needed = 0;
1684 const struct wined3d_shader_lconst *lconst;
1685 const char *prefix;
1686 DWORD map;
1688 prefix = shader_glsl_get_prefix(version->type);
1690 /* Prototype the subroutines */
1691 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1693 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1696 /* Declare the constants (aka uniforms) */
1697 if (shader->limits->constant_float > 0)
1699 unsigned max_constantsF;
1701 /* Unless the shader uses indirect addressing, always declare the
1702 * maximum array size and ignore that we need some uniforms privately.
1703 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1704 * and immediate values, still declare VC[256]. If the shader needs
1705 * more uniforms than we have it won't work in any case. If it uses
1706 * less, the compiler will figure out which uniforms are really used
1707 * and strip them out. This allows a shader to use c255 on a dx9 card,
1708 * as long as it doesn't also use all the other constants.
1710 * If the shader uses indirect addressing the compiler must assume
1711 * that all declared uniforms are used. In this case, declare only the
1712 * amount that we're assured to have.
1714 * Thus we run into problems in these two cases:
1715 * 1) The shader really uses more uniforms than supported.
1716 * 2) The shader uses indirect addressing, less constants than
1717 * supported, but uses a constant index > #supported consts. */
1718 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1720 /* No indirect addressing here. */
1721 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1723 else
1725 if (reg_maps->usesrelconstF)
1727 /* Subtract the other potential uniforms from the max
1728 * available (bools, ints, and 1 row of projection matrix).
1729 * Subtract another uniform for immediate values, which have
1730 * to be loaded via uniform by the driver as well. The shader
1731 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1732 * shader code, so one vec4 should be enough. (Unfortunately
1733 * the Nvidia driver doesn't store 128 and -128 in one float).
1735 * Writing gl_ClipVertex requires one uniform for each
1736 * clipplane as well. */
1737 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1738 if (vs_args->clip_enabled)
1739 max_constantsF -= gl_info->limits.clipplanes;
1740 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
1741 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1742 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1743 * for now take this into account when calculating the number of available constants
1745 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
1746 /* Set by driver quirks in directx.c */
1747 max_constantsF -= gl_info->reserved_glsl_constants;
1749 if (max_constantsF < shader->limits->constant_float)
1751 static unsigned int once;
1753 if (!once++)
1754 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1755 " it may not render correctly.\n");
1756 else
1757 WARN("The hardware does not support enough uniform components to run this shader.\n");
1760 else
1762 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1765 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1766 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1769 /* Always declare the full set of constants, the compiler can remove the
1770 * unused ones because d3d doesn't (yet) support indirect int and bool
1771 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1772 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1773 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1775 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1776 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1778 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1780 if (reg_maps->cb_sizes[i])
1781 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1782 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1785 /* Declare texture samplers */
1786 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1788 struct wined3d_shader_sampler_map_entry *entry;
1789 const char *sampler_type_prefix, *sampler_type;
1790 BOOL shadow_sampler, tex_rect;
1792 entry = &reg_maps->sampler_map.entries[i];
1794 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1796 ERR("Invalid resource index %u.\n", entry->resource_idx);
1797 continue;
1800 switch (reg_maps->resource_info[entry->resource_idx].data_type)
1802 case WINED3D_DATA_FLOAT:
1803 case WINED3D_DATA_UNORM:
1804 case WINED3D_DATA_SNORM:
1805 sampler_type_prefix = "";
1806 break;
1808 case WINED3D_DATA_INT:
1809 sampler_type_prefix = "i";
1810 break;
1812 case WINED3D_DATA_UINT:
1813 sampler_type_prefix = "u";
1814 break;
1816 default:
1817 sampler_type_prefix = "";
1818 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
1819 break;
1822 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
1823 switch (reg_maps->resource_info[entry->resource_idx].type)
1825 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1826 if (shadow_sampler)
1827 sampler_type = "sampler1DShadow";
1828 else
1829 sampler_type = "sampler1D";
1830 break;
1832 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1833 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1834 && (ps_args->np2_fixup & (1u << entry->resource_idx))
1835 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1836 if (shadow_sampler)
1838 if (tex_rect)
1839 sampler_type = "sampler2DRectShadow";
1840 else
1841 sampler_type = "sampler2DShadow";
1843 else
1845 if (tex_rect)
1846 sampler_type = "sampler2DRect";
1847 else
1848 sampler_type = "sampler2D";
1850 break;
1852 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1853 if (shadow_sampler)
1854 FIXME("Unsupported 3D shadow sampler.\n");
1855 sampler_type = "sampler3D";
1856 break;
1858 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1859 if (shadow_sampler)
1860 FIXME("Unsupported Cube shadow sampler.\n");
1861 sampler_type = "samplerCube";
1862 break;
1864 default:
1865 sampler_type = "unsupported_sampler";
1866 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
1867 break;
1869 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
1870 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
1873 /* Declare uniforms for NP2 texcoord fixup:
1874 * This is NOT done inside the loop that declares the texture samplers
1875 * since the NP2 fixup code is currently only used for the GeforceFX
1876 * series and when forcing the ARB_npot extension off. Modern cards just
1877 * skip the code anyway, so put it inside a separate loop. */
1878 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1880 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1881 UINT cur = 0;
1883 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1884 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1885 * samplerNP2Fixup stores texture dimensions and is updated through
1886 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1888 for (i = 0; i < shader->limits->sampler; ++i)
1890 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
1891 continue;
1893 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1895 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1896 continue;
1899 fixup->idx[i] = cur++;
1902 fixup->num_consts = (cur + 1) >> 1;
1903 fixup->active = ps_args->np2_fixup;
1904 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1907 /* Declare address variables */
1908 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1910 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1913 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1915 for (i = 0; i < shader->input_signature.element_count; ++i)
1917 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
1918 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
1919 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1920 prefix, e->register_idx);
1921 else
1922 shader_addline(buffer, "%s vec4 %s_in%u;\n",
1923 get_attribute_keyword(gl_info), prefix, e->register_idx);
1926 if (vs_args->point_size && !vs_args->per_vertex_point_size)
1928 shader_addline(buffer, "uniform struct\n{\n");
1929 shader_addline(buffer, " float size;\n");
1930 shader_addline(buffer, " float size_min;\n");
1931 shader_addline(buffer, " float size_max;\n");
1932 shader_addline(buffer, "} ffp_point;\n");
1935 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && version->major < 3)
1937 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_diffuse;\n");
1938 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_specular;\n");
1939 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
1940 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
1943 shader_addline(buffer, "uniform vec4 posFixup;\n");
1944 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits->packed_output);
1946 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1948 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
1950 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1952 if (version->major < 3 || ps_args->vp_mode != vertexshader)
1954 shader_addline(buffer, "uniform struct\n{\n");
1955 shader_addline(buffer, " vec4 color;\n");
1956 shader_addline(buffer, " float density;\n");
1957 shader_addline(buffer, " float end;\n");
1958 shader_addline(buffer, " float scale;\n");
1959 shader_addline(buffer, "} ffp_fog;\n");
1961 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
1963 if (glsl_is_color_reg_read(shader, 0))
1964 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
1965 if (glsl_is_color_reg_read(shader, 1))
1966 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
1967 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
1968 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
1970 else
1972 if (glsl_is_color_reg_read(shader, 0))
1973 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_diffuse;\n");
1974 if (glsl_is_color_reg_read(shader, 1))
1975 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_specular;\n");
1976 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
1977 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
1978 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
1982 if (version->major >= 3)
1984 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
1986 if (ps_args->vp_mode == vertexshader)
1987 declare_in_varying(gl_info, buffer, FALSE, "vec4 %s_link[%u];\n", prefix, in_count);
1988 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1991 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1993 if (!(map & 1))
1994 continue;
1996 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1998 if (reg_maps->luminanceparams & (1u << i))
2000 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
2001 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
2002 extra_constants_needed++;
2005 extra_constants_needed++;
2008 if (ps_args->srgb_correction)
2010 shader_addline(buffer, "const vec4 srgb_const0 = ");
2011 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
2012 shader_addline(buffer, ";\n");
2013 shader_addline(buffer, "const vec4 srgb_const1 = ");
2014 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
2015 shader_addline(buffer, ";\n");
2017 if (reg_maps->vpos || reg_maps->usesdsy)
2019 ++extra_constants_needed;
2020 shader_addline(buffer, "uniform vec4 ycorrection;\n");
2021 shader_addline(buffer, "vec4 vpos;\n");
2024 if (!needs_legacy_glsl_syntax(gl_info))
2025 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
2027 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
2028 FIXME("Insufficient uniforms to run this shader.\n");
2031 /* Declare output register temporaries */
2032 if (shader->limits->packed_output)
2033 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2035 /* Declare temporary variables */
2036 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2038 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
2041 /* Declare loop registers aLx */
2042 if (version->major < 4)
2044 for (i = 0; i < reg_maps->loop_depth; ++i)
2046 shader_addline(buffer, "int aL%u;\n", i);
2047 shader_addline(buffer, "int tmpInt%u;\n", i);
2051 /* Temporary variables for matrix operations */
2052 shader_addline(buffer, "vec4 tmp0;\n");
2053 shader_addline(buffer, "vec4 tmp1;\n");
2055 if (!shader->load_local_constsF)
2057 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2059 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2060 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2061 shader_addline(buffer, ";\n");
2065 /* Start the main program. */
2066 shader_addline(buffer, "void main()\n{\n");
2068 /* Direct3D applications expect integer vPos values, while OpenGL drivers
2069 * add approximately 0.5. This causes off-by-one problems as spotted by
2070 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
2071 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
2072 * causes precision troubles when we just subtract 0.5.
2074 * To deal with that, just floor() the position. This will eliminate the
2075 * fraction on all cards.
2077 * TODO: Test how this behaves with multisampling.
2079 * An advantage of floor is that it works even if the driver doesn't add
2080 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
2081 * to return in gl_FragCoord, even though coordinates specify the pixel
2082 * centers instead of the pixel corners. This code will behave correctly
2083 * on drivers that returns integer values. */
2084 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
2086 if (shader->device->wined3d->flags & WINED3D_PIXEL_CENTER_INTEGER)
2087 shader_addline(buffer,
2088 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
2089 else
2090 shader_addline(buffer,
2091 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
2095 /*****************************************************************************
2096 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2098 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2099 ****************************************************************************/
2101 /* Prototypes */
2102 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2103 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2105 /** Used for opcode modifiers - They multiply the result by the specified amount */
2106 static const char * const shift_glsl_tab[] = {
2107 "", /* 0 (none) */
2108 "2.0 * ", /* 1 (x2) */
2109 "4.0 * ", /* 2 (x4) */
2110 "8.0 * ", /* 3 (x8) */
2111 "16.0 * ", /* 4 (x16) */
2112 "32.0 * ", /* 5 (x32) */
2113 "", /* 6 (x64) */
2114 "", /* 7 (x128) */
2115 "", /* 8 (d256) */
2116 "", /* 9 (d128) */
2117 "", /* 10 (d64) */
2118 "", /* 11 (d32) */
2119 "0.0625 * ", /* 12 (d16) */
2120 "0.125 * ", /* 13 (d8) */
2121 "0.25 * ", /* 14 (d4) */
2122 "0.5 * " /* 15 (d2) */
2125 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2126 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2127 const char *in_reg, const char *in_regswizzle, char *out_str)
2129 switch (src_modifier)
2131 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2132 case WINED3DSPSM_DW:
2133 case WINED3DSPSM_NONE:
2134 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2135 break;
2136 case WINED3DSPSM_NEG:
2137 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2138 break;
2139 case WINED3DSPSM_NOT:
2140 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2141 break;
2142 case WINED3DSPSM_BIAS:
2143 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2144 break;
2145 case WINED3DSPSM_BIASNEG:
2146 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2147 break;
2148 case WINED3DSPSM_SIGN:
2149 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2150 break;
2151 case WINED3DSPSM_SIGNNEG:
2152 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2153 break;
2154 case WINED3DSPSM_COMP:
2155 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2156 break;
2157 case WINED3DSPSM_X2:
2158 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2159 break;
2160 case WINED3DSPSM_X2NEG:
2161 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2162 break;
2163 case WINED3DSPSM_ABS:
2164 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2165 break;
2166 case WINED3DSPSM_ABSNEG:
2167 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2168 break;
2169 default:
2170 FIXME("Unhandled modifier %u\n", src_modifier);
2171 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2175 /** Writes the GLSL variable name that corresponds to the register that the
2176 * DX opcode parameter is trying to access */
2177 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2178 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2180 /* oPos, oFog and oPts in D3D */
2181 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2183 const struct wined3d_shader *shader = ins->ctx->shader;
2184 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2185 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2186 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2187 const char *prefix = shader_glsl_get_prefix(version->type);
2188 struct glsl_src_param rel_param0, rel_param1;
2189 char imm_str[4][17];
2191 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2192 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2193 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2194 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2195 *is_color = FALSE;
2197 switch (reg->type)
2199 case WINED3DSPR_TEMP:
2200 sprintf(register_name, "R%u", reg->idx[0].offset);
2201 break;
2203 case WINED3DSPR_INPUT:
2204 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2206 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2208 if (reg->idx[0].rel_addr)
2209 FIXME("VS3+ input registers relative addressing.\n");
2210 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2211 *is_color = TRUE;
2212 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2213 break;
2216 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2218 if (reg->idx[0].rel_addr)
2220 if (reg->idx[1].rel_addr)
2221 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
2222 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2223 else
2224 sprintf(register_name, "gs_in[%s + %u][%u]",
2225 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
2227 else if (reg->idx[1].rel_addr)
2228 sprintf(register_name, "gs_in[%u][%s + %u]",
2229 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2230 else
2231 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
2232 break;
2235 /* pixel shaders >= 3.0 */
2236 if (version->major >= 3)
2238 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2239 unsigned int in_count = vec4_varyings(version->major, gl_info);
2241 if (reg->idx[0].rel_addr)
2243 /* Removing a + 0 would be an obvious optimization, but
2244 * OS X doesn't see the NOP operation there. */
2245 if (idx)
2247 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2248 && shader->u.ps.declared_in_count > in_count)
2250 sprintf(register_name,
2251 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2252 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2253 prefix, rel_param0.param_str, idx);
2255 else
2257 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2260 else
2262 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2263 && shader->u.ps.declared_in_count > in_count)
2265 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2266 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2267 prefix, rel_param0.param_str);
2269 else
2271 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2275 else
2277 if (idx == in_count) sprintf(register_name, "gl_Color");
2278 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2279 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2282 else
2284 if (!reg->idx[0].offset)
2285 strcpy(register_name, "ffp_varying_diffuse");
2286 else
2287 strcpy(register_name, "ffp_varying_specular");
2288 break;
2290 break;
2292 case WINED3DSPR_CONST:
2294 /* Relative addressing */
2295 if (reg->idx[0].rel_addr)
2297 if (wined3d_settings.check_float_constants)
2298 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2299 rel_param0.param_str, reg->idx[0].offset,
2300 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2301 prefix, rel_param0.param_str, reg->idx[0].offset);
2302 else if (reg->idx[0].offset)
2303 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2304 else
2305 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2307 else
2309 if (shader_constant_is_local(shader, reg->idx[0].offset))
2310 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2311 else
2312 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2315 break;
2317 case WINED3DSPR_CONSTINT:
2318 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2319 break;
2321 case WINED3DSPR_CONSTBOOL:
2322 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2323 break;
2325 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2326 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2327 sprintf(register_name, "T%u", reg->idx[0].offset);
2328 else
2329 sprintf(register_name, "A%u", reg->idx[0].offset);
2330 break;
2332 case WINED3DSPR_LOOP:
2333 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
2334 break;
2336 case WINED3DSPR_SAMPLER:
2337 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2338 break;
2340 case WINED3DSPR_COLOROUT:
2341 if (reg->idx[0].offset >= gl_info->limits.buffers)
2342 WARN("Write to render target %u, only %d supported.\n",
2343 reg->idx[0].offset, gl_info->limits.buffers);
2345 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2346 break;
2348 case WINED3DSPR_RASTOUT:
2349 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2350 break;
2352 case WINED3DSPR_DEPTHOUT:
2353 sprintf(register_name, "gl_FragDepth");
2354 break;
2356 case WINED3DSPR_ATTROUT:
2357 if (!reg->idx[0].offset)
2358 sprintf(register_name, "%s_out[8]", prefix);
2359 else
2360 sprintf(register_name, "%s_out[9]", prefix);
2361 break;
2363 case WINED3DSPR_TEXCRDOUT:
2364 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2365 if (reg->idx[0].rel_addr)
2366 FIXME("VS3 output registers relative addressing.\n");
2367 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2368 break;
2370 case WINED3DSPR_MISCTYPE:
2371 if (!reg->idx[0].offset)
2373 /* vPos */
2374 sprintf(register_name, "vpos");
2376 else if (reg->idx[0].offset == 1)
2378 /* Note that gl_FrontFacing is a bool, while vFace is
2379 * a float for which the sign determines front/back */
2380 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2382 else
2384 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2385 sprintf(register_name, "unrecognized_register");
2387 break;
2389 case WINED3DSPR_IMMCONST:
2390 switch (reg->immconst_type)
2392 case WINED3D_IMMCONST_SCALAR:
2393 switch (reg->data_type)
2395 case WINED3D_DATA_FLOAT:
2396 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
2397 break;
2398 case WINED3D_DATA_INT:
2399 sprintf(register_name, "%#x", reg->immconst_data[0]);
2400 break;
2401 case WINED3D_DATA_RESOURCE:
2402 case WINED3D_DATA_SAMPLER:
2403 case WINED3D_DATA_UINT:
2404 sprintf(register_name, "%#xu", reg->immconst_data[0]);
2405 break;
2406 default:
2407 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2408 break;
2410 break;
2412 case WINED3D_IMMCONST_VEC4:
2413 switch (reg->data_type)
2415 case WINED3D_DATA_FLOAT:
2416 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
2417 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
2418 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
2419 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
2420 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2421 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2422 break;
2423 case WINED3D_DATA_INT:
2424 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2425 reg->immconst_data[0], reg->immconst_data[1],
2426 reg->immconst_data[2], reg->immconst_data[3]);
2427 break;
2428 case WINED3D_DATA_RESOURCE:
2429 case WINED3D_DATA_SAMPLER:
2430 case WINED3D_DATA_UINT:
2431 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2432 reg->immconst_data[0], reg->immconst_data[1],
2433 reg->immconst_data[2], reg->immconst_data[3]);
2434 break;
2435 default:
2436 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2437 break;
2439 break;
2441 default:
2442 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2443 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2445 break;
2447 case WINED3DSPR_CONSTBUFFER:
2448 if (reg->idx[1].rel_addr)
2449 sprintf(register_name, "%s_cb%u[%s + %u]",
2450 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2451 else
2452 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2453 break;
2455 case WINED3DSPR_IMMCONSTBUFFER:
2456 if (reg->idx[0].rel_addr)
2457 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2458 else
2459 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2460 break;
2462 case WINED3DSPR_PRIMID:
2463 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2464 break;
2466 default:
2467 FIXME("Unhandled register type %#x.\n", reg->type);
2468 sprintf(register_name, "unrecognized_register");
2469 break;
2473 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2475 *str++ = '.';
2476 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2477 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2478 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2479 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2480 *str = '\0';
2483 /* Get the GLSL write mask for the destination register */
2484 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2486 DWORD mask = param->write_mask;
2488 if (shader_is_scalar(&param->reg))
2490 mask = WINED3DSP_WRITEMASK_0;
2491 *write_mask = '\0';
2493 else
2495 shader_glsl_write_mask_to_str(mask, write_mask);
2498 return mask;
2501 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
2502 unsigned int size = 0;
2504 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2505 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2506 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2507 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2509 return size;
2512 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2514 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2515 * but addressed as "rgba". To fix this we need to swap the register's x
2516 * and z components. */
2517 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2519 *str++ = '.';
2520 /* swizzle bits fields: wwzzyyxx */
2521 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
2522 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
2523 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
2524 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
2525 *str = '\0';
2528 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2529 BOOL fixup, DWORD mask, char *swizzle_str)
2531 if (shader_is_scalar(&param->reg))
2532 *swizzle_str = '\0';
2533 else
2534 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2537 /* From a given parameter token, generate the corresponding GLSL string.
2538 * Also, return the actual register name and swizzle in case the
2539 * caller needs this information as well. */
2540 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2541 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
2543 BOOL is_color = FALSE;
2544 char swizzle_str[6];
2546 glsl_src->reg_name[0] = '\0';
2547 glsl_src->param_str[0] = '\0';
2548 swizzle_str[0] = '\0';
2550 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
2551 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
2553 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
2555 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
2557 else
2559 char reg_name[200];
2561 switch (wined3d_src->reg.data_type)
2563 case WINED3D_DATA_FLOAT:
2564 sprintf(reg_name, "%s", glsl_src->reg_name);
2565 break;
2566 case WINED3D_DATA_INT:
2567 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
2568 break;
2569 case WINED3D_DATA_RESOURCE:
2570 case WINED3D_DATA_SAMPLER:
2571 case WINED3D_DATA_UINT:
2572 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
2573 break;
2574 default:
2575 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
2576 sprintf(reg_name, "%s", glsl_src->reg_name);
2577 break;
2580 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
2584 /* From a given parameter token, generate the corresponding GLSL string.
2585 * Also, return the actual register name and swizzle in case the
2586 * caller needs this information as well. */
2587 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
2588 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
2590 BOOL is_color = FALSE;
2592 glsl_dst->mask_str[0] = '\0';
2593 glsl_dst->reg_name[0] = '\0';
2595 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
2596 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
2599 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2600 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
2601 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
2602 enum wined3d_data_type data_type)
2604 struct glsl_dst_param glsl_dst;
2605 DWORD mask;
2607 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
2609 switch (data_type)
2611 case WINED3D_DATA_FLOAT:
2612 shader_addline(buffer, "%s%s = %s(",
2613 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2614 break;
2615 case WINED3D_DATA_INT:
2616 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
2617 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2618 break;
2619 case WINED3D_DATA_RESOURCE:
2620 case WINED3D_DATA_SAMPLER:
2621 case WINED3D_DATA_UINT:
2622 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
2623 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2624 break;
2625 default:
2626 FIXME("Unhandled data type %#x.\n", data_type);
2627 shader_addline(buffer, "%s%s = %s(",
2628 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2629 break;
2633 return mask;
2636 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2637 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
2639 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2642 /** Process GLSL instruction modifiers */
2643 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
2645 struct glsl_dst_param dst_param;
2646 DWORD modifiers;
2648 if (!ins->dst_count) return;
2650 modifiers = ins->dst[0].modifiers;
2651 if (!modifiers) return;
2653 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2655 if (modifiers & WINED3DSPDM_SATURATE)
2657 /* _SAT means to clamp the value of the register to between 0 and 1 */
2658 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
2659 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
2662 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
2664 FIXME("_centroid modifier not handled\n");
2667 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
2669 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2673 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
2675 switch (op)
2677 case WINED3D_SHADER_REL_OP_GT: return ">";
2678 case WINED3D_SHADER_REL_OP_EQ: return "==";
2679 case WINED3D_SHADER_REL_OP_GE: return ">=";
2680 case WINED3D_SHADER_REL_OP_LT: return "<";
2681 case WINED3D_SHADER_REL_OP_NE: return "!=";
2682 case WINED3D_SHADER_REL_OP_LE: return "<=";
2683 default:
2684 FIXME("Unrecognized operator %#x.\n", op);
2685 return "(\?\?)";
2689 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
2690 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
2692 static const struct
2694 unsigned int coord_size;
2695 unsigned int offset_size;
2696 const char *type_part;
2698 resource_types[] =
2700 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
2701 {1, 0, ""}, /* WINED3D_SHADER_RESOURCE_BUFFER */
2702 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
2703 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
2704 {2, 0, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
2705 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
2706 {3, 0, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
2707 {2, 1, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
2708 {3, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
2709 {3, 0, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
2711 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2712 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
2713 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2714 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
2715 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
2716 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
2717 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
2718 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2719 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
2720 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
2721 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
2722 const char *base = "texture", *type_part = "", *suffix = "";
2723 unsigned int coord_size;
2725 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
2727 if (resource_type >= ARRAY_SIZE(resource_types))
2729 ERR("Unexpected resource type %#x.\n", resource_type);
2730 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
2733 /* Note that there's no such thing as a projected cube texture. */
2734 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
2735 projected = FALSE;
2737 if (needs_legacy_glsl_syntax(gl_info))
2739 if (shadow)
2740 base = "shadow";
2742 type_part = resource_types[resource_type].type_part;
2743 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
2744 type_part = "2DRect";
2745 if (!type_part[0])
2746 FIXME("Unhandled resource type %#x.\n", resource_type);
2748 if (!lod && grad && !gl_info->supported[EXT_GPU_SHADER4])
2750 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2751 suffix = "ARB";
2752 else
2753 FIXME("Unsupported grad function.\n");
2757 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
2759 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
2760 if (flags & ~texel_fetch_flags)
2761 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
2763 base = "texelFetch";
2764 type_part = "";
2767 sample_function->offset_size = offset ? resource_types[resource_type].offset_size : 0;
2768 if (offset && !sample_function->offset_size)
2770 FIXME("Offset not supported for resource type %#x.\n", resource_type);
2771 offset = FALSE;
2774 sample_function->name = string_buffer_get(priv->string_buffers);
2775 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
2776 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
2778 coord_size = resource_types[resource_type].coord_size;
2779 if (shadow)
2780 ++coord_size;
2781 sample_function->coord_mask = (1u << coord_size) - 1;
2782 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
2785 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
2786 struct glsl_sample_function *sample_function)
2788 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2790 string_buffer_release(priv->string_buffers, sample_function->name);
2793 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2794 BOOL sign_fixup, enum fixup_channel_source channel_source)
2796 switch(channel_source)
2798 case CHANNEL_SOURCE_ZERO:
2799 strcat(arguments, "0.0");
2800 break;
2802 case CHANNEL_SOURCE_ONE:
2803 strcat(arguments, "1.0");
2804 break;
2806 case CHANNEL_SOURCE_X:
2807 strcat(arguments, reg_name);
2808 strcat(arguments, ".x");
2809 break;
2811 case CHANNEL_SOURCE_Y:
2812 strcat(arguments, reg_name);
2813 strcat(arguments, ".y");
2814 break;
2816 case CHANNEL_SOURCE_Z:
2817 strcat(arguments, reg_name);
2818 strcat(arguments, ".z");
2819 break;
2821 case CHANNEL_SOURCE_W:
2822 strcat(arguments, reg_name);
2823 strcat(arguments, ".w");
2824 break;
2826 default:
2827 FIXME("Unhandled channel source %#x\n", channel_source);
2828 strcat(arguments, "undefined");
2829 break;
2832 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2835 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
2836 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2838 unsigned int mask_size, remaining;
2839 DWORD fixup_mask = 0;
2840 char arguments[256];
2841 char mask_str[6];
2843 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2844 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2845 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2846 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2847 if (!(mask &= fixup_mask))
2848 return;
2850 if (is_complex_fixup(fixup))
2852 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2853 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2854 return;
2857 shader_glsl_write_mask_to_str(mask, mask_str);
2858 mask_size = shader_glsl_get_write_mask_size(mask);
2860 arguments[0] = '\0';
2861 remaining = mask_size;
2862 if (mask & WINED3DSP_WRITEMASK_0)
2864 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2865 if (--remaining) strcat(arguments, ", ");
2867 if (mask & WINED3DSP_WRITEMASK_1)
2869 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2870 if (--remaining) strcat(arguments, ", ");
2872 if (mask & WINED3DSP_WRITEMASK_2)
2874 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2875 if (--remaining) strcat(arguments, ", ");
2877 if (mask & WINED3DSP_WRITEMASK_3)
2879 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2880 if (--remaining) strcat(arguments, ", ");
2883 if (mask_size > 1)
2884 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2885 else
2886 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2889 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2891 char reg_name[256];
2892 BOOL is_color;
2894 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2895 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2898 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2899 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
2900 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
2901 const char *coord_reg_fmt, ...)
2903 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2904 char dst_swizzle[6];
2905 struct color_fixup_desc fixup;
2906 BOOL np2_fixup = FALSE;
2907 va_list args;
2908 int ret;
2910 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2912 /* If ARB_texture_swizzle is supported we don't need to do anything here.
2913 * We actually rely on it for vertex shaders and SM4+. */
2914 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
2916 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2917 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
2919 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
2920 np2_fixup = TRUE;
2922 else
2924 fixup = COLOR_FIXUP_IDENTITY;
2927 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2929 if (sample_function->output_single_component)
2930 shader_addline(ins->ctx->buffer, "vec4(");
2932 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2933 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
2935 for (;;)
2937 va_start(args, coord_reg_fmt);
2938 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2939 va_end(args);
2940 if (!ret)
2941 break;
2942 if (!string_buffer_resize(ins->ctx->buffer, ret))
2943 break;
2946 if (np2_fixup)
2948 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2949 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
2951 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
2953 case 1:
2954 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2955 idx >> 1, (idx % 2) ? "z" : "x");
2956 break;
2957 case 2:
2958 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2959 idx >> 1, (idx % 2) ? "zw" : "xy");
2960 break;
2961 case 3:
2962 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
2963 idx >> 1, (idx % 2) ? "zw" : "xy");
2964 break;
2965 case 4:
2966 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
2967 idx >> 1, (idx % 2) ? "zw" : "xy");
2968 break;
2971 if (dx && dy)
2972 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
2973 else if (bias)
2974 shader_addline(ins->ctx->buffer, ", %s", bias);
2975 if (sample_function->offset_size)
2977 int offset_immdata[4] = {offset->u, offset->v, offset->w};
2978 shader_addline(ins->ctx->buffer, ", ");
2979 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
2981 shader_addline(ins->ctx->buffer, ")");
2983 if (sample_function->output_single_component)
2984 shader_addline(ins->ctx->buffer, ")");
2986 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
2988 if (!is_identity_fixup(fixup))
2989 shader_glsl_color_correction(ins, fixup);
2992 /*****************************************************************************
2993 * Begin processing individual instruction opcodes
2994 ****************************************************************************/
2996 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2998 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2999 struct glsl_src_param src0_param;
3000 struct glsl_src_param src1_param;
3001 DWORD write_mask;
3002 const char *op;
3004 /* Determine the GLSL operator to use based on the opcode */
3005 switch (ins->handler_idx)
3007 case WINED3DSIH_ADD: op = "+"; break;
3008 case WINED3DSIH_AND: op = "&"; break;
3009 case WINED3DSIH_DIV: op = "/"; break;
3010 case WINED3DSIH_IADD: op = "+"; break;
3011 case WINED3DSIH_ISHL: op = "<<"; break;
3012 case WINED3DSIH_MUL: op = "*"; break;
3013 case WINED3DSIH_OR: op = "|"; break;
3014 case WINED3DSIH_SUB: op = "-"; break;
3015 case WINED3DSIH_USHR: op = ">>"; break;
3016 case WINED3DSIH_XOR: op = "^"; break;
3017 default:
3018 op = "<unhandled operator>";
3019 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3020 break;
3023 write_mask = shader_glsl_append_dst(buffer, ins);
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);
3026 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3029 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3031 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3032 struct glsl_src_param src0_param;
3033 struct glsl_src_param src1_param;
3034 unsigned int mask_size;
3035 DWORD write_mask;
3036 const char *op;
3038 write_mask = shader_glsl_append_dst(buffer, ins);
3039 mask_size = shader_glsl_get_write_mask_size(write_mask);
3040 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3041 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3043 if (mask_size > 1)
3045 switch (ins->handler_idx)
3047 case WINED3DSIH_EQ: op = "equal"; break;
3048 case WINED3DSIH_IEQ: op = "equal"; break;
3049 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3050 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3051 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3052 case WINED3DSIH_LT: op = "lessThan"; break;
3053 case WINED3DSIH_ILT: op = "lessThan"; break;
3054 case WINED3DSIH_ULT: op = "lessThan"; break;
3055 case WINED3DSIH_NE: op = "notEqual"; break;
3056 case WINED3DSIH_INE: op = "notEqual"; break;
3057 default:
3058 op = "<unhandled operator>";
3059 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3060 break;
3063 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3064 mask_size, op, src0_param.param_str, src1_param.param_str);
3066 else
3068 switch (ins->handler_idx)
3070 case WINED3DSIH_EQ: op = "=="; break;
3071 case WINED3DSIH_IEQ: op = "=="; break;
3072 case WINED3DSIH_GE: op = ">="; break;
3073 case WINED3DSIH_IGE: op = ">="; break;
3074 case WINED3DSIH_UGE: op = ">="; break;
3075 case WINED3DSIH_LT: op = "<"; break;
3076 case WINED3DSIH_ILT: op = "<"; break;
3077 case WINED3DSIH_ULT: op = "<"; break;
3078 case WINED3DSIH_NE: op = "!="; break;
3079 case WINED3DSIH_INE: op = "!="; break;
3080 default:
3081 op = "<unhandled operator>";
3082 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3083 break;
3086 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3087 src0_param.param_str, op, src1_param.param_str);
3091 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3093 struct glsl_src_param src_param;
3094 DWORD write_mask;
3095 const char *op;
3097 switch (ins->handler_idx)
3099 case WINED3DSIH_INEG: op = "-"; break;
3100 case WINED3DSIH_NOT: op = "~"; break;
3101 default:
3102 op = "<unhandled operator>";
3103 ERR("Unhandled opcode %s.\n",
3104 debug_d3dshaderinstructionhandler(ins->handler_idx));
3105 break;
3108 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3109 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3110 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3113 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
3115 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3116 struct glsl_src_param src0_param;
3117 struct glsl_src_param src1_param;
3118 DWORD write_mask;
3120 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
3121 * not, we can emulate it. */
3122 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3123 FIXME("64-bit integer multiplies not implemented.\n");
3125 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3127 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3128 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3129 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3131 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3132 src0_param.param_str, src1_param.param_str);
3136 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3138 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3139 struct glsl_src_param src0_param, src1_param;
3140 DWORD write_mask;
3142 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3144 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3146 char dst_mask[6];
3148 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
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, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3152 dst_mask, src0_param.param_str, src1_param.param_str);
3154 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3155 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3156 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3157 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3159 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3160 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3162 else
3164 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3165 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3166 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3167 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3170 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3172 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3173 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3174 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3175 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3179 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3180 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3182 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3183 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3184 struct glsl_src_param src0_param;
3185 DWORD write_mask;
3187 write_mask = shader_glsl_append_dst(buffer, ins);
3188 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3190 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3191 * shader versions WINED3DSIO_MOVA is used for this. */
3192 if (ins->ctx->reg_maps->shader_version.major == 1
3193 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3194 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3196 /* This is a simple floor() */
3197 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3198 if (mask_size > 1) {
3199 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3200 } else {
3201 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3204 else if(ins->handler_idx == WINED3DSIH_MOVA)
3206 /* We need to *round* to the nearest int here. */
3207 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3209 if (gl_info->supported[EXT_GPU_SHADER4])
3211 if (mask_size > 1)
3212 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3213 else
3214 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3216 else
3218 if (mask_size > 1)
3219 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3220 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3221 else
3222 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3223 src0_param.param_str, src0_param.param_str);
3226 else
3228 shader_addline(buffer, "%s);\n", src0_param.param_str);
3232 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3233 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3235 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3236 struct glsl_src_param src0_param;
3237 struct glsl_src_param src1_param;
3238 DWORD dst_write_mask, src_write_mask;
3239 unsigned int dst_size;
3241 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3242 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3244 /* dp4 works on vec4, dp3 on vec3, etc. */
3245 if (ins->handler_idx == WINED3DSIH_DP4)
3246 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3247 else if (ins->handler_idx == WINED3DSIH_DP3)
3248 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3249 else
3250 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3252 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3253 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3255 if (dst_size > 1) {
3256 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3257 } else {
3258 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3262 /* Note that this instruction has some restrictions. The destination write mask
3263 * can't contain the w component, and the source swizzles have to be .xyzw */
3264 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3266 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3267 struct glsl_src_param src0_param;
3268 struct glsl_src_param src1_param;
3269 char dst_mask[6];
3271 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3272 shader_glsl_append_dst(ins->ctx->buffer, ins);
3273 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3274 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3275 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3278 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3280 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3283 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3284 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3285 * GLSL uses the value as-is. */
3286 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3288 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3289 struct glsl_src_param src0_param;
3290 struct glsl_src_param src1_param;
3291 DWORD dst_write_mask;
3292 unsigned int dst_size;
3294 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3295 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3297 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3298 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3300 if (dst_size > 1)
3302 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3303 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3305 else
3307 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3308 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3312 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3313 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3315 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3316 struct glsl_src_param src_param;
3317 const char *instruction;
3318 DWORD write_mask;
3319 unsigned i;
3321 /* Determine the GLSL function to use based on the opcode */
3322 /* TODO: Possibly make this a table for faster lookups */
3323 switch (ins->handler_idx)
3325 case WINED3DSIH_ABS: instruction = "abs"; break;
3326 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3327 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3328 case WINED3DSIH_FRC: instruction = "fract"; break;
3329 case WINED3DSIH_IMAX: instruction = "max"; break;
3330 case WINED3DSIH_IMIN: instruction = "min"; break;
3331 case WINED3DSIH_MAX: instruction = "max"; break;
3332 case WINED3DSIH_MIN: instruction = "min"; break;
3333 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3334 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
3335 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
3336 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3337 default: instruction = "";
3338 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3339 break;
3342 write_mask = shader_glsl_append_dst(buffer, ins);
3344 shader_addline(buffer, "%s(", instruction);
3346 if (ins->src_count)
3348 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3349 shader_addline(buffer, "%s", src_param.param_str);
3350 for (i = 1; i < ins->src_count; ++i)
3352 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3353 shader_addline(buffer, ", %s", src_param.param_str);
3357 shader_addline(buffer, "));\n");
3360 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3362 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3364 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3365 struct glsl_src_param src_param;
3366 unsigned int mask_size;
3367 DWORD write_mask;
3368 char dst_mask[6];
3370 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3371 mask_size = shader_glsl_get_write_mask_size(write_mask);
3372 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3374 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3375 src_param.param_str, src_param.param_str);
3376 shader_glsl_append_dst(buffer, ins);
3378 if (mask_size > 1)
3380 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3381 mask_size, src_param.param_str);
3383 else
3385 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3386 src_param.param_str);
3390 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3392 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3393 ins->ctx->reg_maps->shader_version.minor);
3394 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3395 struct glsl_src_param src0_param;
3396 const char *prefix, *suffix;
3397 unsigned int dst_size;
3398 DWORD dst_write_mask;
3400 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3401 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3403 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
3404 dst_write_mask = WINED3DSP_WRITEMASK_3;
3406 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
3408 switch (ins->handler_idx)
3410 case WINED3DSIH_EXP:
3411 case WINED3DSIH_EXPP:
3412 prefix = "exp2(";
3413 suffix = ")";
3414 break;
3416 case WINED3DSIH_LOG:
3417 case WINED3DSIH_LOGP:
3418 prefix = "log2(abs(";
3419 suffix = "))";
3420 break;
3422 case WINED3DSIH_RCP:
3423 prefix = "1.0 / ";
3424 suffix = "";
3425 break;
3427 case WINED3DSIH_RSQ:
3428 prefix = "inversesqrt(abs(";
3429 suffix = "))";
3430 break;
3432 default:
3433 prefix = "";
3434 suffix = "";
3435 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3436 break;
3439 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
3440 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3441 else
3442 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3445 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3446 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3447 * dst.x = 2^(floor(src))
3448 * dst.y = src - floor(src)
3449 * dst.z = 2^src (partial precision is allowed, but optional)
3450 * dst.w = 1.0;
3451 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3452 * dst = 2^src; (partial precision is allowed, but optional)
3454 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
3456 if (ins->ctx->reg_maps->shader_version.major < 2)
3458 struct glsl_src_param src_param;
3459 char dst_mask[6];
3461 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
3463 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
3464 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
3465 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
3466 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
3468 shader_glsl_append_dst(ins->ctx->buffer, ins);
3469 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3470 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
3471 return;
3474 shader_glsl_scalar_op(ins);
3477 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
3478 const char *vector_constructor, const char *scalar_constructor)
3480 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3481 struct glsl_src_param src_param;
3482 unsigned int mask_size;
3483 DWORD write_mask;
3485 write_mask = shader_glsl_append_dst(buffer, ins);
3486 mask_size = shader_glsl_get_write_mask_size(write_mask);
3487 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3489 if (mask_size > 1)
3490 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
3491 else
3492 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
3495 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
3497 shader_glsl_cast(ins, "ivec", "int");
3500 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
3502 shader_glsl_cast(ins, "uvec", "uint");
3505 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
3507 shader_glsl_cast(ins, "vec", "float");
3510 /** Process signed comparison opcodes in GLSL. */
3511 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
3513 struct glsl_src_param src0_param;
3514 struct glsl_src_param src1_param;
3515 DWORD write_mask;
3516 unsigned int mask_size;
3518 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3519 mask_size = shader_glsl_get_write_mask_size(write_mask);
3520 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3521 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3523 if (mask_size > 1) {
3524 const char *compare;
3526 switch(ins->handler_idx)
3528 case WINED3DSIH_SLT: compare = "lessThan"; break;
3529 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
3530 default: compare = "";
3531 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3534 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
3535 src0_param.param_str, src1_param.param_str);
3536 } else {
3537 switch(ins->handler_idx)
3539 case WINED3DSIH_SLT:
3540 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3541 * to return 0.0 but step returns 1.0 because step is not < x
3542 * An alternative is a bvec compare padded with an unused second component.
3543 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3544 * issue. Playing with not() is not possible either because not() does not accept
3545 * a scalar.
3547 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
3548 src0_param.param_str, src1_param.param_str);
3549 break;
3550 case WINED3DSIH_SGE:
3551 /* Here we can use the step() function and safe a conditional */
3552 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
3553 break;
3554 default:
3555 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3561 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
3563 const char *condition_prefix, *condition_suffix;
3564 struct wined3d_shader_dst_param dst;
3565 struct glsl_src_param src0_param;
3566 struct glsl_src_param src1_param;
3567 struct glsl_src_param src2_param;
3568 BOOL temp_destination = FALSE;
3569 DWORD cmp_channel = 0;
3570 unsigned int i, j;
3571 char mask_char[6];
3572 DWORD write_mask;
3574 switch (ins->handler_idx)
3576 case WINED3DSIH_CMP:
3577 condition_prefix = "";
3578 condition_suffix = " >= 0.0";
3579 break;
3581 case WINED3DSIH_CND:
3582 condition_prefix = "";
3583 condition_suffix = " > 0.5";
3584 break;
3586 case WINED3DSIH_MOVC:
3587 condition_prefix = "bool(";
3588 condition_suffix = ")";
3589 break;
3591 default:
3592 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3593 condition_prefix = "<unhandled prefix>";
3594 condition_suffix = "<unhandled suffix>";
3595 break;
3598 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
3600 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3601 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3602 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3603 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3605 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3606 condition_prefix, src0_param.param_str, condition_suffix,
3607 src1_param.param_str, src2_param.param_str);
3608 return;
3611 dst = ins->dst[0];
3613 /* Splitting the instruction up in multiple lines imposes a problem:
3614 * The first lines may overwrite source parameters of the following lines.
3615 * Deal with that by using a temporary destination register if needed. */
3616 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
3617 && ins->src[0].reg.type == dst.reg.type)
3618 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
3619 && ins->src[1].reg.type == dst.reg.type)
3620 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
3621 && ins->src[2].reg.type == dst.reg.type))
3622 temp_destination = TRUE;
3624 /* Cycle through all source0 channels. */
3625 for (i = 0; i < 4; ++i)
3627 write_mask = 0;
3628 /* Find the destination channels which use the current source0 channel. */
3629 for (j = 0; j < 4; ++j)
3631 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
3633 write_mask |= WINED3DSP_WRITEMASK_0 << j;
3634 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
3637 dst.write_mask = ins->dst[0].write_mask & write_mask;
3639 if (temp_destination)
3641 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
3642 continue;
3643 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
3645 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
3646 continue;
3648 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
3649 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3650 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3652 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3653 condition_prefix, src0_param.param_str, condition_suffix,
3654 src1_param.param_str, src2_param.param_str);
3657 if (temp_destination)
3659 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3660 shader_glsl_append_dst(ins->ctx->buffer, ins);
3661 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3665 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3666 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3667 * the compare is done per component of src0. */
3668 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3670 struct glsl_src_param src0_param;
3671 struct glsl_src_param src1_param;
3672 struct glsl_src_param src2_param;
3673 DWORD write_mask;
3674 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3675 ins->ctx->reg_maps->shader_version.minor);
3677 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3679 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3680 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3681 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3682 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3684 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
3685 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3686 else
3687 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3688 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3689 return;
3692 shader_glsl_conditional_move(ins);
3695 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3696 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3698 struct glsl_src_param src0_param;
3699 struct glsl_src_param src1_param;
3700 struct glsl_src_param src2_param;
3701 DWORD write_mask;
3703 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3704 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3705 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3706 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3707 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3708 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3711 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3712 Vertex shaders to GLSL codes */
3713 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3715 int i;
3716 int nComponents = 0;
3717 struct wined3d_shader_dst_param tmp_dst = {{0}};
3718 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3719 struct wined3d_shader_instruction tmp_ins;
3721 memset(&tmp_ins, 0, sizeof(tmp_ins));
3723 /* Set constants for the temporary argument */
3724 tmp_ins.ctx = ins->ctx;
3725 tmp_ins.dst_count = 1;
3726 tmp_ins.dst = &tmp_dst;
3727 tmp_ins.src_count = 2;
3728 tmp_ins.src = tmp_src;
3730 switch(ins->handler_idx)
3732 case WINED3DSIH_M4x4:
3733 nComponents = 4;
3734 tmp_ins.handler_idx = WINED3DSIH_DP4;
3735 break;
3736 case WINED3DSIH_M4x3:
3737 nComponents = 3;
3738 tmp_ins.handler_idx = WINED3DSIH_DP4;
3739 break;
3740 case WINED3DSIH_M3x4:
3741 nComponents = 4;
3742 tmp_ins.handler_idx = WINED3DSIH_DP3;
3743 break;
3744 case WINED3DSIH_M3x3:
3745 nComponents = 3;
3746 tmp_ins.handler_idx = WINED3DSIH_DP3;
3747 break;
3748 case WINED3DSIH_M3x2:
3749 nComponents = 2;
3750 tmp_ins.handler_idx = WINED3DSIH_DP3;
3751 break;
3752 default:
3753 break;
3756 tmp_dst = ins->dst[0];
3757 tmp_src[0] = ins->src[0];
3758 tmp_src[1] = ins->src[1];
3759 for (i = 0; i < nComponents; ++i)
3761 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3762 shader_glsl_dot(&tmp_ins);
3763 ++tmp_src[1].reg.idx[0].offset;
3768 The LRP instruction performs a component-wise linear interpolation
3769 between the second and third operands using the first operand as the
3770 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3771 This is equivalent to mix(src2, src1, src0);
3773 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3775 struct glsl_src_param src0_param;
3776 struct glsl_src_param src1_param;
3777 struct glsl_src_param src2_param;
3778 DWORD write_mask;
3780 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3782 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3783 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3784 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3786 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3787 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3790 /** Process the WINED3DSIO_LIT instruction in GLSL:
3791 * dst.x = dst.w = 1.0
3792 * dst.y = (src0.x > 0) ? src0.x
3793 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3794 * where src.w is clamped at +- 128
3796 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3798 struct glsl_src_param src0_param;
3799 struct glsl_src_param src1_param;
3800 struct glsl_src_param src3_param;
3801 char dst_mask[6];
3803 shader_glsl_append_dst(ins->ctx->buffer, ins);
3804 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3806 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3807 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3808 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3810 /* The sdk specifies the instruction like this
3811 * dst.x = 1.0;
3812 * if(src.x > 0.0) dst.y = src.x
3813 * else dst.y = 0.0.
3814 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3815 * else dst.z = 0.0;
3816 * dst.w = 1.0;
3817 * (where power = src.w clamped between -128 and 128)
3819 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3820 * dst.x = 1.0 ... No further explanation needed
3821 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3822 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3823 * dst.w = 1.0. ... Nothing fancy.
3825 * So we still have one conditional in there. So do this:
3826 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3828 * 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),
3829 * 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.
3830 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3832 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3833 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3834 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3836 shader_addline(ins->ctx->buffer,
3837 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3838 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3839 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3840 src0_param.param_str, src3_param.param_str, dst_mask);
3843 /** Process the WINED3DSIO_DST instruction in GLSL:
3844 * dst.x = 1.0
3845 * dst.y = src0.x * src0.y
3846 * dst.z = src0.z
3847 * dst.w = src1.w
3849 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3851 struct glsl_src_param src0y_param;
3852 struct glsl_src_param src0z_param;
3853 struct glsl_src_param src1y_param;
3854 struct glsl_src_param src1w_param;
3855 char dst_mask[6];
3857 shader_glsl_append_dst(ins->ctx->buffer, ins);
3858 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3860 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3861 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3862 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3863 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3865 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3866 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3869 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3870 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3871 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3873 * dst.x = cos(src0.?)
3874 * dst.y = sin(src0.?)
3875 * dst.z = dst.z
3876 * dst.w = dst.w
3878 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3880 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3881 struct glsl_src_param src0_param;
3882 DWORD write_mask;
3884 if (ins->ctx->reg_maps->shader_version.major < 4)
3886 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3888 write_mask = shader_glsl_append_dst(buffer, ins);
3889 switch (write_mask)
3891 case WINED3DSP_WRITEMASK_0:
3892 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3893 break;
3895 case WINED3DSP_WRITEMASK_1:
3896 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3897 break;
3899 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3900 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3901 src0_param.param_str, src0_param.param_str);
3902 break;
3904 default:
3905 ERR("Write mask should be .x, .y or .xy\n");
3906 break;
3909 return;
3912 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3915 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3917 char dst_mask[6];
3919 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3920 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3921 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3923 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3924 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3925 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3927 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3928 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3930 else
3932 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3933 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3934 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3937 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3939 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3940 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3941 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3945 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3946 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3947 * generate invalid code
3949 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3951 struct glsl_src_param src0_param;
3952 DWORD write_mask;
3954 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3955 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3957 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3960 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3961 * Start a for() loop where src1.y is the initial value of aL,
3962 * increment aL by src1.z for a total of src1.x iterations.
3963 * Need to use a temporary variable for this operation.
3965 /* FIXME: I don't think nested loops will work correctly this way. */
3966 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3968 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3969 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3970 const struct wined3d_shader *shader = ins->ctx->shader;
3971 const struct wined3d_shader_lconst *constant;
3972 struct glsl_src_param src1_param;
3973 const DWORD *control_values = NULL;
3975 if (ins->ctx->reg_maps->shader_version.major < 4)
3977 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3979 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3980 * class hardware doesn't support real varying indexing, but Microsoft
3981 * designed this feature for Shader model 2.x+. If the loop control is
3982 * known at compile time, the GLSL compiler can unroll the loop, and
3983 * replace indirect addressing with direct addressing. */
3984 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3986 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3988 if (constant->idx == ins->src[1].reg.idx[0].offset)
3990 control_values = constant->value;
3991 break;
3996 if (control_values)
3998 struct wined3d_shader_loop_control loop_control;
3999 loop_control.count = control_values[0];
4000 loop_control.start = control_values[1];
4001 loop_control.step = (int)control_values[2];
4003 if (loop_control.step > 0)
4005 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4006 loop_state->current_depth, loop_control.start,
4007 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
4008 loop_state->current_depth, loop_control.step);
4010 else if (loop_control.step < 0)
4012 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4013 loop_state->current_depth, loop_control.start,
4014 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
4015 loop_state->current_depth, loop_control.step);
4017 else
4019 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4020 loop_state->current_depth, loop_control.start, loop_state->current_depth,
4021 loop_state->current_depth, loop_control.count,
4022 loop_state->current_depth);
4025 else
4027 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4028 loop_state->current_depth, loop_state->current_reg,
4029 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
4030 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
4033 ++loop_state->current_reg;
4035 else
4037 shader_addline(buffer, "for (;;)\n{\n");
4040 ++loop_state->current_depth;
4043 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4045 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4047 shader_addline(ins->ctx->buffer, "}\n");
4049 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4051 --loop_state->current_depth;
4052 --loop_state->current_reg;
4055 if (ins->handler_idx == WINED3DSIH_ENDREP)
4057 --loop_state->current_depth;
4061 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4063 const struct wined3d_shader *shader = ins->ctx->shader;
4064 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4065 const struct wined3d_shader_lconst *constant;
4066 struct glsl_src_param src0_param;
4067 const DWORD *control_values = NULL;
4069 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4070 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4072 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4074 if (constant->idx == ins->src[0].reg.idx[0].offset)
4076 control_values = constant->value;
4077 break;
4082 if (control_values)
4084 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4085 loop_state->current_depth, loop_state->current_depth,
4086 control_values[0], loop_state->current_depth);
4088 else
4090 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4091 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4092 loop_state->current_depth, loop_state->current_depth,
4093 src0_param.param_str, loop_state->current_depth);
4096 ++loop_state->current_depth;
4099 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
4101 struct glsl_src_param src0_param;
4103 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4104 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
4107 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
4109 struct glsl_src_param src0_param;
4110 struct glsl_src_param src1_param;
4112 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4113 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4115 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
4116 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4119 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
4121 shader_addline(ins->ctx->buffer, "} else {\n");
4124 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
4126 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
4129 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
4131 shader_addline(ins->ctx->buffer, "break;\n");
4134 /* FIXME: According to MSDN the compare is done per component. */
4135 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
4137 struct glsl_src_param src0_param;
4138 struct glsl_src_param src1_param;
4140 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4141 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4143 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
4144 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4147 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
4149 struct glsl_src_param src_param;
4151 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4152 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
4155 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
4157 shader_addline(ins->ctx->buffer, "}\n");
4158 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
4161 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
4163 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
4166 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
4168 struct glsl_src_param src1_param;
4170 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4171 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
4172 src1_param.param_str, ins->src[0].reg.idx[0].offset);
4175 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
4177 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
4178 * function only suppresses the unhandled instruction warning
4182 /*********************************************
4183 * Pixel Shader Specific Code begins here
4184 ********************************************/
4185 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
4187 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4188 ins->ctx->reg_maps->shader_version.minor);
4189 struct glsl_sample_function sample_function;
4190 DWORD sample_flags = 0;
4191 DWORD resource_idx;
4192 DWORD mask = 0, swizzle;
4193 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4195 /* 1.0-1.4: Use destination register as sampler source.
4196 * 2.0+: Use provided sampler source. */
4197 if (shader_version < WINED3D_SHADER_VERSION(2,0))
4198 resource_idx = ins->dst[0].reg.idx[0].offset;
4199 else
4200 resource_idx = ins->src[1].reg.idx[0].offset;
4202 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4204 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4205 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4206 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4208 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4209 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4211 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4212 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4214 case WINED3D_TTFF_COUNT1:
4215 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4216 break;
4217 case WINED3D_TTFF_COUNT2:
4218 mask = WINED3DSP_WRITEMASK_1;
4219 break;
4220 case WINED3D_TTFF_COUNT3:
4221 mask = WINED3DSP_WRITEMASK_2;
4222 break;
4223 case WINED3D_TTFF_COUNT4:
4224 case WINED3D_TTFF_DISABLE:
4225 mask = WINED3DSP_WRITEMASK_3;
4226 break;
4230 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4232 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4234 if (src_mod == WINED3DSPSM_DZ) {
4235 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4236 mask = WINED3DSP_WRITEMASK_2;
4237 } else if (src_mod == WINED3DSPSM_DW) {
4238 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4239 mask = WINED3DSP_WRITEMASK_3;
4242 else
4244 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4245 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4247 /* ps 2.0 texldp instruction always divides by the fourth component. */
4248 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4249 mask = WINED3DSP_WRITEMASK_3;
4253 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
4254 mask |= sample_function.coord_mask;
4255 sample_function.coord_mask = mask;
4257 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4258 else swizzle = ins->src[1].swizzle;
4260 /* 1.0-1.3: Use destination register as coordinate source.
4261 1.4+: Use provided coordinate source register. */
4262 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4264 char coord_mask[6];
4265 shader_glsl_write_mask_to_str(mask, coord_mask);
4266 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4267 "T%u%s", resource_idx, coord_mask);
4269 else
4271 struct glsl_src_param coord_param;
4272 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4273 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4275 struct glsl_src_param bias;
4276 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4277 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4278 NULL, "%s", coord_param.param_str);
4279 } else {
4280 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4281 "%s", coord_param.param_str);
4284 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4287 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4289 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4290 struct glsl_src_param coord_param, dx_param, dy_param;
4291 struct glsl_sample_function sample_function;
4292 DWORD sampler_idx;
4293 DWORD swizzle = ins->src[1].swizzle;
4295 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
4297 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4298 shader_glsl_tex(ins);
4299 return;
4302 sampler_idx = ins->src[1].reg.idx[0].offset;
4304 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
4305 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4306 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
4307 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
4309 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
4310 NULL, NULL, "%s", coord_param.param_str);
4311 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4314 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4316 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4317 struct glsl_src_param coord_param, lod_param;
4318 struct glsl_sample_function sample_function;
4319 DWORD sampler_idx;
4320 DWORD swizzle = ins->src[1].swizzle;
4322 sampler_idx = ins->src[1].reg.idx[0].offset;
4324 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
4325 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4327 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4329 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
4330 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
4332 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4333 * However, the NVIDIA drivers allow them in fragment shaders as well,
4334 * even without the appropriate extension. */
4335 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4337 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
4338 "%s", coord_param.param_str);
4339 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4342 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4343 unsigned int resource_idx, unsigned int sampler_idx)
4345 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4346 unsigned int i;
4348 for (i = 0; i < sampler_map->count; ++i)
4350 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4351 return entries[i].bind_idx;
4354 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4356 return ~0u;
4359 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
4361 static const unsigned int texture_size_component_count[] =
4363 0, /* WINED3D_SHADER_RESOURCE_NONE */
4364 1, /* WINED3D_SHADER_RESOURCE_BUFFER */
4365 1, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
4366 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
4367 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
4368 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
4369 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
4370 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
4371 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
4372 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
4375 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
4376 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4377 enum wined3d_shader_resource_type resource_type;
4378 unsigned int resource_idx, sampler_bind_idx, i;
4379 enum wined3d_data_type dst_data_type;
4380 struct glsl_src_param lod_param;
4381 char dst_swizzle[6];
4382 DWORD write_mask;
4384 dst_data_type = ins->dst[0].reg.data_type;
4385 if (ins->flags == WINED3DSI_RESINFO_UINT)
4386 dst_data_type = WINED3D_DATA_UINT;
4387 else if (ins->flags)
4388 FIXME("Unhandled flags %#x.\n", ins->flags);
4390 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
4391 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
4393 resource_idx = ins->src[1].reg.idx[0].offset;
4394 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4395 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
4396 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4397 resource_idx, WINED3D_SAMPLER_DEFAULT);
4399 if (resource_type >= ARRAY_SIZE(texture_size_component_count))
4401 ERR("Unexpected resource type %#x.\n", resource_type);
4402 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
4405 if (dst_data_type == WINED3D_DATA_UINT)
4406 shader_addline(ins->ctx->buffer, "uvec4(");
4407 else
4408 shader_addline(ins->ctx->buffer, "vec4(");
4410 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
4411 shader_glsl_get_prefix(version->type), sampler_bind_idx, lod_param.param_str);
4413 for (i = 0; i < 3 - texture_size_component_count[resource_type]; ++i)
4414 shader_addline(ins->ctx->buffer, "0, ");
4416 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
4418 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
4419 shader_glsl_get_prefix(version->type), sampler_bind_idx);
4421 else
4423 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
4424 shader_addline(ins->ctx->buffer, "1");
4427 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
4430 /* FIXME: The current implementation does not handle multisample textures correctly. */
4431 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
4433 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4434 struct glsl_src_param coord_param, lod_param;
4435 struct glsl_sample_function sample_function;
4436 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
4438 if (wined3d_shader_instruction_has_texel_offset(ins))
4439 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4441 resource_idx = ins->src[1].reg.idx[0].offset;
4442 sampler_idx = WINED3D_SAMPLER_DEFAULT;
4444 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4445 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4446 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4447 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4448 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
4449 NULL, NULL, lod_param.param_str, &ins->texel_offset, "%s", coord_param.param_str);
4450 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4453 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
4455 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
4456 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
4457 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4458 struct glsl_sample_function sample_function;
4459 DWORD flags = 0;
4461 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
4462 flags |= WINED3D_GLSL_SAMPLE_GRAD;
4463 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
4464 flags |= WINED3D_GLSL_SAMPLE_LOD;
4465 if (wined3d_shader_instruction_has_texel_offset(ins))
4466 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4468 resource_idx = ins->src[1].reg.idx[0].offset;
4469 sampler_idx = ins->src[2].reg.idx[0].offset;
4471 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4472 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4474 switch (ins->handler_idx)
4476 case WINED3DSIH_SAMPLE:
4477 break;
4478 case WINED3DSIH_SAMPLE_B:
4479 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4480 lod_param_str = lod_param.param_str;
4481 break;
4482 case WINED3DSIH_SAMPLE_GRAD:
4483 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dx_param);
4484 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.coord_mask, &dy_param);
4485 dx_param_str = dx_param.param_str;
4486 dy_param_str = dy_param.param_str;
4487 break;
4488 case WINED3DSIH_SAMPLE_LOD:
4489 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4490 lod_param_str = lod_param.param_str;
4491 break;
4492 default:
4493 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4494 break;
4497 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4498 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
4499 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
4500 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4503 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
4505 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4506 struct glsl_src_param coord_param, compare_param;
4507 struct glsl_sample_function sample_function;
4508 const char *lod_param = NULL;
4509 DWORD flags = 0;
4510 UINT coord_size;
4512 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
4514 lod_param = "0";
4515 flags |= WINED3D_GLSL_SAMPLE_LOD;
4518 if (wined3d_shader_instruction_has_texel_offset(ins))
4519 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4521 resource_idx = ins->src[1].reg.idx[0].offset;
4522 sampler_idx = ins->src[2].reg.idx[0].offset;
4524 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4525 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4526 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
4527 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
4528 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4529 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
4530 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
4531 coord_size, coord_param.param_str, compare_param.param_str);
4532 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4535 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
4537 /* FIXME: Make this work for more than just 2D textures */
4538 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4539 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4541 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
4543 char dst_mask[6];
4545 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4546 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
4547 ins->dst[0].reg.idx[0].offset, dst_mask);
4549 else
4551 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4552 DWORD reg = ins->src[0].reg.idx[0].offset;
4553 char dst_swizzle[6];
4555 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
4557 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
4559 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4560 struct glsl_src_param div_param;
4561 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
4563 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
4565 if (mask_size > 1)
4566 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4567 else
4568 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4570 else
4572 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
4577 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4578 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4579 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4580 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
4582 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4583 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4584 struct glsl_sample_function sample_function;
4585 struct glsl_src_param src0_param;
4586 UINT mask_size;
4588 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4590 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4591 * scalar, and projected sampling would require 4.
4593 * It is a dependent read - not valid with conditional NP2 textures
4595 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4596 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4598 switch(mask_size)
4600 case 1:
4601 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4602 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
4603 break;
4605 case 2:
4606 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4607 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
4608 break;
4610 case 3:
4611 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4612 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
4613 break;
4615 default:
4616 FIXME("Unexpected mask size %u\n", mask_size);
4617 break;
4619 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4622 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4623 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4624 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
4626 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4627 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4628 struct glsl_src_param src0_param;
4629 DWORD dst_mask;
4630 unsigned int mask_size;
4632 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4633 mask_size = shader_glsl_get_write_mask_size(dst_mask);
4634 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4636 if (mask_size > 1) {
4637 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
4638 } else {
4639 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
4643 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4644 * Calculate the depth as dst.x / dst.y */
4645 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
4647 struct glsl_dst_param dst_param;
4649 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4651 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4652 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4653 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4654 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4655 * >= 1.0 or < 0.0
4657 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4658 dst_param.reg_name, dst_param.reg_name);
4661 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4662 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4663 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4664 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4666 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
4668 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4669 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4670 struct glsl_src_param src0_param;
4672 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4674 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
4675 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4678 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4679 * Calculate the 1st of a 2-row matrix multiplication. */
4680 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
4682 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4683 DWORD reg = ins->dst[0].reg.idx[0].offset;
4684 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
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.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4691 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4692 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4693 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
4695 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4696 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4697 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4698 DWORD reg = ins->dst[0].reg.idx[0].offset;
4699 struct glsl_src_param src0_param;
4701 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4702 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
4703 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
4706 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
4708 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4709 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4710 struct glsl_sample_function sample_function;
4711 DWORD reg = ins->dst[0].reg.idx[0].offset;
4712 struct glsl_src_param src0_param;
4714 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4715 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4717 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4719 /* Sample the texture using the calculated coordinates */
4720 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
4721 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4724 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
4725 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
4726 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
4728 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4729 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4730 struct glsl_sample_function sample_function;
4731 DWORD reg = ins->dst[0].reg.idx[0].offset;
4732 struct glsl_src_param src0_param;
4734 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4735 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4737 /* Dependent read, not valid with conditional NP2 */
4738 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4740 /* Sample the texture using the calculated coordinates */
4741 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
4742 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4744 tex_mx->current_row = 0;
4747 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4748 * Perform the 3rd row of a 3x3 matrix multiply */
4749 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
4751 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4752 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4753 DWORD reg = ins->dst[0].reg.idx[0].offset;
4754 struct glsl_src_param src0_param;
4755 char dst_mask[6];
4757 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4759 shader_glsl_append_dst(ins->ctx->buffer, ins);
4760 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4761 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
4763 tex_mx->current_row = 0;
4766 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4767 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4768 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
4770 struct glsl_src_param src0_param;
4771 struct glsl_src_param src1_param;
4772 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4773 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4774 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4775 struct glsl_sample_function sample_function;
4776 DWORD reg = ins->dst[0].reg.idx[0].offset;
4777 char coord_mask[6];
4779 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4780 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4782 /* Perform the last matrix multiply operation */
4783 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4784 /* Reflection calculation */
4785 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
4787 /* Dependent read, not valid with conditional NP2 */
4788 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4789 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4791 /* Sample the texture */
4792 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4793 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
4794 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4796 tex_mx->current_row = 0;
4799 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4800 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4801 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
4803 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4804 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4805 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4806 struct glsl_sample_function sample_function;
4807 DWORD reg = ins->dst[0].reg.idx[0].offset;
4808 struct glsl_src_param src0_param;
4809 char coord_mask[6];
4811 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4813 /* Perform the last matrix multiply operation */
4814 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
4816 /* Construct the eye-ray vector from w coordinates */
4817 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
4818 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
4819 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4821 /* Dependent read, not valid with conditional NP2 */
4822 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4823 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4825 /* Sample the texture using the calculated coordinates */
4826 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4827 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
4828 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4830 tex_mx->current_row = 0;
4833 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4834 * Apply a fake bump map transform.
4835 * texbem is pshader <= 1.3 only, this saves a few version checks
4837 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
4839 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4840 struct glsl_sample_function sample_function;
4841 struct glsl_src_param coord_param;
4842 DWORD sampler_idx;
4843 DWORD mask;
4844 DWORD flags;
4845 char coord_mask[6];
4847 sampler_idx = ins->dst[0].reg.idx[0].offset;
4848 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4849 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4851 /* Dependent read, not valid with conditional NP2 */
4852 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4853 mask = sample_function.coord_mask;
4855 shader_glsl_write_mask_to_str(mask, coord_mask);
4857 /* With projected textures, texbem only divides the static texture coord,
4858 * not the displacement, so we can't let GL handle this. */
4859 if (flags & WINED3D_PSARGS_PROJECTED)
4861 DWORD div_mask=0;
4862 char coord_div_mask[3];
4863 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4865 case WINED3D_TTFF_COUNT1:
4866 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4867 break;
4868 case WINED3D_TTFF_COUNT2:
4869 div_mask = WINED3DSP_WRITEMASK_1;
4870 break;
4871 case WINED3D_TTFF_COUNT3:
4872 div_mask = WINED3DSP_WRITEMASK_2;
4873 break;
4874 case WINED3D_TTFF_COUNT4:
4875 case WINED3D_TTFF_DISABLE:
4876 div_mask = WINED3DSP_WRITEMASK_3;
4877 break;
4879 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4880 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4883 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4885 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4886 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4887 coord_param.param_str, coord_mask);
4889 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4891 struct glsl_src_param luminance_param;
4892 struct glsl_dst_param dst_param;
4894 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4895 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4897 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4898 dst_param.reg_name, dst_param.mask_str,
4899 luminance_param.param_str, sampler_idx, sampler_idx);
4901 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4904 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4906 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4907 struct glsl_src_param src0_param, src1_param;
4909 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4910 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4912 shader_glsl_append_dst(ins->ctx->buffer, ins);
4913 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4914 src0_param.param_str, sampler_idx, src1_param.param_str);
4917 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4918 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4919 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4921 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4922 struct glsl_sample_function sample_function;
4923 struct glsl_src_param src0_param;
4925 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4927 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4928 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4929 "%s.wx", src0_param.reg_name);
4930 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4933 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4934 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4935 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4937 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4938 struct glsl_sample_function sample_function;
4939 struct glsl_src_param src0_param;
4941 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4943 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4944 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4945 "%s.yz", src0_param.reg_name);
4946 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4949 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4950 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4951 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4953 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4954 struct glsl_sample_function sample_function;
4955 struct glsl_src_param src0_param;
4957 /* Dependent read, not valid with conditional NP2 */
4958 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4959 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4961 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4962 "%s", src0_param.param_str);
4963 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4966 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4967 * If any of the first 3 components are < 0, discard this pixel */
4968 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4970 if (ins->ctx->reg_maps->shader_version.major >= 4)
4972 struct glsl_src_param src_param;
4974 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4975 shader_addline(ins->ctx->buffer, "if (bool(floatBitsToUint(%s))) discard;\n", src_param.param_str);
4977 else
4979 struct glsl_dst_param dst_param;
4981 /* The argument is a destination parameter, and no writemasks are allowed */
4982 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4984 /* 2.0 shaders compare all 4 components in texkill. */
4985 if (ins->ctx->reg_maps->shader_version.major >= 2)
4986 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4987 /* 1.x shaders only compare the first 3 components, probably due to
4988 * the nature of the texkill instruction as a tex* instruction, and
4989 * phase, which kills all .w components. Even if all 4 components are
4990 * defined, only the first 3 are used. */
4991 else
4992 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4996 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4997 * dst = dot2(src0, src1) + src2 */
4998 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
5000 struct glsl_src_param src0_param;
5001 struct glsl_src_param src1_param;
5002 struct glsl_src_param src2_param;
5003 DWORD write_mask;
5004 unsigned int mask_size;
5006 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5007 mask_size = shader_glsl_get_write_mask_size(write_mask);
5009 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
5010 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
5011 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
5013 if (mask_size > 1) {
5014 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
5015 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
5016 } else {
5017 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
5018 src0_param.param_str, src1_param.param_str, src2_param.param_str);
5022 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
5023 const struct wined3d_shader_signature *input_signature,
5024 const struct wined3d_shader_reg_maps *reg_maps,
5025 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
5027 unsigned int i;
5029 for (i = 0; i < input_signature->element_count; ++i)
5031 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5032 const char *semantic_name;
5033 UINT semantic_idx;
5034 char reg_mask[6];
5036 /* Unused */
5037 if (!(reg_maps->input_registers & (1u << input->register_idx)))
5038 continue;
5040 semantic_name = input->semantic_name;
5041 semantic_idx = input->semantic_idx;
5042 shader_glsl_write_mask_to_str(input->mask, reg_mask);
5044 if (args->vp_mode == vertexshader)
5046 if (input->sysval_semantic == WINED3D_SV_POSITION)
5047 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
5048 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5049 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5050 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
5051 else
5052 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
5053 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5054 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
5056 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5058 if (args->pointsprite)
5059 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
5060 shader->u.ps.input_reg_map[input->register_idx]);
5061 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
5062 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
5063 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5064 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
5065 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
5066 else
5067 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5068 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5070 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
5072 if (!semantic_idx)
5073 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
5074 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5075 else if (semantic_idx == 1)
5076 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
5077 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5078 else
5079 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5080 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5082 else
5084 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5085 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5090 /*********************************************
5091 * Vertex Shader Specific Code begins here
5092 ********************************************/
5094 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
5096 struct glsl_program_key key;
5098 key.vs_id = entry->vs.id;
5099 key.gs_id = entry->gs.id;
5100 key.ps_id = entry->ps.id;
5102 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
5104 ERR("Failed to insert program entry.\n");
5108 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
5109 GLuint vs_id, GLuint gs_id, GLuint ps_id)
5111 struct wine_rb_entry *entry;
5112 struct glsl_program_key key;
5114 key.vs_id = vs_id;
5115 key.gs_id = gs_id;
5116 key.ps_id = ps_id;
5118 entry = wine_rb_get(&priv->program_lookup, &key);
5119 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
5122 /* Context activation is done by the caller. */
5123 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
5124 struct glsl_shader_prog_link *entry)
5126 struct glsl_program_key key;
5128 key.vs_id = entry->vs.id;
5129 key.gs_id = entry->gs.id;
5130 key.ps_id = entry->ps.id;
5131 wine_rb_remove(&priv->program_lookup, &key);
5133 GL_EXTCALL(glDeleteProgram(entry->id));
5134 if (entry->vs.id)
5135 list_remove(&entry->vs.shader_entry);
5136 if (entry->gs.id)
5137 list_remove(&entry->gs.shader_entry);
5138 if (entry->ps.id)
5139 list_remove(&entry->ps.shader_entry);
5140 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
5141 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
5142 HeapFree(GetProcessHeap(), 0, entry);
5145 static void handle_ps3_input(struct shader_glsl_priv *priv,
5146 const struct wined3d_gl_info *gl_info, const DWORD *map,
5147 const struct wined3d_shader_signature *input_signature,
5148 const struct wined3d_shader_reg_maps *reg_maps_in,
5149 const struct wined3d_shader_signature *output_signature,
5150 const struct wined3d_shader_reg_maps *reg_maps_out)
5152 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5153 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5154 unsigned int i, j;
5155 DWORD *set;
5156 DWORD in_idx;
5157 unsigned int in_count = vec4_varyings(3, gl_info);
5158 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
5159 char reg_mask[6];
5160 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5162 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * max_varyings);
5164 for (i = 0; i < input_signature->element_count; ++i)
5166 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5168 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
5169 continue;
5171 in_idx = map[input->register_idx];
5172 /* Declared, but not read register */
5173 if (in_idx == ~0u)
5174 continue;
5175 if (in_idx >= max_varyings)
5177 FIXME("More input varyings declared than supported, expect issues.\n");
5178 continue;
5181 if (in_idx == in_count)
5182 string_buffer_sprintf(destination, "gl_FrontColor");
5183 else if (in_idx == in_count + 1)
5184 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5185 else
5186 string_buffer_sprintf(destination, "ps_link[%u]", in_idx);
5188 if (!set[in_idx])
5189 set[in_idx] = ~0u;
5191 for (j = 0; j < output_signature->element_count; ++j)
5193 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
5194 DWORD mask;
5196 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
5197 || input->semantic_idx != output->semantic_idx
5198 || strcmp(input->semantic_name, output->semantic_name)
5199 || !(mask = input->mask & output->mask))
5200 continue;
5202 if (set[in_idx] == ~0u)
5203 set[in_idx] = 0;
5204 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
5205 shader_glsl_write_mask_to_str(mask, reg_mask);
5207 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
5208 destination->buffer, reg_mask, output->register_idx, reg_mask);
5212 for (i = 0; i < max_varyings; ++i)
5214 unsigned int size;
5216 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
5217 continue;
5219 if (set[i] == ~0U) set[i] = 0;
5221 size = 0;
5222 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
5223 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
5224 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
5225 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
5226 reg_mask[size] = '\0';
5228 if (i == in_count)
5229 string_buffer_sprintf(destination, "gl_FrontColor");
5230 else if (i == in_count + 1)
5231 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5232 else
5233 string_buffer_sprintf(destination, "ps_link[%u]", i);
5235 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
5236 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
5239 HeapFree(GetProcessHeap(), 0, set);
5240 string_buffer_release(&priv->string_buffers, destination);
5243 /* Context activation is done by the caller. */
5244 static GLuint generate_param_reorder_function(struct shader_glsl_priv *priv,
5245 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
5246 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
5248 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5249 GLuint ret;
5250 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
5251 unsigned int i;
5252 const char *semantic_name;
5253 UINT semantic_idx;
5254 char reg_mask[6];
5255 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5257 string_buffer_clear(buffer);
5259 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &vs->reg_maps.shader_version));
5261 if (per_vertex_point_size)
5263 shader_addline(buffer, "uniform struct\n{\n");
5264 shader_addline(buffer, " float size_min;\n");
5265 shader_addline(buffer, " float size_max;\n");
5266 shader_addline(buffer, "} ffp_point;\n");
5269 if (ps_major < 3)
5271 DWORD colors_written_mask[2] = {0};
5272 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
5274 if (!legacy_context)
5276 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
5277 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
5278 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
5279 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
5282 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
5284 for (i = 0; i < vs->output_signature.element_count; ++i)
5286 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5287 DWORD write_mask;
5289 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5290 continue;
5292 semantic_name = output->semantic_name;
5293 semantic_idx = output->semantic_idx;
5294 write_mask = output->mask;
5295 shader_glsl_write_mask_to_str(write_mask, reg_mask);
5297 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
5299 if (legacy_context)
5300 shader_addline(buffer, "gl_Front%sColor%s = vs_out[%u]%s;\n",
5301 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
5302 else
5303 shader_addline(buffer, "ffp_varying_%s%s = clamp(vs_out[%u]%s, 0.0, 1.0);\n",
5304 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
5306 colors_written_mask[semantic_idx] = write_mask;
5308 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5310 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
5311 reg_mask, output->register_idx, reg_mask);
5313 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5315 if (semantic_idx < MAX_TEXTURES)
5317 shader_addline(buffer, "%s[%u]%s = vs_out[%u]%s;\n",
5318 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
5319 semantic_idx, reg_mask, output->register_idx, reg_mask);
5320 texcoords_written_mask[semantic_idx] = write_mask;
5323 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5325 shader_addline(buffer, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
5326 output->register_idx, reg_mask[1]);
5328 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
5330 shader_addline(buffer, "%s = clamp(vs_out[%u].%c, 0.0, 1.0);\n",
5331 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
5332 output->register_idx, reg_mask[1]);
5336 for (i = 0; i < 2; ++i)
5338 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5340 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5341 if (!i)
5342 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
5343 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
5344 reg_mask, reg_mask);
5345 else
5346 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
5347 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
5348 reg_mask, reg_mask);
5351 for (i = 0; i < MAX_TEXTURES; ++i)
5353 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
5354 continue;
5356 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5358 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
5359 && !texcoords_written_mask[i])
5360 continue;
5362 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5363 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
5364 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
5368 else
5370 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
5372 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
5373 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
5375 /* First, sort out position and point size. Those are not passed to the pixel shader */
5376 for (i = 0; i < vs->output_signature.element_count; ++i)
5378 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5380 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5381 continue;
5383 semantic_name = output->semantic_name;
5384 semantic_idx = output->semantic_idx;
5385 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5387 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5389 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
5390 reg_mask, output->register_idx, reg_mask);
5392 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5394 shader_addline(buffer, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
5395 output->register_idx, reg_mask[1]);
5399 /* Then, fix the pixel shader input */
5400 handle_ps3_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
5401 &ps->reg_maps, &vs->output_signature, &vs->reg_maps);
5404 shader_addline(buffer, "}\n");
5406 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5407 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
5408 shader_glsl_compile(gl_info, ret, buffer->buffer);
5410 return ret;
5413 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
5414 const struct wined3d_gl_info *gl_info)
5416 const char *output = get_fragment_output(gl_info);
5418 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
5419 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
5420 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
5421 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
5422 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
5423 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
5426 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
5427 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
5429 const char *output = get_fragment_output(gl_info);
5431 switch (mode)
5433 case WINED3D_FFP_PS_FOG_OFF:
5434 return;
5436 case WINED3D_FFP_PS_FOG_LINEAR:
5437 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
5438 break;
5440 case WINED3D_FFP_PS_FOG_EXP:
5441 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
5442 break;
5444 case WINED3D_FFP_PS_FOG_EXP2:
5445 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
5446 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
5447 break;
5449 default:
5450 ERR("Invalid fog mode %#x.\n", mode);
5451 return;
5454 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
5455 output, output);
5458 /* Context activation is done by the caller. */
5459 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
5460 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5461 const struct wined3d_shader *shader,
5462 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
5464 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5465 const struct wined3d_gl_info *gl_info = context->gl_info;
5466 const DWORD *function = shader->function;
5467 struct shader_glsl_ctx_priv priv_ctx;
5468 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5470 /* Create the hw GLSL shader object and assign it as the shader->prgId */
5471 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5473 memset(&priv_ctx, 0, sizeof(priv_ctx));
5474 priv_ctx.cur_ps_args = args;
5475 priv_ctx.cur_np2fixup_info = np2fixup_info;
5476 priv_ctx.string_buffers = string_buffers;
5478 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5480 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5481 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5482 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5483 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
5484 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5485 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5486 /* The spec says that it doesn't have to be explicitly enabled, but the
5487 * nvidia drivers write a warning if we don't do so. */
5488 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5489 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5490 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5491 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5492 if (gl_info->supported[EXT_GPU_SHADER4])
5493 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5495 /* Base Declarations */
5496 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5498 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
5500 unsigned int i;
5501 WORD map = reg_maps->texcoord;
5503 if (legacy_context)
5505 if (glsl_is_color_reg_read(shader, 0))
5506 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
5507 if (glsl_is_color_reg_read(shader, 1))
5508 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
5511 for (i = 0; map; map >>= 1, ++i)
5513 if (map & 1)
5515 if (args->pointsprite)
5516 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
5517 else if (args->texcoords_initialized & (1u << i))
5518 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
5519 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
5520 else
5521 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
5522 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
5526 if (legacy_context)
5527 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
5530 /* Pack 3.0 inputs */
5531 if (reg_maps->shader_version.major >= 3)
5532 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
5534 /* Base Shader Body */
5535 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5537 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
5538 if (reg_maps->shader_version.major < 2)
5539 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
5541 if (args->srgb_correction)
5542 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
5544 /* SM < 3 does not replace the fog stage. */
5545 if (reg_maps->shader_version.major < 3)
5546 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
5548 shader_addline(buffer, "}\n");
5550 TRACE("Compiling shader object %u.\n", shader_id);
5551 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5553 return shader_id;
5556 /* Context activation is done by the caller. */
5557 static GLuint shader_glsl_generate_vshader(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 vs_compile_args *args)
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 program and assign it as the shader->prgId */
5569 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5571 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5573 if (gl_info->supported[ARB_DRAW_INSTANCED])
5574 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
5575 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5576 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5577 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5578 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5579 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5580 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5581 if (gl_info->supported[EXT_GPU_SHADER4])
5582 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5584 memset(&priv_ctx, 0, sizeof(priv_ctx));
5585 priv_ctx.cur_vs_args = args;
5586 priv_ctx.string_buffers = string_buffers;
5588 /* Base Declarations */
5589 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5591 /* Base Shader Body */
5592 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5594 /* Unpack outputs */
5595 shader_addline(buffer, "order_ps_input(vs_out);\n");
5597 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
5598 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
5599 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
5600 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
5602 if (reg_maps->shader_version.major < 3)
5604 if (args->fog_src == VS_FOG_Z)
5605 shader_addline(buffer, "%s = gl_Position.z;\n",
5606 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5607 else if (!reg_maps->fog)
5608 shader_addline(buffer, "%s = 0.0;\n",
5609 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5612 /* We always store the clipplanes without y inversion */
5613 if (args->clip_enabled)
5614 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
5616 if (args->point_size && !args->per_vertex_point_size)
5617 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
5619 /* Write the final position.
5621 * OpenGL coordinates specify the center of the pixel while d3d coords specify
5622 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
5623 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
5624 * contains 1.0 to allow a mad.
5626 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
5627 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
5629 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
5631 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
5632 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
5633 * which is the same as z = z * 2 - w.
5635 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
5637 shader_addline(buffer, "}\n");
5639 TRACE("Compiling shader object %u.\n", shader_id);
5640 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5642 return shader_id;
5645 /* Context activation is done by the caller. */
5646 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
5647 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5648 const struct wined3d_shader *shader)
5650 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5651 const struct wined3d_gl_info *gl_info = context->gl_info;
5652 const DWORD *function = shader->function;
5653 struct shader_glsl_ctx_priv priv_ctx;
5654 GLuint shader_id;
5656 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
5658 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5660 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
5661 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
5662 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5663 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5664 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5665 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5666 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5667 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5668 if (gl_info->supported[EXT_GPU_SHADER4])
5669 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5671 memset(&priv_ctx, 0, sizeof(priv_ctx));
5672 priv_ctx.string_buffers = string_buffers;
5673 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5674 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5675 shader_addline(buffer, "}\n");
5677 TRACE("Compiling shader object %u.\n", shader_id);
5678 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5680 return shader_id;
5683 static GLuint find_glsl_pshader(const struct wined3d_context *context,
5684 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5685 struct wined3d_shader *shader,
5686 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
5688 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
5689 struct glsl_shader_private *shader_data;
5690 struct ps_np2fixup_info *np2fixup;
5691 UINT i;
5692 DWORD new_size;
5693 GLuint ret;
5695 if (!shader->backend_data)
5697 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5698 if (!shader->backend_data)
5700 ERR("Failed to allocate backend data.\n");
5701 return 0;
5704 shader_data = shader->backend_data;
5705 gl_shaders = shader_data->gl_shaders.ps;
5707 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5708 * so a linear search is more performant than a hashmap or a binary search
5709 * (cache coherency etc)
5711 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5713 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
5715 if (args->np2_fixup)
5716 *np2fixup_info = &gl_shaders[i].np2fixup;
5717 return gl_shaders[i].id;
5721 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5722 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5723 if (shader_data->num_gl_shaders)
5725 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5726 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
5727 new_size * sizeof(*gl_shaders));
5729 else
5731 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5732 new_size = 1;
5735 if(!new_array) {
5736 ERR("Out of memory\n");
5737 return 0;
5739 shader_data->gl_shaders.ps = new_array;
5740 shader_data->shader_array_size = new_size;
5741 gl_shaders = new_array;
5744 gl_shaders[shader_data->num_gl_shaders].args = *args;
5746 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
5747 memset(np2fixup, 0, sizeof(*np2fixup));
5748 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
5750 pixelshader_update_resource_types(shader, args->tex_types);
5752 string_buffer_clear(buffer);
5753 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
5754 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5756 return ret;
5759 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
5760 const DWORD use_map)
5762 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
5763 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
5764 if (stored->point_size != new->point_size)
5765 return FALSE;
5766 if (stored->per_vertex_point_size != new->per_vertex_point_size)
5767 return FALSE;
5768 if (stored->flatshading != new->flatshading)
5769 return FALSE;
5770 return stored->fog_src == new->fog_src;
5773 static GLuint find_glsl_vshader(const struct wined3d_context *context,
5774 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5775 struct wined3d_shader *shader,
5776 const struct vs_compile_args *args)
5778 UINT i;
5779 DWORD new_size;
5780 DWORD use_map = context->stream_info.use_map;
5781 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
5782 struct glsl_shader_private *shader_data;
5783 GLuint ret;
5785 if (!shader->backend_data)
5787 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5788 if (!shader->backend_data)
5790 ERR("Failed to allocate backend data.\n");
5791 return 0;
5794 shader_data = shader->backend_data;
5795 gl_shaders = shader_data->gl_shaders.vs;
5797 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5798 * so a linear search is more performant than a hashmap or a binary search
5799 * (cache coherency etc)
5801 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5803 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
5804 return gl_shaders[i].id;
5807 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5809 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5810 if (shader_data->num_gl_shaders)
5812 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5813 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
5814 new_size * sizeof(*gl_shaders));
5816 else
5818 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5819 new_size = 1;
5822 if(!new_array) {
5823 ERR("Out of memory\n");
5824 return 0;
5826 shader_data->gl_shaders.vs = new_array;
5827 shader_data->shader_array_size = new_size;
5828 gl_shaders = new_array;
5831 gl_shaders[shader_data->num_gl_shaders].args = *args;
5833 string_buffer_clear(buffer);
5834 ret = shader_glsl_generate_vshader(context, buffer, string_buffers, shader, args);
5835 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5837 return ret;
5840 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
5841 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5842 struct wined3d_shader *shader)
5844 struct glsl_gs_compiled_shader *gl_shaders;
5845 struct glsl_shader_private *shader_data;
5846 GLuint ret;
5848 if (!shader->backend_data)
5850 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
5852 ERR("Failed to allocate backend data.\n");
5853 return 0;
5856 shader_data = shader->backend_data;
5857 gl_shaders = shader_data->gl_shaders.gs;
5859 if (shader_data->num_gl_shaders)
5860 return gl_shaders[0].id;
5862 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5864 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
5866 ERR("Failed to allocate GL shader array.\n");
5867 return 0;
5869 shader_data->shader_array_size = 1;
5870 gl_shaders = shader_data->gl_shaders.gs;
5872 string_buffer_clear(buffer);
5873 ret = shader_glsl_generate_geometry_shader(context, buffer, string_buffers, shader);
5874 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5876 return ret;
5879 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
5881 switch (mcs)
5883 case WINED3D_MCS_MATERIAL:
5884 return material;
5885 case WINED3D_MCS_COLOR1:
5886 return "ffp_attrib_diffuse";
5887 case WINED3D_MCS_COLOR2:
5888 return "ffp_attrib_specular";
5889 default:
5890 ERR("Invalid material color source %#x.\n", mcs);
5891 return "<invalid>";
5895 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
5896 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
5898 const char *diffuse, *specular, *emissive, *ambient;
5899 enum wined3d_light_type light_type;
5900 unsigned int i;
5902 if (!settings->lighting)
5904 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
5905 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
5906 return;
5909 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
5910 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
5911 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
5912 shader_addline(buffer, "vec3 dir, dst;\n");
5913 shader_addline(buffer, "float att, t;\n");
5915 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
5916 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
5917 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
5918 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
5920 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
5922 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
5923 switch (light_type)
5925 case WINED3D_LIGHT_POINT:
5926 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5927 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5928 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5929 shader_addline(buffer, "dst.x = 1.0;\n");
5930 if (legacy_lighting)
5932 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
5933 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
5935 else
5937 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5939 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5940 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", i, i, i);
5941 if (!legacy_lighting)
5942 shader_addline(buffer, "att = 1.0 / att;\n");
5943 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5944 if (!settings->normal)
5946 if (!legacy_lighting)
5947 shader_addline(buffer, "}\n");
5948 break;
5950 shader_addline(buffer, "dir = normalize(dir);\n");
5951 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5952 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
5953 if (settings->localviewer)
5954 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5955 else
5956 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5957 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5958 " * ffp_light[%u].specular) * att;\n", i);
5959 if (!legacy_lighting)
5960 shader_addline(buffer, "}\n");
5961 break;
5963 case WINED3D_LIGHT_SPOT:
5964 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5965 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5966 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5967 shader_addline(buffer, "dst.x = 1.0;\n");
5968 if (legacy_lighting)
5970 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
5971 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
5973 else
5975 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5977 shader_addline(buffer, "dir = normalize(dir);\n");
5978 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", i);
5979 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", i);
5980 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", i);
5981 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
5982 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
5983 i, i, i, i);
5984 if (legacy_lighting)
5985 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5986 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5987 i, i, i);
5988 else
5989 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5990 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5991 i, i, i);
5992 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5993 if (!settings->normal)
5995 if (!legacy_lighting)
5996 shader_addline(buffer, "}\n");
5997 break;
5999 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
6000 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
6001 if (settings->localviewer)
6002 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6003 else
6004 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6005 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
6006 " * ffp_light[%u].specular) * att;\n", i);
6007 if (!legacy_lighting)
6008 shader_addline(buffer, "}\n");
6009 break;
6011 case WINED3D_LIGHT_DIRECTIONAL:
6012 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
6013 if (!settings->normal)
6014 break;
6015 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", i);
6016 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6017 " * ffp_light[%u].diffuse.xyz;\n", i);
6018 /* TODO: In the non-local viewer case the halfvector is constant
6019 * and could be precomputed and stored in a uniform. */
6020 if (settings->localviewer)
6021 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6022 else
6023 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6024 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6025 " * ffp_light[%u].specular;\n", i);
6026 break;
6028 case WINED3D_LIGHT_PARALLELPOINT:
6029 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
6030 if (!settings->normal)
6031 break;
6032 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", i);
6033 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6034 " * ffp_light[%u].diffuse.xyz;\n", i);
6035 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6036 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6037 " * ffp_light[%u].specular;\n", i);
6038 break;
6040 default:
6041 if (light_type)
6042 FIXME("Unhandled light type %#x.\n", light_type);
6043 continue;
6047 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
6048 ambient, diffuse, emissive);
6049 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
6050 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
6053 /* Context activation is done by the caller. */
6054 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
6055 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
6057 static const struct attrib_info
6059 const char type[6];
6060 const char name[24];
6062 attrib_info[] =
6064 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
6065 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
6066 /* TODO: Indexed vertex blending */
6067 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
6068 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
6069 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
6070 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
6071 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
6073 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6074 BOOL legacy_lighting = priv->legacy_lighting;
6075 GLuint shader_obj;
6076 unsigned int i;
6077 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6078 BOOL output_legacy_fogcoord = legacy_context;
6080 string_buffer_clear(buffer);
6082 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6084 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
6086 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
6088 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
6090 shader_addline(buffer, "\n");
6092 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
6093 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
6094 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
6095 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
6097 shader_addline(buffer, "uniform struct\n{\n");
6098 shader_addline(buffer, " vec4 emissive;\n");
6099 shader_addline(buffer, " vec4 ambient;\n");
6100 shader_addline(buffer, " vec4 diffuse;\n");
6101 shader_addline(buffer, " vec4 specular;\n");
6102 shader_addline(buffer, " float shininess;\n");
6103 shader_addline(buffer, "} ffp_material;\n");
6105 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
6106 shader_addline(buffer, "uniform struct\n{\n");
6107 shader_addline(buffer, " vec4 diffuse;\n");
6108 shader_addline(buffer, " vec4 specular;\n");
6109 shader_addline(buffer, " vec4 ambient;\n");
6110 shader_addline(buffer, " vec4 position;\n");
6111 shader_addline(buffer, " vec3 direction;\n");
6112 shader_addline(buffer, " float range;\n");
6113 shader_addline(buffer, " float falloff;\n");
6114 shader_addline(buffer, " float c_att;\n");
6115 shader_addline(buffer, " float l_att;\n");
6116 shader_addline(buffer, " float q_att;\n");
6117 shader_addline(buffer, " float cos_htheta;\n");
6118 shader_addline(buffer, " float cos_hphi;\n");
6119 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
6121 if (settings->point_size)
6123 shader_addline(buffer, "uniform struct\n{\n");
6124 shader_addline(buffer, " float size;\n");
6125 shader_addline(buffer, " float size_min;\n");
6126 shader_addline(buffer, " float size_max;\n");
6127 shader_addline(buffer, " float c_att;\n");
6128 shader_addline(buffer, " float l_att;\n");
6129 shader_addline(buffer, " float q_att;\n");
6130 shader_addline(buffer, "} ffp_point;\n");
6133 if (legacy_context)
6135 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6136 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6137 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6138 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6140 else
6142 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
6143 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
6144 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6145 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6148 shader_addline(buffer, "\nvoid main()\n{\n");
6149 shader_addline(buffer, "float m;\n");
6150 shader_addline(buffer, "vec3 r;\n");
6152 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
6154 if (attrib_info[i].name[0])
6155 shader_addline(buffer, "%s %s = vs_in%u;\n",
6156 attrib_info[i].type, attrib_info[i].name, i);
6158 for (i = 0; i < MAX_TEXTURES; ++i)
6160 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
6161 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
6162 && settings->texcoords & (1u << i))
6163 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
6166 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
6168 if (settings->transformed)
6170 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
6171 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6172 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
6174 else
6176 for (i = 0; i < settings->vertexblends; ++i)
6177 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
6179 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
6180 for (i = 0; i < settings->vertexblends + 1; ++i)
6181 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
6183 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6184 if (settings->clipping)
6185 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
6186 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
6189 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
6190 if (settings->normal)
6192 if (!settings->vertexblends)
6194 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
6196 else
6198 for (i = 0; i < settings->vertexblends + 1; ++i)
6199 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
6202 if (settings->normalize)
6203 shader_addline(buffer, "normal = normalize(normal);\n");
6206 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
6207 if (legacy_context)
6209 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
6210 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
6212 else
6214 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
6215 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
6218 for (i = 0; i < MAX_TEXTURES; ++i)
6220 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6222 switch (settings->texgen[i] & 0xffff0000)
6224 case WINED3DTSS_TCI_PASSTHRU:
6225 if (settings->texcoords & (1u << i))
6226 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
6227 i, i, i);
6228 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
6229 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
6230 else
6231 output_legacy_texcoord = FALSE;
6232 break;
6234 case WINED3DTSS_TCI_CAMERASPACENORMAL:
6235 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
6236 break;
6238 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
6239 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
6240 break;
6242 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
6243 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6244 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
6245 break;
6247 case WINED3DTSS_TCI_SPHEREMAP:
6248 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
6249 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
6250 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6251 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
6252 break;
6254 default:
6255 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
6256 break;
6258 if (output_legacy_texcoord)
6259 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
6262 switch (settings->fog_mode)
6264 case WINED3D_FFP_VS_FOG_OFF:
6265 output_legacy_fogcoord = FALSE;
6266 break;
6268 case WINED3D_FFP_VS_FOG_FOGCOORD:
6269 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
6270 break;
6272 case WINED3D_FFP_VS_FOG_RANGE:
6273 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
6274 break;
6276 case WINED3D_FFP_VS_FOG_DEPTH:
6277 if (settings->ortho_fog)
6278 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
6279 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
6280 else if (settings->transformed)
6281 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
6282 else
6283 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
6284 break;
6286 default:
6287 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
6288 break;
6290 if (output_legacy_fogcoord)
6291 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
6293 if (settings->point_size)
6295 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
6296 " + ffp_point.l_att * length(ec_pos.xyz)"
6297 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
6298 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
6299 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
6302 shader_addline(buffer, "}\n");
6304 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6305 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
6307 return shader_obj;
6310 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
6311 DWORD argnum, unsigned int stage, DWORD arg)
6313 const char *ret;
6315 if (arg == ARG_UNUSED)
6316 return "<unused arg>";
6318 switch (arg & WINED3DTA_SELECTMASK)
6320 case WINED3DTA_DIFFUSE:
6321 ret = "ffp_varying_diffuse";
6322 break;
6324 case WINED3DTA_CURRENT:
6325 ret = "ret";
6326 break;
6328 case WINED3DTA_TEXTURE:
6329 switch (stage)
6331 case 0: ret = "tex0"; break;
6332 case 1: ret = "tex1"; break;
6333 case 2: ret = "tex2"; break;
6334 case 3: ret = "tex3"; break;
6335 case 4: ret = "tex4"; break;
6336 case 5: ret = "tex5"; break;
6337 case 6: ret = "tex6"; break;
6338 case 7: ret = "tex7"; break;
6339 default:
6340 ret = "<invalid texture>";
6341 break;
6343 break;
6345 case WINED3DTA_TFACTOR:
6346 ret = "tex_factor";
6347 break;
6349 case WINED3DTA_SPECULAR:
6350 ret = "ffp_varying_specular";
6351 break;
6353 case WINED3DTA_TEMP:
6354 ret = "temp_reg";
6355 break;
6357 case WINED3DTA_CONSTANT:
6358 switch (stage)
6360 case 0: ret = "tss_const0"; break;
6361 case 1: ret = "tss_const1"; break;
6362 case 2: ret = "tss_const2"; break;
6363 case 3: ret = "tss_const3"; break;
6364 case 4: ret = "tss_const4"; break;
6365 case 5: ret = "tss_const5"; break;
6366 case 6: ret = "tss_const6"; break;
6367 case 7: ret = "tss_const7"; break;
6368 default:
6369 ret = "<invalid constant>";
6370 break;
6372 break;
6374 default:
6375 return "<unhandled arg>";
6378 if (arg & WINED3DTA_COMPLEMENT)
6380 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
6381 if (argnum == 0)
6382 ret = "arg0";
6383 else if (argnum == 1)
6384 ret = "arg1";
6385 else if (argnum == 2)
6386 ret = "arg2";
6389 if (arg & WINED3DTA_ALPHAREPLICATE)
6391 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
6392 if (argnum == 0)
6393 ret = "arg0";
6394 else if (argnum == 1)
6395 ret = "arg1";
6396 else if (argnum == 2)
6397 ret = "arg2";
6400 return ret;
6403 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
6404 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
6406 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
6408 if (color && alpha)
6409 dstmask = "";
6410 else if (color)
6411 dstmask = ".xyz";
6412 else
6413 dstmask = ".w";
6415 if (dst == tempreg)
6416 dstreg = "temp_reg";
6417 else
6418 dstreg = "ret";
6420 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
6421 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
6422 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
6424 switch (op)
6426 case WINED3D_TOP_DISABLE:
6427 break;
6429 case WINED3D_TOP_SELECT_ARG1:
6430 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
6431 break;
6433 case WINED3D_TOP_SELECT_ARG2:
6434 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
6435 break;
6437 case WINED3D_TOP_MODULATE:
6438 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6439 break;
6441 case WINED3D_TOP_MODULATE_4X:
6442 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
6443 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6444 break;
6446 case WINED3D_TOP_MODULATE_2X:
6447 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
6448 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6449 break;
6451 case WINED3D_TOP_ADD:
6452 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
6453 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6454 break;
6456 case WINED3D_TOP_ADD_SIGNED:
6457 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
6458 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6459 break;
6461 case WINED3D_TOP_ADD_SIGNED_2X:
6462 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
6463 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6464 break;
6466 case WINED3D_TOP_SUBTRACT:
6467 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
6468 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6469 break;
6471 case WINED3D_TOP_ADD_SMOOTH:
6472 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
6473 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
6474 break;
6476 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
6477 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
6478 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6479 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6480 break;
6482 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6483 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6484 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6485 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6486 break;
6488 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6489 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
6490 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6491 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6492 break;
6494 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6495 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6496 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6497 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
6498 break;
6500 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
6501 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
6502 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6503 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6504 break;
6506 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
6507 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
6508 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6509 break;
6511 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
6512 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
6513 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6514 break;
6516 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
6517 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6518 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6519 break;
6520 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
6521 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
6522 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6523 break;
6525 case WINED3D_TOP_BUMPENVMAP:
6526 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6527 /* These are handled in the first pass, nothing to do. */
6528 break;
6530 case WINED3D_TOP_DOTPRODUCT3:
6531 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
6532 dstreg, dstmask, arg1, arg2, dstmask);
6533 break;
6535 case WINED3D_TOP_MULTIPLY_ADD:
6536 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
6537 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
6538 break;
6540 case WINED3D_TOP_LERP:
6541 /* MSDN isn't quite right here. */
6542 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
6543 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
6544 break;
6546 default:
6547 FIXME("Unhandled operation %#x.\n", op);
6548 break;
6552 /* Context activation is done by the caller. */
6553 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
6554 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
6556 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6557 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
6558 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
6559 UINT lowest_disabled_stage;
6560 GLuint shader_id;
6561 DWORD arg0, arg1, arg2;
6562 unsigned int stage;
6563 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
6564 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6566 string_buffer_clear(buffer);
6568 /* Find out which textures are read */
6569 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6571 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6572 break;
6574 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
6575 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
6576 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
6578 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
6579 || (stage == 0 && settings->color_key_enabled))
6580 tex_map |= 1u << stage;
6581 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6582 tfactor_used = TRUE;
6583 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6584 tempreg_used = TRUE;
6585 if (settings->op[stage].dst == tempreg)
6586 tempreg_used = TRUE;
6587 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6588 tss_const_map |= 1u << stage;
6590 switch (settings->op[stage].cop)
6592 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6593 lum_map |= 1u << stage;
6594 /* fall through */
6595 case WINED3D_TOP_BUMPENVMAP:
6596 bump_map |= 1u << stage;
6597 /* fall through */
6598 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6599 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6600 tex_map |= 1u << stage;
6601 break;
6603 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6604 tfactor_used = TRUE;
6605 break;
6607 default:
6608 break;
6611 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6612 continue;
6614 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
6615 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
6616 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
6618 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
6619 tex_map |= 1u << stage;
6620 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6621 tfactor_used = TRUE;
6622 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6623 tempreg_used = TRUE;
6624 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6625 tss_const_map |= 1u << stage;
6627 lowest_disabled_stage = stage;
6629 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6631 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6632 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6634 if (!needs_legacy_glsl_syntax(gl_info))
6635 shader_addline(buffer, "out vec4 ps_out[1];\n");
6637 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
6638 shader_addline(buffer, "vec4 ret;\n");
6639 if (tempreg_used || settings->sRGB_write)
6640 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
6641 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
6643 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6645 if (tss_const_map & (1u << stage))
6646 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
6648 if (!(tex_map & (1u << stage)))
6649 continue;
6651 switch (settings->op[stage].tex_type)
6653 case WINED3D_GL_RES_TYPE_TEX_1D:
6654 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
6655 break;
6656 case WINED3D_GL_RES_TYPE_TEX_2D:
6657 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
6658 break;
6659 case WINED3D_GL_RES_TYPE_TEX_3D:
6660 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
6661 break;
6662 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6663 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
6664 break;
6665 case WINED3D_GL_RES_TYPE_TEX_RECT:
6666 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
6667 break;
6668 default:
6669 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
6670 break;
6673 shader_addline(buffer, "vec4 tex%u;\n", stage);
6675 if (!(bump_map & (1u << stage)))
6676 continue;
6677 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
6679 if (!(lum_map & (1u << stage)))
6680 continue;
6681 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
6682 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
6684 if (tfactor_used)
6685 shader_addline(buffer, "uniform vec4 tex_factor;\n");
6686 if (settings->color_key_enabled)
6687 shader_addline(buffer, "uniform vec4 color_key[2];\n");
6688 shader_addline(buffer, "uniform vec4 specular_enable;\n");
6690 if (settings->sRGB_write)
6692 shader_addline(buffer, "const vec4 srgb_const0 = ");
6693 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
6694 shader_addline(buffer, ";\n");
6695 shader_addline(buffer, "const vec4 srgb_const1 = ");
6696 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
6697 shader_addline(buffer, ";\n");
6700 shader_addline(buffer, "uniform struct\n{\n");
6701 shader_addline(buffer, " vec4 color;\n");
6702 shader_addline(buffer, " float density;\n");
6703 shader_addline(buffer, " float end;\n");
6704 shader_addline(buffer, " float scale;\n");
6705 shader_addline(buffer, "} ffp_fog;\n");
6707 if (legacy_context)
6709 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6710 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6711 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6712 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6713 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6715 else
6717 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
6718 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
6719 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6720 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6721 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6724 shader_addline(buffer, "void main()\n{\n");
6726 if (legacy_context)
6728 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
6729 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
6732 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6734 if (tex_map & (1u << stage))
6736 if (settings->pointsprite)
6737 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
6738 else if (settings->texcoords_initialized & (1u << stage))
6739 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
6740 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
6741 else
6742 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
6746 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
6747 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6749 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
6750 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
6752 /* Generate texture sampling instructions */
6753 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
6755 const char *texture_function, *coord_mask;
6756 BOOL proj;
6758 if (!(tex_map & (1u << stage)))
6759 continue;
6761 if (settings->op[stage].projected == proj_none)
6763 proj = FALSE;
6765 else if (settings->op[stage].projected == proj_count4
6766 || settings->op[stage].projected == proj_count3)
6768 proj = TRUE;
6770 else
6772 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
6773 proj = TRUE;
6776 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
6777 proj = FALSE;
6779 switch (settings->op[stage].tex_type)
6781 case WINED3D_GL_RES_TYPE_TEX_1D:
6782 if (proj)
6784 texture_function = "texture1DProj";
6785 coord_mask = "xw";
6787 else
6789 texture_function = "texture1D";
6790 coord_mask = "x";
6792 break;
6793 case WINED3D_GL_RES_TYPE_TEX_2D:
6794 if (proj)
6796 texture_function = "texture2DProj";
6797 coord_mask = "xyw";
6799 else
6801 texture_function = "texture2D";
6802 coord_mask = "xy";
6804 break;
6805 case WINED3D_GL_RES_TYPE_TEX_3D:
6806 if (proj)
6808 texture_function = "texture3DProj";
6809 coord_mask = "xyzw";
6811 else
6813 texture_function = "texture3D";
6814 coord_mask = "xyz";
6816 break;
6817 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6818 texture_function = "textureCube";
6819 coord_mask = "xyz";
6820 break;
6821 case WINED3D_GL_RES_TYPE_TEX_RECT:
6822 if (proj)
6824 texture_function = "texture2DRectProj";
6825 coord_mask = "xyw";
6827 else
6829 texture_function = "texture2DRect";
6830 coord_mask = "xy";
6832 break;
6833 default:
6834 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
6835 texture_function = "";
6836 coord_mask = "xyzw";
6837 break;
6839 if (!needs_legacy_glsl_syntax(gl_info))
6840 texture_function = proj ? "textureProj" : "texture";
6842 if (stage > 0
6843 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
6844 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
6846 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
6848 /* With projective textures, texbem only divides the static
6849 * texture coord, not the displacement, so multiply the
6850 * displacement with the dividing parameter before passing it to
6851 * TXP. */
6852 if (settings->op[stage].projected != proj_none)
6854 if (settings->op[stage].projected == proj_count4)
6856 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
6857 stage, stage);
6858 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
6860 else
6862 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
6863 stage, stage);
6864 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
6867 else
6869 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
6872 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
6873 stage, texture_function, stage, coord_mask);
6875 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6876 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
6877 stage, stage - 1, stage - 1, stage - 1);
6879 else if (settings->op[stage].projected == proj_count3)
6881 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
6882 stage, texture_function, stage, stage);
6884 else
6886 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
6887 stage, texture_function, stage, stage, coord_mask);
6890 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
6891 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
6892 settings->op[stage].color_fixup);
6895 if (settings->color_key_enabled)
6897 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
6898 shader_addline(buffer, " discard;\n");
6901 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
6903 /* Generate the main shader */
6904 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6906 BOOL op_equal;
6908 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6909 break;
6911 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6912 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6913 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
6914 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6915 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6916 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
6917 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6918 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6919 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
6920 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6921 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6922 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
6923 else
6924 op_equal = settings->op[stage].aop == settings->op[stage].cop
6925 && settings->op[stage].carg0 == settings->op[stage].aarg0
6926 && settings->op[stage].carg1 == settings->op[stage].aarg1
6927 && settings->op[stage].carg2 == settings->op[stage].aarg2;
6929 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6931 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6932 settings->op[stage].cop, settings->op[stage].carg0,
6933 settings->op[stage].carg1, settings->op[stage].carg2);
6935 else if (op_equal)
6937 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
6938 settings->op[stage].cop, settings->op[stage].carg0,
6939 settings->op[stage].carg1, settings->op[stage].carg2);
6941 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
6942 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6944 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6945 settings->op[stage].cop, settings->op[stage].carg0,
6946 settings->op[stage].carg1, settings->op[stage].carg2);
6947 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
6948 settings->op[stage].aop, settings->op[stage].aarg0,
6949 settings->op[stage].aarg1, settings->op[stage].aarg2);
6953 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
6954 get_fragment_output(gl_info));
6956 if (settings->sRGB_write)
6957 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
6959 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
6961 shader_addline(buffer, "}\n");
6963 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6964 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6966 string_buffer_release(&priv->string_buffers, tex_reg_name);
6967 return shader_id;
6970 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
6971 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
6973 struct glsl_ffp_vertex_shader *shader;
6974 const struct wine_rb_entry *entry;
6976 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
6977 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
6979 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
6980 return NULL;
6982 shader->desc.settings = *settings;
6983 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
6984 list_init(&shader->linked_programs);
6985 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
6986 ERR("Failed to insert ffp vertex shader.\n");
6988 return shader;
6991 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
6992 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
6994 struct glsl_ffp_fragment_shader *glsl_desc;
6995 const struct ffp_frag_desc *desc;
6997 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
6998 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
7000 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
7001 return NULL;
7003 glsl_desc->entry.settings = *args;
7004 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
7005 list_init(&glsl_desc->linked_programs);
7006 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
7008 return glsl_desc;
7012 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
7013 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
7015 unsigned int i;
7016 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7018 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
7019 sizeof(GLuint) * gl_info->limits.glsl_vs_float_constants);
7020 for (i = 0; i < vs_c_count; ++i)
7022 string_buffer_sprintf(name, "vs_c[%u]", i);
7023 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7025 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
7026 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLuint));
7028 for (i = 0; i < MAX_CONST_I; ++i)
7030 string_buffer_sprintf(name, "vs_i[%u]", i);
7031 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7034 for (i = 0; i < MAX_CONST_B; ++i)
7036 string_buffer_sprintf(name, "vs_b[%u]", i);
7037 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7040 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "posFixup"));
7042 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
7044 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
7045 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7047 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
7048 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
7049 for (i = 0; i < MAX_TEXTURES; ++i)
7051 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
7052 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7054 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
7055 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
7056 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
7057 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
7058 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
7059 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
7060 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
7062 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
7063 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7064 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
7065 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7066 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
7067 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7068 string_buffer_sprintf(name, "ffp_light[%u].position", i);
7069 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7070 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
7071 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7072 string_buffer_sprintf(name, "ffp_light[%u].range", i);
7073 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7074 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
7075 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7076 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
7077 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7078 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
7079 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7080 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
7081 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7082 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
7083 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7084 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
7085 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7087 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
7088 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
7089 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
7090 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
7091 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
7092 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
7094 string_buffer_release(&priv->string_buffers, name);
7097 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
7098 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
7100 unsigned int i;
7101 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7103 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
7104 sizeof(GLuint) * gl_info->limits.glsl_ps_float_constants);
7105 for (i = 0; i < ps_c_count; ++i)
7107 string_buffer_sprintf(name, "ps_c[%u]", i);
7108 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7110 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
7111 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLuint));
7113 for (i = 0; i < MAX_CONST_I; ++i)
7115 string_buffer_sprintf(name, "ps_i[%u]", i);
7116 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7119 for (i = 0; i < MAX_CONST_B; ++i)
7121 string_buffer_sprintf(name, "ps_b[%u]", i);
7122 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7125 for (i = 0; i < MAX_TEXTURES; ++i)
7127 string_buffer_sprintf(name, "bumpenv_mat%u", i);
7128 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7129 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
7130 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7131 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
7132 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7133 string_buffer_sprintf(name, "tss_const%u", i);
7134 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7137 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
7138 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
7140 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
7141 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
7142 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
7143 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
7145 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
7146 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
7147 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
7149 string_buffer_release(&priv->string_buffers, name);
7152 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
7153 struct shader_glsl_priv *priv, GLuint program_id,
7154 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
7156 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
7157 GLuint block_idx;
7158 unsigned int i;
7159 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7161 for (i = 0; i < count; ++i)
7163 if (!reg_maps->cb_sizes[i])
7164 continue;
7166 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
7167 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
7168 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
7170 checkGLcall("glUniformBlockBinding");
7171 string_buffer_release(&priv->string_buffers, name);
7174 /* Context activation is done by the caller. */
7175 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
7176 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
7178 const struct wined3d_gl_info *gl_info = context->gl_info;
7179 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
7180 const struct ps_np2fixup_info *np2fixup_info = NULL;
7181 struct glsl_shader_prog_link *entry = NULL;
7182 struct wined3d_shader *vshader = NULL;
7183 struct wined3d_shader *gshader = NULL;
7184 struct wined3d_shader *pshader = NULL;
7185 GLuint program_id;
7186 GLuint reorder_shader_id = 0;
7187 unsigned int i;
7188 GLuint vs_id = 0;
7189 GLuint gs_id = 0;
7190 GLuint ps_id = 0;
7191 struct list *ps_list, *vs_list;
7192 WORD attribs_map;
7193 struct wined3d_string_buffer *tmp_name;
7195 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
7197 vs_id = ctx_data->glsl_program->vs.id;
7198 vs_list = &ctx_data->glsl_program->vs.shader_entry;
7200 if (use_vs(state))
7202 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7203 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
7205 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
7206 && ctx_data->glsl_program->gs.id)
7207 gs_id = ctx_data->glsl_program->gs.id;
7208 else if (gshader)
7209 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, &priv->string_buffers, gshader);
7212 else if (use_vs(state))
7214 struct vs_compile_args vs_compile_args;
7216 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7218 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, d3d_info);
7219 vs_id = find_glsl_vshader(context, &priv->shader_buffer, &priv->string_buffers, vshader, &vs_compile_args);
7220 vs_list = &vshader->linked_programs;
7222 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
7223 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, &priv->string_buffers, gshader);
7225 else if (priv->vertex_pipe == &glsl_vertex_pipe)
7227 struct glsl_ffp_vertex_shader *ffp_shader;
7228 struct wined3d_ffp_vs_settings settings;
7230 wined3d_ffp_get_vs_settings(context, state, &settings);
7231 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
7232 vs_id = ffp_shader->id;
7233 vs_list = &ffp_shader->linked_programs;
7236 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
7238 ps_id = ctx_data->glsl_program->ps.id;
7239 ps_list = &ctx_data->glsl_program->ps.shader_entry;
7241 if (use_ps(state))
7242 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7244 else if (use_ps(state))
7246 struct ps_compile_args ps_compile_args;
7247 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7248 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
7249 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
7250 pshader, &ps_compile_args, &np2fixup_info);
7251 ps_list = &pshader->linked_programs;
7253 else if (priv->fragment_pipe == &glsl_fragment_pipe)
7255 struct glsl_ffp_fragment_shader *ffp_shader;
7256 struct ffp_frag_settings settings;
7258 gen_ffp_frag_op(context, state, &settings, FALSE);
7259 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
7260 ps_id = ffp_shader->id;
7261 ps_list = &ffp_shader->linked_programs;
7264 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
7266 ctx_data->glsl_program = entry;
7267 return;
7270 /* If we get to this point, then no matching program exists, so we create one */
7271 program_id = GL_EXTCALL(glCreateProgram());
7272 TRACE("Created new GLSL shader program %u.\n", program_id);
7274 /* Create the entry */
7275 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
7276 entry->id = program_id;
7277 entry->vs.id = vs_id;
7278 entry->gs.id = gs_id;
7279 entry->ps.id = ps_id;
7280 entry->constant_version = 0;
7281 entry->ps.np2_fixup_info = np2fixup_info;
7282 /* Add the hash table entry */
7283 add_glsl_program_entry(priv, entry);
7285 /* Set the current program */
7286 ctx_data->glsl_program = entry;
7288 /* Attach GLSL vshader */
7289 if (vs_id)
7291 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
7292 GL_EXTCALL(glAttachShader(program_id, vs_id));
7293 checkGLcall("glAttachShader");
7295 list_add_head(vs_list, &entry->vs.shader_entry);
7298 if (vshader)
7300 attribs_map = vshader->reg_maps.input_registers;
7301 reorder_shader_id = generate_param_reorder_function(priv, vshader, pshader,
7302 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
7303 d3d_info->emulated_flatshading
7304 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
7305 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
7306 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
7307 checkGLcall("glAttachShader");
7308 /* Flag the reorder function for deletion, then it will be freed automatically when the program
7309 * is destroyed
7311 GL_EXTCALL(glDeleteShader(reorder_shader_id));
7313 else
7315 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
7318 /* Bind vertex attributes to a corresponding index number to match
7319 * the same index numbers as ARB_vertex_programs (makes loading
7320 * vertex attributes simpler). With this method, we can use the
7321 * exact same code to load the attributes later for both ARB and
7322 * GLSL shaders.
7324 * We have to do this here because we need to know the Program ID
7325 * in order to make the bindings work, and it has to be done prior
7326 * to linking the GLSL program. */
7327 tmp_name = string_buffer_get(&priv->string_buffers);
7328 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
7330 if (!(attribs_map & 1))
7331 continue;
7333 string_buffer_sprintf(tmp_name, "vs_in%u", i);
7334 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
7336 checkGLcall("glBindAttribLocation");
7337 string_buffer_release(&priv->string_buffers, tmp_name);
7339 if (gshader)
7341 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
7342 GL_EXTCALL(glAttachShader(program_id, gs_id));
7343 checkGLcall("glAttachShader");
7345 TRACE("input type %s, output type %s, vertices out %u.\n",
7346 debug_d3dprimitivetype(gshader->u.gs.input_type),
7347 debug_d3dprimitivetype(gshader->u.gs.output_type),
7348 gshader->u.gs.vertices_out);
7349 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
7350 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
7351 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
7352 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
7353 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
7354 gshader->u.gs.vertices_out));
7355 checkGLcall("glProgramParameteriARB");
7357 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
7360 /* Attach GLSL pshader */
7361 if (ps_id)
7363 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
7364 GL_EXTCALL(glAttachShader(program_id, ps_id));
7365 checkGLcall("glAttachShader");
7367 list_add_head(ps_list, &entry->ps.shader_entry);
7370 /* Link the program */
7371 TRACE("Linking GLSL shader program %u.\n", program_id);
7372 GL_EXTCALL(glLinkProgram(program_id));
7373 shader_glsl_validate_link(gl_info, program_id);
7375 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
7376 vshader ? min(vshader->limits->constant_float, gl_info->limits.glsl_vs_float_constants) : 0);
7377 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
7378 pshader ? min(pshader->limits->constant_float, gl_info->limits.glsl_ps_float_constants) : 0);
7379 checkGLcall("Find glsl program uniform locations");
7381 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
7383 if (pshader && pshader->reg_maps.shader_version.major >= 3
7384 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
7386 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
7387 entry->vs.vertex_color_clamp = GL_FALSE;
7389 else
7391 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7394 else
7396 /* With core profile we never change vertex_color_clamp from
7397 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
7398 * glClampColorARB(). */
7399 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7402 /* Set the shader to allow uniform loading on it */
7403 GL_EXTCALL(glUseProgram(program_id));
7404 checkGLcall("glUseProgram");
7406 /* Texture unit mapping is set up to be the same each time the shader
7407 * program is used so we can hardcode the sampler uniform values. */
7408 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
7410 entry->constant_update_mask = 0;
7411 if (vshader)
7413 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
7414 if (vshader->reg_maps.integer_constants)
7415 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
7416 if (vshader->reg_maps.boolean_constants)
7417 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
7418 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
7420 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps,
7421 0, gl_info->limits.vertex_uniform_blocks);
7423 else
7425 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
7426 | WINED3D_SHADER_CONST_FFP_PROJ;
7428 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
7430 if (entry->vs.modelview_matrix_location[i] != -1)
7432 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
7433 break;
7437 for (i = 0; i < MAX_TEXTURES; ++i)
7439 if (entry->vs.texture_matrix_location[i] != -1)
7441 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7442 break;
7445 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
7446 || entry->vs.material_specular_location != -1
7447 || entry->vs.material_emissive_location != -1
7448 || entry->vs.material_shininess_location != -1)
7449 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
7450 if (entry->vs.light_ambient_location != -1)
7451 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
7453 if (entry->vs.pointsize_min_location != -1)
7454 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
7456 if (gshader)
7457 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps,
7458 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
7460 if (ps_id)
7462 if (pshader)
7464 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
7465 if (pshader->reg_maps.integer_constants)
7466 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
7467 if (pshader->reg_maps.boolean_constants)
7468 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
7469 if (entry->ps.ycorrection_location != -1)
7470 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
7472 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps,
7473 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
7474 gl_info->limits.fragment_uniform_blocks);
7476 else
7478 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7481 for (i = 0; i < MAX_TEXTURES; ++i)
7483 if (entry->ps.bumpenv_mat_location[i] != -1)
7485 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
7486 break;
7490 if (entry->ps.fog_color_location != -1)
7491 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
7492 if (entry->ps.np2_fixup_location != -1)
7493 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
7494 if (entry->ps.color_key_location != -1)
7495 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
7499 /* Context activation is done by the caller. */
7500 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum wined3d_gl_resource_type tex_type,
7501 BOOL masked)
7503 GLuint program_id;
7504 GLuint vshader_id, pshader_id;
7505 const char *blt_pshader;
7507 static const char blt_vshader[] =
7508 "#version 120\n"
7509 "void main(void)\n"
7510 "{\n"
7511 " gl_Position = gl_Vertex;\n"
7512 " gl_FrontColor = vec4(1.0);\n"
7513 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
7514 "}\n";
7516 static const char * const blt_pshaders_full[WINED3D_GL_RES_TYPE_COUNT] =
7518 /* WINED3D_GL_RES_TYPE_TEX_1D */
7519 NULL,
7520 /* WINED3D_GL_RES_TYPE_TEX_2D */
7521 "#version 120\n"
7522 "uniform sampler2D sampler;\n"
7523 "void main(void)\n"
7524 "{\n"
7525 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7526 "}\n",
7527 /* WINED3D_GL_RES_TYPE_TEX_3D */
7528 NULL,
7529 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7530 "#version 120\n"
7531 "uniform samplerCube sampler;\n"
7532 "void main(void)\n"
7533 "{\n"
7534 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7535 "}\n",
7536 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7537 "#version 120\n"
7538 "#extension GL_ARB_texture_rectangle : enable\n"
7539 "uniform sampler2DRect sampler;\n"
7540 "void main(void)\n"
7541 "{\n"
7542 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7543 "}\n",
7544 /* WINED3D_GL_RES_TYPE_BUFFER */
7545 NULL,
7546 /* WINED3D_GL_RES_TYPE_RB */
7547 NULL,
7550 static const char * const blt_pshaders_masked[WINED3D_GL_RES_TYPE_COUNT] =
7552 /* WINED3D_GL_RES_TYPE_TEX_1D */
7553 NULL,
7554 /* WINED3D_GL_RES_TYPE_TEX_2D */
7555 "#version 120\n"
7556 "uniform sampler2D sampler;\n"
7557 "uniform vec4 mask;\n"
7558 "void main(void)\n"
7559 "{\n"
7560 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7561 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7562 "}\n",
7563 /* WINED3D_GL_RES_TYPE_TEX_3D */
7564 NULL,
7565 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7566 "#version 120\n"
7567 "uniform samplerCube sampler;\n"
7568 "uniform vec4 mask;\n"
7569 "void main(void)\n"
7570 "{\n"
7571 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7572 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7573 "}\n",
7574 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7575 "#version 120\n"
7576 "#extension GL_ARB_texture_rectangle : enable\n"
7577 "uniform sampler2DRect sampler;\n"
7578 "uniform vec4 mask;\n"
7579 "void main(void)\n"
7580 "{\n"
7581 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7582 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7583 "}\n",
7584 /* WINED3D_GL_RES_TYPE_BUFFER */
7585 NULL,
7586 /* WINED3D_GL_RES_TYPE_RB */
7587 NULL,
7590 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
7591 if (!blt_pshader)
7593 FIXME("tex_type %#x not supported\n", tex_type);
7594 return 0;
7597 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7598 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
7600 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7601 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
7603 program_id = GL_EXTCALL(glCreateProgram());
7604 GL_EXTCALL(glAttachShader(program_id, vshader_id));
7605 GL_EXTCALL(glAttachShader(program_id, pshader_id));
7606 GL_EXTCALL(glLinkProgram(program_id));
7608 shader_glsl_validate_link(gl_info, program_id);
7610 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
7611 * is destroyed
7613 GL_EXTCALL(glDeleteShader(vshader_id));
7614 GL_EXTCALL(glDeleteShader(pshader_id));
7615 return program_id;
7618 /* Context activation is done by the caller. */
7619 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
7620 const struct wined3d_state *state)
7622 struct glsl_context_data *ctx_data = context->shader_backend_data;
7623 const struct wined3d_gl_info *gl_info = context->gl_info;
7624 struct shader_glsl_priv *priv = shader_priv;
7625 GLuint program_id = 0, prev_id = 0;
7626 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
7628 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
7629 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
7631 if (ctx_data->glsl_program)
7633 prev_id = ctx_data->glsl_program->id;
7634 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7636 else
7638 prev_id = 0;
7639 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7642 set_glsl_shader_program(context, state, priv, ctx_data);
7644 if (ctx_data->glsl_program)
7646 program_id = ctx_data->glsl_program->id;
7647 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7649 else
7651 program_id = 0;
7652 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7655 if (old_vertex_color_clamp != current_vertex_color_clamp)
7657 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7659 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
7660 checkGLcall("glClampColorARB");
7662 else
7664 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
7668 TRACE("Using GLSL program %u.\n", program_id);
7670 if (prev_id != program_id)
7672 GL_EXTCALL(glUseProgram(program_id));
7673 checkGLcall("glUseProgram");
7675 if (program_id)
7676 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
7680 /* "context" is not necessarily the currently active context. */
7681 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
7683 struct glsl_context_data *ctx_data = context->shader_backend_data;
7685 ctx_data->glsl_program = NULL;
7686 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
7687 | (1u << WINED3D_SHADER_TYPE_VERTEX)
7688 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
7689 | (1u << WINED3D_SHADER_TYPE_HULL)
7690 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
7693 /* Context activation is done by the caller. */
7694 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
7696 const struct wined3d_gl_info *gl_info = context->gl_info;
7697 struct shader_glsl_priv *priv = shader_priv;
7699 shader_glsl_invalidate_current_program(context);
7700 GL_EXTCALL(glUseProgram(0));
7701 checkGLcall("glUseProgram");
7703 priv->vertex_pipe->vp_enable(gl_info, FALSE);
7704 priv->fragment_pipe->enable_extension(gl_info, FALSE);
7706 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7708 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
7709 checkGLcall("glClampColorARB");
7713 /* Context activation is done by the caller. */
7714 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
7715 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size)
7717 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
7718 struct shader_glsl_priv *priv = shader_priv;
7719 GLuint *blt_program;
7720 GLint loc;
7722 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
7723 if (!*blt_program)
7725 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
7726 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
7727 GL_EXTCALL(glUseProgram(*blt_program));
7728 GL_EXTCALL(glUniform1i(loc, 0));
7730 else
7732 GL_EXTCALL(glUseProgram(*blt_program));
7735 if (masked)
7737 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
7738 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
7742 /* Context activation is done by the caller. */
7743 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
7745 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
7746 GLuint program_id;
7748 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
7749 if (program_id) TRACE("Using GLSL program %u\n", program_id);
7751 GL_EXTCALL(glUseProgram(program_id));
7752 checkGLcall("glUseProgram");
7755 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
7756 const struct glsl_shader_prog_link *program)
7758 const struct glsl_context_data *ctx_data;
7759 struct wined3d_context *context;
7760 unsigned int i;
7762 for (i = 0; i < device->context_count; ++i)
7764 context = device->contexts[i];
7765 ctx_data = context->shader_backend_data;
7767 if (ctx_data->glsl_program == program)
7768 shader_glsl_invalidate_current_program(context);
7772 static void shader_glsl_destroy(struct wined3d_shader *shader)
7774 struct glsl_shader_private *shader_data = shader->backend_data;
7775 struct wined3d_device *device = shader->device;
7776 struct shader_glsl_priv *priv = device->shader_priv;
7777 const struct wined3d_gl_info *gl_info;
7778 const struct list *linked_programs;
7779 struct wined3d_context *context;
7781 if (!shader_data || !shader_data->num_gl_shaders)
7783 HeapFree(GetProcessHeap(), 0, shader_data);
7784 shader->backend_data = NULL;
7785 return;
7788 context = context_acquire(device, NULL);
7789 gl_info = context->gl_info;
7791 TRACE("Deleting linked programs.\n");
7792 linked_programs = &shader->linked_programs;
7793 if (linked_programs->next)
7795 struct glsl_shader_prog_link *entry, *entry2;
7796 UINT i;
7798 switch (shader->reg_maps.shader_version.type)
7800 case WINED3D_SHADER_TYPE_PIXEL:
7802 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
7804 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7806 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
7807 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7808 checkGLcall("glDeleteShader");
7810 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
7812 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7813 struct glsl_shader_prog_link, ps.shader_entry)
7815 shader_glsl_invalidate_contexts_program(device, entry);
7816 delete_glsl_program_entry(priv, gl_info, entry);
7819 break;
7822 case WINED3D_SHADER_TYPE_VERTEX:
7824 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
7826 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7828 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
7829 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7830 checkGLcall("glDeleteShader");
7832 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
7834 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7835 struct glsl_shader_prog_link, vs.shader_entry)
7837 shader_glsl_invalidate_contexts_program(device, entry);
7838 delete_glsl_program_entry(priv, gl_info, entry);
7841 break;
7844 case WINED3D_SHADER_TYPE_GEOMETRY:
7846 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
7848 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7850 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
7851 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7852 checkGLcall("glDeleteShader");
7854 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
7856 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7857 struct glsl_shader_prog_link, gs.shader_entry)
7859 shader_glsl_invalidate_contexts_program(device, entry);
7860 delete_glsl_program_entry(priv, gl_info, entry);
7863 break;
7866 default:
7867 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
7868 break;
7872 HeapFree(GetProcessHeap(), 0, shader->backend_data);
7873 shader->backend_data = NULL;
7875 context_release(context);
7878 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
7880 const struct glsl_program_key *k = key;
7881 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
7882 const struct glsl_shader_prog_link, program_lookup_entry);
7884 if (k->vs_id > prog->vs.id) return 1;
7885 else if (k->vs_id < prog->vs.id) return -1;
7887 if (k->gs_id > prog->gs.id) return 1;
7888 else if (k->gs_id < prog->gs.id) return -1;
7890 if (k->ps_id > prog->ps.id) return 1;
7891 else if (k->ps_id < prog->ps.id) return -1;
7893 return 0;
7896 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
7898 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
7899 + constant_count * sizeof(*heap->contained)
7900 + constant_count * sizeof(*heap->positions);
7901 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
7903 if (!mem)
7905 ERR("Failed to allocate memory\n");
7906 return FALSE;
7909 heap->entries = mem;
7910 heap->entries[1].version = 0;
7911 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
7912 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
7913 heap->positions = (unsigned int *)(heap->contained + constant_count);
7914 heap->size = 1;
7916 return TRUE;
7919 static void constant_heap_free(struct constant_heap *heap)
7921 HeapFree(GetProcessHeap(), 0, heap->entries);
7924 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
7926 wined3d_rb_alloc,
7927 wined3d_rb_realloc,
7928 wined3d_rb_free,
7929 glsl_program_key_compare,
7932 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
7933 const struct fragment_pipeline *fragment_pipe)
7935 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7936 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
7937 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
7938 gl_info->limits.glsl_ps_float_constants)) + 1;
7939 struct fragment_caps fragment_caps;
7940 void *vertex_priv, *fragment_priv;
7942 string_buffer_list_init(&priv->string_buffers);
7944 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
7946 ERR("Failed to initialize vertex pipe.\n");
7947 HeapFree(GetProcessHeap(), 0, priv);
7948 return E_FAIL;
7951 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
7953 ERR("Failed to initialize fragment pipe.\n");
7954 vertex_pipe->vp_free(device);
7955 HeapFree(GetProcessHeap(), 0, priv);
7956 return E_FAIL;
7959 if (!string_buffer_init(&priv->shader_buffer))
7961 ERR("Failed to initialize shader buffer.\n");
7962 goto fail;
7965 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
7966 if (!priv->stack)
7968 ERR("Failed to allocate memory.\n");
7969 goto fail;
7972 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
7974 ERR("Failed to initialize vertex shader constant heap\n");
7975 goto fail;
7978 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
7980 ERR("Failed to initialize pixel shader constant heap\n");
7981 goto fail;
7984 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
7986 ERR("Failed to initialize rbtree.\n");
7987 goto fail;
7990 priv->next_constant_version = 1;
7991 priv->vertex_pipe = vertex_pipe;
7992 priv->fragment_pipe = fragment_pipe;
7993 fragment_pipe->get_caps(gl_info, &fragment_caps);
7994 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
7995 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
7997 device->vertex_priv = vertex_priv;
7998 device->fragment_priv = fragment_priv;
7999 device->shader_priv = priv;
8001 return WINED3D_OK;
8003 fail:
8004 constant_heap_free(&priv->pconst_heap);
8005 constant_heap_free(&priv->vconst_heap);
8006 HeapFree(GetProcessHeap(), 0, priv->stack);
8007 string_buffer_free(&priv->shader_buffer);
8008 fragment_pipe->free_private(device);
8009 vertex_pipe->vp_free(device);
8010 HeapFree(GetProcessHeap(), 0, priv);
8011 return E_OUTOFMEMORY;
8014 /* Context activation is done by the caller. */
8015 static void shader_glsl_free(struct wined3d_device *device)
8017 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
8018 struct shader_glsl_priv *priv = device->shader_priv;
8019 int i;
8021 for (i = 0; i < WINED3D_GL_RES_TYPE_COUNT; ++i)
8023 if (priv->depth_blt_program_full[i])
8025 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
8027 if (priv->depth_blt_program_masked[i])
8029 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
8033 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
8034 constant_heap_free(&priv->pconst_heap);
8035 constant_heap_free(&priv->vconst_heap);
8036 HeapFree(GetProcessHeap(), 0, priv->stack);
8037 string_buffer_list_cleanup(&priv->string_buffers);
8038 string_buffer_free(&priv->shader_buffer);
8039 priv->fragment_pipe->free_private(device);
8040 priv->vertex_pipe->vp_free(device);
8042 HeapFree(GetProcessHeap(), 0, device->shader_priv);
8043 device->shader_priv = NULL;
8046 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
8048 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
8049 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
8052 static void shader_glsl_free_context_data(struct wined3d_context *context)
8054 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
8057 static void shader_glsl_init_context_state(struct wined3d_context *context)
8059 const struct wined3d_gl_info *gl_info = context->gl_info;
8061 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
8062 checkGLcall("GL_PROGRAM_POINT_SIZE");
8065 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
8067 UINT shader_model;
8069 /* FIXME: Check for the specific extensions required for SM5 support
8070 * (ARB_compute_shader, ARB_tessellation_shader, ARB_gpu_shader5, ...) as
8071 * soon as we introduce them, adjusting the GL / GLSL version checks
8072 * accordingly. */
8073 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 30) && gl_info->supported[WINED3D_GL_VERSION_4_3])
8074 shader_model = 5;
8075 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50) && gl_info->supported[WINED3D_GL_VERSION_3_2]
8076 && gl_info->supported[ARB_SHADER_BIT_ENCODING] && gl_info->supported[ARB_SAMPLER_OBJECTS]
8077 && gl_info->supported[ARB_TEXTURE_SWIZZLE])
8078 shader_model = 4;
8079 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
8080 * texldd and texldl instructions. */
8081 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
8082 shader_model = 3;
8083 else
8084 shader_model = 2;
8085 TRACE("Shader model %u.\n", shader_model);
8087 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
8088 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
8089 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
8090 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
8091 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
8093 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
8094 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
8095 caps->varying_count = gl_info->limits.glsl_varyings;
8097 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
8098 * Direct3D minimum requirement.
8100 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
8101 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
8103 * The problem is that the refrast clamps temporary results in the shader to
8104 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
8105 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
8106 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
8107 * offer a way to query this.
8109 if (shader_model >= 4)
8110 caps->ps_1x_max_value = FLT_MAX;
8111 else
8112 caps->ps_1x_max_value = 1024.0f;
8114 /* Ideally we'd only set caps like sRGB writes here if supported by both
8115 * the shader backend and the fragment pipe, but we can get called before
8116 * shader_glsl_alloc(). */
8117 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
8118 | WINED3D_SHADER_CAP_SRGB_WRITE;
8121 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
8123 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
8125 TRACE("Checking support for fixup:\n");
8126 dump_color_fixup_desc(fixup);
8129 /* We support everything except YUV conversions. */
8130 if (!is_complex_fixup(fixup))
8132 TRACE("[OK]\n");
8133 return TRUE;
8136 TRACE("[FAILED]\n");
8137 return FALSE;
8140 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
8142 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
8143 /* WINED3DSIH_ADD */ shader_glsl_binop,
8144 /* WINED3DSIH_AND */ shader_glsl_binop,
8145 /* WINED3DSIH_BEM */ shader_glsl_bem,
8146 /* WINED3DSIH_BREAK */ shader_glsl_break,
8147 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
8148 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
8149 /* WINED3DSIH_CALL */ shader_glsl_call,
8150 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
8151 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
8152 /* WINED3DSIH_CND */ shader_glsl_cnd,
8153 /* WINED3DSIH_CRS */ shader_glsl_cross,
8154 /* WINED3DSIH_CUT */ shader_glsl_cut,
8155 /* WINED3DSIH_DCL */ shader_glsl_nop,
8156 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
8157 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
8158 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ NULL,
8159 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ NULL,
8160 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ NULL,
8161 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
8162 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ NULL,
8163 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
8164 /* WINED3DSIH_DCL_INPUT_PS */ NULL,
8165 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
8166 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
8167 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
8168 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
8169 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
8170 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ NULL,
8171 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
8172 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
8173 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ NULL,
8174 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
8175 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
8176 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ NULL,
8177 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ NULL,
8178 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ NULL,
8179 /* WINED3DSIH_DCL_UAV_TYPED */ NULL,
8180 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
8181 /* WINED3DSIH_DEF */ shader_glsl_nop,
8182 /* WINED3DSIH_DEFB */ shader_glsl_nop,
8183 /* WINED3DSIH_DEFI */ shader_glsl_nop,
8184 /* WINED3DSIH_DIV */ shader_glsl_binop,
8185 /* WINED3DSIH_DP2 */ shader_glsl_dot,
8186 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
8187 /* WINED3DSIH_DP3 */ shader_glsl_dot,
8188 /* WINED3DSIH_DP4 */ shader_glsl_dot,
8189 /* WINED3DSIH_DST */ shader_glsl_dst,
8190 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
8191 /* WINED3DSIH_DSX_COARSE */ NULL,
8192 /* WINED3DSIH_DSX_FINE */ NULL,
8193 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
8194 /* WINED3DSIH_DSY_COARSE */ NULL,
8195 /* WINED3DSIH_DSY_FINE */ NULL,
8196 /* WINED3DSIH_ELSE */ shader_glsl_else,
8197 /* WINED3DSIH_EMIT */ shader_glsl_emit,
8198 /* WINED3DSIH_ENDIF */ shader_glsl_end,
8199 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
8200 /* WINED3DSIH_ENDREP */ shader_glsl_end,
8201 /* WINED3DSIH_EQ */ shader_glsl_relop,
8202 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
8203 /* WINED3DSIH_EXPP */ shader_glsl_expp,
8204 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
8205 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
8206 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
8207 /* WINED3DSIH_GE */ shader_glsl_relop,
8208 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ NULL,
8209 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
8210 /* WINED3DSIH_HS_FORK_PHASE */ NULL,
8211 /* WINED3DSIH_HS_JOIN_PHASE */ NULL,
8212 /* WINED3DSIH_IADD */ shader_glsl_binop,
8213 /* WINED3DSIH_IEQ */ shader_glsl_relop,
8214 /* WINED3DSIH_IF */ shader_glsl_if,
8215 /* WINED3DSIH_IFC */ shader_glsl_ifc,
8216 /* WINED3DSIH_IGE */ shader_glsl_relop,
8217 /* WINED3DSIH_ILT */ shader_glsl_relop,
8218 /* WINED3DSIH_IMAD */ shader_glsl_mad,
8219 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
8220 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
8221 /* WINED3DSIH_IMUL */ shader_glsl_imul,
8222 /* WINED3DSIH_INE */ shader_glsl_relop,
8223 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
8224 /* WINED3DSIH_ISHL */ shader_glsl_binop,
8225 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
8226 /* WINED3DSIH_LABEL */ shader_glsl_label,
8227 /* WINED3DSIH_LD */ shader_glsl_ld,
8228 /* WINED3DSIH_LD2DMS */ NULL,
8229 /* WINED3DSIH_LD_STRUCTURED */ NULL,
8230 /* WINED3DSIH_LIT */ shader_glsl_lit,
8231 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
8232 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
8233 /* WINED3DSIH_LOOP */ shader_glsl_loop,
8234 /* WINED3DSIH_LRP */ shader_glsl_lrp,
8235 /* WINED3DSIH_LT */ shader_glsl_relop,
8236 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
8237 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
8238 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
8239 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
8240 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
8241 /* WINED3DSIH_MAD */ shader_glsl_mad,
8242 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
8243 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
8244 /* WINED3DSIH_MOV */ shader_glsl_mov,
8245 /* WINED3DSIH_MOVA */ shader_glsl_mov,
8246 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
8247 /* WINED3DSIH_MUL */ shader_glsl_binop,
8248 /* WINED3DSIH_NE */ shader_glsl_relop,
8249 /* WINED3DSIH_NOP */ shader_glsl_nop,
8250 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
8251 /* WINED3DSIH_NRM */ shader_glsl_nrm,
8252 /* WINED3DSIH_OR */ shader_glsl_binop,
8253 /* WINED3DSIH_PHASE */ shader_glsl_nop,
8254 /* WINED3DSIH_POW */ shader_glsl_pow,
8255 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
8256 /* WINED3DSIH_REP */ shader_glsl_rep,
8257 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
8258 /* WINED3DSIH_RET */ shader_glsl_ret,
8259 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
8260 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
8261 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
8262 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
8263 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
8264 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
8265 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
8266 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
8267 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
8268 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
8269 /* WINED3DSIH_SETP */ NULL,
8270 /* WINED3DSIH_SGE */ shader_glsl_compare,
8271 /* WINED3DSIH_SGN */ shader_glsl_sgn,
8272 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
8273 /* WINED3DSIH_SLT */ shader_glsl_compare,
8274 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
8275 /* WINED3DSIH_STORE_UAV_TYPED */ NULL,
8276 /* WINED3DSIH_SUB */ shader_glsl_binop,
8277 /* WINED3DSIH_TEX */ shader_glsl_tex,
8278 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
8279 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
8280 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
8281 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
8282 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
8283 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
8284 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
8285 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
8286 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
8287 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
8288 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
8289 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
8290 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
8291 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
8292 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
8293 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
8294 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
8295 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
8296 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
8297 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
8298 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
8299 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
8300 /* WINED3DSIH_UGE */ shader_glsl_relop,
8301 /* WINED3DSIH_ULT */ shader_glsl_relop,
8302 /* WINED3DSIH_USHR */ shader_glsl_binop,
8303 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
8304 /* WINED3DSIH_XOR */ shader_glsl_binop,
8307 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
8308 SHADER_HANDLER hw_fct;
8310 /* Select handler */
8311 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
8313 /* Unhandled opcode */
8314 if (!hw_fct)
8316 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
8317 return;
8319 hw_fct(ins);
8321 shader_glsl_add_instruction_modifiers(ins);
8324 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
8326 struct shader_glsl_priv *priv = shader_priv;
8328 return priv->ffp_proj_control;
8331 const struct wined3d_shader_backend_ops glsl_shader_backend =
8333 shader_glsl_handle_instruction,
8334 shader_glsl_select,
8335 shader_glsl_disable,
8336 shader_glsl_select_depth_blt,
8337 shader_glsl_deselect_depth_blt,
8338 shader_glsl_update_float_vertex_constants,
8339 shader_glsl_update_float_pixel_constants,
8340 shader_glsl_load_constants,
8341 shader_glsl_destroy,
8342 shader_glsl_alloc,
8343 shader_glsl_free,
8344 shader_glsl_allocate_context_data,
8345 shader_glsl_free_context_data,
8346 shader_glsl_init_context_state,
8347 shader_glsl_get_caps,
8348 shader_glsl_color_fixup_supported,
8349 shader_glsl_has_ffp_proj_control,
8352 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
8354 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
8356 caps->xyzrhw = TRUE;
8357 caps->emulated_flatshading = !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
8358 caps->ffp_generic_attributes = TRUE;
8359 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
8360 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
8361 caps->max_vertex_blend_matrix_index = 0;
8362 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
8363 | WINED3DVTXPCAPS_MATERIALSOURCE7
8364 | WINED3DVTXPCAPS_VERTEXFOG
8365 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
8366 | WINED3DVTXPCAPS_POSITIONALLIGHTS
8367 | WINED3DVTXPCAPS_LOCALVIEWER
8368 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
8369 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
8370 caps->max_user_clip_planes = gl_info->limits.clipplanes;
8371 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
8374 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
8376 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8377 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8378 return 0;
8381 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8383 struct shader_glsl_priv *priv;
8385 if (shader_backend == &glsl_shader_backend)
8387 priv = shader_priv;
8389 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
8391 ERR("Failed to initialize rbtree.\n");
8392 return NULL;
8395 return priv;
8398 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
8400 return NULL;
8403 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
8405 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8406 struct glsl_ffp_vertex_shader, desc.entry);
8407 struct glsl_shader_prog_link *program, *program2;
8408 struct glsl_ffp_destroy_ctx *ctx = context;
8410 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8411 struct glsl_shader_prog_link, vs.shader_entry)
8413 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8415 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8416 HeapFree(GetProcessHeap(), 0, shader);
8419 /* Context activation is done by the caller. */
8420 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
8422 struct shader_glsl_priv *priv = device->vertex_priv;
8423 struct glsl_ffp_destroy_ctx ctx;
8425 ctx.priv = priv;
8426 ctx.gl_info = &device->adapter->gl_info;
8427 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
8430 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
8431 const struct wined3d_state *state, DWORD state_id) {}
8433 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
8434 const struct wined3d_state *state, DWORD state_id)
8436 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8439 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
8440 const struct wined3d_state *state, DWORD state_id)
8442 const struct wined3d_gl_info *gl_info = context->gl_info;
8443 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
8444 BOOL transformed = context->stream_info.position_transformed;
8445 BOOL wasrhw = context->last_was_rhw;
8446 unsigned int i;
8448 context->last_was_rhw = transformed;
8450 /* If the vertex declaration contains a transformed position attribute,
8451 * the draw uses the fixed function vertex pipeline regardless of any
8452 * vertex shader set by the application. */
8453 if (transformed != wasrhw)
8454 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8456 if (!use_vs(state))
8458 if (context->last_was_vshader)
8460 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8461 clipplane(context, state, STATE_CLIPPLANE(i));
8464 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8466 /* Because of settings->texcoords, we have to regenerate the vertex
8467 * shader on a vdecl change if there aren't enough varyings to just
8468 * always output all the texture coordinates. */
8469 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
8470 || normal != context->last_was_normal)
8471 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8473 if (use_ps(state)
8474 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
8475 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
8476 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8478 else
8480 if (!context->last_was_vshader)
8482 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
8483 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8484 clipplane(context, state, STATE_CLIPPLANE(i));
8488 context->last_was_vshader = use_vs(state);
8489 context->last_was_normal = normal;
8492 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
8493 const struct wined3d_state *state, DWORD state_id)
8495 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8496 /* Different vertex shaders potentially require a different vertex attributes setup. */
8497 if (!isStateDirty(context, STATE_VDECL))
8498 context_apply_state(context, state, STATE_VDECL);
8501 static void glsl_vertex_pipe_world(struct wined3d_context *context,
8502 const struct wined3d_state *state, DWORD state_id)
8504 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
8507 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
8508 const struct wined3d_state *state, DWORD state_id)
8510 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8513 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
8515 const struct wined3d_gl_info *gl_info = context->gl_info;
8516 unsigned int k;
8518 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
8519 | WINED3D_SHADER_CONST_FFP_LIGHTS
8520 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8522 for (k = 0; k < gl_info->limits.clipplanes; ++k)
8524 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
8525 clipplane(context, state, STATE_CLIPPLANE(k));
8529 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
8530 const struct wined3d_state *state, DWORD state_id)
8532 /* Table fog behavior depends on the projection matrix. */
8533 if (state->render_states[WINED3D_RS_FOGENABLE]
8534 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
8535 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8536 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
8539 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
8540 const struct wined3d_state *state, DWORD state_id)
8542 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
8543 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
8544 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
8545 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
8546 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8547 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
8550 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
8551 const struct wined3d_state *state, DWORD state_id)
8553 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8556 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
8557 const struct wined3d_state *state, DWORD state_id)
8559 DWORD sampler = state_id - STATE_SAMPLER(0);
8560 const struct wined3d_texture *texture = state->textures[sampler];
8561 BOOL np2;
8563 if (!texture)
8564 return;
8566 if (sampler >= MAX_TEXTURES)
8567 return;
8569 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
8570 || context->lastWasPow2Texture & (1u << sampler))
8572 if (np2)
8573 context->lastWasPow2Texture |= 1u << sampler;
8574 else
8575 context->lastWasPow2Texture &= ~(1u << sampler);
8577 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8581 static void glsl_vertex_pipe_material(struct wined3d_context *context,
8582 const struct wined3d_state *state, DWORD state_id)
8584 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
8587 static void glsl_vertex_pipe_light(struct wined3d_context *context,
8588 const struct wined3d_state *state, DWORD state_id)
8590 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
8593 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
8594 const struct wined3d_state *state, DWORD state_id)
8596 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8599 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
8600 const struct wined3d_state *state, DWORD state_id)
8602 if (!use_vs(state))
8603 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8606 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
8607 const struct wined3d_state *state, DWORD state_id)
8609 static unsigned int once;
8611 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
8612 FIXME("Non-point sprite points not supported in core profile.\n");
8615 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
8616 const struct wined3d_state *state, DWORD state_id)
8618 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
8621 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
8623 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8624 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
8625 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
8626 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
8627 /* Clip planes */
8628 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
8629 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
8630 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
8631 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
8632 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
8633 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
8634 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
8635 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
8636 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
8637 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
8638 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
8639 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
8640 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
8641 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
8642 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
8643 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
8644 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
8645 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
8646 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
8647 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
8648 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
8649 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
8650 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
8651 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
8652 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
8653 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
8654 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
8655 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
8656 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
8657 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
8658 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
8659 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
8660 /* Lights */
8661 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8662 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8663 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8664 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8665 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8666 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8667 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8668 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8669 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8670 /* Viewport */
8671 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
8672 /* Transform states */
8673 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
8674 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
8675 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8676 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8677 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8678 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8679 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8680 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8681 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8682 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8683 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
8684 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8685 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8686 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8687 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8688 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8689 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8690 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8691 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8692 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8693 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8694 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8695 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8696 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8697 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8698 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8699 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8700 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8701 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8702 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8703 /* Fog */
8704 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8705 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8706 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8707 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8708 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
8709 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
8710 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8711 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8712 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8713 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8714 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8715 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8716 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8717 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8718 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8719 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8720 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
8721 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
8722 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
8723 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
8724 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
8725 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
8726 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8727 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8728 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8729 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
8730 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8731 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8732 /* NP2 texture matrix fixups. They are not needed if
8733 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
8734 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
8735 * matrix. */
8736 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8737 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8738 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8739 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8740 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8741 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8742 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8743 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8744 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8745 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8746 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8747 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8748 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8749 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8750 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8751 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8752 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8753 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8754 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8755 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8756 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8757 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8758 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8759 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8760 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8761 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_LEGACY_CONTEXT },
8762 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GL_EXT_NONE },
8763 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
8766 /* TODO:
8767 * - Implement vertex tweening. */
8768 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
8770 glsl_vertex_pipe_vp_enable,
8771 glsl_vertex_pipe_vp_get_caps,
8772 glsl_vertex_pipe_vp_get_emul_mask,
8773 glsl_vertex_pipe_vp_alloc,
8774 glsl_vertex_pipe_vp_free,
8775 glsl_vertex_pipe_vp_states,
8778 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
8780 /* Nothing to do. */
8783 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
8785 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
8786 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
8787 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
8788 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
8789 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
8790 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
8791 | WINED3DTEXOPCAPS_SELECTARG1
8792 | WINED3DTEXOPCAPS_SELECTARG2
8793 | WINED3DTEXOPCAPS_MODULATE4X
8794 | WINED3DTEXOPCAPS_MODULATE2X
8795 | WINED3DTEXOPCAPS_MODULATE
8796 | WINED3DTEXOPCAPS_ADDSIGNED2X
8797 | WINED3DTEXOPCAPS_ADDSIGNED
8798 | WINED3DTEXOPCAPS_ADD
8799 | WINED3DTEXOPCAPS_SUBTRACT
8800 | WINED3DTEXOPCAPS_ADDSMOOTH
8801 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
8802 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
8803 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
8804 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
8805 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
8806 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
8807 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
8808 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
8809 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
8810 | WINED3DTEXOPCAPS_DOTPRODUCT3
8811 | WINED3DTEXOPCAPS_MULTIPLYADD
8812 | WINED3DTEXOPCAPS_LERP
8813 | WINED3DTEXOPCAPS_BUMPENVMAP
8814 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
8815 caps->MaxTextureBlendStages = 8;
8816 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
8819 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
8821 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8822 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8823 return 0;
8826 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8828 struct shader_glsl_priv *priv;
8830 if (shader_backend == &glsl_shader_backend)
8832 priv = shader_priv;
8834 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
8836 ERR("Failed to initialize rbtree.\n");
8837 return NULL;
8840 return priv;
8843 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
8845 return NULL;
8848 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
8850 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8851 struct glsl_ffp_fragment_shader, entry.entry);
8852 struct glsl_shader_prog_link *program, *program2;
8853 struct glsl_ffp_destroy_ctx *ctx = context;
8855 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8856 struct glsl_shader_prog_link, ps.shader_entry)
8858 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8860 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8861 HeapFree(GetProcessHeap(), 0, shader);
8864 /* Context activation is done by the caller. */
8865 static void glsl_fragment_pipe_free(struct wined3d_device *device)
8867 struct shader_glsl_priv *priv = device->fragment_priv;
8868 struct glsl_ffp_destroy_ctx ctx;
8870 ctx.priv = priv;
8871 ctx.gl_info = &device->adapter->gl_info;
8872 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
8875 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
8876 const struct wined3d_state *state, DWORD state_id)
8878 context->last_was_pshader = use_ps(state);
8880 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8883 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
8884 const struct wined3d_state *state, DWORD state_id)
8886 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8889 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
8890 const struct wined3d_state *state, DWORD state_id)
8892 BOOL use_vshader = use_vs(state);
8893 enum fogsource new_source;
8894 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
8895 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
8897 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8899 if (!state->render_states[WINED3D_RS_FOGENABLE])
8900 return;
8902 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
8904 if (use_vshader)
8905 new_source = FOGSOURCE_VS;
8906 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
8907 new_source = FOGSOURCE_COORD;
8908 else
8909 new_source = FOGSOURCE_FFP;
8911 else
8913 new_source = FOGSOURCE_FFP;
8916 if (new_source != context->fog_source || fogstart == fogend)
8918 context->fog_source = new_source;
8919 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8923 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
8924 const struct wined3d_state *state, DWORD state_id)
8926 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
8927 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
8928 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8930 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
8931 glsl_fragment_pipe_fog(context, state, state_id);
8934 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
8935 const struct wined3d_state *state, DWORD state_id)
8937 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
8938 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
8939 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8942 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
8943 const struct wined3d_state *state, DWORD state_id)
8945 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8948 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
8949 const struct wined3d_state *state, DWORD state_id)
8951 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
8954 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
8955 const struct wined3d_state *state, DWORD state_id)
8957 const struct wined3d_gl_info *gl_info = context->gl_info;
8958 int glParm;
8959 float ref;
8961 TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
8963 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
8965 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
8966 checkGLcall("glEnable GL_ALPHA_TEST");
8968 else
8970 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
8971 checkGLcall("glDisable GL_ALPHA_TEST");
8972 return;
8975 ref = ((float)state->render_states[WINED3D_RS_ALPHAREF]) / 255.0f;
8976 glParm = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
8978 if (glParm)
8980 gl_info->gl_ops.gl.p_glAlphaFunc(glParm, ref);
8981 checkGLcall("glAlphaFunc");
8985 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
8986 const struct wined3d_state *state, DWORD state_id)
8988 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
8991 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
8992 const struct wined3d_state *state, DWORD state_id)
8994 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
8997 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
8999 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
9000 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
9001 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9002 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9003 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9004 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9005 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9006 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9007 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9008 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9009 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9010 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9011 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9012 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9013 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9014 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9015 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9016 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9017 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9018 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9019 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9020 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9021 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9022 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9023 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9024 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9025 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9026 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9027 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9028 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9029 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9030 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9031 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9032 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9033 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9034 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9035 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9036 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9037 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9038 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9039 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9040 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9041 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9042 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9043 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9044 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9045 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9046 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9047 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9048 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9049 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9050 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9051 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9052 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9053 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9054 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9055 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9056 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9057 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9058 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9059 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9060 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9061 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9062 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9063 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9064 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9065 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9066 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9067 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9068 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9069 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9070 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9071 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9072 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9073 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9074 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9075 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
9076 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
9077 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_EXT_NONE },
9078 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9079 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
9080 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
9081 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9082 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9083 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9084 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
9085 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
9086 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9087 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9088 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9089 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
9090 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
9091 {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 },
9092 {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 },
9093 {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 },
9094 {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 },
9095 {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 },
9096 {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 },
9097 {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 },
9098 {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 },
9099 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9100 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9101 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9102 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9103 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9104 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9105 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9106 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9107 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9108 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9109 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_LEGACY_CONTEXT},
9110 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GL_EXT_NONE },
9111 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
9114 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
9116 return TRUE;
9119 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
9123 const struct fragment_pipeline glsl_fragment_pipe =
9125 glsl_fragment_pipe_enable,
9126 glsl_fragment_pipe_get_caps,
9127 glsl_fragment_pipe_get_emul_mask,
9128 glsl_fragment_pipe_alloc,
9129 glsl_fragment_pipe_free,
9130 glsl_fragment_pipe_alloc_context_data,
9131 glsl_fragment_pipe_free_context_data,
9132 shader_glsl_color_fixup_supported,
9133 glsl_fragment_pipe_state_template,