wined3d: Use unsigned mask constants in shifts.
[wine.git] / dlls / wined3d / glsl_shader.c
blob6de70bacd3133d899bdb613363dc61c87d5a8224
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 0x1
48 #define WINED3D_GLSL_SAMPLE_NPOT 0x2
49 #define WINED3D_GLSL_SAMPLE_LOD 0x4
50 #define WINED3D_GLSL_SAMPLE_GRAD 0x8
52 struct glsl_dst_param
54 char reg_name[150];
55 char mask_str[6];
58 struct glsl_src_param
60 char reg_name[150];
61 char param_str[200];
64 struct glsl_sample_function
66 struct wined3d_string_buffer *name;
67 DWORD coord_mask;
68 enum wined3d_data_type data_type;
71 enum heap_node_op
73 HEAP_NODE_TRAVERSE_LEFT,
74 HEAP_NODE_TRAVERSE_RIGHT,
75 HEAP_NODE_POP,
78 struct constant_entry
80 unsigned int idx;
81 unsigned int version;
84 struct constant_heap
86 struct constant_entry *entries;
87 BOOL *contained;
88 unsigned int *positions;
89 unsigned int size;
92 /* GLSL shader private data */
93 struct shader_glsl_priv {
94 struct wined3d_string_buffer shader_buffer;
95 struct wined3d_string_buffer_list string_buffers;
96 struct wine_rb_tree program_lookup;
97 struct constant_heap vconst_heap;
98 struct constant_heap pconst_heap;
99 unsigned char *stack;
100 GLuint depth_blt_program_full[WINED3D_GL_RES_TYPE_COUNT];
101 GLuint depth_blt_program_masked[WINED3D_GL_RES_TYPE_COUNT];
102 UINT next_constant_version;
104 const struct wined3d_vertex_pipe_ops *vertex_pipe;
105 const struct fragment_pipeline *fragment_pipe;
106 struct wine_rb_tree ffp_vertex_shaders;
107 struct wine_rb_tree ffp_fragment_shaders;
108 BOOL ffp_proj_control;
109 BOOL legacy_lighting;
112 struct glsl_vs_program
114 struct list shader_entry;
115 GLuint id;
116 GLenum vertex_color_clamp;
117 GLint *uniform_f_locations;
118 GLint uniform_i_locations[MAX_CONST_I];
119 GLint uniform_b_locations[MAX_CONST_B];
120 GLint pos_fixup_location;
122 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
123 GLint projection_matrix_location;
124 GLint normal_matrix_location;
125 GLint texture_matrix_location[MAX_TEXTURES];
126 GLint material_ambient_location;
127 GLint material_diffuse_location;
128 GLint material_specular_location;
129 GLint material_emissive_location;
130 GLint material_shininess_location;
131 GLint light_ambient_location;
132 struct
134 GLint diffuse;
135 GLint specular;
136 GLint ambient;
137 GLint position;
138 GLint direction;
139 GLint range;
140 GLint falloff;
141 GLint c_att;
142 GLint l_att;
143 GLint q_att;
144 GLint cos_htheta;
145 GLint cos_hphi;
146 } light_location[MAX_ACTIVE_LIGHTS];
147 GLint pointsize_location;
148 GLint pointsize_min_location;
149 GLint pointsize_max_location;
150 GLint pointsize_c_att_location;
151 GLint pointsize_l_att_location;
152 GLint pointsize_q_att_location;
155 struct glsl_gs_program
157 struct list shader_entry;
158 GLuint id;
161 struct glsl_ps_program
163 struct list shader_entry;
164 GLuint id;
165 GLint *uniform_f_locations;
166 GLint uniform_i_locations[MAX_CONST_I];
167 GLint uniform_b_locations[MAX_CONST_B];
168 GLint bumpenv_mat_location[MAX_TEXTURES];
169 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
170 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
171 GLint tss_constant_location[MAX_TEXTURES];
172 GLint tex_factor_location;
173 GLint specular_enable_location;
174 GLint fog_color_location;
175 GLint fog_density_location;
176 GLint fog_end_location;
177 GLint fog_scale_location;
178 GLint ycorrection_location;
179 GLint np2_fixup_location;
180 GLint color_key_location;
181 const struct ps_np2fixup_info *np2_fixup_info;
184 /* Struct to maintain data about a linked GLSL program */
185 struct glsl_shader_prog_link
187 struct wine_rb_entry program_lookup_entry;
188 struct glsl_vs_program vs;
189 struct glsl_gs_program gs;
190 struct glsl_ps_program ps;
191 GLuint id;
192 DWORD constant_update_mask;
193 UINT constant_version;
196 struct glsl_program_key
198 GLuint vs_id;
199 GLuint gs_id;
200 GLuint ps_id;
203 struct shader_glsl_ctx_priv {
204 const struct vs_compile_args *cur_vs_args;
205 const struct ps_compile_args *cur_ps_args;
206 struct ps_np2fixup_info *cur_np2fixup_info;
207 struct wined3d_string_buffer_list *string_buffers;
210 struct glsl_context_data
212 struct glsl_shader_prog_link *glsl_program;
215 struct glsl_ps_compiled_shader
217 struct ps_compile_args args;
218 struct ps_np2fixup_info np2fixup;
219 GLuint id;
222 struct glsl_vs_compiled_shader
224 struct vs_compile_args args;
225 GLuint id;
228 struct glsl_gs_compiled_shader
230 GLuint id;
233 struct glsl_shader_private
235 union
237 struct glsl_vs_compiled_shader *vs;
238 struct glsl_gs_compiled_shader *gs;
239 struct glsl_ps_compiled_shader *ps;
240 } gl_shaders;
241 UINT num_gl_shaders, shader_array_size;
244 struct glsl_ffp_vertex_shader
246 struct wined3d_ffp_vs_desc desc;
247 GLuint id;
248 struct list linked_programs;
251 struct glsl_ffp_fragment_shader
253 struct ffp_frag_desc entry;
254 GLuint id;
255 struct list linked_programs;
258 struct glsl_ffp_destroy_ctx
260 struct shader_glsl_priv *priv;
261 const struct wined3d_gl_info *gl_info;
264 static const char *debug_gl_shader_type(GLenum type)
266 switch (type)
268 #define WINED3D_TO_STR(u) case u: return #u
269 WINED3D_TO_STR(GL_VERTEX_SHADER);
270 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
271 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
272 #undef WINED3D_TO_STR
273 default:
274 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
278 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
280 switch (type)
282 case WINED3D_SHADER_TYPE_VERTEX:
283 return "vs";
285 case WINED3D_SHADER_TYPE_GEOMETRY:
286 return "gs";
288 case WINED3D_SHADER_TYPE_PIXEL:
289 return "ps";
291 default:
292 FIXME("Unhandled shader type %#x.\n", type);
293 return "unknown";
297 static const char *shader_glsl_get_version(const struct wined3d_gl_info *gl_info,
298 const struct wined3d_shader_version *version)
300 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
301 return "#version 150";
302 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30) && version && version->major >= 4)
303 return "#version 130";
304 else
305 return "#version 120";
308 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
310 char str[4][17];
312 wined3d_ftoa(values[0], str[0]);
313 wined3d_ftoa(values[1], str[1]);
314 wined3d_ftoa(values[2], str[2]);
315 wined3d_ftoa(values[3], str[3]);
316 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
319 static const char *get_info_log_line(const char **ptr)
321 const char *p, *q;
323 p = *ptr;
324 if (!(q = strstr(p, "\n")))
326 if (!*p) return NULL;
327 *ptr += strlen(p);
328 return p;
330 *ptr = q + 1;
332 return p;
335 /* Context activation is done by the caller. */
336 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
338 int length = 0;
339 char *log;
341 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
342 return;
344 if (program)
345 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
346 else
347 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
349 /* A size of 1 is just a null-terminated string, so the log should be bigger than
350 * that if there are errors. */
351 if (length > 1)
353 const char *ptr, *line;
355 log = HeapAlloc(GetProcessHeap(), 0, length);
356 /* The info log is supposed to be zero-terminated, but at least some
357 * versions of fglrx don't terminate the string properly. The reported
358 * length does include the terminator, so explicitly set it to zero
359 * here. */
360 log[length - 1] = 0;
361 if (program)
362 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
363 else
364 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
366 ptr = log;
367 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
369 WARN("Info log received from GLSL shader #%u:\n", id);
370 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
372 else
374 FIXME("Info log received from GLSL shader #%u:\n", id);
375 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
377 HeapFree(GetProcessHeap(), 0, log);
381 /* Context activation is done by the caller. */
382 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
384 const char *ptr, *line;
386 TRACE("Compiling shader object %u.\n", shader);
388 if (TRACE_ON(d3d_shader))
390 ptr = src;
391 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
394 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
395 checkGLcall("glShaderSource");
396 GL_EXTCALL(glCompileShader(shader));
397 checkGLcall("glCompileShader");
398 print_glsl_info_log(gl_info, shader, FALSE);
401 /* Context activation is done by the caller. */
402 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
404 GLint i, shader_count, source_size = -1;
405 GLuint *shaders;
406 char *source = NULL;
408 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
409 shaders = HeapAlloc(GetProcessHeap(), 0, shader_count * sizeof(*shaders));
410 if (!shaders)
412 ERR("Failed to allocate shader array memory.\n");
413 return;
416 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
417 for (i = 0; i < shader_count; ++i)
419 const char *ptr, *line;
420 GLint tmp;
422 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
424 if (source_size < tmp)
426 HeapFree(GetProcessHeap(), 0, source);
428 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
429 if (!source)
431 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
432 HeapFree(GetProcessHeap(), 0, shaders);
433 return;
435 source_size = tmp;
438 FIXME("Shader %u:\n", shaders[i]);
439 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
440 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
441 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
442 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
443 FIXME("\n");
445 ptr = source;
446 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
447 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
448 FIXME("\n");
451 HeapFree(GetProcessHeap(), 0, source);
452 HeapFree(GetProcessHeap(), 0, shaders);
455 /* Context activation is done by the caller. */
456 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
458 GLint tmp;
460 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
461 return;
463 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
464 if (!tmp)
466 FIXME("Program %u link status invalid.\n", program);
467 shader_glsl_dump_program_source(gl_info, program);
470 print_glsl_info_log(gl_info, program, TRUE);
473 /* Context activation is done by the caller. */
474 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
475 struct shader_glsl_priv *priv, const DWORD *tex_unit_map, GLuint program_id)
477 unsigned int mapped_unit;
478 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
479 const char *prefix;
480 unsigned int i, j;
481 GLint name_loc;
483 static const struct
485 enum wined3d_shader_type type;
486 unsigned int base_idx;
487 unsigned int count;
489 sampler_info[] =
491 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
492 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
495 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
497 prefix = shader_glsl_get_prefix(sampler_info[i].type);
499 for (j = 0; j < sampler_info[i].count; ++j)
501 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, j);
502 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
503 if (name_loc == -1)
504 continue;
506 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
507 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
509 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
510 continue;
513 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
514 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
517 checkGLcall("glUniform1i");
518 string_buffer_release(&priv->string_buffers, sampler_name);
521 /* Context activation is done by the caller. */
522 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
523 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
525 unsigned int start = ~0U, end = 0;
526 int stack_idx = 0;
527 unsigned int heap_idx = 1;
528 unsigned int idx;
530 if (heap->entries[heap_idx].version <= version) return;
532 idx = heap->entries[heap_idx].idx;
533 if (constant_locations[idx] != -1)
534 start = end = idx;
535 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
537 while (stack_idx >= 0)
539 /* Note that we fall through to the next case statement. */
540 switch(stack[stack_idx])
542 case HEAP_NODE_TRAVERSE_LEFT:
544 unsigned int left_idx = heap_idx << 1;
545 if (left_idx < heap->size && heap->entries[left_idx].version > version)
547 heap_idx = left_idx;
548 idx = heap->entries[heap_idx].idx;
549 if (constant_locations[idx] != -1)
551 if (start > idx)
552 start = idx;
553 if (end < idx)
554 end = idx;
557 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
558 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
559 break;
563 case HEAP_NODE_TRAVERSE_RIGHT:
565 unsigned int right_idx = (heap_idx << 1) + 1;
566 if (right_idx < heap->size && heap->entries[right_idx].version > version)
568 heap_idx = right_idx;
569 idx = heap->entries[heap_idx].idx;
570 if (constant_locations[idx] != -1)
572 if (start > idx)
573 start = idx;
574 if (end < idx)
575 end = idx;
578 stack[stack_idx++] = HEAP_NODE_POP;
579 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
580 break;
584 case HEAP_NODE_POP:
585 heap_idx >>= 1;
586 --stack_idx;
587 break;
590 if (start <= end)
591 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start * 4]));
592 checkGLcall("walk_constant_heap()");
595 /* Context activation is done by the caller. */
596 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
598 GLfloat clamped_constant[4];
600 if (location == -1) return;
602 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
603 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
604 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
605 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
607 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
610 /* Context activation is done by the caller. */
611 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
612 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
614 int stack_idx = 0;
615 unsigned int heap_idx = 1;
616 unsigned int idx;
618 if (heap->entries[heap_idx].version <= version) return;
620 idx = heap->entries[heap_idx].idx;
621 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
622 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
624 while (stack_idx >= 0)
626 /* Note that we fall through to the next case statement. */
627 switch(stack[stack_idx])
629 case HEAP_NODE_TRAVERSE_LEFT:
631 unsigned int left_idx = heap_idx << 1;
632 if (left_idx < heap->size && heap->entries[left_idx].version > version)
634 heap_idx = left_idx;
635 idx = heap->entries[heap_idx].idx;
636 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
638 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
639 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
640 break;
644 case HEAP_NODE_TRAVERSE_RIGHT:
646 unsigned int right_idx = (heap_idx << 1) + 1;
647 if (right_idx < heap->size && heap->entries[right_idx].version > version)
649 heap_idx = right_idx;
650 idx = heap->entries[heap_idx].idx;
651 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
653 stack[stack_idx++] = HEAP_NODE_POP;
654 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
655 break;
659 case HEAP_NODE_POP:
660 heap_idx >>= 1;
661 --stack_idx;
662 break;
665 checkGLcall("walk_constant_heap_clamped()");
668 /* Context activation is done by the caller. */
669 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
670 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
671 unsigned char *stack, UINT version)
673 const struct wined3d_shader_lconst *lconst;
675 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
676 if (shader->reg_maps.shader_version.major == 1
677 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
678 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
679 else
680 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
682 if (!shader->load_local_constsF)
684 TRACE("No need to load local float constants for this shader\n");
685 return;
688 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
689 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
691 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
693 checkGLcall("glUniform4fv()");
696 /* Context activation is done by the caller. */
697 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
698 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
700 unsigned int i;
701 struct list* ptr;
703 for (i = 0; constants_set; constants_set >>= 1, ++i)
705 if (!(constants_set & 1)) continue;
707 /* We found this uniform name in the program - go ahead and send the data */
708 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i * 4]));
711 /* Load immediate constants */
712 ptr = list_head(&shader->constantsI);
713 while (ptr)
715 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
716 unsigned int idx = lconst->idx;
717 const GLint *values = (const GLint *)lconst->value;
719 /* We found this uniform name in the program - go ahead and send the data */
720 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
721 ptr = list_next(&shader->constantsI, ptr);
723 checkGLcall("glUniform4iv()");
726 /* Context activation is done by the caller. */
727 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
728 const GLint locations[MAX_CONST_B], const BOOL *constants, WORD constants_set)
730 unsigned int i;
731 struct list* ptr;
733 for (i = 0; constants_set; constants_set >>= 1, ++i)
735 if (!(constants_set & 1)) continue;
737 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
740 /* Load immediate constants */
741 ptr = list_head(&shader->constantsB);
742 while (ptr)
744 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
745 unsigned int idx = lconst->idx;
746 const GLint *values = (const GLint *)lconst->value;
748 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
749 ptr = list_next(&shader->constantsB, ptr);
751 checkGLcall("glUniform1iv()");
754 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
756 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
759 /* Context activation is done by the caller (state handler). */
760 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
761 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
763 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
764 UINT fixup = ps->np2_fixup_info->active;
765 UINT i;
767 for (i = 0; fixup; fixup >>= 1, ++i)
769 const struct wined3d_texture *tex = state->textures[i];
770 unsigned char idx = ps->np2_fixup_info->idx[i];
771 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
773 if (!tex)
775 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
776 continue;
779 if (idx % 2)
781 tex_dim[2] = tex->pow2_matrix[0];
782 tex_dim[3] = tex->pow2_matrix[5];
784 else
786 tex_dim[0] = tex->pow2_matrix[0];
787 tex_dim[1] = tex->pow2_matrix[5];
791 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
794 /* Taken and adapted from Mesa. */
795 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
797 float pos, neg, t, det;
798 struct wined3d_matrix temp;
800 /* Calculate the determinant of upper left 3x3 submatrix and
801 * determine if the matrix is singular. */
802 pos = neg = 0.0f;
803 t = in->_11 * in->_22 * in->_33;
804 if (t >= 0.0f)
805 pos += t;
806 else
807 neg += t;
809 t = in->_21 * in->_32 * in->_13;
810 if (t >= 0.0f)
811 pos += t;
812 else
813 neg += t;
814 t = in->_31 * in->_12 * in->_23;
815 if (t >= 0.0f)
816 pos += t;
817 else
818 neg += t;
820 t = -in->_31 * in->_22 * in->_13;
821 if (t >= 0.0f)
822 pos += t;
823 else
824 neg += t;
825 t = -in->_21 * in->_12 * in->_33;
826 if (t >= 0.0f)
827 pos += t;
828 else
829 neg += t;
831 t = -in->_11 * in->_32 * in->_23;
832 if (t >= 0.0f)
833 pos += t;
834 else
835 neg += t;
837 det = pos + neg;
839 if (fabsf(det) < 1e-25f)
840 return FALSE;
842 det = 1.0f / det;
843 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
844 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
845 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
846 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
847 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
848 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
849 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
850 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
851 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
853 *out = temp;
854 return TRUE;
857 static void swap_rows(float **a, float **b)
859 float *tmp = *a;
861 *a = *b;
862 *b = tmp;
865 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
867 float wtmp[4][8];
868 float m0, m1, m2, m3, s;
869 float *r0, *r1, *r2, *r3;
871 r0 = wtmp[0];
872 r1 = wtmp[1];
873 r2 = wtmp[2];
874 r3 = wtmp[3];
876 r0[0] = m->_11;
877 r0[1] = m->_12;
878 r0[2] = m->_13;
879 r0[3] = m->_14;
880 r0[4] = 1.0f;
881 r0[5] = r0[6] = r0[7] = 0.0f;
883 r1[0] = m->_21;
884 r1[1] = m->_22;
885 r1[2] = m->_23;
886 r1[3] = m->_24;
887 r1[5] = 1.0f;
888 r1[4] = r1[6] = r1[7] = 0.0f;
890 r2[0] = m->_31;
891 r2[1] = m->_32;
892 r2[2] = m->_33;
893 r2[3] = m->_34;
894 r2[6] = 1.0f;
895 r2[4] = r2[5] = r2[7] = 0.0f;
897 r3[0] = m->_41;
898 r3[1] = m->_42;
899 r3[2] = m->_43;
900 r3[3] = m->_44;
901 r3[7] = 1.0f;
902 r3[4] = r3[5] = r3[6] = 0.0f;
904 /* Choose pivot - or die. */
905 if (fabsf(r3[0]) > fabsf(r2[0]))
906 swap_rows(&r3, &r2);
907 if (fabsf(r2[0]) > fabsf(r1[0]))
908 swap_rows(&r2, &r1);
909 if (fabsf(r1[0]) > fabsf(r0[0]))
910 swap_rows(&r1, &r0);
911 if (r0[0] == 0.0f)
912 return FALSE;
914 /* Eliminate first variable. */
915 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
916 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
917 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
918 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
919 s = r0[4];
920 if (s != 0.0f)
922 r1[4] -= m1 * s;
923 r2[4] -= m2 * s;
924 r3[4] -= m3 * s;
926 s = r0[5];
927 if (s != 0.0f)
929 r1[5] -= m1 * s;
930 r2[5] -= m2 * s;
931 r3[5] -= m3 * s;
933 s = r0[6];
934 if (s != 0.0f)
936 r1[6] -= m1 * s;
937 r2[6] -= m2 * s;
938 r3[6] -= m3 * s;
940 s = r0[7];
941 if (s != 0.0f)
943 r1[7] -= m1 * s;
944 r2[7] -= m2 * s;
945 r3[7] -= m3 * s;
948 /* Choose pivot - or die. */
949 if (fabsf(r3[1]) > fabsf(r2[1]))
950 swap_rows(&r3, &r2);
951 if (fabsf(r2[1]) > fabsf(r1[1]))
952 swap_rows(&r2, &r1);
953 if (r1[1] == 0.0f)
954 return FALSE;
956 /* Eliminate second variable. */
957 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
958 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
959 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
960 s = r1[4];
961 if (s != 0.0f)
963 r2[4] -= m2 * s;
964 r3[4] -= m3 * s;
966 s = r1[5];
967 if (s != 0.0f)
969 r2[5] -= m2 * s;
970 r3[5] -= m3 * s;
972 s = r1[6];
973 if (s != 0.0f)
975 r2[6] -= m2 * s;
976 r3[6] -= m3 * s;
978 s = r1[7];
979 if (s != 0.0f)
981 r2[7] -= m2 * s;
982 r3[7] -= m3 * s;
985 /* Choose pivot - or die. */
986 if (fabsf(r3[2]) > fabsf(r2[2]))
987 swap_rows(&r3, &r2);
988 if (r2[2] == 0.0f)
989 return FALSE;
991 /* Eliminate third variable. */
992 m3 = r3[2] / r2[2];
993 r3[3] -= m3 * r2[3];
994 r3[4] -= m3 * r2[4];
995 r3[5] -= m3 * r2[5];
996 r3[6] -= m3 * r2[6];
997 r3[7] -= m3 * r2[7];
999 /* Last check. */
1000 if (r3[3] == 0.0f)
1001 return FALSE;
1003 /* Back substitute row 3. */
1004 s = 1.0f / r3[3];
1005 r3[4] *= s;
1006 r3[5] *= s;
1007 r3[6] *= s;
1008 r3[7] *= s;
1010 /* Back substitute row 2. */
1011 m2 = r2[3];
1012 s = 1.0f / r2[2];
1013 r2[4] = s * (r2[4] - r3[4] * m2);
1014 r2[5] = s * (r2[5] - r3[5] * m2);
1015 r2[6] = s * (r2[6] - r3[6] * m2);
1016 r2[7] = s * (r2[7] - r3[7] * m2);
1017 m1 = r1[3];
1018 r1[4] -= r3[4] * m1;
1019 r1[5] -= r3[5] * m1;
1020 r1[6] -= r3[6] * m1;
1021 r1[7] -= r3[7] * m1;
1022 m0 = r0[3];
1023 r0[4] -= r3[4] * m0;
1024 r0[5] -= r3[5] * m0;
1025 r0[6] -= r3[6] * m0;
1026 r0[7] -= r3[7] * m0;
1028 /* Back substitute row 1. */
1029 m1 = r1[2];
1030 s = 1.0f / r1[1];
1031 r1[4] = s * (r1[4] - r2[4] * m1);
1032 r1[5] = s * (r1[5] - r2[5] * m1);
1033 r1[6] = s * (r1[6] - r2[6] * m1);
1034 r1[7] = s * (r1[7] - r2[7] * m1);
1035 m0 = r0[2];
1036 r0[4] -= r2[4] * m0;
1037 r0[5] -= r2[5] * m0;
1038 r0[6] -= r2[6] * m0;
1039 r0[7] -= r2[7] * m0;
1041 /* Back substitute row 0. */
1042 m0 = r0[1];
1043 s = 1.0f / r0[0];
1044 r0[4] = s * (r0[4] - r1[4] * m0);
1045 r0[5] = s * (r0[5] - r1[5] * m0);
1046 r0[6] = s * (r0[6] - r1[6] * m0);
1047 r0[7] = s * (r0[7] - r1[7] * m0);
1049 out->_11 = r0[4];
1050 out->_12 = r0[5];
1051 out->_13 = r0[6];
1052 out->_14 = r0[7];
1053 out->_21 = r1[4];
1054 out->_22 = r1[5];
1055 out->_23 = r1[6];
1056 out->_24 = r1[7];
1057 out->_31 = r2[4];
1058 out->_32 = r2[5];
1059 out->_33 = r2[6];
1060 out->_34 = r2[7];
1061 out->_41 = r3[4];
1062 out->_42 = r3[5];
1063 out->_43 = r3[6];
1064 out->_44 = r3[7];
1066 return TRUE;
1069 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1070 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1072 const struct wined3d_gl_info *gl_info = context->gl_info;
1073 float mat[3 * 3];
1074 struct wined3d_matrix mv;
1075 unsigned int i, j;
1077 if (prog->vs.normal_matrix_location == -1)
1078 return;
1080 get_modelview_matrix(context, state, 0, &mv);
1081 if (context->swapchain->device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING)
1082 invert_matrix_3d(&mv, &mv);
1083 else
1084 invert_matrix(&mv, &mv);
1085 /* Tests show that singular modelview matrices are used unchanged as normal
1086 * matrices on D3D3 and older. There seems to be no clearly consistent
1087 * behavior on newer D3D versions so always follow older ddraw behavior. */
1088 for (i = 0; i < 3; ++i)
1089 for (j = 0; j < 3; ++j)
1090 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1092 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1093 checkGLcall("glUniformMatrix3fv");
1096 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1097 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1099 const struct wined3d_gl_info *gl_info = context->gl_info;
1100 struct wined3d_matrix mat;
1102 if (tex >= MAX_TEXTURES)
1103 return;
1104 if (prog->vs.texture_matrix_location[tex] == -1)
1105 return;
1107 get_texture_matrix(context, state, tex, &mat);
1108 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1109 checkGLcall("glUniformMatrix4fv");
1112 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1113 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1115 const struct wined3d_gl_info *gl_info = context->gl_info;
1117 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1119 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1120 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1122 else
1124 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1126 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1128 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1129 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1130 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1131 checkGLcall("setting FFP material uniforms");
1134 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1135 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1137 const struct wined3d_gl_info *gl_info = context->gl_info;
1138 float col[4];
1140 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_AMBIENT], col);
1141 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, col));
1142 checkGLcall("glUniform3fv");
1145 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1146 const struct wined3d_matrix *src2)
1148 struct wined3d_vec4 temp;
1150 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1151 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1152 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1153 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1155 *dest = temp;
1158 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1159 const struct wined3d_state *state, unsigned int light, struct glsl_shader_prog_link *prog)
1161 const struct wined3d_gl_info *gl_info = context->gl_info;
1162 const struct wined3d_light_info *light_info = state->lights[light];
1163 struct wined3d_vec4 vec4;
1164 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1166 if (!light_info)
1167 return;
1169 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1170 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1171 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1173 switch (light_info->OriginalParms.type)
1175 case WINED3D_LIGHT_POINT:
1176 multiply_vector_matrix(&vec4, &light_info->position, view);
1177 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1178 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1179 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1180 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1181 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1182 break;
1184 case WINED3D_LIGHT_SPOT:
1185 multiply_vector_matrix(&vec4, &light_info->position, view);
1186 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1188 multiply_vector_matrix(&vec4, &light_info->direction, view);
1189 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1191 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1192 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1193 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1194 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1195 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1196 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1197 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1198 break;
1200 case WINED3D_LIGHT_DIRECTIONAL:
1201 multiply_vector_matrix(&vec4, &light_info->direction, view);
1202 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1203 break;
1205 case WINED3D_LIGHT_PARALLELPOINT:
1206 multiply_vector_matrix(&vec4, &light_info->position, view);
1207 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1208 break;
1210 default:
1211 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1213 checkGLcall("setting FFP lights uniforms");
1216 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1217 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1219 const struct wined3d_gl_info *gl_info = context->gl_info;
1220 float min, max;
1221 float size, att[3];
1223 get_pointsize_minmax(context, state, &min, &max);
1225 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1226 checkGLcall("glUniform1f");
1227 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1228 checkGLcall("glUniform1f");
1230 get_pointsize(context, state, &size, att);
1232 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1233 checkGLcall("glUniform1f");
1234 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1235 checkGLcall("glUniform1f");
1236 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1237 checkGLcall("glUniform1f");
1238 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1239 checkGLcall("glUniform1f");
1242 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1243 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1245 const struct wined3d_gl_info *gl_info = context->gl_info;
1246 float start, end, scale;
1247 union
1249 DWORD d;
1250 float f;
1251 } tmpvalue;
1252 float col[4];
1254 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_FOGCOLOR], col);
1255 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, col));
1256 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1257 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1258 get_fog_start_end(context, state, &start, &end);
1259 scale = 1.0f / (end - start);
1260 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1261 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1262 checkGLcall("fog emulation uniforms");
1265 /* Context activation is done by the caller (state handler). */
1266 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1267 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1269 struct wined3d_color float_key[2];
1270 const struct wined3d_texture *texture = state->textures[0];
1272 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1273 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1276 /* Context activation is done by the caller (state handler). */
1277 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1278 const struct wined3d_state *state)
1280 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1281 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1282 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1283 const struct wined3d_gl_info *gl_info = context->gl_info;
1284 struct shader_glsl_priv *priv = shader_priv;
1285 float position_fixup[4];
1286 DWORD update_mask = 0;
1288 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1289 UINT constant_version;
1290 int i;
1292 if (!prog) {
1293 /* No GLSL program set - nothing to do. */
1294 return;
1296 constant_version = prog->constant_version;
1297 update_mask = context->constant_update_mask & prog->constant_update_mask;
1299 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1300 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
1301 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1303 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1304 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
1305 vshader->reg_maps.integer_constants);
1307 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1308 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1309 vshader->reg_maps.boolean_constants);
1311 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1312 shader_glsl_pointsize_uniform(context, state, prog);
1314 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
1316 shader_get_position_fixup(context, state, position_fixup);
1317 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1318 checkGLcall("glUniform4fv");
1321 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1323 struct wined3d_matrix mat;
1325 get_modelview_matrix(context, state, 0, &mat);
1326 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1327 checkGLcall("glUniformMatrix4fv");
1329 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1332 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1334 struct wined3d_matrix mat;
1336 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1338 if (prog->vs.modelview_matrix_location[i] == -1)
1339 break;
1341 get_modelview_matrix(context, state, i, &mat);
1342 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1343 checkGLcall("glUniformMatrix4fv");
1347 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1349 struct wined3d_matrix projection;
1351 get_projection_matrix(context, state, &projection);
1352 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1353 checkGLcall("glUniformMatrix4fv");
1356 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1358 for (i = 0; i < MAX_TEXTURES; ++i)
1359 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1362 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1363 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1365 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1367 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1368 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1369 shader_glsl_ffp_vertex_light_uniform(context, state, i, prog);
1372 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1373 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
1374 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1376 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1377 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
1378 pshader->reg_maps.integer_constants);
1380 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1381 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1382 pshader->reg_maps.boolean_constants);
1384 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1386 for (i = 0; i < MAX_TEXTURES; ++i)
1388 if (prog->ps.bumpenv_mat_location[i] == -1)
1389 continue;
1391 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1392 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1394 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1396 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1397 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1398 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1399 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1403 checkGLcall("bump env uniforms");
1406 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1408 float correction_params[] =
1410 /* position is window relative, not viewport relative */
1411 context->render_offscreen ? 0.0f : (float)context->current_rt->resource.height,
1412 context->render_offscreen ? 1.0f : -1.0f,
1413 0.0f,
1414 0.0f,
1417 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, correction_params));
1420 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1421 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1422 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1423 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1425 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1427 float col[4];
1429 if (prog->ps.tex_factor_location != -1)
1431 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_TEXTUREFACTOR], col);
1432 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, col));
1435 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1436 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1437 else
1438 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1440 for (i = 0; i < MAX_TEXTURES; ++i)
1442 if (prog->ps.tss_constant_location[i] == -1)
1443 continue;
1445 D3DCOLORTOGLFLOAT4(state->texture_states[i][WINED3D_TSS_CONSTANT], col);
1446 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, col));
1449 checkGLcall("fixed function uniforms");
1452 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1453 shader_glsl_load_fog_uniform(context, state, prog);
1455 if (priv->next_constant_version == UINT_MAX)
1457 TRACE("Max constant version reached, resetting to 0.\n");
1458 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1459 priv->next_constant_version = 1;
1461 else
1463 prog->constant_version = priv->next_constant_version++;
1467 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1469 struct constant_entry *entries = heap->entries;
1470 unsigned int *positions = heap->positions;
1471 unsigned int heap_idx, parent_idx;
1473 if (!heap->contained[idx])
1475 heap_idx = heap->size++;
1476 heap->contained[idx] = TRUE;
1478 else
1480 heap_idx = positions[idx];
1483 while (heap_idx > 1)
1485 parent_idx = heap_idx >> 1;
1487 if (new_version <= entries[parent_idx].version) break;
1489 entries[heap_idx] = entries[parent_idx];
1490 positions[entries[parent_idx].idx] = heap_idx;
1491 heap_idx = parent_idx;
1494 entries[heap_idx].version = new_version;
1495 entries[heap_idx].idx = idx;
1496 positions[idx] = heap_idx;
1499 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1501 struct shader_glsl_priv *priv = device->shader_priv;
1502 struct constant_heap *heap = &priv->vconst_heap;
1503 UINT i;
1505 for (i = start; i < count + start; ++i)
1507 update_heap_entry(heap, i, priv->next_constant_version);
1510 for (i = 0; i < device->context_count; ++i)
1512 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
1516 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1518 struct shader_glsl_priv *priv = device->shader_priv;
1519 struct constant_heap *heap = &priv->pconst_heap;
1520 UINT i;
1522 for (i = start; i < count + start; ++i)
1524 update_heap_entry(heap, i, priv->next_constant_version);
1527 for (i = 0; i < device->context_count; ++i)
1529 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
1533 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1535 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1536 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1537 if(shader_major > 3) return ret;
1539 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1540 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1541 return ret;
1544 /** Generate the variable & register declarations for the GLSL output target */
1545 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1546 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1547 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1549 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1550 const struct wined3d_state *state = &shader->device->state;
1551 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1552 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1553 const struct wined3d_gl_info *gl_info = context->gl_info;
1554 const struct wined3d_fb_state *fb = &shader->device->fb;
1555 unsigned int i, extra_constants_needed = 0;
1556 const struct wined3d_shader_lconst *lconst;
1557 const char *prefix;
1558 DWORD map;
1560 prefix = shader_glsl_get_prefix(version->type);
1562 /* Prototype the subroutines */
1563 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1565 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1568 /* Declare the constants (aka uniforms) */
1569 if (shader->limits->constant_float > 0)
1571 unsigned max_constantsF;
1573 /* Unless the shader uses indirect addressing, always declare the
1574 * maximum array size and ignore that we need some uniforms privately.
1575 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1576 * and immediate values, still declare VC[256]. If the shader needs
1577 * more uniforms than we have it won't work in any case. If it uses
1578 * less, the compiler will figure out which uniforms are really used
1579 * and strip them out. This allows a shader to use c255 on a dx9 card,
1580 * as long as it doesn't also use all the other constants.
1582 * If the shader uses indirect addressing the compiler must assume
1583 * that all declared uniforms are used. In this case, declare only the
1584 * amount that we're assured to have.
1586 * Thus we run into problems in these two cases:
1587 * 1) The shader really uses more uniforms than supported.
1588 * 2) The shader uses indirect addressing, less constants than
1589 * supported, but uses a constant index > #supported consts. */
1590 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1592 /* No indirect addressing here. */
1593 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1595 else
1597 if (reg_maps->usesrelconstF)
1599 /* Subtract the other potential uniforms from the max
1600 * available (bools, ints, and 1 row of projection matrix).
1601 * Subtract another uniform for immediate values, which have
1602 * to be loaded via uniform by the driver as well. The shader
1603 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1604 * shader code, so one vec4 should be enough. (Unfortunately
1605 * the Nvidia driver doesn't store 128 and -128 in one float).
1607 * Writing gl_ClipVertex requires one uniform for each
1608 * clipplane as well. */
1609 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1610 if(ctx_priv->cur_vs_args->clip_enabled)
1612 max_constantsF -= gl_info->limits.clipplanes;
1614 max_constantsF -= count_bits(reg_maps->integer_constants);
1615 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1616 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1617 * for now take this into account when calculating the number of available constants
1619 max_constantsF -= count_bits(reg_maps->boolean_constants);
1620 /* Set by driver quirks in directx.c */
1621 max_constantsF -= gl_info->reserved_glsl_constants;
1623 if (max_constantsF < shader->limits->constant_float)
1625 static unsigned int once;
1627 if (!once++)
1628 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1629 " it may not render correctly.\n");
1630 else
1631 WARN("The hardware does not support enough uniform components to run this shader.\n");
1634 else
1636 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1639 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1640 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1643 /* Always declare the full set of constants, the compiler can remove the
1644 * unused ones because d3d doesn't (yet) support indirect int and bool
1645 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1646 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1647 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1649 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1650 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1652 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1654 if (reg_maps->cb_sizes[i])
1655 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1656 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1659 /* Declare texture samplers */
1660 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1662 struct wined3d_shader_sampler_map_entry *entry;
1663 BOOL shadow_sampler, tex_rect;
1664 const char *sampler_type;
1666 entry = &reg_maps->sampler_map.entries[i];
1668 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1670 ERR("Invalid resource index %u.\n", entry->resource_idx);
1671 continue;
1674 shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << entry->sampler_idx));
1675 switch (reg_maps->resource_info[entry->resource_idx].type)
1677 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1678 if (shadow_sampler)
1679 sampler_type = "sampler1DShadow";
1680 else
1681 sampler_type = "sampler1D";
1682 break;
1684 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1685 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1686 && (ps_args->np2_fixup & (1u << entry->resource_idx))
1687 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1688 if (shadow_sampler)
1690 if (tex_rect)
1691 sampler_type = "sampler2DRectShadow";
1692 else
1693 sampler_type = "sampler2DShadow";
1695 else
1697 if (tex_rect)
1698 sampler_type = "sampler2DRect";
1699 else
1700 sampler_type = "sampler2D";
1702 break;
1704 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1705 if (shadow_sampler)
1706 FIXME("Unsupported 3D shadow sampler.\n");
1707 sampler_type = "sampler3D";
1708 break;
1710 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1711 if (shadow_sampler)
1712 FIXME("Unsupported Cube shadow sampler.\n");
1713 sampler_type = "samplerCube";
1714 break;
1716 default:
1717 sampler_type = "unsupported_sampler";
1718 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[i].type);
1719 break;
1721 shader_addline(buffer, "uniform %s %s_sampler%u;\n", sampler_type, prefix, entry->bind_idx);
1724 /* Declare uniforms for NP2 texcoord fixup:
1725 * This is NOT done inside the loop that declares the texture samplers
1726 * since the NP2 fixup code is currently only used for the GeforceFX
1727 * series and when forcing the ARB_npot extension off. Modern cards just
1728 * skip the code anyway, so put it inside a separate loop. */
1729 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1731 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1732 UINT cur = 0;
1734 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1735 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1736 * samplerNP2Fixup stores texture dimensions and is updated through
1737 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1739 for (i = 0; i < shader->limits->sampler; ++i)
1741 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
1742 continue;
1744 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1746 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1747 continue;
1750 fixup->idx[i] = cur++;
1753 fixup->num_consts = (cur + 1) >> 1;
1754 fixup->active = ps_args->np2_fixup;
1755 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1758 /* Declare address variables */
1759 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1761 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1764 /* Declare texture coordinate temporaries and initialize them */
1765 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1767 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1770 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1772 for (i = 0; i < shader->input_signature.element_count; ++i)
1774 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
1775 if (e->sysval_semantic == WINED3D_SV_INSTANCEID)
1776 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1777 prefix, e->register_idx);
1778 else
1779 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, e->register_idx);
1782 if (vs_args->point_size && !vs_args->per_vertex_point_size)
1784 shader_addline(buffer, "uniform struct\n{\n");
1785 shader_addline(buffer, " float size;\n");
1786 shader_addline(buffer, " float size_min;\n");
1787 shader_addline(buffer, " float size_max;\n");
1788 shader_addline(buffer, "} ffp_point;\n");
1791 shader_addline(buffer, "uniform vec4 posFixup;\n");
1792 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits->packed_output);
1794 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1796 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
1798 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1800 if (version->major < 3 || ps_args->vp_mode != vertexshader)
1802 shader_addline(buffer, "uniform struct\n{\n");
1803 shader_addline(buffer, " vec4 color;\n");
1804 shader_addline(buffer, " float density;\n");
1805 shader_addline(buffer, " float end;\n");
1806 shader_addline(buffer, " float scale;\n");
1807 shader_addline(buffer, "} ffp_fog;\n");
1810 if (version->major >= 3)
1812 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
1814 if (use_vs(state))
1815 shader_addline(buffer, "varying vec4 %s_link[%u];\n", prefix, in_count);
1816 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1819 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1821 if (!(map & 1))
1822 continue;
1824 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1826 if (reg_maps->luminanceparams & (1u << i))
1828 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1829 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1830 extra_constants_needed++;
1833 extra_constants_needed++;
1836 if (ps_args->srgb_correction)
1838 shader_addline(buffer, "const vec4 srgb_const0 = ");
1839 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
1840 shader_addline(buffer, ";\n");
1841 shader_addline(buffer, "const vec4 srgb_const1 = ");
1842 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
1843 shader_addline(buffer, ";\n");
1845 if (reg_maps->vpos || reg_maps->usesdsy)
1847 if (shader->limits->constant_float + extra_constants_needed
1848 + 1 < gl_info->limits.glsl_ps_float_constants)
1850 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1851 extra_constants_needed++;
1853 else
1855 float ycorrection[] =
1857 context->render_offscreen ? 0.0f : fb->render_targets[0]->height,
1858 context->render_offscreen ? 1.0f : -1.0f,
1859 0.0f,
1860 0.0f,
1863 /* This happens because we do not have proper tracking of the
1864 * constant registers that are actually used, only the max
1865 * limit of the shader version. */
1866 FIXME("Cannot find a free uniform for vpos correction params\n");
1867 shader_addline(buffer, "const vec4 ycorrection = ");
1868 shader_glsl_append_imm_vec4(buffer, ycorrection);
1869 shader_addline(buffer, ";\n");
1871 shader_addline(buffer, "vec4 vpos;\n");
1875 /* Declare output register temporaries */
1876 if (shader->limits->packed_output)
1877 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
1879 /* Declare temporary variables */
1880 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1882 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1885 /* Declare loop registers aLx */
1886 if (version->major < 4)
1888 for (i = 0; i < reg_maps->loop_depth; ++i)
1890 shader_addline(buffer, "int aL%u;\n", i);
1891 shader_addline(buffer, "int tmpInt%u;\n", i);
1895 /* Temporary variables for matrix operations */
1896 shader_addline(buffer, "vec4 tmp0;\n");
1897 shader_addline(buffer, "vec4 tmp1;\n");
1899 if (!shader->load_local_constsF)
1901 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1903 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
1904 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
1905 shader_addline(buffer, ";\n");
1909 /* Start the main program. */
1910 shader_addline(buffer, "void main()\n{\n");
1912 /* Direct3D applications expect integer vPos values, while OpenGL drivers
1913 * add approximately 0.5. This causes off-by-one problems as spotted by
1914 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
1915 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
1916 * causes precision troubles when we just subtract 0.5.
1918 * To deal with that, just floor() the position. This will eliminate the
1919 * fraction on all cards.
1921 * TODO: Test how this behaves with multisampling.
1923 * An advantage of floor is that it works even if the driver doesn't add
1924 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
1925 * to return in gl_FragCoord, even though coordinates specify the pixel
1926 * centers instead of the pixel corners. This code will behave correctly
1927 * on drivers that returns integer values. */
1928 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
1930 if (shader->device->wined3d->flags & WINED3D_PIXEL_CENTER_INTEGER)
1931 shader_addline(buffer,
1932 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1933 else
1934 shader_addline(buffer,
1935 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
1939 /*****************************************************************************
1940 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1942 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1943 ****************************************************************************/
1945 /* Prototypes */
1946 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1947 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1949 /** Used for opcode modifiers - They multiply the result by the specified amount */
1950 static const char * const shift_glsl_tab[] = {
1951 "", /* 0 (none) */
1952 "2.0 * ", /* 1 (x2) */
1953 "4.0 * ", /* 2 (x4) */
1954 "8.0 * ", /* 3 (x8) */
1955 "16.0 * ", /* 4 (x16) */
1956 "32.0 * ", /* 5 (x32) */
1957 "", /* 6 (x64) */
1958 "", /* 7 (x128) */
1959 "", /* 8 (d256) */
1960 "", /* 9 (d128) */
1961 "", /* 10 (d64) */
1962 "", /* 11 (d32) */
1963 "0.0625 * ", /* 12 (d16) */
1964 "0.125 * ", /* 13 (d8) */
1965 "0.25 * ", /* 14 (d4) */
1966 "0.5 * " /* 15 (d2) */
1969 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1970 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1971 const char *in_reg, const char *in_regswizzle, char *out_str)
1973 out_str[0] = 0;
1975 switch (src_modifier)
1977 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1978 case WINED3DSPSM_DW:
1979 case WINED3DSPSM_NONE:
1980 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1981 break;
1982 case WINED3DSPSM_NEG:
1983 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1984 break;
1985 case WINED3DSPSM_NOT:
1986 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1987 break;
1988 case WINED3DSPSM_BIAS:
1989 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1990 break;
1991 case WINED3DSPSM_BIASNEG:
1992 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1993 break;
1994 case WINED3DSPSM_SIGN:
1995 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1996 break;
1997 case WINED3DSPSM_SIGNNEG:
1998 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1999 break;
2000 case WINED3DSPSM_COMP:
2001 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2002 break;
2003 case WINED3DSPSM_X2:
2004 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2005 break;
2006 case WINED3DSPSM_X2NEG:
2007 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2008 break;
2009 case WINED3DSPSM_ABS:
2010 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2011 break;
2012 case WINED3DSPSM_ABSNEG:
2013 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2014 break;
2015 default:
2016 FIXME("Unhandled modifier %u\n", src_modifier);
2017 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2021 /** Writes the GLSL variable name that corresponds to the register that the
2022 * DX opcode parameter is trying to access */
2023 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2024 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2026 /* oPos, oFog and oPts in D3D */
2027 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2029 const struct wined3d_shader *shader = ins->ctx->shader;
2030 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2031 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2032 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2033 const char *prefix = shader_glsl_get_prefix(version->type);
2034 struct glsl_src_param rel_param0, rel_param1;
2035 char imm_str[4][17];
2037 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2038 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2039 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2040 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2041 *is_color = FALSE;
2043 switch (reg->type)
2045 case WINED3DSPR_TEMP:
2046 sprintf(register_name, "R%u", reg->idx[0].offset);
2047 break;
2049 case WINED3DSPR_INPUT:
2050 /* vertex shaders */
2051 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2053 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2054 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2055 *is_color = TRUE;
2056 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2057 break;
2060 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2062 if (reg->idx[0].rel_addr)
2064 if (reg->idx[1].rel_addr)
2065 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
2066 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2067 else
2068 sprintf(register_name, "gs_in[%s + %u][%u]",
2069 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
2071 else if (reg->idx[1].rel_addr)
2072 sprintf(register_name, "gs_in[%u][%s + %u]",
2073 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2074 else
2075 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
2076 break;
2079 /* pixel shaders >= 3.0 */
2080 if (version->major >= 3)
2082 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2083 unsigned int in_count = vec4_varyings(version->major, gl_info);
2085 if (reg->idx[0].rel_addr)
2087 /* Removing a + 0 would be an obvious optimization, but
2088 * OS X doesn't see the NOP operation there. */
2089 if (idx)
2091 if (shader->u.ps.declared_in_count > in_count)
2093 sprintf(register_name,
2094 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2095 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2096 prefix, rel_param0.param_str, idx);
2098 else
2100 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2103 else
2105 if (shader->u.ps.declared_in_count > in_count)
2107 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2108 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2109 prefix, rel_param0.param_str);
2111 else
2113 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2117 else
2119 if (idx == in_count) sprintf(register_name, "gl_Color");
2120 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2121 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2124 else
2126 if (!reg->idx[0].offset)
2127 strcpy(register_name, "gl_Color");
2128 else
2129 strcpy(register_name, "gl_SecondaryColor");
2130 break;
2132 break;
2134 case WINED3DSPR_CONST:
2136 /* Relative addressing */
2137 if (reg->idx[0].rel_addr)
2139 if (reg->idx[0].offset)
2140 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2141 else
2142 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2144 else
2146 if (shader_constant_is_local(shader, reg->idx[0].offset))
2147 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2148 else
2149 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2152 break;
2154 case WINED3DSPR_CONSTINT:
2155 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2156 break;
2158 case WINED3DSPR_CONSTBOOL:
2159 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2160 break;
2162 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2163 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2164 sprintf(register_name, "T%u", reg->idx[0].offset);
2165 else
2166 sprintf(register_name, "A%u", reg->idx[0].offset);
2167 break;
2169 case WINED3DSPR_LOOP:
2170 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
2171 break;
2173 case WINED3DSPR_SAMPLER:
2174 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2175 break;
2177 case WINED3DSPR_COLOROUT:
2178 if (reg->idx[0].offset >= gl_info->limits.buffers)
2179 WARN("Write to render target %u, only %d supported.\n",
2180 reg->idx[0].offset, gl_info->limits.buffers);
2182 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
2183 break;
2185 case WINED3DSPR_RASTOUT:
2186 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2187 break;
2189 case WINED3DSPR_DEPTHOUT:
2190 sprintf(register_name, "gl_FragDepth");
2191 break;
2193 case WINED3DSPR_ATTROUT:
2194 if (!reg->idx[0].offset)
2195 sprintf(register_name, "%s_out[8]", prefix);
2196 else
2197 sprintf(register_name, "%s_out[9]", prefix);
2198 break;
2200 case WINED3DSPR_TEXCRDOUT:
2201 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2202 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2203 break;
2205 case WINED3DSPR_MISCTYPE:
2206 if (!reg->idx[0].offset)
2208 /* vPos */
2209 sprintf(register_name, "vpos");
2211 else if (reg->idx[0].offset == 1)
2213 /* Note that gl_FrontFacing is a bool, while vFace is
2214 * a float for which the sign determines front/back */
2215 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2217 else
2219 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2220 sprintf(register_name, "unrecognized_register");
2222 break;
2224 case WINED3DSPR_IMMCONST:
2225 switch (reg->immconst_type)
2227 case WINED3D_IMMCONST_SCALAR:
2228 switch (reg->data_type)
2230 case WINED3D_DATA_FLOAT:
2231 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
2232 break;
2233 case WINED3D_DATA_INT:
2234 sprintf(register_name, "%#x", reg->immconst_data[0]);
2235 break;
2236 case WINED3D_DATA_RESOURCE:
2237 case WINED3D_DATA_SAMPLER:
2238 case WINED3D_DATA_UINT:
2239 sprintf(register_name, "%#xu", reg->immconst_data[0]);
2240 break;
2241 default:
2242 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2243 break;
2245 break;
2247 case WINED3D_IMMCONST_VEC4:
2248 switch (reg->data_type)
2250 case WINED3D_DATA_FLOAT:
2251 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
2252 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
2253 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
2254 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
2255 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2256 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2257 break;
2258 case WINED3D_DATA_INT:
2259 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2260 reg->immconst_data[0], reg->immconst_data[1],
2261 reg->immconst_data[2], reg->immconst_data[3]);
2262 break;
2263 case WINED3D_DATA_RESOURCE:
2264 case WINED3D_DATA_SAMPLER:
2265 case WINED3D_DATA_UINT:
2266 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2267 reg->immconst_data[0], reg->immconst_data[1],
2268 reg->immconst_data[2], reg->immconst_data[3]);
2269 break;
2270 default:
2271 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2272 break;
2274 break;
2276 default:
2277 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2278 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2280 break;
2282 case WINED3DSPR_CONSTBUFFER:
2283 if (reg->idx[1].rel_addr)
2284 sprintf(register_name, "%s_cb%u[%s + %u]",
2285 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2286 else
2287 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2288 break;
2290 case WINED3DSPR_PRIMID:
2291 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2292 break;
2294 default:
2295 FIXME("Unhandled register type %#x.\n", reg->type);
2296 sprintf(register_name, "unrecognized_register");
2297 break;
2301 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2303 *str++ = '.';
2304 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2305 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2306 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2307 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2308 *str = '\0';
2311 /* Get the GLSL write mask for the destination register */
2312 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2314 DWORD mask = param->write_mask;
2316 if (shader_is_scalar(&param->reg))
2318 mask = WINED3DSP_WRITEMASK_0;
2319 *write_mask = '\0';
2321 else
2323 shader_glsl_write_mask_to_str(mask, write_mask);
2326 return mask;
2329 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
2330 unsigned int size = 0;
2332 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2333 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2334 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2335 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2337 return size;
2340 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2342 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2343 * but addressed as "rgba". To fix this we need to swap the register's x
2344 * and z components. */
2345 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2347 *str++ = '.';
2348 /* swizzle bits fields: wwzzyyxx */
2349 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
2350 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
2351 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
2352 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
2353 *str = '\0';
2356 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2357 BOOL fixup, DWORD mask, char *swizzle_str)
2359 if (shader_is_scalar(&param->reg))
2360 *swizzle_str = '\0';
2361 else
2362 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2365 /* From a given parameter token, generate the corresponding GLSL string.
2366 * Also, return the actual register name and swizzle in case the
2367 * caller needs this information as well. */
2368 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2369 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
2371 BOOL is_color = FALSE;
2372 char swizzle_str[6];
2374 glsl_src->reg_name[0] = '\0';
2375 glsl_src->param_str[0] = '\0';
2376 swizzle_str[0] = '\0';
2378 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
2379 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
2381 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
2383 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
2385 else
2387 char reg_name[200];
2389 switch (wined3d_src->reg.data_type)
2391 case WINED3D_DATA_FLOAT:
2392 sprintf(reg_name, "%s", glsl_src->reg_name);
2393 break;
2394 case WINED3D_DATA_INT:
2395 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
2396 break;
2397 case WINED3D_DATA_RESOURCE:
2398 case WINED3D_DATA_SAMPLER:
2399 case WINED3D_DATA_UINT:
2400 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
2401 break;
2402 default:
2403 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
2404 sprintf(reg_name, "%s", glsl_src->reg_name);
2405 break;
2408 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
2412 /* From a given parameter token, generate the corresponding GLSL string.
2413 * Also, return the actual register name and swizzle in case the
2414 * caller needs this information as well. */
2415 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
2416 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
2418 BOOL is_color = FALSE;
2420 glsl_dst->mask_str[0] = '\0';
2421 glsl_dst->reg_name[0] = '\0';
2423 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
2424 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
2427 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2428 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
2429 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
2430 enum wined3d_data_type data_type)
2432 struct glsl_dst_param glsl_dst;
2433 DWORD mask;
2435 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
2437 switch (data_type)
2439 case WINED3D_DATA_FLOAT:
2440 shader_addline(buffer, "%s%s = %s(",
2441 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2442 break;
2443 case WINED3D_DATA_INT:
2444 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
2445 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2446 break;
2447 case WINED3D_DATA_RESOURCE:
2448 case WINED3D_DATA_SAMPLER:
2449 case WINED3D_DATA_UINT:
2450 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
2451 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2452 break;
2453 default:
2454 FIXME("Unhandled data type %#x.\n", data_type);
2455 shader_addline(buffer, "%s%s = %s(",
2456 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2457 break;
2461 return mask;
2464 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2465 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
2467 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2470 /** Process GLSL instruction modifiers */
2471 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
2473 struct glsl_dst_param dst_param;
2474 DWORD modifiers;
2476 if (!ins->dst_count) return;
2478 modifiers = ins->dst[0].modifiers;
2479 if (!modifiers) return;
2481 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2483 if (modifiers & WINED3DSPDM_SATURATE)
2485 /* _SAT means to clamp the value of the register to between 0 and 1 */
2486 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
2487 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
2490 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
2492 FIXME("_centroid modifier not handled\n");
2495 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
2497 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2501 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
2503 switch (op)
2505 case WINED3D_SHADER_REL_OP_GT: return ">";
2506 case WINED3D_SHADER_REL_OP_EQ: return "==";
2507 case WINED3D_SHADER_REL_OP_GE: return ">=";
2508 case WINED3D_SHADER_REL_OP_LT: return "<";
2509 case WINED3D_SHADER_REL_OP_NE: return "!=";
2510 case WINED3D_SHADER_REL_OP_LE: return "<=";
2511 default:
2512 FIXME("Unrecognized operator %#x.\n", op);
2513 return "(\?\?)";
2517 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
2518 DWORD resource_idx, DWORD flags, struct glsl_sample_function *sample_function)
2520 static const struct
2522 unsigned int coord_size;
2523 const char *type_part;
2525 resource_types[] =
2527 {0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
2528 {1, ""}, /* WINED3D_SHADER_RESOURCE_BUFFER */
2529 {1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
2530 {2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
2531 {2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
2532 {3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
2533 {3, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
2534 {2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
2535 {3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
2536 {3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
2538 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2539 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
2540 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2541 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
2542 && (priv->cur_ps_args->shadow & (1u << resource_idx));
2543 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
2544 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2545 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
2546 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
2547 const char *base = "texture", *type_part = "", *suffix = "";
2548 unsigned int coord_size;
2550 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
2552 if (resource_type >= ARRAY_SIZE(resource_types))
2554 ERR("Unexpected resource type %#x.\n", resource_type);
2555 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
2558 /* Note that there's no such thing as a projected cube texture. */
2559 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
2560 projected = FALSE;
2562 if (shadow)
2563 base = "shadow";
2565 type_part = resource_types[resource_type].type_part;
2566 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
2567 type_part = "2DRect";
2568 if (!type_part[0])
2569 FIXME("Unhandled resource type %#x.\n", resource_type);
2571 if (!lod && grad && !gl_info->supported[EXT_GPU_SHADER4])
2573 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2574 suffix = "ARB";
2575 else
2576 FIXME("Unsupported grad function.\n");
2579 sample_function->name = string_buffer_get(priv->string_buffers);
2580 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
2581 lod ? "Lod" : grad ? "Grad" : "", suffix);
2583 coord_size = resource_types[resource_type].coord_size;
2584 if (shadow)
2585 ++coord_size;
2586 sample_function->coord_mask = (1u << coord_size) - 1;
2589 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
2590 struct glsl_sample_function *sample_function)
2592 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2594 string_buffer_release(priv->string_buffers, sample_function->name);
2597 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2598 BOOL sign_fixup, enum fixup_channel_source channel_source)
2600 switch(channel_source)
2602 case CHANNEL_SOURCE_ZERO:
2603 strcat(arguments, "0.0");
2604 break;
2606 case CHANNEL_SOURCE_ONE:
2607 strcat(arguments, "1.0");
2608 break;
2610 case CHANNEL_SOURCE_X:
2611 strcat(arguments, reg_name);
2612 strcat(arguments, ".x");
2613 break;
2615 case CHANNEL_SOURCE_Y:
2616 strcat(arguments, reg_name);
2617 strcat(arguments, ".y");
2618 break;
2620 case CHANNEL_SOURCE_Z:
2621 strcat(arguments, reg_name);
2622 strcat(arguments, ".z");
2623 break;
2625 case CHANNEL_SOURCE_W:
2626 strcat(arguments, reg_name);
2627 strcat(arguments, ".w");
2628 break;
2630 default:
2631 FIXME("Unhandled channel source %#x\n", channel_source);
2632 strcat(arguments, "undefined");
2633 break;
2636 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2639 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
2640 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2642 unsigned int mask_size, remaining;
2643 DWORD fixup_mask = 0;
2644 char arguments[256];
2645 char mask_str[6];
2647 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2648 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2649 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2650 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2651 if (!(mask &= fixup_mask))
2652 return;
2654 if (is_complex_fixup(fixup))
2656 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2657 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2658 return;
2661 shader_glsl_write_mask_to_str(mask, mask_str);
2662 mask_size = shader_glsl_get_write_mask_size(mask);
2664 arguments[0] = '\0';
2665 remaining = mask_size;
2666 if (mask & WINED3DSP_WRITEMASK_0)
2668 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2669 if (--remaining) strcat(arguments, ", ");
2671 if (mask & WINED3DSP_WRITEMASK_1)
2673 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2674 if (--remaining) strcat(arguments, ", ");
2676 if (mask & WINED3DSP_WRITEMASK_2)
2678 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2679 if (--remaining) strcat(arguments, ", ");
2681 if (mask & WINED3DSP_WRITEMASK_3)
2683 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2684 if (--remaining) strcat(arguments, ", ");
2687 if (mask_size > 1)
2688 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2689 else
2690 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2693 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2695 char reg_name[256];
2696 BOOL is_color;
2698 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2699 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2702 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2703 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2704 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2706 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2707 char dst_swizzle[6];
2708 struct color_fixup_desc fixup;
2709 BOOL np2_fixup = FALSE;
2710 va_list args;
2711 int ret;
2713 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2715 /* FIXME: We currently don't support fixups for vertex shaders or anything
2716 * above SM3. Note that for SM4+ the sampler index doesn't have to match
2717 * the resource index. */
2718 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
2720 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2721 fixup = priv->cur_ps_args->color_fixup[sampler];
2723 if (priv->cur_ps_args->np2_fixup & (1u << sampler))
2724 np2_fixup = TRUE;
2726 else
2728 fixup = COLOR_FIXUP_IDENTITY;
2731 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2733 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2734 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler);
2736 for (;;)
2738 va_start(args, coord_reg_fmt);
2739 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2740 va_end(args);
2741 if (!ret)
2742 break;
2743 if (!string_buffer_resize(ins->ctx->buffer, ret))
2744 break;
2747 if (np2_fixup)
2749 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2750 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2752 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
2754 case 1:
2755 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2756 idx >> 1, (idx % 2) ? "z" : "x");
2757 break;
2758 case 2:
2759 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2760 idx >> 1, (idx % 2) ? "zw" : "xy");
2761 break;
2762 case 3:
2763 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
2764 idx >> 1, (idx % 2) ? "zw" : "xy");
2765 break;
2766 case 4:
2767 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
2768 idx >> 1, (idx % 2) ? "zw" : "xy");
2769 break;
2772 if (dx && dy)
2773 shader_addline(ins->ctx->buffer, ", %s, %s)", dx, dy);
2774 else if (bias)
2775 shader_addline(ins->ctx->buffer, ", %s)", bias);
2776 else
2777 shader_addline(ins->ctx->buffer, ")");
2779 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
2781 if (!is_identity_fixup(fixup))
2782 shader_glsl_color_correction(ins, fixup);
2785 /*****************************************************************************
2786 * Begin processing individual instruction opcodes
2787 ****************************************************************************/
2789 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2791 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2792 struct glsl_src_param src0_param;
2793 struct glsl_src_param src1_param;
2794 DWORD write_mask;
2795 const char *op;
2797 /* Determine the GLSL operator to use based on the opcode */
2798 switch (ins->handler_idx)
2800 case WINED3DSIH_ADD: op = "+"; break;
2801 case WINED3DSIH_AND: op = "&"; break;
2802 case WINED3DSIH_DIV: op = "/"; break;
2803 case WINED3DSIH_IADD: op = "+"; break;
2804 case WINED3DSIH_ISHL: op = "<<"; break;
2805 case WINED3DSIH_MUL: op = "*"; break;
2806 case WINED3DSIH_OR: op = "|"; break;
2807 case WINED3DSIH_SUB: op = "-"; break;
2808 case WINED3DSIH_USHR: op = ">>"; break;
2809 case WINED3DSIH_XOR: op = "^"; break;
2810 default:
2811 op = "<unhandled operator>";
2812 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2813 break;
2816 write_mask = shader_glsl_append_dst(buffer, ins);
2817 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2818 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2819 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2822 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2824 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2825 struct glsl_src_param src0_param;
2826 struct glsl_src_param src1_param;
2827 unsigned int mask_size;
2828 DWORD write_mask;
2829 const char *op;
2831 write_mask = shader_glsl_append_dst(buffer, ins);
2832 mask_size = shader_glsl_get_write_mask_size(write_mask);
2833 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2834 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2836 if (mask_size > 1)
2838 switch (ins->handler_idx)
2840 case WINED3DSIH_EQ: op = "equal"; break;
2841 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2842 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2843 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
2844 case WINED3DSIH_LT: op = "lessThan"; break;
2845 case WINED3DSIH_NE: op = "notEqual"; break;
2846 default:
2847 op = "<unhandled operator>";
2848 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2849 break;
2852 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2853 mask_size, op, src0_param.param_str, src1_param.param_str);
2855 else
2857 switch (ins->handler_idx)
2859 case WINED3DSIH_EQ: op = "=="; break;
2860 case WINED3DSIH_GE: op = ">="; break;
2861 case WINED3DSIH_IGE: op = ">="; break;
2862 case WINED3DSIH_UGE: op = ">="; break;
2863 case WINED3DSIH_LT: op = "<"; break;
2864 case WINED3DSIH_NE: op = "!="; break;
2865 default:
2866 op = "<unhandled operator>";
2867 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2868 break;
2871 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2872 src0_param.param_str, op, src1_param.param_str);
2876 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2878 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2879 struct glsl_src_param src0_param;
2880 struct glsl_src_param src1_param;
2881 DWORD write_mask;
2883 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2884 * not, we can emulate it. */
2885 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2886 FIXME("64-bit integer multiplies not implemented.\n");
2888 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2890 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2891 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2892 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2894 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2895 src0_param.param_str, src1_param.param_str);
2899 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2901 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2902 struct glsl_src_param src0_param, src1_param;
2903 DWORD write_mask;
2905 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2908 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2910 char dst_mask[6];
2912 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2913 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2914 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2915 shader_addline(buffer, "tmp0%s = %s / %s;\n",
2916 dst_mask, src0_param.param_str, src1_param.param_str);
2918 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2919 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2920 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2921 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
2923 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2924 shader_addline(buffer, "tmp0%s);\n", dst_mask);
2926 else
2928 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2929 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2930 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2931 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
2934 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2936 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2937 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2938 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2939 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
2943 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2944 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2946 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2947 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2948 struct glsl_src_param src0_param;
2949 DWORD write_mask;
2951 write_mask = shader_glsl_append_dst(buffer, ins);
2952 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2954 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2955 * shader versions WINED3DSIO_MOVA is used for this. */
2956 if (ins->ctx->reg_maps->shader_version.major == 1
2957 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
2958 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2960 /* This is a simple floor() */
2961 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2962 if (mask_size > 1) {
2963 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2964 } else {
2965 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2968 else if(ins->handler_idx == WINED3DSIH_MOVA)
2970 /* We need to *round* to the nearest int here. */
2971 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2973 if (gl_info->supported[EXT_GPU_SHADER4])
2975 if (mask_size > 1)
2976 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2977 else
2978 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2980 else
2982 if (mask_size > 1)
2983 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2984 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2985 else
2986 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2987 src0_param.param_str, src0_param.param_str);
2990 else
2992 shader_addline(buffer, "%s);\n", src0_param.param_str);
2996 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2997 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2999 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3000 struct glsl_src_param src0_param;
3001 struct glsl_src_param src1_param;
3002 DWORD dst_write_mask, src_write_mask;
3003 unsigned int dst_size = 0;
3005 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3006 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3008 /* dp4 works on vec4, dp3 on vec3, etc. */
3009 if (ins->handler_idx == WINED3DSIH_DP4)
3010 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3011 else if (ins->handler_idx == WINED3DSIH_DP3)
3012 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3013 else
3014 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3016 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3017 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3019 if (dst_size > 1) {
3020 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3021 } else {
3022 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3026 /* Note that this instruction has some restrictions. The destination write mask
3027 * can't contain the w component, and the source swizzles have to be .xyzw */
3028 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3030 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3031 struct glsl_src_param src0_param;
3032 struct glsl_src_param src1_param;
3033 char dst_mask[6];
3035 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3036 shader_glsl_append_dst(ins->ctx->buffer, ins);
3037 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3038 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3039 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3042 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3044 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3047 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3048 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3049 * GLSL uses the value as-is. */
3050 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3052 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3053 struct glsl_src_param src0_param;
3054 struct glsl_src_param src1_param;
3055 DWORD dst_write_mask;
3056 unsigned int dst_size;
3058 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3059 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3061 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3062 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3064 if (dst_size > 1)
3066 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3067 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3069 else
3071 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3072 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3076 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3077 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3079 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3080 struct glsl_src_param src_param;
3081 const char *instruction;
3082 DWORD write_mask;
3083 unsigned i;
3085 /* Determine the GLSL function to use based on the opcode */
3086 /* TODO: Possibly make this a table for faster lookups */
3087 switch (ins->handler_idx)
3089 case WINED3DSIH_MIN: instruction = "min"; break;
3090 case WINED3DSIH_MAX: instruction = "max"; break;
3091 case WINED3DSIH_ABS: instruction = "abs"; break;
3092 case WINED3DSIH_FRC: instruction = "fract"; break;
3093 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3094 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3095 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3096 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3097 default: instruction = "";
3098 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
3099 break;
3102 write_mask = shader_glsl_append_dst(buffer, ins);
3104 shader_addline(buffer, "%s(", instruction);
3106 if (ins->src_count)
3108 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3109 shader_addline(buffer, "%s", src_param.param_str);
3110 for (i = 1; i < ins->src_count; ++i)
3112 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3113 shader_addline(buffer, ", %s", src_param.param_str);
3117 shader_addline(buffer, "));\n");
3120 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3122 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3124 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3125 struct glsl_src_param src_param;
3126 unsigned int mask_size;
3127 DWORD write_mask;
3128 char dst_mask[6];
3130 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3131 mask_size = shader_glsl_get_write_mask_size(write_mask);
3132 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3134 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3135 src_param.param_str, src_param.param_str);
3136 shader_glsl_append_dst(buffer, ins);
3138 if (mask_size > 1)
3140 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3141 mask_size, src_param.param_str);
3143 else
3145 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3146 src_param.param_str);
3150 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3152 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3153 struct glsl_src_param src0_param;
3154 const char *prefix, *suffix;
3155 unsigned int dst_size;
3156 DWORD dst_write_mask;
3158 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3159 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3161 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src0_param);
3163 switch (ins->handler_idx)
3165 case WINED3DSIH_EXP:
3166 case WINED3DSIH_EXPP:
3167 prefix = "exp2(";
3168 suffix = ")";
3169 break;
3171 case WINED3DSIH_LOG:
3172 case WINED3DSIH_LOGP:
3173 prefix = "log2(abs(";
3174 suffix = "))";
3175 break;
3177 case WINED3DSIH_RCP:
3178 prefix = "1.0 / ";
3179 suffix = "";
3180 break;
3182 case WINED3DSIH_RSQ:
3183 prefix = "inversesqrt(abs(";
3184 suffix = "))";
3185 break;
3187 default:
3188 prefix = "";
3189 suffix = "";
3190 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3191 break;
3194 if (dst_size > 1)
3195 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3196 else
3197 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3200 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3201 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3202 * dst.x = 2^(floor(src))
3203 * dst.y = src - floor(src)
3204 * dst.z = 2^src (partial precision is allowed, but optional)
3205 * dst.w = 1.0;
3206 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3207 * dst = 2^src; (partial precision is allowed, but optional)
3209 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
3211 if (ins->ctx->reg_maps->shader_version.major < 2)
3213 struct glsl_src_param src_param;
3214 char dst_mask[6];
3216 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
3218 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
3219 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
3220 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
3221 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
3223 shader_glsl_append_dst(ins->ctx->buffer, ins);
3224 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3225 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
3226 return;
3229 shader_glsl_scalar_op(ins);
3232 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
3234 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3235 struct glsl_src_param src_param;
3236 unsigned int mask_size;
3237 DWORD write_mask;
3239 write_mask = shader_glsl_append_dst(buffer, ins);
3240 mask_size = shader_glsl_get_write_mask_size(write_mask);
3241 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3243 if (mask_size > 1)
3244 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
3245 else
3246 shader_addline(buffer, "int(%s));\n", src_param.param_str);
3249 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
3251 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3252 struct glsl_src_param src_param;
3253 unsigned int mask_size;
3254 DWORD write_mask;
3256 write_mask = shader_glsl_append_dst(buffer, ins);
3257 mask_size = shader_glsl_get_write_mask_size(write_mask);
3258 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3260 if (mask_size > 1)
3261 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
3262 else
3263 shader_addline(buffer, "float(%s));\n", src_param.param_str);
3266 /** Process signed comparison opcodes in GLSL. */
3267 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
3269 struct glsl_src_param src0_param;
3270 struct glsl_src_param src1_param;
3271 DWORD write_mask;
3272 unsigned int mask_size;
3274 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3275 mask_size = shader_glsl_get_write_mask_size(write_mask);
3276 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3277 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3279 if (mask_size > 1) {
3280 const char *compare;
3282 switch(ins->handler_idx)
3284 case WINED3DSIH_SLT: compare = "lessThan"; break;
3285 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
3286 default: compare = "";
3287 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
3290 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
3291 src0_param.param_str, src1_param.param_str);
3292 } else {
3293 switch(ins->handler_idx)
3295 case WINED3DSIH_SLT:
3296 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3297 * to return 0.0 but step returns 1.0 because step is not < x
3298 * An alternative is a bvec compare padded with an unused second component.
3299 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3300 * issue. Playing with not() is not possible either because not() does not accept
3301 * a scalar.
3303 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
3304 src0_param.param_str, src1_param.param_str);
3305 break;
3306 case WINED3DSIH_SGE:
3307 /* Here we can use the step() function and safe a conditional */
3308 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
3309 break;
3310 default:
3311 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
3317 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
3319 const char *condition_prefix, *condition_suffix;
3320 struct wined3d_shader_dst_param dst;
3321 struct glsl_src_param src0_param;
3322 struct glsl_src_param src1_param;
3323 struct glsl_src_param src2_param;
3324 BOOL temp_destination = FALSE;
3325 DWORD cmp_channel = 0;
3326 unsigned int i, j;
3327 char mask_char[6];
3328 DWORD write_mask;
3330 switch (ins->handler_idx)
3332 case WINED3DSIH_CMP:
3333 condition_prefix = "";
3334 condition_suffix = " >= 0.0";
3335 break;
3337 case WINED3DSIH_CND:
3338 condition_prefix = "";
3339 condition_suffix = " > 0.5";
3340 break;
3342 case WINED3DSIH_MOVC:
3343 condition_prefix = "bool(";
3344 condition_suffix = ")";
3345 break;
3347 default:
3348 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3349 condition_prefix = "<unhandled prefix>";
3350 condition_suffix = "<unhandled suffix>";
3351 break;
3354 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
3356 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3357 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3358 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3359 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3361 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3362 condition_prefix, src0_param.param_str, condition_suffix,
3363 src1_param.param_str, src2_param.param_str);
3364 return;
3367 dst = ins->dst[0];
3369 /* Splitting the instruction up in multiple lines imposes a problem:
3370 * The first lines may overwrite source parameters of the following lines.
3371 * Deal with that by using a temporary destination register if needed. */
3372 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
3373 && ins->src[0].reg.type == dst.reg.type)
3374 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
3375 && ins->src[1].reg.type == dst.reg.type)
3376 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
3377 && ins->src[2].reg.type == dst.reg.type))
3378 temp_destination = TRUE;
3380 /* Cycle through all source0 channels. */
3381 for (i = 0; i < 4; ++i)
3383 write_mask = 0;
3384 /* Find the destination channels which use the current source0 channel. */
3385 for (j = 0; j < 4; ++j)
3387 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
3389 write_mask |= WINED3DSP_WRITEMASK_0 << j;
3390 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
3393 dst.write_mask = ins->dst[0].write_mask & write_mask;
3395 if (temp_destination)
3397 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
3398 continue;
3399 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
3401 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
3402 continue;
3404 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
3405 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3406 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3408 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3409 condition_prefix, src0_param.param_str, condition_suffix,
3410 src1_param.param_str, src2_param.param_str);
3413 if (temp_destination)
3415 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3416 shader_glsl_append_dst(ins->ctx->buffer, ins);
3417 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3421 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3422 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3423 * the compare is done per component of src0. */
3424 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3426 struct glsl_src_param src0_param;
3427 struct glsl_src_param src1_param;
3428 struct glsl_src_param src2_param;
3429 DWORD write_mask;
3430 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3431 ins->ctx->reg_maps->shader_version.minor);
3433 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3435 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3436 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3437 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3438 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3440 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
3441 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3442 else
3443 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3444 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3445 return;
3448 shader_glsl_conditional_move(ins);
3451 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3452 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3454 struct glsl_src_param src0_param;
3455 struct glsl_src_param src1_param;
3456 struct glsl_src_param src2_param;
3457 DWORD write_mask;
3459 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3460 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3461 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3462 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3463 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3464 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3467 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3468 Vertex shaders to GLSL codes */
3469 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3471 int i;
3472 int nComponents = 0;
3473 struct wined3d_shader_dst_param tmp_dst = {{0}};
3474 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3475 struct wined3d_shader_instruction tmp_ins;
3477 memset(&tmp_ins, 0, sizeof(tmp_ins));
3479 /* Set constants for the temporary argument */
3480 tmp_ins.ctx = ins->ctx;
3481 tmp_ins.dst_count = 1;
3482 tmp_ins.dst = &tmp_dst;
3483 tmp_ins.src_count = 2;
3484 tmp_ins.src = tmp_src;
3486 switch(ins->handler_idx)
3488 case WINED3DSIH_M4x4:
3489 nComponents = 4;
3490 tmp_ins.handler_idx = WINED3DSIH_DP4;
3491 break;
3492 case WINED3DSIH_M4x3:
3493 nComponents = 3;
3494 tmp_ins.handler_idx = WINED3DSIH_DP4;
3495 break;
3496 case WINED3DSIH_M3x4:
3497 nComponents = 4;
3498 tmp_ins.handler_idx = WINED3DSIH_DP3;
3499 break;
3500 case WINED3DSIH_M3x3:
3501 nComponents = 3;
3502 tmp_ins.handler_idx = WINED3DSIH_DP3;
3503 break;
3504 case WINED3DSIH_M3x2:
3505 nComponents = 2;
3506 tmp_ins.handler_idx = WINED3DSIH_DP3;
3507 break;
3508 default:
3509 break;
3512 tmp_dst = ins->dst[0];
3513 tmp_src[0] = ins->src[0];
3514 tmp_src[1] = ins->src[1];
3515 for (i = 0; i < nComponents; ++i)
3517 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3518 shader_glsl_dot(&tmp_ins);
3519 ++tmp_src[1].reg.idx[0].offset;
3524 The LRP instruction performs a component-wise linear interpolation
3525 between the second and third operands using the first operand as the
3526 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3527 This is equivalent to mix(src2, src1, src0);
3529 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3531 struct glsl_src_param src0_param;
3532 struct glsl_src_param src1_param;
3533 struct glsl_src_param src2_param;
3534 DWORD write_mask;
3536 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3538 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3539 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3540 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3542 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3543 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3546 /** Process the WINED3DSIO_LIT instruction in GLSL:
3547 * dst.x = dst.w = 1.0
3548 * dst.y = (src0.x > 0) ? src0.x
3549 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3550 * where src.w is clamped at +- 128
3552 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3554 struct glsl_src_param src0_param;
3555 struct glsl_src_param src1_param;
3556 struct glsl_src_param src3_param;
3557 char dst_mask[6];
3559 shader_glsl_append_dst(ins->ctx->buffer, ins);
3560 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3562 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3563 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3564 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3566 /* The sdk specifies the instruction like this
3567 * dst.x = 1.0;
3568 * if(src.x > 0.0) dst.y = src.x
3569 * else dst.y = 0.0.
3570 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3571 * else dst.z = 0.0;
3572 * dst.w = 1.0;
3573 * (where power = src.w clamped between -128 and 128)
3575 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3576 * dst.x = 1.0 ... No further explanation needed
3577 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3578 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3579 * dst.w = 1.0. ... Nothing fancy.
3581 * So we still have one conditional in there. So do this:
3582 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3584 * 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),
3585 * 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.
3586 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3588 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3589 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3590 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3592 shader_addline(ins->ctx->buffer,
3593 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3594 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3595 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3596 src0_param.param_str, src3_param.param_str, dst_mask);
3599 /** Process the WINED3DSIO_DST instruction in GLSL:
3600 * dst.x = 1.0
3601 * dst.y = src0.x * src0.y
3602 * dst.z = src0.z
3603 * dst.w = src1.w
3605 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3607 struct glsl_src_param src0y_param;
3608 struct glsl_src_param src0z_param;
3609 struct glsl_src_param src1y_param;
3610 struct glsl_src_param src1w_param;
3611 char dst_mask[6];
3613 shader_glsl_append_dst(ins->ctx->buffer, ins);
3614 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3616 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3617 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3618 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3619 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3621 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3622 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3625 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3626 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3627 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3629 * dst.x = cos(src0.?)
3630 * dst.y = sin(src0.?)
3631 * dst.z = dst.z
3632 * dst.w = dst.w
3634 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3636 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3637 struct glsl_src_param src0_param;
3638 DWORD write_mask;
3640 if (ins->ctx->reg_maps->shader_version.major < 4)
3642 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3644 write_mask = shader_glsl_append_dst(buffer, ins);
3645 switch (write_mask)
3647 case WINED3DSP_WRITEMASK_0:
3648 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3649 break;
3651 case WINED3DSP_WRITEMASK_1:
3652 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3653 break;
3655 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3656 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3657 src0_param.param_str, src0_param.param_str);
3658 break;
3660 default:
3661 ERR("Write mask should be .x, .y or .xy\n");
3662 break;
3665 return;
3668 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3671 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3673 char dst_mask[6];
3675 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3676 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3677 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3679 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3680 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3681 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3683 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3684 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3686 else
3688 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3689 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3690 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3693 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3695 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3696 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3697 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3701 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3702 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3703 * generate invalid code
3705 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3707 struct glsl_src_param src0_param;
3708 DWORD write_mask;
3710 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3711 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3713 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3716 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3717 * Start a for() loop where src1.y is the initial value of aL,
3718 * increment aL by src1.z for a total of src1.x iterations.
3719 * Need to use a temporary variable for this operation.
3721 /* FIXME: I don't think nested loops will work correctly this way. */
3722 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3724 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3725 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3726 const struct wined3d_shader *shader = ins->ctx->shader;
3727 const struct wined3d_shader_lconst *constant;
3728 struct glsl_src_param src1_param;
3729 const DWORD *control_values = NULL;
3731 if (ins->ctx->reg_maps->shader_version.major < 4)
3733 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3735 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3736 * class hardware doesn't support real varying indexing, but Microsoft
3737 * designed this feature for Shader model 2.x+. If the loop control is
3738 * known at compile time, the GLSL compiler can unroll the loop, and
3739 * replace indirect addressing with direct addressing. */
3740 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3742 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3744 if (constant->idx == ins->src[1].reg.idx[0].offset)
3746 control_values = constant->value;
3747 break;
3752 if (control_values)
3754 struct wined3d_shader_loop_control loop_control;
3755 loop_control.count = control_values[0];
3756 loop_control.start = control_values[1];
3757 loop_control.step = (int)control_values[2];
3759 if (loop_control.step > 0)
3761 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3762 loop_state->current_depth, loop_control.start,
3763 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3764 loop_state->current_depth, loop_control.step);
3766 else if (loop_control.step < 0)
3768 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3769 loop_state->current_depth, loop_control.start,
3770 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3771 loop_state->current_depth, loop_control.step);
3773 else
3775 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3776 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3777 loop_state->current_depth, loop_control.count,
3778 loop_state->current_depth);
3781 else
3783 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3784 loop_state->current_depth, loop_state->current_reg,
3785 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3786 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3789 ++loop_state->current_reg;
3791 else
3793 shader_addline(buffer, "for (;;)\n{\n");
3796 ++loop_state->current_depth;
3799 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3801 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3803 shader_addline(ins->ctx->buffer, "}\n");
3805 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3807 --loop_state->current_depth;
3808 --loop_state->current_reg;
3811 if (ins->handler_idx == WINED3DSIH_ENDREP)
3813 --loop_state->current_depth;
3817 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3819 const struct wined3d_shader *shader = ins->ctx->shader;
3820 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3821 const struct wined3d_shader_lconst *constant;
3822 struct glsl_src_param src0_param;
3823 const DWORD *control_values = NULL;
3825 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3826 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3828 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3830 if (constant->idx == ins->src[0].reg.idx[0].offset)
3832 control_values = constant->value;
3833 break;
3838 if (control_values)
3840 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3841 loop_state->current_depth, loop_state->current_depth,
3842 control_values[0], loop_state->current_depth);
3844 else
3846 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3847 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3848 loop_state->current_depth, loop_state->current_depth,
3849 src0_param.param_str, loop_state->current_depth);
3852 ++loop_state->current_depth;
3855 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3857 struct glsl_src_param src0_param;
3859 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3860 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
3863 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3865 struct glsl_src_param src0_param;
3866 struct glsl_src_param src1_param;
3868 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3869 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3871 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3872 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3875 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3877 shader_addline(ins->ctx->buffer, "} else {\n");
3880 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3882 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3885 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3887 shader_addline(ins->ctx->buffer, "break;\n");
3890 /* FIXME: According to MSDN the compare is done per component. */
3891 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3893 struct glsl_src_param src0_param;
3894 struct glsl_src_param src1_param;
3896 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3897 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3899 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3900 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3903 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
3905 struct glsl_src_param src_param;
3907 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
3908 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
3911 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
3913 shader_addline(ins->ctx->buffer, "}\n");
3914 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
3917 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
3919 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
3922 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3924 struct glsl_src_param src1_param;
3926 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3927 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
3928 src1_param.param_str, ins->src[0].reg.idx[0].offset);
3931 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3933 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3934 * function only suppresses the unhandled instruction warning
3938 /*********************************************
3939 * Pixel Shader Specific Code begins here
3940 ********************************************/
3941 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3943 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3944 ins->ctx->reg_maps->shader_version.minor);
3945 struct glsl_sample_function sample_function;
3946 DWORD sample_flags = 0;
3947 DWORD resource_idx;
3948 DWORD mask = 0, swizzle;
3949 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3951 /* 1.0-1.4: Use destination register as sampler source.
3952 * 2.0+: Use provided sampler source. */
3953 if (shader_version < WINED3D_SHADER_VERSION(2,0))
3954 resource_idx = ins->dst[0].reg.idx[0].offset;
3955 else
3956 resource_idx = ins->src[1].reg.idx[0].offset;
3958 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3960 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3961 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3962 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
3964 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3965 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3967 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3968 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3970 case WINED3D_TTFF_COUNT1:
3971 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3972 break;
3973 case WINED3D_TTFF_COUNT2:
3974 mask = WINED3DSP_WRITEMASK_1;
3975 break;
3976 case WINED3D_TTFF_COUNT3:
3977 mask = WINED3DSP_WRITEMASK_2;
3978 break;
3979 case WINED3D_TTFF_COUNT4:
3980 case WINED3D_TTFF_DISABLE:
3981 mask = WINED3DSP_WRITEMASK_3;
3982 break;
3986 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3988 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3990 if (src_mod == WINED3DSPSM_DZ) {
3991 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3992 mask = WINED3DSP_WRITEMASK_2;
3993 } else if (src_mod == WINED3DSPSM_DW) {
3994 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3995 mask = WINED3DSP_WRITEMASK_3;
3998 else
4000 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4001 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4003 /* ps 2.0 texldp instruction always divides by the fourth component. */
4004 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4005 mask = WINED3DSP_WRITEMASK_3;
4009 if (priv->cur_ps_args->np2_fixup & (1u << resource_idx))
4010 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4012 shader_glsl_get_sample_function(ins->ctx, resource_idx, sample_flags, &sample_function);
4013 mask |= sample_function.coord_mask;
4014 sample_function.coord_mask = mask;
4016 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4017 else swizzle = ins->src[1].swizzle;
4019 /* 1.0-1.3: Use destination register as coordinate source.
4020 1.4+: Use provided coordinate source register. */
4021 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4023 char coord_mask[6];
4024 shader_glsl_write_mask_to_str(mask, coord_mask);
4025 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
4026 "T%u%s", resource_idx, coord_mask);
4028 else
4030 struct glsl_src_param coord_param;
4031 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4032 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4034 struct glsl_src_param bias;
4035 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4036 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4037 "%s", coord_param.param_str);
4038 } else {
4039 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
4040 "%s", coord_param.param_str);
4043 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4046 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4048 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4049 struct glsl_src_param coord_param, dx_param, dy_param;
4050 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
4051 struct glsl_sample_function sample_function;
4052 DWORD sampler_idx;
4053 DWORD swizzle = ins->src[1].swizzle;
4054 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4056 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
4058 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4059 shader_glsl_tex(ins);
4060 return;
4063 sampler_idx = ins->src[1].reg.idx[0].offset;
4064 if (priv->cur_ps_args->np2_fixup & (1u << sampler_idx))
4065 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4067 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
4068 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4069 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
4070 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
4072 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
4073 "%s", coord_param.param_str);
4074 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4077 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4079 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4080 struct glsl_src_param coord_param, lod_param;
4081 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
4082 struct glsl_sample_function sample_function;
4083 DWORD sampler_idx;
4084 DWORD swizzle = ins->src[1].swizzle;
4085 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4087 sampler_idx = ins->src[1].reg.idx[0].offset;
4088 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
4089 && priv->cur_ps_args->np2_fixup & (1u << sampler_idx))
4090 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4092 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
4093 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4095 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4097 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
4098 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
4100 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4101 * However, the NVIDIA drivers allow them in fragment shaders as well,
4102 * even without the appropriate extension. */
4103 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4105 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
4106 "%s", coord_param.param_str);
4107 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4110 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4111 unsigned int resource_idx, unsigned int sampler_idx)
4113 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4114 unsigned int i;
4116 for (i = 0; i < sampler_map->count; ++i)
4118 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4119 return entries[i].bind_idx;
4122 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4124 return ~0u;
4127 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
4129 struct glsl_sample_function sample_function;
4130 struct glsl_src_param coord_param;
4131 unsigned int sampler_idx;
4133 shader_glsl_get_sample_function(ins->ctx, ins->src[1].reg.idx[0].offset, 0, &sample_function);
4134 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4135 sampler_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4136 ins->src[1].reg.idx[0].offset, ins->src[2].reg.idx[0].offset);
4137 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE,
4138 NULL, NULL, NULL, "%s", coord_param.param_str);
4139 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4142 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
4144 /* FIXME: Make this work for more than just 2D textures */
4145 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4146 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4148 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
4150 char dst_mask[6];
4152 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4153 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
4154 ins->dst[0].reg.idx[0].offset, dst_mask);
4156 else
4158 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4159 DWORD reg = ins->src[0].reg.idx[0].offset;
4160 char dst_swizzle[6];
4162 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
4164 if (src_mod == WINED3DSPSM_DZ)
4166 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4167 struct glsl_src_param div_param;
4169 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
4171 if (mask_size > 1) {
4172 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4173 } else {
4174 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4177 else if (src_mod == WINED3DSPSM_DW)
4179 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4180 struct glsl_src_param div_param;
4182 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
4184 if (mask_size > 1) {
4185 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4186 } else {
4187 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4189 } else {
4190 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
4195 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4196 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4197 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4198 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
4200 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4201 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4202 struct glsl_sample_function sample_function;
4203 struct glsl_src_param src0_param;
4204 UINT mask_size;
4206 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4208 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4209 * scalar, and projected sampling would require 4.
4211 * It is a dependent read - not valid with conditional NP2 textures
4213 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4214 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4216 switch(mask_size)
4218 case 1:
4219 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4220 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
4221 break;
4223 case 2:
4224 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4225 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
4226 break;
4228 case 3:
4229 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4230 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
4231 break;
4233 default:
4234 FIXME("Unexpected mask size %u\n", mask_size);
4235 break;
4237 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4240 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4241 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4242 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
4244 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4245 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4246 struct glsl_src_param src0_param;
4247 DWORD dst_mask;
4248 unsigned int mask_size;
4250 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4251 mask_size = shader_glsl_get_write_mask_size(dst_mask);
4252 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4254 if (mask_size > 1) {
4255 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
4256 } else {
4257 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
4261 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4262 * Calculate the depth as dst.x / dst.y */
4263 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
4265 struct glsl_dst_param dst_param;
4267 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4269 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4270 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4271 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4272 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4273 * >= 1.0 or < 0.0
4275 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4276 dst_param.reg_name, dst_param.reg_name);
4279 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4280 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4281 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4282 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4284 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
4286 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4287 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4288 struct glsl_src_param src0_param;
4290 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4292 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
4293 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4296 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4297 * Calculate the 1st of a 2-row matrix multiplication. */
4298 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
4300 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4301 DWORD reg = ins->dst[0].reg.idx[0].offset;
4302 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4303 struct glsl_src_param src0_param;
4305 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4306 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4309 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4310 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4311 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
4313 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4314 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4315 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4316 DWORD reg = ins->dst[0].reg.idx[0].offset;
4317 struct glsl_src_param src0_param;
4319 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4320 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
4321 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
4324 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
4326 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4327 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4328 struct glsl_sample_function sample_function;
4329 DWORD reg = ins->dst[0].reg.idx[0].offset;
4330 struct glsl_src_param src0_param;
4332 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4333 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4335 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4337 /* Sample the texture using the calculated coordinates */
4338 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
4339 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4342 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
4343 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
4344 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
4346 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4347 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4348 struct glsl_sample_function sample_function;
4349 DWORD reg = ins->dst[0].reg.idx[0].offset;
4350 struct glsl_src_param src0_param;
4352 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4353 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4355 /* Dependent read, not valid with conditional NP2 */
4356 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4358 /* Sample the texture using the calculated coordinates */
4359 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
4360 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4362 tex_mx->current_row = 0;
4365 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4366 * Perform the 3rd row of a 3x3 matrix multiply */
4367 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
4369 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4370 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4371 DWORD reg = ins->dst[0].reg.idx[0].offset;
4372 struct glsl_src_param src0_param;
4373 char dst_mask[6];
4375 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4377 shader_glsl_append_dst(ins->ctx->buffer, ins);
4378 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4379 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
4381 tex_mx->current_row = 0;
4384 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4385 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4386 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
4388 struct glsl_src_param src0_param;
4389 struct glsl_src_param src1_param;
4390 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4391 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4392 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4393 struct glsl_sample_function sample_function;
4394 DWORD reg = ins->dst[0].reg.idx[0].offset;
4395 char coord_mask[6];
4397 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4398 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4400 /* Perform the last matrix multiply operation */
4401 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4402 /* Reflection calculation */
4403 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
4405 /* Dependent read, not valid with conditional NP2 */
4406 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4407 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4409 /* Sample the texture */
4410 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4411 NULL, NULL, NULL, "tmp0%s", coord_mask);
4412 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4414 tex_mx->current_row = 0;
4417 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4418 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4419 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
4421 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4422 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4423 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4424 struct glsl_sample_function sample_function;
4425 DWORD reg = ins->dst[0].reg.idx[0].offset;
4426 struct glsl_src_param src0_param;
4427 char coord_mask[6];
4429 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4431 /* Perform the last matrix multiply operation */
4432 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
4434 /* Construct the eye-ray vector from w coordinates */
4435 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
4436 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
4437 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4439 /* Dependent read, not valid with conditional NP2 */
4440 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4441 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4443 /* Sample the texture using the calculated coordinates */
4444 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4445 NULL, NULL, NULL, "tmp0%s", coord_mask);
4446 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4448 tex_mx->current_row = 0;
4451 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4452 * Apply a fake bump map transform.
4453 * texbem is pshader <= 1.3 only, this saves a few version checks
4455 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
4457 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4458 struct glsl_sample_function sample_function;
4459 struct glsl_src_param coord_param;
4460 DWORD sampler_idx;
4461 DWORD mask;
4462 DWORD flags;
4463 char coord_mask[6];
4465 sampler_idx = ins->dst[0].reg.idx[0].offset;
4466 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4467 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4469 /* Dependent read, not valid with conditional NP2 */
4470 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4471 mask = sample_function.coord_mask;
4473 shader_glsl_write_mask_to_str(mask, coord_mask);
4475 /* With projected textures, texbem only divides the static texture coord,
4476 * not the displacement, so we can't let GL handle this. */
4477 if (flags & WINED3D_PSARGS_PROJECTED)
4479 DWORD div_mask=0;
4480 char coord_div_mask[3];
4481 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4483 case WINED3D_TTFF_COUNT1:
4484 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4485 break;
4486 case WINED3D_TTFF_COUNT2:
4487 div_mask = WINED3DSP_WRITEMASK_1;
4488 break;
4489 case WINED3D_TTFF_COUNT3:
4490 div_mask = WINED3DSP_WRITEMASK_2;
4491 break;
4492 case WINED3D_TTFF_COUNT4:
4493 case WINED3D_TTFF_DISABLE:
4494 div_mask = WINED3DSP_WRITEMASK_3;
4495 break;
4497 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4498 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4501 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4503 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4504 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4505 coord_param.param_str, coord_mask);
4507 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4509 struct glsl_src_param luminance_param;
4510 struct glsl_dst_param dst_param;
4512 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4513 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4515 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4516 dst_param.reg_name, dst_param.mask_str,
4517 luminance_param.param_str, sampler_idx, sampler_idx);
4519 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4522 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4524 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4525 struct glsl_src_param src0_param, src1_param;
4527 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4528 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4530 shader_glsl_append_dst(ins->ctx->buffer, ins);
4531 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4532 src0_param.param_str, sampler_idx, src1_param.param_str);
4535 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4536 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4537 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4539 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4540 struct glsl_sample_function sample_function;
4541 struct glsl_src_param src0_param;
4543 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4545 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4546 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4547 "%s.wx", src0_param.reg_name);
4548 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4551 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4552 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4553 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4555 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4556 struct glsl_sample_function sample_function;
4557 struct glsl_src_param src0_param;
4559 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4561 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4562 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4563 "%s.yz", src0_param.reg_name);
4564 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4567 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4568 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4569 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4571 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4572 struct glsl_sample_function sample_function;
4573 struct glsl_src_param src0_param;
4575 /* Dependent read, not valid with conditional NP2 */
4576 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4577 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4579 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4580 "%s", src0_param.param_str);
4581 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4584 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4585 * If any of the first 3 components are < 0, discard this pixel */
4586 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4588 if (ins->ctx->reg_maps->shader_version.major >= 4)
4590 struct glsl_src_param src_param;
4592 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4593 shader_addline(ins->ctx->buffer, "if (bool(floatBitsToUint(%s))) discard;\n", src_param.param_str);
4595 else
4597 struct glsl_dst_param dst_param;
4599 /* The argument is a destination parameter, and no writemasks are allowed */
4600 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4602 /* 2.0 shaders compare all 4 components in texkill. */
4603 if (ins->ctx->reg_maps->shader_version.major >= 2)
4604 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4605 /* 1.x shaders only compare the first 3 components, probably due to
4606 * the nature of the texkill instruction as a tex* instruction, and
4607 * phase, which kills all .w components. Even if all 4 components are
4608 * defined, only the first 3 are used. */
4609 else
4610 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4614 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4615 * dst = dot2(src0, src1) + src2 */
4616 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4618 struct glsl_src_param src0_param;
4619 struct glsl_src_param src1_param;
4620 struct glsl_src_param src2_param;
4621 DWORD write_mask;
4622 unsigned int mask_size;
4624 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4625 mask_size = shader_glsl_get_write_mask_size(write_mask);
4627 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4628 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4629 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4631 if (mask_size > 1) {
4632 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4633 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4634 } else {
4635 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4636 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4640 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
4641 const struct wined3d_shader_signature *input_signature,
4642 const struct wined3d_shader_reg_maps *reg_maps,
4643 const struct ps_compile_args *args)
4645 unsigned int i;
4647 for (i = 0; i < input_signature->element_count; ++i)
4649 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4650 const char *semantic_name;
4651 UINT semantic_idx;
4652 char reg_mask[6];
4654 /* Unused */
4655 if (!(reg_maps->input_registers & (1u << input->register_idx)))
4656 continue;
4658 semantic_name = input->semantic_name;
4659 semantic_idx = input->semantic_idx;
4660 shader_glsl_write_mask_to_str(input->mask, reg_mask);
4662 if (args->vp_mode == vertexshader)
4664 if (input->sysval_semantic == WINED3D_SV_POSITION)
4665 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
4666 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4667 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4668 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
4669 else
4670 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
4671 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
4672 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
4674 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4676 if (semantic_idx < 8 && args->vp_mode == pretransformed)
4677 shader_addline(buffer, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4678 shader->u.ps.input_reg_map[input->register_idx], reg_mask, semantic_idx, reg_mask);
4679 else
4680 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4681 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4683 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4685 if (!semantic_idx)
4686 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4687 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4688 else if (semantic_idx == 1)
4689 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4690 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4691 else
4692 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4693 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4695 else
4697 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4698 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4703 /*********************************************
4704 * Vertex Shader Specific Code begins here
4705 ********************************************/
4707 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4709 struct glsl_program_key key;
4711 key.vs_id = entry->vs.id;
4712 key.gs_id = entry->gs.id;
4713 key.ps_id = entry->ps.id;
4715 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4717 ERR("Failed to insert program entry.\n");
4721 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4722 GLuint vs_id, GLuint gs_id, GLuint ps_id)
4724 struct wine_rb_entry *entry;
4725 struct glsl_program_key key;
4727 key.vs_id = vs_id;
4728 key.gs_id = gs_id;
4729 key.ps_id = ps_id;
4731 entry = wine_rb_get(&priv->program_lookup, &key);
4732 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4735 /* Context activation is done by the caller. */
4736 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4737 struct glsl_shader_prog_link *entry)
4739 struct glsl_program_key key;
4741 key.vs_id = entry->vs.id;
4742 key.gs_id = entry->gs.id;
4743 key.ps_id = entry->ps.id;
4744 wine_rb_remove(&priv->program_lookup, &key);
4746 GL_EXTCALL(glDeleteProgram(entry->id));
4747 if (entry->vs.id)
4748 list_remove(&entry->vs.shader_entry);
4749 if (entry->gs.id)
4750 list_remove(&entry->gs.shader_entry);
4751 if (entry->ps.id)
4752 list_remove(&entry->ps.shader_entry);
4753 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4754 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4755 HeapFree(GetProcessHeap(), 0, entry);
4758 static void handle_ps3_input(struct shader_glsl_priv *priv,
4759 const struct wined3d_gl_info *gl_info, const DWORD *map,
4760 const struct wined3d_shader_signature *input_signature,
4761 const struct wined3d_shader_reg_maps *reg_maps_in,
4762 const struct wined3d_shader_signature *output_signature,
4763 const struct wined3d_shader_reg_maps *reg_maps_out)
4765 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
4766 unsigned int i, j;
4767 DWORD *set;
4768 DWORD in_idx;
4769 unsigned int in_count = vec4_varyings(3, gl_info);
4770 char reg_mask[6];
4771 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
4773 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
4775 for (i = 0; i < input_signature->element_count; ++i)
4777 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4779 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
4780 continue;
4782 in_idx = map[input->register_idx];
4783 /* Declared, but not read register */
4784 if (in_idx == ~0u)
4785 continue;
4786 if (in_idx >= (in_count + 2))
4788 FIXME("More input varyings declared than supported, expect issues.\n");
4789 continue;
4792 if (in_idx == in_count)
4793 string_buffer_sprintf(destination, "gl_FrontColor");
4794 else if (in_idx == in_count + 1)
4795 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
4796 else
4797 string_buffer_sprintf(destination, "ps_link[%u]", in_idx);
4799 if (!set[in_idx])
4800 set[in_idx] = ~0u;
4802 for (j = 0; j < output_signature->element_count; ++j)
4804 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
4805 DWORD mask;
4807 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
4808 || input->semantic_idx != output->semantic_idx
4809 || strcmp(input->semantic_name, output->semantic_name)
4810 || !(mask = input->mask & output->mask))
4811 continue;
4813 if (set[in_idx] == ~0u)
4814 set[in_idx] = mask;
4815 else
4816 set[in_idx] |= mask;
4817 shader_glsl_write_mask_to_str(mask, reg_mask);
4819 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4820 destination->buffer, reg_mask, output->register_idx, reg_mask);
4824 for (i = 0; i < in_count + 2; ++i)
4826 unsigned int size;
4828 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4829 continue;
4831 if (set[i] == ~0U) set[i] = 0;
4833 size = 0;
4834 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4835 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4836 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4837 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4838 reg_mask[size] = '\0';
4840 if (i == in_count)
4841 string_buffer_sprintf(destination, "gl_FrontColor");
4842 else if (i == in_count + 1)
4843 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
4844 else
4845 string_buffer_sprintf(destination, "ps_link[%u]", i);
4847 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
4848 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
4851 HeapFree(GetProcessHeap(), 0, set);
4852 string_buffer_release(&priv->string_buffers, destination);
4855 /* Context activation is done by the caller. */
4856 static GLuint generate_param_reorder_function(struct shader_glsl_priv *priv,
4857 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4858 BOOL per_vertex_point_size, const struct wined3d_gl_info *gl_info)
4860 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
4861 GLuint ret = 0;
4862 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4863 unsigned int i;
4864 const char *semantic_name;
4865 UINT semantic_idx;
4866 char reg_mask[6];
4868 string_buffer_clear(buffer);
4870 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &vs->reg_maps.shader_version));
4872 if (per_vertex_point_size)
4874 shader_addline(buffer, "uniform struct\n{\n");
4875 shader_addline(buffer, " float size_min;\n");
4876 shader_addline(buffer, " float size_max;\n");
4877 shader_addline(buffer, "} ffp_point;\n");
4880 if (ps_major < 3)
4882 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4884 for (i = 0; i < vs->output_signature.element_count; ++i)
4886 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
4887 DWORD write_mask;
4889 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
4890 continue;
4892 semantic_name = output->semantic_name;
4893 semantic_idx = output->semantic_idx;
4894 write_mask = output->mask;
4895 shader_glsl_write_mask_to_str(write_mask, reg_mask);
4897 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4899 if (!semantic_idx)
4900 shader_addline(buffer, "gl_FrontColor%s = vs_out[%u]%s;\n",
4901 reg_mask, output->register_idx, reg_mask);
4902 else if (semantic_idx == 1)
4903 shader_addline(buffer, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4904 reg_mask, output->register_idx, reg_mask);
4906 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
4908 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4909 reg_mask, output->register_idx, reg_mask);
4911 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4913 if (semantic_idx < 8)
4915 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
4916 write_mask |= WINED3DSP_WRITEMASK_3;
4918 shader_addline(buffer, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4919 semantic_idx, reg_mask, output->register_idx, reg_mask);
4920 if (!(write_mask & WINED3DSP_WRITEMASK_3))
4921 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
4924 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
4926 shader_addline(buffer, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
4927 output->register_idx, reg_mask[1]);
4929 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
4931 shader_addline(buffer, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n",
4932 output->register_idx, reg_mask[1]);
4935 shader_addline(buffer, "}\n");
4937 else
4939 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
4940 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4941 shader_addline(buffer, "varying vec4 ps_link[%u];\n", in_count);
4942 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4944 /* First, sort out position and point size. Those are not passed to the pixel shader */
4945 for (i = 0; i < vs->output_signature.element_count; ++i)
4947 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
4949 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
4950 continue;
4952 semantic_name = output->semantic_name;
4953 semantic_idx = output->semantic_idx;
4954 shader_glsl_write_mask_to_str(output->mask, reg_mask);
4956 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
4958 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4959 reg_mask, output->register_idx, reg_mask);
4961 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
4963 shader_addline(buffer, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
4964 output->register_idx, reg_mask[1]);
4968 /* Then, fix the pixel shader input */
4969 handle_ps3_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
4970 &ps->reg_maps, &vs->output_signature, &vs->reg_maps);
4972 shader_addline(buffer, "}\n");
4975 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
4976 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
4977 shader_glsl_compile(gl_info, ret, buffer->buffer);
4979 return ret;
4982 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer)
4984 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4985 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4986 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4987 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4988 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4989 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4992 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
4994 switch (mode)
4996 case WINED3D_FFP_PS_FOG_OFF:
4997 return;
4999 case WINED3D_FFP_PS_FOG_LINEAR:
5000 shader_addline(buffer, "float fog = (ffp_fog.end - gl_FogFragCoord) * ffp_fog.scale;\n");
5001 break;
5003 case WINED3D_FFP_PS_FOG_EXP:
5004 shader_addline(buffer, "float fog = exp(-ffp_fog.density * gl_FogFragCoord);\n");
5005 break;
5007 case WINED3D_FFP_PS_FOG_EXP2:
5008 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
5009 " * gl_FogFragCoord * gl_FogFragCoord);\n");
5010 break;
5012 default:
5013 ERR("Invalid fog mode %#x.\n", mode);
5014 return;
5017 shader_addline(buffer, "gl_FragData[0].xyz = mix(ffp_fog.color.xyz, gl_FragData[0].xyz,"
5018 " clamp(fog, 0.0, 1.0));\n");
5021 /* Context activation is done by the caller. */
5022 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
5023 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5024 const struct wined3d_shader *shader,
5025 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
5027 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5028 const struct wined3d_gl_info *gl_info = context->gl_info;
5029 const DWORD *function = shader->function;
5030 struct shader_glsl_ctx_priv priv_ctx;
5032 /* Create the hw GLSL shader object and assign it as the shader->prgId */
5033 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5035 memset(&priv_ctx, 0, sizeof(priv_ctx));
5036 priv_ctx.cur_ps_args = args;
5037 priv_ctx.cur_np2fixup_info = np2fixup_info;
5038 priv_ctx.string_buffers = string_buffers;
5040 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5042 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5043 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5044 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5045 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
5046 /* The spec says that it doesn't have to be explicitly enabled, but the
5047 * nvidia drivers write a warning if we don't do so. */
5048 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5049 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5050 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5051 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5052 if (gl_info->supported[EXT_GPU_SHADER4])
5053 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5055 /* Base Declarations */
5056 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5058 /* Pack 3.0 inputs */
5059 if (reg_maps->shader_version.major >= 3)
5060 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args);
5062 /* Base Shader Body */
5063 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5065 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
5066 if (reg_maps->shader_version.major < 2)
5068 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
5069 shader_addline(buffer, "gl_FragData[0] = R0;\n");
5072 if (args->srgb_correction)
5073 shader_glsl_generate_srgb_write_correction(buffer);
5075 /* SM < 3 does not replace the fog stage. */
5076 if (reg_maps->shader_version.major < 3)
5077 shader_glsl_generate_fog_code(buffer, args->fog);
5079 shader_addline(buffer, "}\n");
5081 TRACE("Compiling shader object %u.\n", shader_id);
5082 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5084 return shader_id;
5087 /* Context activation is done by the caller. */
5088 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
5089 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5090 const struct wined3d_shader *shader,
5091 const struct vs_compile_args *args)
5093 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5094 const struct wined3d_gl_info *gl_info = context->gl_info;
5095 const DWORD *function = shader->function;
5096 struct shader_glsl_ctx_priv priv_ctx;
5098 /* Create the hw GLSL shader program and assign it as the shader->prgId */
5099 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5101 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5103 if (gl_info->supported[ARB_DRAW_INSTANCED])
5104 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
5105 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5106 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5107 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5108 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5109 if (gl_info->supported[EXT_GPU_SHADER4])
5110 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5112 memset(&priv_ctx, 0, sizeof(priv_ctx));
5113 priv_ctx.cur_vs_args = args;
5114 priv_ctx.string_buffers = string_buffers;
5116 /* Base Declarations */
5117 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5119 /* Base Shader Body */
5120 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5122 /* Unpack outputs */
5123 shader_addline(buffer, "order_ps_input(vs_out);\n");
5125 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
5126 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
5127 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
5128 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
5130 if (args->fog_src == VS_FOG_Z)
5131 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
5132 else if (!reg_maps->fog)
5133 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
5135 /* We always store the clipplanes without y inversion */
5136 if (args->clip_enabled)
5137 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
5139 if (args->point_size && !args->per_vertex_point_size)
5140 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
5142 /* Write the final position.
5144 * OpenGL coordinates specify the center of the pixel while d3d coords specify
5145 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
5146 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
5147 * contains 1.0 to allow a mad.
5149 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
5150 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
5152 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
5154 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
5155 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
5156 * which is the same as z = z * 2 - w.
5158 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
5160 shader_addline(buffer, "}\n");
5162 TRACE("Compiling shader object %u.\n", shader_id);
5163 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5165 return shader_id;
5168 /* Context activation is done by the caller. */
5169 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
5170 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5171 const struct wined3d_shader *shader)
5173 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5174 const struct wined3d_gl_info *gl_info = context->gl_info;
5175 const DWORD *function = shader->function;
5176 struct shader_glsl_ctx_priv priv_ctx;
5177 GLuint shader_id;
5179 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
5181 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5183 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
5184 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
5185 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5186 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5187 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5188 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5189 if (gl_info->supported[EXT_GPU_SHADER4])
5190 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5192 memset(&priv_ctx, 0, sizeof(priv_ctx));
5193 priv_ctx.string_buffers = string_buffers;
5194 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5195 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5196 shader_addline(buffer, "}\n");
5198 TRACE("Compiling shader object %u.\n", shader_id);
5199 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5201 return shader_id;
5204 static GLuint find_glsl_pshader(const struct wined3d_context *context,
5205 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5206 struct wined3d_shader *shader,
5207 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
5209 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
5210 struct glsl_shader_private *shader_data;
5211 struct ps_np2fixup_info *np2fixup;
5212 UINT i;
5213 DWORD new_size;
5214 GLuint ret;
5216 if (!shader->backend_data)
5218 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5219 if (!shader->backend_data)
5221 ERR("Failed to allocate backend data.\n");
5222 return 0;
5225 shader_data = shader->backend_data;
5226 gl_shaders = shader_data->gl_shaders.ps;
5228 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5229 * so a linear search is more performant than a hashmap or a binary search
5230 * (cache coherency etc)
5232 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5234 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
5236 if (args->np2_fixup)
5237 *np2fixup_info = &gl_shaders[i].np2fixup;
5238 return gl_shaders[i].id;
5242 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5243 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5244 if (shader_data->num_gl_shaders)
5246 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5247 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
5248 new_size * sizeof(*gl_shaders));
5250 else
5252 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5253 new_size = 1;
5256 if(!new_array) {
5257 ERR("Out of memory\n");
5258 return 0;
5260 shader_data->gl_shaders.ps = new_array;
5261 shader_data->shader_array_size = new_size;
5262 gl_shaders = new_array;
5265 gl_shaders[shader_data->num_gl_shaders].args = *args;
5267 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
5268 memset(np2fixup, 0, sizeof(*np2fixup));
5269 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
5271 pixelshader_update_resource_types(shader, args->tex_types);
5273 string_buffer_clear(buffer);
5274 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
5275 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5277 return ret;
5280 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
5281 const DWORD use_map)
5283 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
5284 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
5285 if (stored->point_size != new->point_size)
5286 return FALSE;
5287 if (stored->per_vertex_point_size != new->per_vertex_point_size)
5288 return FALSE;
5289 return stored->fog_src == new->fog_src;
5292 static GLuint find_glsl_vshader(const struct wined3d_context *context,
5293 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5294 struct wined3d_shader *shader,
5295 const struct vs_compile_args *args)
5297 UINT i;
5298 DWORD new_size;
5299 DWORD use_map = context->stream_info.use_map;
5300 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
5301 struct glsl_shader_private *shader_data;
5302 GLuint ret;
5304 if (!shader->backend_data)
5306 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5307 if (!shader->backend_data)
5309 ERR("Failed to allocate backend data.\n");
5310 return 0;
5313 shader_data = shader->backend_data;
5314 gl_shaders = shader_data->gl_shaders.vs;
5316 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5317 * so a linear search is more performant than a hashmap or a binary search
5318 * (cache coherency etc)
5320 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5322 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
5323 return gl_shaders[i].id;
5326 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5328 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5329 if (shader_data->num_gl_shaders)
5331 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5332 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
5333 new_size * sizeof(*gl_shaders));
5335 else
5337 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5338 new_size = 1;
5341 if(!new_array) {
5342 ERR("Out of memory\n");
5343 return 0;
5345 shader_data->gl_shaders.vs = new_array;
5346 shader_data->shader_array_size = new_size;
5347 gl_shaders = new_array;
5350 gl_shaders[shader_data->num_gl_shaders].args = *args;
5352 string_buffer_clear(buffer);
5353 ret = shader_glsl_generate_vshader(context, buffer, string_buffers, shader, args);
5354 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5356 return ret;
5359 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
5360 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5361 struct wined3d_shader *shader)
5363 struct glsl_gs_compiled_shader *gl_shaders;
5364 struct glsl_shader_private *shader_data;
5365 GLuint ret;
5367 if (!shader->backend_data)
5369 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
5371 ERR("Failed to allocate backend data.\n");
5372 return 0;
5375 shader_data = shader->backend_data;
5376 gl_shaders = shader_data->gl_shaders.gs;
5378 if (shader_data->num_gl_shaders)
5379 return gl_shaders[0].id;
5381 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5383 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
5385 ERR("Failed to allocate GL shader array.\n");
5386 return 0;
5388 shader_data->shader_array_size = 1;
5389 gl_shaders = shader_data->gl_shaders.gs;
5391 string_buffer_clear(buffer);
5392 ret = shader_glsl_generate_geometry_shader(context, buffer, string_buffers, shader);
5393 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5395 return ret;
5398 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
5400 switch (mcs)
5402 case WINED3D_MCS_MATERIAL:
5403 return material;
5404 case WINED3D_MCS_COLOR1:
5405 return "ffp_attrib_diffuse";
5406 case WINED3D_MCS_COLOR2:
5407 return "ffp_attrib_specular";
5408 default:
5409 ERR("Invalid material color source %#x.\n", mcs);
5410 return "<invalid>";
5414 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
5415 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
5417 const char *diffuse, *specular, *emissive, *ambient;
5418 enum wined3d_light_type light_type;
5419 unsigned int i;
5421 if (!settings->lighting)
5423 shader_addline(buffer, "gl_FrontColor = ffp_attrib_diffuse;\n");
5424 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_attrib_specular;\n");
5425 return;
5428 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
5429 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
5430 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
5431 shader_addline(buffer, "vec3 dir, dst;\n");
5432 shader_addline(buffer, "float att, t;\n");
5434 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
5435 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
5436 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
5437 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
5439 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
5441 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
5442 switch (light_type)
5444 case WINED3D_LIGHT_POINT:
5445 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5446 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5447 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5448 shader_addline(buffer, "dst.x = 1.0;\n");
5449 if (legacy_lighting)
5451 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
5452 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
5454 else
5456 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5458 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5459 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", i, i, i);
5460 if (!legacy_lighting)
5461 shader_addline(buffer, "att = 1.0 / att;\n");
5462 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5463 if (!settings->normal)
5465 if (!legacy_lighting)
5466 shader_addline(buffer, "}\n");
5467 break;
5469 shader_addline(buffer, "dir = normalize(dir);\n");
5470 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5471 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
5472 if (settings->localviewer)
5473 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5474 else
5475 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5476 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5477 " * ffp_light[%u].specular) * att;\n", i);
5478 if (!legacy_lighting)
5479 shader_addline(buffer, "}\n");
5480 break;
5482 case WINED3D_LIGHT_SPOT:
5483 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5484 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5485 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5486 shader_addline(buffer, "dst.x = 1.0;\n");
5487 if (legacy_lighting)
5489 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
5490 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
5492 else
5494 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5496 shader_addline(buffer, "dir = normalize(dir);\n");
5497 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", i);
5498 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", i);
5499 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", i);
5500 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
5501 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
5502 i, i, i, i);
5503 if (legacy_lighting)
5504 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5505 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5506 i, i, i);
5507 else
5508 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5509 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5510 i, i, i);
5511 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5512 if (!settings->normal)
5514 if (!legacy_lighting)
5515 shader_addline(buffer, "}\n");
5516 break;
5518 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5519 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
5520 if (settings->localviewer)
5521 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5522 else
5523 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5524 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5525 " * ffp_light[%u].specular) * att;\n", i);
5526 if (!legacy_lighting)
5527 shader_addline(buffer, "}\n");
5528 break;
5530 case WINED3D_LIGHT_DIRECTIONAL:
5531 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
5532 if (!settings->normal)
5533 break;
5534 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", i);
5535 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5536 " * ffp_light[%u].diffuse.xyz;\n", i);
5537 /* TODO: In the non-local viewer case the halfvector is constant
5538 * and could be precomputed and stored in a uniform. */
5539 if (settings->localviewer)
5540 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5541 else
5542 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5543 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
5544 " * ffp_light[%u].specular;\n", i);
5545 break;
5547 case WINED3D_LIGHT_PARALLELPOINT:
5548 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
5549 if (!settings->normal)
5550 break;
5551 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", i);
5552 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5553 " * ffp_light[%u].diffuse.xyz;\n", i);
5554 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5555 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
5556 " * ffp_light[%u].specular;\n", i);
5557 break;
5559 default:
5560 if (light_type)
5561 FIXME("Unhandled light type %#x.\n", light_type);
5562 continue;
5566 shader_addline(buffer, "gl_FrontColor.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
5567 ambient, diffuse, emissive);
5568 shader_addline(buffer, "gl_FrontColor.w = %s.w;\n", diffuse);
5569 shader_addline(buffer, "gl_FrontSecondaryColor = %s * specular;\n", specular);
5572 /* Context activation is done by the caller. */
5573 static GLuint shader_glsl_generate_ffp_vertex_shader(struct wined3d_string_buffer *buffer,
5574 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info,
5575 BOOL legacy_lighting)
5577 static const struct attrib_info
5579 const char type[6];
5580 const char name[24];
5582 attrib_info[] =
5584 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
5585 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
5586 /* TODO: Indexed vertex blending */
5587 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
5588 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
5589 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
5590 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
5591 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
5593 GLuint shader_obj;
5594 unsigned int i;
5596 string_buffer_clear(buffer);
5598 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
5600 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
5602 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
5604 shader_addline(buffer, "attribute %s vs_in%u;\n", type, i);
5606 shader_addline(buffer, "\n");
5608 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
5609 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
5610 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
5611 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
5613 shader_addline(buffer, "uniform struct\n{\n");
5614 shader_addline(buffer, " vec4 emissive;\n");
5615 shader_addline(buffer, " vec4 ambient;\n");
5616 shader_addline(buffer, " vec4 diffuse;\n");
5617 shader_addline(buffer, " vec4 specular;\n");
5618 shader_addline(buffer, " float shininess;\n");
5619 shader_addline(buffer, "} ffp_material;\n");
5621 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
5622 shader_addline(buffer, "uniform struct\n{\n");
5623 shader_addline(buffer, " vec4 diffuse;\n");
5624 shader_addline(buffer, " vec4 specular;\n");
5625 shader_addline(buffer, " vec4 ambient;\n");
5626 shader_addline(buffer, " vec4 position;\n");
5627 shader_addline(buffer, " vec3 direction;\n");
5628 shader_addline(buffer, " float range;\n");
5629 shader_addline(buffer, " float falloff;\n");
5630 shader_addline(buffer, " float c_att;\n");
5631 shader_addline(buffer, " float l_att;\n");
5632 shader_addline(buffer, " float q_att;\n");
5633 shader_addline(buffer, " float cos_htheta;\n");
5634 shader_addline(buffer, " float cos_hphi;\n");
5635 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
5637 if (settings->point_size)
5639 shader_addline(buffer, "uniform struct\n{\n");
5640 shader_addline(buffer, " float size;\n");
5641 shader_addline(buffer, " float size_min;\n");
5642 shader_addline(buffer, " float size_max;\n");
5643 shader_addline(buffer, " float c_att;\n");
5644 shader_addline(buffer, " float l_att;\n");
5645 shader_addline(buffer, " float q_att;\n");
5646 shader_addline(buffer, "} ffp_point;\n");
5649 shader_addline(buffer, "\nvoid main()\n{\n");
5650 shader_addline(buffer, "float m;\n");
5651 shader_addline(buffer, "vec3 r;\n");
5653 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
5655 if (attrib_info[i].name[0])
5656 shader_addline(buffer, "%s %s = vs_in%u;\n",
5657 attrib_info[i].type, attrib_info[i].name, i);
5659 for (i = 0; i < MAX_TEXTURES; ++i)
5661 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
5662 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU)
5664 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
5668 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
5670 if (settings->transformed)
5672 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
5673 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5674 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
5676 else
5678 for (i = 0; i < settings->vertexblends; ++i)
5679 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
5681 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
5682 for (i = 0; i < settings->vertexblends + 1; ++i)
5683 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
5685 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5686 if (settings->clipping)
5687 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
5688 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
5691 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
5692 if (settings->normal)
5694 if (!settings->vertexblends)
5696 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
5698 else
5700 for (i = 0; i < settings->vertexblends + 1; ++i)
5701 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
5704 if (settings->normalize)
5705 shader_addline(buffer, "normal = normalize(normal);\n");
5708 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
5710 for (i = 0; i < MAX_TEXTURES; ++i)
5712 switch (settings->texgen[i] & 0xffff0000)
5714 case WINED3DTSS_TCI_PASSTHRU:
5715 if (settings->texcoords & (1u << i))
5716 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
5717 i, i, i);
5718 break;
5720 case WINED3DTSS_TCI_CAMERASPACENORMAL:
5721 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
5722 break;
5724 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
5725 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
5726 break;
5728 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
5729 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u]"
5730 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
5731 break;
5733 case WINED3DTSS_TCI_SPHEREMAP:
5734 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
5735 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
5736 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u]"
5737 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
5738 break;
5740 default:
5741 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
5742 break;
5746 switch (settings->fog_mode)
5748 case WINED3D_FFP_VS_FOG_OFF:
5749 break;
5751 case WINED3D_FFP_VS_FOG_FOGCOORD:
5752 shader_addline(buffer, "gl_FogFragCoord = ffp_attrib_specular.w * 255.0;\n");
5753 break;
5755 case WINED3D_FFP_VS_FOG_RANGE:
5756 shader_addline(buffer, "gl_FogFragCoord = length(ec_pos.xyz);\n");
5757 break;
5759 case WINED3D_FFP_VS_FOG_DEPTH:
5760 if (settings->ortho_fog)
5761 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
5762 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z * 0.5 + 0.5;\n");
5763 else if (settings->transformed)
5764 shader_addline(buffer, "gl_FogFragCoord = ec_pos.z;\n");
5765 else
5766 shader_addline(buffer, "gl_FogFragCoord = abs(ec_pos.z);\n");
5767 break;
5769 default:
5770 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
5771 break;
5774 if (settings->point_size)
5776 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
5777 " + ffp_point.l_att * length(ec_pos.xyz)"
5778 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
5779 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
5780 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
5783 shader_addline(buffer, "}\n");
5785 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5786 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5788 return shader_obj;
5791 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
5792 DWORD argnum, unsigned int stage, DWORD arg)
5794 const char *ret;
5796 if (arg == ARG_UNUSED)
5797 return "<unused arg>";
5799 switch (arg & WINED3DTA_SELECTMASK)
5801 case WINED3DTA_DIFFUSE:
5802 ret = "gl_Color";
5803 break;
5805 case WINED3DTA_CURRENT:
5806 if (!stage)
5807 ret = "gl_Color";
5808 else
5809 ret = "ret";
5810 break;
5812 case WINED3DTA_TEXTURE:
5813 switch (stage)
5815 case 0: ret = "tex0"; break;
5816 case 1: ret = "tex1"; break;
5817 case 2: ret = "tex2"; break;
5818 case 3: ret = "tex3"; break;
5819 case 4: ret = "tex4"; break;
5820 case 5: ret = "tex5"; break;
5821 case 6: ret = "tex6"; break;
5822 case 7: ret = "tex7"; break;
5823 default:
5824 ret = "<invalid texture>";
5825 break;
5827 break;
5829 case WINED3DTA_TFACTOR:
5830 ret = "tex_factor";
5831 break;
5833 case WINED3DTA_SPECULAR:
5834 ret = "gl_SecondaryColor";
5835 break;
5837 case WINED3DTA_TEMP:
5838 ret = "temp_reg";
5839 break;
5841 case WINED3DTA_CONSTANT:
5842 switch (stage)
5844 case 0: ret = "tss_const0"; break;
5845 case 1: ret = "tss_const1"; break;
5846 case 2: ret = "tss_const2"; break;
5847 case 3: ret = "tss_const3"; break;
5848 case 4: ret = "tss_const4"; break;
5849 case 5: ret = "tss_const5"; break;
5850 case 6: ret = "tss_const6"; break;
5851 case 7: ret = "tss_const7"; break;
5852 default:
5853 ret = "<invalid constant>";
5854 break;
5856 break;
5858 default:
5859 return "<unhandled arg>";
5862 if (arg & WINED3DTA_COMPLEMENT)
5864 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
5865 if (argnum == 0)
5866 ret = "arg0";
5867 else if (argnum == 1)
5868 ret = "arg1";
5869 else if (argnum == 2)
5870 ret = "arg2";
5873 if (arg & WINED3DTA_ALPHAREPLICATE)
5875 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
5876 if (argnum == 0)
5877 ret = "arg0";
5878 else if (argnum == 1)
5879 ret = "arg1";
5880 else if (argnum == 2)
5881 ret = "arg2";
5884 return ret;
5887 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
5888 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
5890 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5892 if (color && alpha)
5893 dstmask = "";
5894 else if (color)
5895 dstmask = ".xyz";
5896 else
5897 dstmask = ".w";
5899 if (dst == tempreg)
5900 dstreg = "temp_reg";
5901 else
5902 dstreg = "ret";
5904 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
5905 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
5906 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
5908 switch (op)
5910 case WINED3D_TOP_DISABLE:
5911 if (!stage)
5912 shader_addline(buffer, "%s%s = gl_Color%s;\n", dstreg, dstmask, dstmask);
5913 break;
5915 case WINED3D_TOP_SELECT_ARG1:
5916 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
5917 break;
5919 case WINED3D_TOP_SELECT_ARG2:
5920 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
5921 break;
5923 case WINED3D_TOP_MODULATE:
5924 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5925 break;
5927 case WINED3D_TOP_MODULATE_4X:
5928 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
5929 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5930 break;
5932 case WINED3D_TOP_MODULATE_2X:
5933 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
5934 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5935 break;
5937 case WINED3D_TOP_ADD:
5938 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
5939 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5940 break;
5942 case WINED3D_TOP_ADD_SIGNED:
5943 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
5944 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5945 break;
5947 case WINED3D_TOP_ADD_SIGNED_2X:
5948 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
5949 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5950 break;
5952 case WINED3D_TOP_SUBTRACT:
5953 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
5954 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5955 break;
5957 case WINED3D_TOP_ADD_SMOOTH:
5958 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
5959 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
5960 break;
5962 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
5963 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5964 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5965 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5966 break;
5968 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5969 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5970 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5971 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5972 break;
5974 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5975 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
5976 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5977 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5978 break;
5980 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5981 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5982 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5983 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
5984 break;
5986 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
5987 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
5988 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5989 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5990 break;
5992 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
5993 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
5994 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5995 break;
5997 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
5998 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
5999 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6000 break;
6002 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
6003 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6004 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6005 break;
6006 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
6007 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
6008 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6009 break;
6011 case WINED3D_TOP_BUMPENVMAP:
6012 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6013 /* These are handled in the first pass, nothing to do. */
6014 break;
6016 case WINED3D_TOP_DOTPRODUCT3:
6017 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
6018 dstreg, dstmask, arg1, arg2, dstmask);
6019 break;
6021 case WINED3D_TOP_MULTIPLY_ADD:
6022 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
6023 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
6024 break;
6026 case WINED3D_TOP_LERP:
6027 /* MSDN isn't quite right here. */
6028 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
6029 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
6030 break;
6032 default:
6033 FIXME("Unhandled operation %#x.\n", op);
6034 break;
6038 /* Context activation is done by the caller. */
6039 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
6040 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
6042 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6043 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
6044 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
6045 const char *final_combiner_src = "ret";
6046 UINT lowest_disabled_stage;
6047 GLuint shader_id;
6048 DWORD arg0, arg1, arg2;
6049 unsigned int stage;
6050 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
6052 string_buffer_clear(buffer);
6054 /* Find out which textures are read */
6055 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6057 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6058 break;
6060 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
6061 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
6062 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
6064 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
6065 || (stage == 0 && settings->color_key_enabled))
6066 tex_map |= 1u << stage;
6067 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6068 tfactor_used = TRUE;
6069 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6070 tempreg_used = TRUE;
6071 if (settings->op[stage].dst == tempreg)
6072 tempreg_used = TRUE;
6073 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6074 tss_const_map |= 1u << stage;
6076 switch (settings->op[stage].cop)
6078 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6079 lum_map |= 1u << stage;
6080 /* fall through */
6081 case WINED3D_TOP_BUMPENVMAP:
6082 bump_map |= 1u << stage;
6083 /* fall through */
6084 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6085 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6086 tex_map |= 1u << stage;
6087 break;
6089 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6090 tfactor_used = TRUE;
6091 break;
6093 default:
6094 break;
6097 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6098 continue;
6100 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
6101 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
6102 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
6104 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
6105 tex_map |= 1u << stage;
6106 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6107 tfactor_used = TRUE;
6108 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6109 tempreg_used = TRUE;
6110 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6111 tss_const_map |= 1u << stage;
6113 lowest_disabled_stage = stage;
6115 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6117 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6118 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6120 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
6121 shader_addline(buffer, "vec4 ret;\n");
6122 if (tempreg_used || settings->sRGB_write)
6123 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
6124 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
6126 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6128 if (tss_const_map & (1u << stage))
6129 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
6131 if (!(tex_map & (1u << stage)))
6132 continue;
6134 switch (settings->op[stage].tex_type)
6136 case WINED3D_GL_RES_TYPE_TEX_1D:
6137 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
6138 break;
6139 case WINED3D_GL_RES_TYPE_TEX_2D:
6140 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
6141 break;
6142 case WINED3D_GL_RES_TYPE_TEX_3D:
6143 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
6144 break;
6145 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6146 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
6147 break;
6148 case WINED3D_GL_RES_TYPE_TEX_RECT:
6149 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
6150 break;
6151 default:
6152 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
6153 break;
6156 shader_addline(buffer, "vec4 tex%u;\n", stage);
6158 if (!(bump_map & (1u << stage)))
6159 continue;
6160 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
6162 if (!(lum_map & (1u << stage)))
6163 continue;
6164 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
6165 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
6167 if (tfactor_used)
6168 shader_addline(buffer, "uniform vec4 tex_factor;\n");
6169 if (settings->color_key_enabled)
6170 shader_addline(buffer, "uniform vec4 color_key[2];\n");
6171 shader_addline(buffer, "uniform vec4 specular_enable;\n");
6173 if (settings->sRGB_write)
6175 shader_addline(buffer, "const vec4 srgb_const0 = ");
6176 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
6177 shader_addline(buffer, ";\n");
6178 shader_addline(buffer, "const vec4 srgb_const1 = ");
6179 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
6180 shader_addline(buffer, ";\n");
6183 shader_addline(buffer, "uniform struct\n{\n");
6184 shader_addline(buffer, " vec4 color;\n");
6185 shader_addline(buffer, " float density;\n");
6186 shader_addline(buffer, " float end;\n");
6187 shader_addline(buffer, " float scale;\n");
6188 shader_addline(buffer, "} ffp_fog;\n");
6190 shader_addline(buffer, "void main()\n{\n");
6192 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
6193 shader_addline(buffer, "if (any(lessThan(gl_TexCoord[7], vec4(0.0)))) discard;\n");
6195 /* Generate texture sampling instructions) */
6196 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
6198 const char *texture_function, *coord_mask;
6199 BOOL proj;
6201 if (!(tex_map & (1u << stage)))
6202 continue;
6204 if (settings->op[stage].projected == proj_none)
6206 proj = FALSE;
6208 else if (settings->op[stage].projected == proj_count4
6209 || settings->op[stage].projected == proj_count3)
6211 proj = TRUE;
6213 else
6215 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
6216 proj = TRUE;
6219 switch (settings->op[stage].tex_type)
6221 case WINED3D_GL_RES_TYPE_TEX_1D:
6222 if (proj)
6224 texture_function = "texture1DProj";
6225 coord_mask = "xw";
6227 else
6229 texture_function = "texture1D";
6230 coord_mask = "x";
6232 break;
6233 case WINED3D_GL_RES_TYPE_TEX_2D:
6234 if (proj)
6236 texture_function = "texture2DProj";
6237 coord_mask = "xyw";
6239 else
6241 texture_function = "texture2D";
6242 coord_mask = "xy";
6244 break;
6245 case WINED3D_GL_RES_TYPE_TEX_3D:
6246 if (proj)
6248 texture_function = "texture3DProj";
6249 coord_mask = "xyzw";
6251 else
6253 texture_function = "texture3D";
6254 coord_mask = "xyz";
6256 break;
6257 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6258 texture_function = "textureCube";
6259 coord_mask = "xyz";
6260 break;
6261 case WINED3D_GL_RES_TYPE_TEX_RECT:
6262 if (proj)
6264 texture_function = "texture2DRectProj";
6265 coord_mask = "xyw";
6267 else
6269 texture_function = "texture2DRect";
6270 coord_mask = "xy";
6272 break;
6273 default:
6274 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
6275 texture_function = "";
6276 coord_mask = "xyzw";
6277 break;
6280 if (stage > 0
6281 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
6282 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
6284 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
6286 /* With projective textures, texbem only divides the static
6287 * texture coord, not the displacement, so multiply the
6288 * displacement with the dividing parameter before passing it to
6289 * TXP. */
6290 if (settings->op[stage].projected != proj_none)
6292 if (settings->op[stage].projected == proj_count4)
6294 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].w) + gl_TexCoord[%u].xy;\n",
6295 stage, stage);
6296 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].ww;\n", stage);
6298 else
6300 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].z) + gl_TexCoord[%u].xy;\n",
6301 stage, stage);
6302 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].zz;\n", stage);
6305 else
6307 shader_addline(buffer, "ret = gl_TexCoord[%u] + ret.xyxy;\n", stage);
6310 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
6311 stage, texture_function, stage, coord_mask);
6313 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6314 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
6315 stage, stage - 1, stage - 1, stage - 1);
6317 else if (settings->op[stage].projected == proj_count3)
6319 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].xyz);\n",
6320 stage, texture_function, stage, stage);
6322 else
6324 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].%s);\n",
6325 stage, texture_function, stage, stage, coord_mask);
6328 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
6329 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
6330 settings->op[stage].color_fixup);
6333 if (settings->color_key_enabled)
6335 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
6336 shader_addline(buffer, " discard;\n");
6339 /* Generate the main shader */
6340 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6342 BOOL op_equal;
6344 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6346 if (!stage)
6347 final_combiner_src = "gl_Color";
6348 break;
6351 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6352 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6353 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
6354 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6355 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6356 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
6357 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6358 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6359 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
6360 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6361 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6362 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
6363 else
6364 op_equal = settings->op[stage].aop == settings->op[stage].cop
6365 && settings->op[stage].carg0 == settings->op[stage].aarg0
6366 && settings->op[stage].carg1 == settings->op[stage].aarg1
6367 && settings->op[stage].carg2 == settings->op[stage].aarg2;
6369 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6371 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6372 settings->op[stage].cop, settings->op[stage].carg0,
6373 settings->op[stage].carg1, settings->op[stage].carg2);
6374 if (!stage)
6375 shader_addline(buffer, "ret.w = gl_Color.w;\n");
6377 else if (op_equal)
6379 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
6380 settings->op[stage].cop, settings->op[stage].carg0,
6381 settings->op[stage].carg1, settings->op[stage].carg2);
6383 else
6385 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6386 settings->op[stage].cop, settings->op[stage].carg0,
6387 settings->op[stage].carg1, settings->op[stage].carg2);
6388 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
6389 settings->op[stage].aop, settings->op[stage].aarg0,
6390 settings->op[stage].aarg1, settings->op[stage].aarg2);
6394 shader_addline(buffer, "gl_FragData[0] = gl_SecondaryColor * specular_enable + %s;\n", final_combiner_src);
6396 if (settings->sRGB_write)
6397 shader_glsl_generate_srgb_write_correction(buffer);
6399 shader_glsl_generate_fog_code(buffer, settings->fog);
6401 shader_addline(buffer, "}\n");
6403 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6404 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6406 string_buffer_release(&priv->string_buffers, tex_reg_name);
6407 return shader_id;
6410 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
6411 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
6413 struct glsl_ffp_vertex_shader *shader;
6414 const struct wine_rb_entry *entry;
6416 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
6417 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
6419 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
6420 return NULL;
6422 shader->desc.settings = *settings;
6423 shader->id = shader_glsl_generate_ffp_vertex_shader(&priv->shader_buffer, settings, gl_info, priv->legacy_lighting);
6424 list_init(&shader->linked_programs);
6425 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
6426 ERR("Failed to insert ffp vertex shader.\n");
6428 return shader;
6431 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
6432 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
6434 struct glsl_ffp_fragment_shader *glsl_desc;
6435 const struct ffp_frag_desc *desc;
6437 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
6438 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
6440 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
6441 return NULL;
6443 glsl_desc->entry.settings = *args;
6444 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
6445 list_init(&glsl_desc->linked_programs);
6446 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
6448 return glsl_desc;
6452 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
6453 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
6455 unsigned int i;
6456 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6458 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
6459 sizeof(GLuint) * gl_info->limits.glsl_vs_float_constants);
6460 for (i = 0; i < vs_c_count; ++i)
6462 string_buffer_sprintf(name, "vs_c[%u]", i);
6463 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6465 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
6466 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLuint));
6468 for (i = 0; i < MAX_CONST_I; ++i)
6470 string_buffer_sprintf(name, "vs_i[%u]", i);
6471 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6474 for (i = 0; i < MAX_CONST_B; ++i)
6476 string_buffer_sprintf(name, "vs_b[%u]", i);
6477 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6480 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "posFixup"));
6482 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
6484 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
6485 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6487 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
6488 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
6489 for (i = 0; i < MAX_TEXTURES; ++i)
6491 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
6492 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6494 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
6495 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
6496 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
6497 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
6498 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
6499 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
6500 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
6502 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
6503 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6504 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
6505 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6506 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
6507 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6508 string_buffer_sprintf(name, "ffp_light[%u].position", i);
6509 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6510 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
6511 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6512 string_buffer_sprintf(name, "ffp_light[%u].range", i);
6513 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6514 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
6515 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6516 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
6517 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6518 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
6519 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6520 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
6521 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6522 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
6523 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6524 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
6525 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6527 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
6528 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
6529 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
6530 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
6531 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
6532 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
6534 string_buffer_release(&priv->string_buffers, name);
6537 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
6538 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
6540 unsigned int i;
6541 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6543 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
6544 sizeof(GLuint) * gl_info->limits.glsl_ps_float_constants);
6545 for (i = 0; i < ps_c_count; ++i)
6547 string_buffer_sprintf(name, "ps_c[%u]", i);
6548 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6550 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
6551 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLuint));
6553 for (i = 0; i < MAX_CONST_I; ++i)
6555 string_buffer_sprintf(name, "ps_i[%u]", i);
6556 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6559 for (i = 0; i < MAX_CONST_B; ++i)
6561 string_buffer_sprintf(name, "ps_b[%u]", i);
6562 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6565 for (i = 0; i < MAX_TEXTURES; ++i)
6567 string_buffer_sprintf(name, "bumpenv_mat%u", i);
6568 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6569 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
6570 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6571 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
6572 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6573 string_buffer_sprintf(name, "tss_const%u", i);
6574 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6577 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
6578 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
6580 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
6581 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
6582 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
6583 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
6585 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
6586 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
6587 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
6589 string_buffer_release(&priv->string_buffers, name);
6592 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
6593 struct shader_glsl_priv *priv, GLuint program_id,
6594 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
6596 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6597 GLuint block_idx;
6598 unsigned int i;
6599 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6601 for (i = 0; i < count; ++i)
6603 if (!reg_maps->cb_sizes[i])
6604 continue;
6606 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
6607 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
6608 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
6610 checkGLcall("glUniformBlockBinding");
6611 string_buffer_release(&priv->string_buffers, name);
6614 /* Context activation is done by the caller. */
6615 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
6616 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
6618 const struct wined3d_gl_info *gl_info = context->gl_info;
6619 const struct ps_np2fixup_info *np2fixup_info = NULL;
6620 struct glsl_shader_prog_link *entry = NULL;
6621 struct wined3d_shader *vshader = NULL;
6622 struct wined3d_shader *gshader = NULL;
6623 struct wined3d_shader *pshader = NULL;
6624 GLuint program_id = 0;
6625 GLuint reorder_shader_id = 0;
6626 unsigned int i;
6627 GLuint vs_id = 0;
6628 GLuint gs_id = 0;
6629 GLuint ps_id = 0;
6630 struct list *ps_list, *vs_list;
6631 WORD attribs_map;
6632 struct wined3d_string_buffer *tmp_name;
6634 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
6636 vs_id = ctx_data->glsl_program->vs.id;
6637 vs_list = &ctx_data->glsl_program->vs.shader_entry;
6639 if (use_vs(state))
6641 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
6642 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
6644 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
6645 && ctx_data->glsl_program->gs.id)
6646 gs_id = ctx_data->glsl_program->gs.id;
6647 else if (gshader)
6648 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, &priv->string_buffers, gshader);
6651 else if (use_vs(state))
6653 struct vs_compile_args vs_compile_args;
6654 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
6656 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args);
6657 vs_id = find_glsl_vshader(context, &priv->shader_buffer, &priv->string_buffers, vshader, &vs_compile_args);
6658 vs_list = &vshader->linked_programs;
6660 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
6661 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, &priv->string_buffers, gshader);
6663 else if (priv->vertex_pipe == &glsl_vertex_pipe)
6665 struct glsl_ffp_vertex_shader *ffp_shader;
6666 struct wined3d_ffp_vs_settings settings;
6668 wined3d_ffp_get_vs_settings(state, &context->stream_info, &settings);
6669 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
6670 vs_id = ffp_shader->id;
6671 vs_list = &ffp_shader->linked_programs;
6674 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
6676 ps_id = ctx_data->glsl_program->ps.id;
6677 ps_list = &ctx_data->glsl_program->ps.shader_entry;
6679 if (use_ps(state))
6680 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
6682 else if (use_ps(state))
6684 struct ps_compile_args ps_compile_args;
6685 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
6686 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, gl_info);
6687 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
6688 pshader, &ps_compile_args, &np2fixup_info);
6689 ps_list = &pshader->linked_programs;
6691 else if (priv->fragment_pipe == &glsl_fragment_pipe)
6693 struct glsl_ffp_fragment_shader *ffp_shader;
6694 struct ffp_frag_settings settings;
6696 gen_ffp_frag_op(context, state, &settings, FALSE);
6697 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
6698 ps_id = ffp_shader->id;
6699 ps_list = &ffp_shader->linked_programs;
6702 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
6704 ctx_data->glsl_program = entry;
6705 return;
6708 /* If we get to this point, then no matching program exists, so we create one */
6709 program_id = GL_EXTCALL(glCreateProgram());
6710 TRACE("Created new GLSL shader program %u.\n", program_id);
6712 /* Create the entry */
6713 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
6714 entry->id = program_id;
6715 entry->vs.id = vs_id;
6716 entry->gs.id = gs_id;
6717 entry->ps.id = ps_id;
6718 entry->constant_version = 0;
6719 entry->ps.np2_fixup_info = np2fixup_info;
6720 /* Add the hash table entry */
6721 add_glsl_program_entry(priv, entry);
6723 /* Set the current program */
6724 ctx_data->glsl_program = entry;
6726 /* Attach GLSL vshader */
6727 if (vs_id)
6729 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
6730 GL_EXTCALL(glAttachShader(program_id, vs_id));
6731 checkGLcall("glAttachShader");
6733 list_add_head(vs_list, &entry->vs.shader_entry);
6736 if (vshader)
6738 attribs_map = vshader->reg_maps.input_registers;
6739 reorder_shader_id = generate_param_reorder_function(priv, vshader, pshader,
6740 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size, gl_info);
6741 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
6742 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
6743 checkGLcall("glAttachShader");
6744 /* Flag the reorder function for deletion, then it will be freed automatically when the program
6745 * is destroyed
6747 GL_EXTCALL(glDeleteShader(reorder_shader_id));
6749 else
6751 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
6754 /* Bind vertex attributes to a corresponding index number to match
6755 * the same index numbers as ARB_vertex_programs (makes loading
6756 * vertex attributes simpler). With this method, we can use the
6757 * exact same code to load the attributes later for both ARB and
6758 * GLSL shaders.
6760 * We have to do this here because we need to know the Program ID
6761 * in order to make the bindings work, and it has to be done prior
6762 * to linking the GLSL program. */
6763 tmp_name = string_buffer_get(&priv->string_buffers);
6764 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
6766 if (!(attribs_map & 1))
6767 continue;
6769 string_buffer_sprintf(tmp_name, "vs_in%u", i);
6770 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
6772 checkGLcall("glBindAttribLocation");
6773 string_buffer_release(&priv->string_buffers, tmp_name);
6775 if (gshader)
6777 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
6778 GL_EXTCALL(glAttachShader(program_id, gs_id));
6779 checkGLcall("glAttachShader");
6781 TRACE("input type %s, output type %s, vertices out %u.\n",
6782 debug_d3dprimitivetype(gshader->u.gs.input_type),
6783 debug_d3dprimitivetype(gshader->u.gs.output_type),
6784 gshader->u.gs.vertices_out);
6785 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
6786 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
6787 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
6788 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
6789 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
6790 gshader->u.gs.vertices_out));
6791 checkGLcall("glProgramParameteriARB");
6793 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
6796 /* Attach GLSL pshader */
6797 if (ps_id)
6799 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
6800 GL_EXTCALL(glAttachShader(program_id, ps_id));
6801 checkGLcall("glAttachShader");
6803 list_add_head(ps_list, &entry->ps.shader_entry);
6806 /* Link the program */
6807 TRACE("Linking GLSL shader program %u.\n", program_id);
6808 GL_EXTCALL(glLinkProgram(program_id));
6809 shader_glsl_validate_link(gl_info, program_id);
6811 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
6812 vshader ? min(vshader->limits->constant_float, gl_info->limits.glsl_vs_float_constants) : 0);
6813 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
6814 pshader ? min(pshader->limits->constant_float, gl_info->limits.glsl_ps_float_constants) : 0);
6815 checkGLcall("Find glsl program uniform locations");
6817 if (pshader && pshader->reg_maps.shader_version.major >= 3
6818 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
6820 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
6821 entry->vs.vertex_color_clamp = GL_FALSE;
6823 else
6825 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
6828 /* Set the shader to allow uniform loading on it */
6829 GL_EXTCALL(glUseProgram(program_id));
6830 checkGLcall("glUseProgram");
6832 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
6833 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
6834 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
6835 * vertex shader with fixed function pixel processing is used we make sure that the card
6836 * supports enough samplers to allow the max number of vertex samplers with all possible
6837 * fixed function fragment processing setups. So once the program is linked these samplers
6838 * won't change. */
6839 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
6841 entry->constant_update_mask = 0;
6842 if (vshader)
6844 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
6845 if (vshader->reg_maps.integer_constants)
6846 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
6847 if (vshader->reg_maps.boolean_constants)
6848 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
6849 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
6851 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps,
6852 0, gl_info->limits.vertex_uniform_blocks);
6854 else
6856 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
6857 | WINED3D_SHADER_CONST_FFP_PROJ;
6859 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
6861 if (entry->vs.modelview_matrix_location[i] != -1)
6863 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
6864 break;
6868 for (i = 0; i < MAX_TEXTURES; ++i)
6870 if (entry->vs.texture_matrix_location[i] != -1)
6872 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
6873 break;
6876 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
6877 || entry->vs.material_specular_location != -1
6878 || entry->vs.material_emissive_location != -1
6879 || entry->vs.material_shininess_location != -1)
6880 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
6881 if (entry->vs.light_ambient_location != -1)
6882 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
6884 if (entry->vs.pointsize_min_location != -1)
6885 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
6887 if (gshader)
6888 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps,
6889 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
6891 if (ps_id)
6893 if (pshader)
6895 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
6896 if (pshader->reg_maps.integer_constants)
6897 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
6898 if (pshader->reg_maps.boolean_constants)
6899 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
6900 if (entry->ps.ycorrection_location != -1)
6901 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
6903 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps,
6904 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
6905 gl_info->limits.fragment_uniform_blocks);
6907 else
6909 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
6912 for (i = 0; i < MAX_TEXTURES; ++i)
6914 if (entry->ps.bumpenv_mat_location[i] != -1)
6916 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
6917 break;
6921 if (entry->ps.fog_color_location != -1)
6922 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
6923 if (entry->ps.np2_fixup_location != -1)
6924 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
6925 if (entry->ps.color_key_location != -1)
6926 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
6930 /* Context activation is done by the caller. */
6931 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum wined3d_gl_resource_type tex_type,
6932 BOOL masked)
6934 GLuint program_id;
6935 GLuint vshader_id, pshader_id;
6936 const char *blt_pshader;
6938 static const char blt_vshader[] =
6939 "#version 120\n"
6940 "void main(void)\n"
6941 "{\n"
6942 " gl_Position = gl_Vertex;\n"
6943 " gl_FrontColor = vec4(1.0);\n"
6944 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
6945 "}\n";
6947 static const char * const blt_pshaders_full[WINED3D_GL_RES_TYPE_COUNT] =
6949 /* WINED3D_GL_RES_TYPE_TEX_1D */
6950 NULL,
6951 /* WINED3D_GL_RES_TYPE_TEX_2D */
6952 "#version 120\n"
6953 "uniform sampler2D sampler;\n"
6954 "void main(void)\n"
6955 "{\n"
6956 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6957 "}\n",
6958 /* WINED3D_GL_RES_TYPE_TEX_3D */
6959 NULL,
6960 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
6961 "#version 120\n"
6962 "uniform samplerCube sampler;\n"
6963 "void main(void)\n"
6964 "{\n"
6965 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6966 "}\n",
6967 /* WINED3D_GL_RES_TYPE_TEX_RECT */
6968 "#version 120\n"
6969 "#extension GL_ARB_texture_rectangle : enable\n"
6970 "uniform sampler2DRect sampler;\n"
6971 "void main(void)\n"
6972 "{\n"
6973 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6974 "}\n",
6975 /* WINED3D_GL_RES_TYPE_BUFFER */
6976 NULL,
6977 /* WINED3D_GL_RES_TYPE_RB */
6978 NULL,
6981 static const char * const blt_pshaders_masked[WINED3D_GL_RES_TYPE_COUNT] =
6983 /* WINED3D_GL_RES_TYPE_TEX_1D */
6984 NULL,
6985 /* WINED3D_GL_RES_TYPE_TEX_2D */
6986 "#version 120\n"
6987 "uniform sampler2D sampler;\n"
6988 "uniform vec4 mask;\n"
6989 "void main(void)\n"
6990 "{\n"
6991 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6992 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6993 "}\n",
6994 /* WINED3D_GL_RES_TYPE_TEX_3D */
6995 NULL,
6996 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
6997 "#version 120\n"
6998 "uniform samplerCube sampler;\n"
6999 "uniform vec4 mask;\n"
7000 "void main(void)\n"
7001 "{\n"
7002 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7003 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7004 "}\n",
7005 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7006 "#version 120\n"
7007 "#extension GL_ARB_texture_rectangle : enable\n"
7008 "uniform sampler2DRect sampler;\n"
7009 "uniform vec4 mask;\n"
7010 "void main(void)\n"
7011 "{\n"
7012 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7013 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7014 "}\n",
7015 /* WINED3D_GL_RES_TYPE_BUFFER */
7016 NULL,
7017 /* WINED3D_GL_RES_TYPE_RB */
7018 NULL,
7021 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
7022 if (!blt_pshader)
7024 FIXME("tex_type %#x not supported\n", tex_type);
7025 return 0;
7028 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7029 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
7031 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7032 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
7034 program_id = GL_EXTCALL(glCreateProgram());
7035 GL_EXTCALL(glAttachShader(program_id, vshader_id));
7036 GL_EXTCALL(glAttachShader(program_id, pshader_id));
7037 GL_EXTCALL(glLinkProgram(program_id));
7039 shader_glsl_validate_link(gl_info, program_id);
7041 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
7042 * is destroyed
7044 GL_EXTCALL(glDeleteShader(vshader_id));
7045 GL_EXTCALL(glDeleteShader(pshader_id));
7046 return program_id;
7049 /* Context activation is done by the caller. */
7050 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
7051 const struct wined3d_state *state)
7053 struct glsl_context_data *ctx_data = context->shader_backend_data;
7054 const struct wined3d_gl_info *gl_info = context->gl_info;
7055 struct shader_glsl_priv *priv = shader_priv;
7056 GLuint program_id = 0, prev_id = 0;
7057 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
7059 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
7060 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
7062 if (ctx_data->glsl_program)
7064 prev_id = ctx_data->glsl_program->id;
7065 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7067 else
7069 prev_id = 0;
7070 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7073 set_glsl_shader_program(context, state, priv, ctx_data);
7075 if (ctx_data->glsl_program)
7077 program_id = ctx_data->glsl_program->id;
7078 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7080 else
7082 program_id = 0;
7083 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7086 if (old_vertex_color_clamp != current_vertex_color_clamp)
7088 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7090 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
7091 checkGLcall("glClampColorARB");
7093 else
7095 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
7099 TRACE("Using GLSL program %u.\n", program_id);
7101 if (prev_id != program_id)
7103 GL_EXTCALL(glUseProgram(program_id));
7104 checkGLcall("glUseProgram");
7106 if (program_id)
7107 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
7111 /* "context" is not necessarily the currently active context. */
7112 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
7114 struct glsl_context_data *ctx_data = context->shader_backend_data;
7116 ctx_data->glsl_program = NULL;
7117 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
7118 | (1u << WINED3D_SHADER_TYPE_VERTEX)
7119 | (1u << WINED3D_SHADER_TYPE_GEOMETRY);
7122 /* Context activation is done by the caller. */
7123 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
7125 const struct wined3d_gl_info *gl_info = context->gl_info;
7126 struct shader_glsl_priv *priv = shader_priv;
7128 shader_glsl_invalidate_current_program(context);
7129 GL_EXTCALL(glUseProgram(0));
7130 checkGLcall("glUseProgram");
7132 priv->vertex_pipe->vp_enable(gl_info, FALSE);
7133 priv->fragment_pipe->enable_extension(gl_info, FALSE);
7135 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7137 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
7138 checkGLcall("glClampColorARB");
7142 /* Context activation is done by the caller. */
7143 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
7144 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size)
7146 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
7147 struct shader_glsl_priv *priv = shader_priv;
7148 GLuint *blt_program;
7149 GLint loc;
7151 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
7152 if (!*blt_program)
7154 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
7155 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
7156 GL_EXTCALL(glUseProgram(*blt_program));
7157 GL_EXTCALL(glUniform1i(loc, 0));
7159 else
7161 GL_EXTCALL(glUseProgram(*blt_program));
7164 if (masked)
7166 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
7167 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
7171 /* Context activation is done by the caller. */
7172 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
7174 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
7175 GLuint program_id;
7177 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
7178 if (program_id) TRACE("Using GLSL program %u\n", program_id);
7180 GL_EXTCALL(glUseProgram(program_id));
7181 checkGLcall("glUseProgram");
7184 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
7185 const struct glsl_shader_prog_link *program)
7187 const struct glsl_context_data *ctx_data;
7188 struct wined3d_context *context;
7189 unsigned int i;
7191 for (i = 0; i < device->context_count; ++i)
7193 context = device->contexts[i];
7194 ctx_data = context->shader_backend_data;
7196 if (ctx_data->glsl_program == program)
7197 shader_glsl_invalidate_current_program(context);
7201 static void shader_glsl_destroy(struct wined3d_shader *shader)
7203 struct glsl_shader_private *shader_data = shader->backend_data;
7204 struct wined3d_device *device = shader->device;
7205 struct shader_glsl_priv *priv = device->shader_priv;
7206 const struct wined3d_gl_info *gl_info;
7207 const struct list *linked_programs;
7208 struct wined3d_context *context;
7210 if (!shader_data || !shader_data->num_gl_shaders)
7212 HeapFree(GetProcessHeap(), 0, shader_data);
7213 shader->backend_data = NULL;
7214 return;
7217 context = context_acquire(device, NULL);
7218 gl_info = context->gl_info;
7220 TRACE("Deleting linked programs.\n");
7221 linked_programs = &shader->linked_programs;
7222 if (linked_programs->next)
7224 struct glsl_shader_prog_link *entry, *entry2;
7225 UINT i;
7227 switch (shader->reg_maps.shader_version.type)
7229 case WINED3D_SHADER_TYPE_PIXEL:
7231 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
7233 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7235 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
7236 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7237 checkGLcall("glDeleteShader");
7239 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
7241 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7242 struct glsl_shader_prog_link, ps.shader_entry)
7244 shader_glsl_invalidate_contexts_program(device, entry);
7245 delete_glsl_program_entry(priv, gl_info, entry);
7248 break;
7251 case WINED3D_SHADER_TYPE_VERTEX:
7253 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
7255 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7257 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
7258 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7259 checkGLcall("glDeleteShader");
7261 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
7263 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7264 struct glsl_shader_prog_link, vs.shader_entry)
7266 shader_glsl_invalidate_contexts_program(device, entry);
7267 delete_glsl_program_entry(priv, gl_info, entry);
7270 break;
7273 case WINED3D_SHADER_TYPE_GEOMETRY:
7275 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
7277 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7279 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
7280 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7281 checkGLcall("glDeleteShader");
7283 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
7285 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7286 struct glsl_shader_prog_link, gs.shader_entry)
7288 shader_glsl_invalidate_contexts_program(device, entry);
7289 delete_glsl_program_entry(priv, gl_info, entry);
7292 break;
7295 default:
7296 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
7297 break;
7301 HeapFree(GetProcessHeap(), 0, shader->backend_data);
7302 shader->backend_data = NULL;
7304 context_release(context);
7307 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
7309 const struct glsl_program_key *k = key;
7310 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
7311 const struct glsl_shader_prog_link, program_lookup_entry);
7313 if (k->vs_id > prog->vs.id) return 1;
7314 else if (k->vs_id < prog->vs.id) return -1;
7316 if (k->gs_id > prog->gs.id) return 1;
7317 else if (k->gs_id < prog->gs.id) return -1;
7319 if (k->ps_id > prog->ps.id) return 1;
7320 else if (k->ps_id < prog->ps.id) return -1;
7322 return 0;
7325 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
7327 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
7328 + constant_count * sizeof(*heap->contained)
7329 + constant_count * sizeof(*heap->positions);
7330 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
7332 if (!mem)
7334 ERR("Failed to allocate memory\n");
7335 return FALSE;
7338 heap->entries = mem;
7339 heap->entries[1].version = 0;
7340 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
7341 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
7342 heap->positions = (unsigned int *)(heap->contained + constant_count);
7343 heap->size = 1;
7345 return TRUE;
7348 static void constant_heap_free(struct constant_heap *heap)
7350 HeapFree(GetProcessHeap(), 0, heap->entries);
7353 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
7355 wined3d_rb_alloc,
7356 wined3d_rb_realloc,
7357 wined3d_rb_free,
7358 glsl_program_key_compare,
7361 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
7362 const struct fragment_pipeline *fragment_pipe)
7364 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7365 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
7366 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
7367 gl_info->limits.glsl_ps_float_constants)) + 1;
7368 struct fragment_caps fragment_caps;
7369 void *vertex_priv, *fragment_priv;
7371 string_buffer_list_init(&priv->string_buffers);
7373 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
7375 ERR("Failed to initialize vertex pipe.\n");
7376 HeapFree(GetProcessHeap(), 0, priv);
7377 return E_FAIL;
7380 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
7382 ERR("Failed to initialize fragment pipe.\n");
7383 vertex_pipe->vp_free(device);
7384 HeapFree(GetProcessHeap(), 0, priv);
7385 return E_FAIL;
7388 if (!string_buffer_init(&priv->shader_buffer))
7390 ERR("Failed to initialize shader buffer.\n");
7391 goto fail;
7394 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
7395 if (!priv->stack)
7397 ERR("Failed to allocate memory.\n");
7398 goto fail;
7401 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
7403 ERR("Failed to initialize vertex shader constant heap\n");
7404 goto fail;
7407 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
7409 ERR("Failed to initialize pixel shader constant heap\n");
7410 goto fail;
7413 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
7415 ERR("Failed to initialize rbtree.\n");
7416 goto fail;
7419 priv->next_constant_version = 1;
7420 priv->vertex_pipe = vertex_pipe;
7421 priv->fragment_pipe = fragment_pipe;
7422 fragment_pipe->get_caps(gl_info, &fragment_caps);
7423 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
7424 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
7426 device->vertex_priv = vertex_priv;
7427 device->fragment_priv = fragment_priv;
7428 device->shader_priv = priv;
7430 return WINED3D_OK;
7432 fail:
7433 constant_heap_free(&priv->pconst_heap);
7434 constant_heap_free(&priv->vconst_heap);
7435 HeapFree(GetProcessHeap(), 0, priv->stack);
7436 string_buffer_free(&priv->shader_buffer);
7437 fragment_pipe->free_private(device);
7438 vertex_pipe->vp_free(device);
7439 HeapFree(GetProcessHeap(), 0, priv);
7440 return E_OUTOFMEMORY;
7443 /* Context activation is done by the caller. */
7444 static void shader_glsl_free(struct wined3d_device *device)
7446 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7447 struct shader_glsl_priv *priv = device->shader_priv;
7448 int i;
7450 for (i = 0; i < WINED3D_GL_RES_TYPE_COUNT; ++i)
7452 if (priv->depth_blt_program_full[i])
7454 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
7456 if (priv->depth_blt_program_masked[i])
7458 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
7462 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
7463 constant_heap_free(&priv->pconst_heap);
7464 constant_heap_free(&priv->vconst_heap);
7465 HeapFree(GetProcessHeap(), 0, priv->stack);
7466 string_buffer_list_cleanup(&priv->string_buffers);
7467 string_buffer_free(&priv->shader_buffer);
7468 priv->fragment_pipe->free_private(device);
7469 priv->vertex_pipe->vp_free(device);
7471 HeapFree(GetProcessHeap(), 0, device->shader_priv);
7472 device->shader_priv = NULL;
7475 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
7477 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
7478 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
7481 static void shader_glsl_free_context_data(struct wined3d_context *context)
7483 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
7486 static void shader_glsl_init_context_state(struct wined3d_context *context)
7488 const struct wined3d_gl_info *gl_info = context->gl_info;
7490 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
7491 checkGLcall("GL_PROGRAM_POINT_SIZE");
7494 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
7496 UINT shader_model;
7498 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
7499 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
7500 && gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] && gl_info->supported[ARB_DRAW_INSTANCED]
7501 && gl_info->supported[ARB_TEXTURE_RG] && gl_info->supported[ARB_SAMPLER_OBJECTS])
7502 shader_model = 4;
7503 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
7504 * texldd and texldl instructions. */
7505 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
7506 shader_model = 3;
7507 else
7508 shader_model = 2;
7509 TRACE("Shader model %u.\n", shader_model);
7511 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
7512 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
7513 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
7515 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
7516 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
7518 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
7519 * Direct3D minimum requirement.
7521 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
7522 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
7524 * The problem is that the refrast clamps temporary results in the shader to
7525 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
7526 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
7527 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
7528 * offer a way to query this.
7530 if (shader_model >= 4)
7531 caps->ps_1x_max_value = FLT_MAX;
7532 else
7533 caps->ps_1x_max_value = 1024.0f;
7535 /* Ideally we'd only set caps like sRGB writes here if supported by both
7536 * the shader backend and the fragment pipe, but we can get called before
7537 * shader_glsl_alloc(). */
7538 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
7539 | WINED3D_SHADER_CAP_SRGB_WRITE;
7542 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
7544 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
7546 TRACE("Checking support for fixup:\n");
7547 dump_color_fixup_desc(fixup);
7550 /* We support everything except YUV conversions. */
7551 if (!is_complex_fixup(fixup))
7553 TRACE("[OK]\n");
7554 return TRUE;
7557 TRACE("[FAILED]\n");
7558 return FALSE;
7561 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
7563 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
7564 /* WINED3DSIH_ADD */ shader_glsl_binop,
7565 /* WINED3DSIH_AND */ shader_glsl_binop,
7566 /* WINED3DSIH_BEM */ shader_glsl_bem,
7567 /* WINED3DSIH_BREAK */ shader_glsl_break,
7568 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
7569 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
7570 /* WINED3DSIH_CALL */ shader_glsl_call,
7571 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
7572 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
7573 /* WINED3DSIH_CND */ shader_glsl_cnd,
7574 /* WINED3DSIH_CRS */ shader_glsl_cross,
7575 /* WINED3DSIH_CUT */ shader_glsl_cut,
7576 /* WINED3DSIH_DCL */ shader_glsl_nop,
7577 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
7578 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
7579 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
7580 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
7581 /* WINED3DSIH_DEF */ shader_glsl_nop,
7582 /* WINED3DSIH_DEFB */ shader_glsl_nop,
7583 /* WINED3DSIH_DEFI */ shader_glsl_nop,
7584 /* WINED3DSIH_DIV */ shader_glsl_binop,
7585 /* WINED3DSIH_DP2 */ shader_glsl_dot,
7586 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
7587 /* WINED3DSIH_DP3 */ shader_glsl_dot,
7588 /* WINED3DSIH_DP4 */ shader_glsl_dot,
7589 /* WINED3DSIH_DST */ shader_glsl_dst,
7590 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
7591 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
7592 /* WINED3DSIH_ELSE */ shader_glsl_else,
7593 /* WINED3DSIH_EMIT */ shader_glsl_emit,
7594 /* WINED3DSIH_ENDIF */ shader_glsl_end,
7595 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
7596 /* WINED3DSIH_ENDREP */ shader_glsl_end,
7597 /* WINED3DSIH_EQ */ shader_glsl_relop,
7598 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
7599 /* WINED3DSIH_EXPP */ shader_glsl_expp,
7600 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
7601 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
7602 /* WINED3DSIH_GE */ shader_glsl_relop,
7603 /* WINED3DSIH_IADD */ shader_glsl_binop,
7604 /* WINED3DSIH_IEQ */ NULL,
7605 /* WINED3DSIH_IF */ shader_glsl_if,
7606 /* WINED3DSIH_IFC */ shader_glsl_ifc,
7607 /* WINED3DSIH_IGE */ shader_glsl_relop,
7608 /* WINED3DSIH_IMUL */ shader_glsl_imul,
7609 /* WINED3DSIH_ISHL */ shader_glsl_binop,
7610 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
7611 /* WINED3DSIH_LABEL */ shader_glsl_label,
7612 /* WINED3DSIH_LD */ NULL,
7613 /* WINED3DSIH_LIT */ shader_glsl_lit,
7614 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
7615 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
7616 /* WINED3DSIH_LOOP */ shader_glsl_loop,
7617 /* WINED3DSIH_LRP */ shader_glsl_lrp,
7618 /* WINED3DSIH_LT */ shader_glsl_relop,
7619 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
7620 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
7621 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
7622 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
7623 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
7624 /* WINED3DSIH_MAD */ shader_glsl_mad,
7625 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
7626 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
7627 /* WINED3DSIH_MOV */ shader_glsl_mov,
7628 /* WINED3DSIH_MOVA */ shader_glsl_mov,
7629 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
7630 /* WINED3DSIH_MUL */ shader_glsl_binop,
7631 /* WINED3DSIH_NE */ shader_glsl_relop,
7632 /* WINED3DSIH_NOP */ shader_glsl_nop,
7633 /* WINED3DSIH_NRM */ shader_glsl_nrm,
7634 /* WINED3DSIH_OR */ shader_glsl_binop,
7635 /* WINED3DSIH_PHASE */ shader_glsl_nop,
7636 /* WINED3DSIH_POW */ shader_glsl_pow,
7637 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
7638 /* WINED3DSIH_REP */ shader_glsl_rep,
7639 /* WINED3DSIH_RET */ shader_glsl_ret,
7640 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
7641 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
7642 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
7643 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
7644 /* WINED3DSIH_SAMPLE_LOD */ NULL,
7645 /* WINED3DSIH_SETP */ NULL,
7646 /* WINED3DSIH_SGE */ shader_glsl_compare,
7647 /* WINED3DSIH_SGN */ shader_glsl_sgn,
7648 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
7649 /* WINED3DSIH_SLT */ shader_glsl_compare,
7650 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
7651 /* WINED3DSIH_SUB */ shader_glsl_binop,
7652 /* WINED3DSIH_TEX */ shader_glsl_tex,
7653 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
7654 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
7655 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
7656 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
7657 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
7658 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
7659 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
7660 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
7661 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
7662 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
7663 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
7664 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
7665 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
7666 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
7667 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
7668 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
7669 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
7670 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
7671 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
7672 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
7673 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
7674 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
7675 /* WINED3DSIH_UGE */ shader_glsl_relop,
7676 /* WINED3DSIH_USHR */ shader_glsl_binop,
7677 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
7678 /* WINED3DSIH_XOR */ shader_glsl_binop,
7681 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
7682 SHADER_HANDLER hw_fct;
7684 /* Select handler */
7685 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
7687 /* Unhandled opcode */
7688 if (!hw_fct)
7690 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
7691 return;
7693 hw_fct(ins);
7695 shader_glsl_add_instruction_modifiers(ins);
7698 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
7700 struct shader_glsl_priv *priv = shader_priv;
7702 return priv->ffp_proj_control;
7705 const struct wined3d_shader_backend_ops glsl_shader_backend =
7707 shader_glsl_handle_instruction,
7708 shader_glsl_select,
7709 shader_glsl_disable,
7710 shader_glsl_select_depth_blt,
7711 shader_glsl_deselect_depth_blt,
7712 shader_glsl_update_float_vertex_constants,
7713 shader_glsl_update_float_pixel_constants,
7714 shader_glsl_load_constants,
7715 shader_glsl_destroy,
7716 shader_glsl_alloc,
7717 shader_glsl_free,
7718 shader_glsl_allocate_context_data,
7719 shader_glsl_free_context_data,
7720 shader_glsl_init_context_state,
7721 shader_glsl_get_caps,
7722 shader_glsl_color_fixup_supported,
7723 shader_glsl_has_ffp_proj_control,
7726 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
7728 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
7730 caps->xyzrhw = TRUE;
7731 caps->ffp_generic_attributes = TRUE;
7732 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
7733 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
7734 caps->max_vertex_blend_matrix_index = 0;
7735 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
7736 | WINED3DVTXPCAPS_MATERIALSOURCE7
7737 | WINED3DVTXPCAPS_VERTEXFOG
7738 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
7739 | WINED3DVTXPCAPS_POSITIONALLIGHTS
7740 | WINED3DVTXPCAPS_LOCALVIEWER
7741 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
7742 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
7743 caps->max_user_clip_planes = gl_info->limits.clipplanes;
7744 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
7747 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
7749 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
7750 return GL_EXT_EMUL_ARB_MULTITEXTURE;
7751 return 0;
7754 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7756 struct shader_glsl_priv *priv;
7758 if (shader_backend == &glsl_shader_backend)
7760 priv = shader_priv;
7762 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
7764 ERR("Failed to initialize rbtree.\n");
7765 return NULL;
7768 return priv;
7771 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
7773 return NULL;
7776 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
7778 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7779 struct glsl_ffp_vertex_shader, desc.entry);
7780 struct glsl_shader_prog_link *program, *program2;
7781 struct glsl_ffp_destroy_ctx *ctx = context;
7783 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7784 struct glsl_shader_prog_link, vs.shader_entry)
7786 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7788 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
7789 HeapFree(GetProcessHeap(), 0, shader);
7792 /* Context activation is done by the caller. */
7793 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
7795 struct shader_glsl_priv *priv = device->vertex_priv;
7796 struct glsl_ffp_destroy_ctx ctx;
7798 ctx.priv = priv;
7799 ctx.gl_info = &device->adapter->gl_info;
7800 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
7803 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
7804 const struct wined3d_state *state, DWORD state_id)
7806 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
7809 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
7810 const struct wined3d_state *state, DWORD state_id)
7812 const struct wined3d_gl_info *gl_info = context->gl_info;
7813 BOOL transformed = context->stream_info.position_transformed;
7814 BOOL wasrhw = context->last_was_rhw;
7815 unsigned int i;
7817 context->last_was_rhw = transformed;
7819 /* If the vertex declaration contains a transformed position attribute,
7820 * the draw uses the fixed function vertex pipeline regardless of any
7821 * vertex shader set by the application. */
7822 if (transformed != wasrhw)
7823 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
7825 if (!use_vs(state))
7827 if (context->last_was_vshader)
7829 for (i = 0; i < gl_info->limits.clipplanes; ++i)
7830 clipplane(context, state, STATE_CLIPPLANE(i));
7833 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7835 /* Because of settings->texcoords, we have to always regenerate the
7836 * vertex shader on a vdecl change.
7837 * TODO: Just always output all the texcoords when there are enough
7838 * varyings available to drop the dependency. */
7839 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
7841 if (use_ps(state)
7842 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
7843 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
7844 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
7846 else
7848 if (!context->last_was_vshader)
7850 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
7851 for (i = 0; i < gl_info->limits.clipplanes; ++i)
7852 clipplane(context, state, STATE_CLIPPLANE(i));
7856 context->last_was_vshader = use_vs(state);
7859 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
7860 const struct wined3d_state *state, DWORD state_id)
7862 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
7863 /* Different vertex shaders potentially require a different vertex attributes setup. */
7864 if (!isStateDirty(context, STATE_VDECL))
7865 context_apply_state(context, state, STATE_VDECL);
7868 static void glsl_vertex_pipe_world(struct wined3d_context *context,
7869 const struct wined3d_state *state, DWORD state_id)
7871 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
7874 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
7875 const struct wined3d_state *state, DWORD state_id)
7877 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
7880 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
7882 const struct wined3d_gl_info *gl_info = context->gl_info;
7883 unsigned int k;
7885 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
7886 | WINED3D_SHADER_CONST_FFP_LIGHTS
7887 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
7889 for (k = 0; k < gl_info->limits.clipplanes; ++k)
7891 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
7892 clipplane(context, state, STATE_CLIPPLANE(k));
7896 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
7897 const struct wined3d_state *state, DWORD state_id)
7899 /* Table fog behavior depends on the projection matrix. */
7900 if (state->render_states[WINED3D_RS_FOGENABLE]
7901 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
7902 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
7903 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
7906 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
7907 const struct wined3d_state *state, DWORD state_id)
7909 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
7910 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
7911 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
7912 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
7913 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
7914 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
7917 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
7918 const struct wined3d_state *state, DWORD state_id)
7920 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7923 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
7924 const struct wined3d_state *state, DWORD state_id)
7926 DWORD sampler = state_id - STATE_SAMPLER(0);
7927 const struct wined3d_texture *texture = state->textures[sampler];
7928 BOOL np2;
7930 if (!texture)
7931 return;
7933 if (sampler >= MAX_TEXTURES)
7934 return;
7936 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
7937 || context->lastWasPow2Texture & (1u << sampler))
7939 if (np2)
7940 context->lastWasPow2Texture |= 1u << sampler;
7941 else
7942 context->lastWasPow2Texture &= ~(1u << sampler);
7944 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7948 static void glsl_vertex_pipe_material(struct wined3d_context *context,
7949 const struct wined3d_state *state, DWORD state_id)
7951 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
7954 static void glsl_vertex_pipe_light(struct wined3d_context *context,
7955 const struct wined3d_state *state, DWORD state_id)
7957 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
7960 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
7961 const struct wined3d_state *state, DWORD state_id)
7963 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
7966 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
7967 const struct wined3d_state *state, DWORD state_id)
7969 if (!use_vs(state))
7970 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
7973 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
7975 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
7976 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
7977 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
7978 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
7979 /* Clip planes */
7980 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
7981 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
7982 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
7983 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
7984 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
7985 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
7986 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
7987 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
7988 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
7989 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
7990 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
7991 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
7992 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
7993 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
7994 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
7995 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
7996 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
7997 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
7998 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
7999 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
8000 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
8001 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
8002 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
8003 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
8004 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
8005 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
8006 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
8007 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
8008 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
8009 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
8010 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
8011 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
8012 /* Lights */
8013 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8014 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8015 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8016 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8017 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8018 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8019 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8020 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8021 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8022 /* Viewport */
8023 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
8024 /* Transform states */
8025 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
8026 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
8027 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8028 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8029 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8030 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8031 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8032 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8033 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8034 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8035 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
8036 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8037 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8038 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8039 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8040 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8041 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8042 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8043 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8044 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8045 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8046 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8047 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8048 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8049 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8050 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8051 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8052 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8053 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8054 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8055 /* Fog */
8056 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8057 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8058 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8059 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8060 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
8061 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
8062 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8063 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8064 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8065 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8066 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8067 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8068 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8069 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8070 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8071 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8072 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
8073 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
8074 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
8075 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
8076 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
8077 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8078 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8079 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8080 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
8081 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8082 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8083 /* NP2 texture matrix fixups. They are not needed if
8084 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
8085 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
8086 * matrix. */
8087 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8088 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8089 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8090 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8091 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8092 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8093 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8094 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8095 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8096 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8097 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8098 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8099 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8100 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8101 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8102 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8103 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8104 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8105 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8106 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8107 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8108 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8109 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8110 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8111 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8112 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
8115 /* TODO:
8116 * - Implement vertex tweening. */
8117 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
8119 glsl_vertex_pipe_vp_enable,
8120 glsl_vertex_pipe_vp_get_caps,
8121 glsl_vertex_pipe_vp_get_emul_mask,
8122 glsl_vertex_pipe_vp_alloc,
8123 glsl_vertex_pipe_vp_free,
8124 glsl_vertex_pipe_vp_states,
8127 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
8129 /* Nothing to do. */
8132 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
8134 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
8135 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
8136 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
8137 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
8138 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
8139 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
8140 | WINED3DTEXOPCAPS_SELECTARG1
8141 | WINED3DTEXOPCAPS_SELECTARG2
8142 | WINED3DTEXOPCAPS_MODULATE4X
8143 | WINED3DTEXOPCAPS_MODULATE2X
8144 | WINED3DTEXOPCAPS_MODULATE
8145 | WINED3DTEXOPCAPS_ADDSIGNED2X
8146 | WINED3DTEXOPCAPS_ADDSIGNED
8147 | WINED3DTEXOPCAPS_ADD
8148 | WINED3DTEXOPCAPS_SUBTRACT
8149 | WINED3DTEXOPCAPS_ADDSMOOTH
8150 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
8151 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
8152 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
8153 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
8154 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
8155 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
8156 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
8157 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
8158 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
8159 | WINED3DTEXOPCAPS_DOTPRODUCT3
8160 | WINED3DTEXOPCAPS_MULTIPLYADD
8161 | WINED3DTEXOPCAPS_LERP
8162 | WINED3DTEXOPCAPS_BUMPENVMAP
8163 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
8164 caps->MaxTextureBlendStages = 8;
8165 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
8168 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
8170 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8171 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8172 return 0;
8175 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8177 struct shader_glsl_priv *priv;
8179 if (shader_backend == &glsl_shader_backend)
8181 priv = shader_priv;
8183 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
8185 ERR("Failed to initialize rbtree.\n");
8186 return NULL;
8189 return priv;
8192 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
8194 return NULL;
8197 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
8199 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8200 struct glsl_ffp_fragment_shader, entry.entry);
8201 struct glsl_shader_prog_link *program, *program2;
8202 struct glsl_ffp_destroy_ctx *ctx = context;
8204 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8205 struct glsl_shader_prog_link, ps.shader_entry)
8207 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8209 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8210 HeapFree(GetProcessHeap(), 0, shader);
8213 /* Context activation is done by the caller. */
8214 static void glsl_fragment_pipe_free(struct wined3d_device *device)
8216 struct shader_glsl_priv *priv = device->fragment_priv;
8217 struct glsl_ffp_destroy_ctx ctx;
8219 ctx.priv = priv;
8220 ctx.gl_info = &device->adapter->gl_info;
8221 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
8224 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
8225 const struct wined3d_state *state, DWORD state_id)
8227 context->last_was_pshader = use_ps(state);
8229 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8232 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
8233 const struct wined3d_state *state, DWORD state_id)
8235 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8238 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
8239 const struct wined3d_state *state, DWORD state_id)
8241 BOOL use_vshader = use_vs(state);
8242 enum fogsource new_source;
8243 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
8244 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
8246 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8248 if (!state->render_states[WINED3D_RS_FOGENABLE])
8249 return;
8251 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
8253 if (use_vshader)
8254 new_source = FOGSOURCE_VS;
8255 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
8256 new_source = FOGSOURCE_COORD;
8257 else
8258 new_source = FOGSOURCE_FFP;
8260 else
8262 new_source = FOGSOURCE_FFP;
8265 if (new_source != context->fog_source || fogstart == fogend)
8267 context->fog_source = new_source;
8268 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8272 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
8273 const struct wined3d_state *state, DWORD state_id)
8275 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
8276 glsl_fragment_pipe_fog(context, state, state_id);
8279 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
8280 const struct wined3d_state *state, DWORD state_id)
8282 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8285 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
8286 const struct wined3d_state *state, DWORD state_id)
8288 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
8291 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
8292 const struct wined3d_state *state, DWORD state_id)
8294 const struct wined3d_gl_info *gl_info = context->gl_info;
8295 int glParm;
8296 float ref;
8298 TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
8300 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
8302 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
8303 checkGLcall("glEnable GL_ALPHA_TEST");
8305 else
8307 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
8308 checkGLcall("glDisable GL_ALPHA_TEST");
8309 return;
8312 ref = ((float)state->render_states[WINED3D_RS_ALPHAREF]) / 255.0f;
8313 glParm = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
8315 if (glParm)
8317 gl_info->gl_ops.gl.p_glAlphaFunc(glParm, ref);
8318 checkGLcall("glAlphaFunc");
8322 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
8323 const struct wined3d_state *state, DWORD state_id)
8325 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
8328 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
8330 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8331 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8332 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8333 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8334 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8335 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8336 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8337 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8338 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8339 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8340 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8341 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8342 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8343 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8344 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8345 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8346 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8347 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8348 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8349 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8350 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8351 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8352 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8353 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8354 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8355 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8356 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8357 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8358 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8359 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8360 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8361 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8362 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8363 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8364 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8365 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8366 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8367 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8368 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8369 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8370 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8371 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8372 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8373 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8374 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8375 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8376 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8377 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8378 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8379 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8380 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8381 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8382 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8383 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8384 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8385 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8386 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8387 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8388 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8389 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8390 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8391 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8392 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8393 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8394 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8395 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8396 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8397 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8398 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8399 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8400 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8401 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8402 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8403 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8404 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
8405 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
8406 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
8407 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_EXT_NONE },
8408 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8409 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
8410 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
8411 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8412 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8413 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
8414 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
8415 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
8416 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8417 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
8418 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
8419 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
8420 {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 },
8421 {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 },
8422 {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 },
8423 {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 },
8424 {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 },
8425 {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 },
8426 {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 },
8427 {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 },
8428 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8429 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8430 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8431 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8432 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8433 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8434 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8435 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8436 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8437 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
8438 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
8441 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
8443 return TRUE;
8446 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
8450 const struct fragment_pipeline glsl_fragment_pipe =
8452 glsl_fragment_pipe_enable,
8453 glsl_fragment_pipe_get_caps,
8454 glsl_fragment_pipe_get_emul_mask,
8455 glsl_fragment_pipe_alloc,
8456 glsl_fragment_pipe_free,
8457 glsl_fragment_pipe_alloc_context_data,
8458 glsl_fragment_pipe_free_context_data,
8459 shader_glsl_color_fixup_supported,
8460 glsl_fragment_pipe_state_template,