wined3d: Avoid the builtin color varyings on core profile.
[wine.git] / dlls / wined3d / glsl_shader.c
blob93734867f1cd8790b412c1c440f4ab75f33a893d
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 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1546 return gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
1549 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1550 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1552 va_list args;
1553 int ret;
1555 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1556 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1557 for (;;)
1559 va_start(args, format);
1560 ret = shader_vaddline(buffer, format, args);
1561 va_end(args);
1562 if (!ret)
1563 return;
1564 if (!string_buffer_resize(buffer, ret))
1565 return;
1569 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1570 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1572 va_list args;
1573 int ret;
1575 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1576 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1577 for (;;)
1579 va_start(args, format);
1580 ret = shader_vaddline(buffer, format, args);
1581 va_end(args);
1582 if (!ret)
1583 return;
1584 if (!string_buffer_resize(buffer, ret))
1585 return;
1589 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
1591 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
1592 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1593 const BOOL *input_reg_used = shader->u.ps.input_reg_used;
1594 unsigned int i;
1596 if (reg_maps->shader_version.major < 3)
1597 return input_reg_used[idx];
1599 for (i = 0; i < input_signature->element_count; ++i)
1601 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
1603 if (!(reg_maps->input_registers & (1u << input->register_idx)))
1604 continue;
1606 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
1607 && input->semantic_idx == idx)
1609 if (input_reg_used[input->register_idx])
1610 return TRUE;
1611 else
1612 return FALSE;
1615 return FALSE;
1618 /** Generate the variable & register declarations for the GLSL output target */
1619 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1620 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1621 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1623 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1624 const struct wined3d_state *state = &shader->device->state;
1625 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1626 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1627 const struct wined3d_gl_info *gl_info = context->gl_info;
1628 const struct wined3d_fb_state *fb = &shader->device->fb;
1629 unsigned int i, extra_constants_needed = 0;
1630 const struct wined3d_shader_lconst *lconst;
1631 const char *prefix;
1632 DWORD map;
1634 prefix = shader_glsl_get_prefix(version->type);
1636 /* Prototype the subroutines */
1637 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1639 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1642 /* Declare the constants (aka uniforms) */
1643 if (shader->limits->constant_float > 0)
1645 unsigned max_constantsF;
1647 /* Unless the shader uses indirect addressing, always declare the
1648 * maximum array size and ignore that we need some uniforms privately.
1649 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1650 * and immediate values, still declare VC[256]. If the shader needs
1651 * more uniforms than we have it won't work in any case. If it uses
1652 * less, the compiler will figure out which uniforms are really used
1653 * and strip them out. This allows a shader to use c255 on a dx9 card,
1654 * as long as it doesn't also use all the other constants.
1656 * If the shader uses indirect addressing the compiler must assume
1657 * that all declared uniforms are used. In this case, declare only the
1658 * amount that we're assured to have.
1660 * Thus we run into problems in these two cases:
1661 * 1) The shader really uses more uniforms than supported.
1662 * 2) The shader uses indirect addressing, less constants than
1663 * supported, but uses a constant index > #supported consts. */
1664 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1666 /* No indirect addressing here. */
1667 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1669 else
1671 if (reg_maps->usesrelconstF)
1673 /* Subtract the other potential uniforms from the max
1674 * available (bools, ints, and 1 row of projection matrix).
1675 * Subtract another uniform for immediate values, which have
1676 * to be loaded via uniform by the driver as well. The shader
1677 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1678 * shader code, so one vec4 should be enough. (Unfortunately
1679 * the Nvidia driver doesn't store 128 and -128 in one float).
1681 * Writing gl_ClipVertex requires one uniform for each
1682 * clipplane as well. */
1683 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1684 if(ctx_priv->cur_vs_args->clip_enabled)
1686 max_constantsF -= gl_info->limits.clipplanes;
1688 max_constantsF -= count_bits(reg_maps->integer_constants);
1689 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1690 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1691 * for now take this into account when calculating the number of available constants
1693 max_constantsF -= count_bits(reg_maps->boolean_constants);
1694 /* Set by driver quirks in directx.c */
1695 max_constantsF -= gl_info->reserved_glsl_constants;
1697 if (max_constantsF < shader->limits->constant_float)
1699 static unsigned int once;
1701 if (!once++)
1702 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1703 " it may not render correctly.\n");
1704 else
1705 WARN("The hardware does not support enough uniform components to run this shader.\n");
1708 else
1710 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1713 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1714 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1717 /* Always declare the full set of constants, the compiler can remove the
1718 * unused ones because d3d doesn't (yet) support indirect int and bool
1719 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1720 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1721 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1723 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1724 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1726 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1728 if (reg_maps->cb_sizes[i])
1729 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1730 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1733 /* Declare texture samplers */
1734 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1736 struct wined3d_shader_sampler_map_entry *entry;
1737 BOOL shadow_sampler, tex_rect;
1738 const char *sampler_type;
1740 entry = &reg_maps->sampler_map.entries[i];
1742 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1744 ERR("Invalid resource index %u.\n", entry->resource_idx);
1745 continue;
1748 shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << entry->sampler_idx));
1749 switch (reg_maps->resource_info[entry->resource_idx].type)
1751 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1752 if (shadow_sampler)
1753 sampler_type = "sampler1DShadow";
1754 else
1755 sampler_type = "sampler1D";
1756 break;
1758 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1759 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1760 && (ps_args->np2_fixup & (1u << entry->resource_idx))
1761 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1762 if (shadow_sampler)
1764 if (tex_rect)
1765 sampler_type = "sampler2DRectShadow";
1766 else
1767 sampler_type = "sampler2DShadow";
1769 else
1771 if (tex_rect)
1772 sampler_type = "sampler2DRect";
1773 else
1774 sampler_type = "sampler2D";
1776 break;
1778 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1779 if (shadow_sampler)
1780 FIXME("Unsupported 3D shadow sampler.\n");
1781 sampler_type = "sampler3D";
1782 break;
1784 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1785 if (shadow_sampler)
1786 FIXME("Unsupported Cube shadow sampler.\n");
1787 sampler_type = "samplerCube";
1788 break;
1790 default:
1791 sampler_type = "unsupported_sampler";
1792 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[i].type);
1793 break;
1795 shader_addline(buffer, "uniform %s %s_sampler%u;\n", sampler_type, prefix, entry->bind_idx);
1798 /* Declare uniforms for NP2 texcoord fixup:
1799 * This is NOT done inside the loop that declares the texture samplers
1800 * since the NP2 fixup code is currently only used for the GeforceFX
1801 * series and when forcing the ARB_npot extension off. Modern cards just
1802 * skip the code anyway, so put it inside a separate loop. */
1803 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1805 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1806 UINT cur = 0;
1808 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1809 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1810 * samplerNP2Fixup stores texture dimensions and is updated through
1811 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1813 for (i = 0; i < shader->limits->sampler; ++i)
1815 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
1816 continue;
1818 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1820 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1821 continue;
1824 fixup->idx[i] = cur++;
1827 fixup->num_consts = (cur + 1) >> 1;
1828 fixup->active = ps_args->np2_fixup;
1829 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1832 /* Declare address variables */
1833 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1835 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1838 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1840 for (i = 0; i < shader->input_signature.element_count; ++i)
1842 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
1843 if (e->sysval_semantic == WINED3D_SV_INSTANCEID)
1844 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1845 prefix, e->register_idx);
1846 else
1847 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, e->register_idx);
1850 if (vs_args->point_size && !vs_args->per_vertex_point_size)
1852 shader_addline(buffer, "uniform struct\n{\n");
1853 shader_addline(buffer, " float size;\n");
1854 shader_addline(buffer, " float size_min;\n");
1855 shader_addline(buffer, " float size_max;\n");
1856 shader_addline(buffer, "} ffp_point;\n");
1859 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && version->major < 3)
1861 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_diffuse;\n");
1862 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_specular;\n");
1863 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
1864 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
1867 shader_addline(buffer, "uniform vec4 posFixup;\n");
1868 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits->packed_output);
1870 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1872 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
1874 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1876 if (version->major < 3 || ps_args->vp_mode != vertexshader)
1878 shader_addline(buffer, "uniform struct\n{\n");
1879 shader_addline(buffer, " vec4 color;\n");
1880 shader_addline(buffer, " float density;\n");
1881 shader_addline(buffer, " float end;\n");
1882 shader_addline(buffer, " float scale;\n");
1883 shader_addline(buffer, "} ffp_fog;\n");
1885 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
1887 if (glsl_is_color_reg_read(shader, 0))
1888 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
1889 if (glsl_is_color_reg_read(shader, 1))
1890 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
1891 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
1892 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
1894 else
1896 if (glsl_is_color_reg_read(shader, 0))
1897 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_diffuse;\n");
1898 if (glsl_is_color_reg_read(shader, 1))
1899 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_specular;\n");
1900 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
1901 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
1902 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
1906 if (version->major >= 3)
1908 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
1910 if (use_vs(state))
1911 declare_in_varying(gl_info, buffer, FALSE, "vec4 %s_link[%u];\n", prefix, in_count);
1912 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1915 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1917 if (!(map & 1))
1918 continue;
1920 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1922 if (reg_maps->luminanceparams & (1u << i))
1924 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1925 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1926 extra_constants_needed++;
1929 extra_constants_needed++;
1932 if (ps_args->srgb_correction)
1934 shader_addline(buffer, "const vec4 srgb_const0 = ");
1935 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
1936 shader_addline(buffer, ";\n");
1937 shader_addline(buffer, "const vec4 srgb_const1 = ");
1938 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
1939 shader_addline(buffer, ";\n");
1941 if (reg_maps->vpos || reg_maps->usesdsy)
1943 if (shader->limits->constant_float + extra_constants_needed
1944 + 1 < gl_info->limits.glsl_ps_float_constants)
1946 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1947 extra_constants_needed++;
1949 else
1951 float ycorrection[] =
1953 context->render_offscreen ? 0.0f : fb->render_targets[0]->height,
1954 context->render_offscreen ? 1.0f : -1.0f,
1955 0.0f,
1956 0.0f,
1959 /* This happens because we do not have proper tracking of the
1960 * constant registers that are actually used, only the max
1961 * limit of the shader version. */
1962 FIXME("Cannot find a free uniform for vpos correction params\n");
1963 shader_addline(buffer, "const vec4 ycorrection = ");
1964 shader_glsl_append_imm_vec4(buffer, ycorrection);
1965 shader_addline(buffer, ";\n");
1967 shader_addline(buffer, "vec4 vpos;\n");
1971 /* Declare output register temporaries */
1972 if (shader->limits->packed_output)
1973 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
1975 /* Declare temporary variables */
1976 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1978 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1981 /* Declare loop registers aLx */
1982 if (version->major < 4)
1984 for (i = 0; i < reg_maps->loop_depth; ++i)
1986 shader_addline(buffer, "int aL%u;\n", i);
1987 shader_addline(buffer, "int tmpInt%u;\n", i);
1991 /* Temporary variables for matrix operations */
1992 shader_addline(buffer, "vec4 tmp0;\n");
1993 shader_addline(buffer, "vec4 tmp1;\n");
1995 if (!shader->load_local_constsF)
1997 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1999 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2000 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2001 shader_addline(buffer, ";\n");
2005 /* Start the main program. */
2006 shader_addline(buffer, "void main()\n{\n");
2008 /* Direct3D applications expect integer vPos values, while OpenGL drivers
2009 * add approximately 0.5. This causes off-by-one problems as spotted by
2010 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
2011 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
2012 * causes precision troubles when we just subtract 0.5.
2014 * To deal with that, just floor() the position. This will eliminate the
2015 * fraction on all cards.
2017 * TODO: Test how this behaves with multisampling.
2019 * An advantage of floor is that it works even if the driver doesn't add
2020 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
2021 * to return in gl_FragCoord, even though coordinates specify the pixel
2022 * centers instead of the pixel corners. This code will behave correctly
2023 * on drivers that returns integer values. */
2024 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
2026 if (shader->device->wined3d->flags & WINED3D_PIXEL_CENTER_INTEGER)
2027 shader_addline(buffer,
2028 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
2029 else
2030 shader_addline(buffer,
2031 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
2035 /*****************************************************************************
2036 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2038 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2039 ****************************************************************************/
2041 /* Prototypes */
2042 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2043 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2045 /** Used for opcode modifiers - They multiply the result by the specified amount */
2046 static const char * const shift_glsl_tab[] = {
2047 "", /* 0 (none) */
2048 "2.0 * ", /* 1 (x2) */
2049 "4.0 * ", /* 2 (x4) */
2050 "8.0 * ", /* 3 (x8) */
2051 "16.0 * ", /* 4 (x16) */
2052 "32.0 * ", /* 5 (x32) */
2053 "", /* 6 (x64) */
2054 "", /* 7 (x128) */
2055 "", /* 8 (d256) */
2056 "", /* 9 (d128) */
2057 "", /* 10 (d64) */
2058 "", /* 11 (d32) */
2059 "0.0625 * ", /* 12 (d16) */
2060 "0.125 * ", /* 13 (d8) */
2061 "0.25 * ", /* 14 (d4) */
2062 "0.5 * " /* 15 (d2) */
2065 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2066 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2067 const char *in_reg, const char *in_regswizzle, char *out_str)
2069 out_str[0] = 0;
2071 switch (src_modifier)
2073 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2074 case WINED3DSPSM_DW:
2075 case WINED3DSPSM_NONE:
2076 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2077 break;
2078 case WINED3DSPSM_NEG:
2079 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2080 break;
2081 case WINED3DSPSM_NOT:
2082 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2083 break;
2084 case WINED3DSPSM_BIAS:
2085 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2086 break;
2087 case WINED3DSPSM_BIASNEG:
2088 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2089 break;
2090 case WINED3DSPSM_SIGN:
2091 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2092 break;
2093 case WINED3DSPSM_SIGNNEG:
2094 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2095 break;
2096 case WINED3DSPSM_COMP:
2097 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2098 break;
2099 case WINED3DSPSM_X2:
2100 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2101 break;
2102 case WINED3DSPSM_X2NEG:
2103 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2104 break;
2105 case WINED3DSPSM_ABS:
2106 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2107 break;
2108 case WINED3DSPSM_ABSNEG:
2109 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2110 break;
2111 default:
2112 FIXME("Unhandled modifier %u\n", src_modifier);
2113 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2117 /** Writes the GLSL variable name that corresponds to the register that the
2118 * DX opcode parameter is trying to access */
2119 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2120 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2122 /* oPos, oFog and oPts in D3D */
2123 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2125 const struct wined3d_shader *shader = ins->ctx->shader;
2126 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2127 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2128 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2129 const char *prefix = shader_glsl_get_prefix(version->type);
2130 struct glsl_src_param rel_param0, rel_param1;
2131 char imm_str[4][17];
2133 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2134 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2135 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2136 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2137 *is_color = FALSE;
2139 switch (reg->type)
2141 case WINED3DSPR_TEMP:
2142 sprintf(register_name, "R%u", reg->idx[0].offset);
2143 break;
2145 case WINED3DSPR_INPUT:
2146 /* vertex shaders */
2147 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2149 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2150 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2151 *is_color = TRUE;
2152 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2153 break;
2156 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2158 if (reg->idx[0].rel_addr)
2160 if (reg->idx[1].rel_addr)
2161 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
2162 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2163 else
2164 sprintf(register_name, "gs_in[%s + %u][%u]",
2165 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
2167 else if (reg->idx[1].rel_addr)
2168 sprintf(register_name, "gs_in[%u][%s + %u]",
2169 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2170 else
2171 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
2172 break;
2175 /* pixel shaders >= 3.0 */
2176 if (version->major >= 3)
2178 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2179 unsigned int in_count = vec4_varyings(version->major, gl_info);
2181 if (reg->idx[0].rel_addr)
2183 /* Removing a + 0 would be an obvious optimization, but
2184 * OS X doesn't see the NOP operation there. */
2185 if (idx)
2187 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2188 && shader->u.ps.declared_in_count > in_count)
2190 sprintf(register_name,
2191 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2192 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2193 prefix, rel_param0.param_str, idx);
2195 else
2197 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2200 else
2202 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2203 && shader->u.ps.declared_in_count > in_count)
2205 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2206 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2207 prefix, rel_param0.param_str);
2209 else
2211 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2215 else
2217 if (idx == in_count) sprintf(register_name, "gl_Color");
2218 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2219 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2222 else
2224 if (!reg->idx[0].offset)
2225 strcpy(register_name, "ffp_varying_diffuse");
2226 else
2227 strcpy(register_name, "ffp_varying_specular");
2228 break;
2230 break;
2232 case WINED3DSPR_CONST:
2234 /* Relative addressing */
2235 if (reg->idx[0].rel_addr)
2237 if (reg->idx[0].offset)
2238 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2239 else
2240 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2242 else
2244 if (shader_constant_is_local(shader, reg->idx[0].offset))
2245 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2246 else
2247 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2250 break;
2252 case WINED3DSPR_CONSTINT:
2253 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2254 break;
2256 case WINED3DSPR_CONSTBOOL:
2257 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2258 break;
2260 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2261 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2262 sprintf(register_name, "T%u", reg->idx[0].offset);
2263 else
2264 sprintf(register_name, "A%u", reg->idx[0].offset);
2265 break;
2267 case WINED3DSPR_LOOP:
2268 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
2269 break;
2271 case WINED3DSPR_SAMPLER:
2272 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2273 break;
2275 case WINED3DSPR_COLOROUT:
2276 if (reg->idx[0].offset >= gl_info->limits.buffers)
2277 WARN("Write to render target %u, only %d supported.\n",
2278 reg->idx[0].offset, gl_info->limits.buffers);
2280 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
2281 break;
2283 case WINED3DSPR_RASTOUT:
2284 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2285 break;
2287 case WINED3DSPR_DEPTHOUT:
2288 sprintf(register_name, "gl_FragDepth");
2289 break;
2291 case WINED3DSPR_ATTROUT:
2292 if (!reg->idx[0].offset)
2293 sprintf(register_name, "%s_out[8]", prefix);
2294 else
2295 sprintf(register_name, "%s_out[9]", prefix);
2296 break;
2298 case WINED3DSPR_TEXCRDOUT:
2299 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2300 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2301 break;
2303 case WINED3DSPR_MISCTYPE:
2304 if (!reg->idx[0].offset)
2306 /* vPos */
2307 sprintf(register_name, "vpos");
2309 else if (reg->idx[0].offset == 1)
2311 /* Note that gl_FrontFacing is a bool, while vFace is
2312 * a float for which the sign determines front/back */
2313 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2315 else
2317 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2318 sprintf(register_name, "unrecognized_register");
2320 break;
2322 case WINED3DSPR_IMMCONST:
2323 switch (reg->immconst_type)
2325 case WINED3D_IMMCONST_SCALAR:
2326 switch (reg->data_type)
2328 case WINED3D_DATA_FLOAT:
2329 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
2330 break;
2331 case WINED3D_DATA_INT:
2332 sprintf(register_name, "%#x", reg->immconst_data[0]);
2333 break;
2334 case WINED3D_DATA_RESOURCE:
2335 case WINED3D_DATA_SAMPLER:
2336 case WINED3D_DATA_UINT:
2337 sprintf(register_name, "%#xu", reg->immconst_data[0]);
2338 break;
2339 default:
2340 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2341 break;
2343 break;
2345 case WINED3D_IMMCONST_VEC4:
2346 switch (reg->data_type)
2348 case WINED3D_DATA_FLOAT:
2349 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
2350 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
2351 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
2352 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
2353 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2354 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2355 break;
2356 case WINED3D_DATA_INT:
2357 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2358 reg->immconst_data[0], reg->immconst_data[1],
2359 reg->immconst_data[2], reg->immconst_data[3]);
2360 break;
2361 case WINED3D_DATA_RESOURCE:
2362 case WINED3D_DATA_SAMPLER:
2363 case WINED3D_DATA_UINT:
2364 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2365 reg->immconst_data[0], reg->immconst_data[1],
2366 reg->immconst_data[2], reg->immconst_data[3]);
2367 break;
2368 default:
2369 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2370 break;
2372 break;
2374 default:
2375 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2376 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2378 break;
2380 case WINED3DSPR_CONSTBUFFER:
2381 if (reg->idx[1].rel_addr)
2382 sprintf(register_name, "%s_cb%u[%s + %u]",
2383 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2384 else
2385 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2386 break;
2388 case WINED3DSPR_PRIMID:
2389 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2390 break;
2392 default:
2393 FIXME("Unhandled register type %#x.\n", reg->type);
2394 sprintf(register_name, "unrecognized_register");
2395 break;
2399 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2401 *str++ = '.';
2402 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2403 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2404 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2405 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2406 *str = '\0';
2409 /* Get the GLSL write mask for the destination register */
2410 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2412 DWORD mask = param->write_mask;
2414 if (shader_is_scalar(&param->reg))
2416 mask = WINED3DSP_WRITEMASK_0;
2417 *write_mask = '\0';
2419 else
2421 shader_glsl_write_mask_to_str(mask, write_mask);
2424 return mask;
2427 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
2428 unsigned int size = 0;
2430 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2431 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2432 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2433 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2435 return size;
2438 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2440 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2441 * but addressed as "rgba". To fix this we need to swap the register's x
2442 * and z components. */
2443 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2445 *str++ = '.';
2446 /* swizzle bits fields: wwzzyyxx */
2447 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
2448 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
2449 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
2450 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
2451 *str = '\0';
2454 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2455 BOOL fixup, DWORD mask, char *swizzle_str)
2457 if (shader_is_scalar(&param->reg))
2458 *swizzle_str = '\0';
2459 else
2460 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2463 /* From a given parameter token, generate the corresponding GLSL string.
2464 * Also, return the actual register name and swizzle in case the
2465 * caller needs this information as well. */
2466 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2467 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
2469 BOOL is_color = FALSE;
2470 char swizzle_str[6];
2472 glsl_src->reg_name[0] = '\0';
2473 glsl_src->param_str[0] = '\0';
2474 swizzle_str[0] = '\0';
2476 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
2477 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
2479 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
2481 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
2483 else
2485 char reg_name[200];
2487 switch (wined3d_src->reg.data_type)
2489 case WINED3D_DATA_FLOAT:
2490 sprintf(reg_name, "%s", glsl_src->reg_name);
2491 break;
2492 case WINED3D_DATA_INT:
2493 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
2494 break;
2495 case WINED3D_DATA_RESOURCE:
2496 case WINED3D_DATA_SAMPLER:
2497 case WINED3D_DATA_UINT:
2498 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
2499 break;
2500 default:
2501 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
2502 sprintf(reg_name, "%s", glsl_src->reg_name);
2503 break;
2506 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
2510 /* From a given parameter token, generate the corresponding GLSL string.
2511 * Also, return the actual register name and swizzle in case the
2512 * caller needs this information as well. */
2513 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
2514 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
2516 BOOL is_color = FALSE;
2518 glsl_dst->mask_str[0] = '\0';
2519 glsl_dst->reg_name[0] = '\0';
2521 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
2522 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
2525 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2526 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
2527 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
2528 enum wined3d_data_type data_type)
2530 struct glsl_dst_param glsl_dst;
2531 DWORD mask;
2533 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
2535 switch (data_type)
2537 case WINED3D_DATA_FLOAT:
2538 shader_addline(buffer, "%s%s = %s(",
2539 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2540 break;
2541 case WINED3D_DATA_INT:
2542 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
2543 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2544 break;
2545 case WINED3D_DATA_RESOURCE:
2546 case WINED3D_DATA_SAMPLER:
2547 case WINED3D_DATA_UINT:
2548 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
2549 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2550 break;
2551 default:
2552 FIXME("Unhandled data type %#x.\n", data_type);
2553 shader_addline(buffer, "%s%s = %s(",
2554 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2555 break;
2559 return mask;
2562 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2563 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
2565 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2568 /** Process GLSL instruction modifiers */
2569 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
2571 struct glsl_dst_param dst_param;
2572 DWORD modifiers;
2574 if (!ins->dst_count) return;
2576 modifiers = ins->dst[0].modifiers;
2577 if (!modifiers) return;
2579 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2581 if (modifiers & WINED3DSPDM_SATURATE)
2583 /* _SAT means to clamp the value of the register to between 0 and 1 */
2584 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
2585 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
2588 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
2590 FIXME("_centroid modifier not handled\n");
2593 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
2595 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2599 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
2601 switch (op)
2603 case WINED3D_SHADER_REL_OP_GT: return ">";
2604 case WINED3D_SHADER_REL_OP_EQ: return "==";
2605 case WINED3D_SHADER_REL_OP_GE: return ">=";
2606 case WINED3D_SHADER_REL_OP_LT: return "<";
2607 case WINED3D_SHADER_REL_OP_NE: return "!=";
2608 case WINED3D_SHADER_REL_OP_LE: return "<=";
2609 default:
2610 FIXME("Unrecognized operator %#x.\n", op);
2611 return "(\?\?)";
2615 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
2616 DWORD resource_idx, DWORD flags, struct glsl_sample_function *sample_function)
2618 static const struct
2620 unsigned int coord_size;
2621 const char *type_part;
2623 resource_types[] =
2625 {0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
2626 {1, ""}, /* WINED3D_SHADER_RESOURCE_BUFFER */
2627 {1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
2628 {2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
2629 {2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
2630 {3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
2631 {3, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
2632 {2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
2633 {3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
2634 {3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
2636 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2637 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
2638 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2639 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
2640 && (priv->cur_ps_args->shadow & (1u << resource_idx));
2641 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
2642 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2643 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
2644 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
2645 const char *base = "texture", *type_part = "", *suffix = "";
2646 unsigned int coord_size;
2648 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
2650 if (resource_type >= ARRAY_SIZE(resource_types))
2652 ERR("Unexpected resource type %#x.\n", resource_type);
2653 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
2656 /* Note that there's no such thing as a projected cube texture. */
2657 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
2658 projected = FALSE;
2660 if (shadow)
2661 base = "shadow";
2663 type_part = resource_types[resource_type].type_part;
2664 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
2665 type_part = "2DRect";
2666 if (!type_part[0])
2667 FIXME("Unhandled resource type %#x.\n", resource_type);
2669 if (!lod && grad && !gl_info->supported[EXT_GPU_SHADER4])
2671 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2672 suffix = "ARB";
2673 else
2674 FIXME("Unsupported grad function.\n");
2677 sample_function->name = string_buffer_get(priv->string_buffers);
2678 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
2679 lod ? "Lod" : grad ? "Grad" : "", suffix);
2681 coord_size = resource_types[resource_type].coord_size;
2682 if (shadow)
2683 ++coord_size;
2684 sample_function->coord_mask = (1u << coord_size) - 1;
2687 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
2688 struct glsl_sample_function *sample_function)
2690 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2692 string_buffer_release(priv->string_buffers, sample_function->name);
2695 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2696 BOOL sign_fixup, enum fixup_channel_source channel_source)
2698 switch(channel_source)
2700 case CHANNEL_SOURCE_ZERO:
2701 strcat(arguments, "0.0");
2702 break;
2704 case CHANNEL_SOURCE_ONE:
2705 strcat(arguments, "1.0");
2706 break;
2708 case CHANNEL_SOURCE_X:
2709 strcat(arguments, reg_name);
2710 strcat(arguments, ".x");
2711 break;
2713 case CHANNEL_SOURCE_Y:
2714 strcat(arguments, reg_name);
2715 strcat(arguments, ".y");
2716 break;
2718 case CHANNEL_SOURCE_Z:
2719 strcat(arguments, reg_name);
2720 strcat(arguments, ".z");
2721 break;
2723 case CHANNEL_SOURCE_W:
2724 strcat(arguments, reg_name);
2725 strcat(arguments, ".w");
2726 break;
2728 default:
2729 FIXME("Unhandled channel source %#x\n", channel_source);
2730 strcat(arguments, "undefined");
2731 break;
2734 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2737 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
2738 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2740 unsigned int mask_size, remaining;
2741 DWORD fixup_mask = 0;
2742 char arguments[256];
2743 char mask_str[6];
2745 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2746 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2747 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2748 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2749 if (!(mask &= fixup_mask))
2750 return;
2752 if (is_complex_fixup(fixup))
2754 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2755 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2756 return;
2759 shader_glsl_write_mask_to_str(mask, mask_str);
2760 mask_size = shader_glsl_get_write_mask_size(mask);
2762 arguments[0] = '\0';
2763 remaining = mask_size;
2764 if (mask & WINED3DSP_WRITEMASK_0)
2766 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2767 if (--remaining) strcat(arguments, ", ");
2769 if (mask & WINED3DSP_WRITEMASK_1)
2771 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2772 if (--remaining) strcat(arguments, ", ");
2774 if (mask & WINED3DSP_WRITEMASK_2)
2776 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2777 if (--remaining) strcat(arguments, ", ");
2779 if (mask & WINED3DSP_WRITEMASK_3)
2781 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2782 if (--remaining) strcat(arguments, ", ");
2785 if (mask_size > 1)
2786 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2787 else
2788 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2791 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2793 char reg_name[256];
2794 BOOL is_color;
2796 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2797 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2800 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2801 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2802 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2804 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2805 char dst_swizzle[6];
2806 struct color_fixup_desc fixup;
2807 BOOL np2_fixup = FALSE;
2808 va_list args;
2809 int ret;
2811 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2813 /* FIXME: We currently don't support fixups for vertex shaders or anything
2814 * above SM3. Note that for SM4+ the sampler index doesn't have to match
2815 * the resource index. */
2816 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
2818 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2819 fixup = priv->cur_ps_args->color_fixup[sampler];
2821 if (priv->cur_ps_args->np2_fixup & (1u << sampler))
2822 np2_fixup = TRUE;
2824 else
2826 fixup = COLOR_FIXUP_IDENTITY;
2829 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2831 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2832 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler);
2834 for (;;)
2836 va_start(args, coord_reg_fmt);
2837 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2838 va_end(args);
2839 if (!ret)
2840 break;
2841 if (!string_buffer_resize(ins->ctx->buffer, ret))
2842 break;
2845 if (np2_fixup)
2847 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2848 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2850 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
2852 case 1:
2853 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2854 idx >> 1, (idx % 2) ? "z" : "x");
2855 break;
2856 case 2:
2857 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2858 idx >> 1, (idx % 2) ? "zw" : "xy");
2859 break;
2860 case 3:
2861 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
2862 idx >> 1, (idx % 2) ? "zw" : "xy");
2863 break;
2864 case 4:
2865 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
2866 idx >> 1, (idx % 2) ? "zw" : "xy");
2867 break;
2870 if (dx && dy)
2871 shader_addline(ins->ctx->buffer, ", %s, %s)", dx, dy);
2872 else if (bias)
2873 shader_addline(ins->ctx->buffer, ", %s)", bias);
2874 else
2875 shader_addline(ins->ctx->buffer, ")");
2877 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
2879 if (!is_identity_fixup(fixup))
2880 shader_glsl_color_correction(ins, fixup);
2883 /*****************************************************************************
2884 * Begin processing individual instruction opcodes
2885 ****************************************************************************/
2887 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2889 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2890 struct glsl_src_param src0_param;
2891 struct glsl_src_param src1_param;
2892 DWORD write_mask;
2893 const char *op;
2895 /* Determine the GLSL operator to use based on the opcode */
2896 switch (ins->handler_idx)
2898 case WINED3DSIH_ADD: op = "+"; break;
2899 case WINED3DSIH_AND: op = "&"; break;
2900 case WINED3DSIH_DIV: op = "/"; break;
2901 case WINED3DSIH_IADD: op = "+"; break;
2902 case WINED3DSIH_ISHL: op = "<<"; break;
2903 case WINED3DSIH_MUL: op = "*"; break;
2904 case WINED3DSIH_OR: op = "|"; break;
2905 case WINED3DSIH_SUB: op = "-"; break;
2906 case WINED3DSIH_USHR: op = ">>"; break;
2907 case WINED3DSIH_XOR: op = "^"; break;
2908 default:
2909 op = "<unhandled operator>";
2910 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2911 break;
2914 write_mask = shader_glsl_append_dst(buffer, ins);
2915 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2916 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2917 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2920 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2922 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2923 struct glsl_src_param src0_param;
2924 struct glsl_src_param src1_param;
2925 unsigned int mask_size;
2926 DWORD write_mask;
2927 const char *op;
2929 write_mask = shader_glsl_append_dst(buffer, ins);
2930 mask_size = shader_glsl_get_write_mask_size(write_mask);
2931 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2932 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2934 if (mask_size > 1)
2936 switch (ins->handler_idx)
2938 case WINED3DSIH_EQ: op = "equal"; break;
2939 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2940 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2941 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
2942 case WINED3DSIH_LT: op = "lessThan"; break;
2943 case WINED3DSIH_NE: op = "notEqual"; break;
2944 default:
2945 op = "<unhandled operator>";
2946 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2947 break;
2950 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2951 mask_size, op, src0_param.param_str, src1_param.param_str);
2953 else
2955 switch (ins->handler_idx)
2957 case WINED3DSIH_EQ: op = "=="; break;
2958 case WINED3DSIH_GE: op = ">="; break;
2959 case WINED3DSIH_IGE: op = ">="; break;
2960 case WINED3DSIH_UGE: op = ">="; break;
2961 case WINED3DSIH_LT: op = "<"; break;
2962 case WINED3DSIH_NE: op = "!="; break;
2963 default:
2964 op = "<unhandled operator>";
2965 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2966 break;
2969 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2970 src0_param.param_str, op, src1_param.param_str);
2974 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2976 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2977 struct glsl_src_param src0_param;
2978 struct glsl_src_param src1_param;
2979 DWORD write_mask;
2981 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2982 * not, we can emulate it. */
2983 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2984 FIXME("64-bit integer multiplies not implemented.\n");
2986 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2988 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2989 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2990 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2992 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2993 src0_param.param_str, src1_param.param_str);
2997 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2999 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3000 struct glsl_src_param src0_param, src1_param;
3001 DWORD write_mask;
3003 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3006 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3008 char dst_mask[6];
3010 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3011 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3012 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3013 shader_addline(buffer, "tmp0%s = %s / %s;\n",
3014 dst_mask, src0_param.param_str, src1_param.param_str);
3016 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3017 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3018 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3019 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
3021 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3022 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3024 else
3026 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3027 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3028 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3029 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3032 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3034 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3035 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3036 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3037 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3041 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3042 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3044 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3045 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3046 struct glsl_src_param src0_param;
3047 DWORD write_mask;
3049 write_mask = shader_glsl_append_dst(buffer, ins);
3050 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3052 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3053 * shader versions WINED3DSIO_MOVA is used for this. */
3054 if (ins->ctx->reg_maps->shader_version.major == 1
3055 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3056 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3058 /* This is a simple floor() */
3059 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3060 if (mask_size > 1) {
3061 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3062 } else {
3063 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3066 else if(ins->handler_idx == WINED3DSIH_MOVA)
3068 /* We need to *round* to the nearest int here. */
3069 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3071 if (gl_info->supported[EXT_GPU_SHADER4])
3073 if (mask_size > 1)
3074 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3075 else
3076 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3078 else
3080 if (mask_size > 1)
3081 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3082 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3083 else
3084 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3085 src0_param.param_str, src0_param.param_str);
3088 else
3090 shader_addline(buffer, "%s);\n", src0_param.param_str);
3094 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3095 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3097 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3098 struct glsl_src_param src0_param;
3099 struct glsl_src_param src1_param;
3100 DWORD dst_write_mask, src_write_mask;
3101 unsigned int dst_size = 0;
3103 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3104 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3106 /* dp4 works on vec4, dp3 on vec3, etc. */
3107 if (ins->handler_idx == WINED3DSIH_DP4)
3108 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3109 else if (ins->handler_idx == WINED3DSIH_DP3)
3110 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3111 else
3112 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3114 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3115 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3117 if (dst_size > 1) {
3118 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3119 } else {
3120 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3124 /* Note that this instruction has some restrictions. The destination write mask
3125 * can't contain the w component, and the source swizzles have to be .xyzw */
3126 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3128 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3129 struct glsl_src_param src0_param;
3130 struct glsl_src_param src1_param;
3131 char dst_mask[6];
3133 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3134 shader_glsl_append_dst(ins->ctx->buffer, ins);
3135 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3136 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3137 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3140 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3142 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3145 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3146 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3147 * GLSL uses the value as-is. */
3148 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3150 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3151 struct glsl_src_param src0_param;
3152 struct glsl_src_param src1_param;
3153 DWORD dst_write_mask;
3154 unsigned int dst_size;
3156 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3157 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3159 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3160 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3162 if (dst_size > 1)
3164 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3165 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3167 else
3169 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3170 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3174 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3175 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3177 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3178 struct glsl_src_param src_param;
3179 const char *instruction;
3180 DWORD write_mask;
3181 unsigned i;
3183 /* Determine the GLSL function to use based on the opcode */
3184 /* TODO: Possibly make this a table for faster lookups */
3185 switch (ins->handler_idx)
3187 case WINED3DSIH_MIN: instruction = "min"; break;
3188 case WINED3DSIH_MAX: instruction = "max"; break;
3189 case WINED3DSIH_ABS: instruction = "abs"; break;
3190 case WINED3DSIH_FRC: instruction = "fract"; break;
3191 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3192 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3193 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3194 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3195 default: instruction = "";
3196 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
3197 break;
3200 write_mask = shader_glsl_append_dst(buffer, ins);
3202 shader_addline(buffer, "%s(", instruction);
3204 if (ins->src_count)
3206 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3207 shader_addline(buffer, "%s", src_param.param_str);
3208 for (i = 1; i < ins->src_count; ++i)
3210 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3211 shader_addline(buffer, ", %s", src_param.param_str);
3215 shader_addline(buffer, "));\n");
3218 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3220 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3222 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3223 struct glsl_src_param src_param;
3224 unsigned int mask_size;
3225 DWORD write_mask;
3226 char dst_mask[6];
3228 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3229 mask_size = shader_glsl_get_write_mask_size(write_mask);
3230 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3232 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3233 src_param.param_str, src_param.param_str);
3234 shader_glsl_append_dst(buffer, ins);
3236 if (mask_size > 1)
3238 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3239 mask_size, src_param.param_str);
3241 else
3243 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3244 src_param.param_str);
3248 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3250 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3251 struct glsl_src_param src0_param;
3252 const char *prefix, *suffix;
3253 unsigned int dst_size;
3254 DWORD dst_write_mask;
3256 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3257 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3259 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src0_param);
3261 switch (ins->handler_idx)
3263 case WINED3DSIH_EXP:
3264 case WINED3DSIH_EXPP:
3265 prefix = "exp2(";
3266 suffix = ")";
3267 break;
3269 case WINED3DSIH_LOG:
3270 case WINED3DSIH_LOGP:
3271 prefix = "log2(abs(";
3272 suffix = "))";
3273 break;
3275 case WINED3DSIH_RCP:
3276 prefix = "1.0 / ";
3277 suffix = "";
3278 break;
3280 case WINED3DSIH_RSQ:
3281 prefix = "inversesqrt(abs(";
3282 suffix = "))";
3283 break;
3285 default:
3286 prefix = "";
3287 suffix = "";
3288 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3289 break;
3292 if (dst_size > 1)
3293 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3294 else
3295 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3298 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3299 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3300 * dst.x = 2^(floor(src))
3301 * dst.y = src - floor(src)
3302 * dst.z = 2^src (partial precision is allowed, but optional)
3303 * dst.w = 1.0;
3304 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3305 * dst = 2^src; (partial precision is allowed, but optional)
3307 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
3309 if (ins->ctx->reg_maps->shader_version.major < 2)
3311 struct glsl_src_param src_param;
3312 char dst_mask[6];
3314 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
3316 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
3317 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
3318 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
3319 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
3321 shader_glsl_append_dst(ins->ctx->buffer, ins);
3322 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3323 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
3324 return;
3327 shader_glsl_scalar_op(ins);
3330 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
3332 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3333 struct glsl_src_param src_param;
3334 unsigned int mask_size;
3335 DWORD write_mask;
3337 write_mask = shader_glsl_append_dst(buffer, ins);
3338 mask_size = shader_glsl_get_write_mask_size(write_mask);
3339 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3341 if (mask_size > 1)
3342 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
3343 else
3344 shader_addline(buffer, "int(%s));\n", src_param.param_str);
3347 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
3349 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3350 struct glsl_src_param src_param;
3351 unsigned int mask_size;
3352 DWORD write_mask;
3354 write_mask = shader_glsl_append_dst(buffer, ins);
3355 mask_size = shader_glsl_get_write_mask_size(write_mask);
3356 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3358 if (mask_size > 1)
3359 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
3360 else
3361 shader_addline(buffer, "float(%s));\n", src_param.param_str);
3364 /** Process signed comparison opcodes in GLSL. */
3365 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
3367 struct glsl_src_param src0_param;
3368 struct glsl_src_param src1_param;
3369 DWORD write_mask;
3370 unsigned int mask_size;
3372 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3373 mask_size = shader_glsl_get_write_mask_size(write_mask);
3374 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3375 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3377 if (mask_size > 1) {
3378 const char *compare;
3380 switch(ins->handler_idx)
3382 case WINED3DSIH_SLT: compare = "lessThan"; break;
3383 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
3384 default: compare = "";
3385 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
3388 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
3389 src0_param.param_str, src1_param.param_str);
3390 } else {
3391 switch(ins->handler_idx)
3393 case WINED3DSIH_SLT:
3394 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3395 * to return 0.0 but step returns 1.0 because step is not < x
3396 * An alternative is a bvec compare padded with an unused second component.
3397 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3398 * issue. Playing with not() is not possible either because not() does not accept
3399 * a scalar.
3401 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
3402 src0_param.param_str, src1_param.param_str);
3403 break;
3404 case WINED3DSIH_SGE:
3405 /* Here we can use the step() function and safe a conditional */
3406 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
3407 break;
3408 default:
3409 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
3415 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
3417 const char *condition_prefix, *condition_suffix;
3418 struct wined3d_shader_dst_param dst;
3419 struct glsl_src_param src0_param;
3420 struct glsl_src_param src1_param;
3421 struct glsl_src_param src2_param;
3422 BOOL temp_destination = FALSE;
3423 DWORD cmp_channel = 0;
3424 unsigned int i, j;
3425 char mask_char[6];
3426 DWORD write_mask;
3428 switch (ins->handler_idx)
3430 case WINED3DSIH_CMP:
3431 condition_prefix = "";
3432 condition_suffix = " >= 0.0";
3433 break;
3435 case WINED3DSIH_CND:
3436 condition_prefix = "";
3437 condition_suffix = " > 0.5";
3438 break;
3440 case WINED3DSIH_MOVC:
3441 condition_prefix = "bool(";
3442 condition_suffix = ")";
3443 break;
3445 default:
3446 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3447 condition_prefix = "<unhandled prefix>";
3448 condition_suffix = "<unhandled suffix>";
3449 break;
3452 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
3454 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3455 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3456 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3457 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3459 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3460 condition_prefix, src0_param.param_str, condition_suffix,
3461 src1_param.param_str, src2_param.param_str);
3462 return;
3465 dst = ins->dst[0];
3467 /* Splitting the instruction up in multiple lines imposes a problem:
3468 * The first lines may overwrite source parameters of the following lines.
3469 * Deal with that by using a temporary destination register if needed. */
3470 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
3471 && ins->src[0].reg.type == dst.reg.type)
3472 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
3473 && ins->src[1].reg.type == dst.reg.type)
3474 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
3475 && ins->src[2].reg.type == dst.reg.type))
3476 temp_destination = TRUE;
3478 /* Cycle through all source0 channels. */
3479 for (i = 0; i < 4; ++i)
3481 write_mask = 0;
3482 /* Find the destination channels which use the current source0 channel. */
3483 for (j = 0; j < 4; ++j)
3485 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
3487 write_mask |= WINED3DSP_WRITEMASK_0 << j;
3488 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
3491 dst.write_mask = ins->dst[0].write_mask & write_mask;
3493 if (temp_destination)
3495 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
3496 continue;
3497 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
3499 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
3500 continue;
3502 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
3503 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3504 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3506 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3507 condition_prefix, src0_param.param_str, condition_suffix,
3508 src1_param.param_str, src2_param.param_str);
3511 if (temp_destination)
3513 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3514 shader_glsl_append_dst(ins->ctx->buffer, ins);
3515 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3519 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3520 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3521 * the compare is done per component of src0. */
3522 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3524 struct glsl_src_param src0_param;
3525 struct glsl_src_param src1_param;
3526 struct glsl_src_param src2_param;
3527 DWORD write_mask;
3528 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3529 ins->ctx->reg_maps->shader_version.minor);
3531 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3533 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3534 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3535 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3536 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3538 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
3539 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3540 else
3541 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3542 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3543 return;
3546 shader_glsl_conditional_move(ins);
3549 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3550 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3552 struct glsl_src_param src0_param;
3553 struct glsl_src_param src1_param;
3554 struct glsl_src_param src2_param;
3555 DWORD write_mask;
3557 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3558 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3559 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3560 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3561 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3562 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3565 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3566 Vertex shaders to GLSL codes */
3567 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3569 int i;
3570 int nComponents = 0;
3571 struct wined3d_shader_dst_param tmp_dst = {{0}};
3572 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3573 struct wined3d_shader_instruction tmp_ins;
3575 memset(&tmp_ins, 0, sizeof(tmp_ins));
3577 /* Set constants for the temporary argument */
3578 tmp_ins.ctx = ins->ctx;
3579 tmp_ins.dst_count = 1;
3580 tmp_ins.dst = &tmp_dst;
3581 tmp_ins.src_count = 2;
3582 tmp_ins.src = tmp_src;
3584 switch(ins->handler_idx)
3586 case WINED3DSIH_M4x4:
3587 nComponents = 4;
3588 tmp_ins.handler_idx = WINED3DSIH_DP4;
3589 break;
3590 case WINED3DSIH_M4x3:
3591 nComponents = 3;
3592 tmp_ins.handler_idx = WINED3DSIH_DP4;
3593 break;
3594 case WINED3DSIH_M3x4:
3595 nComponents = 4;
3596 tmp_ins.handler_idx = WINED3DSIH_DP3;
3597 break;
3598 case WINED3DSIH_M3x3:
3599 nComponents = 3;
3600 tmp_ins.handler_idx = WINED3DSIH_DP3;
3601 break;
3602 case WINED3DSIH_M3x2:
3603 nComponents = 2;
3604 tmp_ins.handler_idx = WINED3DSIH_DP3;
3605 break;
3606 default:
3607 break;
3610 tmp_dst = ins->dst[0];
3611 tmp_src[0] = ins->src[0];
3612 tmp_src[1] = ins->src[1];
3613 for (i = 0; i < nComponents; ++i)
3615 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3616 shader_glsl_dot(&tmp_ins);
3617 ++tmp_src[1].reg.idx[0].offset;
3622 The LRP instruction performs a component-wise linear interpolation
3623 between the second and third operands using the first operand as the
3624 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3625 This is equivalent to mix(src2, src1, src0);
3627 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3629 struct glsl_src_param src0_param;
3630 struct glsl_src_param src1_param;
3631 struct glsl_src_param src2_param;
3632 DWORD write_mask;
3634 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3636 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3637 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3638 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3640 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3641 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3644 /** Process the WINED3DSIO_LIT instruction in GLSL:
3645 * dst.x = dst.w = 1.0
3646 * dst.y = (src0.x > 0) ? src0.x
3647 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3648 * where src.w is clamped at +- 128
3650 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3652 struct glsl_src_param src0_param;
3653 struct glsl_src_param src1_param;
3654 struct glsl_src_param src3_param;
3655 char dst_mask[6];
3657 shader_glsl_append_dst(ins->ctx->buffer, ins);
3658 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3660 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3661 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3662 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3664 /* The sdk specifies the instruction like this
3665 * dst.x = 1.0;
3666 * if(src.x > 0.0) dst.y = src.x
3667 * else dst.y = 0.0.
3668 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3669 * else dst.z = 0.0;
3670 * dst.w = 1.0;
3671 * (where power = src.w clamped between -128 and 128)
3673 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3674 * dst.x = 1.0 ... No further explanation needed
3675 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3676 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3677 * dst.w = 1.0. ... Nothing fancy.
3679 * So we still have one conditional in there. So do this:
3680 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3682 * 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),
3683 * 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.
3684 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3686 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3687 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3688 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3690 shader_addline(ins->ctx->buffer,
3691 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3692 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3693 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3694 src0_param.param_str, src3_param.param_str, dst_mask);
3697 /** Process the WINED3DSIO_DST instruction in GLSL:
3698 * dst.x = 1.0
3699 * dst.y = src0.x * src0.y
3700 * dst.z = src0.z
3701 * dst.w = src1.w
3703 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3705 struct glsl_src_param src0y_param;
3706 struct glsl_src_param src0z_param;
3707 struct glsl_src_param src1y_param;
3708 struct glsl_src_param src1w_param;
3709 char dst_mask[6];
3711 shader_glsl_append_dst(ins->ctx->buffer, ins);
3712 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3714 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3715 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3716 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3717 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3719 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3720 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3723 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3724 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3725 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3727 * dst.x = cos(src0.?)
3728 * dst.y = sin(src0.?)
3729 * dst.z = dst.z
3730 * dst.w = dst.w
3732 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3734 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3735 struct glsl_src_param src0_param;
3736 DWORD write_mask;
3738 if (ins->ctx->reg_maps->shader_version.major < 4)
3740 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3742 write_mask = shader_glsl_append_dst(buffer, ins);
3743 switch (write_mask)
3745 case WINED3DSP_WRITEMASK_0:
3746 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3747 break;
3749 case WINED3DSP_WRITEMASK_1:
3750 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3751 break;
3753 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3754 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3755 src0_param.param_str, src0_param.param_str);
3756 break;
3758 default:
3759 ERR("Write mask should be .x, .y or .xy\n");
3760 break;
3763 return;
3766 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3769 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3771 char dst_mask[6];
3773 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3774 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3775 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3777 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3778 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3779 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3781 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3782 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3784 else
3786 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3787 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3788 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3791 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3793 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3794 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3795 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3799 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3800 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3801 * generate invalid code
3803 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3805 struct glsl_src_param src0_param;
3806 DWORD write_mask;
3808 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3809 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3811 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3814 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3815 * Start a for() loop where src1.y is the initial value of aL,
3816 * increment aL by src1.z for a total of src1.x iterations.
3817 * Need to use a temporary variable for this operation.
3819 /* FIXME: I don't think nested loops will work correctly this way. */
3820 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3822 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3823 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3824 const struct wined3d_shader *shader = ins->ctx->shader;
3825 const struct wined3d_shader_lconst *constant;
3826 struct glsl_src_param src1_param;
3827 const DWORD *control_values = NULL;
3829 if (ins->ctx->reg_maps->shader_version.major < 4)
3831 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3833 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3834 * class hardware doesn't support real varying indexing, but Microsoft
3835 * designed this feature for Shader model 2.x+. If the loop control is
3836 * known at compile time, the GLSL compiler can unroll the loop, and
3837 * replace indirect addressing with direct addressing. */
3838 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3840 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3842 if (constant->idx == ins->src[1].reg.idx[0].offset)
3844 control_values = constant->value;
3845 break;
3850 if (control_values)
3852 struct wined3d_shader_loop_control loop_control;
3853 loop_control.count = control_values[0];
3854 loop_control.start = control_values[1];
3855 loop_control.step = (int)control_values[2];
3857 if (loop_control.step > 0)
3859 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3860 loop_state->current_depth, loop_control.start,
3861 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3862 loop_state->current_depth, loop_control.step);
3864 else if (loop_control.step < 0)
3866 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3867 loop_state->current_depth, loop_control.start,
3868 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3869 loop_state->current_depth, loop_control.step);
3871 else
3873 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3874 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3875 loop_state->current_depth, loop_control.count,
3876 loop_state->current_depth);
3879 else
3881 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3882 loop_state->current_depth, loop_state->current_reg,
3883 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3884 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3887 ++loop_state->current_reg;
3889 else
3891 shader_addline(buffer, "for (;;)\n{\n");
3894 ++loop_state->current_depth;
3897 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3899 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3901 shader_addline(ins->ctx->buffer, "}\n");
3903 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3905 --loop_state->current_depth;
3906 --loop_state->current_reg;
3909 if (ins->handler_idx == WINED3DSIH_ENDREP)
3911 --loop_state->current_depth;
3915 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3917 const struct wined3d_shader *shader = ins->ctx->shader;
3918 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3919 const struct wined3d_shader_lconst *constant;
3920 struct glsl_src_param src0_param;
3921 const DWORD *control_values = NULL;
3923 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3924 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3926 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3928 if (constant->idx == ins->src[0].reg.idx[0].offset)
3930 control_values = constant->value;
3931 break;
3936 if (control_values)
3938 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3939 loop_state->current_depth, loop_state->current_depth,
3940 control_values[0], loop_state->current_depth);
3942 else
3944 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3945 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3946 loop_state->current_depth, loop_state->current_depth,
3947 src0_param.param_str, loop_state->current_depth);
3950 ++loop_state->current_depth;
3953 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3955 struct glsl_src_param src0_param;
3957 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3958 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
3961 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3963 struct glsl_src_param src0_param;
3964 struct glsl_src_param src1_param;
3966 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3967 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3969 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3970 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3973 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3975 shader_addline(ins->ctx->buffer, "} else {\n");
3978 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3980 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3983 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3985 shader_addline(ins->ctx->buffer, "break;\n");
3988 /* FIXME: According to MSDN the compare is done per component. */
3989 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3991 struct glsl_src_param src0_param;
3992 struct glsl_src_param src1_param;
3994 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3995 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3997 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3998 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4001 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
4003 struct glsl_src_param src_param;
4005 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4006 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
4009 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
4011 shader_addline(ins->ctx->buffer, "}\n");
4012 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
4015 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
4017 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
4020 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
4022 struct glsl_src_param src1_param;
4024 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4025 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
4026 src1_param.param_str, ins->src[0].reg.idx[0].offset);
4029 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
4031 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
4032 * function only suppresses the unhandled instruction warning
4036 /*********************************************
4037 * Pixel Shader Specific Code begins here
4038 ********************************************/
4039 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
4041 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4042 ins->ctx->reg_maps->shader_version.minor);
4043 struct glsl_sample_function sample_function;
4044 DWORD sample_flags = 0;
4045 DWORD resource_idx;
4046 DWORD mask = 0, swizzle;
4047 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4049 /* 1.0-1.4: Use destination register as sampler source.
4050 * 2.0+: Use provided sampler source. */
4051 if (shader_version < WINED3D_SHADER_VERSION(2,0))
4052 resource_idx = ins->dst[0].reg.idx[0].offset;
4053 else
4054 resource_idx = ins->src[1].reg.idx[0].offset;
4056 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4058 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4059 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4060 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4062 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4063 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4065 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4066 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4068 case WINED3D_TTFF_COUNT1:
4069 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4070 break;
4071 case WINED3D_TTFF_COUNT2:
4072 mask = WINED3DSP_WRITEMASK_1;
4073 break;
4074 case WINED3D_TTFF_COUNT3:
4075 mask = WINED3DSP_WRITEMASK_2;
4076 break;
4077 case WINED3D_TTFF_COUNT4:
4078 case WINED3D_TTFF_DISABLE:
4079 mask = WINED3DSP_WRITEMASK_3;
4080 break;
4084 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4086 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4088 if (src_mod == WINED3DSPSM_DZ) {
4089 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4090 mask = WINED3DSP_WRITEMASK_2;
4091 } else if (src_mod == WINED3DSPSM_DW) {
4092 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4093 mask = WINED3DSP_WRITEMASK_3;
4096 else
4098 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4099 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4101 /* ps 2.0 texldp instruction always divides by the fourth component. */
4102 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4103 mask = WINED3DSP_WRITEMASK_3;
4107 if (priv->cur_ps_args->np2_fixup & (1u << resource_idx))
4108 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4110 shader_glsl_get_sample_function(ins->ctx, resource_idx, sample_flags, &sample_function);
4111 mask |= sample_function.coord_mask;
4112 sample_function.coord_mask = mask;
4114 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4115 else swizzle = ins->src[1].swizzle;
4117 /* 1.0-1.3: Use destination register as coordinate source.
4118 1.4+: Use provided coordinate source register. */
4119 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4121 char coord_mask[6];
4122 shader_glsl_write_mask_to_str(mask, coord_mask);
4123 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
4124 "T%u%s", resource_idx, coord_mask);
4126 else
4128 struct glsl_src_param coord_param;
4129 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4130 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4132 struct glsl_src_param bias;
4133 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4134 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4135 "%s", coord_param.param_str);
4136 } else {
4137 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
4138 "%s", coord_param.param_str);
4141 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4144 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4146 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4147 struct glsl_src_param coord_param, dx_param, dy_param;
4148 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
4149 struct glsl_sample_function sample_function;
4150 DWORD sampler_idx;
4151 DWORD swizzle = ins->src[1].swizzle;
4152 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4154 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
4156 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4157 shader_glsl_tex(ins);
4158 return;
4161 sampler_idx = ins->src[1].reg.idx[0].offset;
4162 if (priv->cur_ps_args->np2_fixup & (1u << sampler_idx))
4163 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4165 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
4166 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4167 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
4168 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
4170 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
4171 "%s", coord_param.param_str);
4172 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4175 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4177 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4178 struct glsl_src_param coord_param, lod_param;
4179 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
4180 struct glsl_sample_function sample_function;
4181 DWORD sampler_idx;
4182 DWORD swizzle = ins->src[1].swizzle;
4183 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4185 sampler_idx = ins->src[1].reg.idx[0].offset;
4186 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
4187 && priv->cur_ps_args->np2_fixup & (1u << sampler_idx))
4188 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4190 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
4191 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4193 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4195 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
4196 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
4198 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4199 * However, the NVIDIA drivers allow them in fragment shaders as well,
4200 * even without the appropriate extension. */
4201 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4203 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
4204 "%s", coord_param.param_str);
4205 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4208 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4209 unsigned int resource_idx, unsigned int sampler_idx)
4211 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4212 unsigned int i;
4214 for (i = 0; i < sampler_map->count; ++i)
4216 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4217 return entries[i].bind_idx;
4220 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4222 return ~0u;
4225 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
4227 struct glsl_sample_function sample_function;
4228 struct glsl_src_param coord_param;
4229 unsigned int sampler_idx;
4231 shader_glsl_get_sample_function(ins->ctx, ins->src[1].reg.idx[0].offset, 0, &sample_function);
4232 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4233 sampler_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4234 ins->src[1].reg.idx[0].offset, ins->src[2].reg.idx[0].offset);
4235 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE,
4236 NULL, NULL, NULL, "%s", coord_param.param_str);
4237 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4240 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
4242 /* FIXME: Make this work for more than just 2D textures */
4243 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4244 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4246 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
4248 char dst_mask[6];
4250 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4251 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
4252 ins->dst[0].reg.idx[0].offset, dst_mask);
4254 else
4256 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4257 DWORD reg = ins->src[0].reg.idx[0].offset;
4258 char dst_swizzle[6];
4260 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
4262 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
4264 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4265 struct glsl_src_param div_param;
4266 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
4268 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
4270 if (mask_size > 1)
4271 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4272 else
4273 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4275 else
4277 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
4282 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4283 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4284 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4285 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
4287 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4288 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4289 struct glsl_sample_function sample_function;
4290 struct glsl_src_param src0_param;
4291 UINT mask_size;
4293 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4295 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4296 * scalar, and projected sampling would require 4.
4298 * It is a dependent read - not valid with conditional NP2 textures
4300 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4301 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4303 switch(mask_size)
4305 case 1:
4306 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4307 "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
4308 break;
4310 case 2:
4311 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4312 "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
4313 break;
4315 case 3:
4316 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4317 "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
4318 break;
4320 default:
4321 FIXME("Unexpected mask size %u\n", mask_size);
4322 break;
4324 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4327 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4328 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4329 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
4331 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4332 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4333 struct glsl_src_param src0_param;
4334 DWORD dst_mask;
4335 unsigned int mask_size;
4337 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4338 mask_size = shader_glsl_get_write_mask_size(dst_mask);
4339 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4341 if (mask_size > 1) {
4342 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
4343 } else {
4344 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
4348 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4349 * Calculate the depth as dst.x / dst.y */
4350 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
4352 struct glsl_dst_param dst_param;
4354 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4356 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4357 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4358 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4359 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4360 * >= 1.0 or < 0.0
4362 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4363 dst_param.reg_name, dst_param.reg_name);
4366 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4367 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4368 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4369 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4371 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
4373 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4374 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4375 struct glsl_src_param src0_param;
4377 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4379 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
4380 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4383 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4384 * Calculate the 1st of a 2-row matrix multiplication. */
4385 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
4387 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4388 DWORD reg = ins->dst[0].reg.idx[0].offset;
4389 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4390 struct glsl_src_param src0_param;
4392 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4393 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4396 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4397 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4398 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
4400 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4401 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4402 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4403 DWORD reg = ins->dst[0].reg.idx[0].offset;
4404 struct glsl_src_param src0_param;
4406 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4407 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
4408 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
4411 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
4413 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4414 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4415 struct glsl_sample_function sample_function;
4416 DWORD reg = ins->dst[0].reg.idx[0].offset;
4417 struct glsl_src_param src0_param;
4419 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4420 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4422 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4424 /* Sample the texture using the calculated coordinates */
4425 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
4426 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4429 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
4430 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
4431 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
4433 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4434 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4435 struct glsl_sample_function sample_function;
4436 DWORD reg = ins->dst[0].reg.idx[0].offset;
4437 struct glsl_src_param src0_param;
4439 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4440 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4442 /* Dependent read, not valid with conditional NP2 */
4443 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4445 /* Sample the texture using the calculated coordinates */
4446 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
4447 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4449 tex_mx->current_row = 0;
4452 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4453 * Perform the 3rd row of a 3x3 matrix multiply */
4454 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
4456 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4457 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4458 DWORD reg = ins->dst[0].reg.idx[0].offset;
4459 struct glsl_src_param src0_param;
4460 char dst_mask[6];
4462 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4464 shader_glsl_append_dst(ins->ctx->buffer, ins);
4465 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4466 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
4468 tex_mx->current_row = 0;
4471 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4472 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4473 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
4475 struct glsl_src_param src0_param;
4476 struct glsl_src_param src1_param;
4477 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4478 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4479 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4480 struct glsl_sample_function sample_function;
4481 DWORD reg = ins->dst[0].reg.idx[0].offset;
4482 char coord_mask[6];
4484 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4485 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4487 /* Perform the last matrix multiply operation */
4488 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4489 /* Reflection calculation */
4490 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
4492 /* Dependent read, not valid with conditional NP2 */
4493 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4494 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4496 /* Sample the texture */
4497 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4498 NULL, NULL, NULL, "tmp0%s", coord_mask);
4499 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4501 tex_mx->current_row = 0;
4504 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4505 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4506 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
4508 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4509 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4510 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4511 struct glsl_sample_function sample_function;
4512 DWORD reg = ins->dst[0].reg.idx[0].offset;
4513 struct glsl_src_param src0_param;
4514 char coord_mask[6];
4516 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4518 /* Perform the last matrix multiply operation */
4519 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
4521 /* Construct the eye-ray vector from w coordinates */
4522 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
4523 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
4524 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4526 /* Dependent read, not valid with conditional NP2 */
4527 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4528 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4530 /* Sample the texture using the calculated coordinates */
4531 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4532 NULL, NULL, NULL, "tmp0%s", coord_mask);
4533 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4535 tex_mx->current_row = 0;
4538 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4539 * Apply a fake bump map transform.
4540 * texbem is pshader <= 1.3 only, this saves a few version checks
4542 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
4544 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4545 struct glsl_sample_function sample_function;
4546 struct glsl_src_param coord_param;
4547 DWORD sampler_idx;
4548 DWORD mask;
4549 DWORD flags;
4550 char coord_mask[6];
4552 sampler_idx = ins->dst[0].reg.idx[0].offset;
4553 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4554 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4556 /* Dependent read, not valid with conditional NP2 */
4557 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4558 mask = sample_function.coord_mask;
4560 shader_glsl_write_mask_to_str(mask, coord_mask);
4562 /* With projected textures, texbem only divides the static texture coord,
4563 * not the displacement, so we can't let GL handle this. */
4564 if (flags & WINED3D_PSARGS_PROJECTED)
4566 DWORD div_mask=0;
4567 char coord_div_mask[3];
4568 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4570 case WINED3D_TTFF_COUNT1:
4571 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4572 break;
4573 case WINED3D_TTFF_COUNT2:
4574 div_mask = WINED3DSP_WRITEMASK_1;
4575 break;
4576 case WINED3D_TTFF_COUNT3:
4577 div_mask = WINED3DSP_WRITEMASK_2;
4578 break;
4579 case WINED3D_TTFF_COUNT4:
4580 case WINED3D_TTFF_DISABLE:
4581 div_mask = WINED3DSP_WRITEMASK_3;
4582 break;
4584 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4585 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4588 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4590 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4591 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4592 coord_param.param_str, coord_mask);
4594 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4596 struct glsl_src_param luminance_param;
4597 struct glsl_dst_param dst_param;
4599 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4600 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4602 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4603 dst_param.reg_name, dst_param.mask_str,
4604 luminance_param.param_str, sampler_idx, sampler_idx);
4606 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4609 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4611 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4612 struct glsl_src_param src0_param, src1_param;
4614 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4615 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4617 shader_glsl_append_dst(ins->ctx->buffer, ins);
4618 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4619 src0_param.param_str, sampler_idx, src1_param.param_str);
4622 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4623 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4624 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4626 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4627 struct glsl_sample_function sample_function;
4628 struct glsl_src_param src0_param;
4630 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4632 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4633 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4634 "%s.wx", src0_param.reg_name);
4635 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4638 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4639 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4640 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4642 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4643 struct glsl_sample_function sample_function;
4644 struct glsl_src_param src0_param;
4646 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4648 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4649 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4650 "%s.yz", src0_param.reg_name);
4651 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4654 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4655 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4656 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4658 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4659 struct glsl_sample_function sample_function;
4660 struct glsl_src_param src0_param;
4662 /* Dependent read, not valid with conditional NP2 */
4663 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4664 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4666 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4667 "%s", src0_param.param_str);
4668 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4671 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4672 * If any of the first 3 components are < 0, discard this pixel */
4673 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4675 if (ins->ctx->reg_maps->shader_version.major >= 4)
4677 struct glsl_src_param src_param;
4679 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4680 shader_addline(ins->ctx->buffer, "if (bool(floatBitsToUint(%s))) discard;\n", src_param.param_str);
4682 else
4684 struct glsl_dst_param dst_param;
4686 /* The argument is a destination parameter, and no writemasks are allowed */
4687 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4689 /* 2.0 shaders compare all 4 components in texkill. */
4690 if (ins->ctx->reg_maps->shader_version.major >= 2)
4691 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4692 /* 1.x shaders only compare the first 3 components, probably due to
4693 * the nature of the texkill instruction as a tex* instruction, and
4694 * phase, which kills all .w components. Even if all 4 components are
4695 * defined, only the first 3 are used. */
4696 else
4697 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4701 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4702 * dst = dot2(src0, src1) + src2 */
4703 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4705 struct glsl_src_param src0_param;
4706 struct glsl_src_param src1_param;
4707 struct glsl_src_param src2_param;
4708 DWORD write_mask;
4709 unsigned int mask_size;
4711 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4712 mask_size = shader_glsl_get_write_mask_size(write_mask);
4714 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4715 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4716 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4718 if (mask_size > 1) {
4719 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4720 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4721 } else {
4722 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4723 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4727 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
4728 const struct wined3d_shader_signature *input_signature,
4729 const struct wined3d_shader_reg_maps *reg_maps,
4730 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
4732 unsigned int i;
4734 for (i = 0; i < input_signature->element_count; ++i)
4736 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4737 const char *semantic_name;
4738 UINT semantic_idx;
4739 char reg_mask[6];
4741 /* Unused */
4742 if (!(reg_maps->input_registers & (1u << input->register_idx)))
4743 continue;
4745 semantic_name = input->semantic_name;
4746 semantic_idx = input->semantic_idx;
4747 shader_glsl_write_mask_to_str(input->mask, reg_mask);
4749 if (args->vp_mode == vertexshader)
4751 if (input->sysval_semantic == WINED3D_SV_POSITION)
4752 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
4753 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4754 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4755 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
4756 else
4757 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
4758 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
4759 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
4761 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4763 if (args->pointsprite)
4764 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
4765 shader->u.ps.input_reg_map[input->register_idx]);
4766 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
4767 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
4768 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
4769 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
4770 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
4771 else
4772 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4773 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4775 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4777 if (!semantic_idx)
4778 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
4779 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4780 else if (semantic_idx == 1)
4781 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
4782 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4783 else
4784 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4785 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4787 else
4789 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4790 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4795 /*********************************************
4796 * Vertex Shader Specific Code begins here
4797 ********************************************/
4799 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4801 struct glsl_program_key key;
4803 key.vs_id = entry->vs.id;
4804 key.gs_id = entry->gs.id;
4805 key.ps_id = entry->ps.id;
4807 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4809 ERR("Failed to insert program entry.\n");
4813 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4814 GLuint vs_id, GLuint gs_id, GLuint ps_id)
4816 struct wine_rb_entry *entry;
4817 struct glsl_program_key key;
4819 key.vs_id = vs_id;
4820 key.gs_id = gs_id;
4821 key.ps_id = ps_id;
4823 entry = wine_rb_get(&priv->program_lookup, &key);
4824 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4827 /* Context activation is done by the caller. */
4828 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4829 struct glsl_shader_prog_link *entry)
4831 struct glsl_program_key key;
4833 key.vs_id = entry->vs.id;
4834 key.gs_id = entry->gs.id;
4835 key.ps_id = entry->ps.id;
4836 wine_rb_remove(&priv->program_lookup, &key);
4838 GL_EXTCALL(glDeleteProgram(entry->id));
4839 if (entry->vs.id)
4840 list_remove(&entry->vs.shader_entry);
4841 if (entry->gs.id)
4842 list_remove(&entry->gs.shader_entry);
4843 if (entry->ps.id)
4844 list_remove(&entry->ps.shader_entry);
4845 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4846 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4847 HeapFree(GetProcessHeap(), 0, entry);
4850 static void handle_ps3_input(struct shader_glsl_priv *priv,
4851 const struct wined3d_gl_info *gl_info, const DWORD *map,
4852 const struct wined3d_shader_signature *input_signature,
4853 const struct wined3d_shader_reg_maps *reg_maps_in,
4854 const struct wined3d_shader_signature *output_signature,
4855 const struct wined3d_shader_reg_maps *reg_maps_out)
4857 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
4858 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
4859 unsigned int i, j;
4860 DWORD *set;
4861 DWORD in_idx;
4862 unsigned int in_count = vec4_varyings(3, gl_info);
4863 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
4864 char reg_mask[6];
4865 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
4867 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * max_varyings);
4869 for (i = 0; i < input_signature->element_count; ++i)
4871 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4873 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
4874 continue;
4876 in_idx = map[input->register_idx];
4877 /* Declared, but not read register */
4878 if (in_idx == ~0u)
4879 continue;
4880 if (in_idx >= max_varyings)
4882 FIXME("More input varyings declared than supported, expect issues.\n");
4883 continue;
4886 if (in_idx == in_count)
4887 string_buffer_sprintf(destination, "gl_FrontColor");
4888 else if (in_idx == in_count + 1)
4889 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
4890 else
4891 string_buffer_sprintf(destination, "ps_link[%u]", in_idx);
4893 if (!set[in_idx])
4894 set[in_idx] = ~0u;
4896 for (j = 0; j < output_signature->element_count; ++j)
4898 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
4899 DWORD mask;
4901 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
4902 || input->semantic_idx != output->semantic_idx
4903 || strcmp(input->semantic_name, output->semantic_name)
4904 || !(mask = input->mask & output->mask))
4905 continue;
4907 if (set[in_idx] == ~0u)
4908 set[in_idx] = 0;
4909 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
4910 shader_glsl_write_mask_to_str(mask, reg_mask);
4912 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4913 destination->buffer, reg_mask, output->register_idx, reg_mask);
4917 for (i = 0; i < max_varyings; ++i)
4919 unsigned int size;
4921 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4922 continue;
4924 if (set[i] == ~0U) set[i] = 0;
4926 size = 0;
4927 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4928 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4929 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4930 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4931 reg_mask[size] = '\0';
4933 if (i == in_count)
4934 string_buffer_sprintf(destination, "gl_FrontColor");
4935 else if (i == in_count + 1)
4936 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
4937 else
4938 string_buffer_sprintf(destination, "ps_link[%u]", i);
4940 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
4941 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
4944 HeapFree(GetProcessHeap(), 0, set);
4945 string_buffer_release(&priv->string_buffers, destination);
4948 /* Context activation is done by the caller. */
4949 static GLuint generate_param_reorder_function(struct shader_glsl_priv *priv,
4950 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4951 BOOL per_vertex_point_size, const struct wined3d_gl_info *gl_info)
4953 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
4954 GLuint ret = 0;
4955 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4956 unsigned int i;
4957 const char *semantic_name;
4958 UINT semantic_idx;
4959 char reg_mask[6];
4960 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
4962 string_buffer_clear(buffer);
4964 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &vs->reg_maps.shader_version));
4966 if (per_vertex_point_size)
4968 shader_addline(buffer, "uniform struct\n{\n");
4969 shader_addline(buffer, " float size_min;\n");
4970 shader_addline(buffer, " float size_max;\n");
4971 shader_addline(buffer, "} ffp_point;\n");
4974 if (ps_major < 3)
4976 DWORD colors_written_mask[2] = {0};
4977 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
4979 if (!legacy_context)
4981 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_diffuse;\n");
4982 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_specular;\n");
4983 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
4984 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
4987 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4989 for (i = 0; i < vs->output_signature.element_count; ++i)
4991 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
4992 DWORD write_mask;
4994 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
4995 continue;
4997 semantic_name = output->semantic_name;
4998 semantic_idx = output->semantic_idx;
4999 write_mask = output->mask;
5000 shader_glsl_write_mask_to_str(write_mask, reg_mask);
5002 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
5004 if (legacy_context)
5005 shader_addline(buffer, "gl_Front%sColor%s = vs_out[%u]%s;\n",
5006 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
5007 else
5008 shader_addline(buffer, "ffp_varying_%s%s = clamp(vs_out[%u]%s, 0.0, 1.0);\n",
5009 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
5011 colors_written_mask[semantic_idx] = write_mask;
5013 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5015 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
5016 reg_mask, output->register_idx, reg_mask);
5018 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5020 if (semantic_idx < MAX_TEXTURES)
5022 shader_addline(buffer, "%s[%u]%s = vs_out[%u]%s;\n",
5023 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
5024 semantic_idx, reg_mask, output->register_idx, reg_mask);
5025 texcoords_written_mask[semantic_idx] = write_mask;
5028 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5030 shader_addline(buffer, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
5031 output->register_idx, reg_mask[1]);
5033 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
5035 shader_addline(buffer, "%s = clamp(vs_out[%u].%c, 0.0, 1.0);\n",
5036 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
5037 output->register_idx, reg_mask[1]);
5041 for (i = 0; i < 2; ++i)
5043 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5045 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5046 if (!i)
5047 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
5048 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
5049 reg_mask, reg_mask);
5050 else
5051 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
5052 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
5053 reg_mask, reg_mask);
5056 for (i = 0; i < MAX_TEXTURES; ++i)
5058 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
5059 continue;
5061 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5063 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
5064 && !texcoords_written_mask[i])
5065 continue;
5067 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5068 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
5069 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
5073 else
5075 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
5077 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
5078 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
5080 /* First, sort out position and point size. Those are not passed to the pixel shader */
5081 for (i = 0; i < vs->output_signature.element_count; ++i)
5083 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5085 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5086 continue;
5088 semantic_name = output->semantic_name;
5089 semantic_idx = output->semantic_idx;
5090 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5092 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5094 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
5095 reg_mask, output->register_idx, reg_mask);
5097 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5099 shader_addline(buffer, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
5100 output->register_idx, reg_mask[1]);
5104 /* Then, fix the pixel shader input */
5105 handle_ps3_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
5106 &ps->reg_maps, &vs->output_signature, &vs->reg_maps);
5109 shader_addline(buffer, "}\n");
5111 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5112 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
5113 shader_glsl_compile(gl_info, ret, buffer->buffer);
5115 return ret;
5118 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer)
5120 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
5121 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
5122 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
5123 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
5124 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
5125 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
5128 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
5130 switch (mode)
5132 case WINED3D_FFP_PS_FOG_OFF:
5133 return;
5135 case WINED3D_FFP_PS_FOG_LINEAR:
5136 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
5137 break;
5139 case WINED3D_FFP_PS_FOG_EXP:
5140 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
5141 break;
5143 case WINED3D_FFP_PS_FOG_EXP2:
5144 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
5145 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
5146 break;
5148 default:
5149 ERR("Invalid fog mode %#x.\n", mode);
5150 return;
5153 shader_addline(buffer, "gl_FragData[0].xyz = mix(ffp_fog.color.xyz, gl_FragData[0].xyz,"
5154 " clamp(fog, 0.0, 1.0));\n");
5157 /* Context activation is done by the caller. */
5158 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
5159 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5160 const struct wined3d_shader *shader,
5161 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
5163 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5164 const struct wined3d_gl_info *gl_info = context->gl_info;
5165 const DWORD *function = shader->function;
5166 struct shader_glsl_ctx_priv priv_ctx;
5167 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5169 /* Create the hw GLSL shader object and assign it as the shader->prgId */
5170 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5172 memset(&priv_ctx, 0, sizeof(priv_ctx));
5173 priv_ctx.cur_ps_args = args;
5174 priv_ctx.cur_np2fixup_info = np2fixup_info;
5175 priv_ctx.string_buffers = string_buffers;
5177 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5179 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5180 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5181 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5182 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
5183 /* The spec says that it doesn't have to be explicitly enabled, but the
5184 * nvidia drivers write a warning if we don't do so. */
5185 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5186 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : 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 /* Base Declarations */
5193 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5195 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
5197 unsigned int i;
5198 WORD map = reg_maps->texcoord;
5200 if (legacy_context)
5202 if (glsl_is_color_reg_read(shader, 0))
5203 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
5204 if (glsl_is_color_reg_read(shader, 1))
5205 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
5208 for (i = 0; map; map >>= 1, ++i)
5210 if (map & 1)
5212 if (args->pointsprite)
5213 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
5214 else if (args->texcoords_initialized & (1u << i))
5215 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
5216 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
5217 else
5218 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
5219 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
5223 if (legacy_context)
5224 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
5227 /* Pack 3.0 inputs */
5228 if (reg_maps->shader_version.major >= 3)
5229 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
5231 /* Base Shader Body */
5232 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5234 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
5235 if (reg_maps->shader_version.major < 2)
5237 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
5238 shader_addline(buffer, "gl_FragData[0] = R0;\n");
5241 if (args->srgb_correction)
5242 shader_glsl_generate_srgb_write_correction(buffer);
5244 /* SM < 3 does not replace the fog stage. */
5245 if (reg_maps->shader_version.major < 3)
5246 shader_glsl_generate_fog_code(buffer, args->fog);
5248 shader_addline(buffer, "}\n");
5250 TRACE("Compiling shader object %u.\n", shader_id);
5251 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5253 return shader_id;
5256 /* Context activation is done by the caller. */
5257 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
5258 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5259 const struct wined3d_shader *shader,
5260 const struct vs_compile_args *args)
5262 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5263 const struct wined3d_gl_info *gl_info = context->gl_info;
5264 const DWORD *function = shader->function;
5265 struct shader_glsl_ctx_priv priv_ctx;
5266 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5268 /* Create the hw GLSL shader program and assign it as the shader->prgId */
5269 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5271 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5273 if (gl_info->supported[ARB_DRAW_INSTANCED])
5274 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
5275 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5276 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5277 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5278 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5279 if (gl_info->supported[EXT_GPU_SHADER4])
5280 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5282 memset(&priv_ctx, 0, sizeof(priv_ctx));
5283 priv_ctx.cur_vs_args = args;
5284 priv_ctx.string_buffers = string_buffers;
5286 /* Base Declarations */
5287 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5289 /* Base Shader Body */
5290 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5292 /* Unpack outputs */
5293 shader_addline(buffer, "order_ps_input(vs_out);\n");
5295 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
5296 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
5297 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
5298 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
5300 if (reg_maps->shader_version.major < 3)
5302 if (args->fog_src == VS_FOG_Z)
5303 shader_addline(buffer, "%s = gl_Position.z;\n",
5304 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5305 else if (!reg_maps->fog)
5306 shader_addline(buffer, "%s = 0.0;\n",
5307 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5310 /* We always store the clipplanes without y inversion */
5311 if (args->clip_enabled)
5312 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
5314 if (args->point_size && !args->per_vertex_point_size)
5315 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
5317 /* Write the final position.
5319 * OpenGL coordinates specify the center of the pixel while d3d coords specify
5320 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
5321 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
5322 * contains 1.0 to allow a mad.
5324 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
5325 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
5327 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
5329 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
5330 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
5331 * which is the same as z = z * 2 - w.
5333 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
5335 shader_addline(buffer, "}\n");
5337 TRACE("Compiling shader object %u.\n", shader_id);
5338 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5340 return shader_id;
5343 /* Context activation is done by the caller. */
5344 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
5345 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5346 const struct wined3d_shader *shader)
5348 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5349 const struct wined3d_gl_info *gl_info = context->gl_info;
5350 const DWORD *function = shader->function;
5351 struct shader_glsl_ctx_priv priv_ctx;
5352 GLuint shader_id;
5354 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
5356 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5358 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
5359 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
5360 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5361 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5362 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5363 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5364 if (gl_info->supported[EXT_GPU_SHADER4])
5365 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5367 memset(&priv_ctx, 0, sizeof(priv_ctx));
5368 priv_ctx.string_buffers = string_buffers;
5369 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5370 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5371 shader_addline(buffer, "}\n");
5373 TRACE("Compiling shader object %u.\n", shader_id);
5374 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5376 return shader_id;
5379 static GLuint find_glsl_pshader(const struct wined3d_context *context,
5380 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5381 struct wined3d_shader *shader,
5382 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
5384 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
5385 struct glsl_shader_private *shader_data;
5386 struct ps_np2fixup_info *np2fixup;
5387 UINT i;
5388 DWORD new_size;
5389 GLuint ret;
5391 if (!shader->backend_data)
5393 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5394 if (!shader->backend_data)
5396 ERR("Failed to allocate backend data.\n");
5397 return 0;
5400 shader_data = shader->backend_data;
5401 gl_shaders = shader_data->gl_shaders.ps;
5403 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5404 * so a linear search is more performant than a hashmap or a binary search
5405 * (cache coherency etc)
5407 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5409 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
5411 if (args->np2_fixup)
5412 *np2fixup_info = &gl_shaders[i].np2fixup;
5413 return gl_shaders[i].id;
5417 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5418 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5419 if (shader_data->num_gl_shaders)
5421 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5422 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
5423 new_size * sizeof(*gl_shaders));
5425 else
5427 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5428 new_size = 1;
5431 if(!new_array) {
5432 ERR("Out of memory\n");
5433 return 0;
5435 shader_data->gl_shaders.ps = new_array;
5436 shader_data->shader_array_size = new_size;
5437 gl_shaders = new_array;
5440 gl_shaders[shader_data->num_gl_shaders].args = *args;
5442 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
5443 memset(np2fixup, 0, sizeof(*np2fixup));
5444 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
5446 pixelshader_update_resource_types(shader, args->tex_types);
5448 string_buffer_clear(buffer);
5449 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
5450 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5452 return ret;
5455 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
5456 const DWORD use_map)
5458 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
5459 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
5460 if (stored->point_size != new->point_size)
5461 return FALSE;
5462 if (stored->per_vertex_point_size != new->per_vertex_point_size)
5463 return FALSE;
5464 return stored->fog_src == new->fog_src;
5467 static GLuint find_glsl_vshader(const struct wined3d_context *context,
5468 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5469 struct wined3d_shader *shader,
5470 const struct vs_compile_args *args)
5472 UINT i;
5473 DWORD new_size;
5474 DWORD use_map = context->stream_info.use_map;
5475 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
5476 struct glsl_shader_private *shader_data;
5477 GLuint ret;
5479 if (!shader->backend_data)
5481 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5482 if (!shader->backend_data)
5484 ERR("Failed to allocate backend data.\n");
5485 return 0;
5488 shader_data = shader->backend_data;
5489 gl_shaders = shader_data->gl_shaders.vs;
5491 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5492 * so a linear search is more performant than a hashmap or a binary search
5493 * (cache coherency etc)
5495 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5497 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
5498 return gl_shaders[i].id;
5501 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5503 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5504 if (shader_data->num_gl_shaders)
5506 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5507 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
5508 new_size * sizeof(*gl_shaders));
5510 else
5512 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5513 new_size = 1;
5516 if(!new_array) {
5517 ERR("Out of memory\n");
5518 return 0;
5520 shader_data->gl_shaders.vs = new_array;
5521 shader_data->shader_array_size = new_size;
5522 gl_shaders = new_array;
5525 gl_shaders[shader_data->num_gl_shaders].args = *args;
5527 string_buffer_clear(buffer);
5528 ret = shader_glsl_generate_vshader(context, buffer, string_buffers, shader, args);
5529 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5531 return ret;
5534 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
5535 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5536 struct wined3d_shader *shader)
5538 struct glsl_gs_compiled_shader *gl_shaders;
5539 struct glsl_shader_private *shader_data;
5540 GLuint ret;
5542 if (!shader->backend_data)
5544 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
5546 ERR("Failed to allocate backend data.\n");
5547 return 0;
5550 shader_data = shader->backend_data;
5551 gl_shaders = shader_data->gl_shaders.gs;
5553 if (shader_data->num_gl_shaders)
5554 return gl_shaders[0].id;
5556 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5558 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
5560 ERR("Failed to allocate GL shader array.\n");
5561 return 0;
5563 shader_data->shader_array_size = 1;
5564 gl_shaders = shader_data->gl_shaders.gs;
5566 string_buffer_clear(buffer);
5567 ret = shader_glsl_generate_geometry_shader(context, buffer, string_buffers, shader);
5568 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5570 return ret;
5573 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
5575 switch (mcs)
5577 case WINED3D_MCS_MATERIAL:
5578 return material;
5579 case WINED3D_MCS_COLOR1:
5580 return "ffp_attrib_diffuse";
5581 case WINED3D_MCS_COLOR2:
5582 return "ffp_attrib_specular";
5583 default:
5584 ERR("Invalid material color source %#x.\n", mcs);
5585 return "<invalid>";
5589 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
5590 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
5592 const char *diffuse, *specular, *emissive, *ambient;
5593 enum wined3d_light_type light_type;
5594 unsigned int i;
5596 if (!settings->lighting)
5598 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
5599 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
5600 return;
5603 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
5604 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
5605 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
5606 shader_addline(buffer, "vec3 dir, dst;\n");
5607 shader_addline(buffer, "float att, t;\n");
5609 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
5610 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
5611 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
5612 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
5614 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
5616 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
5617 switch (light_type)
5619 case WINED3D_LIGHT_POINT:
5620 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5621 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5622 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5623 shader_addline(buffer, "dst.x = 1.0;\n");
5624 if (legacy_lighting)
5626 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
5627 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
5629 else
5631 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5633 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5634 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", i, i, i);
5635 if (!legacy_lighting)
5636 shader_addline(buffer, "att = 1.0 / att;\n");
5637 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5638 if (!settings->normal)
5640 if (!legacy_lighting)
5641 shader_addline(buffer, "}\n");
5642 break;
5644 shader_addline(buffer, "dir = normalize(dir);\n");
5645 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5646 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
5647 if (settings->localviewer)
5648 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5649 else
5650 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5651 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5652 " * ffp_light[%u].specular) * att;\n", i);
5653 if (!legacy_lighting)
5654 shader_addline(buffer, "}\n");
5655 break;
5657 case WINED3D_LIGHT_SPOT:
5658 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5659 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5660 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5661 shader_addline(buffer, "dst.x = 1.0;\n");
5662 if (legacy_lighting)
5664 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
5665 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
5667 else
5669 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5671 shader_addline(buffer, "dir = normalize(dir);\n");
5672 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", i);
5673 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", i);
5674 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", i);
5675 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
5676 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
5677 i, i, i, i);
5678 if (legacy_lighting)
5679 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5680 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5681 i, i, i);
5682 else
5683 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5684 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5685 i, i, i);
5686 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5687 if (!settings->normal)
5689 if (!legacy_lighting)
5690 shader_addline(buffer, "}\n");
5691 break;
5693 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5694 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
5695 if (settings->localviewer)
5696 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5697 else
5698 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5699 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5700 " * ffp_light[%u].specular) * att;\n", i);
5701 if (!legacy_lighting)
5702 shader_addline(buffer, "}\n");
5703 break;
5705 case WINED3D_LIGHT_DIRECTIONAL:
5706 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
5707 if (!settings->normal)
5708 break;
5709 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", i);
5710 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5711 " * ffp_light[%u].diffuse.xyz;\n", i);
5712 /* TODO: In the non-local viewer case the halfvector is constant
5713 * and could be precomputed and stored in a uniform. */
5714 if (settings->localviewer)
5715 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5716 else
5717 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5718 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
5719 " * ffp_light[%u].specular;\n", i);
5720 break;
5722 case WINED3D_LIGHT_PARALLELPOINT:
5723 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
5724 if (!settings->normal)
5725 break;
5726 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", i);
5727 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5728 " * ffp_light[%u].diffuse.xyz;\n", i);
5729 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5730 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
5731 " * ffp_light[%u].specular;\n", i);
5732 break;
5734 default:
5735 if (light_type)
5736 FIXME("Unhandled light type %#x.\n", light_type);
5737 continue;
5741 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
5742 ambient, diffuse, emissive);
5743 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
5744 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
5747 /* Context activation is done by the caller. */
5748 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
5749 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
5751 static const struct attrib_info
5753 const char type[6];
5754 const char name[24];
5756 attrib_info[] =
5758 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
5759 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
5760 /* TODO: Indexed vertex blending */
5761 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
5762 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
5763 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
5764 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
5765 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
5767 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5768 BOOL legacy_lighting = priv->legacy_lighting;
5769 GLuint shader_obj;
5770 unsigned int i;
5771 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5772 BOOL output_legacy_fogcoord = legacy_context;
5774 string_buffer_clear(buffer);
5776 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
5778 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
5780 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
5782 shader_addline(buffer, "attribute %s vs_in%u;\n", type, i);
5784 shader_addline(buffer, "\n");
5786 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
5787 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
5788 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
5789 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
5791 shader_addline(buffer, "uniform struct\n{\n");
5792 shader_addline(buffer, " vec4 emissive;\n");
5793 shader_addline(buffer, " vec4 ambient;\n");
5794 shader_addline(buffer, " vec4 diffuse;\n");
5795 shader_addline(buffer, " vec4 specular;\n");
5796 shader_addline(buffer, " float shininess;\n");
5797 shader_addline(buffer, "} ffp_material;\n");
5799 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
5800 shader_addline(buffer, "uniform struct\n{\n");
5801 shader_addline(buffer, " vec4 diffuse;\n");
5802 shader_addline(buffer, " vec4 specular;\n");
5803 shader_addline(buffer, " vec4 ambient;\n");
5804 shader_addline(buffer, " vec4 position;\n");
5805 shader_addline(buffer, " vec3 direction;\n");
5806 shader_addline(buffer, " float range;\n");
5807 shader_addline(buffer, " float falloff;\n");
5808 shader_addline(buffer, " float c_att;\n");
5809 shader_addline(buffer, " float l_att;\n");
5810 shader_addline(buffer, " float q_att;\n");
5811 shader_addline(buffer, " float cos_htheta;\n");
5812 shader_addline(buffer, " float cos_hphi;\n");
5813 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
5815 if (settings->point_size)
5817 shader_addline(buffer, "uniform struct\n{\n");
5818 shader_addline(buffer, " float size;\n");
5819 shader_addline(buffer, " float size_min;\n");
5820 shader_addline(buffer, " float size_max;\n");
5821 shader_addline(buffer, " float c_att;\n");
5822 shader_addline(buffer, " float l_att;\n");
5823 shader_addline(buffer, " float q_att;\n");
5824 shader_addline(buffer, "} ffp_point;\n");
5827 if (legacy_context)
5829 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
5830 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
5831 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
5832 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
5834 else
5836 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_diffuse;\n");
5837 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_specular;\n");
5838 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
5839 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
5842 shader_addline(buffer, "\nvoid main()\n{\n");
5843 shader_addline(buffer, "float m;\n");
5844 shader_addline(buffer, "vec3 r;\n");
5846 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
5848 if (attrib_info[i].name[0])
5849 shader_addline(buffer, "%s %s = vs_in%u;\n",
5850 attrib_info[i].type, attrib_info[i].name, i);
5852 for (i = 0; i < MAX_TEXTURES; ++i)
5854 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
5855 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU)
5857 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
5861 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
5863 if (settings->transformed)
5865 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
5866 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5867 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
5869 else
5871 for (i = 0; i < settings->vertexblends; ++i)
5872 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
5874 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
5875 for (i = 0; i < settings->vertexblends + 1; ++i)
5876 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
5878 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5879 if (settings->clipping)
5880 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
5881 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
5884 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
5885 if (settings->normal)
5887 if (!settings->vertexblends)
5889 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
5891 else
5893 for (i = 0; i < settings->vertexblends + 1; ++i)
5894 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
5897 if (settings->normalize)
5898 shader_addline(buffer, "normal = normalize(normal);\n");
5901 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
5902 if (legacy_context)
5904 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
5905 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
5908 for (i = 0; i < MAX_TEXTURES; ++i)
5910 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5912 switch (settings->texgen[i] & 0xffff0000)
5914 case WINED3DTSS_TCI_PASSTHRU:
5915 if (settings->texcoords & (1u << i))
5916 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
5917 i, i, i);
5918 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
5919 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
5920 else
5921 output_legacy_texcoord = FALSE;
5922 break;
5924 case WINED3DTSS_TCI_CAMERASPACENORMAL:
5925 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
5926 break;
5928 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
5929 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
5930 break;
5932 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
5933 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
5934 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
5935 break;
5937 case WINED3DTSS_TCI_SPHEREMAP:
5938 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
5939 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
5940 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
5941 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
5942 break;
5944 default:
5945 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
5946 break;
5948 if (output_legacy_texcoord)
5949 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
5952 switch (settings->fog_mode)
5954 case WINED3D_FFP_VS_FOG_OFF:
5955 output_legacy_fogcoord = FALSE;
5956 break;
5958 case WINED3D_FFP_VS_FOG_FOGCOORD:
5959 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
5960 break;
5962 case WINED3D_FFP_VS_FOG_RANGE:
5963 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
5964 break;
5966 case WINED3D_FFP_VS_FOG_DEPTH:
5967 if (settings->ortho_fog)
5968 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
5969 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
5970 else if (settings->transformed)
5971 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
5972 else
5973 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
5974 break;
5976 default:
5977 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
5978 break;
5980 if (output_legacy_fogcoord)
5981 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
5983 if (settings->point_size)
5985 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
5986 " + ffp_point.l_att * length(ec_pos.xyz)"
5987 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
5988 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
5989 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
5992 shader_addline(buffer, "}\n");
5994 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5995 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5997 return shader_obj;
6000 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
6001 DWORD argnum, unsigned int stage, DWORD arg)
6003 const char *ret;
6005 if (arg == ARG_UNUSED)
6006 return "<unused arg>";
6008 switch (arg & WINED3DTA_SELECTMASK)
6010 case WINED3DTA_DIFFUSE:
6011 ret = "ffp_varying_diffuse";
6012 break;
6014 case WINED3DTA_CURRENT:
6015 if (!stage)
6016 ret = "ffp_varying_diffuse";
6017 else
6018 ret = "ret";
6019 break;
6021 case WINED3DTA_TEXTURE:
6022 switch (stage)
6024 case 0: ret = "tex0"; break;
6025 case 1: ret = "tex1"; break;
6026 case 2: ret = "tex2"; break;
6027 case 3: ret = "tex3"; break;
6028 case 4: ret = "tex4"; break;
6029 case 5: ret = "tex5"; break;
6030 case 6: ret = "tex6"; break;
6031 case 7: ret = "tex7"; break;
6032 default:
6033 ret = "<invalid texture>";
6034 break;
6036 break;
6038 case WINED3DTA_TFACTOR:
6039 ret = "tex_factor";
6040 break;
6042 case WINED3DTA_SPECULAR:
6043 ret = "ffp_varying_specular";
6044 break;
6046 case WINED3DTA_TEMP:
6047 ret = "temp_reg";
6048 break;
6050 case WINED3DTA_CONSTANT:
6051 switch (stage)
6053 case 0: ret = "tss_const0"; break;
6054 case 1: ret = "tss_const1"; break;
6055 case 2: ret = "tss_const2"; break;
6056 case 3: ret = "tss_const3"; break;
6057 case 4: ret = "tss_const4"; break;
6058 case 5: ret = "tss_const5"; break;
6059 case 6: ret = "tss_const6"; break;
6060 case 7: ret = "tss_const7"; break;
6061 default:
6062 ret = "<invalid constant>";
6063 break;
6065 break;
6067 default:
6068 return "<unhandled arg>";
6071 if (arg & WINED3DTA_COMPLEMENT)
6073 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
6074 if (argnum == 0)
6075 ret = "arg0";
6076 else if (argnum == 1)
6077 ret = "arg1";
6078 else if (argnum == 2)
6079 ret = "arg2";
6082 if (arg & WINED3DTA_ALPHAREPLICATE)
6084 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
6085 if (argnum == 0)
6086 ret = "arg0";
6087 else if (argnum == 1)
6088 ret = "arg1";
6089 else if (argnum == 2)
6090 ret = "arg2";
6093 return ret;
6096 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
6097 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
6099 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
6101 if (color && alpha)
6102 dstmask = "";
6103 else if (color)
6104 dstmask = ".xyz";
6105 else
6106 dstmask = ".w";
6108 if (dst == tempreg)
6109 dstreg = "temp_reg";
6110 else
6111 dstreg = "ret";
6113 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
6114 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
6115 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
6117 switch (op)
6119 case WINED3D_TOP_DISABLE:
6120 if (!stage)
6121 shader_addline(buffer, "%s%s = ffp_varying_diffuse%s;\n", dstreg, dstmask, dstmask);
6122 break;
6124 case WINED3D_TOP_SELECT_ARG1:
6125 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
6126 break;
6128 case WINED3D_TOP_SELECT_ARG2:
6129 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
6130 break;
6132 case WINED3D_TOP_MODULATE:
6133 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6134 break;
6136 case WINED3D_TOP_MODULATE_4X:
6137 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
6138 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6139 break;
6141 case WINED3D_TOP_MODULATE_2X:
6142 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
6143 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6144 break;
6146 case WINED3D_TOP_ADD:
6147 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
6148 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6149 break;
6151 case WINED3D_TOP_ADD_SIGNED:
6152 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
6153 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6154 break;
6156 case WINED3D_TOP_ADD_SIGNED_2X:
6157 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
6158 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6159 break;
6161 case WINED3D_TOP_SUBTRACT:
6162 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
6163 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6164 break;
6166 case WINED3D_TOP_ADD_SMOOTH:
6167 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
6168 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
6169 break;
6171 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
6172 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
6173 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6174 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6175 break;
6177 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6178 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6179 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6180 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6181 break;
6183 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6184 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
6185 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6186 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6187 break;
6189 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6190 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6191 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6192 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
6193 break;
6195 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
6196 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
6197 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6198 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6199 break;
6201 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
6202 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
6203 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6204 break;
6206 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
6207 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
6208 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6209 break;
6211 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
6212 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6213 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6214 break;
6215 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
6216 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
6217 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6218 break;
6220 case WINED3D_TOP_BUMPENVMAP:
6221 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6222 /* These are handled in the first pass, nothing to do. */
6223 break;
6225 case WINED3D_TOP_DOTPRODUCT3:
6226 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
6227 dstreg, dstmask, arg1, arg2, dstmask);
6228 break;
6230 case WINED3D_TOP_MULTIPLY_ADD:
6231 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
6232 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
6233 break;
6235 case WINED3D_TOP_LERP:
6236 /* MSDN isn't quite right here. */
6237 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
6238 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
6239 break;
6241 default:
6242 FIXME("Unhandled operation %#x.\n", op);
6243 break;
6247 /* Context activation is done by the caller. */
6248 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
6249 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
6251 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6252 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
6253 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
6254 const char *final_combiner_src = "ret";
6255 UINT lowest_disabled_stage;
6256 GLuint shader_id;
6257 DWORD arg0, arg1, arg2;
6258 unsigned int stage;
6259 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
6260 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6262 string_buffer_clear(buffer);
6264 /* Find out which textures are read */
6265 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6267 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6268 break;
6270 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
6271 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
6272 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
6274 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
6275 || (stage == 0 && settings->color_key_enabled))
6276 tex_map |= 1u << stage;
6277 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6278 tfactor_used = TRUE;
6279 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6280 tempreg_used = TRUE;
6281 if (settings->op[stage].dst == tempreg)
6282 tempreg_used = TRUE;
6283 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6284 tss_const_map |= 1u << stage;
6286 switch (settings->op[stage].cop)
6288 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6289 lum_map |= 1u << stage;
6290 /* fall through */
6291 case WINED3D_TOP_BUMPENVMAP:
6292 bump_map |= 1u << stage;
6293 /* fall through */
6294 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6295 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6296 tex_map |= 1u << stage;
6297 break;
6299 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6300 tfactor_used = TRUE;
6301 break;
6303 default:
6304 break;
6307 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6308 continue;
6310 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
6311 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
6312 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
6314 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
6315 tex_map |= 1u << stage;
6316 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6317 tfactor_used = TRUE;
6318 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6319 tempreg_used = TRUE;
6320 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6321 tss_const_map |= 1u << stage;
6323 lowest_disabled_stage = stage;
6325 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6327 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6328 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6330 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
6331 shader_addline(buffer, "vec4 ret;\n");
6332 if (tempreg_used || settings->sRGB_write)
6333 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
6334 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
6336 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6338 if (tss_const_map & (1u << stage))
6339 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
6341 if (!(tex_map & (1u << stage)))
6342 continue;
6344 switch (settings->op[stage].tex_type)
6346 case WINED3D_GL_RES_TYPE_TEX_1D:
6347 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
6348 break;
6349 case WINED3D_GL_RES_TYPE_TEX_2D:
6350 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
6351 break;
6352 case WINED3D_GL_RES_TYPE_TEX_3D:
6353 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
6354 break;
6355 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6356 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
6357 break;
6358 case WINED3D_GL_RES_TYPE_TEX_RECT:
6359 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
6360 break;
6361 default:
6362 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
6363 break;
6366 shader_addline(buffer, "vec4 tex%u;\n", stage);
6368 if (!(bump_map & (1u << stage)))
6369 continue;
6370 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
6372 if (!(lum_map & (1u << stage)))
6373 continue;
6374 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
6375 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
6377 if (tfactor_used)
6378 shader_addline(buffer, "uniform vec4 tex_factor;\n");
6379 if (settings->color_key_enabled)
6380 shader_addline(buffer, "uniform vec4 color_key[2];\n");
6381 shader_addline(buffer, "uniform vec4 specular_enable;\n");
6383 if (settings->sRGB_write)
6385 shader_addline(buffer, "const vec4 srgb_const0 = ");
6386 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
6387 shader_addline(buffer, ";\n");
6388 shader_addline(buffer, "const vec4 srgb_const1 = ");
6389 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
6390 shader_addline(buffer, ";\n");
6393 shader_addline(buffer, "uniform struct\n{\n");
6394 shader_addline(buffer, " vec4 color;\n");
6395 shader_addline(buffer, " float density;\n");
6396 shader_addline(buffer, " float end;\n");
6397 shader_addline(buffer, " float scale;\n");
6398 shader_addline(buffer, "} ffp_fog;\n");
6400 if (legacy_context)
6402 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6403 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6404 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6405 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6406 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6408 else
6410 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_diffuse;\n");
6411 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_specular;\n");
6412 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6413 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6414 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6417 shader_addline(buffer, "void main()\n{\n");
6419 if (legacy_context)
6421 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
6422 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
6425 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6427 if (tex_map & (1u << stage))
6429 if (settings->pointsprite)
6430 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
6431 else if (settings->texcoords_initialized & (1u << stage))
6432 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
6433 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
6434 else
6435 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
6439 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
6440 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6442 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
6443 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
6445 /* Generate texture sampling instructions) */
6446 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
6448 const char *texture_function, *coord_mask;
6449 BOOL proj;
6451 if (!(tex_map & (1u << stage)))
6452 continue;
6454 if (settings->op[stage].projected == proj_none)
6456 proj = FALSE;
6458 else if (settings->op[stage].projected == proj_count4
6459 || settings->op[stage].projected == proj_count3)
6461 proj = TRUE;
6463 else
6465 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
6466 proj = TRUE;
6469 switch (settings->op[stage].tex_type)
6471 case WINED3D_GL_RES_TYPE_TEX_1D:
6472 if (proj)
6474 texture_function = "texture1DProj";
6475 coord_mask = "xw";
6477 else
6479 texture_function = "texture1D";
6480 coord_mask = "x";
6482 break;
6483 case WINED3D_GL_RES_TYPE_TEX_2D:
6484 if (proj)
6486 texture_function = "texture2DProj";
6487 coord_mask = "xyw";
6489 else
6491 texture_function = "texture2D";
6492 coord_mask = "xy";
6494 break;
6495 case WINED3D_GL_RES_TYPE_TEX_3D:
6496 if (proj)
6498 texture_function = "texture3DProj";
6499 coord_mask = "xyzw";
6501 else
6503 texture_function = "texture3D";
6504 coord_mask = "xyz";
6506 break;
6507 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6508 texture_function = "textureCube";
6509 coord_mask = "xyz";
6510 break;
6511 case WINED3D_GL_RES_TYPE_TEX_RECT:
6512 if (proj)
6514 texture_function = "texture2DRectProj";
6515 coord_mask = "xyw";
6517 else
6519 texture_function = "texture2DRect";
6520 coord_mask = "xy";
6522 break;
6523 default:
6524 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
6525 texture_function = "";
6526 coord_mask = "xyzw";
6527 break;
6530 if (stage > 0
6531 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
6532 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
6534 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
6536 /* With projective textures, texbem only divides the static
6537 * texture coord, not the displacement, so multiply the
6538 * displacement with the dividing parameter before passing it to
6539 * TXP. */
6540 if (settings->op[stage].projected != proj_none)
6542 if (settings->op[stage].projected == proj_count4)
6544 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
6545 stage, stage);
6546 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
6548 else
6550 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
6551 stage, stage);
6552 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
6555 else
6557 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
6560 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
6561 stage, texture_function, stage, coord_mask);
6563 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6564 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
6565 stage, stage - 1, stage - 1, stage - 1);
6567 else if (settings->op[stage].projected == proj_count3)
6569 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
6570 stage, texture_function, stage, stage);
6572 else
6574 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
6575 stage, texture_function, stage, stage, coord_mask);
6578 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
6579 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
6580 settings->op[stage].color_fixup);
6583 if (settings->color_key_enabled)
6585 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
6586 shader_addline(buffer, " discard;\n");
6589 /* Generate the main shader */
6590 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6592 BOOL op_equal;
6594 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6596 if (!stage)
6597 final_combiner_src = "ffp_varying_diffuse";
6598 break;
6601 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6602 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6603 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
6604 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6605 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6606 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
6607 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6608 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6609 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
6610 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6611 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6612 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
6613 else
6614 op_equal = settings->op[stage].aop == settings->op[stage].cop
6615 && settings->op[stage].carg0 == settings->op[stage].aarg0
6616 && settings->op[stage].carg1 == settings->op[stage].aarg1
6617 && settings->op[stage].carg2 == settings->op[stage].aarg2;
6619 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6621 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6622 settings->op[stage].cop, settings->op[stage].carg0,
6623 settings->op[stage].carg1, settings->op[stage].carg2);
6624 if (!stage)
6625 shader_addline(buffer, "ret.w = ffp_varying_diffuse.w;\n");
6627 else if (op_equal)
6629 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
6630 settings->op[stage].cop, settings->op[stage].carg0,
6631 settings->op[stage].carg1, settings->op[stage].carg2);
6633 else
6635 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6636 settings->op[stage].cop, settings->op[stage].carg0,
6637 settings->op[stage].carg1, settings->op[stage].carg2);
6638 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
6639 settings->op[stage].aop, settings->op[stage].aarg0,
6640 settings->op[stage].aarg1, settings->op[stage].aarg2);
6644 shader_addline(buffer, "gl_FragData[0] = ffp_varying_specular * specular_enable + %s;\n", final_combiner_src);
6646 if (settings->sRGB_write)
6647 shader_glsl_generate_srgb_write_correction(buffer);
6649 shader_glsl_generate_fog_code(buffer, settings->fog);
6651 shader_addline(buffer, "}\n");
6653 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6654 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6656 string_buffer_release(&priv->string_buffers, tex_reg_name);
6657 return shader_id;
6660 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
6661 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
6663 struct glsl_ffp_vertex_shader *shader;
6664 const struct wine_rb_entry *entry;
6666 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
6667 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
6669 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
6670 return NULL;
6672 shader->desc.settings = *settings;
6673 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
6674 list_init(&shader->linked_programs);
6675 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
6676 ERR("Failed to insert ffp vertex shader.\n");
6678 return shader;
6681 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
6682 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
6684 struct glsl_ffp_fragment_shader *glsl_desc;
6685 const struct ffp_frag_desc *desc;
6687 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
6688 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
6690 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
6691 return NULL;
6693 glsl_desc->entry.settings = *args;
6694 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
6695 list_init(&glsl_desc->linked_programs);
6696 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
6698 return glsl_desc;
6702 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
6703 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
6705 unsigned int i;
6706 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6708 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
6709 sizeof(GLuint) * gl_info->limits.glsl_vs_float_constants);
6710 for (i = 0; i < vs_c_count; ++i)
6712 string_buffer_sprintf(name, "vs_c[%u]", i);
6713 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6715 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
6716 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLuint));
6718 for (i = 0; i < MAX_CONST_I; ++i)
6720 string_buffer_sprintf(name, "vs_i[%u]", i);
6721 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6724 for (i = 0; i < MAX_CONST_B; ++i)
6726 string_buffer_sprintf(name, "vs_b[%u]", i);
6727 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6730 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "posFixup"));
6732 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
6734 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
6735 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6737 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
6738 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
6739 for (i = 0; i < MAX_TEXTURES; ++i)
6741 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
6742 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6744 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
6745 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
6746 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
6747 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
6748 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
6749 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
6750 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
6752 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
6753 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6754 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
6755 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6756 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
6757 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6758 string_buffer_sprintf(name, "ffp_light[%u].position", i);
6759 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6760 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
6761 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6762 string_buffer_sprintf(name, "ffp_light[%u].range", i);
6763 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6764 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
6765 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6766 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
6767 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6768 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
6769 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6770 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
6771 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6772 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
6773 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6774 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
6775 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6777 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
6778 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
6779 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
6780 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
6781 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
6782 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
6784 string_buffer_release(&priv->string_buffers, name);
6787 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
6788 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
6790 unsigned int i;
6791 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6793 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
6794 sizeof(GLuint) * gl_info->limits.glsl_ps_float_constants);
6795 for (i = 0; i < ps_c_count; ++i)
6797 string_buffer_sprintf(name, "ps_c[%u]", i);
6798 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6800 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
6801 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLuint));
6803 for (i = 0; i < MAX_CONST_I; ++i)
6805 string_buffer_sprintf(name, "ps_i[%u]", i);
6806 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6809 for (i = 0; i < MAX_CONST_B; ++i)
6811 string_buffer_sprintf(name, "ps_b[%u]", i);
6812 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6815 for (i = 0; i < MAX_TEXTURES; ++i)
6817 string_buffer_sprintf(name, "bumpenv_mat%u", i);
6818 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6819 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
6820 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6821 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
6822 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6823 string_buffer_sprintf(name, "tss_const%u", i);
6824 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6827 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
6828 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
6830 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
6831 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
6832 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
6833 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
6835 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
6836 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
6837 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
6839 string_buffer_release(&priv->string_buffers, name);
6842 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
6843 struct shader_glsl_priv *priv, GLuint program_id,
6844 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
6846 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6847 GLuint block_idx;
6848 unsigned int i;
6849 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6851 for (i = 0; i < count; ++i)
6853 if (!reg_maps->cb_sizes[i])
6854 continue;
6856 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
6857 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
6858 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
6860 checkGLcall("glUniformBlockBinding");
6861 string_buffer_release(&priv->string_buffers, name);
6864 /* Context activation is done by the caller. */
6865 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
6866 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
6868 const struct wined3d_gl_info *gl_info = context->gl_info;
6869 const struct ps_np2fixup_info *np2fixup_info = NULL;
6870 struct glsl_shader_prog_link *entry = NULL;
6871 struct wined3d_shader *vshader = NULL;
6872 struct wined3d_shader *gshader = NULL;
6873 struct wined3d_shader *pshader = NULL;
6874 GLuint program_id = 0;
6875 GLuint reorder_shader_id = 0;
6876 unsigned int i;
6877 GLuint vs_id = 0;
6878 GLuint gs_id = 0;
6879 GLuint ps_id = 0;
6880 struct list *ps_list, *vs_list;
6881 WORD attribs_map;
6882 struct wined3d_string_buffer *tmp_name;
6884 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
6886 vs_id = ctx_data->glsl_program->vs.id;
6887 vs_list = &ctx_data->glsl_program->vs.shader_entry;
6889 if (use_vs(state))
6891 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
6892 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
6894 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
6895 && ctx_data->glsl_program->gs.id)
6896 gs_id = ctx_data->glsl_program->gs.id;
6897 else if (gshader)
6898 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, &priv->string_buffers, gshader);
6901 else if (use_vs(state))
6903 struct vs_compile_args vs_compile_args;
6904 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
6906 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args);
6907 vs_id = find_glsl_vshader(context, &priv->shader_buffer, &priv->string_buffers, vshader, &vs_compile_args);
6908 vs_list = &vshader->linked_programs;
6910 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
6911 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, &priv->string_buffers, gshader);
6913 else if (priv->vertex_pipe == &glsl_vertex_pipe)
6915 struct glsl_ffp_vertex_shader *ffp_shader;
6916 struct wined3d_ffp_vs_settings settings;
6918 wined3d_ffp_get_vs_settings(context, state, &settings);
6919 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
6920 vs_id = ffp_shader->id;
6921 vs_list = &ffp_shader->linked_programs;
6924 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
6926 ps_id = ctx_data->glsl_program->ps.id;
6927 ps_list = &ctx_data->glsl_program->ps.shader_entry;
6929 if (use_ps(state))
6930 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
6932 else if (use_ps(state))
6934 struct ps_compile_args ps_compile_args;
6935 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
6936 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
6937 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
6938 pshader, &ps_compile_args, &np2fixup_info);
6939 ps_list = &pshader->linked_programs;
6941 else if (priv->fragment_pipe == &glsl_fragment_pipe)
6943 struct glsl_ffp_fragment_shader *ffp_shader;
6944 struct ffp_frag_settings settings;
6946 gen_ffp_frag_op(context, state, &settings, FALSE);
6947 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
6948 ps_id = ffp_shader->id;
6949 ps_list = &ffp_shader->linked_programs;
6952 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
6954 ctx_data->glsl_program = entry;
6955 return;
6958 /* If we get to this point, then no matching program exists, so we create one */
6959 program_id = GL_EXTCALL(glCreateProgram());
6960 TRACE("Created new GLSL shader program %u.\n", program_id);
6962 /* Create the entry */
6963 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
6964 entry->id = program_id;
6965 entry->vs.id = vs_id;
6966 entry->gs.id = gs_id;
6967 entry->ps.id = ps_id;
6968 entry->constant_version = 0;
6969 entry->ps.np2_fixup_info = np2fixup_info;
6970 /* Add the hash table entry */
6971 add_glsl_program_entry(priv, entry);
6973 /* Set the current program */
6974 ctx_data->glsl_program = entry;
6976 /* Attach GLSL vshader */
6977 if (vs_id)
6979 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
6980 GL_EXTCALL(glAttachShader(program_id, vs_id));
6981 checkGLcall("glAttachShader");
6983 list_add_head(vs_list, &entry->vs.shader_entry);
6986 if (vshader)
6988 attribs_map = vshader->reg_maps.input_registers;
6989 reorder_shader_id = generate_param_reorder_function(priv, vshader, pshader,
6990 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size, gl_info);
6991 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
6992 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
6993 checkGLcall("glAttachShader");
6994 /* Flag the reorder function for deletion, then it will be freed automatically when the program
6995 * is destroyed
6997 GL_EXTCALL(glDeleteShader(reorder_shader_id));
6999 else
7001 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
7004 /* Bind vertex attributes to a corresponding index number to match
7005 * the same index numbers as ARB_vertex_programs (makes loading
7006 * vertex attributes simpler). With this method, we can use the
7007 * exact same code to load the attributes later for both ARB and
7008 * GLSL shaders.
7010 * We have to do this here because we need to know the Program ID
7011 * in order to make the bindings work, and it has to be done prior
7012 * to linking the GLSL program. */
7013 tmp_name = string_buffer_get(&priv->string_buffers);
7014 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
7016 if (!(attribs_map & 1))
7017 continue;
7019 string_buffer_sprintf(tmp_name, "vs_in%u", i);
7020 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
7022 checkGLcall("glBindAttribLocation");
7023 string_buffer_release(&priv->string_buffers, tmp_name);
7025 if (gshader)
7027 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
7028 GL_EXTCALL(glAttachShader(program_id, gs_id));
7029 checkGLcall("glAttachShader");
7031 TRACE("input type %s, output type %s, vertices out %u.\n",
7032 debug_d3dprimitivetype(gshader->u.gs.input_type),
7033 debug_d3dprimitivetype(gshader->u.gs.output_type),
7034 gshader->u.gs.vertices_out);
7035 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
7036 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
7037 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
7038 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
7039 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
7040 gshader->u.gs.vertices_out));
7041 checkGLcall("glProgramParameteriARB");
7043 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
7046 /* Attach GLSL pshader */
7047 if (ps_id)
7049 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
7050 GL_EXTCALL(glAttachShader(program_id, ps_id));
7051 checkGLcall("glAttachShader");
7053 list_add_head(ps_list, &entry->ps.shader_entry);
7056 /* Link the program */
7057 TRACE("Linking GLSL shader program %u.\n", program_id);
7058 GL_EXTCALL(glLinkProgram(program_id));
7059 shader_glsl_validate_link(gl_info, program_id);
7061 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
7062 vshader ? min(vshader->limits->constant_float, gl_info->limits.glsl_vs_float_constants) : 0);
7063 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
7064 pshader ? min(pshader->limits->constant_float, gl_info->limits.glsl_ps_float_constants) : 0);
7065 checkGLcall("Find glsl program uniform locations");
7067 if (pshader && pshader->reg_maps.shader_version.major >= 3
7068 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
7070 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
7071 entry->vs.vertex_color_clamp = GL_FALSE;
7073 else
7075 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7078 /* Set the shader to allow uniform loading on it */
7079 GL_EXTCALL(glUseProgram(program_id));
7080 checkGLcall("glUseProgram");
7082 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
7083 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
7084 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
7085 * vertex shader with fixed function pixel processing is used we make sure that the card
7086 * supports enough samplers to allow the max number of vertex samplers with all possible
7087 * fixed function fragment processing setups. So once the program is linked these samplers
7088 * won't change. */
7089 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
7091 entry->constant_update_mask = 0;
7092 if (vshader)
7094 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
7095 if (vshader->reg_maps.integer_constants)
7096 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
7097 if (vshader->reg_maps.boolean_constants)
7098 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
7099 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
7101 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps,
7102 0, gl_info->limits.vertex_uniform_blocks);
7104 else
7106 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
7107 | WINED3D_SHADER_CONST_FFP_PROJ;
7109 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
7111 if (entry->vs.modelview_matrix_location[i] != -1)
7113 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
7114 break;
7118 for (i = 0; i < MAX_TEXTURES; ++i)
7120 if (entry->vs.texture_matrix_location[i] != -1)
7122 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7123 break;
7126 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
7127 || entry->vs.material_specular_location != -1
7128 || entry->vs.material_emissive_location != -1
7129 || entry->vs.material_shininess_location != -1)
7130 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
7131 if (entry->vs.light_ambient_location != -1)
7132 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
7134 if (entry->vs.pointsize_min_location != -1)
7135 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
7137 if (gshader)
7138 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps,
7139 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
7141 if (ps_id)
7143 if (pshader)
7145 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
7146 if (pshader->reg_maps.integer_constants)
7147 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
7148 if (pshader->reg_maps.boolean_constants)
7149 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
7150 if (entry->ps.ycorrection_location != -1)
7151 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
7153 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps,
7154 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
7155 gl_info->limits.fragment_uniform_blocks);
7157 else
7159 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7162 for (i = 0; i < MAX_TEXTURES; ++i)
7164 if (entry->ps.bumpenv_mat_location[i] != -1)
7166 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
7167 break;
7171 if (entry->ps.fog_color_location != -1)
7172 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
7173 if (entry->ps.np2_fixup_location != -1)
7174 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
7175 if (entry->ps.color_key_location != -1)
7176 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
7180 /* Context activation is done by the caller. */
7181 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum wined3d_gl_resource_type tex_type,
7182 BOOL masked)
7184 GLuint program_id;
7185 GLuint vshader_id, pshader_id;
7186 const char *blt_pshader;
7188 static const char blt_vshader[] =
7189 "#version 120\n"
7190 "void main(void)\n"
7191 "{\n"
7192 " gl_Position = gl_Vertex;\n"
7193 " gl_FrontColor = vec4(1.0);\n"
7194 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
7195 "}\n";
7197 static const char * const blt_pshaders_full[WINED3D_GL_RES_TYPE_COUNT] =
7199 /* WINED3D_GL_RES_TYPE_TEX_1D */
7200 NULL,
7201 /* WINED3D_GL_RES_TYPE_TEX_2D */
7202 "#version 120\n"
7203 "uniform sampler2D sampler;\n"
7204 "void main(void)\n"
7205 "{\n"
7206 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7207 "}\n",
7208 /* WINED3D_GL_RES_TYPE_TEX_3D */
7209 NULL,
7210 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7211 "#version 120\n"
7212 "uniform samplerCube sampler;\n"
7213 "void main(void)\n"
7214 "{\n"
7215 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7216 "}\n",
7217 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7218 "#version 120\n"
7219 "#extension GL_ARB_texture_rectangle : enable\n"
7220 "uniform sampler2DRect sampler;\n"
7221 "void main(void)\n"
7222 "{\n"
7223 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7224 "}\n",
7225 /* WINED3D_GL_RES_TYPE_BUFFER */
7226 NULL,
7227 /* WINED3D_GL_RES_TYPE_RB */
7228 NULL,
7231 static const char * const blt_pshaders_masked[WINED3D_GL_RES_TYPE_COUNT] =
7233 /* WINED3D_GL_RES_TYPE_TEX_1D */
7234 NULL,
7235 /* WINED3D_GL_RES_TYPE_TEX_2D */
7236 "#version 120\n"
7237 "uniform sampler2D sampler;\n"
7238 "uniform vec4 mask;\n"
7239 "void main(void)\n"
7240 "{\n"
7241 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7242 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7243 "}\n",
7244 /* WINED3D_GL_RES_TYPE_TEX_3D */
7245 NULL,
7246 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7247 "#version 120\n"
7248 "uniform samplerCube sampler;\n"
7249 "uniform vec4 mask;\n"
7250 "void main(void)\n"
7251 "{\n"
7252 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7253 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7254 "}\n",
7255 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7256 "#version 120\n"
7257 "#extension GL_ARB_texture_rectangle : enable\n"
7258 "uniform sampler2DRect sampler;\n"
7259 "uniform vec4 mask;\n"
7260 "void main(void)\n"
7261 "{\n"
7262 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7263 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7264 "}\n",
7265 /* WINED3D_GL_RES_TYPE_BUFFER */
7266 NULL,
7267 /* WINED3D_GL_RES_TYPE_RB */
7268 NULL,
7271 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
7272 if (!blt_pshader)
7274 FIXME("tex_type %#x not supported\n", tex_type);
7275 return 0;
7278 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7279 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
7281 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7282 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
7284 program_id = GL_EXTCALL(glCreateProgram());
7285 GL_EXTCALL(glAttachShader(program_id, vshader_id));
7286 GL_EXTCALL(glAttachShader(program_id, pshader_id));
7287 GL_EXTCALL(glLinkProgram(program_id));
7289 shader_glsl_validate_link(gl_info, program_id);
7291 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
7292 * is destroyed
7294 GL_EXTCALL(glDeleteShader(vshader_id));
7295 GL_EXTCALL(glDeleteShader(pshader_id));
7296 return program_id;
7299 /* Context activation is done by the caller. */
7300 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
7301 const struct wined3d_state *state)
7303 struct glsl_context_data *ctx_data = context->shader_backend_data;
7304 const struct wined3d_gl_info *gl_info = context->gl_info;
7305 struct shader_glsl_priv *priv = shader_priv;
7306 GLuint program_id = 0, prev_id = 0;
7307 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
7309 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
7310 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
7312 if (ctx_data->glsl_program)
7314 prev_id = ctx_data->glsl_program->id;
7315 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7317 else
7319 prev_id = 0;
7320 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7323 set_glsl_shader_program(context, state, priv, ctx_data);
7325 if (ctx_data->glsl_program)
7327 program_id = ctx_data->glsl_program->id;
7328 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7330 else
7332 program_id = 0;
7333 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7336 if (old_vertex_color_clamp != current_vertex_color_clamp)
7338 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7340 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
7341 checkGLcall("glClampColorARB");
7343 else
7345 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
7349 TRACE("Using GLSL program %u.\n", program_id);
7351 if (prev_id != program_id)
7353 GL_EXTCALL(glUseProgram(program_id));
7354 checkGLcall("glUseProgram");
7356 if (program_id)
7357 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
7361 /* "context" is not necessarily the currently active context. */
7362 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
7364 struct glsl_context_data *ctx_data = context->shader_backend_data;
7366 ctx_data->glsl_program = NULL;
7367 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
7368 | (1u << WINED3D_SHADER_TYPE_VERTEX)
7369 | (1u << WINED3D_SHADER_TYPE_GEOMETRY);
7372 /* Context activation is done by the caller. */
7373 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
7375 const struct wined3d_gl_info *gl_info = context->gl_info;
7376 struct shader_glsl_priv *priv = shader_priv;
7378 shader_glsl_invalidate_current_program(context);
7379 GL_EXTCALL(glUseProgram(0));
7380 checkGLcall("glUseProgram");
7382 priv->vertex_pipe->vp_enable(gl_info, FALSE);
7383 priv->fragment_pipe->enable_extension(gl_info, FALSE);
7385 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7387 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
7388 checkGLcall("glClampColorARB");
7392 /* Context activation is done by the caller. */
7393 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
7394 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size)
7396 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
7397 struct shader_glsl_priv *priv = shader_priv;
7398 GLuint *blt_program;
7399 GLint loc;
7401 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
7402 if (!*blt_program)
7404 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
7405 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
7406 GL_EXTCALL(glUseProgram(*blt_program));
7407 GL_EXTCALL(glUniform1i(loc, 0));
7409 else
7411 GL_EXTCALL(glUseProgram(*blt_program));
7414 if (masked)
7416 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
7417 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
7421 /* Context activation is done by the caller. */
7422 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
7424 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
7425 GLuint program_id;
7427 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
7428 if (program_id) TRACE("Using GLSL program %u\n", program_id);
7430 GL_EXTCALL(glUseProgram(program_id));
7431 checkGLcall("glUseProgram");
7434 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
7435 const struct glsl_shader_prog_link *program)
7437 const struct glsl_context_data *ctx_data;
7438 struct wined3d_context *context;
7439 unsigned int i;
7441 for (i = 0; i < device->context_count; ++i)
7443 context = device->contexts[i];
7444 ctx_data = context->shader_backend_data;
7446 if (ctx_data->glsl_program == program)
7447 shader_glsl_invalidate_current_program(context);
7451 static void shader_glsl_destroy(struct wined3d_shader *shader)
7453 struct glsl_shader_private *shader_data = shader->backend_data;
7454 struct wined3d_device *device = shader->device;
7455 struct shader_glsl_priv *priv = device->shader_priv;
7456 const struct wined3d_gl_info *gl_info;
7457 const struct list *linked_programs;
7458 struct wined3d_context *context;
7460 if (!shader_data || !shader_data->num_gl_shaders)
7462 HeapFree(GetProcessHeap(), 0, shader_data);
7463 shader->backend_data = NULL;
7464 return;
7467 context = context_acquire(device, NULL);
7468 gl_info = context->gl_info;
7470 TRACE("Deleting linked programs.\n");
7471 linked_programs = &shader->linked_programs;
7472 if (linked_programs->next)
7474 struct glsl_shader_prog_link *entry, *entry2;
7475 UINT i;
7477 switch (shader->reg_maps.shader_version.type)
7479 case WINED3D_SHADER_TYPE_PIXEL:
7481 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
7483 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7485 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
7486 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7487 checkGLcall("glDeleteShader");
7489 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
7491 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7492 struct glsl_shader_prog_link, ps.shader_entry)
7494 shader_glsl_invalidate_contexts_program(device, entry);
7495 delete_glsl_program_entry(priv, gl_info, entry);
7498 break;
7501 case WINED3D_SHADER_TYPE_VERTEX:
7503 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
7505 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7507 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
7508 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7509 checkGLcall("glDeleteShader");
7511 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
7513 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7514 struct glsl_shader_prog_link, vs.shader_entry)
7516 shader_glsl_invalidate_contexts_program(device, entry);
7517 delete_glsl_program_entry(priv, gl_info, entry);
7520 break;
7523 case WINED3D_SHADER_TYPE_GEOMETRY:
7525 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
7527 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7529 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
7530 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7531 checkGLcall("glDeleteShader");
7533 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
7535 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7536 struct glsl_shader_prog_link, gs.shader_entry)
7538 shader_glsl_invalidate_contexts_program(device, entry);
7539 delete_glsl_program_entry(priv, gl_info, entry);
7542 break;
7545 default:
7546 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
7547 break;
7551 HeapFree(GetProcessHeap(), 0, shader->backend_data);
7552 shader->backend_data = NULL;
7554 context_release(context);
7557 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
7559 const struct glsl_program_key *k = key;
7560 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
7561 const struct glsl_shader_prog_link, program_lookup_entry);
7563 if (k->vs_id > prog->vs.id) return 1;
7564 else if (k->vs_id < prog->vs.id) return -1;
7566 if (k->gs_id > prog->gs.id) return 1;
7567 else if (k->gs_id < prog->gs.id) return -1;
7569 if (k->ps_id > prog->ps.id) return 1;
7570 else if (k->ps_id < prog->ps.id) return -1;
7572 return 0;
7575 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
7577 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
7578 + constant_count * sizeof(*heap->contained)
7579 + constant_count * sizeof(*heap->positions);
7580 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
7582 if (!mem)
7584 ERR("Failed to allocate memory\n");
7585 return FALSE;
7588 heap->entries = mem;
7589 heap->entries[1].version = 0;
7590 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
7591 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
7592 heap->positions = (unsigned int *)(heap->contained + constant_count);
7593 heap->size = 1;
7595 return TRUE;
7598 static void constant_heap_free(struct constant_heap *heap)
7600 HeapFree(GetProcessHeap(), 0, heap->entries);
7603 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
7605 wined3d_rb_alloc,
7606 wined3d_rb_realloc,
7607 wined3d_rb_free,
7608 glsl_program_key_compare,
7611 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
7612 const struct fragment_pipeline *fragment_pipe)
7614 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7615 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
7616 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
7617 gl_info->limits.glsl_ps_float_constants)) + 1;
7618 struct fragment_caps fragment_caps;
7619 void *vertex_priv, *fragment_priv;
7621 string_buffer_list_init(&priv->string_buffers);
7623 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
7625 ERR("Failed to initialize vertex pipe.\n");
7626 HeapFree(GetProcessHeap(), 0, priv);
7627 return E_FAIL;
7630 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
7632 ERR("Failed to initialize fragment pipe.\n");
7633 vertex_pipe->vp_free(device);
7634 HeapFree(GetProcessHeap(), 0, priv);
7635 return E_FAIL;
7638 if (!string_buffer_init(&priv->shader_buffer))
7640 ERR("Failed to initialize shader buffer.\n");
7641 goto fail;
7644 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
7645 if (!priv->stack)
7647 ERR("Failed to allocate memory.\n");
7648 goto fail;
7651 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
7653 ERR("Failed to initialize vertex shader constant heap\n");
7654 goto fail;
7657 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
7659 ERR("Failed to initialize pixel shader constant heap\n");
7660 goto fail;
7663 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
7665 ERR("Failed to initialize rbtree.\n");
7666 goto fail;
7669 priv->next_constant_version = 1;
7670 priv->vertex_pipe = vertex_pipe;
7671 priv->fragment_pipe = fragment_pipe;
7672 fragment_pipe->get_caps(gl_info, &fragment_caps);
7673 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
7674 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
7676 device->vertex_priv = vertex_priv;
7677 device->fragment_priv = fragment_priv;
7678 device->shader_priv = priv;
7680 return WINED3D_OK;
7682 fail:
7683 constant_heap_free(&priv->pconst_heap);
7684 constant_heap_free(&priv->vconst_heap);
7685 HeapFree(GetProcessHeap(), 0, priv->stack);
7686 string_buffer_free(&priv->shader_buffer);
7687 fragment_pipe->free_private(device);
7688 vertex_pipe->vp_free(device);
7689 HeapFree(GetProcessHeap(), 0, priv);
7690 return E_OUTOFMEMORY;
7693 /* Context activation is done by the caller. */
7694 static void shader_glsl_free(struct wined3d_device *device)
7696 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7697 struct shader_glsl_priv *priv = device->shader_priv;
7698 int i;
7700 for (i = 0; i < WINED3D_GL_RES_TYPE_COUNT; ++i)
7702 if (priv->depth_blt_program_full[i])
7704 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
7706 if (priv->depth_blt_program_masked[i])
7708 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
7712 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
7713 constant_heap_free(&priv->pconst_heap);
7714 constant_heap_free(&priv->vconst_heap);
7715 HeapFree(GetProcessHeap(), 0, priv->stack);
7716 string_buffer_list_cleanup(&priv->string_buffers);
7717 string_buffer_free(&priv->shader_buffer);
7718 priv->fragment_pipe->free_private(device);
7719 priv->vertex_pipe->vp_free(device);
7721 HeapFree(GetProcessHeap(), 0, device->shader_priv);
7722 device->shader_priv = NULL;
7725 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
7727 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
7728 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
7731 static void shader_glsl_free_context_data(struct wined3d_context *context)
7733 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
7736 static void shader_glsl_init_context_state(struct wined3d_context *context)
7738 const struct wined3d_gl_info *gl_info = context->gl_info;
7740 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
7741 checkGLcall("GL_PROGRAM_POINT_SIZE");
7744 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
7746 UINT shader_model;
7748 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
7749 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
7750 && gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] && gl_info->supported[ARB_DRAW_INSTANCED]
7751 && gl_info->supported[ARB_TEXTURE_RG] && gl_info->supported[ARB_SAMPLER_OBJECTS])
7752 shader_model = 4;
7753 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
7754 * texldd and texldl instructions. */
7755 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
7756 shader_model = 3;
7757 else
7758 shader_model = 2;
7759 TRACE("Shader model %u.\n", shader_model);
7761 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
7762 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
7763 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
7765 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
7766 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
7767 caps->varying_count = gl_info->limits.glsl_varyings;
7769 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
7770 * Direct3D minimum requirement.
7772 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
7773 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
7775 * The problem is that the refrast clamps temporary results in the shader to
7776 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
7777 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
7778 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
7779 * offer a way to query this.
7781 if (shader_model >= 4)
7782 caps->ps_1x_max_value = FLT_MAX;
7783 else
7784 caps->ps_1x_max_value = 1024.0f;
7786 /* Ideally we'd only set caps like sRGB writes here if supported by both
7787 * the shader backend and the fragment pipe, but we can get called before
7788 * shader_glsl_alloc(). */
7789 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
7790 | WINED3D_SHADER_CAP_SRGB_WRITE;
7793 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
7795 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
7797 TRACE("Checking support for fixup:\n");
7798 dump_color_fixup_desc(fixup);
7801 /* We support everything except YUV conversions. */
7802 if (!is_complex_fixup(fixup))
7804 TRACE("[OK]\n");
7805 return TRUE;
7808 TRACE("[FAILED]\n");
7809 return FALSE;
7812 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
7814 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
7815 /* WINED3DSIH_ADD */ shader_glsl_binop,
7816 /* WINED3DSIH_AND */ shader_glsl_binop,
7817 /* WINED3DSIH_BEM */ shader_glsl_bem,
7818 /* WINED3DSIH_BREAK */ shader_glsl_break,
7819 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
7820 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
7821 /* WINED3DSIH_CALL */ shader_glsl_call,
7822 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
7823 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
7824 /* WINED3DSIH_CND */ shader_glsl_cnd,
7825 /* WINED3DSIH_CRS */ shader_glsl_cross,
7826 /* WINED3DSIH_CUT */ shader_glsl_cut,
7827 /* WINED3DSIH_DCL */ shader_glsl_nop,
7828 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
7829 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
7830 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
7831 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
7832 /* WINED3DSIH_DEF */ shader_glsl_nop,
7833 /* WINED3DSIH_DEFB */ shader_glsl_nop,
7834 /* WINED3DSIH_DEFI */ shader_glsl_nop,
7835 /* WINED3DSIH_DIV */ shader_glsl_binop,
7836 /* WINED3DSIH_DP2 */ shader_glsl_dot,
7837 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
7838 /* WINED3DSIH_DP3 */ shader_glsl_dot,
7839 /* WINED3DSIH_DP4 */ shader_glsl_dot,
7840 /* WINED3DSIH_DST */ shader_glsl_dst,
7841 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
7842 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
7843 /* WINED3DSIH_ELSE */ shader_glsl_else,
7844 /* WINED3DSIH_EMIT */ shader_glsl_emit,
7845 /* WINED3DSIH_ENDIF */ shader_glsl_end,
7846 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
7847 /* WINED3DSIH_ENDREP */ shader_glsl_end,
7848 /* WINED3DSIH_EQ */ shader_glsl_relop,
7849 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
7850 /* WINED3DSIH_EXPP */ shader_glsl_expp,
7851 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
7852 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
7853 /* WINED3DSIH_GE */ shader_glsl_relop,
7854 /* WINED3DSIH_IADD */ shader_glsl_binop,
7855 /* WINED3DSIH_IEQ */ NULL,
7856 /* WINED3DSIH_IF */ shader_glsl_if,
7857 /* WINED3DSIH_IFC */ shader_glsl_ifc,
7858 /* WINED3DSIH_IGE */ shader_glsl_relop,
7859 /* WINED3DSIH_IMUL */ shader_glsl_imul,
7860 /* WINED3DSIH_ISHL */ shader_glsl_binop,
7861 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
7862 /* WINED3DSIH_LABEL */ shader_glsl_label,
7863 /* WINED3DSIH_LD */ NULL,
7864 /* WINED3DSIH_LIT */ shader_glsl_lit,
7865 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
7866 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
7867 /* WINED3DSIH_LOOP */ shader_glsl_loop,
7868 /* WINED3DSIH_LRP */ shader_glsl_lrp,
7869 /* WINED3DSIH_LT */ shader_glsl_relop,
7870 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
7871 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
7872 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
7873 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
7874 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
7875 /* WINED3DSIH_MAD */ shader_glsl_mad,
7876 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
7877 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
7878 /* WINED3DSIH_MOV */ shader_glsl_mov,
7879 /* WINED3DSIH_MOVA */ shader_glsl_mov,
7880 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
7881 /* WINED3DSIH_MUL */ shader_glsl_binop,
7882 /* WINED3DSIH_NE */ shader_glsl_relop,
7883 /* WINED3DSIH_NOP */ shader_glsl_nop,
7884 /* WINED3DSIH_NRM */ shader_glsl_nrm,
7885 /* WINED3DSIH_OR */ shader_glsl_binop,
7886 /* WINED3DSIH_PHASE */ shader_glsl_nop,
7887 /* WINED3DSIH_POW */ shader_glsl_pow,
7888 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
7889 /* WINED3DSIH_REP */ shader_glsl_rep,
7890 /* WINED3DSIH_RET */ shader_glsl_ret,
7891 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
7892 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
7893 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
7894 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
7895 /* WINED3DSIH_SAMPLE_LOD */ NULL,
7896 /* WINED3DSIH_SETP */ NULL,
7897 /* WINED3DSIH_SGE */ shader_glsl_compare,
7898 /* WINED3DSIH_SGN */ shader_glsl_sgn,
7899 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
7900 /* WINED3DSIH_SLT */ shader_glsl_compare,
7901 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
7902 /* WINED3DSIH_SUB */ shader_glsl_binop,
7903 /* WINED3DSIH_TEX */ shader_glsl_tex,
7904 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
7905 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
7906 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
7907 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
7908 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
7909 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
7910 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
7911 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
7912 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
7913 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
7914 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
7915 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
7916 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
7917 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
7918 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
7919 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
7920 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
7921 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
7922 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
7923 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
7924 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
7925 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
7926 /* WINED3DSIH_UGE */ shader_glsl_relop,
7927 /* WINED3DSIH_USHR */ shader_glsl_binop,
7928 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
7929 /* WINED3DSIH_XOR */ shader_glsl_binop,
7932 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
7933 SHADER_HANDLER hw_fct;
7935 /* Select handler */
7936 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
7938 /* Unhandled opcode */
7939 if (!hw_fct)
7941 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
7942 return;
7944 hw_fct(ins);
7946 shader_glsl_add_instruction_modifiers(ins);
7949 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
7951 struct shader_glsl_priv *priv = shader_priv;
7953 return priv->ffp_proj_control;
7956 const struct wined3d_shader_backend_ops glsl_shader_backend =
7958 shader_glsl_handle_instruction,
7959 shader_glsl_select,
7960 shader_glsl_disable,
7961 shader_glsl_select_depth_blt,
7962 shader_glsl_deselect_depth_blt,
7963 shader_glsl_update_float_vertex_constants,
7964 shader_glsl_update_float_pixel_constants,
7965 shader_glsl_load_constants,
7966 shader_glsl_destroy,
7967 shader_glsl_alloc,
7968 shader_glsl_free,
7969 shader_glsl_allocate_context_data,
7970 shader_glsl_free_context_data,
7971 shader_glsl_init_context_state,
7972 shader_glsl_get_caps,
7973 shader_glsl_color_fixup_supported,
7974 shader_glsl_has_ffp_proj_control,
7977 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
7979 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
7981 caps->xyzrhw = TRUE;
7982 caps->ffp_generic_attributes = TRUE;
7983 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
7984 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
7985 caps->max_vertex_blend_matrix_index = 0;
7986 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
7987 | WINED3DVTXPCAPS_MATERIALSOURCE7
7988 | WINED3DVTXPCAPS_VERTEXFOG
7989 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
7990 | WINED3DVTXPCAPS_POSITIONALLIGHTS
7991 | WINED3DVTXPCAPS_LOCALVIEWER
7992 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
7993 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
7994 caps->max_user_clip_planes = gl_info->limits.clipplanes;
7995 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
7998 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
8000 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8001 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8002 return 0;
8005 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8007 struct shader_glsl_priv *priv;
8009 if (shader_backend == &glsl_shader_backend)
8011 priv = shader_priv;
8013 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
8015 ERR("Failed to initialize rbtree.\n");
8016 return NULL;
8019 return priv;
8022 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
8024 return NULL;
8027 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
8029 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8030 struct glsl_ffp_vertex_shader, desc.entry);
8031 struct glsl_shader_prog_link *program, *program2;
8032 struct glsl_ffp_destroy_ctx *ctx = context;
8034 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8035 struct glsl_shader_prog_link, vs.shader_entry)
8037 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8039 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8040 HeapFree(GetProcessHeap(), 0, shader);
8043 /* Context activation is done by the caller. */
8044 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
8046 struct shader_glsl_priv *priv = device->vertex_priv;
8047 struct glsl_ffp_destroy_ctx ctx;
8049 ctx.priv = priv;
8050 ctx.gl_info = &device->adapter->gl_info;
8051 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
8054 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
8055 const struct wined3d_state *state, DWORD state_id)
8057 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8060 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
8061 const struct wined3d_state *state, DWORD state_id)
8063 const struct wined3d_gl_info *gl_info = context->gl_info;
8064 BOOL transformed = context->stream_info.position_transformed;
8065 BOOL wasrhw = context->last_was_rhw;
8066 unsigned int i;
8068 context->last_was_rhw = transformed;
8070 /* If the vertex declaration contains a transformed position attribute,
8071 * the draw uses the fixed function vertex pipeline regardless of any
8072 * vertex shader set by the application. */
8073 if (transformed != wasrhw)
8074 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8076 if (!use_vs(state))
8078 if (context->last_was_vshader)
8080 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8081 clipplane(context, state, STATE_CLIPPLANE(i));
8084 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8086 /* Because of settings->texcoords, we have to regenerate the vertex
8087 * shader on a vdecl change if there aren't enough varyings to just
8088 * always output all the texture coordinates. */
8089 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info))
8090 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8092 if (use_ps(state)
8093 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
8094 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
8095 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8097 else
8099 if (!context->last_was_vshader)
8101 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
8102 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8103 clipplane(context, state, STATE_CLIPPLANE(i));
8107 context->last_was_vshader = use_vs(state);
8110 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
8111 const struct wined3d_state *state, DWORD state_id)
8113 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8114 /* Different vertex shaders potentially require a different vertex attributes setup. */
8115 if (!isStateDirty(context, STATE_VDECL))
8116 context_apply_state(context, state, STATE_VDECL);
8119 static void glsl_vertex_pipe_world(struct wined3d_context *context,
8120 const struct wined3d_state *state, DWORD state_id)
8122 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
8125 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
8126 const struct wined3d_state *state, DWORD state_id)
8128 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8131 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
8133 const struct wined3d_gl_info *gl_info = context->gl_info;
8134 unsigned int k;
8136 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
8137 | WINED3D_SHADER_CONST_FFP_LIGHTS
8138 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8140 for (k = 0; k < gl_info->limits.clipplanes; ++k)
8142 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
8143 clipplane(context, state, STATE_CLIPPLANE(k));
8147 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
8148 const struct wined3d_state *state, DWORD state_id)
8150 /* Table fog behavior depends on the projection matrix. */
8151 if (state->render_states[WINED3D_RS_FOGENABLE]
8152 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
8153 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8154 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
8157 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
8158 const struct wined3d_state *state, DWORD state_id)
8160 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
8161 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
8162 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
8163 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
8164 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8165 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
8168 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
8169 const struct wined3d_state *state, DWORD state_id)
8171 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8174 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
8175 const struct wined3d_state *state, DWORD state_id)
8177 DWORD sampler = state_id - STATE_SAMPLER(0);
8178 const struct wined3d_texture *texture = state->textures[sampler];
8179 BOOL np2;
8181 if (!texture)
8182 return;
8184 if (sampler >= MAX_TEXTURES)
8185 return;
8187 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
8188 || context->lastWasPow2Texture & (1u << sampler))
8190 if (np2)
8191 context->lastWasPow2Texture |= 1u << sampler;
8192 else
8193 context->lastWasPow2Texture &= ~(1u << sampler);
8195 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8199 static void glsl_vertex_pipe_material(struct wined3d_context *context,
8200 const struct wined3d_state *state, DWORD state_id)
8202 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
8205 static void glsl_vertex_pipe_light(struct wined3d_context *context,
8206 const struct wined3d_state *state, DWORD state_id)
8208 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
8211 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
8212 const struct wined3d_state *state, DWORD state_id)
8214 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8217 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
8218 const struct wined3d_state *state, DWORD state_id)
8220 if (!use_vs(state))
8221 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8224 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
8226 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8227 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
8228 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
8229 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
8230 /* Clip planes */
8231 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
8232 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
8233 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
8234 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
8235 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
8236 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
8237 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
8238 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
8239 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
8240 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
8241 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
8242 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
8243 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
8244 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
8245 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
8246 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
8247 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
8248 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
8249 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
8250 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
8251 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
8252 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
8253 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
8254 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
8255 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
8256 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
8257 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
8258 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
8259 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
8260 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
8261 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
8262 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
8263 /* Lights */
8264 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8265 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8266 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8267 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8268 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8269 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8270 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8271 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8272 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8273 /* Viewport */
8274 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
8275 /* Transform states */
8276 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
8277 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
8278 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8279 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8280 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8281 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8282 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8283 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8284 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8285 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8286 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
8287 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8288 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8289 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8290 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8291 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8292 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8293 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8294 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8295 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8296 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8297 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8298 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8299 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8300 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8301 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8302 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8303 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8304 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8305 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8306 /* Fog */
8307 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8308 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8309 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8310 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8311 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
8312 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
8313 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8314 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8315 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8316 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8317 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8318 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8319 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8320 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8321 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8322 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8323 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
8324 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
8325 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
8326 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
8327 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
8328 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8329 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8330 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8331 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
8332 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8333 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8334 /* NP2 texture matrix fixups. They are not needed if
8335 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
8336 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
8337 * matrix. */
8338 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8339 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8340 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8341 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8342 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8343 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8344 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8345 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8346 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8347 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8348 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8349 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8350 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8351 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8352 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8353 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8354 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8355 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8356 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8357 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8358 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8359 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8360 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8361 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8362 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8363 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
8366 /* TODO:
8367 * - Implement vertex tweening. */
8368 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
8370 glsl_vertex_pipe_vp_enable,
8371 glsl_vertex_pipe_vp_get_caps,
8372 glsl_vertex_pipe_vp_get_emul_mask,
8373 glsl_vertex_pipe_vp_alloc,
8374 glsl_vertex_pipe_vp_free,
8375 glsl_vertex_pipe_vp_states,
8378 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
8380 /* Nothing to do. */
8383 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
8385 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
8386 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
8387 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
8388 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
8389 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
8390 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
8391 | WINED3DTEXOPCAPS_SELECTARG1
8392 | WINED3DTEXOPCAPS_SELECTARG2
8393 | WINED3DTEXOPCAPS_MODULATE4X
8394 | WINED3DTEXOPCAPS_MODULATE2X
8395 | WINED3DTEXOPCAPS_MODULATE
8396 | WINED3DTEXOPCAPS_ADDSIGNED2X
8397 | WINED3DTEXOPCAPS_ADDSIGNED
8398 | WINED3DTEXOPCAPS_ADD
8399 | WINED3DTEXOPCAPS_SUBTRACT
8400 | WINED3DTEXOPCAPS_ADDSMOOTH
8401 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
8402 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
8403 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
8404 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
8405 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
8406 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
8407 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
8408 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
8409 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
8410 | WINED3DTEXOPCAPS_DOTPRODUCT3
8411 | WINED3DTEXOPCAPS_MULTIPLYADD
8412 | WINED3DTEXOPCAPS_LERP
8413 | WINED3DTEXOPCAPS_BUMPENVMAP
8414 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
8415 caps->MaxTextureBlendStages = 8;
8416 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
8419 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
8421 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8422 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8423 return 0;
8426 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8428 struct shader_glsl_priv *priv;
8430 if (shader_backend == &glsl_shader_backend)
8432 priv = shader_priv;
8434 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
8436 ERR("Failed to initialize rbtree.\n");
8437 return NULL;
8440 return priv;
8443 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
8445 return NULL;
8448 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
8450 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8451 struct glsl_ffp_fragment_shader, entry.entry);
8452 struct glsl_shader_prog_link *program, *program2;
8453 struct glsl_ffp_destroy_ctx *ctx = context;
8455 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8456 struct glsl_shader_prog_link, ps.shader_entry)
8458 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8460 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8461 HeapFree(GetProcessHeap(), 0, shader);
8464 /* Context activation is done by the caller. */
8465 static void glsl_fragment_pipe_free(struct wined3d_device *device)
8467 struct shader_glsl_priv *priv = device->fragment_priv;
8468 struct glsl_ffp_destroy_ctx ctx;
8470 ctx.priv = priv;
8471 ctx.gl_info = &device->adapter->gl_info;
8472 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
8475 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
8476 const struct wined3d_state *state, DWORD state_id)
8478 context->last_was_pshader = use_ps(state);
8480 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8483 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
8484 const struct wined3d_state *state, DWORD state_id)
8486 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8489 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
8490 const struct wined3d_state *state, DWORD state_id)
8492 BOOL use_vshader = use_vs(state);
8493 enum fogsource new_source;
8494 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
8495 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
8497 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8499 if (!state->render_states[WINED3D_RS_FOGENABLE])
8500 return;
8502 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
8504 if (use_vshader)
8505 new_source = FOGSOURCE_VS;
8506 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
8507 new_source = FOGSOURCE_COORD;
8508 else
8509 new_source = FOGSOURCE_FFP;
8511 else
8513 new_source = FOGSOURCE_FFP;
8516 if (new_source != context->fog_source || fogstart == fogend)
8518 context->fog_source = new_source;
8519 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8523 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
8524 const struct wined3d_state *state, DWORD state_id)
8526 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
8527 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
8528 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8530 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
8531 glsl_fragment_pipe_fog(context, state, state_id);
8534 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
8535 const struct wined3d_state *state, DWORD state_id)
8537 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
8538 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
8539 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8542 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
8543 const struct wined3d_state *state, DWORD state_id)
8545 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8548 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
8549 const struct wined3d_state *state, DWORD state_id)
8551 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
8554 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
8555 const struct wined3d_state *state, DWORD state_id)
8557 const struct wined3d_gl_info *gl_info = context->gl_info;
8558 int glParm;
8559 float ref;
8561 TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
8563 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
8565 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
8566 checkGLcall("glEnable GL_ALPHA_TEST");
8568 else
8570 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
8571 checkGLcall("glDisable GL_ALPHA_TEST");
8572 return;
8575 ref = ((float)state->render_states[WINED3D_RS_ALPHAREF]) / 255.0f;
8576 glParm = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
8578 if (glParm)
8580 gl_info->gl_ops.gl.p_glAlphaFunc(glParm, ref);
8581 checkGLcall("glAlphaFunc");
8585 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
8586 const struct wined3d_state *state, DWORD state_id)
8588 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
8591 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
8593 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8594 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
8595 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8596 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8597 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8598 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8599 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8600 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8601 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8602 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8603 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8604 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8605 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8606 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8607 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8608 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8609 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8610 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8611 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8612 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8613 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8614 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8615 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8616 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8617 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8618 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8619 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8620 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8621 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8622 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8623 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8624 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8625 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8626 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8627 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8628 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8629 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8630 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8631 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8632 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8633 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8634 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8635 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8636 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8637 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8638 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8639 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8640 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8641 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8642 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8643 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8644 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8645 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8646 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8647 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8648 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8649 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8650 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8651 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8652 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8653 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8654 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8655 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8656 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8657 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8658 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8659 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8660 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8661 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8662 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8663 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8664 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8665 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8666 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8667 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8668 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
8669 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
8670 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
8671 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_EXT_NONE },
8672 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8673 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
8674 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
8675 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8676 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8677 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
8678 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
8679 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
8680 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8681 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
8682 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
8683 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
8684 {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 },
8685 {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 },
8686 {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 },
8687 {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 },
8688 {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 },
8689 {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 },
8690 {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 },
8691 {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 },
8692 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8693 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8694 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8695 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8696 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8697 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8698 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8699 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8700 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8701 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
8702 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
8705 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
8707 return TRUE;
8710 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
8714 const struct fragment_pipeline glsl_fragment_pipe =
8716 glsl_fragment_pipe_enable,
8717 glsl_fragment_pipe_get_caps,
8718 glsl_fragment_pipe_get_emul_mask,
8719 glsl_fragment_pipe_alloc,
8720 glsl_fragment_pipe_free,
8721 glsl_fragment_pipe_alloc_context_data,
8722 glsl_fragment_pipe_free_context_data,
8723 shader_glsl_color_fixup_supported,
8724 glsl_fragment_pipe_state_template,