d3d9/tests: Add some more surface GetDC() tests to test_getdc().
[wine.git] / dlls / wined3d / glsl_shader.c
blobcea5e5a8a3979adaef7c920bf4ab045c4fcf36cf
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, GLint location, const GLfloat *data)
630 GLfloat clamped_constant[4];
632 if (location == -1) return;
634 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
635 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
636 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
637 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
639 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
642 /* Context activation is done by the caller. */
643 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
644 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
646 int stack_idx = 0;
647 unsigned int heap_idx = 1;
648 unsigned int idx;
650 if (heap->entries[heap_idx].version <= version) return;
652 idx = heap->entries[heap_idx].idx;
653 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
654 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
656 while (stack_idx >= 0)
658 /* Note that we fall through to the next case statement. */
659 switch(stack[stack_idx])
661 case HEAP_NODE_TRAVERSE_LEFT:
663 unsigned int left_idx = heap_idx << 1;
664 if (left_idx < heap->size && heap->entries[left_idx].version > version)
666 heap_idx = left_idx;
667 idx = heap->entries[heap_idx].idx;
668 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
670 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
671 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
672 break;
676 case HEAP_NODE_TRAVERSE_RIGHT:
678 unsigned int right_idx = (heap_idx << 1) + 1;
679 if (right_idx < heap->size && heap->entries[right_idx].version > version)
681 heap_idx = right_idx;
682 idx = heap->entries[heap_idx].idx;
683 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
685 stack[stack_idx++] = HEAP_NODE_POP;
686 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
687 break;
691 case HEAP_NODE_POP:
692 heap_idx >>= 1;
693 --stack_idx;
694 break;
697 checkGLcall("walk_constant_heap_clamped()");
700 /* Context activation is done by the caller. */
701 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
702 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
703 unsigned char *stack, UINT version)
705 const struct wined3d_shader_lconst *lconst;
707 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
708 if (shader->reg_maps.shader_version.major == 1
709 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
710 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
711 else
712 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
714 if (!shader->load_local_constsF)
716 TRACE("No need to load local float constants for this shader\n");
717 return;
720 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
721 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
723 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
725 checkGLcall("glUniform4fv()");
728 /* Context activation is done by the caller. */
729 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
730 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
732 unsigned int i;
733 struct list* ptr;
735 for (i = 0; constants_set; constants_set >>= 1, ++i)
737 if (!(constants_set & 1)) continue;
739 /* We found this uniform name in the program - go ahead and send the data */
740 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i * 4]));
743 /* Load immediate constants */
744 ptr = list_head(&shader->constantsI);
745 while (ptr)
747 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
748 unsigned int idx = lconst->idx;
749 const GLint *values = (const GLint *)lconst->value;
751 /* We found this uniform name in the program - go ahead and send the data */
752 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
753 ptr = list_next(&shader->constantsI, ptr);
755 checkGLcall("glUniform4iv()");
758 /* Context activation is done by the caller. */
759 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
760 const GLint locations[MAX_CONST_B], const BOOL *constants, WORD constants_set)
762 unsigned int i;
763 struct list* ptr;
765 for (i = 0; constants_set; constants_set >>= 1, ++i)
767 if (!(constants_set & 1)) continue;
769 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
772 /* Load immediate constants */
773 ptr = list_head(&shader->constantsB);
774 while (ptr)
776 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
777 unsigned int idx = lconst->idx;
778 const GLint *values = (const GLint *)lconst->value;
780 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
781 ptr = list_next(&shader->constantsB, ptr);
783 checkGLcall("glUniform1iv()");
786 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
788 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
791 /* Context activation is done by the caller (state handler). */
792 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
793 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
795 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
796 UINT fixup = ps->np2_fixup_info->active;
797 UINT i;
799 for (i = 0; fixup; fixup >>= 1, ++i)
801 const struct wined3d_texture *tex = state->textures[i];
802 unsigned char idx = ps->np2_fixup_info->idx[i];
803 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
805 if (!tex)
807 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
808 continue;
811 if (idx % 2)
813 tex_dim[2] = tex->pow2_matrix[0];
814 tex_dim[3] = tex->pow2_matrix[5];
816 else
818 tex_dim[0] = tex->pow2_matrix[0];
819 tex_dim[1] = tex->pow2_matrix[5];
823 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
826 /* Taken and adapted from Mesa. */
827 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
829 float pos, neg, t, det;
830 struct wined3d_matrix temp;
832 /* Calculate the determinant of upper left 3x3 submatrix and
833 * determine if the matrix is singular. */
834 pos = neg = 0.0f;
835 t = in->_11 * in->_22 * in->_33;
836 if (t >= 0.0f)
837 pos += t;
838 else
839 neg += t;
841 t = in->_21 * in->_32 * in->_13;
842 if (t >= 0.0f)
843 pos += t;
844 else
845 neg += t;
846 t = in->_31 * in->_12 * in->_23;
847 if (t >= 0.0f)
848 pos += t;
849 else
850 neg += t;
852 t = -in->_31 * in->_22 * in->_13;
853 if (t >= 0.0f)
854 pos += t;
855 else
856 neg += t;
857 t = -in->_21 * in->_12 * in->_33;
858 if (t >= 0.0f)
859 pos += t;
860 else
861 neg += t;
863 t = -in->_11 * in->_32 * in->_23;
864 if (t >= 0.0f)
865 pos += t;
866 else
867 neg += t;
869 det = pos + neg;
871 if (fabsf(det) < 1e-25f)
872 return FALSE;
874 det = 1.0f / det;
875 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
876 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
877 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
878 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
879 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
880 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
881 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
882 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
883 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
885 *out = temp;
886 return TRUE;
889 static void swap_rows(float **a, float **b)
891 float *tmp = *a;
893 *a = *b;
894 *b = tmp;
897 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
899 float wtmp[4][8];
900 float m0, m1, m2, m3, s;
901 float *r0, *r1, *r2, *r3;
903 r0 = wtmp[0];
904 r1 = wtmp[1];
905 r2 = wtmp[2];
906 r3 = wtmp[3];
908 r0[0] = m->_11;
909 r0[1] = m->_12;
910 r0[2] = m->_13;
911 r0[3] = m->_14;
912 r0[4] = 1.0f;
913 r0[5] = r0[6] = r0[7] = 0.0f;
915 r1[0] = m->_21;
916 r1[1] = m->_22;
917 r1[2] = m->_23;
918 r1[3] = m->_24;
919 r1[5] = 1.0f;
920 r1[4] = r1[6] = r1[7] = 0.0f;
922 r2[0] = m->_31;
923 r2[1] = m->_32;
924 r2[2] = m->_33;
925 r2[3] = m->_34;
926 r2[6] = 1.0f;
927 r2[4] = r2[5] = r2[7] = 0.0f;
929 r3[0] = m->_41;
930 r3[1] = m->_42;
931 r3[2] = m->_43;
932 r3[3] = m->_44;
933 r3[7] = 1.0f;
934 r3[4] = r3[5] = r3[6] = 0.0f;
936 /* Choose pivot - or die. */
937 if (fabsf(r3[0]) > fabsf(r2[0]))
938 swap_rows(&r3, &r2);
939 if (fabsf(r2[0]) > fabsf(r1[0]))
940 swap_rows(&r2, &r1);
941 if (fabsf(r1[0]) > fabsf(r0[0]))
942 swap_rows(&r1, &r0);
943 if (r0[0] == 0.0f)
944 return FALSE;
946 /* Eliminate first variable. */
947 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
948 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
949 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
950 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
951 s = r0[4];
952 if (s != 0.0f)
954 r1[4] -= m1 * s;
955 r2[4] -= m2 * s;
956 r3[4] -= m3 * s;
958 s = r0[5];
959 if (s != 0.0f)
961 r1[5] -= m1 * s;
962 r2[5] -= m2 * s;
963 r3[5] -= m3 * s;
965 s = r0[6];
966 if (s != 0.0f)
968 r1[6] -= m1 * s;
969 r2[6] -= m2 * s;
970 r3[6] -= m3 * s;
972 s = r0[7];
973 if (s != 0.0f)
975 r1[7] -= m1 * s;
976 r2[7] -= m2 * s;
977 r3[7] -= m3 * s;
980 /* Choose pivot - or die. */
981 if (fabsf(r3[1]) > fabsf(r2[1]))
982 swap_rows(&r3, &r2);
983 if (fabsf(r2[1]) > fabsf(r1[1]))
984 swap_rows(&r2, &r1);
985 if (r1[1] == 0.0f)
986 return FALSE;
988 /* Eliminate second variable. */
989 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
990 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
991 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
992 s = r1[4];
993 if (s != 0.0f)
995 r2[4] -= m2 * s;
996 r3[4] -= m3 * s;
998 s = r1[5];
999 if (s != 0.0f)
1001 r2[5] -= m2 * s;
1002 r3[5] -= m3 * s;
1004 s = r1[6];
1005 if (s != 0.0f)
1007 r2[6] -= m2 * s;
1008 r3[6] -= m3 * s;
1010 s = r1[7];
1011 if (s != 0.0f)
1013 r2[7] -= m2 * s;
1014 r3[7] -= m3 * s;
1017 /* Choose pivot - or die. */
1018 if (fabsf(r3[2]) > fabsf(r2[2]))
1019 swap_rows(&r3, &r2);
1020 if (r2[2] == 0.0f)
1021 return FALSE;
1023 /* Eliminate third variable. */
1024 m3 = r3[2] / r2[2];
1025 r3[3] -= m3 * r2[3];
1026 r3[4] -= m3 * r2[4];
1027 r3[5] -= m3 * r2[5];
1028 r3[6] -= m3 * r2[6];
1029 r3[7] -= m3 * r2[7];
1031 /* Last check. */
1032 if (r3[3] == 0.0f)
1033 return FALSE;
1035 /* Back substitute row 3. */
1036 s = 1.0f / r3[3];
1037 r3[4] *= s;
1038 r3[5] *= s;
1039 r3[6] *= s;
1040 r3[7] *= s;
1042 /* Back substitute row 2. */
1043 m2 = r2[3];
1044 s = 1.0f / r2[2];
1045 r2[4] = s * (r2[4] - r3[4] * m2);
1046 r2[5] = s * (r2[5] - r3[5] * m2);
1047 r2[6] = s * (r2[6] - r3[6] * m2);
1048 r2[7] = s * (r2[7] - r3[7] * m2);
1049 m1 = r1[3];
1050 r1[4] -= r3[4] * m1;
1051 r1[5] -= r3[5] * m1;
1052 r1[6] -= r3[6] * m1;
1053 r1[7] -= r3[7] * m1;
1054 m0 = r0[3];
1055 r0[4] -= r3[4] * m0;
1056 r0[5] -= r3[5] * m0;
1057 r0[6] -= r3[6] * m0;
1058 r0[7] -= r3[7] * m0;
1060 /* Back substitute row 1. */
1061 m1 = r1[2];
1062 s = 1.0f / r1[1];
1063 r1[4] = s * (r1[4] - r2[4] * m1);
1064 r1[5] = s * (r1[5] - r2[5] * m1);
1065 r1[6] = s * (r1[6] - r2[6] * m1);
1066 r1[7] = s * (r1[7] - r2[7] * m1);
1067 m0 = r0[2];
1068 r0[4] -= r2[4] * m0;
1069 r0[5] -= r2[5] * m0;
1070 r0[6] -= r2[6] * m0;
1071 r0[7] -= r2[7] * m0;
1073 /* Back substitute row 0. */
1074 m0 = r0[1];
1075 s = 1.0f / r0[0];
1076 r0[4] = s * (r0[4] - r1[4] * m0);
1077 r0[5] = s * (r0[5] - r1[5] * m0);
1078 r0[6] = s * (r0[6] - r1[6] * m0);
1079 r0[7] = s * (r0[7] - r1[7] * m0);
1081 out->_11 = r0[4];
1082 out->_12 = r0[5];
1083 out->_13 = r0[6];
1084 out->_14 = r0[7];
1085 out->_21 = r1[4];
1086 out->_22 = r1[5];
1087 out->_23 = r1[6];
1088 out->_24 = r1[7];
1089 out->_31 = r2[4];
1090 out->_32 = r2[5];
1091 out->_33 = r2[6];
1092 out->_34 = r2[7];
1093 out->_41 = r3[4];
1094 out->_42 = r3[5];
1095 out->_43 = r3[6];
1096 out->_44 = r3[7];
1098 return TRUE;
1101 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1102 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1104 const struct wined3d_gl_info *gl_info = context->gl_info;
1105 float mat[3 * 3];
1106 struct wined3d_matrix mv;
1107 unsigned int i, j;
1109 if (prog->vs.normal_matrix_location == -1)
1110 return;
1112 get_modelview_matrix(context, state, 0, &mv);
1113 if (context->swapchain->device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING)
1114 invert_matrix_3d(&mv, &mv);
1115 else
1116 invert_matrix(&mv, &mv);
1117 /* Tests show that singular modelview matrices are used unchanged as normal
1118 * matrices on D3D3 and older. There seems to be no clearly consistent
1119 * behavior on newer D3D versions so always follow older ddraw behavior. */
1120 for (i = 0; i < 3; ++i)
1121 for (j = 0; j < 3; ++j)
1122 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1124 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1125 checkGLcall("glUniformMatrix3fv");
1128 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1129 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1131 const struct wined3d_gl_info *gl_info = context->gl_info;
1132 struct wined3d_matrix mat;
1134 if (tex >= MAX_TEXTURES)
1135 return;
1136 if (prog->vs.texture_matrix_location[tex] == -1)
1137 return;
1139 get_texture_matrix(context, state, tex, &mat);
1140 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1141 checkGLcall("glUniformMatrix4fv");
1144 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1145 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1147 const struct wined3d_gl_info *gl_info = context->gl_info;
1149 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1151 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1152 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1154 else
1156 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1158 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1160 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1161 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1162 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1163 checkGLcall("setting FFP material uniforms");
1166 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1167 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1169 const struct wined3d_gl_info *gl_info = context->gl_info;
1170 struct wined3d_color color;
1172 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1173 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1174 checkGLcall("glUniform3fv");
1177 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1178 const struct wined3d_matrix *src2)
1180 struct wined3d_vec4 temp;
1182 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1183 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1184 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1185 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1187 *dest = temp;
1190 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1191 const struct wined3d_state *state, unsigned int light, struct glsl_shader_prog_link *prog)
1193 const struct wined3d_gl_info *gl_info = context->gl_info;
1194 const struct wined3d_light_info *light_info = state->lights[light];
1195 struct wined3d_vec4 vec4;
1196 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1198 if (!light_info)
1199 return;
1201 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1202 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1203 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1205 switch (light_info->OriginalParms.type)
1207 case WINED3D_LIGHT_POINT:
1208 multiply_vector_matrix(&vec4, &light_info->position, view);
1209 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1210 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1211 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1212 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1213 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1214 break;
1216 case WINED3D_LIGHT_SPOT:
1217 multiply_vector_matrix(&vec4, &light_info->position, view);
1218 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1220 multiply_vector_matrix(&vec4, &light_info->direction, view);
1221 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1223 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1224 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1225 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1226 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1227 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1228 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1229 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1230 break;
1232 case WINED3D_LIGHT_DIRECTIONAL:
1233 multiply_vector_matrix(&vec4, &light_info->direction, view);
1234 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1235 break;
1237 case WINED3D_LIGHT_PARALLELPOINT:
1238 multiply_vector_matrix(&vec4, &light_info->position, view);
1239 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1240 break;
1242 default:
1243 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1245 checkGLcall("setting FFP lights uniforms");
1248 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1249 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1251 const struct wined3d_gl_info *gl_info = context->gl_info;
1252 float min, max;
1253 float size, att[3];
1255 get_pointsize_minmax(context, state, &min, &max);
1257 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1258 checkGLcall("glUniform1f");
1259 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1260 checkGLcall("glUniform1f");
1262 get_pointsize(context, state, &size, att);
1264 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1265 checkGLcall("glUniform1f");
1266 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1267 checkGLcall("glUniform1f");
1268 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1269 checkGLcall("glUniform1f");
1270 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1271 checkGLcall("glUniform1f");
1274 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1275 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1277 const struct wined3d_gl_info *gl_info = context->gl_info;
1278 struct wined3d_color color;
1279 float start, end, scale;
1280 union
1282 DWORD d;
1283 float f;
1284 } tmpvalue;
1286 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1287 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1288 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1289 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1290 get_fog_start_end(context, state, &start, &end);
1291 scale = 1.0f / (end - start);
1292 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1293 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1294 checkGLcall("fog emulation uniforms");
1297 /* Context activation is done by the caller (state handler). */
1298 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1299 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1301 struct wined3d_color float_key[2];
1302 const struct wined3d_texture *texture = state->textures[0];
1304 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1305 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1308 /* Context activation is done by the caller (state handler). */
1309 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1310 const struct wined3d_state *state)
1312 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1313 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1314 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1315 const struct wined3d_gl_info *gl_info = context->gl_info;
1316 struct shader_glsl_priv *priv = shader_priv;
1317 float position_fixup[4];
1318 DWORD update_mask;
1320 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1321 UINT constant_version;
1322 int i;
1324 if (!prog) {
1325 /* No GLSL program set - nothing to do. */
1326 return;
1328 constant_version = prog->constant_version;
1329 update_mask = context->constant_update_mask & prog->constant_update_mask;
1331 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1332 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
1333 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1335 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1336 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
1337 vshader->reg_maps.integer_constants);
1339 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1340 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1341 vshader->reg_maps.boolean_constants);
1343 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1344 shader_glsl_pointsize_uniform(context, state, prog);
1346 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
1348 shader_get_position_fixup(context, state, position_fixup);
1349 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1350 checkGLcall("glUniform4fv");
1353 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1355 struct wined3d_matrix mat;
1357 get_modelview_matrix(context, state, 0, &mat);
1358 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1359 checkGLcall("glUniformMatrix4fv");
1361 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1364 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1366 struct wined3d_matrix mat;
1368 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1370 if (prog->vs.modelview_matrix_location[i] == -1)
1371 break;
1373 get_modelview_matrix(context, state, i, &mat);
1374 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1375 checkGLcall("glUniformMatrix4fv");
1379 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1381 struct wined3d_matrix projection;
1383 get_projection_matrix(context, state, &projection);
1384 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1385 checkGLcall("glUniformMatrix4fv");
1388 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1390 for (i = 0; i < MAX_TEXTURES; ++i)
1391 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1394 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1395 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1397 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1399 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1400 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1401 shader_glsl_ffp_vertex_light_uniform(context, state, i, prog);
1404 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1405 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
1406 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1408 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1409 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
1410 pshader->reg_maps.integer_constants);
1412 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1413 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1414 pshader->reg_maps.boolean_constants);
1416 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1418 for (i = 0; i < MAX_TEXTURES; ++i)
1420 if (prog->ps.bumpenv_mat_location[i] == -1)
1421 continue;
1423 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1424 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1426 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1428 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1429 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1430 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1431 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1435 checkGLcall("bump env uniforms");
1438 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1440 const struct wined3d_vec4 correction_params =
1442 /* Position is relative to the framebuffer, not the viewport. */
1443 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1444 context->render_offscreen ? 1.0f : -1.0f,
1445 0.0f,
1446 0.0f,
1449 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1452 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1453 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1454 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1455 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1457 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1459 struct wined3d_color color;
1461 if (prog->ps.tex_factor_location != -1)
1463 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1464 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1467 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1468 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1469 else
1470 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1472 for (i = 0; i < MAX_TEXTURES; ++i)
1474 if (prog->ps.tss_constant_location[i] == -1)
1475 continue;
1477 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1478 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1481 checkGLcall("fixed function uniforms");
1484 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1485 shader_glsl_load_fog_uniform(context, state, prog);
1487 if (priv->next_constant_version == UINT_MAX)
1489 TRACE("Max constant version reached, resetting to 0.\n");
1490 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1491 priv->next_constant_version = 1;
1493 else
1495 prog->constant_version = priv->next_constant_version++;
1499 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1501 struct constant_entry *entries = heap->entries;
1502 unsigned int *positions = heap->positions;
1503 unsigned int heap_idx, parent_idx;
1505 if (!heap->contained[idx])
1507 heap_idx = heap->size++;
1508 heap->contained[idx] = TRUE;
1510 else
1512 heap_idx = positions[idx];
1515 while (heap_idx > 1)
1517 parent_idx = heap_idx >> 1;
1519 if (new_version <= entries[parent_idx].version) break;
1521 entries[heap_idx] = entries[parent_idx];
1522 positions[entries[parent_idx].idx] = heap_idx;
1523 heap_idx = parent_idx;
1526 entries[heap_idx].version = new_version;
1527 entries[heap_idx].idx = idx;
1528 positions[idx] = heap_idx;
1531 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1533 struct shader_glsl_priv *priv = device->shader_priv;
1534 struct constant_heap *heap = &priv->vconst_heap;
1535 UINT i;
1537 for (i = start; i < count + start; ++i)
1539 update_heap_entry(heap, i, priv->next_constant_version);
1542 for (i = 0; i < device->context_count; ++i)
1544 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
1548 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1550 struct shader_glsl_priv *priv = device->shader_priv;
1551 struct constant_heap *heap = &priv->pconst_heap;
1552 UINT i;
1554 for (i = start; i < count + start; ++i)
1556 update_heap_entry(heap, i, priv->next_constant_version);
1559 for (i = 0; i < device->context_count; ++i)
1561 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
1565 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1567 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1568 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1569 if(shader_major > 3) return ret;
1571 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1572 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1573 return ret;
1576 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1578 return gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
1581 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
1583 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
1586 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1587 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1589 va_list args;
1590 int ret;
1592 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1593 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1594 for (;;)
1596 va_start(args, format);
1597 ret = shader_vaddline(buffer, format, args);
1598 va_end(args);
1599 if (!ret)
1600 return;
1601 if (!string_buffer_resize(buffer, ret))
1602 return;
1606 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1607 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1609 va_list args;
1610 int ret;
1612 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1613 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1614 for (;;)
1616 va_start(args, format);
1617 ret = shader_vaddline(buffer, format, args);
1618 va_end(args);
1619 if (!ret)
1620 return;
1621 if (!string_buffer_resize(buffer, ret))
1622 return;
1626 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
1628 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
1631 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
1633 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
1634 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1635 const BOOL *input_reg_used = shader->u.ps.input_reg_used;
1636 unsigned int i;
1638 if (reg_maps->shader_version.major < 3)
1639 return input_reg_used[idx];
1641 for (i = 0; i < input_signature->element_count; ++i)
1643 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
1645 if (!(reg_maps->input_registers & (1u << input->register_idx)))
1646 continue;
1648 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
1649 && input->semantic_idx == idx)
1651 if (input_reg_used[input->register_idx])
1652 return TRUE;
1653 else
1654 return FALSE;
1657 return FALSE;
1660 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
1661 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
1663 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
1665 if (version->major >= 4)
1666 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
1667 else
1668 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
1671 /** Generate the variable & register declarations for the GLSL output target */
1672 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1673 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1674 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1676 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1677 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1678 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1679 const struct wined3d_gl_info *gl_info = context->gl_info;
1680 unsigned int i, extra_constants_needed = 0;
1681 const struct wined3d_shader_lconst *lconst;
1682 const char *prefix;
1683 DWORD map;
1685 prefix = shader_glsl_get_prefix(version->type);
1687 /* Prototype the subroutines */
1688 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1690 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1693 /* Declare the constants (aka uniforms) */
1694 if (shader->limits->constant_float > 0)
1696 unsigned max_constantsF;
1698 /* Unless the shader uses indirect addressing, always declare the
1699 * maximum array size and ignore that we need some uniforms privately.
1700 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1701 * and immediate values, still declare VC[256]. If the shader needs
1702 * more uniforms than we have it won't work in any case. If it uses
1703 * less, the compiler will figure out which uniforms are really used
1704 * and strip them out. This allows a shader to use c255 on a dx9 card,
1705 * as long as it doesn't also use all the other constants.
1707 * If the shader uses indirect addressing the compiler must assume
1708 * that all declared uniforms are used. In this case, declare only the
1709 * amount that we're assured to have.
1711 * Thus we run into problems in these two cases:
1712 * 1) The shader really uses more uniforms than supported.
1713 * 2) The shader uses indirect addressing, less constants than
1714 * supported, but uses a constant index > #supported consts. */
1715 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1717 /* No indirect addressing here. */
1718 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1720 else
1722 if (reg_maps->usesrelconstF)
1724 /* Subtract the other potential uniforms from the max
1725 * available (bools, ints, and 1 row of projection matrix).
1726 * Subtract another uniform for immediate values, which have
1727 * to be loaded via uniform by the driver as well. The shader
1728 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1729 * shader code, so one vec4 should be enough. (Unfortunately
1730 * the Nvidia driver doesn't store 128 and -128 in one float).
1732 * Writing gl_ClipVertex requires one uniform for each
1733 * clipplane as well. */
1734 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1735 if (vs_args->clip_enabled)
1736 max_constantsF -= gl_info->limits.clipplanes;
1737 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
1738 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1739 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1740 * for now take this into account when calculating the number of available constants
1742 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
1743 /* Set by driver quirks in directx.c */
1744 max_constantsF -= gl_info->reserved_glsl_constants;
1746 if (max_constantsF < shader->limits->constant_float)
1748 static unsigned int once;
1750 if (!once++)
1751 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1752 " it may not render correctly.\n");
1753 else
1754 WARN("The hardware does not support enough uniform components to run this shader.\n");
1757 else
1759 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1762 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1763 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1766 /* Always declare the full set of constants, the compiler can remove the
1767 * unused ones because d3d doesn't (yet) support indirect int and bool
1768 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1769 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1770 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1772 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1773 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1775 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1777 if (reg_maps->cb_sizes[i])
1778 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1779 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1782 /* Declare texture samplers */
1783 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1785 struct wined3d_shader_sampler_map_entry *entry;
1786 const char *sampler_type_prefix, *sampler_type;
1787 BOOL shadow_sampler, tex_rect;
1789 entry = &reg_maps->sampler_map.entries[i];
1791 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1793 ERR("Invalid resource index %u.\n", entry->resource_idx);
1794 continue;
1797 switch (reg_maps->resource_info[entry->resource_idx].data_type)
1799 case WINED3D_DATA_FLOAT:
1800 case WINED3D_DATA_UNORM:
1801 case WINED3D_DATA_SNORM:
1802 sampler_type_prefix = "";
1803 break;
1805 case WINED3D_DATA_INT:
1806 sampler_type_prefix = "i";
1807 break;
1809 case WINED3D_DATA_UINT:
1810 sampler_type_prefix = "u";
1811 break;
1813 default:
1814 sampler_type_prefix = "";
1815 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
1816 break;
1819 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
1820 switch (reg_maps->resource_info[entry->resource_idx].type)
1822 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1823 if (shadow_sampler)
1824 sampler_type = "sampler1DShadow";
1825 else
1826 sampler_type = "sampler1D";
1827 break;
1829 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1830 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1831 && (ps_args->np2_fixup & (1u << entry->resource_idx))
1832 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1833 if (shadow_sampler)
1835 if (tex_rect)
1836 sampler_type = "sampler2DRectShadow";
1837 else
1838 sampler_type = "sampler2DShadow";
1840 else
1842 if (tex_rect)
1843 sampler_type = "sampler2DRect";
1844 else
1845 sampler_type = "sampler2D";
1847 break;
1849 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1850 if (shadow_sampler)
1851 FIXME("Unsupported 3D shadow sampler.\n");
1852 sampler_type = "sampler3D";
1853 break;
1855 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1856 if (shadow_sampler)
1857 FIXME("Unsupported Cube shadow sampler.\n");
1858 sampler_type = "samplerCube";
1859 break;
1861 default:
1862 sampler_type = "unsupported_sampler";
1863 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[i].type);
1864 break;
1866 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
1867 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
1870 /* Declare uniforms for NP2 texcoord fixup:
1871 * This is NOT done inside the loop that declares the texture samplers
1872 * since the NP2 fixup code is currently only used for the GeforceFX
1873 * series and when forcing the ARB_npot extension off. Modern cards just
1874 * skip the code anyway, so put it inside a separate loop. */
1875 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1877 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1878 UINT cur = 0;
1880 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1881 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1882 * samplerNP2Fixup stores texture dimensions and is updated through
1883 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1885 for (i = 0; i < shader->limits->sampler; ++i)
1887 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
1888 continue;
1890 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1892 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1893 continue;
1896 fixup->idx[i] = cur++;
1899 fixup->num_consts = (cur + 1) >> 1;
1900 fixup->active = ps_args->np2_fixup;
1901 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1904 /* Declare address variables */
1905 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1907 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1910 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1912 for (i = 0; i < shader->input_signature.element_count; ++i)
1914 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
1915 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
1916 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1917 prefix, e->register_idx);
1918 else
1919 shader_addline(buffer, "%s vec4 %s_in%u;\n",
1920 get_attribute_keyword(gl_info), prefix, e->register_idx);
1923 if (vs_args->point_size && !vs_args->per_vertex_point_size)
1925 shader_addline(buffer, "uniform struct\n{\n");
1926 shader_addline(buffer, " float size;\n");
1927 shader_addline(buffer, " float size_min;\n");
1928 shader_addline(buffer, " float size_max;\n");
1929 shader_addline(buffer, "} ffp_point;\n");
1932 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && version->major < 3)
1934 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_diffuse;\n");
1935 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_specular;\n");
1936 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
1937 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
1940 shader_addline(buffer, "uniform vec4 posFixup;\n");
1941 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits->packed_output);
1943 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1945 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
1947 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1949 if (version->major < 3 || ps_args->vp_mode != vertexshader)
1951 shader_addline(buffer, "uniform struct\n{\n");
1952 shader_addline(buffer, " vec4 color;\n");
1953 shader_addline(buffer, " float density;\n");
1954 shader_addline(buffer, " float end;\n");
1955 shader_addline(buffer, " float scale;\n");
1956 shader_addline(buffer, "} ffp_fog;\n");
1958 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
1960 if (glsl_is_color_reg_read(shader, 0))
1961 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
1962 if (glsl_is_color_reg_read(shader, 1))
1963 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
1964 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
1965 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
1967 else
1969 if (glsl_is_color_reg_read(shader, 0))
1970 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_diffuse;\n");
1971 if (glsl_is_color_reg_read(shader, 1))
1972 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_specular;\n");
1973 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
1974 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
1975 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
1979 if (version->major >= 3)
1981 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
1983 if (ps_args->vp_mode == vertexshader)
1984 declare_in_varying(gl_info, buffer, FALSE, "vec4 %s_link[%u];\n", prefix, in_count);
1985 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1988 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1990 if (!(map & 1))
1991 continue;
1993 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1995 if (reg_maps->luminanceparams & (1u << i))
1997 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1998 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1999 extra_constants_needed++;
2002 extra_constants_needed++;
2005 if (ps_args->srgb_correction)
2007 shader_addline(buffer, "const vec4 srgb_const0 = ");
2008 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
2009 shader_addline(buffer, ";\n");
2010 shader_addline(buffer, "const vec4 srgb_const1 = ");
2011 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
2012 shader_addline(buffer, ";\n");
2014 if (reg_maps->vpos || reg_maps->usesdsy)
2016 ++extra_constants_needed;
2017 shader_addline(buffer, "uniform vec4 ycorrection;\n");
2018 shader_addline(buffer, "vec4 vpos;\n");
2021 if (!needs_legacy_glsl_syntax(gl_info))
2022 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
2024 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
2025 FIXME("Insufficient uniforms to run this shader.\n");
2028 /* Declare output register temporaries */
2029 if (shader->limits->packed_output)
2030 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2032 /* Declare temporary variables */
2033 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2035 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
2038 /* Declare loop registers aLx */
2039 if (version->major < 4)
2041 for (i = 0; i < reg_maps->loop_depth; ++i)
2043 shader_addline(buffer, "int aL%u;\n", i);
2044 shader_addline(buffer, "int tmpInt%u;\n", i);
2048 /* Temporary variables for matrix operations */
2049 shader_addline(buffer, "vec4 tmp0;\n");
2050 shader_addline(buffer, "vec4 tmp1;\n");
2052 if (!shader->load_local_constsF)
2054 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2056 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2057 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2058 shader_addline(buffer, ";\n");
2062 /* Start the main program. */
2063 shader_addline(buffer, "void main()\n{\n");
2065 /* Direct3D applications expect integer vPos values, while OpenGL drivers
2066 * add approximately 0.5. This causes off-by-one problems as spotted by
2067 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
2068 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
2069 * causes precision troubles when we just subtract 0.5.
2071 * To deal with that, just floor() the position. This will eliminate the
2072 * fraction on all cards.
2074 * TODO: Test how this behaves with multisampling.
2076 * An advantage of floor is that it works even if the driver doesn't add
2077 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
2078 * to return in gl_FragCoord, even though coordinates specify the pixel
2079 * centers instead of the pixel corners. This code will behave correctly
2080 * on drivers that returns integer values. */
2081 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
2083 if (shader->device->wined3d->flags & WINED3D_PIXEL_CENTER_INTEGER)
2084 shader_addline(buffer,
2085 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
2086 else
2087 shader_addline(buffer,
2088 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
2092 /*****************************************************************************
2093 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2095 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2096 ****************************************************************************/
2098 /* Prototypes */
2099 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2100 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2102 /** Used for opcode modifiers - They multiply the result by the specified amount */
2103 static const char * const shift_glsl_tab[] = {
2104 "", /* 0 (none) */
2105 "2.0 * ", /* 1 (x2) */
2106 "4.0 * ", /* 2 (x4) */
2107 "8.0 * ", /* 3 (x8) */
2108 "16.0 * ", /* 4 (x16) */
2109 "32.0 * ", /* 5 (x32) */
2110 "", /* 6 (x64) */
2111 "", /* 7 (x128) */
2112 "", /* 8 (d256) */
2113 "", /* 9 (d128) */
2114 "", /* 10 (d64) */
2115 "", /* 11 (d32) */
2116 "0.0625 * ", /* 12 (d16) */
2117 "0.125 * ", /* 13 (d8) */
2118 "0.25 * ", /* 14 (d4) */
2119 "0.5 * " /* 15 (d2) */
2122 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2123 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2124 const char *in_reg, const char *in_regswizzle, char *out_str)
2126 switch (src_modifier)
2128 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2129 case WINED3DSPSM_DW:
2130 case WINED3DSPSM_NONE:
2131 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2132 break;
2133 case WINED3DSPSM_NEG:
2134 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2135 break;
2136 case WINED3DSPSM_NOT:
2137 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2138 break;
2139 case WINED3DSPSM_BIAS:
2140 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2141 break;
2142 case WINED3DSPSM_BIASNEG:
2143 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2144 break;
2145 case WINED3DSPSM_SIGN:
2146 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2147 break;
2148 case WINED3DSPSM_SIGNNEG:
2149 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2150 break;
2151 case WINED3DSPSM_COMP:
2152 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2153 break;
2154 case WINED3DSPSM_X2:
2155 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2156 break;
2157 case WINED3DSPSM_X2NEG:
2158 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2159 break;
2160 case WINED3DSPSM_ABS:
2161 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2162 break;
2163 case WINED3DSPSM_ABSNEG:
2164 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2165 break;
2166 default:
2167 FIXME("Unhandled modifier %u\n", src_modifier);
2168 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2172 /** Writes the GLSL variable name that corresponds to the register that the
2173 * DX opcode parameter is trying to access */
2174 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2175 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2177 /* oPos, oFog and oPts in D3D */
2178 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2180 const struct wined3d_shader *shader = ins->ctx->shader;
2181 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2182 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2183 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2184 const char *prefix = shader_glsl_get_prefix(version->type);
2185 struct glsl_src_param rel_param0, rel_param1;
2186 char imm_str[4][17];
2188 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2189 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2190 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2191 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2192 *is_color = FALSE;
2194 switch (reg->type)
2196 case WINED3DSPR_TEMP:
2197 sprintf(register_name, "R%u", reg->idx[0].offset);
2198 break;
2200 case WINED3DSPR_INPUT:
2201 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2203 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2205 if (reg->idx[0].rel_addr)
2206 FIXME("VS3+ input registers relative addressing.\n");
2207 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2208 *is_color = TRUE;
2209 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2210 break;
2213 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2215 if (reg->idx[0].rel_addr)
2217 if (reg->idx[1].rel_addr)
2218 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
2219 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2220 else
2221 sprintf(register_name, "gs_in[%s + %u][%u]",
2222 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
2224 else if (reg->idx[1].rel_addr)
2225 sprintf(register_name, "gs_in[%u][%s + %u]",
2226 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2227 else
2228 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
2229 break;
2232 /* pixel shaders >= 3.0 */
2233 if (version->major >= 3)
2235 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2236 unsigned int in_count = vec4_varyings(version->major, gl_info);
2238 if (reg->idx[0].rel_addr)
2240 /* Removing a + 0 would be an obvious optimization, but
2241 * OS X doesn't see the NOP operation there. */
2242 if (idx)
2244 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2245 && shader->u.ps.declared_in_count > in_count)
2247 sprintf(register_name,
2248 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2249 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2250 prefix, rel_param0.param_str, idx);
2252 else
2254 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2257 else
2259 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2260 && shader->u.ps.declared_in_count > in_count)
2262 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2263 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2264 prefix, rel_param0.param_str);
2266 else
2268 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2272 else
2274 if (idx == in_count) sprintf(register_name, "gl_Color");
2275 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2276 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2279 else
2281 if (!reg->idx[0].offset)
2282 strcpy(register_name, "ffp_varying_diffuse");
2283 else
2284 strcpy(register_name, "ffp_varying_specular");
2285 break;
2287 break;
2289 case WINED3DSPR_CONST:
2291 /* Relative addressing */
2292 if (reg->idx[0].rel_addr)
2294 if (wined3d_settings.check_float_constants)
2295 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2296 rel_param0.param_str, reg->idx[0].offset,
2297 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2298 prefix, rel_param0.param_str, reg->idx[0].offset);
2299 else if (reg->idx[0].offset)
2300 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2301 else
2302 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2304 else
2306 if (shader_constant_is_local(shader, reg->idx[0].offset))
2307 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2308 else
2309 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2312 break;
2314 case WINED3DSPR_CONSTINT:
2315 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2316 break;
2318 case WINED3DSPR_CONSTBOOL:
2319 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2320 break;
2322 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2323 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2324 sprintf(register_name, "T%u", reg->idx[0].offset);
2325 else
2326 sprintf(register_name, "A%u", reg->idx[0].offset);
2327 break;
2329 case WINED3DSPR_LOOP:
2330 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
2331 break;
2333 case WINED3DSPR_SAMPLER:
2334 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2335 break;
2337 case WINED3DSPR_COLOROUT:
2338 if (reg->idx[0].offset >= gl_info->limits.buffers)
2339 WARN("Write to render target %u, only %d supported.\n",
2340 reg->idx[0].offset, gl_info->limits.buffers);
2342 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2343 break;
2345 case WINED3DSPR_RASTOUT:
2346 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2347 break;
2349 case WINED3DSPR_DEPTHOUT:
2350 sprintf(register_name, "gl_FragDepth");
2351 break;
2353 case WINED3DSPR_ATTROUT:
2354 if (!reg->idx[0].offset)
2355 sprintf(register_name, "%s_out[8]", prefix);
2356 else
2357 sprintf(register_name, "%s_out[9]", prefix);
2358 break;
2360 case WINED3DSPR_TEXCRDOUT:
2361 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2362 if (reg->idx[0].rel_addr)
2363 FIXME("VS3 output registers relative addressing.\n");
2364 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2365 break;
2367 case WINED3DSPR_MISCTYPE:
2368 if (!reg->idx[0].offset)
2370 /* vPos */
2371 sprintf(register_name, "vpos");
2373 else if (reg->idx[0].offset == 1)
2375 /* Note that gl_FrontFacing is a bool, while vFace is
2376 * a float for which the sign determines front/back */
2377 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2379 else
2381 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2382 sprintf(register_name, "unrecognized_register");
2384 break;
2386 case WINED3DSPR_IMMCONST:
2387 switch (reg->immconst_type)
2389 case WINED3D_IMMCONST_SCALAR:
2390 switch (reg->data_type)
2392 case WINED3D_DATA_FLOAT:
2393 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
2394 break;
2395 case WINED3D_DATA_INT:
2396 sprintf(register_name, "%#x", reg->immconst_data[0]);
2397 break;
2398 case WINED3D_DATA_RESOURCE:
2399 case WINED3D_DATA_SAMPLER:
2400 case WINED3D_DATA_UINT:
2401 sprintf(register_name, "%#xu", reg->immconst_data[0]);
2402 break;
2403 default:
2404 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2405 break;
2407 break;
2409 case WINED3D_IMMCONST_VEC4:
2410 switch (reg->data_type)
2412 case WINED3D_DATA_FLOAT:
2413 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
2414 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
2415 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
2416 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
2417 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2418 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2419 break;
2420 case WINED3D_DATA_INT:
2421 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2422 reg->immconst_data[0], reg->immconst_data[1],
2423 reg->immconst_data[2], reg->immconst_data[3]);
2424 break;
2425 case WINED3D_DATA_RESOURCE:
2426 case WINED3D_DATA_SAMPLER:
2427 case WINED3D_DATA_UINT:
2428 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2429 reg->immconst_data[0], reg->immconst_data[1],
2430 reg->immconst_data[2], reg->immconst_data[3]);
2431 break;
2432 default:
2433 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2434 break;
2436 break;
2438 default:
2439 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2440 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2442 break;
2444 case WINED3DSPR_CONSTBUFFER:
2445 if (reg->idx[1].rel_addr)
2446 sprintf(register_name, "%s_cb%u[%s + %u]",
2447 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2448 else
2449 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2450 break;
2452 case WINED3DSPR_IMMCONSTBUFFER:
2453 if (reg->idx[0].rel_addr)
2454 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2455 else
2456 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2457 break;
2459 case WINED3DSPR_PRIMID:
2460 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2461 break;
2463 default:
2464 FIXME("Unhandled register type %#x.\n", reg->type);
2465 sprintf(register_name, "unrecognized_register");
2466 break;
2470 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2472 *str++ = '.';
2473 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2474 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2475 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2476 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2477 *str = '\0';
2480 /* Get the GLSL write mask for the destination register */
2481 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2483 DWORD mask = param->write_mask;
2485 if (shader_is_scalar(&param->reg))
2487 mask = WINED3DSP_WRITEMASK_0;
2488 *write_mask = '\0';
2490 else
2492 shader_glsl_write_mask_to_str(mask, write_mask);
2495 return mask;
2498 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
2499 unsigned int size = 0;
2501 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2502 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2503 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2504 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2506 return size;
2509 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2511 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2512 * but addressed as "rgba". To fix this we need to swap the register's x
2513 * and z components. */
2514 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2516 *str++ = '.';
2517 /* swizzle bits fields: wwzzyyxx */
2518 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
2519 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
2520 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
2521 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
2522 *str = '\0';
2525 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2526 BOOL fixup, DWORD mask, char *swizzle_str)
2528 if (shader_is_scalar(&param->reg))
2529 *swizzle_str = '\0';
2530 else
2531 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2534 /* From a given parameter token, generate the corresponding GLSL string.
2535 * Also, return the actual register name and swizzle in case the
2536 * caller needs this information as well. */
2537 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2538 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
2540 BOOL is_color = FALSE;
2541 char swizzle_str[6];
2543 glsl_src->reg_name[0] = '\0';
2544 glsl_src->param_str[0] = '\0';
2545 swizzle_str[0] = '\0';
2547 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
2548 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
2550 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
2552 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
2554 else
2556 char reg_name[200];
2558 switch (wined3d_src->reg.data_type)
2560 case WINED3D_DATA_FLOAT:
2561 sprintf(reg_name, "%s", glsl_src->reg_name);
2562 break;
2563 case WINED3D_DATA_INT:
2564 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
2565 break;
2566 case WINED3D_DATA_RESOURCE:
2567 case WINED3D_DATA_SAMPLER:
2568 case WINED3D_DATA_UINT:
2569 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
2570 break;
2571 default:
2572 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
2573 sprintf(reg_name, "%s", glsl_src->reg_name);
2574 break;
2577 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
2581 /* From a given parameter token, generate the corresponding GLSL string.
2582 * Also, return the actual register name and swizzle in case the
2583 * caller needs this information as well. */
2584 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
2585 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
2587 BOOL is_color = FALSE;
2589 glsl_dst->mask_str[0] = '\0';
2590 glsl_dst->reg_name[0] = '\0';
2592 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
2593 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
2596 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2597 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
2598 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
2599 enum wined3d_data_type data_type)
2601 struct glsl_dst_param glsl_dst;
2602 DWORD mask;
2604 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
2606 switch (data_type)
2608 case WINED3D_DATA_FLOAT:
2609 shader_addline(buffer, "%s%s = %s(",
2610 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2611 break;
2612 case WINED3D_DATA_INT:
2613 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
2614 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2615 break;
2616 case WINED3D_DATA_RESOURCE:
2617 case WINED3D_DATA_SAMPLER:
2618 case WINED3D_DATA_UINT:
2619 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
2620 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2621 break;
2622 default:
2623 FIXME("Unhandled data type %#x.\n", data_type);
2624 shader_addline(buffer, "%s%s = %s(",
2625 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2626 break;
2630 return mask;
2633 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2634 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
2636 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2639 /** Process GLSL instruction modifiers */
2640 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
2642 struct glsl_dst_param dst_param;
2643 DWORD modifiers;
2645 if (!ins->dst_count) return;
2647 modifiers = ins->dst[0].modifiers;
2648 if (!modifiers) return;
2650 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2652 if (modifiers & WINED3DSPDM_SATURATE)
2654 /* _SAT means to clamp the value of the register to between 0 and 1 */
2655 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
2656 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
2659 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
2661 FIXME("_centroid modifier not handled\n");
2664 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
2666 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2670 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
2672 switch (op)
2674 case WINED3D_SHADER_REL_OP_GT: return ">";
2675 case WINED3D_SHADER_REL_OP_EQ: return "==";
2676 case WINED3D_SHADER_REL_OP_GE: return ">=";
2677 case WINED3D_SHADER_REL_OP_LT: return "<";
2678 case WINED3D_SHADER_REL_OP_NE: return "!=";
2679 case WINED3D_SHADER_REL_OP_LE: return "<=";
2680 default:
2681 FIXME("Unrecognized operator %#x.\n", op);
2682 return "(\?\?)";
2686 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
2687 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
2689 static const struct
2691 unsigned int coord_size;
2692 unsigned int offset_size;
2693 const char *type_part;
2695 resource_types[] =
2697 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
2698 {1, 0, ""}, /* WINED3D_SHADER_RESOURCE_BUFFER */
2699 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
2700 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
2701 {2, 0, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
2702 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
2703 {3, 0, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
2704 {2, 1, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
2705 {3, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
2706 {3, 0, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
2708 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2709 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
2710 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2711 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
2712 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
2713 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
2714 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
2715 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2716 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
2717 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
2718 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
2719 const char *base = "texture", *type_part = "", *suffix = "";
2720 unsigned int coord_size;
2722 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
2724 if (resource_type >= ARRAY_SIZE(resource_types))
2726 ERR("Unexpected resource type %#x.\n", resource_type);
2727 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
2730 /* Note that there's no such thing as a projected cube texture. */
2731 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
2732 projected = FALSE;
2734 if (needs_legacy_glsl_syntax(gl_info))
2736 if (shadow)
2737 base = "shadow";
2739 type_part = resource_types[resource_type].type_part;
2740 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
2741 type_part = "2DRect";
2742 if (!type_part[0])
2743 FIXME("Unhandled resource type %#x.\n", resource_type);
2745 if (!lod && grad && !gl_info->supported[EXT_GPU_SHADER4])
2747 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2748 suffix = "ARB";
2749 else
2750 FIXME("Unsupported grad function.\n");
2754 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
2756 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
2757 if (flags & ~texel_fetch_flags)
2758 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
2760 base = "texelFetch";
2761 type_part = "";
2764 sample_function->offset_size = offset ? resource_types[resource_type].offset_size : 0;
2765 if (offset && !sample_function->offset_size)
2767 FIXME("Offset not supported for resource type %#x.\n", resource_type);
2768 offset = FALSE;
2771 sample_function->name = string_buffer_get(priv->string_buffers);
2772 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
2773 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
2775 coord_size = resource_types[resource_type].coord_size;
2776 if (shadow)
2777 ++coord_size;
2778 sample_function->coord_mask = (1u << coord_size) - 1;
2779 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
2782 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
2783 struct glsl_sample_function *sample_function)
2785 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2787 string_buffer_release(priv->string_buffers, sample_function->name);
2790 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2791 BOOL sign_fixup, enum fixup_channel_source channel_source)
2793 switch(channel_source)
2795 case CHANNEL_SOURCE_ZERO:
2796 strcat(arguments, "0.0");
2797 break;
2799 case CHANNEL_SOURCE_ONE:
2800 strcat(arguments, "1.0");
2801 break;
2803 case CHANNEL_SOURCE_X:
2804 strcat(arguments, reg_name);
2805 strcat(arguments, ".x");
2806 break;
2808 case CHANNEL_SOURCE_Y:
2809 strcat(arguments, reg_name);
2810 strcat(arguments, ".y");
2811 break;
2813 case CHANNEL_SOURCE_Z:
2814 strcat(arguments, reg_name);
2815 strcat(arguments, ".z");
2816 break;
2818 case CHANNEL_SOURCE_W:
2819 strcat(arguments, reg_name);
2820 strcat(arguments, ".w");
2821 break;
2823 default:
2824 FIXME("Unhandled channel source %#x\n", channel_source);
2825 strcat(arguments, "undefined");
2826 break;
2829 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2832 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
2833 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2835 unsigned int mask_size, remaining;
2836 DWORD fixup_mask = 0;
2837 char arguments[256];
2838 char mask_str[6];
2840 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2841 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2842 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2843 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2844 if (!(mask &= fixup_mask))
2845 return;
2847 if (is_complex_fixup(fixup))
2849 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2850 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2851 return;
2854 shader_glsl_write_mask_to_str(mask, mask_str);
2855 mask_size = shader_glsl_get_write_mask_size(mask);
2857 arguments[0] = '\0';
2858 remaining = mask_size;
2859 if (mask & WINED3DSP_WRITEMASK_0)
2861 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2862 if (--remaining) strcat(arguments, ", ");
2864 if (mask & WINED3DSP_WRITEMASK_1)
2866 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2867 if (--remaining) strcat(arguments, ", ");
2869 if (mask & WINED3DSP_WRITEMASK_2)
2871 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2872 if (--remaining) strcat(arguments, ", ");
2874 if (mask & WINED3DSP_WRITEMASK_3)
2876 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2877 if (--remaining) strcat(arguments, ", ");
2880 if (mask_size > 1)
2881 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2882 else
2883 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2886 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2888 char reg_name[256];
2889 BOOL is_color;
2891 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2892 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2895 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2896 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
2897 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
2898 const char *coord_reg_fmt, ...)
2900 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2901 char dst_swizzle[6];
2902 struct color_fixup_desc fixup;
2903 BOOL np2_fixup = FALSE;
2904 va_list args;
2905 int ret;
2907 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2909 /* If ARB_texture_swizzle is supported we don't need to do anything here.
2910 * We actually rely on it for vertex shaders and SM4+. */
2911 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
2913 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2914 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
2916 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
2917 np2_fixup = TRUE;
2919 else
2921 fixup = COLOR_FIXUP_IDENTITY;
2924 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2926 if (sample_function->output_single_component)
2927 shader_addline(ins->ctx->buffer, "vec4(");
2929 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2930 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
2932 for (;;)
2934 va_start(args, coord_reg_fmt);
2935 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2936 va_end(args);
2937 if (!ret)
2938 break;
2939 if (!string_buffer_resize(ins->ctx->buffer, ret))
2940 break;
2943 if (np2_fixup)
2945 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2946 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
2948 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
2950 case 1:
2951 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2952 idx >> 1, (idx % 2) ? "z" : "x");
2953 break;
2954 case 2:
2955 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2956 idx >> 1, (idx % 2) ? "zw" : "xy");
2957 break;
2958 case 3:
2959 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
2960 idx >> 1, (idx % 2) ? "zw" : "xy");
2961 break;
2962 case 4:
2963 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
2964 idx >> 1, (idx % 2) ? "zw" : "xy");
2965 break;
2968 if (dx && dy)
2969 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
2970 else if (bias)
2971 shader_addline(ins->ctx->buffer, ", %s", bias);
2972 if (sample_function->offset_size)
2974 int offset_immdata[4] = {offset->u, offset->v, offset->w};
2975 shader_addline(ins->ctx->buffer, ", ");
2976 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
2978 shader_addline(ins->ctx->buffer, ")");
2980 if (sample_function->output_single_component)
2981 shader_addline(ins->ctx->buffer, ")");
2983 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
2985 if (!is_identity_fixup(fixup))
2986 shader_glsl_color_correction(ins, fixup);
2989 /*****************************************************************************
2990 * Begin processing individual instruction opcodes
2991 ****************************************************************************/
2993 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2995 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2996 struct glsl_src_param src0_param;
2997 struct glsl_src_param src1_param;
2998 DWORD write_mask;
2999 const char *op;
3001 /* Determine the GLSL operator to use based on the opcode */
3002 switch (ins->handler_idx)
3004 case WINED3DSIH_ADD: op = "+"; break;
3005 case WINED3DSIH_AND: op = "&"; break;
3006 case WINED3DSIH_DIV: op = "/"; break;
3007 case WINED3DSIH_IADD: op = "+"; break;
3008 case WINED3DSIH_ISHL: op = "<<"; break;
3009 case WINED3DSIH_MUL: op = "*"; break;
3010 case WINED3DSIH_OR: op = "|"; break;
3011 case WINED3DSIH_SUB: op = "-"; break;
3012 case WINED3DSIH_USHR: op = ">>"; break;
3013 case WINED3DSIH_XOR: op = "^"; break;
3014 default:
3015 op = "<unhandled operator>";
3016 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3017 break;
3020 write_mask = shader_glsl_append_dst(buffer, ins);
3021 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3022 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3023 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3026 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3028 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3029 struct glsl_src_param src0_param;
3030 struct glsl_src_param src1_param;
3031 unsigned int mask_size;
3032 DWORD write_mask;
3033 const char *op;
3035 write_mask = shader_glsl_append_dst(buffer, ins);
3036 mask_size = shader_glsl_get_write_mask_size(write_mask);
3037 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3038 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3040 if (mask_size > 1)
3042 switch (ins->handler_idx)
3044 case WINED3DSIH_EQ: op = "equal"; break;
3045 case WINED3DSIH_IEQ: op = "equal"; break;
3046 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3047 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3048 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3049 case WINED3DSIH_LT: op = "lessThan"; break;
3050 case WINED3DSIH_ILT: op = "lessThan"; break;
3051 case WINED3DSIH_ULT: op = "lessThan"; break;
3052 case WINED3DSIH_NE: op = "notEqual"; break;
3053 case WINED3DSIH_INE: op = "notEqual"; break;
3054 default:
3055 op = "<unhandled operator>";
3056 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3057 break;
3060 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3061 mask_size, op, src0_param.param_str, src1_param.param_str);
3063 else
3065 switch (ins->handler_idx)
3067 case WINED3DSIH_EQ: op = "=="; break;
3068 case WINED3DSIH_IEQ: op = "=="; break;
3069 case WINED3DSIH_GE: op = ">="; break;
3070 case WINED3DSIH_IGE: op = ">="; break;
3071 case WINED3DSIH_UGE: op = ">="; break;
3072 case WINED3DSIH_LT: op = "<"; break;
3073 case WINED3DSIH_ILT: op = "<"; break;
3074 case WINED3DSIH_ULT: op = "<"; break;
3075 case WINED3DSIH_NE: op = "!="; break;
3076 case WINED3DSIH_INE: op = "!="; break;
3077 default:
3078 op = "<unhandled operator>";
3079 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3080 break;
3083 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3084 src0_param.param_str, op, src1_param.param_str);
3088 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3090 struct glsl_src_param src_param;
3091 DWORD write_mask;
3092 const char *op;
3094 switch (ins->handler_idx)
3096 case WINED3DSIH_INEG: op = "-"; break;
3097 case WINED3DSIH_NOT: op = "~"; break;
3098 default:
3099 op = "<unhandled operator>";
3100 ERR("Unhandled opcode %s.\n",
3101 debug_d3dshaderinstructionhandler(ins->handler_idx));
3102 break;
3105 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3106 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3107 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3110 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
3112 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3113 struct glsl_src_param src0_param;
3114 struct glsl_src_param src1_param;
3115 DWORD write_mask;
3117 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
3118 * not, we can emulate it. */
3119 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3120 FIXME("64-bit integer multiplies not implemented.\n");
3122 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3124 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3125 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3126 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3128 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3129 src0_param.param_str, src1_param.param_str);
3133 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3135 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3136 struct glsl_src_param src0_param, src1_param;
3137 DWORD write_mask;
3139 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3141 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3143 char dst_mask[6];
3145 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3146 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3147 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3148 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3149 dst_mask, src0_param.param_str, src1_param.param_str);
3151 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3152 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3153 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3154 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3156 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3157 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3159 else
3161 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3162 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3163 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3164 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3167 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3169 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3170 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3171 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3172 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3176 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3177 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3179 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3180 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3181 struct glsl_src_param src0_param;
3182 DWORD write_mask;
3184 write_mask = shader_glsl_append_dst(buffer, ins);
3185 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3187 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3188 * shader versions WINED3DSIO_MOVA is used for this. */
3189 if (ins->ctx->reg_maps->shader_version.major == 1
3190 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3191 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3193 /* This is a simple floor() */
3194 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3195 if (mask_size > 1) {
3196 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3197 } else {
3198 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3201 else if(ins->handler_idx == WINED3DSIH_MOVA)
3203 /* We need to *round* to the nearest int here. */
3204 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3206 if (gl_info->supported[EXT_GPU_SHADER4])
3208 if (mask_size > 1)
3209 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3210 else
3211 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3213 else
3215 if (mask_size > 1)
3216 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3217 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3218 else
3219 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3220 src0_param.param_str, src0_param.param_str);
3223 else
3225 shader_addline(buffer, "%s);\n", src0_param.param_str);
3229 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3230 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3232 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3233 struct glsl_src_param src0_param;
3234 struct glsl_src_param src1_param;
3235 DWORD dst_write_mask, src_write_mask;
3236 unsigned int dst_size;
3238 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3239 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3241 /* dp4 works on vec4, dp3 on vec3, etc. */
3242 if (ins->handler_idx == WINED3DSIH_DP4)
3243 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3244 else if (ins->handler_idx == WINED3DSIH_DP3)
3245 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3246 else
3247 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3249 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3250 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3252 if (dst_size > 1) {
3253 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3254 } else {
3255 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3259 /* Note that this instruction has some restrictions. The destination write mask
3260 * can't contain the w component, and the source swizzles have to be .xyzw */
3261 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3263 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3264 struct glsl_src_param src0_param;
3265 struct glsl_src_param src1_param;
3266 char dst_mask[6];
3268 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3269 shader_glsl_append_dst(ins->ctx->buffer, ins);
3270 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3271 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3272 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3275 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3277 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3280 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3281 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3282 * GLSL uses the value as-is. */
3283 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3285 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3286 struct glsl_src_param src0_param;
3287 struct glsl_src_param src1_param;
3288 DWORD dst_write_mask;
3289 unsigned int dst_size;
3291 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3292 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3294 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3295 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3297 if (dst_size > 1)
3299 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3300 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3302 else
3304 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3305 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3309 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3310 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3312 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3313 struct glsl_src_param src_param;
3314 const char *instruction;
3315 DWORD write_mask;
3316 unsigned i;
3318 /* Determine the GLSL function to use based on the opcode */
3319 /* TODO: Possibly make this a table for faster lookups */
3320 switch (ins->handler_idx)
3322 case WINED3DSIH_ABS: instruction = "abs"; break;
3323 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3324 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3325 case WINED3DSIH_FRC: instruction = "fract"; break;
3326 case WINED3DSIH_IMAX: instruction = "max"; break;
3327 case WINED3DSIH_IMIN: instruction = "min"; break;
3328 case WINED3DSIH_MAX: instruction = "max"; break;
3329 case WINED3DSIH_MIN: instruction = "min"; break;
3330 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3331 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
3332 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
3333 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3334 default: instruction = "";
3335 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3336 break;
3339 write_mask = shader_glsl_append_dst(buffer, ins);
3341 shader_addline(buffer, "%s(", instruction);
3343 if (ins->src_count)
3345 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3346 shader_addline(buffer, "%s", src_param.param_str);
3347 for (i = 1; i < ins->src_count; ++i)
3349 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3350 shader_addline(buffer, ", %s", src_param.param_str);
3354 shader_addline(buffer, "));\n");
3357 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3359 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3361 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3362 struct glsl_src_param src_param;
3363 unsigned int mask_size;
3364 DWORD write_mask;
3365 char dst_mask[6];
3367 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3368 mask_size = shader_glsl_get_write_mask_size(write_mask);
3369 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3371 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3372 src_param.param_str, src_param.param_str);
3373 shader_glsl_append_dst(buffer, ins);
3375 if (mask_size > 1)
3377 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3378 mask_size, src_param.param_str);
3380 else
3382 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3383 src_param.param_str);
3387 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3389 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3390 ins->ctx->reg_maps->shader_version.minor);
3391 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3392 struct glsl_src_param src0_param;
3393 const char *prefix, *suffix;
3394 unsigned int dst_size;
3395 DWORD dst_write_mask;
3397 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3398 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3400 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
3401 dst_write_mask = WINED3DSP_WRITEMASK_3;
3403 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
3405 switch (ins->handler_idx)
3407 case WINED3DSIH_EXP:
3408 case WINED3DSIH_EXPP:
3409 prefix = "exp2(";
3410 suffix = ")";
3411 break;
3413 case WINED3DSIH_LOG:
3414 case WINED3DSIH_LOGP:
3415 prefix = "log2(abs(";
3416 suffix = "))";
3417 break;
3419 case WINED3DSIH_RCP:
3420 prefix = "1.0 / ";
3421 suffix = "";
3422 break;
3424 case WINED3DSIH_RSQ:
3425 prefix = "inversesqrt(abs(";
3426 suffix = "))";
3427 break;
3429 default:
3430 prefix = "";
3431 suffix = "";
3432 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3433 break;
3436 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
3437 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3438 else
3439 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3442 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3443 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3444 * dst.x = 2^(floor(src))
3445 * dst.y = src - floor(src)
3446 * dst.z = 2^src (partial precision is allowed, but optional)
3447 * dst.w = 1.0;
3448 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3449 * dst = 2^src; (partial precision is allowed, but optional)
3451 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
3453 if (ins->ctx->reg_maps->shader_version.major < 2)
3455 struct glsl_src_param src_param;
3456 char dst_mask[6];
3458 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
3460 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
3461 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
3462 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
3463 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
3465 shader_glsl_append_dst(ins->ctx->buffer, ins);
3466 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3467 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
3468 return;
3471 shader_glsl_scalar_op(ins);
3474 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
3475 const char *vector_constructor, const char *scalar_constructor)
3477 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3478 struct glsl_src_param src_param;
3479 unsigned int mask_size;
3480 DWORD write_mask;
3482 write_mask = shader_glsl_append_dst(buffer, ins);
3483 mask_size = shader_glsl_get_write_mask_size(write_mask);
3484 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3486 if (mask_size > 1)
3487 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
3488 else
3489 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
3492 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
3494 shader_glsl_cast(ins, "ivec", "int");
3497 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
3499 shader_glsl_cast(ins, "uvec", "uint");
3502 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
3504 shader_glsl_cast(ins, "vec", "float");
3507 /** Process signed comparison opcodes in GLSL. */
3508 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
3510 struct glsl_src_param src0_param;
3511 struct glsl_src_param src1_param;
3512 DWORD write_mask;
3513 unsigned int mask_size;
3515 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3516 mask_size = shader_glsl_get_write_mask_size(write_mask);
3517 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3518 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3520 if (mask_size > 1) {
3521 const char *compare;
3523 switch(ins->handler_idx)
3525 case WINED3DSIH_SLT: compare = "lessThan"; break;
3526 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
3527 default: compare = "";
3528 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3531 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
3532 src0_param.param_str, src1_param.param_str);
3533 } else {
3534 switch(ins->handler_idx)
3536 case WINED3DSIH_SLT:
3537 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3538 * to return 0.0 but step returns 1.0 because step is not < x
3539 * An alternative is a bvec compare padded with an unused second component.
3540 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3541 * issue. Playing with not() is not possible either because not() does not accept
3542 * a scalar.
3544 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
3545 src0_param.param_str, src1_param.param_str);
3546 break;
3547 case WINED3DSIH_SGE:
3548 /* Here we can use the step() function and safe a conditional */
3549 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
3550 break;
3551 default:
3552 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3558 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
3560 const char *condition_prefix, *condition_suffix;
3561 struct wined3d_shader_dst_param dst;
3562 struct glsl_src_param src0_param;
3563 struct glsl_src_param src1_param;
3564 struct glsl_src_param src2_param;
3565 BOOL temp_destination = FALSE;
3566 DWORD cmp_channel = 0;
3567 unsigned int i, j;
3568 char mask_char[6];
3569 DWORD write_mask;
3571 switch (ins->handler_idx)
3573 case WINED3DSIH_CMP:
3574 condition_prefix = "";
3575 condition_suffix = " >= 0.0";
3576 break;
3578 case WINED3DSIH_CND:
3579 condition_prefix = "";
3580 condition_suffix = " > 0.5";
3581 break;
3583 case WINED3DSIH_MOVC:
3584 condition_prefix = "bool(";
3585 condition_suffix = ")";
3586 break;
3588 default:
3589 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3590 condition_prefix = "<unhandled prefix>";
3591 condition_suffix = "<unhandled suffix>";
3592 break;
3595 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
3597 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3598 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3599 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3600 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3602 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3603 condition_prefix, src0_param.param_str, condition_suffix,
3604 src1_param.param_str, src2_param.param_str);
3605 return;
3608 dst = ins->dst[0];
3610 /* Splitting the instruction up in multiple lines imposes a problem:
3611 * The first lines may overwrite source parameters of the following lines.
3612 * Deal with that by using a temporary destination register if needed. */
3613 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
3614 && ins->src[0].reg.type == dst.reg.type)
3615 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
3616 && ins->src[1].reg.type == dst.reg.type)
3617 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
3618 && ins->src[2].reg.type == dst.reg.type))
3619 temp_destination = TRUE;
3621 /* Cycle through all source0 channels. */
3622 for (i = 0; i < 4; ++i)
3624 write_mask = 0;
3625 /* Find the destination channels which use the current source0 channel. */
3626 for (j = 0; j < 4; ++j)
3628 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
3630 write_mask |= WINED3DSP_WRITEMASK_0 << j;
3631 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
3634 dst.write_mask = ins->dst[0].write_mask & write_mask;
3636 if (temp_destination)
3638 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
3639 continue;
3640 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
3642 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
3643 continue;
3645 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
3646 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3647 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3649 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3650 condition_prefix, src0_param.param_str, condition_suffix,
3651 src1_param.param_str, src2_param.param_str);
3654 if (temp_destination)
3656 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3657 shader_glsl_append_dst(ins->ctx->buffer, ins);
3658 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3662 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3663 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3664 * the compare is done per component of src0. */
3665 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3667 struct glsl_src_param src0_param;
3668 struct glsl_src_param src1_param;
3669 struct glsl_src_param src2_param;
3670 DWORD write_mask;
3671 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3672 ins->ctx->reg_maps->shader_version.minor);
3674 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3676 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3677 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3678 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3679 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3681 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
3682 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3683 else
3684 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3685 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3686 return;
3689 shader_glsl_conditional_move(ins);
3692 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3693 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3695 struct glsl_src_param src0_param;
3696 struct glsl_src_param src1_param;
3697 struct glsl_src_param src2_param;
3698 DWORD write_mask;
3700 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3701 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3702 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3703 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3704 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3705 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3708 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3709 Vertex shaders to GLSL codes */
3710 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3712 int i;
3713 int nComponents = 0;
3714 struct wined3d_shader_dst_param tmp_dst = {{0}};
3715 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3716 struct wined3d_shader_instruction tmp_ins;
3718 memset(&tmp_ins, 0, sizeof(tmp_ins));
3720 /* Set constants for the temporary argument */
3721 tmp_ins.ctx = ins->ctx;
3722 tmp_ins.dst_count = 1;
3723 tmp_ins.dst = &tmp_dst;
3724 tmp_ins.src_count = 2;
3725 tmp_ins.src = tmp_src;
3727 switch(ins->handler_idx)
3729 case WINED3DSIH_M4x4:
3730 nComponents = 4;
3731 tmp_ins.handler_idx = WINED3DSIH_DP4;
3732 break;
3733 case WINED3DSIH_M4x3:
3734 nComponents = 3;
3735 tmp_ins.handler_idx = WINED3DSIH_DP4;
3736 break;
3737 case WINED3DSIH_M3x4:
3738 nComponents = 4;
3739 tmp_ins.handler_idx = WINED3DSIH_DP3;
3740 break;
3741 case WINED3DSIH_M3x3:
3742 nComponents = 3;
3743 tmp_ins.handler_idx = WINED3DSIH_DP3;
3744 break;
3745 case WINED3DSIH_M3x2:
3746 nComponents = 2;
3747 tmp_ins.handler_idx = WINED3DSIH_DP3;
3748 break;
3749 default:
3750 break;
3753 tmp_dst = ins->dst[0];
3754 tmp_src[0] = ins->src[0];
3755 tmp_src[1] = ins->src[1];
3756 for (i = 0; i < nComponents; ++i)
3758 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3759 shader_glsl_dot(&tmp_ins);
3760 ++tmp_src[1].reg.idx[0].offset;
3765 The LRP instruction performs a component-wise linear interpolation
3766 between the second and third operands using the first operand as the
3767 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3768 This is equivalent to mix(src2, src1, src0);
3770 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3772 struct glsl_src_param src0_param;
3773 struct glsl_src_param src1_param;
3774 struct glsl_src_param src2_param;
3775 DWORD write_mask;
3777 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3779 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3780 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3781 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3783 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3784 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3787 /** Process the WINED3DSIO_LIT instruction in GLSL:
3788 * dst.x = dst.w = 1.0
3789 * dst.y = (src0.x > 0) ? src0.x
3790 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3791 * where src.w is clamped at +- 128
3793 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3795 struct glsl_src_param src0_param;
3796 struct glsl_src_param src1_param;
3797 struct glsl_src_param src3_param;
3798 char dst_mask[6];
3800 shader_glsl_append_dst(ins->ctx->buffer, ins);
3801 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3803 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3804 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3805 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3807 /* The sdk specifies the instruction like this
3808 * dst.x = 1.0;
3809 * if(src.x > 0.0) dst.y = src.x
3810 * else dst.y = 0.0.
3811 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3812 * else dst.z = 0.0;
3813 * dst.w = 1.0;
3814 * (where power = src.w clamped between -128 and 128)
3816 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3817 * dst.x = 1.0 ... No further explanation needed
3818 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3819 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3820 * dst.w = 1.0. ... Nothing fancy.
3822 * So we still have one conditional in there. So do this:
3823 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3825 * 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),
3826 * 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.
3827 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3829 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3830 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3831 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3833 shader_addline(ins->ctx->buffer,
3834 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3835 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3836 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3837 src0_param.param_str, src3_param.param_str, dst_mask);
3840 /** Process the WINED3DSIO_DST instruction in GLSL:
3841 * dst.x = 1.0
3842 * dst.y = src0.x * src0.y
3843 * dst.z = src0.z
3844 * dst.w = src1.w
3846 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3848 struct glsl_src_param src0y_param;
3849 struct glsl_src_param src0z_param;
3850 struct glsl_src_param src1y_param;
3851 struct glsl_src_param src1w_param;
3852 char dst_mask[6];
3854 shader_glsl_append_dst(ins->ctx->buffer, ins);
3855 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3857 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3858 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3859 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3860 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3862 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3863 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3866 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3867 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3868 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3870 * dst.x = cos(src0.?)
3871 * dst.y = sin(src0.?)
3872 * dst.z = dst.z
3873 * dst.w = dst.w
3875 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3877 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3878 struct glsl_src_param src0_param;
3879 DWORD write_mask;
3881 if (ins->ctx->reg_maps->shader_version.major < 4)
3883 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3885 write_mask = shader_glsl_append_dst(buffer, ins);
3886 switch (write_mask)
3888 case WINED3DSP_WRITEMASK_0:
3889 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3890 break;
3892 case WINED3DSP_WRITEMASK_1:
3893 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3894 break;
3896 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3897 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3898 src0_param.param_str, src0_param.param_str);
3899 break;
3901 default:
3902 ERR("Write mask should be .x, .y or .xy\n");
3903 break;
3906 return;
3909 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3912 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3914 char dst_mask[6];
3916 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3917 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3918 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3920 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3921 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3922 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3924 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3925 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3927 else
3929 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3930 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3931 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3934 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3936 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3937 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3938 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3942 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3943 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3944 * generate invalid code
3946 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3948 struct glsl_src_param src0_param;
3949 DWORD write_mask;
3951 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3952 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3954 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3957 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3958 * Start a for() loop where src1.y is the initial value of aL,
3959 * increment aL by src1.z for a total of src1.x iterations.
3960 * Need to use a temporary variable for this operation.
3962 /* FIXME: I don't think nested loops will work correctly this way. */
3963 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3965 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3966 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3967 const struct wined3d_shader *shader = ins->ctx->shader;
3968 const struct wined3d_shader_lconst *constant;
3969 struct glsl_src_param src1_param;
3970 const DWORD *control_values = NULL;
3972 if (ins->ctx->reg_maps->shader_version.major < 4)
3974 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3976 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3977 * class hardware doesn't support real varying indexing, but Microsoft
3978 * designed this feature for Shader model 2.x+. If the loop control is
3979 * known at compile time, the GLSL compiler can unroll the loop, and
3980 * replace indirect addressing with direct addressing. */
3981 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3983 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3985 if (constant->idx == ins->src[1].reg.idx[0].offset)
3987 control_values = constant->value;
3988 break;
3993 if (control_values)
3995 struct wined3d_shader_loop_control loop_control;
3996 loop_control.count = control_values[0];
3997 loop_control.start = control_values[1];
3998 loop_control.step = (int)control_values[2];
4000 if (loop_control.step > 0)
4002 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4003 loop_state->current_depth, loop_control.start,
4004 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
4005 loop_state->current_depth, loop_control.step);
4007 else if (loop_control.step < 0)
4009 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4010 loop_state->current_depth, loop_control.start,
4011 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
4012 loop_state->current_depth, loop_control.step);
4014 else
4016 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4017 loop_state->current_depth, loop_control.start, loop_state->current_depth,
4018 loop_state->current_depth, loop_control.count,
4019 loop_state->current_depth);
4022 else
4024 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4025 loop_state->current_depth, loop_state->current_reg,
4026 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
4027 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
4030 ++loop_state->current_reg;
4032 else
4034 shader_addline(buffer, "for (;;)\n{\n");
4037 ++loop_state->current_depth;
4040 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4042 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4044 shader_addline(ins->ctx->buffer, "}\n");
4046 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4048 --loop_state->current_depth;
4049 --loop_state->current_reg;
4052 if (ins->handler_idx == WINED3DSIH_ENDREP)
4054 --loop_state->current_depth;
4058 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4060 const struct wined3d_shader *shader = ins->ctx->shader;
4061 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4062 const struct wined3d_shader_lconst *constant;
4063 struct glsl_src_param src0_param;
4064 const DWORD *control_values = NULL;
4066 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4067 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4069 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4071 if (constant->idx == ins->src[0].reg.idx[0].offset)
4073 control_values = constant->value;
4074 break;
4079 if (control_values)
4081 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4082 loop_state->current_depth, loop_state->current_depth,
4083 control_values[0], loop_state->current_depth);
4085 else
4087 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4088 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4089 loop_state->current_depth, loop_state->current_depth,
4090 src0_param.param_str, loop_state->current_depth);
4093 ++loop_state->current_depth;
4096 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
4098 struct glsl_src_param src0_param;
4100 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4101 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
4104 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
4106 struct glsl_src_param src0_param;
4107 struct glsl_src_param src1_param;
4109 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4110 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4112 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
4113 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4116 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
4118 shader_addline(ins->ctx->buffer, "} else {\n");
4121 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
4123 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
4126 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
4128 shader_addline(ins->ctx->buffer, "break;\n");
4131 /* FIXME: According to MSDN the compare is done per component. */
4132 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
4134 struct glsl_src_param src0_param;
4135 struct glsl_src_param src1_param;
4137 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4138 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4140 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
4141 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4144 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
4146 struct glsl_src_param src_param;
4148 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4149 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
4152 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
4154 shader_addline(ins->ctx->buffer, "}\n");
4155 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
4158 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
4160 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
4163 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
4165 struct glsl_src_param src1_param;
4167 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4168 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
4169 src1_param.param_str, ins->src[0].reg.idx[0].offset);
4172 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
4174 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
4175 * function only suppresses the unhandled instruction warning
4179 /*********************************************
4180 * Pixel Shader Specific Code begins here
4181 ********************************************/
4182 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
4184 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4185 ins->ctx->reg_maps->shader_version.minor);
4186 struct glsl_sample_function sample_function;
4187 DWORD sample_flags = 0;
4188 DWORD resource_idx;
4189 DWORD mask = 0, swizzle;
4190 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4192 /* 1.0-1.4: Use destination register as sampler source.
4193 * 2.0+: Use provided sampler source. */
4194 if (shader_version < WINED3D_SHADER_VERSION(2,0))
4195 resource_idx = ins->dst[0].reg.idx[0].offset;
4196 else
4197 resource_idx = ins->src[1].reg.idx[0].offset;
4199 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4201 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4202 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4203 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4205 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4206 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4208 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4209 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4211 case WINED3D_TTFF_COUNT1:
4212 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4213 break;
4214 case WINED3D_TTFF_COUNT2:
4215 mask = WINED3DSP_WRITEMASK_1;
4216 break;
4217 case WINED3D_TTFF_COUNT3:
4218 mask = WINED3DSP_WRITEMASK_2;
4219 break;
4220 case WINED3D_TTFF_COUNT4:
4221 case WINED3D_TTFF_DISABLE:
4222 mask = WINED3DSP_WRITEMASK_3;
4223 break;
4227 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4229 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4231 if (src_mod == WINED3DSPSM_DZ) {
4232 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4233 mask = WINED3DSP_WRITEMASK_2;
4234 } else if (src_mod == WINED3DSPSM_DW) {
4235 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4236 mask = WINED3DSP_WRITEMASK_3;
4239 else
4241 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4242 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4244 /* ps 2.0 texldp instruction always divides by the fourth component. */
4245 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4246 mask = WINED3DSP_WRITEMASK_3;
4250 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
4251 mask |= sample_function.coord_mask;
4252 sample_function.coord_mask = mask;
4254 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4255 else swizzle = ins->src[1].swizzle;
4257 /* 1.0-1.3: Use destination register as coordinate source.
4258 1.4+: Use provided coordinate source register. */
4259 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4261 char coord_mask[6];
4262 shader_glsl_write_mask_to_str(mask, coord_mask);
4263 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4264 "T%u%s", resource_idx, coord_mask);
4266 else
4268 struct glsl_src_param coord_param;
4269 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4270 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4272 struct glsl_src_param bias;
4273 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4274 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4275 NULL, "%s", coord_param.param_str);
4276 } else {
4277 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4278 "%s", coord_param.param_str);
4281 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4284 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4286 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4287 struct glsl_src_param coord_param, dx_param, dy_param;
4288 struct glsl_sample_function sample_function;
4289 DWORD sampler_idx;
4290 DWORD swizzle = ins->src[1].swizzle;
4292 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
4294 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4295 shader_glsl_tex(ins);
4296 return;
4299 sampler_idx = ins->src[1].reg.idx[0].offset;
4301 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
4302 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4303 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
4304 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
4306 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
4307 NULL, NULL, "%s", coord_param.param_str);
4308 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4311 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4313 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4314 struct glsl_src_param coord_param, lod_param;
4315 struct glsl_sample_function sample_function;
4316 DWORD sampler_idx;
4317 DWORD swizzle = ins->src[1].swizzle;
4319 sampler_idx = ins->src[1].reg.idx[0].offset;
4321 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
4322 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4324 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4326 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
4327 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
4329 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4330 * However, the NVIDIA drivers allow them in fragment shaders as well,
4331 * even without the appropriate extension. */
4332 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4334 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
4335 "%s", coord_param.param_str);
4336 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4339 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4340 unsigned int resource_idx, unsigned int sampler_idx)
4342 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4343 unsigned int i;
4345 for (i = 0; i < sampler_map->count; ++i)
4347 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4348 return entries[i].bind_idx;
4351 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4353 return ~0u;
4356 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
4358 static const unsigned int texture_size_component_count[] =
4360 0, /* WINED3D_SHADER_RESOURCE_NONE */
4361 1, /* WINED3D_SHADER_RESOURCE_BUFFER */
4362 1, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
4363 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
4364 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
4365 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
4366 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
4367 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
4368 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
4369 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
4372 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
4373 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4374 enum wined3d_shader_resource_type resource_type;
4375 unsigned int resource_idx, sampler_bind_idx, i;
4376 enum wined3d_data_type dst_data_type;
4377 struct glsl_src_param lod_param;
4378 char dst_swizzle[6];
4379 DWORD write_mask;
4381 dst_data_type = ins->dst[0].reg.data_type;
4382 if (ins->flags == WINED3DSI_RESINFO_UINT)
4383 dst_data_type = WINED3D_DATA_UINT;
4384 else if (ins->flags)
4385 FIXME("Unhandled flags %#x.\n", ins->flags);
4387 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
4388 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
4390 resource_idx = ins->src[1].reg.idx[0].offset;
4391 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4392 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
4393 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4394 resource_idx, WINED3D_SAMPLER_DEFAULT);
4396 if (resource_type >= ARRAY_SIZE(texture_size_component_count))
4398 ERR("Unexpected resource type %#x.\n", resource_type);
4399 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
4402 if (dst_data_type == WINED3D_DATA_UINT)
4403 shader_addline(ins->ctx->buffer, "uvec4(");
4404 else
4405 shader_addline(ins->ctx->buffer, "vec4(");
4407 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
4408 shader_glsl_get_prefix(version->type), sampler_bind_idx, lod_param.param_str);
4410 for (i = 0; i < 3 - texture_size_component_count[resource_type]; ++i)
4411 shader_addline(ins->ctx->buffer, "0, ");
4413 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
4415 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
4416 shader_glsl_get_prefix(version->type), sampler_bind_idx);
4418 else
4420 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
4421 shader_addline(ins->ctx->buffer, "1");
4424 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
4427 /* FIXME: The current implementation does not handle multisample textures correctly. */
4428 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
4430 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4431 struct glsl_src_param coord_param, lod_param;
4432 struct glsl_sample_function sample_function;
4433 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
4435 if (wined3d_shader_instruction_has_texel_offset(ins))
4436 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4438 resource_idx = ins->src[1].reg.idx[0].offset;
4439 sampler_idx = WINED3D_SAMPLER_DEFAULT;
4441 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4442 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4443 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4444 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4445 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
4446 NULL, NULL, lod_param.param_str, &ins->texel_offset, "%s", coord_param.param_str);
4447 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4450 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
4452 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
4453 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
4454 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4455 struct glsl_sample_function sample_function;
4456 DWORD flags = 0;
4458 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
4459 flags |= WINED3D_GLSL_SAMPLE_GRAD;
4460 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
4461 flags |= WINED3D_GLSL_SAMPLE_LOD;
4462 if (wined3d_shader_instruction_has_texel_offset(ins))
4463 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4465 resource_idx = ins->src[1].reg.idx[0].offset;
4466 sampler_idx = ins->src[2].reg.idx[0].offset;
4468 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4469 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4471 switch (ins->handler_idx)
4473 case WINED3DSIH_SAMPLE:
4474 break;
4475 case WINED3DSIH_SAMPLE_B:
4476 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4477 lod_param_str = lod_param.param_str;
4478 break;
4479 case WINED3DSIH_SAMPLE_GRAD:
4480 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dx_param);
4481 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.coord_mask, &dy_param);
4482 dx_param_str = dx_param.param_str;
4483 dy_param_str = dy_param.param_str;
4484 break;
4485 case WINED3DSIH_SAMPLE_LOD:
4486 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4487 lod_param_str = lod_param.param_str;
4488 break;
4489 default:
4490 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4491 break;
4494 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4495 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
4496 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
4497 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4500 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
4502 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4503 struct glsl_src_param coord_param, compare_param;
4504 struct glsl_sample_function sample_function;
4505 const char *lod_param = NULL;
4506 DWORD flags = 0;
4507 UINT coord_size;
4509 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
4511 lod_param = "0";
4512 flags |= WINED3D_GLSL_SAMPLE_LOD;
4515 if (wined3d_shader_instruction_has_texel_offset(ins))
4516 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4518 resource_idx = ins->src[1].reg.idx[0].offset;
4519 sampler_idx = ins->src[2].reg.idx[0].offset;
4521 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4522 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4523 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
4524 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
4525 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4526 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
4527 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
4528 coord_size, coord_param.param_str, compare_param.param_str);
4529 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4532 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
4534 /* FIXME: Make this work for more than just 2D textures */
4535 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4536 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4538 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
4540 char dst_mask[6];
4542 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4543 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
4544 ins->dst[0].reg.idx[0].offset, dst_mask);
4546 else
4548 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4549 DWORD reg = ins->src[0].reg.idx[0].offset;
4550 char dst_swizzle[6];
4552 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
4554 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
4556 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4557 struct glsl_src_param div_param;
4558 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
4560 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
4562 if (mask_size > 1)
4563 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4564 else
4565 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4567 else
4569 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
4574 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4575 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4576 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4577 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
4579 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4580 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4581 struct glsl_sample_function sample_function;
4582 struct glsl_src_param src0_param;
4583 UINT mask_size;
4585 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4587 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4588 * scalar, and projected sampling would require 4.
4590 * It is a dependent read - not valid with conditional NP2 textures
4592 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4593 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4595 switch(mask_size)
4597 case 1:
4598 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4599 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
4600 break;
4602 case 2:
4603 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4604 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
4605 break;
4607 case 3:
4608 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4609 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
4610 break;
4612 default:
4613 FIXME("Unexpected mask size %u\n", mask_size);
4614 break;
4616 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4619 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4620 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4621 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
4623 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4624 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4625 struct glsl_src_param src0_param;
4626 DWORD dst_mask;
4627 unsigned int mask_size;
4629 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4630 mask_size = shader_glsl_get_write_mask_size(dst_mask);
4631 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4633 if (mask_size > 1) {
4634 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
4635 } else {
4636 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
4640 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4641 * Calculate the depth as dst.x / dst.y */
4642 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
4644 struct glsl_dst_param dst_param;
4646 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4648 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4649 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4650 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4651 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4652 * >= 1.0 or < 0.0
4654 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4655 dst_param.reg_name, dst_param.reg_name);
4658 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4659 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4660 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4661 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4663 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
4665 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4666 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4667 struct glsl_src_param src0_param;
4669 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4671 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
4672 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4675 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4676 * Calculate the 1st of a 2-row matrix multiplication. */
4677 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
4679 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4680 DWORD reg = ins->dst[0].reg.idx[0].offset;
4681 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4682 struct glsl_src_param src0_param;
4684 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4685 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4688 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4689 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4690 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
4692 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4693 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4694 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4695 DWORD reg = ins->dst[0].reg.idx[0].offset;
4696 struct glsl_src_param src0_param;
4698 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4699 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
4700 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
4703 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
4705 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4706 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4707 struct glsl_sample_function sample_function;
4708 DWORD reg = ins->dst[0].reg.idx[0].offset;
4709 struct glsl_src_param src0_param;
4711 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4712 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4714 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4716 /* Sample the texture using the calculated coordinates */
4717 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
4718 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4721 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
4722 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
4723 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
4725 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4726 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4727 struct glsl_sample_function sample_function;
4728 DWORD reg = ins->dst[0].reg.idx[0].offset;
4729 struct glsl_src_param src0_param;
4731 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4732 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4734 /* Dependent read, not valid with conditional NP2 */
4735 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4737 /* Sample the texture using the calculated coordinates */
4738 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
4739 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4741 tex_mx->current_row = 0;
4744 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4745 * Perform the 3rd row of a 3x3 matrix multiply */
4746 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
4748 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4749 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4750 DWORD reg = ins->dst[0].reg.idx[0].offset;
4751 struct glsl_src_param src0_param;
4752 char dst_mask[6];
4754 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4756 shader_glsl_append_dst(ins->ctx->buffer, ins);
4757 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4758 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
4760 tex_mx->current_row = 0;
4763 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4764 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4765 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
4767 struct glsl_src_param src0_param;
4768 struct glsl_src_param src1_param;
4769 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4770 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4771 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4772 struct glsl_sample_function sample_function;
4773 DWORD reg = ins->dst[0].reg.idx[0].offset;
4774 char coord_mask[6];
4776 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4777 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4779 /* Perform the last matrix multiply operation */
4780 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4781 /* Reflection calculation */
4782 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
4784 /* Dependent read, not valid with conditional NP2 */
4785 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4786 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4788 /* Sample the texture */
4789 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4790 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
4791 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4793 tex_mx->current_row = 0;
4796 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4797 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4798 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
4800 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4801 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4802 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4803 struct glsl_sample_function sample_function;
4804 DWORD reg = ins->dst[0].reg.idx[0].offset;
4805 struct glsl_src_param src0_param;
4806 char coord_mask[6];
4808 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4810 /* Perform the last matrix multiply operation */
4811 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
4813 /* Construct the eye-ray vector from w coordinates */
4814 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
4815 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
4816 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4818 /* Dependent read, not valid with conditional NP2 */
4819 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4820 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4822 /* Sample the texture using the calculated coordinates */
4823 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4824 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
4825 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4827 tex_mx->current_row = 0;
4830 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4831 * Apply a fake bump map transform.
4832 * texbem is pshader <= 1.3 only, this saves a few version checks
4834 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
4836 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4837 struct glsl_sample_function sample_function;
4838 struct glsl_src_param coord_param;
4839 DWORD sampler_idx;
4840 DWORD mask;
4841 DWORD flags;
4842 char coord_mask[6];
4844 sampler_idx = ins->dst[0].reg.idx[0].offset;
4845 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4846 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4848 /* Dependent read, not valid with conditional NP2 */
4849 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4850 mask = sample_function.coord_mask;
4852 shader_glsl_write_mask_to_str(mask, coord_mask);
4854 /* With projected textures, texbem only divides the static texture coord,
4855 * not the displacement, so we can't let GL handle this. */
4856 if (flags & WINED3D_PSARGS_PROJECTED)
4858 DWORD div_mask=0;
4859 char coord_div_mask[3];
4860 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4862 case WINED3D_TTFF_COUNT1:
4863 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4864 break;
4865 case WINED3D_TTFF_COUNT2:
4866 div_mask = WINED3DSP_WRITEMASK_1;
4867 break;
4868 case WINED3D_TTFF_COUNT3:
4869 div_mask = WINED3DSP_WRITEMASK_2;
4870 break;
4871 case WINED3D_TTFF_COUNT4:
4872 case WINED3D_TTFF_DISABLE:
4873 div_mask = WINED3DSP_WRITEMASK_3;
4874 break;
4876 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4877 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4880 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4882 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4883 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4884 coord_param.param_str, coord_mask);
4886 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4888 struct glsl_src_param luminance_param;
4889 struct glsl_dst_param dst_param;
4891 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4892 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4894 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4895 dst_param.reg_name, dst_param.mask_str,
4896 luminance_param.param_str, sampler_idx, sampler_idx);
4898 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4901 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4903 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4904 struct glsl_src_param src0_param, src1_param;
4906 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4907 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4909 shader_glsl_append_dst(ins->ctx->buffer, ins);
4910 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4911 src0_param.param_str, sampler_idx, src1_param.param_str);
4914 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4915 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4916 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4918 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4919 struct glsl_sample_function sample_function;
4920 struct glsl_src_param src0_param;
4922 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4924 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4925 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4926 "%s.wx", src0_param.reg_name);
4927 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4930 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4931 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4932 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4934 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4935 struct glsl_sample_function sample_function;
4936 struct glsl_src_param src0_param;
4938 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4940 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4941 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4942 "%s.yz", src0_param.reg_name);
4943 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4946 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4947 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4948 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4950 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4951 struct glsl_sample_function sample_function;
4952 struct glsl_src_param src0_param;
4954 /* Dependent read, not valid with conditional NP2 */
4955 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4956 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4958 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4959 "%s", src0_param.param_str);
4960 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4963 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4964 * If any of the first 3 components are < 0, discard this pixel */
4965 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4967 if (ins->ctx->reg_maps->shader_version.major >= 4)
4969 struct glsl_src_param src_param;
4971 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4972 shader_addline(ins->ctx->buffer, "if (bool(floatBitsToUint(%s))) discard;\n", src_param.param_str);
4974 else
4976 struct glsl_dst_param dst_param;
4978 /* The argument is a destination parameter, and no writemasks are allowed */
4979 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4981 /* 2.0 shaders compare all 4 components in texkill. */
4982 if (ins->ctx->reg_maps->shader_version.major >= 2)
4983 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4984 /* 1.x shaders only compare the first 3 components, probably due to
4985 * the nature of the texkill instruction as a tex* instruction, and
4986 * phase, which kills all .w components. Even if all 4 components are
4987 * defined, only the first 3 are used. */
4988 else
4989 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4993 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4994 * dst = dot2(src0, src1) + src2 */
4995 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4997 struct glsl_src_param src0_param;
4998 struct glsl_src_param src1_param;
4999 struct glsl_src_param src2_param;
5000 DWORD write_mask;
5001 unsigned int mask_size;
5003 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5004 mask_size = shader_glsl_get_write_mask_size(write_mask);
5006 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
5007 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
5008 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
5010 if (mask_size > 1) {
5011 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
5012 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
5013 } else {
5014 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
5015 src0_param.param_str, src1_param.param_str, src2_param.param_str);
5019 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
5020 const struct wined3d_shader_signature *input_signature,
5021 const struct wined3d_shader_reg_maps *reg_maps,
5022 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
5024 unsigned int i;
5026 for (i = 0; i < input_signature->element_count; ++i)
5028 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5029 const char *semantic_name;
5030 UINT semantic_idx;
5031 char reg_mask[6];
5033 /* Unused */
5034 if (!(reg_maps->input_registers & (1u << input->register_idx)))
5035 continue;
5037 semantic_name = input->semantic_name;
5038 semantic_idx = input->semantic_idx;
5039 shader_glsl_write_mask_to_str(input->mask, reg_mask);
5041 if (args->vp_mode == vertexshader)
5043 if (input->sysval_semantic == WINED3D_SV_POSITION)
5044 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
5045 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5046 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5047 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
5048 else
5049 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
5050 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5051 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
5053 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5055 if (args->pointsprite)
5056 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
5057 shader->u.ps.input_reg_map[input->register_idx]);
5058 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
5059 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
5060 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5061 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
5062 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
5063 else
5064 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5065 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5067 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
5069 if (!semantic_idx)
5070 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
5071 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5072 else if (semantic_idx == 1)
5073 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
5074 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5075 else
5076 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5077 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5079 else
5081 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5082 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5087 /*********************************************
5088 * Vertex Shader Specific Code begins here
5089 ********************************************/
5091 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
5093 struct glsl_program_key key;
5095 key.vs_id = entry->vs.id;
5096 key.gs_id = entry->gs.id;
5097 key.ps_id = entry->ps.id;
5099 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
5101 ERR("Failed to insert program entry.\n");
5105 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
5106 GLuint vs_id, GLuint gs_id, GLuint ps_id)
5108 struct wine_rb_entry *entry;
5109 struct glsl_program_key key;
5111 key.vs_id = vs_id;
5112 key.gs_id = gs_id;
5113 key.ps_id = ps_id;
5115 entry = wine_rb_get(&priv->program_lookup, &key);
5116 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
5119 /* Context activation is done by the caller. */
5120 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
5121 struct glsl_shader_prog_link *entry)
5123 struct glsl_program_key key;
5125 key.vs_id = entry->vs.id;
5126 key.gs_id = entry->gs.id;
5127 key.ps_id = entry->ps.id;
5128 wine_rb_remove(&priv->program_lookup, &key);
5130 GL_EXTCALL(glDeleteProgram(entry->id));
5131 if (entry->vs.id)
5132 list_remove(&entry->vs.shader_entry);
5133 if (entry->gs.id)
5134 list_remove(&entry->gs.shader_entry);
5135 if (entry->ps.id)
5136 list_remove(&entry->ps.shader_entry);
5137 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
5138 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
5139 HeapFree(GetProcessHeap(), 0, entry);
5142 static void handle_ps3_input(struct shader_glsl_priv *priv,
5143 const struct wined3d_gl_info *gl_info, const DWORD *map,
5144 const struct wined3d_shader_signature *input_signature,
5145 const struct wined3d_shader_reg_maps *reg_maps_in,
5146 const struct wined3d_shader_signature *output_signature,
5147 const struct wined3d_shader_reg_maps *reg_maps_out)
5149 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5150 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5151 unsigned int i, j;
5152 DWORD *set;
5153 DWORD in_idx;
5154 unsigned int in_count = vec4_varyings(3, gl_info);
5155 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
5156 char reg_mask[6];
5157 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5159 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * max_varyings);
5161 for (i = 0; i < input_signature->element_count; ++i)
5163 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5165 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
5166 continue;
5168 in_idx = map[input->register_idx];
5169 /* Declared, but not read register */
5170 if (in_idx == ~0u)
5171 continue;
5172 if (in_idx >= max_varyings)
5174 FIXME("More input varyings declared than supported, expect issues.\n");
5175 continue;
5178 if (in_idx == in_count)
5179 string_buffer_sprintf(destination, "gl_FrontColor");
5180 else if (in_idx == in_count + 1)
5181 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5182 else
5183 string_buffer_sprintf(destination, "ps_link[%u]", in_idx);
5185 if (!set[in_idx])
5186 set[in_idx] = ~0u;
5188 for (j = 0; j < output_signature->element_count; ++j)
5190 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
5191 DWORD mask;
5193 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
5194 || input->semantic_idx != output->semantic_idx
5195 || strcmp(input->semantic_name, output->semantic_name)
5196 || !(mask = input->mask & output->mask))
5197 continue;
5199 if (set[in_idx] == ~0u)
5200 set[in_idx] = 0;
5201 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
5202 shader_glsl_write_mask_to_str(mask, reg_mask);
5204 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
5205 destination->buffer, reg_mask, output->register_idx, reg_mask);
5209 for (i = 0; i < max_varyings; ++i)
5211 unsigned int size;
5213 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
5214 continue;
5216 if (set[i] == ~0U) set[i] = 0;
5218 size = 0;
5219 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
5220 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
5221 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
5222 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
5223 reg_mask[size] = '\0';
5225 if (i == in_count)
5226 string_buffer_sprintf(destination, "gl_FrontColor");
5227 else if (i == in_count + 1)
5228 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5229 else
5230 string_buffer_sprintf(destination, "ps_link[%u]", i);
5232 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
5233 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
5236 HeapFree(GetProcessHeap(), 0, set);
5237 string_buffer_release(&priv->string_buffers, destination);
5240 /* Context activation is done by the caller. */
5241 static GLuint generate_param_reorder_function(struct shader_glsl_priv *priv,
5242 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
5243 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
5245 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5246 GLuint ret;
5247 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
5248 unsigned int i;
5249 const char *semantic_name;
5250 UINT semantic_idx;
5251 char reg_mask[6];
5252 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5254 string_buffer_clear(buffer);
5256 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &vs->reg_maps.shader_version));
5258 if (per_vertex_point_size)
5260 shader_addline(buffer, "uniform struct\n{\n");
5261 shader_addline(buffer, " float size_min;\n");
5262 shader_addline(buffer, " float size_max;\n");
5263 shader_addline(buffer, "} ffp_point;\n");
5266 if (ps_major < 3)
5268 DWORD colors_written_mask[2] = {0};
5269 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
5271 if (!legacy_context)
5273 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
5274 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
5275 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
5276 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
5279 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
5281 for (i = 0; i < vs->output_signature.element_count; ++i)
5283 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5284 DWORD write_mask;
5286 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5287 continue;
5289 semantic_name = output->semantic_name;
5290 semantic_idx = output->semantic_idx;
5291 write_mask = output->mask;
5292 shader_glsl_write_mask_to_str(write_mask, reg_mask);
5294 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
5296 if (legacy_context)
5297 shader_addline(buffer, "gl_Front%sColor%s = vs_out[%u]%s;\n",
5298 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
5299 else
5300 shader_addline(buffer, "ffp_varying_%s%s = clamp(vs_out[%u]%s, 0.0, 1.0);\n",
5301 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
5303 colors_written_mask[semantic_idx] = write_mask;
5305 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5307 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
5308 reg_mask, output->register_idx, reg_mask);
5310 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5312 if (semantic_idx < MAX_TEXTURES)
5314 shader_addline(buffer, "%s[%u]%s = vs_out[%u]%s;\n",
5315 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
5316 semantic_idx, reg_mask, output->register_idx, reg_mask);
5317 texcoords_written_mask[semantic_idx] = write_mask;
5320 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5322 shader_addline(buffer, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
5323 output->register_idx, reg_mask[1]);
5325 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
5327 shader_addline(buffer, "%s = clamp(vs_out[%u].%c, 0.0, 1.0);\n",
5328 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
5329 output->register_idx, reg_mask[1]);
5333 for (i = 0; i < 2; ++i)
5335 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5337 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5338 if (!i)
5339 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
5340 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
5341 reg_mask, reg_mask);
5342 else
5343 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
5344 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
5345 reg_mask, reg_mask);
5348 for (i = 0; i < MAX_TEXTURES; ++i)
5350 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
5351 continue;
5353 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5355 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
5356 && !texcoords_written_mask[i])
5357 continue;
5359 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5360 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
5361 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
5365 else
5367 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
5369 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
5370 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
5372 /* First, sort out position and point size. Those are not passed to the pixel shader */
5373 for (i = 0; i < vs->output_signature.element_count; ++i)
5375 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5377 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5378 continue;
5380 semantic_name = output->semantic_name;
5381 semantic_idx = output->semantic_idx;
5382 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5384 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5386 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
5387 reg_mask, output->register_idx, reg_mask);
5389 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5391 shader_addline(buffer, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
5392 output->register_idx, reg_mask[1]);
5396 /* Then, fix the pixel shader input */
5397 handle_ps3_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
5398 &ps->reg_maps, &vs->output_signature, &vs->reg_maps);
5401 shader_addline(buffer, "}\n");
5403 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5404 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
5405 shader_glsl_compile(gl_info, ret, buffer->buffer);
5407 return ret;
5410 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
5411 const struct wined3d_gl_info *gl_info)
5413 const char *output = get_fragment_output(gl_info);
5415 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
5416 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
5417 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
5418 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
5419 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
5420 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
5423 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
5424 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
5426 const char *output = get_fragment_output(gl_info);
5428 switch (mode)
5430 case WINED3D_FFP_PS_FOG_OFF:
5431 return;
5433 case WINED3D_FFP_PS_FOG_LINEAR:
5434 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
5435 break;
5437 case WINED3D_FFP_PS_FOG_EXP:
5438 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
5439 break;
5441 case WINED3D_FFP_PS_FOG_EXP2:
5442 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
5443 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
5444 break;
5446 default:
5447 ERR("Invalid fog mode %#x.\n", mode);
5448 return;
5451 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
5452 output, output);
5455 /* Context activation is done by the caller. */
5456 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
5457 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5458 const struct wined3d_shader *shader,
5459 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
5461 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5462 const struct wined3d_gl_info *gl_info = context->gl_info;
5463 const DWORD *function = shader->function;
5464 struct shader_glsl_ctx_priv priv_ctx;
5465 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5467 /* Create the hw GLSL shader object and assign it as the shader->prgId */
5468 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5470 memset(&priv_ctx, 0, sizeof(priv_ctx));
5471 priv_ctx.cur_ps_args = args;
5472 priv_ctx.cur_np2fixup_info = np2fixup_info;
5473 priv_ctx.string_buffers = string_buffers;
5475 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5477 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5478 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5479 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5480 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
5481 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5482 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5483 /* The spec says that it doesn't have to be explicitly enabled, but the
5484 * nvidia drivers write a warning if we don't do so. */
5485 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5486 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5487 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5488 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5489 if (gl_info->supported[EXT_GPU_SHADER4])
5490 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5492 /* Base Declarations */
5493 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5495 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
5497 unsigned int i;
5498 WORD map = reg_maps->texcoord;
5500 if (legacy_context)
5502 if (glsl_is_color_reg_read(shader, 0))
5503 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
5504 if (glsl_is_color_reg_read(shader, 1))
5505 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
5508 for (i = 0; map; map >>= 1, ++i)
5510 if (map & 1)
5512 if (args->pointsprite)
5513 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
5514 else if (args->texcoords_initialized & (1u << i))
5515 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
5516 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
5517 else
5518 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
5519 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
5523 if (legacy_context)
5524 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
5527 /* Pack 3.0 inputs */
5528 if (reg_maps->shader_version.major >= 3)
5529 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
5531 /* Base Shader Body */
5532 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5534 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
5535 if (reg_maps->shader_version.major < 2)
5536 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
5538 if (args->srgb_correction)
5539 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
5541 /* SM < 3 does not replace the fog stage. */
5542 if (reg_maps->shader_version.major < 3)
5543 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
5545 shader_addline(buffer, "}\n");
5547 TRACE("Compiling shader object %u.\n", shader_id);
5548 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5550 return shader_id;
5553 /* Context activation is done by the caller. */
5554 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
5555 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5556 const struct wined3d_shader *shader,
5557 const struct vs_compile_args *args)
5559 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5560 const struct wined3d_gl_info *gl_info = context->gl_info;
5561 const DWORD *function = shader->function;
5562 struct shader_glsl_ctx_priv priv_ctx;
5563 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5565 /* Create the hw GLSL shader program and assign it as the shader->prgId */
5566 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5568 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5570 if (gl_info->supported[ARB_DRAW_INSTANCED])
5571 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
5572 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5573 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5574 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5575 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5576 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5577 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5578 if (gl_info->supported[EXT_GPU_SHADER4])
5579 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5581 memset(&priv_ctx, 0, sizeof(priv_ctx));
5582 priv_ctx.cur_vs_args = args;
5583 priv_ctx.string_buffers = string_buffers;
5585 /* Base Declarations */
5586 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5588 /* Base Shader Body */
5589 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5591 /* Unpack outputs */
5592 shader_addline(buffer, "order_ps_input(vs_out);\n");
5594 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
5595 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
5596 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
5597 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
5599 if (reg_maps->shader_version.major < 3)
5601 if (args->fog_src == VS_FOG_Z)
5602 shader_addline(buffer, "%s = gl_Position.z;\n",
5603 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5604 else if (!reg_maps->fog)
5605 shader_addline(buffer, "%s = 0.0;\n",
5606 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5609 /* We always store the clipplanes without y inversion */
5610 if (args->clip_enabled)
5611 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
5613 if (args->point_size && !args->per_vertex_point_size)
5614 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
5616 /* Write the final position.
5618 * OpenGL coordinates specify the center of the pixel while d3d coords specify
5619 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
5620 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
5621 * contains 1.0 to allow a mad.
5623 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
5624 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
5626 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
5628 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
5629 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
5630 * which is the same as z = z * 2 - w.
5632 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
5634 shader_addline(buffer, "}\n");
5636 TRACE("Compiling shader object %u.\n", shader_id);
5637 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5639 return shader_id;
5642 /* Context activation is done by the caller. */
5643 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
5644 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5645 const struct wined3d_shader *shader)
5647 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5648 const struct wined3d_gl_info *gl_info = context->gl_info;
5649 const DWORD *function = shader->function;
5650 struct shader_glsl_ctx_priv priv_ctx;
5651 GLuint shader_id;
5653 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
5655 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5657 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
5658 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
5659 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5660 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5661 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5662 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5663 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5664 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5665 if (gl_info->supported[EXT_GPU_SHADER4])
5666 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5668 memset(&priv_ctx, 0, sizeof(priv_ctx));
5669 priv_ctx.string_buffers = string_buffers;
5670 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5671 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5672 shader_addline(buffer, "}\n");
5674 TRACE("Compiling shader object %u.\n", shader_id);
5675 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5677 return shader_id;
5680 static GLuint find_glsl_pshader(const struct wined3d_context *context,
5681 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5682 struct wined3d_shader *shader,
5683 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
5685 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
5686 struct glsl_shader_private *shader_data;
5687 struct ps_np2fixup_info *np2fixup;
5688 UINT i;
5689 DWORD new_size;
5690 GLuint ret;
5692 if (!shader->backend_data)
5694 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5695 if (!shader->backend_data)
5697 ERR("Failed to allocate backend data.\n");
5698 return 0;
5701 shader_data = shader->backend_data;
5702 gl_shaders = shader_data->gl_shaders.ps;
5704 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5705 * so a linear search is more performant than a hashmap or a binary search
5706 * (cache coherency etc)
5708 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5710 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
5712 if (args->np2_fixup)
5713 *np2fixup_info = &gl_shaders[i].np2fixup;
5714 return gl_shaders[i].id;
5718 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5719 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5720 if (shader_data->num_gl_shaders)
5722 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5723 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
5724 new_size * sizeof(*gl_shaders));
5726 else
5728 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5729 new_size = 1;
5732 if(!new_array) {
5733 ERR("Out of memory\n");
5734 return 0;
5736 shader_data->gl_shaders.ps = new_array;
5737 shader_data->shader_array_size = new_size;
5738 gl_shaders = new_array;
5741 gl_shaders[shader_data->num_gl_shaders].args = *args;
5743 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
5744 memset(np2fixup, 0, sizeof(*np2fixup));
5745 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
5747 pixelshader_update_resource_types(shader, args->tex_types);
5749 string_buffer_clear(buffer);
5750 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
5751 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5753 return ret;
5756 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
5757 const DWORD use_map)
5759 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
5760 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
5761 if (stored->point_size != new->point_size)
5762 return FALSE;
5763 if (stored->per_vertex_point_size != new->per_vertex_point_size)
5764 return FALSE;
5765 if (stored->flatshading != new->flatshading)
5766 return FALSE;
5767 return stored->fog_src == new->fog_src;
5770 static GLuint find_glsl_vshader(const struct wined3d_context *context,
5771 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5772 struct wined3d_shader *shader,
5773 const struct vs_compile_args *args)
5775 UINT i;
5776 DWORD new_size;
5777 DWORD use_map = context->stream_info.use_map;
5778 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
5779 struct glsl_shader_private *shader_data;
5780 GLuint ret;
5782 if (!shader->backend_data)
5784 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5785 if (!shader->backend_data)
5787 ERR("Failed to allocate backend data.\n");
5788 return 0;
5791 shader_data = shader->backend_data;
5792 gl_shaders = shader_data->gl_shaders.vs;
5794 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5795 * so a linear search is more performant than a hashmap or a binary search
5796 * (cache coherency etc)
5798 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5800 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
5801 return gl_shaders[i].id;
5804 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5806 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5807 if (shader_data->num_gl_shaders)
5809 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5810 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
5811 new_size * sizeof(*gl_shaders));
5813 else
5815 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5816 new_size = 1;
5819 if(!new_array) {
5820 ERR("Out of memory\n");
5821 return 0;
5823 shader_data->gl_shaders.vs = new_array;
5824 shader_data->shader_array_size = new_size;
5825 gl_shaders = new_array;
5828 gl_shaders[shader_data->num_gl_shaders].args = *args;
5830 string_buffer_clear(buffer);
5831 ret = shader_glsl_generate_vshader(context, buffer, string_buffers, shader, args);
5832 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5834 return ret;
5837 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
5838 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5839 struct wined3d_shader *shader)
5841 struct glsl_gs_compiled_shader *gl_shaders;
5842 struct glsl_shader_private *shader_data;
5843 GLuint ret;
5845 if (!shader->backend_data)
5847 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
5849 ERR("Failed to allocate backend data.\n");
5850 return 0;
5853 shader_data = shader->backend_data;
5854 gl_shaders = shader_data->gl_shaders.gs;
5856 if (shader_data->num_gl_shaders)
5857 return gl_shaders[0].id;
5859 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5861 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
5863 ERR("Failed to allocate GL shader array.\n");
5864 return 0;
5866 shader_data->shader_array_size = 1;
5867 gl_shaders = shader_data->gl_shaders.gs;
5869 string_buffer_clear(buffer);
5870 ret = shader_glsl_generate_geometry_shader(context, buffer, string_buffers, shader);
5871 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5873 return ret;
5876 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
5878 switch (mcs)
5880 case WINED3D_MCS_MATERIAL:
5881 return material;
5882 case WINED3D_MCS_COLOR1:
5883 return "ffp_attrib_diffuse";
5884 case WINED3D_MCS_COLOR2:
5885 return "ffp_attrib_specular";
5886 default:
5887 ERR("Invalid material color source %#x.\n", mcs);
5888 return "<invalid>";
5892 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
5893 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
5895 const char *diffuse, *specular, *emissive, *ambient;
5896 enum wined3d_light_type light_type;
5897 unsigned int i;
5899 if (!settings->lighting)
5901 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
5902 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
5903 return;
5906 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
5907 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
5908 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
5909 shader_addline(buffer, "vec3 dir, dst;\n");
5910 shader_addline(buffer, "float att, t;\n");
5912 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
5913 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
5914 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
5915 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
5917 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
5919 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
5920 switch (light_type)
5922 case WINED3D_LIGHT_POINT:
5923 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5924 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5925 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5926 shader_addline(buffer, "dst.x = 1.0;\n");
5927 if (legacy_lighting)
5929 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
5930 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
5932 else
5934 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5936 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5937 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", i, i, i);
5938 if (!legacy_lighting)
5939 shader_addline(buffer, "att = 1.0 / att;\n");
5940 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5941 if (!settings->normal)
5943 if (!legacy_lighting)
5944 shader_addline(buffer, "}\n");
5945 break;
5947 shader_addline(buffer, "dir = normalize(dir);\n");
5948 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5949 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
5950 if (settings->localviewer)
5951 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5952 else
5953 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5954 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5955 " * ffp_light[%u].specular) * att;\n", i);
5956 if (!legacy_lighting)
5957 shader_addline(buffer, "}\n");
5958 break;
5960 case WINED3D_LIGHT_SPOT:
5961 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5962 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5963 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5964 shader_addline(buffer, "dst.x = 1.0;\n");
5965 if (legacy_lighting)
5967 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
5968 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
5970 else
5972 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5974 shader_addline(buffer, "dir = normalize(dir);\n");
5975 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", i);
5976 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", i);
5977 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", i);
5978 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
5979 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
5980 i, i, i, i);
5981 if (legacy_lighting)
5982 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5983 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5984 i, i, i);
5985 else
5986 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5987 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5988 i, i, i);
5989 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5990 if (!settings->normal)
5992 if (!legacy_lighting)
5993 shader_addline(buffer, "}\n");
5994 break;
5996 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5997 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
5998 if (settings->localviewer)
5999 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6000 else
6001 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6002 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
6003 " * ffp_light[%u].specular) * att;\n", i);
6004 if (!legacy_lighting)
6005 shader_addline(buffer, "}\n");
6006 break;
6008 case WINED3D_LIGHT_DIRECTIONAL:
6009 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
6010 if (!settings->normal)
6011 break;
6012 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", i);
6013 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6014 " * ffp_light[%u].diffuse.xyz;\n", i);
6015 /* TODO: In the non-local viewer case the halfvector is constant
6016 * and could be precomputed and stored in a uniform. */
6017 if (settings->localviewer)
6018 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6019 else
6020 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6021 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6022 " * ffp_light[%u].specular;\n", i);
6023 break;
6025 case WINED3D_LIGHT_PARALLELPOINT:
6026 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
6027 if (!settings->normal)
6028 break;
6029 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", i);
6030 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6031 " * ffp_light[%u].diffuse.xyz;\n", i);
6032 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6033 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6034 " * ffp_light[%u].specular;\n", i);
6035 break;
6037 default:
6038 if (light_type)
6039 FIXME("Unhandled light type %#x.\n", light_type);
6040 continue;
6044 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
6045 ambient, diffuse, emissive);
6046 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
6047 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
6050 /* Context activation is done by the caller. */
6051 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
6052 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
6054 static const struct attrib_info
6056 const char type[6];
6057 const char name[24];
6059 attrib_info[] =
6061 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
6062 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
6063 /* TODO: Indexed vertex blending */
6064 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
6065 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
6066 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
6067 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
6068 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
6070 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6071 BOOL legacy_lighting = priv->legacy_lighting;
6072 GLuint shader_obj;
6073 unsigned int i;
6074 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6075 BOOL output_legacy_fogcoord = legacy_context;
6077 string_buffer_clear(buffer);
6079 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6081 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
6083 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
6085 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
6087 shader_addline(buffer, "\n");
6089 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
6090 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
6091 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
6092 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
6094 shader_addline(buffer, "uniform struct\n{\n");
6095 shader_addline(buffer, " vec4 emissive;\n");
6096 shader_addline(buffer, " vec4 ambient;\n");
6097 shader_addline(buffer, " vec4 diffuse;\n");
6098 shader_addline(buffer, " vec4 specular;\n");
6099 shader_addline(buffer, " float shininess;\n");
6100 shader_addline(buffer, "} ffp_material;\n");
6102 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
6103 shader_addline(buffer, "uniform struct\n{\n");
6104 shader_addline(buffer, " vec4 diffuse;\n");
6105 shader_addline(buffer, " vec4 specular;\n");
6106 shader_addline(buffer, " vec4 ambient;\n");
6107 shader_addline(buffer, " vec4 position;\n");
6108 shader_addline(buffer, " vec3 direction;\n");
6109 shader_addline(buffer, " float range;\n");
6110 shader_addline(buffer, " float falloff;\n");
6111 shader_addline(buffer, " float c_att;\n");
6112 shader_addline(buffer, " float l_att;\n");
6113 shader_addline(buffer, " float q_att;\n");
6114 shader_addline(buffer, " float cos_htheta;\n");
6115 shader_addline(buffer, " float cos_hphi;\n");
6116 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
6118 if (settings->point_size)
6120 shader_addline(buffer, "uniform struct\n{\n");
6121 shader_addline(buffer, " float size;\n");
6122 shader_addline(buffer, " float size_min;\n");
6123 shader_addline(buffer, " float size_max;\n");
6124 shader_addline(buffer, " float c_att;\n");
6125 shader_addline(buffer, " float l_att;\n");
6126 shader_addline(buffer, " float q_att;\n");
6127 shader_addline(buffer, "} ffp_point;\n");
6130 if (legacy_context)
6132 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6133 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6134 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6135 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6137 else
6139 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
6140 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
6141 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6142 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6145 shader_addline(buffer, "\nvoid main()\n{\n");
6146 shader_addline(buffer, "float m;\n");
6147 shader_addline(buffer, "vec3 r;\n");
6149 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
6151 if (attrib_info[i].name[0])
6152 shader_addline(buffer, "%s %s = vs_in%u;\n",
6153 attrib_info[i].type, attrib_info[i].name, i);
6155 for (i = 0; i < MAX_TEXTURES; ++i)
6157 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
6158 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
6159 && settings->texcoords & (1u << i))
6160 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
6163 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
6165 if (settings->transformed)
6167 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
6168 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6169 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
6171 else
6173 for (i = 0; i < settings->vertexblends; ++i)
6174 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
6176 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
6177 for (i = 0; i < settings->vertexblends + 1; ++i)
6178 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
6180 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6181 if (settings->clipping)
6182 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
6183 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
6186 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
6187 if (settings->normal)
6189 if (!settings->vertexblends)
6191 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
6193 else
6195 for (i = 0; i < settings->vertexblends + 1; ++i)
6196 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
6199 if (settings->normalize)
6200 shader_addline(buffer, "normal = normalize(normal);\n");
6203 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
6204 if (legacy_context)
6206 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
6207 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
6209 else
6211 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
6212 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
6215 for (i = 0; i < MAX_TEXTURES; ++i)
6217 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6219 switch (settings->texgen[i] & 0xffff0000)
6221 case WINED3DTSS_TCI_PASSTHRU:
6222 if (settings->texcoords & (1u << i))
6223 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
6224 i, i, i);
6225 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
6226 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
6227 else
6228 output_legacy_texcoord = FALSE;
6229 break;
6231 case WINED3DTSS_TCI_CAMERASPACENORMAL:
6232 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
6233 break;
6235 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
6236 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
6237 break;
6239 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
6240 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6241 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
6242 break;
6244 case WINED3DTSS_TCI_SPHEREMAP:
6245 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
6246 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
6247 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6248 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
6249 break;
6251 default:
6252 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
6253 break;
6255 if (output_legacy_texcoord)
6256 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
6259 switch (settings->fog_mode)
6261 case WINED3D_FFP_VS_FOG_OFF:
6262 output_legacy_fogcoord = FALSE;
6263 break;
6265 case WINED3D_FFP_VS_FOG_FOGCOORD:
6266 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
6267 break;
6269 case WINED3D_FFP_VS_FOG_RANGE:
6270 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
6271 break;
6273 case WINED3D_FFP_VS_FOG_DEPTH:
6274 if (settings->ortho_fog)
6275 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
6276 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
6277 else if (settings->transformed)
6278 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
6279 else
6280 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
6281 break;
6283 default:
6284 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
6285 break;
6287 if (output_legacy_fogcoord)
6288 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
6290 if (settings->point_size)
6292 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
6293 " + ffp_point.l_att * length(ec_pos.xyz)"
6294 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
6295 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
6296 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
6299 shader_addline(buffer, "}\n");
6301 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6302 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
6304 return shader_obj;
6307 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
6308 DWORD argnum, unsigned int stage, DWORD arg)
6310 const char *ret;
6312 if (arg == ARG_UNUSED)
6313 return "<unused arg>";
6315 switch (arg & WINED3DTA_SELECTMASK)
6317 case WINED3DTA_DIFFUSE:
6318 ret = "ffp_varying_diffuse";
6319 break;
6321 case WINED3DTA_CURRENT:
6322 ret = "ret";
6323 break;
6325 case WINED3DTA_TEXTURE:
6326 switch (stage)
6328 case 0: ret = "tex0"; break;
6329 case 1: ret = "tex1"; break;
6330 case 2: ret = "tex2"; break;
6331 case 3: ret = "tex3"; break;
6332 case 4: ret = "tex4"; break;
6333 case 5: ret = "tex5"; break;
6334 case 6: ret = "tex6"; break;
6335 case 7: ret = "tex7"; break;
6336 default:
6337 ret = "<invalid texture>";
6338 break;
6340 break;
6342 case WINED3DTA_TFACTOR:
6343 ret = "tex_factor";
6344 break;
6346 case WINED3DTA_SPECULAR:
6347 ret = "ffp_varying_specular";
6348 break;
6350 case WINED3DTA_TEMP:
6351 ret = "temp_reg";
6352 break;
6354 case WINED3DTA_CONSTANT:
6355 switch (stage)
6357 case 0: ret = "tss_const0"; break;
6358 case 1: ret = "tss_const1"; break;
6359 case 2: ret = "tss_const2"; break;
6360 case 3: ret = "tss_const3"; break;
6361 case 4: ret = "tss_const4"; break;
6362 case 5: ret = "tss_const5"; break;
6363 case 6: ret = "tss_const6"; break;
6364 case 7: ret = "tss_const7"; break;
6365 default:
6366 ret = "<invalid constant>";
6367 break;
6369 break;
6371 default:
6372 return "<unhandled arg>";
6375 if (arg & WINED3DTA_COMPLEMENT)
6377 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
6378 if (argnum == 0)
6379 ret = "arg0";
6380 else if (argnum == 1)
6381 ret = "arg1";
6382 else if (argnum == 2)
6383 ret = "arg2";
6386 if (arg & WINED3DTA_ALPHAREPLICATE)
6388 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
6389 if (argnum == 0)
6390 ret = "arg0";
6391 else if (argnum == 1)
6392 ret = "arg1";
6393 else if (argnum == 2)
6394 ret = "arg2";
6397 return ret;
6400 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
6401 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
6403 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
6405 if (color && alpha)
6406 dstmask = "";
6407 else if (color)
6408 dstmask = ".xyz";
6409 else
6410 dstmask = ".w";
6412 if (dst == tempreg)
6413 dstreg = "temp_reg";
6414 else
6415 dstreg = "ret";
6417 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
6418 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
6419 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
6421 switch (op)
6423 case WINED3D_TOP_DISABLE:
6424 break;
6426 case WINED3D_TOP_SELECT_ARG1:
6427 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
6428 break;
6430 case WINED3D_TOP_SELECT_ARG2:
6431 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
6432 break;
6434 case WINED3D_TOP_MODULATE:
6435 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6436 break;
6438 case WINED3D_TOP_MODULATE_4X:
6439 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
6440 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6441 break;
6443 case WINED3D_TOP_MODULATE_2X:
6444 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
6445 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6446 break;
6448 case WINED3D_TOP_ADD:
6449 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
6450 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6451 break;
6453 case WINED3D_TOP_ADD_SIGNED:
6454 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
6455 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6456 break;
6458 case WINED3D_TOP_ADD_SIGNED_2X:
6459 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
6460 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6461 break;
6463 case WINED3D_TOP_SUBTRACT:
6464 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
6465 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6466 break;
6468 case WINED3D_TOP_ADD_SMOOTH:
6469 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
6470 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
6471 break;
6473 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
6474 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
6475 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6476 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6477 break;
6479 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6480 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6481 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6482 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6483 break;
6485 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6486 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
6487 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6488 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6489 break;
6491 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6492 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6493 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6494 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
6495 break;
6497 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
6498 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
6499 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6500 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6501 break;
6503 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
6504 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
6505 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6506 break;
6508 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
6509 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
6510 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6511 break;
6513 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
6514 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6515 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6516 break;
6517 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
6518 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
6519 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6520 break;
6522 case WINED3D_TOP_BUMPENVMAP:
6523 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6524 /* These are handled in the first pass, nothing to do. */
6525 break;
6527 case WINED3D_TOP_DOTPRODUCT3:
6528 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
6529 dstreg, dstmask, arg1, arg2, dstmask);
6530 break;
6532 case WINED3D_TOP_MULTIPLY_ADD:
6533 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
6534 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
6535 break;
6537 case WINED3D_TOP_LERP:
6538 /* MSDN isn't quite right here. */
6539 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
6540 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
6541 break;
6543 default:
6544 FIXME("Unhandled operation %#x.\n", op);
6545 break;
6549 /* Context activation is done by the caller. */
6550 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
6551 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
6553 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6554 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
6555 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
6556 UINT lowest_disabled_stage;
6557 GLuint shader_id;
6558 DWORD arg0, arg1, arg2;
6559 unsigned int stage;
6560 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
6561 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6563 string_buffer_clear(buffer);
6565 /* Find out which textures are read */
6566 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6568 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6569 break;
6571 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
6572 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
6573 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
6575 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
6576 || (stage == 0 && settings->color_key_enabled))
6577 tex_map |= 1u << stage;
6578 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6579 tfactor_used = TRUE;
6580 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6581 tempreg_used = TRUE;
6582 if (settings->op[stage].dst == tempreg)
6583 tempreg_used = TRUE;
6584 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6585 tss_const_map |= 1u << stage;
6587 switch (settings->op[stage].cop)
6589 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6590 lum_map |= 1u << stage;
6591 /* fall through */
6592 case WINED3D_TOP_BUMPENVMAP:
6593 bump_map |= 1u << stage;
6594 /* fall through */
6595 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6596 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6597 tex_map |= 1u << stage;
6598 break;
6600 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6601 tfactor_used = TRUE;
6602 break;
6604 default:
6605 break;
6608 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6609 continue;
6611 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
6612 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
6613 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
6615 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
6616 tex_map |= 1u << stage;
6617 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6618 tfactor_used = TRUE;
6619 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6620 tempreg_used = TRUE;
6621 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6622 tss_const_map |= 1u << stage;
6624 lowest_disabled_stage = stage;
6626 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6628 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6629 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6631 if (!needs_legacy_glsl_syntax(gl_info))
6632 shader_addline(buffer, "out vec4 ps_out[1];\n");
6634 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
6635 shader_addline(buffer, "vec4 ret;\n");
6636 if (tempreg_used || settings->sRGB_write)
6637 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
6638 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
6640 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6642 if (tss_const_map & (1u << stage))
6643 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
6645 if (!(tex_map & (1u << stage)))
6646 continue;
6648 switch (settings->op[stage].tex_type)
6650 case WINED3D_GL_RES_TYPE_TEX_1D:
6651 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
6652 break;
6653 case WINED3D_GL_RES_TYPE_TEX_2D:
6654 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
6655 break;
6656 case WINED3D_GL_RES_TYPE_TEX_3D:
6657 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
6658 break;
6659 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6660 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
6661 break;
6662 case WINED3D_GL_RES_TYPE_TEX_RECT:
6663 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
6664 break;
6665 default:
6666 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
6667 break;
6670 shader_addline(buffer, "vec4 tex%u;\n", stage);
6672 if (!(bump_map & (1u << stage)))
6673 continue;
6674 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
6676 if (!(lum_map & (1u << stage)))
6677 continue;
6678 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
6679 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
6681 if (tfactor_used)
6682 shader_addline(buffer, "uniform vec4 tex_factor;\n");
6683 if (settings->color_key_enabled)
6684 shader_addline(buffer, "uniform vec4 color_key[2];\n");
6685 shader_addline(buffer, "uniform vec4 specular_enable;\n");
6687 if (settings->sRGB_write)
6689 shader_addline(buffer, "const vec4 srgb_const0 = ");
6690 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
6691 shader_addline(buffer, ";\n");
6692 shader_addline(buffer, "const vec4 srgb_const1 = ");
6693 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
6694 shader_addline(buffer, ";\n");
6697 shader_addline(buffer, "uniform struct\n{\n");
6698 shader_addline(buffer, " vec4 color;\n");
6699 shader_addline(buffer, " float density;\n");
6700 shader_addline(buffer, " float end;\n");
6701 shader_addline(buffer, " float scale;\n");
6702 shader_addline(buffer, "} ffp_fog;\n");
6704 if (legacy_context)
6706 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6707 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6708 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6709 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6710 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6712 else
6714 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
6715 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
6716 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6717 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6718 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6721 shader_addline(buffer, "void main()\n{\n");
6723 if (legacy_context)
6725 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
6726 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
6729 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6731 if (tex_map & (1u << stage))
6733 if (settings->pointsprite)
6734 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
6735 else if (settings->texcoords_initialized & (1u << stage))
6736 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
6737 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
6738 else
6739 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
6743 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
6744 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6746 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
6747 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
6749 /* Generate texture sampling instructions */
6750 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
6752 const char *texture_function, *coord_mask;
6753 BOOL proj;
6755 if (!(tex_map & (1u << stage)))
6756 continue;
6758 if (settings->op[stage].projected == proj_none)
6760 proj = FALSE;
6762 else if (settings->op[stage].projected == proj_count4
6763 || settings->op[stage].projected == proj_count3)
6765 proj = TRUE;
6767 else
6769 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
6770 proj = TRUE;
6773 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
6774 proj = FALSE;
6776 switch (settings->op[stage].tex_type)
6778 case WINED3D_GL_RES_TYPE_TEX_1D:
6779 if (proj)
6781 texture_function = "texture1DProj";
6782 coord_mask = "xw";
6784 else
6786 texture_function = "texture1D";
6787 coord_mask = "x";
6789 break;
6790 case WINED3D_GL_RES_TYPE_TEX_2D:
6791 if (proj)
6793 texture_function = "texture2DProj";
6794 coord_mask = "xyw";
6796 else
6798 texture_function = "texture2D";
6799 coord_mask = "xy";
6801 break;
6802 case WINED3D_GL_RES_TYPE_TEX_3D:
6803 if (proj)
6805 texture_function = "texture3DProj";
6806 coord_mask = "xyzw";
6808 else
6810 texture_function = "texture3D";
6811 coord_mask = "xyz";
6813 break;
6814 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6815 texture_function = "textureCube";
6816 coord_mask = "xyz";
6817 break;
6818 case WINED3D_GL_RES_TYPE_TEX_RECT:
6819 if (proj)
6821 texture_function = "texture2DRectProj";
6822 coord_mask = "xyw";
6824 else
6826 texture_function = "texture2DRect";
6827 coord_mask = "xy";
6829 break;
6830 default:
6831 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
6832 texture_function = "";
6833 coord_mask = "xyzw";
6834 break;
6836 if (!needs_legacy_glsl_syntax(gl_info))
6837 texture_function = proj ? "textureProj" : "texture";
6839 if (stage > 0
6840 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
6841 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
6843 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
6845 /* With projective textures, texbem only divides the static
6846 * texture coord, not the displacement, so multiply the
6847 * displacement with the dividing parameter before passing it to
6848 * TXP. */
6849 if (settings->op[stage].projected != proj_none)
6851 if (settings->op[stage].projected == proj_count4)
6853 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
6854 stage, stage);
6855 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
6857 else
6859 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
6860 stage, stage);
6861 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
6864 else
6866 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
6869 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
6870 stage, texture_function, stage, coord_mask);
6872 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6873 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
6874 stage, stage - 1, stage - 1, stage - 1);
6876 else if (settings->op[stage].projected == proj_count3)
6878 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
6879 stage, texture_function, stage, stage);
6881 else
6883 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
6884 stage, texture_function, stage, stage, coord_mask);
6887 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
6888 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
6889 settings->op[stage].color_fixup);
6892 if (settings->color_key_enabled)
6894 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
6895 shader_addline(buffer, " discard;\n");
6898 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
6900 /* Generate the main shader */
6901 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6903 BOOL op_equal;
6905 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6906 break;
6908 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6909 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6910 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
6911 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6912 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6913 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
6914 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6915 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6916 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
6917 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6918 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6919 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
6920 else
6921 op_equal = settings->op[stage].aop == settings->op[stage].cop
6922 && settings->op[stage].carg0 == settings->op[stage].aarg0
6923 && settings->op[stage].carg1 == settings->op[stage].aarg1
6924 && settings->op[stage].carg2 == settings->op[stage].aarg2;
6926 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6928 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6929 settings->op[stage].cop, settings->op[stage].carg0,
6930 settings->op[stage].carg1, settings->op[stage].carg2);
6932 else if (op_equal)
6934 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
6935 settings->op[stage].cop, settings->op[stage].carg0,
6936 settings->op[stage].carg1, settings->op[stage].carg2);
6938 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
6939 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6941 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6942 settings->op[stage].cop, settings->op[stage].carg0,
6943 settings->op[stage].carg1, settings->op[stage].carg2);
6944 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
6945 settings->op[stage].aop, settings->op[stage].aarg0,
6946 settings->op[stage].aarg1, settings->op[stage].aarg2);
6950 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
6951 get_fragment_output(gl_info));
6953 if (settings->sRGB_write)
6954 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
6956 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
6958 shader_addline(buffer, "}\n");
6960 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6961 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6963 string_buffer_release(&priv->string_buffers, tex_reg_name);
6964 return shader_id;
6967 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
6968 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
6970 struct glsl_ffp_vertex_shader *shader;
6971 const struct wine_rb_entry *entry;
6973 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
6974 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
6976 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
6977 return NULL;
6979 shader->desc.settings = *settings;
6980 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
6981 list_init(&shader->linked_programs);
6982 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
6983 ERR("Failed to insert ffp vertex shader.\n");
6985 return shader;
6988 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
6989 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
6991 struct glsl_ffp_fragment_shader *glsl_desc;
6992 const struct ffp_frag_desc *desc;
6994 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
6995 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
6997 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
6998 return NULL;
7000 glsl_desc->entry.settings = *args;
7001 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
7002 list_init(&glsl_desc->linked_programs);
7003 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
7005 return glsl_desc;
7009 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
7010 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
7012 unsigned int i;
7013 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7015 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
7016 sizeof(GLuint) * gl_info->limits.glsl_vs_float_constants);
7017 for (i = 0; i < vs_c_count; ++i)
7019 string_buffer_sprintf(name, "vs_c[%u]", i);
7020 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7022 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
7023 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLuint));
7025 for (i = 0; i < MAX_CONST_I; ++i)
7027 string_buffer_sprintf(name, "vs_i[%u]", i);
7028 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7031 for (i = 0; i < MAX_CONST_B; ++i)
7033 string_buffer_sprintf(name, "vs_b[%u]", i);
7034 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7037 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "posFixup"));
7039 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
7041 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
7042 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7044 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
7045 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
7046 for (i = 0; i < MAX_TEXTURES; ++i)
7048 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
7049 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7051 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
7052 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
7053 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
7054 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
7055 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
7056 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
7057 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
7059 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
7060 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7061 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
7062 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7063 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
7064 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7065 string_buffer_sprintf(name, "ffp_light[%u].position", i);
7066 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7067 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
7068 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7069 string_buffer_sprintf(name, "ffp_light[%u].range", i);
7070 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7071 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
7072 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7073 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
7074 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7075 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
7076 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7077 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
7078 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7079 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
7080 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7081 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
7082 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7084 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
7085 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
7086 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
7087 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
7088 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
7089 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
7091 string_buffer_release(&priv->string_buffers, name);
7094 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
7095 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
7097 unsigned int i;
7098 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7100 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
7101 sizeof(GLuint) * gl_info->limits.glsl_ps_float_constants);
7102 for (i = 0; i < ps_c_count; ++i)
7104 string_buffer_sprintf(name, "ps_c[%u]", i);
7105 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7107 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
7108 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLuint));
7110 for (i = 0; i < MAX_CONST_I; ++i)
7112 string_buffer_sprintf(name, "ps_i[%u]", i);
7113 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7116 for (i = 0; i < MAX_CONST_B; ++i)
7118 string_buffer_sprintf(name, "ps_b[%u]", i);
7119 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7122 for (i = 0; i < MAX_TEXTURES; ++i)
7124 string_buffer_sprintf(name, "bumpenv_mat%u", i);
7125 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7126 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
7127 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7128 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
7129 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7130 string_buffer_sprintf(name, "tss_const%u", i);
7131 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7134 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
7135 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
7137 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
7138 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
7139 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
7140 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
7142 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
7143 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
7144 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
7146 string_buffer_release(&priv->string_buffers, name);
7149 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
7150 struct shader_glsl_priv *priv, GLuint program_id,
7151 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
7153 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
7154 GLuint block_idx;
7155 unsigned int i;
7156 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7158 for (i = 0; i < count; ++i)
7160 if (!reg_maps->cb_sizes[i])
7161 continue;
7163 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
7164 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
7165 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
7167 checkGLcall("glUniformBlockBinding");
7168 string_buffer_release(&priv->string_buffers, name);
7171 /* Context activation is done by the caller. */
7172 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
7173 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
7175 const struct wined3d_gl_info *gl_info = context->gl_info;
7176 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
7177 const struct ps_np2fixup_info *np2fixup_info = NULL;
7178 struct glsl_shader_prog_link *entry = NULL;
7179 struct wined3d_shader *vshader = NULL;
7180 struct wined3d_shader *gshader = NULL;
7181 struct wined3d_shader *pshader = NULL;
7182 GLuint program_id;
7183 GLuint reorder_shader_id = 0;
7184 unsigned int i;
7185 GLuint vs_id = 0;
7186 GLuint gs_id = 0;
7187 GLuint ps_id = 0;
7188 struct list *ps_list, *vs_list;
7189 WORD attribs_map;
7190 struct wined3d_string_buffer *tmp_name;
7192 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
7194 vs_id = ctx_data->glsl_program->vs.id;
7195 vs_list = &ctx_data->glsl_program->vs.shader_entry;
7197 if (use_vs(state))
7199 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7200 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
7202 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
7203 && ctx_data->glsl_program->gs.id)
7204 gs_id = ctx_data->glsl_program->gs.id;
7205 else if (gshader)
7206 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, &priv->string_buffers, gshader);
7209 else if (use_vs(state))
7211 struct vs_compile_args vs_compile_args;
7213 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7215 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, d3d_info);
7216 vs_id = find_glsl_vshader(context, &priv->shader_buffer, &priv->string_buffers, vshader, &vs_compile_args);
7217 vs_list = &vshader->linked_programs;
7219 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
7220 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, &priv->string_buffers, gshader);
7222 else if (priv->vertex_pipe == &glsl_vertex_pipe)
7224 struct glsl_ffp_vertex_shader *ffp_shader;
7225 struct wined3d_ffp_vs_settings settings;
7227 wined3d_ffp_get_vs_settings(context, state, &settings);
7228 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
7229 vs_id = ffp_shader->id;
7230 vs_list = &ffp_shader->linked_programs;
7233 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
7235 ps_id = ctx_data->glsl_program->ps.id;
7236 ps_list = &ctx_data->glsl_program->ps.shader_entry;
7238 if (use_ps(state))
7239 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7241 else if (use_ps(state))
7243 struct ps_compile_args ps_compile_args;
7244 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7245 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
7246 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
7247 pshader, &ps_compile_args, &np2fixup_info);
7248 ps_list = &pshader->linked_programs;
7250 else if (priv->fragment_pipe == &glsl_fragment_pipe)
7252 struct glsl_ffp_fragment_shader *ffp_shader;
7253 struct ffp_frag_settings settings;
7255 gen_ffp_frag_op(context, state, &settings, FALSE);
7256 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
7257 ps_id = ffp_shader->id;
7258 ps_list = &ffp_shader->linked_programs;
7261 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
7263 ctx_data->glsl_program = entry;
7264 return;
7267 /* If we get to this point, then no matching program exists, so we create one */
7268 program_id = GL_EXTCALL(glCreateProgram());
7269 TRACE("Created new GLSL shader program %u.\n", program_id);
7271 /* Create the entry */
7272 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
7273 entry->id = program_id;
7274 entry->vs.id = vs_id;
7275 entry->gs.id = gs_id;
7276 entry->ps.id = ps_id;
7277 entry->constant_version = 0;
7278 entry->ps.np2_fixup_info = np2fixup_info;
7279 /* Add the hash table entry */
7280 add_glsl_program_entry(priv, entry);
7282 /* Set the current program */
7283 ctx_data->glsl_program = entry;
7285 /* Attach GLSL vshader */
7286 if (vs_id)
7288 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
7289 GL_EXTCALL(glAttachShader(program_id, vs_id));
7290 checkGLcall("glAttachShader");
7292 list_add_head(vs_list, &entry->vs.shader_entry);
7295 if (vshader)
7297 attribs_map = vshader->reg_maps.input_registers;
7298 reorder_shader_id = generate_param_reorder_function(priv, vshader, pshader,
7299 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
7300 d3d_info->emulated_flatshading
7301 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
7302 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
7303 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
7304 checkGLcall("glAttachShader");
7305 /* Flag the reorder function for deletion, then it will be freed automatically when the program
7306 * is destroyed
7308 GL_EXTCALL(glDeleteShader(reorder_shader_id));
7310 else
7312 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
7315 /* Bind vertex attributes to a corresponding index number to match
7316 * the same index numbers as ARB_vertex_programs (makes loading
7317 * vertex attributes simpler). With this method, we can use the
7318 * exact same code to load the attributes later for both ARB and
7319 * GLSL shaders.
7321 * We have to do this here because we need to know the Program ID
7322 * in order to make the bindings work, and it has to be done prior
7323 * to linking the GLSL program. */
7324 tmp_name = string_buffer_get(&priv->string_buffers);
7325 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
7327 if (!(attribs_map & 1))
7328 continue;
7330 string_buffer_sprintf(tmp_name, "vs_in%u", i);
7331 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
7333 checkGLcall("glBindAttribLocation");
7334 string_buffer_release(&priv->string_buffers, tmp_name);
7336 if (gshader)
7338 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
7339 GL_EXTCALL(glAttachShader(program_id, gs_id));
7340 checkGLcall("glAttachShader");
7342 TRACE("input type %s, output type %s, vertices out %u.\n",
7343 debug_d3dprimitivetype(gshader->u.gs.input_type),
7344 debug_d3dprimitivetype(gshader->u.gs.output_type),
7345 gshader->u.gs.vertices_out);
7346 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
7347 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
7348 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
7349 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
7350 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
7351 gshader->u.gs.vertices_out));
7352 checkGLcall("glProgramParameteriARB");
7354 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
7357 /* Attach GLSL pshader */
7358 if (ps_id)
7360 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
7361 GL_EXTCALL(glAttachShader(program_id, ps_id));
7362 checkGLcall("glAttachShader");
7364 list_add_head(ps_list, &entry->ps.shader_entry);
7367 /* Link the program */
7368 TRACE("Linking GLSL shader program %u.\n", program_id);
7369 GL_EXTCALL(glLinkProgram(program_id));
7370 shader_glsl_validate_link(gl_info, program_id);
7372 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
7373 vshader ? min(vshader->limits->constant_float, gl_info->limits.glsl_vs_float_constants) : 0);
7374 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
7375 pshader ? min(pshader->limits->constant_float, gl_info->limits.glsl_ps_float_constants) : 0);
7376 checkGLcall("Find glsl program uniform locations");
7378 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
7380 if (pshader && pshader->reg_maps.shader_version.major >= 3
7381 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
7383 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
7384 entry->vs.vertex_color_clamp = GL_FALSE;
7386 else
7388 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7391 else
7393 /* With core profile we never change vertex_color_clamp from
7394 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
7395 * glClampColorARB(). */
7396 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7399 /* Set the shader to allow uniform loading on it */
7400 GL_EXTCALL(glUseProgram(program_id));
7401 checkGLcall("glUseProgram");
7403 /* Texture unit mapping is set up to be the same each time the shader
7404 * program is used so we can hardcode the sampler uniform values. */
7405 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
7407 entry->constant_update_mask = 0;
7408 if (vshader)
7410 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
7411 if (vshader->reg_maps.integer_constants)
7412 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
7413 if (vshader->reg_maps.boolean_constants)
7414 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
7415 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
7417 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps,
7418 0, gl_info->limits.vertex_uniform_blocks);
7420 else
7422 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
7423 | WINED3D_SHADER_CONST_FFP_PROJ;
7425 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
7427 if (entry->vs.modelview_matrix_location[i] != -1)
7429 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
7430 break;
7434 for (i = 0; i < MAX_TEXTURES; ++i)
7436 if (entry->vs.texture_matrix_location[i] != -1)
7438 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7439 break;
7442 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
7443 || entry->vs.material_specular_location != -1
7444 || entry->vs.material_emissive_location != -1
7445 || entry->vs.material_shininess_location != -1)
7446 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
7447 if (entry->vs.light_ambient_location != -1)
7448 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
7450 if (entry->vs.pointsize_min_location != -1)
7451 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
7453 if (gshader)
7454 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps,
7455 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
7457 if (ps_id)
7459 if (pshader)
7461 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
7462 if (pshader->reg_maps.integer_constants)
7463 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
7464 if (pshader->reg_maps.boolean_constants)
7465 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
7466 if (entry->ps.ycorrection_location != -1)
7467 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
7469 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps,
7470 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
7471 gl_info->limits.fragment_uniform_blocks);
7473 else
7475 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7478 for (i = 0; i < MAX_TEXTURES; ++i)
7480 if (entry->ps.bumpenv_mat_location[i] != -1)
7482 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
7483 break;
7487 if (entry->ps.fog_color_location != -1)
7488 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
7489 if (entry->ps.np2_fixup_location != -1)
7490 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
7491 if (entry->ps.color_key_location != -1)
7492 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
7496 /* Context activation is done by the caller. */
7497 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum wined3d_gl_resource_type tex_type,
7498 BOOL masked)
7500 GLuint program_id;
7501 GLuint vshader_id, pshader_id;
7502 const char *blt_pshader;
7504 static const char blt_vshader[] =
7505 "#version 120\n"
7506 "void main(void)\n"
7507 "{\n"
7508 " gl_Position = gl_Vertex;\n"
7509 " gl_FrontColor = vec4(1.0);\n"
7510 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
7511 "}\n";
7513 static const char * const blt_pshaders_full[WINED3D_GL_RES_TYPE_COUNT] =
7515 /* WINED3D_GL_RES_TYPE_TEX_1D */
7516 NULL,
7517 /* WINED3D_GL_RES_TYPE_TEX_2D */
7518 "#version 120\n"
7519 "uniform sampler2D sampler;\n"
7520 "void main(void)\n"
7521 "{\n"
7522 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7523 "}\n",
7524 /* WINED3D_GL_RES_TYPE_TEX_3D */
7525 NULL,
7526 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7527 "#version 120\n"
7528 "uniform samplerCube sampler;\n"
7529 "void main(void)\n"
7530 "{\n"
7531 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7532 "}\n",
7533 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7534 "#version 120\n"
7535 "#extension GL_ARB_texture_rectangle : enable\n"
7536 "uniform sampler2DRect sampler;\n"
7537 "void main(void)\n"
7538 "{\n"
7539 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7540 "}\n",
7541 /* WINED3D_GL_RES_TYPE_BUFFER */
7542 NULL,
7543 /* WINED3D_GL_RES_TYPE_RB */
7544 NULL,
7547 static const char * const blt_pshaders_masked[WINED3D_GL_RES_TYPE_COUNT] =
7549 /* WINED3D_GL_RES_TYPE_TEX_1D */
7550 NULL,
7551 /* WINED3D_GL_RES_TYPE_TEX_2D */
7552 "#version 120\n"
7553 "uniform sampler2D sampler;\n"
7554 "uniform vec4 mask;\n"
7555 "void main(void)\n"
7556 "{\n"
7557 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7558 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7559 "}\n",
7560 /* WINED3D_GL_RES_TYPE_TEX_3D */
7561 NULL,
7562 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7563 "#version 120\n"
7564 "uniform samplerCube sampler;\n"
7565 "uniform vec4 mask;\n"
7566 "void main(void)\n"
7567 "{\n"
7568 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7569 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7570 "}\n",
7571 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7572 "#version 120\n"
7573 "#extension GL_ARB_texture_rectangle : enable\n"
7574 "uniform sampler2DRect sampler;\n"
7575 "uniform vec4 mask;\n"
7576 "void main(void)\n"
7577 "{\n"
7578 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7579 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7580 "}\n",
7581 /* WINED3D_GL_RES_TYPE_BUFFER */
7582 NULL,
7583 /* WINED3D_GL_RES_TYPE_RB */
7584 NULL,
7587 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
7588 if (!blt_pshader)
7590 FIXME("tex_type %#x not supported\n", tex_type);
7591 return 0;
7594 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7595 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
7597 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7598 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
7600 program_id = GL_EXTCALL(glCreateProgram());
7601 GL_EXTCALL(glAttachShader(program_id, vshader_id));
7602 GL_EXTCALL(glAttachShader(program_id, pshader_id));
7603 GL_EXTCALL(glLinkProgram(program_id));
7605 shader_glsl_validate_link(gl_info, program_id);
7607 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
7608 * is destroyed
7610 GL_EXTCALL(glDeleteShader(vshader_id));
7611 GL_EXTCALL(glDeleteShader(pshader_id));
7612 return program_id;
7615 /* Context activation is done by the caller. */
7616 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
7617 const struct wined3d_state *state)
7619 struct glsl_context_data *ctx_data = context->shader_backend_data;
7620 const struct wined3d_gl_info *gl_info = context->gl_info;
7621 struct shader_glsl_priv *priv = shader_priv;
7622 GLuint program_id = 0, prev_id = 0;
7623 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
7625 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
7626 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
7628 if (ctx_data->glsl_program)
7630 prev_id = ctx_data->glsl_program->id;
7631 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7633 else
7635 prev_id = 0;
7636 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7639 set_glsl_shader_program(context, state, priv, ctx_data);
7641 if (ctx_data->glsl_program)
7643 program_id = ctx_data->glsl_program->id;
7644 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7646 else
7648 program_id = 0;
7649 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7652 if (old_vertex_color_clamp != current_vertex_color_clamp)
7654 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7656 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
7657 checkGLcall("glClampColorARB");
7659 else
7661 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
7665 TRACE("Using GLSL program %u.\n", program_id);
7667 if (prev_id != program_id)
7669 GL_EXTCALL(glUseProgram(program_id));
7670 checkGLcall("glUseProgram");
7672 if (program_id)
7673 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
7677 /* "context" is not necessarily the currently active context. */
7678 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
7680 struct glsl_context_data *ctx_data = context->shader_backend_data;
7682 ctx_data->glsl_program = NULL;
7683 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
7684 | (1u << WINED3D_SHADER_TYPE_VERTEX)
7685 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
7686 | (1u << WINED3D_SHADER_TYPE_HULL)
7687 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
7690 /* Context activation is done by the caller. */
7691 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
7693 const struct wined3d_gl_info *gl_info = context->gl_info;
7694 struct shader_glsl_priv *priv = shader_priv;
7696 shader_glsl_invalidate_current_program(context);
7697 GL_EXTCALL(glUseProgram(0));
7698 checkGLcall("glUseProgram");
7700 priv->vertex_pipe->vp_enable(gl_info, FALSE);
7701 priv->fragment_pipe->enable_extension(gl_info, FALSE);
7703 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7705 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
7706 checkGLcall("glClampColorARB");
7710 /* Context activation is done by the caller. */
7711 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
7712 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size)
7714 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
7715 struct shader_glsl_priv *priv = shader_priv;
7716 GLuint *blt_program;
7717 GLint loc;
7719 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
7720 if (!*blt_program)
7722 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
7723 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
7724 GL_EXTCALL(glUseProgram(*blt_program));
7725 GL_EXTCALL(glUniform1i(loc, 0));
7727 else
7729 GL_EXTCALL(glUseProgram(*blt_program));
7732 if (masked)
7734 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
7735 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
7739 /* Context activation is done by the caller. */
7740 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
7742 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
7743 GLuint program_id;
7745 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
7746 if (program_id) TRACE("Using GLSL program %u\n", program_id);
7748 GL_EXTCALL(glUseProgram(program_id));
7749 checkGLcall("glUseProgram");
7752 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
7753 const struct glsl_shader_prog_link *program)
7755 const struct glsl_context_data *ctx_data;
7756 struct wined3d_context *context;
7757 unsigned int i;
7759 for (i = 0; i < device->context_count; ++i)
7761 context = device->contexts[i];
7762 ctx_data = context->shader_backend_data;
7764 if (ctx_data->glsl_program == program)
7765 shader_glsl_invalidate_current_program(context);
7769 static void shader_glsl_destroy(struct wined3d_shader *shader)
7771 struct glsl_shader_private *shader_data = shader->backend_data;
7772 struct wined3d_device *device = shader->device;
7773 struct shader_glsl_priv *priv = device->shader_priv;
7774 const struct wined3d_gl_info *gl_info;
7775 const struct list *linked_programs;
7776 struct wined3d_context *context;
7778 if (!shader_data || !shader_data->num_gl_shaders)
7780 HeapFree(GetProcessHeap(), 0, shader_data);
7781 shader->backend_data = NULL;
7782 return;
7785 context = context_acquire(device, NULL);
7786 gl_info = context->gl_info;
7788 TRACE("Deleting linked programs.\n");
7789 linked_programs = &shader->linked_programs;
7790 if (linked_programs->next)
7792 struct glsl_shader_prog_link *entry, *entry2;
7793 UINT i;
7795 switch (shader->reg_maps.shader_version.type)
7797 case WINED3D_SHADER_TYPE_PIXEL:
7799 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
7801 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7803 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
7804 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7805 checkGLcall("glDeleteShader");
7807 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
7809 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7810 struct glsl_shader_prog_link, ps.shader_entry)
7812 shader_glsl_invalidate_contexts_program(device, entry);
7813 delete_glsl_program_entry(priv, gl_info, entry);
7816 break;
7819 case WINED3D_SHADER_TYPE_VERTEX:
7821 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
7823 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7825 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
7826 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7827 checkGLcall("glDeleteShader");
7829 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
7831 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7832 struct glsl_shader_prog_link, vs.shader_entry)
7834 shader_glsl_invalidate_contexts_program(device, entry);
7835 delete_glsl_program_entry(priv, gl_info, entry);
7838 break;
7841 case WINED3D_SHADER_TYPE_GEOMETRY:
7843 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
7845 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7847 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
7848 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7849 checkGLcall("glDeleteShader");
7851 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
7853 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7854 struct glsl_shader_prog_link, gs.shader_entry)
7856 shader_glsl_invalidate_contexts_program(device, entry);
7857 delete_glsl_program_entry(priv, gl_info, entry);
7860 break;
7863 default:
7864 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
7865 break;
7869 HeapFree(GetProcessHeap(), 0, shader->backend_data);
7870 shader->backend_data = NULL;
7872 context_release(context);
7875 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
7877 const struct glsl_program_key *k = key;
7878 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
7879 const struct glsl_shader_prog_link, program_lookup_entry);
7881 if (k->vs_id > prog->vs.id) return 1;
7882 else if (k->vs_id < prog->vs.id) return -1;
7884 if (k->gs_id > prog->gs.id) return 1;
7885 else if (k->gs_id < prog->gs.id) return -1;
7887 if (k->ps_id > prog->ps.id) return 1;
7888 else if (k->ps_id < prog->ps.id) return -1;
7890 return 0;
7893 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
7895 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
7896 + constant_count * sizeof(*heap->contained)
7897 + constant_count * sizeof(*heap->positions);
7898 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
7900 if (!mem)
7902 ERR("Failed to allocate memory\n");
7903 return FALSE;
7906 heap->entries = mem;
7907 heap->entries[1].version = 0;
7908 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
7909 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
7910 heap->positions = (unsigned int *)(heap->contained + constant_count);
7911 heap->size = 1;
7913 return TRUE;
7916 static void constant_heap_free(struct constant_heap *heap)
7918 HeapFree(GetProcessHeap(), 0, heap->entries);
7921 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
7923 wined3d_rb_alloc,
7924 wined3d_rb_realloc,
7925 wined3d_rb_free,
7926 glsl_program_key_compare,
7929 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
7930 const struct fragment_pipeline *fragment_pipe)
7932 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7933 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
7934 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
7935 gl_info->limits.glsl_ps_float_constants)) + 1;
7936 struct fragment_caps fragment_caps;
7937 void *vertex_priv, *fragment_priv;
7939 string_buffer_list_init(&priv->string_buffers);
7941 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
7943 ERR("Failed to initialize vertex pipe.\n");
7944 HeapFree(GetProcessHeap(), 0, priv);
7945 return E_FAIL;
7948 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
7950 ERR("Failed to initialize fragment pipe.\n");
7951 vertex_pipe->vp_free(device);
7952 HeapFree(GetProcessHeap(), 0, priv);
7953 return E_FAIL;
7956 if (!string_buffer_init(&priv->shader_buffer))
7958 ERR("Failed to initialize shader buffer.\n");
7959 goto fail;
7962 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
7963 if (!priv->stack)
7965 ERR("Failed to allocate memory.\n");
7966 goto fail;
7969 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
7971 ERR("Failed to initialize vertex shader constant heap\n");
7972 goto fail;
7975 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
7977 ERR("Failed to initialize pixel shader constant heap\n");
7978 goto fail;
7981 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
7983 ERR("Failed to initialize rbtree.\n");
7984 goto fail;
7987 priv->next_constant_version = 1;
7988 priv->vertex_pipe = vertex_pipe;
7989 priv->fragment_pipe = fragment_pipe;
7990 fragment_pipe->get_caps(gl_info, &fragment_caps);
7991 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
7992 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
7994 device->vertex_priv = vertex_priv;
7995 device->fragment_priv = fragment_priv;
7996 device->shader_priv = priv;
7998 return WINED3D_OK;
8000 fail:
8001 constant_heap_free(&priv->pconst_heap);
8002 constant_heap_free(&priv->vconst_heap);
8003 HeapFree(GetProcessHeap(), 0, priv->stack);
8004 string_buffer_free(&priv->shader_buffer);
8005 fragment_pipe->free_private(device);
8006 vertex_pipe->vp_free(device);
8007 HeapFree(GetProcessHeap(), 0, priv);
8008 return E_OUTOFMEMORY;
8011 /* Context activation is done by the caller. */
8012 static void shader_glsl_free(struct wined3d_device *device)
8014 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
8015 struct shader_glsl_priv *priv = device->shader_priv;
8016 int i;
8018 for (i = 0; i < WINED3D_GL_RES_TYPE_COUNT; ++i)
8020 if (priv->depth_blt_program_full[i])
8022 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
8024 if (priv->depth_blt_program_masked[i])
8026 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
8030 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
8031 constant_heap_free(&priv->pconst_heap);
8032 constant_heap_free(&priv->vconst_heap);
8033 HeapFree(GetProcessHeap(), 0, priv->stack);
8034 string_buffer_list_cleanup(&priv->string_buffers);
8035 string_buffer_free(&priv->shader_buffer);
8036 priv->fragment_pipe->free_private(device);
8037 priv->vertex_pipe->vp_free(device);
8039 HeapFree(GetProcessHeap(), 0, device->shader_priv);
8040 device->shader_priv = NULL;
8043 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
8045 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
8046 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
8049 static void shader_glsl_free_context_data(struct wined3d_context *context)
8051 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
8054 static void shader_glsl_init_context_state(struct wined3d_context *context)
8056 const struct wined3d_gl_info *gl_info = context->gl_info;
8058 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
8059 checkGLcall("GL_PROGRAM_POINT_SIZE");
8062 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
8064 UINT shader_model;
8066 /* FIXME: Check for the specific extensions required for SM5 support
8067 * (ARB_compute_shader, ARB_tessellation_shader, ARB_gpu_shader5, ...) as
8068 * soon as we introduce them, adjusting the GL / GLSL version checks
8069 * accordingly. */
8070 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 30) && gl_info->supported[WINED3D_GL_VERSION_4_3])
8071 shader_model = 5;
8072 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50) && gl_info->supported[WINED3D_GL_VERSION_3_2]
8073 && gl_info->supported[ARB_SHADER_BIT_ENCODING] && gl_info->supported[ARB_SAMPLER_OBJECTS]
8074 && gl_info->supported[ARB_TEXTURE_SWIZZLE])
8075 shader_model = 4;
8076 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
8077 * texldd and texldl instructions. */
8078 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
8079 shader_model = 3;
8080 else
8081 shader_model = 2;
8082 TRACE("Shader model %u.\n", shader_model);
8084 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
8085 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
8086 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
8087 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
8088 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
8090 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
8091 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
8092 caps->varying_count = gl_info->limits.glsl_varyings;
8094 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
8095 * Direct3D minimum requirement.
8097 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
8098 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
8100 * The problem is that the refrast clamps temporary results in the shader to
8101 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
8102 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
8103 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
8104 * offer a way to query this.
8106 if (shader_model >= 4)
8107 caps->ps_1x_max_value = FLT_MAX;
8108 else
8109 caps->ps_1x_max_value = 1024.0f;
8111 /* Ideally we'd only set caps like sRGB writes here if supported by both
8112 * the shader backend and the fragment pipe, but we can get called before
8113 * shader_glsl_alloc(). */
8114 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
8115 | WINED3D_SHADER_CAP_SRGB_WRITE;
8118 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
8120 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
8122 TRACE("Checking support for fixup:\n");
8123 dump_color_fixup_desc(fixup);
8126 /* We support everything except YUV conversions. */
8127 if (!is_complex_fixup(fixup))
8129 TRACE("[OK]\n");
8130 return TRUE;
8133 TRACE("[FAILED]\n");
8134 return FALSE;
8137 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
8139 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
8140 /* WINED3DSIH_ADD */ shader_glsl_binop,
8141 /* WINED3DSIH_AND */ shader_glsl_binop,
8142 /* WINED3DSIH_BEM */ shader_glsl_bem,
8143 /* WINED3DSIH_BREAK */ shader_glsl_break,
8144 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
8145 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
8146 /* WINED3DSIH_CALL */ shader_glsl_call,
8147 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
8148 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
8149 /* WINED3DSIH_CND */ shader_glsl_cnd,
8150 /* WINED3DSIH_CRS */ shader_glsl_cross,
8151 /* WINED3DSIH_CUT */ shader_glsl_cut,
8152 /* WINED3DSIH_DCL */ shader_glsl_nop,
8153 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
8154 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
8155 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ NULL,
8156 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ NULL,
8157 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ NULL,
8158 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
8159 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ NULL,
8160 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
8161 /* WINED3DSIH_DCL_INPUT_PS */ NULL,
8162 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
8163 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
8164 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
8165 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
8166 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
8167 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ NULL,
8168 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
8169 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
8170 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ NULL,
8171 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
8172 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
8173 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ NULL,
8174 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ NULL,
8175 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ NULL,
8176 /* WINED3DSIH_DCL_UAV_TYPED */ NULL,
8177 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
8178 /* WINED3DSIH_DEF */ shader_glsl_nop,
8179 /* WINED3DSIH_DEFB */ shader_glsl_nop,
8180 /* WINED3DSIH_DEFI */ shader_glsl_nop,
8181 /* WINED3DSIH_DIV */ shader_glsl_binop,
8182 /* WINED3DSIH_DP2 */ shader_glsl_dot,
8183 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
8184 /* WINED3DSIH_DP3 */ shader_glsl_dot,
8185 /* WINED3DSIH_DP4 */ shader_glsl_dot,
8186 /* WINED3DSIH_DST */ shader_glsl_dst,
8187 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
8188 /* WINED3DSIH_DSX_COARSE */ NULL,
8189 /* WINED3DSIH_DSX_FINE */ NULL,
8190 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
8191 /* WINED3DSIH_DSY_COARSE */ NULL,
8192 /* WINED3DSIH_DSY_FINE */ NULL,
8193 /* WINED3DSIH_ELSE */ shader_glsl_else,
8194 /* WINED3DSIH_EMIT */ shader_glsl_emit,
8195 /* WINED3DSIH_ENDIF */ shader_glsl_end,
8196 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
8197 /* WINED3DSIH_ENDREP */ shader_glsl_end,
8198 /* WINED3DSIH_EQ */ shader_glsl_relop,
8199 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
8200 /* WINED3DSIH_EXPP */ shader_glsl_expp,
8201 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
8202 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
8203 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
8204 /* WINED3DSIH_GE */ shader_glsl_relop,
8205 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ NULL,
8206 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
8207 /* WINED3DSIH_HS_FORK_PHASE */ NULL,
8208 /* WINED3DSIH_HS_JOIN_PHASE */ NULL,
8209 /* WINED3DSIH_IADD */ shader_glsl_binop,
8210 /* WINED3DSIH_IEQ */ shader_glsl_relop,
8211 /* WINED3DSIH_IF */ shader_glsl_if,
8212 /* WINED3DSIH_IFC */ shader_glsl_ifc,
8213 /* WINED3DSIH_IGE */ shader_glsl_relop,
8214 /* WINED3DSIH_ILT */ shader_glsl_relop,
8215 /* WINED3DSIH_IMAD */ shader_glsl_mad,
8216 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
8217 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
8218 /* WINED3DSIH_IMUL */ shader_glsl_imul,
8219 /* WINED3DSIH_INE */ shader_glsl_relop,
8220 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
8221 /* WINED3DSIH_ISHL */ shader_glsl_binop,
8222 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
8223 /* WINED3DSIH_LABEL */ shader_glsl_label,
8224 /* WINED3DSIH_LD */ shader_glsl_ld,
8225 /* WINED3DSIH_LD2DMS */ NULL,
8226 /* WINED3DSIH_LD_STRUCTURED */ NULL,
8227 /* WINED3DSIH_LIT */ shader_glsl_lit,
8228 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
8229 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
8230 /* WINED3DSIH_LOOP */ shader_glsl_loop,
8231 /* WINED3DSIH_LRP */ shader_glsl_lrp,
8232 /* WINED3DSIH_LT */ shader_glsl_relop,
8233 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
8234 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
8235 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
8236 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
8237 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
8238 /* WINED3DSIH_MAD */ shader_glsl_mad,
8239 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
8240 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
8241 /* WINED3DSIH_MOV */ shader_glsl_mov,
8242 /* WINED3DSIH_MOVA */ shader_glsl_mov,
8243 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
8244 /* WINED3DSIH_MUL */ shader_glsl_binop,
8245 /* WINED3DSIH_NE */ shader_glsl_relop,
8246 /* WINED3DSIH_NOP */ shader_glsl_nop,
8247 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
8248 /* WINED3DSIH_NRM */ shader_glsl_nrm,
8249 /* WINED3DSIH_OR */ shader_glsl_binop,
8250 /* WINED3DSIH_PHASE */ shader_glsl_nop,
8251 /* WINED3DSIH_POW */ shader_glsl_pow,
8252 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
8253 /* WINED3DSIH_REP */ shader_glsl_rep,
8254 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
8255 /* WINED3DSIH_RET */ shader_glsl_ret,
8256 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
8257 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
8258 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
8259 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
8260 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
8261 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
8262 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
8263 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
8264 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
8265 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
8266 /* WINED3DSIH_SETP */ NULL,
8267 /* WINED3DSIH_SGE */ shader_glsl_compare,
8268 /* WINED3DSIH_SGN */ shader_glsl_sgn,
8269 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
8270 /* WINED3DSIH_SLT */ shader_glsl_compare,
8271 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
8272 /* WINED3DSIH_STORE_UAV_TYPED */ NULL,
8273 /* WINED3DSIH_SUB */ shader_glsl_binop,
8274 /* WINED3DSIH_TEX */ shader_glsl_tex,
8275 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
8276 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
8277 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
8278 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
8279 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
8280 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
8281 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
8282 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
8283 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
8284 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
8285 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
8286 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
8287 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
8288 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
8289 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
8290 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
8291 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
8292 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
8293 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
8294 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
8295 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
8296 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
8297 /* WINED3DSIH_UGE */ shader_glsl_relop,
8298 /* WINED3DSIH_ULT */ shader_glsl_relop,
8299 /* WINED3DSIH_USHR */ shader_glsl_binop,
8300 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
8301 /* WINED3DSIH_XOR */ shader_glsl_binop,
8304 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
8305 SHADER_HANDLER hw_fct;
8307 /* Select handler */
8308 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
8310 /* Unhandled opcode */
8311 if (!hw_fct)
8313 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
8314 return;
8316 hw_fct(ins);
8318 shader_glsl_add_instruction_modifiers(ins);
8321 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
8323 struct shader_glsl_priv *priv = shader_priv;
8325 return priv->ffp_proj_control;
8328 const struct wined3d_shader_backend_ops glsl_shader_backend =
8330 shader_glsl_handle_instruction,
8331 shader_glsl_select,
8332 shader_glsl_disable,
8333 shader_glsl_select_depth_blt,
8334 shader_glsl_deselect_depth_blt,
8335 shader_glsl_update_float_vertex_constants,
8336 shader_glsl_update_float_pixel_constants,
8337 shader_glsl_load_constants,
8338 shader_glsl_destroy,
8339 shader_glsl_alloc,
8340 shader_glsl_free,
8341 shader_glsl_allocate_context_data,
8342 shader_glsl_free_context_data,
8343 shader_glsl_init_context_state,
8344 shader_glsl_get_caps,
8345 shader_glsl_color_fixup_supported,
8346 shader_glsl_has_ffp_proj_control,
8349 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
8351 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
8353 caps->xyzrhw = TRUE;
8354 caps->emulated_flatshading = !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
8355 caps->ffp_generic_attributes = TRUE;
8356 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
8357 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
8358 caps->max_vertex_blend_matrix_index = 0;
8359 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
8360 | WINED3DVTXPCAPS_MATERIALSOURCE7
8361 | WINED3DVTXPCAPS_VERTEXFOG
8362 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
8363 | WINED3DVTXPCAPS_POSITIONALLIGHTS
8364 | WINED3DVTXPCAPS_LOCALVIEWER
8365 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
8366 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
8367 caps->max_user_clip_planes = gl_info->limits.clipplanes;
8368 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
8371 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
8373 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8374 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8375 return 0;
8378 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8380 struct shader_glsl_priv *priv;
8382 if (shader_backend == &glsl_shader_backend)
8384 priv = shader_priv;
8386 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
8388 ERR("Failed to initialize rbtree.\n");
8389 return NULL;
8392 return priv;
8395 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
8397 return NULL;
8400 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
8402 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8403 struct glsl_ffp_vertex_shader, desc.entry);
8404 struct glsl_shader_prog_link *program, *program2;
8405 struct glsl_ffp_destroy_ctx *ctx = context;
8407 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8408 struct glsl_shader_prog_link, vs.shader_entry)
8410 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8412 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8413 HeapFree(GetProcessHeap(), 0, shader);
8416 /* Context activation is done by the caller. */
8417 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
8419 struct shader_glsl_priv *priv = device->vertex_priv;
8420 struct glsl_ffp_destroy_ctx ctx;
8422 ctx.priv = priv;
8423 ctx.gl_info = &device->adapter->gl_info;
8424 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
8427 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
8428 const struct wined3d_state *state, DWORD state_id) {}
8430 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
8431 const struct wined3d_state *state, DWORD state_id)
8433 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8436 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
8437 const struct wined3d_state *state, DWORD state_id)
8439 const struct wined3d_gl_info *gl_info = context->gl_info;
8440 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
8441 BOOL transformed = context->stream_info.position_transformed;
8442 BOOL wasrhw = context->last_was_rhw;
8443 unsigned int i;
8445 context->last_was_rhw = transformed;
8447 /* If the vertex declaration contains a transformed position attribute,
8448 * the draw uses the fixed function vertex pipeline regardless of any
8449 * vertex shader set by the application. */
8450 if (transformed != wasrhw)
8451 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8453 if (!use_vs(state))
8455 if (context->last_was_vshader)
8457 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8458 clipplane(context, state, STATE_CLIPPLANE(i));
8461 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8463 /* Because of settings->texcoords, we have to regenerate the vertex
8464 * shader on a vdecl change if there aren't enough varyings to just
8465 * always output all the texture coordinates. */
8466 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
8467 || normal != context->last_was_normal)
8468 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8470 if (use_ps(state)
8471 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
8472 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
8473 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8475 else
8477 if (!context->last_was_vshader)
8479 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
8480 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8481 clipplane(context, state, STATE_CLIPPLANE(i));
8485 context->last_was_vshader = use_vs(state);
8486 context->last_was_normal = normal;
8489 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
8490 const struct wined3d_state *state, DWORD state_id)
8492 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8493 /* Different vertex shaders potentially require a different vertex attributes setup. */
8494 if (!isStateDirty(context, STATE_VDECL))
8495 context_apply_state(context, state, STATE_VDECL);
8498 static void glsl_vertex_pipe_world(struct wined3d_context *context,
8499 const struct wined3d_state *state, DWORD state_id)
8501 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
8504 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
8505 const struct wined3d_state *state, DWORD state_id)
8507 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8510 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
8512 const struct wined3d_gl_info *gl_info = context->gl_info;
8513 unsigned int k;
8515 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
8516 | WINED3D_SHADER_CONST_FFP_LIGHTS
8517 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8519 for (k = 0; k < gl_info->limits.clipplanes; ++k)
8521 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
8522 clipplane(context, state, STATE_CLIPPLANE(k));
8526 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
8527 const struct wined3d_state *state, DWORD state_id)
8529 /* Table fog behavior depends on the projection matrix. */
8530 if (state->render_states[WINED3D_RS_FOGENABLE]
8531 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
8532 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8533 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
8536 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
8537 const struct wined3d_state *state, DWORD state_id)
8539 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
8540 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
8541 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
8542 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
8543 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8544 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
8547 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
8548 const struct wined3d_state *state, DWORD state_id)
8550 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8553 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
8554 const struct wined3d_state *state, DWORD state_id)
8556 DWORD sampler = state_id - STATE_SAMPLER(0);
8557 const struct wined3d_texture *texture = state->textures[sampler];
8558 BOOL np2;
8560 if (!texture)
8561 return;
8563 if (sampler >= MAX_TEXTURES)
8564 return;
8566 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
8567 || context->lastWasPow2Texture & (1u << sampler))
8569 if (np2)
8570 context->lastWasPow2Texture |= 1u << sampler;
8571 else
8572 context->lastWasPow2Texture &= ~(1u << sampler);
8574 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8578 static void glsl_vertex_pipe_material(struct wined3d_context *context,
8579 const struct wined3d_state *state, DWORD state_id)
8581 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
8584 static void glsl_vertex_pipe_light(struct wined3d_context *context,
8585 const struct wined3d_state *state, DWORD state_id)
8587 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
8590 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
8591 const struct wined3d_state *state, DWORD state_id)
8593 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8596 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
8597 const struct wined3d_state *state, DWORD state_id)
8599 if (!use_vs(state))
8600 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8603 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
8604 const struct wined3d_state *state, DWORD state_id)
8606 static unsigned int once;
8608 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
8609 FIXME("Non-point sprite points not supported in core profile.\n");
8612 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
8613 const struct wined3d_state *state, DWORD state_id)
8615 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
8618 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
8620 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8621 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
8622 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
8623 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
8624 /* Clip planes */
8625 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
8626 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
8627 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
8628 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
8629 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
8630 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
8631 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
8632 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
8633 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
8634 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
8635 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
8636 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
8637 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
8638 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
8639 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
8640 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
8641 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
8642 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
8643 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
8644 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
8645 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
8646 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
8647 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
8648 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
8649 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
8650 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
8651 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
8652 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
8653 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
8654 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
8655 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
8656 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
8657 /* Lights */
8658 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8659 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8660 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8661 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8662 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8663 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8664 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8665 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8666 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8667 /* Viewport */
8668 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
8669 /* Transform states */
8670 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
8671 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
8672 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8673 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8674 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8675 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8676 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8677 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8678 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8679 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8680 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
8681 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8682 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8683 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8684 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8685 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8686 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8687 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8688 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8689 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8690 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8691 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8692 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8693 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8694 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8695 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8696 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8697 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8698 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8699 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8700 /* Fog */
8701 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8702 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8703 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8704 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8705 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
8706 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
8707 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8708 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8709 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8710 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8711 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8712 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8713 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8714 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8715 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8716 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8717 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
8718 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
8719 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
8720 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
8721 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
8722 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
8723 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8724 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8725 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8726 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
8727 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8728 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8729 /* NP2 texture matrix fixups. They are not needed if
8730 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
8731 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
8732 * matrix. */
8733 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8734 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8735 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8736 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8737 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8738 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8739 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8740 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8741 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8742 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8743 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8744 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8745 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8746 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8747 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8748 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8749 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8750 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8751 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8752 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8753 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8754 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8755 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8756 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8757 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8758 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_LEGACY_CONTEXT },
8759 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GL_EXT_NONE },
8760 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
8763 /* TODO:
8764 * - Implement vertex tweening. */
8765 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
8767 glsl_vertex_pipe_vp_enable,
8768 glsl_vertex_pipe_vp_get_caps,
8769 glsl_vertex_pipe_vp_get_emul_mask,
8770 glsl_vertex_pipe_vp_alloc,
8771 glsl_vertex_pipe_vp_free,
8772 glsl_vertex_pipe_vp_states,
8775 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
8777 /* Nothing to do. */
8780 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
8782 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
8783 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
8784 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
8785 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
8786 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
8787 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
8788 | WINED3DTEXOPCAPS_SELECTARG1
8789 | WINED3DTEXOPCAPS_SELECTARG2
8790 | WINED3DTEXOPCAPS_MODULATE4X
8791 | WINED3DTEXOPCAPS_MODULATE2X
8792 | WINED3DTEXOPCAPS_MODULATE
8793 | WINED3DTEXOPCAPS_ADDSIGNED2X
8794 | WINED3DTEXOPCAPS_ADDSIGNED
8795 | WINED3DTEXOPCAPS_ADD
8796 | WINED3DTEXOPCAPS_SUBTRACT
8797 | WINED3DTEXOPCAPS_ADDSMOOTH
8798 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
8799 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
8800 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
8801 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
8802 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
8803 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
8804 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
8805 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
8806 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
8807 | WINED3DTEXOPCAPS_DOTPRODUCT3
8808 | WINED3DTEXOPCAPS_MULTIPLYADD
8809 | WINED3DTEXOPCAPS_LERP
8810 | WINED3DTEXOPCAPS_BUMPENVMAP
8811 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
8812 caps->MaxTextureBlendStages = 8;
8813 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
8816 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
8818 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8819 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8820 return 0;
8823 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8825 struct shader_glsl_priv *priv;
8827 if (shader_backend == &glsl_shader_backend)
8829 priv = shader_priv;
8831 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
8833 ERR("Failed to initialize rbtree.\n");
8834 return NULL;
8837 return priv;
8840 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
8842 return NULL;
8845 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
8847 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8848 struct glsl_ffp_fragment_shader, entry.entry);
8849 struct glsl_shader_prog_link *program, *program2;
8850 struct glsl_ffp_destroy_ctx *ctx = context;
8852 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8853 struct glsl_shader_prog_link, ps.shader_entry)
8855 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8857 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8858 HeapFree(GetProcessHeap(), 0, shader);
8861 /* Context activation is done by the caller. */
8862 static void glsl_fragment_pipe_free(struct wined3d_device *device)
8864 struct shader_glsl_priv *priv = device->fragment_priv;
8865 struct glsl_ffp_destroy_ctx ctx;
8867 ctx.priv = priv;
8868 ctx.gl_info = &device->adapter->gl_info;
8869 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
8872 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
8873 const struct wined3d_state *state, DWORD state_id)
8875 context->last_was_pshader = use_ps(state);
8877 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8880 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
8881 const struct wined3d_state *state, DWORD state_id)
8883 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8886 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
8887 const struct wined3d_state *state, DWORD state_id)
8889 BOOL use_vshader = use_vs(state);
8890 enum fogsource new_source;
8891 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
8892 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
8894 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8896 if (!state->render_states[WINED3D_RS_FOGENABLE])
8897 return;
8899 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
8901 if (use_vshader)
8902 new_source = FOGSOURCE_VS;
8903 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
8904 new_source = FOGSOURCE_COORD;
8905 else
8906 new_source = FOGSOURCE_FFP;
8908 else
8910 new_source = FOGSOURCE_FFP;
8913 if (new_source != context->fog_source || fogstart == fogend)
8915 context->fog_source = new_source;
8916 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8920 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
8921 const struct wined3d_state *state, DWORD state_id)
8923 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
8924 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
8925 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8927 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
8928 glsl_fragment_pipe_fog(context, state, state_id);
8931 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
8932 const struct wined3d_state *state, DWORD state_id)
8934 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
8935 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
8936 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8939 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
8940 const struct wined3d_state *state, DWORD state_id)
8942 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8945 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
8946 const struct wined3d_state *state, DWORD state_id)
8948 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
8951 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
8952 const struct wined3d_state *state, DWORD state_id)
8954 const struct wined3d_gl_info *gl_info = context->gl_info;
8955 int glParm;
8956 float ref;
8958 TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
8960 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
8962 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
8963 checkGLcall("glEnable GL_ALPHA_TEST");
8965 else
8967 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
8968 checkGLcall("glDisable GL_ALPHA_TEST");
8969 return;
8972 ref = ((float)state->render_states[WINED3D_RS_ALPHAREF]) / 255.0f;
8973 glParm = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
8975 if (glParm)
8977 gl_info->gl_ops.gl.p_glAlphaFunc(glParm, ref);
8978 checkGLcall("glAlphaFunc");
8982 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
8983 const struct wined3d_state *state, DWORD state_id)
8985 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
8988 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
8989 const struct wined3d_state *state, DWORD state_id)
8991 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
8994 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
8996 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8997 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
8998 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8999 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9000 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9001 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9002 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9003 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9004 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9005 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9006 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9007 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9008 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9009 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9010 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9011 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9012 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9013 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9014 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9015 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9016 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9017 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9018 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9019 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9020 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9021 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9022 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9023 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9024 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9025 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9026 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9027 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9028 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9029 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9030 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9031 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9032 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9033 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9034 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9035 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9036 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9037 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9038 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9039 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9040 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9041 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9042 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9043 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9044 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9045 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9046 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9047 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9048 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9049 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9050 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9051 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9052 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9053 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9054 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9055 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9056 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9057 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9058 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9059 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9060 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9061 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9062 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9063 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9064 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9065 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9066 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9067 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9068 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9069 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9070 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9071 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9072 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
9073 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
9074 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_EXT_NONE },
9075 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9076 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
9077 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
9078 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9079 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9080 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9081 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
9082 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
9083 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9084 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9085 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9086 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
9087 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
9088 {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 },
9089 {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 },
9090 {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 },
9091 {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 },
9092 {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 },
9093 {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 },
9094 {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 },
9095 {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 },
9096 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9097 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9098 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9099 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9100 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9101 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9102 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9103 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9104 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9105 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9106 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_LEGACY_CONTEXT},
9107 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GL_EXT_NONE },
9108 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
9111 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
9113 return TRUE;
9116 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
9120 const struct fragment_pipeline glsl_fragment_pipe =
9122 glsl_fragment_pipe_enable,
9123 glsl_fragment_pipe_get_caps,
9124 glsl_fragment_pipe_get_emul_mask,
9125 glsl_fragment_pipe_alloc,
9126 glsl_fragment_pipe_free,
9127 glsl_fragment_pipe_alloc_context_data,
9128 glsl_fragment_pipe_free_context_data,
9129 shader_glsl_color_fixup_supported,
9130 glsl_fragment_pipe_state_template,