wined3d: Don't try to apply format fixups for SM4+ shaders.
[wine/multimedia.git] / dlls / wined3d / glsl_shader.c
blob132ecdfdd6cc6ef49a2b63193a0fa18af089ba35
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 const char *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;
111 struct glsl_vs_program
113 struct list shader_entry;
114 GLuint id;
115 GLenum vertex_color_clamp;
116 GLint *uniform_f_locations;
117 GLint uniform_i_locations[MAX_CONST_I];
118 GLint uniform_b_locations[MAX_CONST_B];
119 GLint pos_fixup_location;
121 GLint modelview_matrix_location;
122 GLint projection_matrix_location;
123 GLint normal_matrix_location;
124 GLint texture_matrix_location[MAX_TEXTURES];
125 GLint material_ambient_location;
126 GLint material_diffuse_location;
127 GLint material_specular_location;
128 GLint material_emission_location;
129 GLint material_shininess_location;
130 GLint light_ambient_location;
131 struct
133 GLint diffuse;
134 GLint specular;
135 GLint ambient;
136 GLint position;
137 GLint direction;
138 GLint range;
139 GLint falloff;
140 GLint c_att;
141 GLint l_att;
142 GLint q_att;
143 GLint cos_htheta;
144 GLint cos_hphi;
145 } light_location[MAX_ACTIVE_LIGHTS];
148 struct glsl_gs_program
150 struct list shader_entry;
151 GLuint id;
154 struct glsl_ps_program
156 struct list shader_entry;
157 GLuint id;
158 GLint *uniform_f_locations;
159 GLint uniform_i_locations[MAX_CONST_I];
160 GLint uniform_b_locations[MAX_CONST_B];
161 GLint bumpenv_mat_location[MAX_TEXTURES];
162 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
163 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
164 GLint tss_constant_location[MAX_TEXTURES];
165 GLint tex_factor_location;
166 GLint specular_enable_location;
167 GLint ycorrection_location;
168 GLint np2_fixup_location;
169 GLint color_key_location;
170 const struct ps_np2fixup_info *np2_fixup_info;
173 /* Struct to maintain data about a linked GLSL program */
174 struct glsl_shader_prog_link
176 struct wine_rb_entry program_lookup_entry;
177 struct glsl_vs_program vs;
178 struct glsl_gs_program gs;
179 struct glsl_ps_program ps;
180 GLuint id;
181 DWORD constant_update_mask;
182 UINT constant_version;
185 struct glsl_program_key
187 GLuint vs_id;
188 GLuint gs_id;
189 GLuint ps_id;
192 struct shader_glsl_ctx_priv {
193 const struct vs_compile_args *cur_vs_args;
194 const struct ps_compile_args *cur_ps_args;
195 struct ps_np2fixup_info *cur_np2fixup_info;
198 struct glsl_context_data
200 struct glsl_shader_prog_link *glsl_program;
203 struct glsl_ps_compiled_shader
205 struct ps_compile_args args;
206 struct ps_np2fixup_info np2fixup;
207 GLuint id;
210 struct glsl_vs_compiled_shader
212 struct vs_compile_args args;
213 GLuint id;
216 struct glsl_gs_compiled_shader
218 GLuint id;
221 struct glsl_shader_private
223 union
225 struct glsl_vs_compiled_shader *vs;
226 struct glsl_gs_compiled_shader *gs;
227 struct glsl_ps_compiled_shader *ps;
228 } gl_shaders;
229 UINT num_gl_shaders, shader_array_size;
232 struct glsl_ffp_vertex_shader
234 struct wined3d_ffp_vs_desc desc;
235 GLuint id;
236 struct list linked_programs;
239 struct glsl_ffp_fragment_shader
241 struct ffp_frag_desc entry;
242 GLuint id;
243 struct list linked_programs;
246 struct glsl_ffp_destroy_ctx
248 struct shader_glsl_priv *priv;
249 const struct wined3d_gl_info *gl_info;
252 static const char *debug_gl_shader_type(GLenum type)
254 switch (type)
256 #define WINED3D_TO_STR(u) case u: return #u
257 WINED3D_TO_STR(GL_VERTEX_SHADER);
258 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
259 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
260 #undef WINED3D_TO_STR
261 default:
262 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
266 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
268 switch (type)
270 case WINED3D_SHADER_TYPE_VERTEX:
271 return "vs";
273 case WINED3D_SHADER_TYPE_GEOMETRY:
274 return "gs";
276 case WINED3D_SHADER_TYPE_PIXEL:
277 return "ps";
279 default:
280 FIXME("Unhandled shader type %#x.\n", type);
281 return "unknown";
285 static const char *shader_glsl_get_version(const struct wined3d_gl_info *gl_info,
286 const struct wined3d_shader_version *version)
288 if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30) && version->major >= 4)
289 return "#version 130";
290 else
291 return "#version 120";
294 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
296 char str[4][17];
298 wined3d_ftoa(values[0], str[0]);
299 wined3d_ftoa(values[1], str[1]);
300 wined3d_ftoa(values[2], str[2]);
301 wined3d_ftoa(values[3], str[3]);
302 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
305 static const char *get_info_log_line(const char **ptr)
307 const char *p, *q;
309 p = *ptr;
310 if (!(q = strstr(p, "\n")))
312 if (!*p) return NULL;
313 *ptr += strlen(p);
314 return p;
316 *ptr = q + 1;
318 return p;
321 /* Context activation is done by the caller. */
322 static void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
324 int length = 0;
325 char *log;
327 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
328 return;
330 if (program)
331 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
332 else
333 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
335 /* A size of 1 is just a null-terminated string, so the log should be bigger than
336 * that if there are errors. */
337 if (length > 1)
339 const char *ptr, *line;
341 log = HeapAlloc(GetProcessHeap(), 0, length);
342 /* The info log is supposed to be zero-terminated, but at least some
343 * versions of fglrx don't terminate the string properly. The reported
344 * length does include the terminator, so explicitly set it to zero
345 * here. */
346 log[length - 1] = 0;
347 if (program)
348 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
349 else
350 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
352 ptr = log;
353 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
355 WARN("Info log received from GLSL shader #%u:\n", id);
356 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
358 else
360 FIXME("Info log received from GLSL shader #%u:\n", id);
361 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
363 HeapFree(GetProcessHeap(), 0, log);
367 /* Context activation is done by the caller. */
368 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
370 const char *ptr, *line;
372 TRACE("Compiling shader object %u.\n", shader);
374 if (TRACE_ON(d3d_shader))
376 ptr = src;
377 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
380 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
381 checkGLcall("glShaderSource");
382 GL_EXTCALL(glCompileShader(shader));
383 checkGLcall("glCompileShader");
384 print_glsl_info_log(gl_info, shader, FALSE);
387 /* Context activation is done by the caller. */
388 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
390 GLint i, shader_count, source_size = -1;
391 GLuint *shaders;
392 char *source = NULL;
394 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
395 shaders = HeapAlloc(GetProcessHeap(), 0, shader_count * sizeof(*shaders));
396 if (!shaders)
398 ERR("Failed to allocate shader array memory.\n");
399 return;
402 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
403 for (i = 0; i < shader_count; ++i)
405 const char *ptr, *line;
406 GLint tmp;
408 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
410 if (source_size < tmp)
412 HeapFree(GetProcessHeap(), 0, source);
414 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
415 if (!source)
417 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
418 HeapFree(GetProcessHeap(), 0, shaders);
419 return;
421 source_size = tmp;
424 FIXME("Shader %u:\n", shaders[i]);
425 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
426 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
427 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
428 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
429 FIXME("\n");
431 ptr = source;
432 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
433 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
434 FIXME("\n");
437 HeapFree(GetProcessHeap(), 0, source);
438 HeapFree(GetProcessHeap(), 0, shaders);
441 /* Context activation is done by the caller. */
442 static void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
444 GLint tmp;
446 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
447 return;
449 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
450 if (!tmp)
452 FIXME("Program %u link status invalid.\n", program);
453 shader_glsl_dump_program_source(gl_info, program);
456 print_glsl_info_log(gl_info, program, TRUE);
459 /* Context activation is done by the caller. */
460 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
461 struct shader_glsl_priv *priv, const DWORD *tex_unit_map, GLuint program_id)
463 unsigned int mapped_unit;
464 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
465 const char *prefix;
466 unsigned int i, j;
467 GLint name_loc;
469 static const struct
471 enum wined3d_shader_type type;
472 unsigned int base_idx;
473 unsigned int count;
475 sampler_info[] =
477 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
478 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
481 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
483 prefix = shader_glsl_get_prefix(sampler_info[i].type);
485 for (j = 0; j < sampler_info[i].count; ++j)
487 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, j);
488 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
489 if (name_loc == -1)
490 continue;
492 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
493 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
495 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
496 continue;
499 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
500 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
503 checkGLcall("glUniform1i");
504 string_buffer_release(&priv->string_buffers, sampler_name);
507 /* Context activation is done by the caller. */
508 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
509 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
511 unsigned int start = ~0U, end = 0;
512 int stack_idx = 0;
513 unsigned int heap_idx = 1;
514 unsigned int idx;
516 if (heap->entries[heap_idx].version <= version) return;
518 idx = heap->entries[heap_idx].idx;
519 if (constant_locations[idx] != -1)
520 start = end = idx;
521 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
523 while (stack_idx >= 0)
525 /* Note that we fall through to the next case statement. */
526 switch(stack[stack_idx])
528 case HEAP_NODE_TRAVERSE_LEFT:
530 unsigned int left_idx = heap_idx << 1;
531 if (left_idx < heap->size && heap->entries[left_idx].version > version)
533 heap_idx = left_idx;
534 idx = heap->entries[heap_idx].idx;
535 if (constant_locations[idx] != -1)
537 if (start > idx)
538 start = idx;
539 if (end < idx)
540 end = idx;
543 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
544 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
545 break;
549 case HEAP_NODE_TRAVERSE_RIGHT:
551 unsigned int right_idx = (heap_idx << 1) + 1;
552 if (right_idx < heap->size && heap->entries[right_idx].version > version)
554 heap_idx = right_idx;
555 idx = heap->entries[heap_idx].idx;
556 if (constant_locations[idx] != -1)
558 if (start > idx)
559 start = idx;
560 if (end < idx)
561 end = idx;
564 stack[stack_idx++] = HEAP_NODE_POP;
565 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
566 break;
570 case HEAP_NODE_POP:
571 heap_idx >>= 1;
572 --stack_idx;
573 break;
576 if (start <= end)
577 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start * 4]));
578 checkGLcall("walk_constant_heap()");
581 /* Context activation is done by the caller. */
582 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
584 GLfloat clamped_constant[4];
586 if (location == -1) return;
588 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
589 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
590 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
591 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
593 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
596 /* Context activation is done by the caller. */
597 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
598 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
600 int stack_idx = 0;
601 unsigned int heap_idx = 1;
602 unsigned int idx;
604 if (heap->entries[heap_idx].version <= version) return;
606 idx = heap->entries[heap_idx].idx;
607 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
608 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
610 while (stack_idx >= 0)
612 /* Note that we fall through to the next case statement. */
613 switch(stack[stack_idx])
615 case HEAP_NODE_TRAVERSE_LEFT:
617 unsigned int left_idx = heap_idx << 1;
618 if (left_idx < heap->size && heap->entries[left_idx].version > version)
620 heap_idx = left_idx;
621 idx = heap->entries[heap_idx].idx;
622 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
624 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
625 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
626 break;
630 case HEAP_NODE_TRAVERSE_RIGHT:
632 unsigned int right_idx = (heap_idx << 1) + 1;
633 if (right_idx < heap->size && heap->entries[right_idx].version > version)
635 heap_idx = right_idx;
636 idx = heap->entries[heap_idx].idx;
637 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
639 stack[stack_idx++] = HEAP_NODE_POP;
640 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
641 break;
645 case HEAP_NODE_POP:
646 heap_idx >>= 1;
647 --stack_idx;
648 break;
651 checkGLcall("walk_constant_heap_clamped()");
654 /* Context activation is done by the caller. */
655 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
656 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
657 unsigned char *stack, UINT version)
659 const struct wined3d_shader_lconst *lconst;
661 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
662 if (shader->reg_maps.shader_version.major == 1
663 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
664 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
665 else
666 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
668 if (!shader->load_local_constsF)
670 TRACE("No need to load local float constants for this shader\n");
671 return;
674 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
675 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
677 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
679 checkGLcall("glUniform4fv()");
682 /* Context activation is done by the caller. */
683 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
684 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
686 unsigned int i;
687 struct list* ptr;
689 for (i = 0; constants_set; constants_set >>= 1, ++i)
691 if (!(constants_set & 1)) continue;
693 /* We found this uniform name in the program - go ahead and send the data */
694 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i * 4]));
697 /* Load immediate constants */
698 ptr = list_head(&shader->constantsI);
699 while (ptr)
701 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
702 unsigned int idx = lconst->idx;
703 const GLint *values = (const GLint *)lconst->value;
705 /* We found this uniform name in the program - go ahead and send the data */
706 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
707 ptr = list_next(&shader->constantsI, ptr);
709 checkGLcall("glUniform4iv()");
712 /* Context activation is done by the caller. */
713 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
714 const GLint locations[MAX_CONST_B], const BOOL *constants, WORD constants_set)
716 unsigned int i;
717 struct list* ptr;
719 for (i = 0; constants_set; constants_set >>= 1, ++i)
721 if (!(constants_set & 1)) continue;
723 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
726 /* Load immediate constants */
727 ptr = list_head(&shader->constantsB);
728 while (ptr)
730 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
731 unsigned int idx = lconst->idx;
732 const GLint *values = (const GLint *)lconst->value;
734 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
735 ptr = list_next(&shader->constantsB, ptr);
737 checkGLcall("glUniform1iv()");
740 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
742 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
745 /* Context activation is done by the caller (state handler). */
746 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
747 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
749 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
750 UINT fixup = ps->np2_fixup_info->active;
751 UINT i;
753 for (i = 0; fixup; fixup >>= 1, ++i)
755 const struct wined3d_texture *tex = state->textures[i];
756 unsigned char idx = ps->np2_fixup_info->idx[i];
757 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
759 if (!tex)
761 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
762 continue;
765 if (idx % 2)
767 tex_dim[2] = tex->pow2_matrix[0];
768 tex_dim[3] = tex->pow2_matrix[5];
770 else
772 tex_dim[0] = tex->pow2_matrix[0];
773 tex_dim[1] = tex->pow2_matrix[5];
777 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
780 /* Taken and adapted from Mesa. */
781 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
783 float pos, neg, t, det;
784 struct wined3d_matrix temp;
786 /* Calculate the determinant of upper left 3x3 submatrix and
787 * determine if the matrix is singular. */
788 pos = neg = 0.0f;
789 t = in->_11 * in->_22 * in->_33;
790 if (t >= 0.0f)
791 pos += t;
792 else
793 neg += t;
795 t = in->_21 * in->_32 * in->_13;
796 if (t >= 0.0f)
797 pos += t;
798 else
799 neg += t;
800 t = in->_31 * in->_12 * in->_23;
801 if (t >= 0.0f)
802 pos += t;
803 else
804 neg += t;
806 t = -in->_31 * in->_22 * in->_13;
807 if (t >= 0.0f)
808 pos += t;
809 else
810 neg += t;
811 t = -in->_21 * in->_12 * in->_33;
812 if (t >= 0.0f)
813 pos += t;
814 else
815 neg += t;
817 t = -in->_11 * in->_32 * in->_23;
818 if (t >= 0.0f)
819 pos += t;
820 else
821 neg += t;
823 det = pos + neg;
825 if (fabsf(det) < 1e-25f)
826 return FALSE;
828 det = 1.0f / det;
829 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
830 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
831 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
832 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
833 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
834 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
835 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
836 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
837 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
839 *out = temp;
840 return TRUE;
843 static void swap_rows(float **a, float **b)
845 float *tmp = *a;
847 *a = *b;
848 *b = tmp;
851 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
853 float wtmp[4][8];
854 float m0, m1, m2, m3, s;
855 float *r0, *r1, *r2, *r3;
857 r0 = wtmp[0];
858 r1 = wtmp[1];
859 r2 = wtmp[2];
860 r3 = wtmp[3];
862 r0[0] = m->_11;
863 r0[1] = m->_12;
864 r0[2] = m->_13;
865 r0[3] = m->_14;
866 r0[4] = 1.0f;
867 r0[5] = r0[6] = r0[7] = 0.0f;
869 r1[0] = m->_21;
870 r1[1] = m->_22;
871 r1[2] = m->_23;
872 r1[3] = m->_24;
873 r1[5] = 1.0f;
874 r1[4] = r1[6] = r1[7] = 0.0f;
876 r2[0] = m->_31;
877 r2[1] = m->_32;
878 r2[2] = m->_33;
879 r2[3] = m->_34;
880 r2[6] = 1.0f;
881 r2[4] = r2[5] = r2[7] = 0.0f;
883 r3[0] = m->_41;
884 r3[1] = m->_42;
885 r3[2] = m->_43;
886 r3[3] = m->_44;
887 r3[7] = 1.0f;
888 r3[4] = r3[5] = r3[6] = 0.0f;
890 /* Choose pivot - or die. */
891 if (fabsf(r3[0]) > fabsf(r2[0]))
892 swap_rows(&r3, &r2);
893 if (fabsf(r2[0]) > fabsf(r1[0]))
894 swap_rows(&r2, &r1);
895 if (fabsf(r1[0]) > fabsf(r0[0]))
896 swap_rows(&r1, &r0);
897 if (r0[0] == 0.0f)
898 return FALSE;
900 /* Eliminate first variable. */
901 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
902 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
903 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
904 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
905 s = r0[4];
906 if (s != 0.0f)
908 r1[4] -= m1 * s;
909 r2[4] -= m2 * s;
910 r3[4] -= m3 * s;
912 s = r0[5];
913 if (s != 0.0f)
915 r1[5] -= m1 * s;
916 r2[5] -= m2 * s;
917 r3[5] -= m3 * s;
919 s = r0[6];
920 if (s != 0.0f)
922 r1[6] -= m1 * s;
923 r2[6] -= m2 * s;
924 r3[6] -= m3 * s;
926 s = r0[7];
927 if (s != 0.0f)
929 r1[7] -= m1 * s;
930 r2[7] -= m2 * s;
931 r3[7] -= m3 * s;
934 /* Choose pivot - or die. */
935 if (fabsf(r3[1]) > fabsf(r2[1]))
936 swap_rows(&r3, &r2);
937 if (fabsf(r2[1]) > fabsf(r1[1]))
938 swap_rows(&r2, &r1);
939 if (r1[1] == 0.0f)
940 return FALSE;
942 /* Eliminate second variable. */
943 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
944 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
945 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
946 s = r1[4];
947 if (s != 0.0f)
949 r2[4] -= m2 * s;
950 r3[4] -= m3 * s;
952 s = r1[5];
953 if (s != 0.0f)
955 r2[5] -= m2 * s;
956 r3[5] -= m3 * s;
958 s = r1[6];
959 if (s != 0.0f)
961 r2[6] -= m2 * s;
962 r3[6] -= m3 * s;
964 s = r1[7];
965 if (s != 0.0f)
967 r2[7] -= m2 * s;
968 r3[7] -= m3 * s;
971 /* Choose pivot - or die. */
972 if (fabsf(r3[2]) > fabsf(r2[2]))
973 swap_rows(&r3, &r2);
974 if (r2[2] == 0.0f)
975 return FALSE;
977 /* Eliminate third variable. */
978 m3 = r3[2] / r2[2];
979 r3[3] -= m3 * r2[3];
980 r3[4] -= m3 * r2[4];
981 r3[5] -= m3 * r2[5];
982 r3[6] -= m3 * r2[6];
983 r3[7] -= m3 * r2[7];
985 /* Last check. */
986 if (r3[3] == 0.0f)
987 return FALSE;
989 /* Back substitute row 3. */
990 s = 1.0f / r3[3];
991 r3[4] *= s;
992 r3[5] *= s;
993 r3[6] *= s;
994 r3[7] *= s;
996 /* Back substitute row 2. */
997 m2 = r2[3];
998 s = 1.0f / r2[2];
999 r2[4] = s * (r2[4] - r3[4] * m2);
1000 r2[5] = s * (r2[5] - r3[5] * m2);
1001 r2[6] = s * (r2[6] - r3[6] * m2);
1002 r2[7] = s * (r2[7] - r3[7] * m2);
1003 m1 = r1[3];
1004 r1[4] -= r3[4] * m1;
1005 r1[5] -= r3[5] * m1;
1006 r1[6] -= r3[6] * m1;
1007 r1[7] -= r3[7] * m1;
1008 m0 = r0[3];
1009 r0[4] -= r3[4] * m0;
1010 r0[5] -= r3[5] * m0;
1011 r0[6] -= r3[6] * m0;
1012 r0[7] -= r3[7] * m0;
1014 /* Back substitute row 1. */
1015 m1 = r1[2];
1016 s = 1.0f / r1[1];
1017 r1[4] = s * (r1[4] - r2[4] * m1);
1018 r1[5] = s * (r1[5] - r2[5] * m1);
1019 r1[6] = s * (r1[6] - r2[6] * m1);
1020 r1[7] = s * (r1[7] - r2[7] * m1);
1021 m0 = r0[2];
1022 r0[4] -= r2[4] * m0;
1023 r0[5] -= r2[5] * m0;
1024 r0[6] -= r2[6] * m0;
1025 r0[7] -= r2[7] * m0;
1027 /* Back substitute row 0. */
1028 m0 = r0[1];
1029 s = 1.0f / r0[0];
1030 r0[4] = s * (r0[4] - r1[4] * m0);
1031 r0[5] = s * (r0[5] - r1[5] * m0);
1032 r0[6] = s * (r0[6] - r1[6] * m0);
1033 r0[7] = s * (r0[7] - r1[7] * m0);
1035 out->_11 = r0[4];
1036 out->_12 = r0[5];
1037 out->_13 = r0[6];
1038 out->_14 = r0[7];
1039 out->_21 = r1[4];
1040 out->_22 = r1[5];
1041 out->_23 = r1[6];
1042 out->_24 = r1[7];
1043 out->_31 = r2[4];
1044 out->_32 = r2[5];
1045 out->_33 = r2[6];
1046 out->_34 = r2[7];
1047 out->_41 = r3[4];
1048 out->_42 = r3[5];
1049 out->_43 = r3[6];
1050 out->_44 = r3[7];
1052 return TRUE;
1055 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1056 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1058 const struct wined3d_gl_info *gl_info = context->gl_info;
1059 float mat[3 * 3];
1060 struct wined3d_matrix mv;
1061 unsigned int i, j;
1063 get_modelview_matrix(context, state, &mv);
1064 if (context->swapchain->device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING)
1065 invert_matrix_3d(&mv, &mv);
1066 else
1067 invert_matrix(&mv, &mv);
1068 /* Tests show that singular modelview matrices are used unchanged as normal
1069 * matrices on D3D3 and older. There seems to be no clearly consistent
1070 * behavior on newer D3D versions so always follow older ddraw behavior. */
1071 for (i = 0; i < 3; ++i)
1072 for (j = 0; j < 3; ++j)
1073 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1075 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1076 checkGLcall("glUniformMatrix3fv");
1079 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1080 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1082 const struct wined3d_gl_info *gl_info = context->gl_info;
1083 struct wined3d_matrix mat;
1085 if (tex >= MAX_TEXTURES)
1086 return;
1087 if (prog->vs.texture_matrix_location[tex] == -1)
1088 return;
1090 get_texture_matrix(context, state, tex, &mat);
1091 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1092 checkGLcall("glUniformMatrix4fv");
1095 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1096 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1098 const struct wined3d_gl_info *gl_info = context->gl_info;
1100 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1102 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1103 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1105 else
1107 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1109 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1111 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1112 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1113 GL_EXTCALL(glUniform4fv(prog->vs.material_emission_location, 1, &state->material.emissive.r));
1114 checkGLcall("setting FFP material uniforms");
1117 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1118 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1120 const struct wined3d_gl_info *gl_info = context->gl_info;
1121 float col[4];
1123 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_AMBIENT], col);
1124 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, col));
1125 checkGLcall("glUniform3fv");
1128 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1129 const struct wined3d_matrix *src2)
1131 struct wined3d_vec4 temp;
1133 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1134 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1135 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1136 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1138 *dest = temp;
1141 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1142 const struct wined3d_state *state, unsigned int light, struct glsl_shader_prog_link *prog)
1144 const struct wined3d_gl_info *gl_info = context->gl_info;
1145 const struct wined3d_light_info *light_info = state->lights[light];
1146 struct wined3d_vec4 vec4;
1147 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1149 if (!light_info)
1150 return;
1152 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1153 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1154 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1156 switch (light_info->OriginalParms.type)
1158 case WINED3D_LIGHT_POINT:
1159 multiply_vector_matrix(&vec4, &light_info->position, view);
1160 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1161 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1162 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1163 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1164 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1165 break;
1167 case WINED3D_LIGHT_SPOT:
1168 multiply_vector_matrix(&vec4, &light_info->position, view);
1169 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1171 multiply_vector_matrix(&vec4, &light_info->direction, view);
1172 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1174 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1175 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1176 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1177 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1178 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1179 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1180 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1181 break;
1183 case WINED3D_LIGHT_DIRECTIONAL:
1184 multiply_vector_matrix(&vec4, &light_info->direction, view);
1185 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1186 break;
1188 default:
1189 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1191 checkGLcall("setting FFP lights uniforms");
1194 /* Context activation is done by the caller (state handler). */
1195 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1196 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1198 struct wined3d_color float_key;
1199 const struct wined3d_texture *texture = state->textures[0];
1201 wined3d_format_convert_color_to_float(texture->resource.format, NULL,
1202 texture->async.src_blt_color_key.color_space_high_value, &float_key);
1203 GL_EXTCALL(glUniform4fv(ps->color_key_location, 1, &float_key.r));
1206 /* Context activation is done by the caller (state handler). */
1207 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1208 const struct wined3d_state *state)
1210 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1211 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1212 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1213 const struct wined3d_gl_info *gl_info = context->gl_info;
1214 struct shader_glsl_priv *priv = shader_priv;
1215 float position_fixup[4];
1216 DWORD update_mask = 0;
1218 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1219 UINT constant_version;
1220 int i;
1222 if (!prog) {
1223 /* No GLSL program set - nothing to do. */
1224 return;
1226 constant_version = prog->constant_version;
1227 update_mask = context->constant_update_mask & prog->constant_update_mask;
1229 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1230 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
1231 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1233 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1234 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
1235 vshader->reg_maps.integer_constants);
1237 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1238 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1239 vshader->reg_maps.boolean_constants);
1241 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
1243 shader_get_position_fixup(context, state, position_fixup);
1244 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1245 checkGLcall("glUniform4fv");
1248 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1250 struct wined3d_matrix mat;
1252 get_modelview_matrix(context, state, &mat);
1253 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location, 1, FALSE, &mat._11));
1254 checkGLcall("glUniformMatrix4fv");
1256 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1259 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1261 struct wined3d_matrix projection;
1263 get_projection_matrix(context, state, &projection);
1264 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1265 checkGLcall("glUniformMatrix4fv");
1268 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1270 for (i = 0; i < MAX_TEXTURES; ++i)
1271 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1274 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1275 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1277 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1279 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1280 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1281 shader_glsl_ffp_vertex_light_uniform(context, state, i, prog);
1284 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1285 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
1286 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1288 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1289 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
1290 pshader->reg_maps.integer_constants);
1292 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1293 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1294 pshader->reg_maps.boolean_constants);
1296 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1298 for (i = 0; i < MAX_TEXTURES; ++i)
1300 if (prog->ps.bumpenv_mat_location[i] == -1)
1301 continue;
1303 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1304 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1306 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1308 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1309 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1310 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1311 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1315 checkGLcall("bump env uniforms");
1318 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1320 float correction_params[] =
1322 /* position is window relative, not viewport relative */
1323 context->render_offscreen ? 0.0f : (float)context->current_rt->resource.height,
1324 context->render_offscreen ? 1.0f : -1.0f,
1325 0.0f,
1326 0.0f,
1329 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, correction_params));
1332 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1333 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1334 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1335 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1337 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1339 float col[4];
1341 if (prog->ps.tex_factor_location != -1)
1343 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_TEXTUREFACTOR], col);
1344 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, col));
1347 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1348 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1349 else
1350 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1352 for (i = 0; i < MAX_TEXTURES; ++i)
1354 if (prog->ps.tss_constant_location[i] == -1)
1355 continue;
1357 D3DCOLORTOGLFLOAT4(state->texture_states[i][WINED3D_TSS_CONSTANT], col);
1358 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, col));
1361 checkGLcall("fixed function uniforms");
1364 if (priv->next_constant_version == UINT_MAX)
1366 TRACE("Max constant version reached, resetting to 0.\n");
1367 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1368 priv->next_constant_version = 1;
1370 else
1372 prog->constant_version = priv->next_constant_version++;
1376 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1378 struct constant_entry *entries = heap->entries;
1379 unsigned int *positions = heap->positions;
1380 unsigned int heap_idx, parent_idx;
1382 if (!heap->contained[idx])
1384 heap_idx = heap->size++;
1385 heap->contained[idx] = TRUE;
1387 else
1389 heap_idx = positions[idx];
1392 while (heap_idx > 1)
1394 parent_idx = heap_idx >> 1;
1396 if (new_version <= entries[parent_idx].version) break;
1398 entries[heap_idx] = entries[parent_idx];
1399 positions[entries[parent_idx].idx] = heap_idx;
1400 heap_idx = parent_idx;
1403 entries[heap_idx].version = new_version;
1404 entries[heap_idx].idx = idx;
1405 positions[idx] = heap_idx;
1408 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1410 struct shader_glsl_priv *priv = device->shader_priv;
1411 struct constant_heap *heap = &priv->vconst_heap;
1412 UINT i;
1414 for (i = start; i < count + start; ++i)
1416 update_heap_entry(heap, i, priv->next_constant_version);
1419 for (i = 0; i < device->context_count; ++i)
1421 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
1425 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1427 struct shader_glsl_priv *priv = device->shader_priv;
1428 struct constant_heap *heap = &priv->pconst_heap;
1429 UINT i;
1431 for (i = start; i < count + start; ++i)
1433 update_heap_entry(heap, i, priv->next_constant_version);
1436 for (i = 0; i < device->context_count; ++i)
1438 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
1442 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1444 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1445 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1446 if(shader_major > 3) return ret;
1448 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1449 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1450 return ret;
1453 /** Generate the variable & register declarations for the GLSL output target */
1454 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1455 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1456 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1458 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1459 const struct wined3d_state *state = &shader->device->state;
1460 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1461 const struct wined3d_gl_info *gl_info = context->gl_info;
1462 const struct wined3d_fb_state *fb = &shader->device->fb;
1463 unsigned int i, extra_constants_needed = 0;
1464 const struct wined3d_shader_lconst *lconst;
1465 const char *prefix;
1466 DWORD map;
1468 prefix = shader_glsl_get_prefix(version->type);
1470 /* Prototype the subroutines */
1471 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1473 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1476 /* Declare the constants (aka uniforms) */
1477 if (shader->limits->constant_float > 0)
1479 unsigned max_constantsF;
1481 /* Unless the shader uses indirect addressing, always declare the
1482 * maximum array size and ignore that we need some uniforms privately.
1483 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1484 * and immediate values, still declare VC[256]. If the shader needs
1485 * more uniforms than we have it won't work in any case. If it uses
1486 * less, the compiler will figure out which uniforms are really used
1487 * and strip them out. This allows a shader to use c255 on a dx9 card,
1488 * as long as it doesn't also use all the other constants.
1490 * If the shader uses indirect addressing the compiler must assume
1491 * that all declared uniforms are used. In this case, declare only the
1492 * amount that we're assured to have.
1494 * Thus we run into problems in these two cases:
1495 * 1) The shader really uses more uniforms than supported.
1496 * 2) The shader uses indirect addressing, less constants than
1497 * supported, but uses a constant index > #supported consts. */
1498 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1500 /* No indirect addressing here. */
1501 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1503 else
1505 if (reg_maps->usesrelconstF)
1507 /* Subtract the other potential uniforms from the max
1508 * available (bools, ints, and 1 row of projection matrix).
1509 * Subtract another uniform for immediate values, which have
1510 * to be loaded via uniform by the driver as well. The shader
1511 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1512 * shader code, so one vec4 should be enough. (Unfortunately
1513 * the Nvidia driver doesn't store 128 and -128 in one float).
1515 * Writing gl_ClipVertex requires one uniform for each
1516 * clipplane as well. */
1517 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1518 if(ctx_priv->cur_vs_args->clip_enabled)
1520 max_constantsF -= gl_info->limits.clipplanes;
1522 max_constantsF -= count_bits(reg_maps->integer_constants);
1523 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1524 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1525 * for now take this into account when calculating the number of available constants
1527 max_constantsF -= count_bits(reg_maps->boolean_constants);
1528 /* Set by driver quirks in directx.c */
1529 max_constantsF -= gl_info->reserved_glsl_constants;
1531 if (max_constantsF < shader->limits->constant_float)
1533 static unsigned int once;
1535 if (!once++)
1536 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1537 " it may not render correctly.\n");
1538 else
1539 WARN("The hardware does not support enough uniform components to run this shader.\n");
1542 else
1544 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1547 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1548 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1551 /* Always declare the full set of constants, the compiler can remove the
1552 * unused ones because d3d doesn't (yet) support indirect int and bool
1553 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1554 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1555 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1557 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1558 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1560 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1562 if (reg_maps->cb_sizes[i])
1563 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1564 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1567 /* Declare texture samplers */
1568 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1570 struct wined3d_shader_sampler_map_entry *entry;
1571 BOOL shadow_sampler, tex_rect;
1572 const char *sampler_type;
1574 entry = &reg_maps->sampler_map.entries[i];
1576 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1578 ERR("Invalid resource index %u.\n", entry->resource_idx);
1579 continue;
1582 shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1 << entry->sampler_idx));
1583 switch (reg_maps->resource_info[entry->resource_idx].type)
1585 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1586 if (shadow_sampler)
1587 sampler_type = "sampler1DShadow";
1588 else
1589 sampler_type = "sampler1D";
1590 break;
1592 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1593 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1594 && (ps_args->np2_fixup & (1 << entry->resource_idx))
1595 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1596 if (shadow_sampler)
1598 if (tex_rect)
1599 sampler_type = "sampler2DRectShadow";
1600 else
1601 sampler_type = "sampler2DShadow";
1603 else
1605 if (tex_rect)
1606 sampler_type = "sampler2DRect";
1607 else
1608 sampler_type = "sampler2D";
1610 break;
1612 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1613 if (shadow_sampler)
1614 FIXME("Unsupported 3D shadow sampler.\n");
1615 sampler_type = "sampler3D";
1616 break;
1618 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1619 if (shadow_sampler)
1620 FIXME("Unsupported Cube shadow sampler.\n");
1621 sampler_type = "samplerCube";
1622 break;
1624 default:
1625 sampler_type = "unsupported_sampler";
1626 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[i].type);
1627 break;
1629 shader_addline(buffer, "uniform %s %s_sampler%u;\n", sampler_type, prefix, entry->bind_idx);
1632 /* Declare uniforms for NP2 texcoord fixup:
1633 * This is NOT done inside the loop that declares the texture samplers
1634 * since the NP2 fixup code is currently only used for the GeforceFX
1635 * series and when forcing the ARB_npot extension off. Modern cards just
1636 * skip the code anyway, so put it inside a separate loop. */
1637 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1639 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1640 UINT cur = 0;
1642 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1643 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1644 * samplerNP2Fixup stores texture dimensions and is updated through
1645 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1647 for (i = 0; i < shader->limits->sampler; ++i)
1649 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1 << i)))
1650 continue;
1652 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1654 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1655 continue;
1658 fixup->idx[i] = cur++;
1661 fixup->num_consts = (cur + 1) >> 1;
1662 fixup->active = ps_args->np2_fixup;
1663 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1666 /* Declare address variables */
1667 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1669 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1672 /* Declare texture coordinate temporaries and initialize them */
1673 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1675 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1678 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1680 for (i = 0; i < shader->input_signature.element_count; ++i)
1682 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
1683 if (e->sysval_semantic == WINED3D_SV_INSTANCEID)
1684 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1685 prefix, e->register_idx);
1686 else
1687 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, e->register_idx);
1690 shader_addline(buffer, "uniform vec4 posFixup;\n");
1691 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits->packed_output);
1693 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1695 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
1697 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1699 if (version->major >= 3)
1701 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
1703 if (use_vs(state))
1704 shader_addline(buffer, "varying vec4 %s_link[%u];\n", prefix, in_count);
1705 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1708 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1710 if (!(map & 1))
1711 continue;
1713 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1715 if (reg_maps->luminanceparams & (1 << i))
1717 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1718 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1719 extra_constants_needed++;
1722 extra_constants_needed++;
1725 if (ps_args->srgb_correction)
1727 shader_addline(buffer, "const vec4 srgb_const0 = ");
1728 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
1729 shader_addline(buffer, ";\n");
1730 shader_addline(buffer, "const vec4 srgb_const1 = ");
1731 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
1732 shader_addline(buffer, ";\n");
1734 if (reg_maps->vpos || reg_maps->usesdsy)
1736 if (shader->limits->constant_float + extra_constants_needed
1737 + 1 < gl_info->limits.glsl_ps_float_constants)
1739 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1740 extra_constants_needed++;
1742 else
1744 float ycorrection[] =
1746 context->render_offscreen ? 0.0f : fb->render_targets[0]->height,
1747 context->render_offscreen ? 1.0f : -1.0f,
1748 0.0f,
1749 0.0f,
1752 /* This happens because we do not have proper tracking of the
1753 * constant registers that are actually used, only the max
1754 * limit of the shader version. */
1755 FIXME("Cannot find a free uniform for vpos correction params\n");
1756 shader_addline(buffer, "const vec4 ycorrection = ");
1757 shader_glsl_append_imm_vec4(buffer, ycorrection);
1758 shader_addline(buffer, ";\n");
1760 shader_addline(buffer, "vec4 vpos;\n");
1764 /* Declare output register temporaries */
1765 if (shader->limits->packed_output)
1766 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
1768 /* Declare temporary variables */
1769 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1771 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1774 /* Declare loop registers aLx */
1775 if (version->major < 4)
1777 for (i = 0; i < reg_maps->loop_depth; ++i)
1779 shader_addline(buffer, "int aL%u;\n", i);
1780 shader_addline(buffer, "int tmpInt%u;\n", i);
1784 /* Temporary variables for matrix operations */
1785 shader_addline(buffer, "vec4 tmp0;\n");
1786 shader_addline(buffer, "vec4 tmp1;\n");
1788 if (!shader->load_local_constsF)
1790 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1792 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
1793 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
1794 shader_addline(buffer, ";\n");
1798 /* Start the main program. */
1799 shader_addline(buffer, "void main()\n{\n");
1801 /* Direct3D applications expect integer vPos values, while OpenGL drivers
1802 * add approximately 0.5. This causes off-by-one problems as spotted by
1803 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
1804 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
1805 * causes precision troubles when we just subtract 0.5.
1807 * To deal with that, just floor() the position. This will eliminate the
1808 * fraction on all cards.
1810 * TODO: Test how this behaves with multisampling.
1812 * An advantage of floor is that it works even if the driver doesn't add
1813 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
1814 * to return in gl_FragCoord, even though coordinates specify the pixel
1815 * centers instead of the pixel corners. This code will behave correctly
1816 * on drivers that returns integer values. */
1817 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
1819 if (shader->device->wined3d->flags & WINED3D_PIXEL_CENTER_INTEGER)
1820 shader_addline(buffer,
1821 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1822 else
1823 shader_addline(buffer,
1824 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
1828 /*****************************************************************************
1829 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1831 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1832 ****************************************************************************/
1834 /* Prototypes */
1835 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1836 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1838 /** Used for opcode modifiers - They multiply the result by the specified amount */
1839 static const char * const shift_glsl_tab[] = {
1840 "", /* 0 (none) */
1841 "2.0 * ", /* 1 (x2) */
1842 "4.0 * ", /* 2 (x4) */
1843 "8.0 * ", /* 3 (x8) */
1844 "16.0 * ", /* 4 (x16) */
1845 "32.0 * ", /* 5 (x32) */
1846 "", /* 6 (x64) */
1847 "", /* 7 (x128) */
1848 "", /* 8 (d256) */
1849 "", /* 9 (d128) */
1850 "", /* 10 (d64) */
1851 "", /* 11 (d32) */
1852 "0.0625 * ", /* 12 (d16) */
1853 "0.125 * ", /* 13 (d8) */
1854 "0.25 * ", /* 14 (d4) */
1855 "0.5 * " /* 15 (d2) */
1858 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1859 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1860 const char *in_reg, const char *in_regswizzle, char *out_str)
1862 out_str[0] = 0;
1864 switch (src_modifier)
1866 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1867 case WINED3DSPSM_DW:
1868 case WINED3DSPSM_NONE:
1869 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1870 break;
1871 case WINED3DSPSM_NEG:
1872 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1873 break;
1874 case WINED3DSPSM_NOT:
1875 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1876 break;
1877 case WINED3DSPSM_BIAS:
1878 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1879 break;
1880 case WINED3DSPSM_BIASNEG:
1881 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1882 break;
1883 case WINED3DSPSM_SIGN:
1884 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1885 break;
1886 case WINED3DSPSM_SIGNNEG:
1887 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1888 break;
1889 case WINED3DSPSM_COMP:
1890 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1891 break;
1892 case WINED3DSPSM_X2:
1893 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1894 break;
1895 case WINED3DSPSM_X2NEG:
1896 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1897 break;
1898 case WINED3DSPSM_ABS:
1899 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1900 break;
1901 case WINED3DSPSM_ABSNEG:
1902 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1903 break;
1904 default:
1905 FIXME("Unhandled modifier %u\n", src_modifier);
1906 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1910 /** Writes the GLSL variable name that corresponds to the register that the
1911 * DX opcode parameter is trying to access */
1912 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1913 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1915 /* oPos, oFog and oPts in D3D */
1916 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
1918 const struct wined3d_shader *shader = ins->ctx->shader;
1919 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1920 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1921 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1922 const char *prefix = shader_glsl_get_prefix(version->type);
1923 struct glsl_src_param rel_param0, rel_param1;
1924 char imm_str[4][17];
1926 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
1927 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
1928 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
1929 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
1930 *is_color = FALSE;
1932 switch (reg->type)
1934 case WINED3DSPR_TEMP:
1935 sprintf(register_name, "R%u", reg->idx[0].offset);
1936 break;
1938 case WINED3DSPR_INPUT:
1939 /* vertex shaders */
1940 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1942 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1943 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx[0].offset))
1944 *is_color = TRUE;
1945 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
1946 break;
1949 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1951 if (reg->idx[0].rel_addr)
1953 if (reg->idx[1].rel_addr)
1954 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
1955 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1956 else
1957 sprintf(register_name, "gs_in[%s + %u][%u]",
1958 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
1960 else if (reg->idx[1].rel_addr)
1961 sprintf(register_name, "gs_in[%u][%s + %u]",
1962 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1963 else
1964 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
1965 break;
1968 /* pixel shaders >= 3.0 */
1969 if (version->major >= 3)
1971 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
1972 unsigned int in_count = vec4_varyings(version->major, gl_info);
1974 if (reg->idx[0].rel_addr)
1976 /* Removing a + 0 would be an obvious optimization, but
1977 * OS X doesn't see the NOP operation there. */
1978 if (idx)
1980 if (shader->u.ps.declared_in_count > in_count)
1982 sprintf(register_name,
1983 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
1984 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
1985 prefix, rel_param0.param_str, idx);
1987 else
1989 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
1992 else
1994 if (shader->u.ps.declared_in_count > in_count)
1996 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
1997 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
1998 prefix, rel_param0.param_str);
2000 else
2002 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2006 else
2008 if (idx == in_count) sprintf(register_name, "gl_Color");
2009 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2010 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2013 else
2015 if (!reg->idx[0].offset)
2016 strcpy(register_name, "gl_Color");
2017 else
2018 strcpy(register_name, "gl_SecondaryColor");
2019 break;
2021 break;
2023 case WINED3DSPR_CONST:
2025 /* Relative addressing */
2026 if (reg->idx[0].rel_addr)
2028 if (reg->idx[0].offset)
2029 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2030 else
2031 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2033 else
2035 if (shader_constant_is_local(shader, reg->idx[0].offset))
2036 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2037 else
2038 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2041 break;
2043 case WINED3DSPR_CONSTINT:
2044 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2045 break;
2047 case WINED3DSPR_CONSTBOOL:
2048 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2049 break;
2051 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2052 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2053 sprintf(register_name, "T%u", reg->idx[0].offset);
2054 else
2055 sprintf(register_name, "A%u", reg->idx[0].offset);
2056 break;
2058 case WINED3DSPR_LOOP:
2059 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
2060 break;
2062 case WINED3DSPR_SAMPLER:
2063 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2064 break;
2066 case WINED3DSPR_COLOROUT:
2067 if (reg->idx[0].offset >= gl_info->limits.buffers)
2068 WARN("Write to render target %u, only %d supported.\n",
2069 reg->idx[0].offset, gl_info->limits.buffers);
2071 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
2072 break;
2074 case WINED3DSPR_RASTOUT:
2075 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2076 break;
2078 case WINED3DSPR_DEPTHOUT:
2079 sprintf(register_name, "gl_FragDepth");
2080 break;
2082 case WINED3DSPR_ATTROUT:
2083 if (!reg->idx[0].offset)
2084 sprintf(register_name, "%s_out[8]", prefix);
2085 else
2086 sprintf(register_name, "%s_out[9]", prefix);
2087 break;
2089 case WINED3DSPR_TEXCRDOUT:
2090 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2091 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2092 break;
2094 case WINED3DSPR_MISCTYPE:
2095 if (!reg->idx[0].offset)
2097 /* vPos */
2098 sprintf(register_name, "vpos");
2100 else if (reg->idx[0].offset == 1)
2102 /* Note that gl_FrontFacing is a bool, while vFace is
2103 * a float for which the sign determines front/back */
2104 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2106 else
2108 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2109 sprintf(register_name, "unrecognized_register");
2111 break;
2113 case WINED3DSPR_IMMCONST:
2114 switch (reg->immconst_type)
2116 case WINED3D_IMMCONST_SCALAR:
2117 switch (reg->data_type)
2119 case WINED3D_DATA_FLOAT:
2120 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
2121 break;
2122 case WINED3D_DATA_INT:
2123 sprintf(register_name, "%#x", reg->immconst_data[0]);
2124 break;
2125 case WINED3D_DATA_RESOURCE:
2126 case WINED3D_DATA_SAMPLER:
2127 case WINED3D_DATA_UINT:
2128 sprintf(register_name, "%#xu", reg->immconst_data[0]);
2129 break;
2130 default:
2131 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2132 break;
2134 break;
2136 case WINED3D_IMMCONST_VEC4:
2137 switch (reg->data_type)
2139 case WINED3D_DATA_FLOAT:
2140 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
2141 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
2142 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
2143 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
2144 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2145 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2146 break;
2147 case WINED3D_DATA_INT:
2148 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2149 reg->immconst_data[0], reg->immconst_data[1],
2150 reg->immconst_data[2], reg->immconst_data[3]);
2151 break;
2152 case WINED3D_DATA_RESOURCE:
2153 case WINED3D_DATA_SAMPLER:
2154 case WINED3D_DATA_UINT:
2155 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2156 reg->immconst_data[0], reg->immconst_data[1],
2157 reg->immconst_data[2], reg->immconst_data[3]);
2158 break;
2159 default:
2160 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2161 break;
2163 break;
2165 default:
2166 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2167 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2169 break;
2171 case WINED3DSPR_CONSTBUFFER:
2172 if (reg->idx[1].rel_addr)
2173 sprintf(register_name, "%s_cb%u[%s + %u]",
2174 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2175 else
2176 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2177 break;
2179 case WINED3DSPR_PRIMID:
2180 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2181 break;
2183 default:
2184 FIXME("Unhandled register type %#x.\n", reg->type);
2185 sprintf(register_name, "unrecognized_register");
2186 break;
2190 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2192 *str++ = '.';
2193 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2194 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2195 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2196 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2197 *str = '\0';
2200 /* Get the GLSL write mask for the destination register */
2201 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2203 DWORD mask = param->write_mask;
2205 if (shader_is_scalar(&param->reg))
2207 mask = WINED3DSP_WRITEMASK_0;
2208 *write_mask = '\0';
2210 else
2212 shader_glsl_write_mask_to_str(mask, write_mask);
2215 return mask;
2218 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
2219 unsigned int size = 0;
2221 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2222 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2223 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2224 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2226 return size;
2229 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2231 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2232 * but addressed as "rgba". To fix this we need to swap the register's x
2233 * and z components. */
2234 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2236 *str++ = '.';
2237 /* swizzle bits fields: wwzzyyxx */
2238 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
2239 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
2240 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
2241 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
2242 *str = '\0';
2245 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2246 BOOL fixup, DWORD mask, char *swizzle_str)
2248 if (shader_is_scalar(&param->reg))
2249 *swizzle_str = '\0';
2250 else
2251 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2254 /* From a given parameter token, generate the corresponding GLSL string.
2255 * Also, return the actual register name and swizzle in case the
2256 * caller needs this information as well. */
2257 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2258 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
2260 BOOL is_color = FALSE;
2261 char swizzle_str[6];
2263 glsl_src->reg_name[0] = '\0';
2264 glsl_src->param_str[0] = '\0';
2265 swizzle_str[0] = '\0';
2267 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
2268 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
2270 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
2272 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
2274 else
2276 char reg_name[200];
2278 switch (wined3d_src->reg.data_type)
2280 case WINED3D_DATA_FLOAT:
2281 sprintf(reg_name, "%s", glsl_src->reg_name);
2282 break;
2283 case WINED3D_DATA_INT:
2284 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
2285 break;
2286 case WINED3D_DATA_RESOURCE:
2287 case WINED3D_DATA_SAMPLER:
2288 case WINED3D_DATA_UINT:
2289 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
2290 break;
2291 default:
2292 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
2293 sprintf(reg_name, "%s", glsl_src->reg_name);
2294 break;
2297 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
2301 /* From a given parameter token, generate the corresponding GLSL string.
2302 * Also, return the actual register name and swizzle in case the
2303 * caller needs this information as well. */
2304 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
2305 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
2307 BOOL is_color = FALSE;
2309 glsl_dst->mask_str[0] = '\0';
2310 glsl_dst->reg_name[0] = '\0';
2312 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
2313 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
2316 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2317 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
2318 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
2319 enum wined3d_data_type data_type)
2321 struct glsl_dst_param glsl_dst;
2322 DWORD mask;
2324 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
2326 switch (data_type)
2328 case WINED3D_DATA_FLOAT:
2329 shader_addline(buffer, "%s%s = %s(",
2330 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2331 break;
2332 case WINED3D_DATA_INT:
2333 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
2334 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2335 break;
2336 case WINED3D_DATA_RESOURCE:
2337 case WINED3D_DATA_SAMPLER:
2338 case WINED3D_DATA_UINT:
2339 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
2340 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2341 break;
2342 default:
2343 FIXME("Unhandled data type %#x.\n", data_type);
2344 shader_addline(buffer, "%s%s = %s(",
2345 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2346 break;
2350 return mask;
2353 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2354 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
2356 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2359 /** Process GLSL instruction modifiers */
2360 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
2362 struct glsl_dst_param dst_param;
2363 DWORD modifiers;
2365 if (!ins->dst_count) return;
2367 modifiers = ins->dst[0].modifiers;
2368 if (!modifiers) return;
2370 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2372 if (modifiers & WINED3DSPDM_SATURATE)
2374 /* _SAT means to clamp the value of the register to between 0 and 1 */
2375 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
2376 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
2379 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
2381 FIXME("_centroid modifier not handled\n");
2384 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
2386 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2390 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
2392 switch (op)
2394 case WINED3D_SHADER_REL_OP_GT: return ">";
2395 case WINED3D_SHADER_REL_OP_EQ: return "==";
2396 case WINED3D_SHADER_REL_OP_GE: return ">=";
2397 case WINED3D_SHADER_REL_OP_LT: return "<";
2398 case WINED3D_SHADER_REL_OP_NE: return "!=";
2399 case WINED3D_SHADER_REL_OP_LE: return "<=";
2400 default:
2401 FIXME("Unrecognized operator %#x.\n", op);
2402 return "(\?\?)";
2406 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
2407 DWORD resource_idx, DWORD flags, struct glsl_sample_function *sample_function)
2409 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
2410 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2411 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
2412 && (((const struct shader_glsl_ctx_priv *)ctx->backend_data)->cur_ps_args->shadow & (1 << resource_idx));
2413 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
2414 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2415 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
2416 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
2418 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
2420 /* Note that there's no such thing as a projected cube texture. */
2421 switch (resource_type)
2423 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2424 if (shadow)
2426 if (lod)
2428 sample_function->name = projected ? "shadow1DProjLod" : "shadow1DLod";
2430 else if (grad)
2432 if (gl_info->supported[EXT_GPU_SHADER4])
2433 sample_function->name = projected ? "shadow1DProjGrad" : "shadow1DGrad";
2434 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2435 sample_function->name = projected ? "shadow1DProjGradARB" : "shadow1DGradARB";
2436 else
2438 FIXME("Unsupported 1D shadow grad function.\n");
2439 sample_function->name = "unsupported1DGrad";
2442 else
2444 sample_function->name = projected ? "shadow1DProj" : "shadow1D";
2446 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2448 else
2450 if (lod)
2452 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
2454 else if (grad)
2456 if (gl_info->supported[EXT_GPU_SHADER4])
2457 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
2458 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2459 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
2460 else
2462 FIXME("Unsupported 1D grad function.\n");
2463 sample_function->name = "unsupported1DGrad";
2466 else
2468 sample_function->name = projected ? "texture1DProj" : "texture1D";
2470 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
2472 break;
2474 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2475 if (shadow)
2477 if (texrect)
2479 if (lod)
2481 sample_function->name = projected ? "shadow2DRectProjLod" : "shadow2DRectLod";
2483 else if (grad)
2485 if (gl_info->supported[EXT_GPU_SHADER4])
2486 sample_function->name = projected ? "shadow2DRectProjGrad" : "shadow2DRectGrad";
2487 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2488 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
2489 else
2491 FIXME("Unsupported RECT shadow grad function.\n");
2492 sample_function->name = "unsupported2DRectGrad";
2495 else
2497 sample_function->name = projected ? "shadow2DRectProj" : "shadow2DRect";
2500 else
2502 if (lod)
2504 sample_function->name = projected ? "shadow2DProjLod" : "shadow2DLod";
2506 else if (grad)
2508 if (gl_info->supported[EXT_GPU_SHADER4])
2509 sample_function->name = projected ? "shadow2DProjGrad" : "shadow2DGrad";
2510 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2511 sample_function->name = projected ? "shadow2DProjGradARB" : "shadow2DGradARB";
2512 else
2514 FIXME("Unsupported 2D shadow grad function.\n");
2515 sample_function->name = "unsupported2DGrad";
2518 else
2520 sample_function->name = projected ? "shadow2DProj" : "shadow2D";
2523 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2525 else
2527 if (texrect)
2529 if (lod)
2531 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
2533 else if (grad)
2535 if (gl_info->supported[EXT_GPU_SHADER4])
2536 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
2537 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2538 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
2539 else
2541 FIXME("Unsupported RECT grad function.\n");
2542 sample_function->name = "unsupported2DRectGrad";
2545 else
2547 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
2550 else
2552 if (lod)
2554 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
2556 else if (grad)
2558 if (gl_info->supported[EXT_GPU_SHADER4])
2559 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
2560 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2561 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
2562 else
2564 FIXME("Unsupported 2D grad function.\n");
2565 sample_function->name = "unsupported2DGrad";
2568 else
2570 sample_function->name = projected ? "texture2DProj" : "texture2D";
2573 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2575 break;
2577 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2578 if (shadow)
2580 FIXME("Unsupported 3D shadow function.\n");
2581 sample_function->name = "unsupported3DShadow";
2582 sample_function->coord_mask = 0;
2584 else
2586 if (lod)
2588 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
2590 else if (grad)
2592 if (gl_info->supported[EXT_GPU_SHADER4])
2593 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
2594 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2595 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
2596 else
2598 FIXME("Unsupported 3D grad function.\n");
2599 sample_function->name = "unsupported3DGrad";
2602 else
2604 sample_function->name = projected ? "texture3DProj" : "texture3D";
2606 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2608 break;
2610 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2611 if (shadow)
2613 FIXME("Unsupported Cube shadow function.\n");
2614 sample_function->name = "unsupportedCubeShadow";
2615 sample_function->coord_mask = 0;
2617 else
2619 if (lod)
2621 sample_function->name = "textureCubeLod";
2623 else if (grad)
2625 if (gl_info->supported[EXT_GPU_SHADER4])
2626 sample_function->name = "textureCubeGrad";
2627 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2628 sample_function->name = "textureCubeGradARB";
2629 else
2631 FIXME("Unsupported Cube grad function.\n");
2632 sample_function->name = "unsupportedCubeGrad";
2635 else
2637 sample_function->name = "textureCube";
2639 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2641 break;
2643 default:
2644 sample_function->name = "";
2645 sample_function->coord_mask = 0;
2646 FIXME("Unhandled resource type %#x.\n", resource_type);
2647 break;
2651 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2652 BOOL sign_fixup, enum fixup_channel_source channel_source)
2654 switch(channel_source)
2656 case CHANNEL_SOURCE_ZERO:
2657 strcat(arguments, "0.0");
2658 break;
2660 case CHANNEL_SOURCE_ONE:
2661 strcat(arguments, "1.0");
2662 break;
2664 case CHANNEL_SOURCE_X:
2665 strcat(arguments, reg_name);
2666 strcat(arguments, ".x");
2667 break;
2669 case CHANNEL_SOURCE_Y:
2670 strcat(arguments, reg_name);
2671 strcat(arguments, ".y");
2672 break;
2674 case CHANNEL_SOURCE_Z:
2675 strcat(arguments, reg_name);
2676 strcat(arguments, ".z");
2677 break;
2679 case CHANNEL_SOURCE_W:
2680 strcat(arguments, reg_name);
2681 strcat(arguments, ".w");
2682 break;
2684 default:
2685 FIXME("Unhandled channel source %#x\n", channel_source);
2686 strcat(arguments, "undefined");
2687 break;
2690 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2693 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
2694 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2696 unsigned int mask_size, remaining;
2697 DWORD fixup_mask = 0;
2698 char arguments[256];
2699 char mask_str[6];
2701 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2702 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2703 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2704 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2705 if (!(mask &= fixup_mask))
2706 return;
2708 if (is_complex_fixup(fixup))
2710 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2711 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2712 return;
2715 shader_glsl_write_mask_to_str(mask, mask_str);
2716 mask_size = shader_glsl_get_write_mask_size(mask);
2718 arguments[0] = '\0';
2719 remaining = mask_size;
2720 if (mask & WINED3DSP_WRITEMASK_0)
2722 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2723 if (--remaining) strcat(arguments, ", ");
2725 if (mask & WINED3DSP_WRITEMASK_1)
2727 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2728 if (--remaining) strcat(arguments, ", ");
2730 if (mask & WINED3DSP_WRITEMASK_2)
2732 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2733 if (--remaining) strcat(arguments, ", ");
2735 if (mask & WINED3DSP_WRITEMASK_3)
2737 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2738 if (--remaining) strcat(arguments, ", ");
2741 if (mask_size > 1)
2742 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2743 else
2744 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2747 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2749 char reg_name[256];
2750 BOOL is_color;
2752 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2753 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2756 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2757 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2758 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2760 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2761 char dst_swizzle[6];
2762 struct color_fixup_desc fixup;
2763 BOOL np2_fixup = FALSE;
2764 va_list args;
2766 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2768 /* FIXME: We currently don't support fixups for vertex shaders or anything
2769 * above SM3. Note that for SM4+ the sampler index doesn't have to match
2770 * the resource index. */
2771 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
2773 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2774 fixup = priv->cur_ps_args->color_fixup[sampler];
2776 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2777 if(bias) {
2778 FIXME("Biased sampling from NP2 textures is unsupported\n");
2779 } else {
2780 np2_fixup = TRUE;
2784 else
2786 fixup = COLOR_FIXUP_IDENTITY;
2789 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2791 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2792 sample_function->name, shader_glsl_get_prefix(version->type), sampler);
2794 va_start(args, coord_reg_fmt);
2795 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2796 va_end(args);
2798 if(bias) {
2799 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2800 } else {
2801 if (np2_fixup) {
2802 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2803 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2805 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2806 (idx % 2) ? "zw" : "xy", dst_swizzle);
2807 } else if(dx && dy) {
2808 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2809 } else {
2810 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2814 if(!is_identity_fixup(fixup)) {
2815 shader_glsl_color_correction(ins, fixup);
2819 /*****************************************************************************
2820 * Begin processing individual instruction opcodes
2821 ****************************************************************************/
2823 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2825 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2826 struct glsl_src_param src0_param;
2827 struct glsl_src_param src1_param;
2828 DWORD write_mask;
2829 const char *op;
2831 /* Determine the GLSL operator to use based on the opcode */
2832 switch (ins->handler_idx)
2834 case WINED3DSIH_ADD: op = "+"; break;
2835 case WINED3DSIH_AND: op = "&"; break;
2836 case WINED3DSIH_DIV: op = "/"; break;
2837 case WINED3DSIH_IADD: op = "+"; break;
2838 case WINED3DSIH_ISHL: op = "<<"; break;
2839 case WINED3DSIH_MUL: op = "*"; break;
2840 case WINED3DSIH_OR: op = "|"; break;
2841 case WINED3DSIH_SUB: op = "-"; break;
2842 case WINED3DSIH_USHR: op = ">>"; break;
2843 case WINED3DSIH_XOR: op = "^"; break;
2844 default:
2845 op = "<unhandled operator>";
2846 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2847 break;
2850 write_mask = shader_glsl_append_dst(buffer, ins);
2851 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2852 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2853 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2856 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2858 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2859 struct glsl_src_param src0_param;
2860 struct glsl_src_param src1_param;
2861 unsigned int mask_size;
2862 DWORD write_mask;
2863 const char *op;
2865 write_mask = shader_glsl_append_dst(buffer, ins);
2866 mask_size = shader_glsl_get_write_mask_size(write_mask);
2867 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2868 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2870 if (mask_size > 1)
2872 switch (ins->handler_idx)
2874 case WINED3DSIH_EQ: op = "equal"; break;
2875 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2876 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2877 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
2878 case WINED3DSIH_LT: op = "lessThan"; break;
2879 case WINED3DSIH_NE: op = "notEqual"; break;
2880 default:
2881 op = "<unhandled operator>";
2882 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2883 break;
2886 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2887 mask_size, op, src0_param.param_str, src1_param.param_str);
2889 else
2891 switch (ins->handler_idx)
2893 case WINED3DSIH_EQ: op = "=="; break;
2894 case WINED3DSIH_GE: op = ">="; break;
2895 case WINED3DSIH_IGE: op = ">="; break;
2896 case WINED3DSIH_UGE: op = ">="; break;
2897 case WINED3DSIH_LT: op = "<"; break;
2898 case WINED3DSIH_NE: op = "!="; break;
2899 default:
2900 op = "<unhandled operator>";
2901 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2902 break;
2905 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2906 src0_param.param_str, op, src1_param.param_str);
2910 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2912 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2913 struct glsl_src_param src0_param;
2914 struct glsl_src_param src1_param;
2915 DWORD write_mask;
2917 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2918 * not, we can emulate it. */
2919 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2920 FIXME("64-bit integer multiplies not implemented.\n");
2922 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2924 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2925 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2926 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2928 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2929 src0_param.param_str, src1_param.param_str);
2933 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2935 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2936 struct glsl_src_param src0_param, src1_param;
2937 DWORD write_mask;
2939 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2942 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2944 char dst_mask[6];
2946 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2947 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2948 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2949 shader_addline(buffer, "tmp0%s = %s / %s;\n",
2950 dst_mask, src0_param.param_str, src1_param.param_str);
2952 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2953 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2954 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2955 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
2957 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2958 shader_addline(buffer, "tmp0%s);\n", dst_mask);
2960 else
2962 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2963 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2964 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2965 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
2968 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2970 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2971 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2972 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2973 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
2977 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2978 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2980 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2981 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2982 struct glsl_src_param src0_param;
2983 DWORD write_mask;
2985 write_mask = shader_glsl_append_dst(buffer, ins);
2986 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2988 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2989 * shader versions WINED3DSIO_MOVA is used for this. */
2990 if (ins->ctx->reg_maps->shader_version.major == 1
2991 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
2992 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2994 /* This is a simple floor() */
2995 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2996 if (mask_size > 1) {
2997 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2998 } else {
2999 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3002 else if(ins->handler_idx == WINED3DSIH_MOVA)
3004 /* We need to *round* to the nearest int here. */
3005 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3007 if (gl_info->supported[EXT_GPU_SHADER4])
3009 if (mask_size > 1)
3010 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3011 else
3012 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3014 else
3016 if (mask_size > 1)
3017 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3018 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3019 else
3020 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3021 src0_param.param_str, src0_param.param_str);
3024 else
3026 shader_addline(buffer, "%s);\n", src0_param.param_str);
3030 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3031 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3033 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3034 struct glsl_src_param src0_param;
3035 struct glsl_src_param src1_param;
3036 DWORD dst_write_mask, src_write_mask;
3037 unsigned int dst_size = 0;
3039 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3040 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3042 /* dp4 works on vec4, dp3 on vec3, etc. */
3043 if (ins->handler_idx == WINED3DSIH_DP4)
3044 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3045 else if (ins->handler_idx == WINED3DSIH_DP3)
3046 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3047 else
3048 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3050 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3051 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3053 if (dst_size > 1) {
3054 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3055 } else {
3056 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3060 /* Note that this instruction has some restrictions. The destination write mask
3061 * can't contain the w component, and the source swizzles have to be .xyzw */
3062 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3064 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3065 struct glsl_src_param src0_param;
3066 struct glsl_src_param src1_param;
3067 char dst_mask[6];
3069 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3070 shader_glsl_append_dst(ins->ctx->buffer, ins);
3071 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3072 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3073 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3076 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3078 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3081 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3082 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3083 * GLSL uses the value as-is. */
3084 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3086 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3087 struct glsl_src_param src0_param;
3088 struct glsl_src_param src1_param;
3089 DWORD dst_write_mask;
3090 unsigned int dst_size;
3092 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3093 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3095 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3096 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3098 if (dst_size > 1)
3100 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3101 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3103 else
3105 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3106 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3110 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3111 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3113 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3114 struct glsl_src_param src_param;
3115 const char *instruction;
3116 DWORD write_mask;
3117 unsigned i;
3119 /* Determine the GLSL function to use based on the opcode */
3120 /* TODO: Possibly make this a table for faster lookups */
3121 switch (ins->handler_idx)
3123 case WINED3DSIH_MIN: instruction = "min"; break;
3124 case WINED3DSIH_MAX: instruction = "max"; break;
3125 case WINED3DSIH_ABS: instruction = "abs"; break;
3126 case WINED3DSIH_FRC: instruction = "fract"; break;
3127 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3128 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3129 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3130 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3131 default: instruction = "";
3132 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
3133 break;
3136 write_mask = shader_glsl_append_dst(buffer, ins);
3138 shader_addline(buffer, "%s(", instruction);
3140 if (ins->src_count)
3142 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3143 shader_addline(buffer, "%s", src_param.param_str);
3144 for (i = 1; i < ins->src_count; ++i)
3146 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3147 shader_addline(buffer, ", %s", src_param.param_str);
3151 shader_addline(buffer, "));\n");
3154 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3156 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3158 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3159 struct glsl_src_param src_param;
3160 unsigned int mask_size;
3161 DWORD write_mask;
3162 char dst_mask[6];
3164 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3165 mask_size = shader_glsl_get_write_mask_size(write_mask);
3166 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3168 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3169 src_param.param_str, src_param.param_str);
3170 shader_glsl_append_dst(buffer, ins);
3172 if (mask_size > 1)
3174 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3175 mask_size, src_param.param_str);
3177 else
3179 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3180 src_param.param_str);
3184 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3186 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3187 struct glsl_src_param src0_param;
3188 const char *prefix, *suffix;
3189 unsigned int dst_size;
3190 DWORD dst_write_mask;
3192 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3193 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3195 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src0_param);
3197 switch (ins->handler_idx)
3199 case WINED3DSIH_EXP:
3200 case WINED3DSIH_EXPP:
3201 prefix = "exp2(";
3202 suffix = ")";
3203 break;
3205 case WINED3DSIH_LOG:
3206 case WINED3DSIH_LOGP:
3207 prefix = "log2(abs(";
3208 suffix = "))";
3209 break;
3211 case WINED3DSIH_RCP:
3212 prefix = "1.0 / ";
3213 suffix = "";
3214 break;
3216 case WINED3DSIH_RSQ:
3217 prefix = "inversesqrt(abs(";
3218 suffix = "))";
3219 break;
3221 default:
3222 prefix = "";
3223 suffix = "";
3224 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3225 break;
3228 if (dst_size > 1)
3229 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3230 else
3231 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3234 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3235 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3236 * dst.x = 2^(floor(src))
3237 * dst.y = src - floor(src)
3238 * dst.z = 2^src (partial precision is allowed, but optional)
3239 * dst.w = 1.0;
3240 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3241 * dst = 2^src; (partial precision is allowed, but optional)
3243 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
3245 if (ins->ctx->reg_maps->shader_version.major < 2)
3247 struct glsl_src_param src_param;
3248 char dst_mask[6];
3250 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
3252 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
3253 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
3254 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
3255 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
3257 shader_glsl_append_dst(ins->ctx->buffer, ins);
3258 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3259 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
3260 return;
3263 shader_glsl_scalar_op(ins);
3266 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
3268 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3269 struct glsl_src_param src_param;
3270 unsigned int mask_size;
3271 DWORD write_mask;
3273 write_mask = shader_glsl_append_dst(buffer, ins);
3274 mask_size = shader_glsl_get_write_mask_size(write_mask);
3275 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3277 if (mask_size > 1)
3278 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
3279 else
3280 shader_addline(buffer, "int(%s));\n", src_param.param_str);
3283 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
3285 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3286 struct glsl_src_param src_param;
3287 unsigned int mask_size;
3288 DWORD write_mask;
3290 write_mask = shader_glsl_append_dst(buffer, ins);
3291 mask_size = shader_glsl_get_write_mask_size(write_mask);
3292 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3294 if (mask_size > 1)
3295 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
3296 else
3297 shader_addline(buffer, "float(%s));\n", src_param.param_str);
3300 /** Process signed comparison opcodes in GLSL. */
3301 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
3303 struct glsl_src_param src0_param;
3304 struct glsl_src_param src1_param;
3305 DWORD write_mask;
3306 unsigned int mask_size;
3308 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3309 mask_size = shader_glsl_get_write_mask_size(write_mask);
3310 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3311 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3313 if (mask_size > 1) {
3314 const char *compare;
3316 switch(ins->handler_idx)
3318 case WINED3DSIH_SLT: compare = "lessThan"; break;
3319 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
3320 default: compare = "";
3321 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
3324 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
3325 src0_param.param_str, src1_param.param_str);
3326 } else {
3327 switch(ins->handler_idx)
3329 case WINED3DSIH_SLT:
3330 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3331 * to return 0.0 but step returns 1.0 because step is not < x
3332 * An alternative is a bvec compare padded with an unused second component.
3333 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3334 * issue. Playing with not() is not possible either because not() does not accept
3335 * a scalar.
3337 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
3338 src0_param.param_str, src1_param.param_str);
3339 break;
3340 case WINED3DSIH_SGE:
3341 /* Here we can use the step() function and safe a conditional */
3342 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
3343 break;
3344 default:
3345 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
3351 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
3353 const char *condition_prefix, *condition_suffix;
3354 struct wined3d_shader_dst_param dst;
3355 struct glsl_src_param src0_param;
3356 struct glsl_src_param src1_param;
3357 struct glsl_src_param src2_param;
3358 BOOL temp_destination = FALSE;
3359 DWORD cmp_channel = 0;
3360 unsigned int i, j;
3361 char mask_char[6];
3362 DWORD write_mask;
3364 switch (ins->handler_idx)
3366 case WINED3DSIH_CMP:
3367 condition_prefix = "";
3368 condition_suffix = " >= 0.0";
3369 break;
3371 case WINED3DSIH_CND:
3372 condition_prefix = "";
3373 condition_suffix = " > 0.5";
3374 break;
3376 case WINED3DSIH_MOVC:
3377 condition_prefix = "bool(";
3378 condition_suffix = ")";
3379 break;
3381 default:
3382 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3383 condition_prefix = "<unhandled prefix>";
3384 condition_suffix = "<unhandled suffix>";
3385 break;
3388 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
3390 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3391 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3392 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3393 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3395 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3396 condition_prefix, src0_param.param_str, condition_suffix,
3397 src1_param.param_str, src2_param.param_str);
3398 return;
3401 dst = ins->dst[0];
3403 /* Splitting the instruction up in multiple lines imposes a problem:
3404 * The first lines may overwrite source parameters of the following lines.
3405 * Deal with that by using a temporary destination register if needed. */
3406 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
3407 && ins->src[0].reg.type == dst.reg.type)
3408 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
3409 && ins->src[1].reg.type == dst.reg.type)
3410 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
3411 && ins->src[2].reg.type == dst.reg.type))
3412 temp_destination = TRUE;
3414 /* Cycle through all source0 channels. */
3415 for (i = 0; i < 4; ++i)
3417 write_mask = 0;
3418 /* Find the destination channels which use the current source0 channel. */
3419 for (j = 0; j < 4; ++j)
3421 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
3423 write_mask |= WINED3DSP_WRITEMASK_0 << j;
3424 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
3427 dst.write_mask = ins->dst[0].write_mask & write_mask;
3429 if (temp_destination)
3431 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
3432 continue;
3433 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
3435 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
3436 continue;
3438 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
3439 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3440 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3442 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3443 condition_prefix, src0_param.param_str, condition_suffix,
3444 src1_param.param_str, src2_param.param_str);
3447 if (temp_destination)
3449 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3450 shader_glsl_append_dst(ins->ctx->buffer, ins);
3451 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3455 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3456 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3457 * the compare is done per component of src0. */
3458 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3460 struct glsl_src_param src0_param;
3461 struct glsl_src_param src1_param;
3462 struct glsl_src_param src2_param;
3463 DWORD write_mask;
3464 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3465 ins->ctx->reg_maps->shader_version.minor);
3467 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3469 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3470 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3471 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3472 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3474 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
3475 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3476 else
3477 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3478 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3479 return;
3482 shader_glsl_conditional_move(ins);
3485 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3486 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3488 struct glsl_src_param src0_param;
3489 struct glsl_src_param src1_param;
3490 struct glsl_src_param src2_param;
3491 DWORD write_mask;
3493 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3494 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3495 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3496 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3497 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3498 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3501 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3502 Vertex shaders to GLSL codes */
3503 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3505 int i;
3506 int nComponents = 0;
3507 struct wined3d_shader_dst_param tmp_dst = {{0}};
3508 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3509 struct wined3d_shader_instruction tmp_ins;
3511 memset(&tmp_ins, 0, sizeof(tmp_ins));
3513 /* Set constants for the temporary argument */
3514 tmp_ins.ctx = ins->ctx;
3515 tmp_ins.dst_count = 1;
3516 tmp_ins.dst = &tmp_dst;
3517 tmp_ins.src_count = 2;
3518 tmp_ins.src = tmp_src;
3520 switch(ins->handler_idx)
3522 case WINED3DSIH_M4x4:
3523 nComponents = 4;
3524 tmp_ins.handler_idx = WINED3DSIH_DP4;
3525 break;
3526 case WINED3DSIH_M4x3:
3527 nComponents = 3;
3528 tmp_ins.handler_idx = WINED3DSIH_DP4;
3529 break;
3530 case WINED3DSIH_M3x4:
3531 nComponents = 4;
3532 tmp_ins.handler_idx = WINED3DSIH_DP3;
3533 break;
3534 case WINED3DSIH_M3x3:
3535 nComponents = 3;
3536 tmp_ins.handler_idx = WINED3DSIH_DP3;
3537 break;
3538 case WINED3DSIH_M3x2:
3539 nComponents = 2;
3540 tmp_ins.handler_idx = WINED3DSIH_DP3;
3541 break;
3542 default:
3543 break;
3546 tmp_dst = ins->dst[0];
3547 tmp_src[0] = ins->src[0];
3548 tmp_src[1] = ins->src[1];
3549 for (i = 0; i < nComponents; ++i)
3551 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3552 shader_glsl_dot(&tmp_ins);
3553 ++tmp_src[1].reg.idx[0].offset;
3558 The LRP instruction performs a component-wise linear interpolation
3559 between the second and third operands using the first operand as the
3560 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3561 This is equivalent to mix(src2, src1, src0);
3563 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3565 struct glsl_src_param src0_param;
3566 struct glsl_src_param src1_param;
3567 struct glsl_src_param src2_param;
3568 DWORD write_mask;
3570 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3572 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3573 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3574 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3576 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3577 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3580 /** Process the WINED3DSIO_LIT instruction in GLSL:
3581 * dst.x = dst.w = 1.0
3582 * dst.y = (src0.x > 0) ? src0.x
3583 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3584 * where src.w is clamped at +- 128
3586 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3588 struct glsl_src_param src0_param;
3589 struct glsl_src_param src1_param;
3590 struct glsl_src_param src3_param;
3591 char dst_mask[6];
3593 shader_glsl_append_dst(ins->ctx->buffer, ins);
3594 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3596 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3597 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3598 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3600 /* The sdk specifies the instruction like this
3601 * dst.x = 1.0;
3602 * if(src.x > 0.0) dst.y = src.x
3603 * else dst.y = 0.0.
3604 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3605 * else dst.z = 0.0;
3606 * dst.w = 1.0;
3607 * (where power = src.w clamped between -128 and 128)
3609 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3610 * dst.x = 1.0 ... No further explanation needed
3611 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3612 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3613 * dst.w = 1.0. ... Nothing fancy.
3615 * So we still have one conditional in there. So do this:
3616 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3618 * 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),
3619 * 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.
3620 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3622 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3623 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3624 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3626 shader_addline(ins->ctx->buffer,
3627 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3628 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3629 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3630 src0_param.param_str, src3_param.param_str, dst_mask);
3633 /** Process the WINED3DSIO_DST instruction in GLSL:
3634 * dst.x = 1.0
3635 * dst.y = src0.x * src0.y
3636 * dst.z = src0.z
3637 * dst.w = src1.w
3639 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3641 struct glsl_src_param src0y_param;
3642 struct glsl_src_param src0z_param;
3643 struct glsl_src_param src1y_param;
3644 struct glsl_src_param src1w_param;
3645 char dst_mask[6];
3647 shader_glsl_append_dst(ins->ctx->buffer, ins);
3648 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3650 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3651 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3652 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3653 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3655 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3656 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3659 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3660 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3661 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3663 * dst.x = cos(src0.?)
3664 * dst.y = sin(src0.?)
3665 * dst.z = dst.z
3666 * dst.w = dst.w
3668 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3670 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3671 struct glsl_src_param src0_param;
3672 DWORD write_mask;
3674 if (ins->ctx->reg_maps->shader_version.major < 4)
3676 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3678 write_mask = shader_glsl_append_dst(buffer, ins);
3679 switch (write_mask)
3681 case WINED3DSP_WRITEMASK_0:
3682 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3683 break;
3685 case WINED3DSP_WRITEMASK_1:
3686 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3687 break;
3689 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3690 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3691 src0_param.param_str, src0_param.param_str);
3692 break;
3694 default:
3695 ERR("Write mask should be .x, .y or .xy\n");
3696 break;
3699 return;
3702 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3705 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3707 char dst_mask[6];
3709 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3710 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3711 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3713 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3714 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3715 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3717 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3718 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3720 else
3722 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3723 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3724 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3727 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3729 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3730 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3731 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3735 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3736 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3737 * generate invalid code
3739 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3741 struct glsl_src_param src0_param;
3742 DWORD write_mask;
3744 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3745 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3747 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3750 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3751 * Start a for() loop where src1.y is the initial value of aL,
3752 * increment aL by src1.z for a total of src1.x iterations.
3753 * Need to use a temporary variable for this operation.
3755 /* FIXME: I don't think nested loops will work correctly this way. */
3756 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3758 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3759 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3760 const struct wined3d_shader *shader = ins->ctx->shader;
3761 const struct wined3d_shader_lconst *constant;
3762 struct glsl_src_param src1_param;
3763 const DWORD *control_values = NULL;
3765 if (ins->ctx->reg_maps->shader_version.major < 4)
3767 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3769 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3770 * class hardware doesn't support real varying indexing, but Microsoft
3771 * designed this feature for Shader model 2.x+. If the loop control is
3772 * known at compile time, the GLSL compiler can unroll the loop, and
3773 * replace indirect addressing with direct addressing. */
3774 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3776 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3778 if (constant->idx == ins->src[1].reg.idx[0].offset)
3780 control_values = constant->value;
3781 break;
3786 if (control_values)
3788 struct wined3d_shader_loop_control loop_control;
3789 loop_control.count = control_values[0];
3790 loop_control.start = control_values[1];
3791 loop_control.step = (int)control_values[2];
3793 if (loop_control.step > 0)
3795 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3796 loop_state->current_depth, loop_control.start,
3797 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3798 loop_state->current_depth, loop_control.step);
3800 else if (loop_control.step < 0)
3802 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3803 loop_state->current_depth, loop_control.start,
3804 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3805 loop_state->current_depth, loop_control.step);
3807 else
3809 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3810 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3811 loop_state->current_depth, loop_control.count,
3812 loop_state->current_depth);
3815 else
3817 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3818 loop_state->current_depth, loop_state->current_reg,
3819 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3820 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3823 ++loop_state->current_reg;
3825 else
3827 shader_addline(buffer, "for (;;)\n{\n");
3830 ++loop_state->current_depth;
3833 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3835 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3837 shader_addline(ins->ctx->buffer, "}\n");
3839 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3841 --loop_state->current_depth;
3842 --loop_state->current_reg;
3845 if (ins->handler_idx == WINED3DSIH_ENDREP)
3847 --loop_state->current_depth;
3851 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3853 const struct wined3d_shader *shader = ins->ctx->shader;
3854 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3855 const struct wined3d_shader_lconst *constant;
3856 struct glsl_src_param src0_param;
3857 const DWORD *control_values = NULL;
3859 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3860 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3862 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3864 if (constant->idx == ins->src[0].reg.idx[0].offset)
3866 control_values = constant->value;
3867 break;
3872 if (control_values)
3874 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3875 loop_state->current_depth, loop_state->current_depth,
3876 control_values[0], loop_state->current_depth);
3878 else
3880 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3881 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3882 loop_state->current_depth, loop_state->current_depth,
3883 src0_param.param_str, loop_state->current_depth);
3886 ++loop_state->current_depth;
3889 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3891 struct glsl_src_param src0_param;
3893 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3894 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
3897 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3899 struct glsl_src_param src0_param;
3900 struct glsl_src_param src1_param;
3902 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3903 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3905 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3906 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3909 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3911 shader_addline(ins->ctx->buffer, "} else {\n");
3914 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3916 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3919 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3921 shader_addline(ins->ctx->buffer, "break;\n");
3924 /* FIXME: According to MSDN the compare is done per component. */
3925 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3927 struct glsl_src_param src0_param;
3928 struct glsl_src_param src1_param;
3930 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3931 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3933 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3934 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3937 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
3939 struct glsl_src_param src_param;
3941 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
3942 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
3945 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
3947 shader_addline(ins->ctx->buffer, "}\n");
3948 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
3951 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
3953 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
3956 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3958 struct glsl_src_param src1_param;
3960 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3961 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
3962 src1_param.param_str, ins->src[0].reg.idx[0].offset);
3965 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3967 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3968 * function only suppresses the unhandled instruction warning
3972 /*********************************************
3973 * Pixel Shader Specific Code begins here
3974 ********************************************/
3975 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3977 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3978 ins->ctx->reg_maps->shader_version.minor);
3979 struct glsl_sample_function sample_function;
3980 DWORD sample_flags = 0;
3981 DWORD resource_idx;
3982 DWORD mask = 0, swizzle;
3983 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3985 /* 1.0-1.4: Use destination register as sampler source.
3986 * 2.0+: Use provided sampler source. */
3987 if (shader_version < WINED3D_SHADER_VERSION(2,0))
3988 resource_idx = ins->dst[0].reg.idx[0].offset;
3989 else
3990 resource_idx = ins->src[1].reg.idx[0].offset;
3992 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3994 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3995 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3996 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
3998 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3999 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4001 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4002 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4004 case WINED3D_TTFF_COUNT1:
4005 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4006 break;
4007 case WINED3D_TTFF_COUNT2:
4008 mask = WINED3DSP_WRITEMASK_1;
4009 break;
4010 case WINED3D_TTFF_COUNT3:
4011 mask = WINED3DSP_WRITEMASK_2;
4012 break;
4013 case WINED3D_TTFF_COUNT4:
4014 case WINED3D_TTFF_DISABLE:
4015 mask = WINED3DSP_WRITEMASK_3;
4016 break;
4020 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4022 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4024 if (src_mod == WINED3DSPSM_DZ) {
4025 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4026 mask = WINED3DSP_WRITEMASK_2;
4027 } else if (src_mod == WINED3DSPSM_DW) {
4028 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4029 mask = WINED3DSP_WRITEMASK_3;
4032 else
4034 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4035 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4037 /* ps 2.0 texldp instruction always divides by the fourth component. */
4038 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4039 mask = WINED3DSP_WRITEMASK_3;
4043 if (priv->cur_ps_args->np2_fixup & (1 << resource_idx))
4044 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4046 shader_glsl_get_sample_function(ins->ctx, resource_idx, sample_flags, &sample_function);
4047 mask |= sample_function.coord_mask;
4049 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4050 else swizzle = ins->src[1].swizzle;
4052 /* 1.0-1.3: Use destination register as coordinate source.
4053 1.4+: Use provided coordinate source register. */
4054 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4056 char coord_mask[6];
4057 shader_glsl_write_mask_to_str(mask, coord_mask);
4058 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
4059 "T%u%s", resource_idx, coord_mask);
4061 else
4063 struct glsl_src_param coord_param;
4064 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4065 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4067 struct glsl_src_param bias;
4068 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4069 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4070 "%s", coord_param.param_str);
4071 } else {
4072 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
4073 "%s", coord_param.param_str);
4078 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4080 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4081 struct glsl_src_param coord_param, dx_param, dy_param;
4082 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
4083 struct glsl_sample_function sample_function;
4084 DWORD sampler_idx;
4085 DWORD swizzle = ins->src[1].swizzle;
4086 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4088 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
4090 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4091 shader_glsl_tex(ins);
4092 return;
4095 sampler_idx = ins->src[1].reg.idx[0].offset;
4096 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
4097 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4099 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
4100 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4101 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
4102 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
4104 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
4105 "%s", coord_param.param_str);
4108 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4110 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4111 struct glsl_src_param coord_param, lod_param;
4112 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
4113 struct glsl_sample_function sample_function;
4114 DWORD sampler_idx;
4115 DWORD swizzle = ins->src[1].swizzle;
4116 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4118 sampler_idx = ins->src[1].reg.idx[0].offset;
4119 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
4120 && priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
4121 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4123 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
4124 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4126 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4128 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
4129 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
4131 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4132 * However, the NVIDIA drivers allow them in fragment shaders as well,
4133 * even without the appropriate extension. */
4134 WARN("Using %s in fragment shader.\n", sample_function.name);
4136 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
4137 "%s", coord_param.param_str);
4140 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4141 unsigned int resource_idx, unsigned int sampler_idx)
4143 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4144 unsigned int i;
4146 for (i = 0; i < sampler_map->count; ++i)
4148 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4149 return entries[i].bind_idx;
4152 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4154 return ~0u;
4157 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
4159 struct glsl_sample_function sample_function;
4160 struct glsl_src_param coord_param;
4161 unsigned int sampler_idx;
4163 shader_glsl_get_sample_function(ins->ctx, ins->src[1].reg.idx[0].offset, 0, &sample_function);
4164 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4165 sampler_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4166 ins->src[1].reg.idx[0].offset, ins->src[2].reg.idx[0].offset);
4167 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE,
4168 NULL, NULL, NULL, "%s", coord_param.param_str);
4171 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
4173 /* FIXME: Make this work for more than just 2D textures */
4174 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4175 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4177 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
4179 char dst_mask[6];
4181 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4182 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
4183 ins->dst[0].reg.idx[0].offset, dst_mask);
4185 else
4187 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4188 DWORD reg = ins->src[0].reg.idx[0].offset;
4189 char dst_swizzle[6];
4191 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
4193 if (src_mod == WINED3DSPSM_DZ)
4195 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4196 struct glsl_src_param div_param;
4198 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
4200 if (mask_size > 1) {
4201 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4202 } else {
4203 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4206 else if (src_mod == WINED3DSPSM_DW)
4208 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4209 struct glsl_src_param div_param;
4211 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
4213 if (mask_size > 1) {
4214 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4215 } else {
4216 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4218 } else {
4219 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
4224 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4225 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4226 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4227 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
4229 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4230 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4231 struct glsl_sample_function sample_function;
4232 struct glsl_src_param src0_param;
4233 UINT mask_size;
4235 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4237 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4238 * scalar, and projected sampling would require 4.
4240 * It is a dependent read - not valid with conditional NP2 textures
4242 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4243 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4245 switch(mask_size)
4247 case 1:
4248 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4249 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
4250 break;
4252 case 2:
4253 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4254 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
4255 break;
4257 case 3:
4258 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4259 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
4260 break;
4262 default:
4263 FIXME("Unexpected mask size %u\n", mask_size);
4264 break;
4268 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4269 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4270 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
4272 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4273 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4274 struct glsl_src_param src0_param;
4275 DWORD dst_mask;
4276 unsigned int mask_size;
4278 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4279 mask_size = shader_glsl_get_write_mask_size(dst_mask);
4280 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4282 if (mask_size > 1) {
4283 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
4284 } else {
4285 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
4289 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4290 * Calculate the depth as dst.x / dst.y */
4291 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
4293 struct glsl_dst_param dst_param;
4295 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4297 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4298 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4299 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4300 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4301 * >= 1.0 or < 0.0
4303 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4304 dst_param.reg_name, dst_param.reg_name);
4307 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4308 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4309 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4310 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4312 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
4314 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4315 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4316 struct glsl_src_param src0_param;
4318 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4320 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
4321 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4324 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4325 * Calculate the 1st of a 2-row matrix multiplication. */
4326 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
4328 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4329 DWORD reg = ins->dst[0].reg.idx[0].offset;
4330 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4331 struct glsl_src_param src0_param;
4333 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4334 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4337 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4338 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4339 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
4341 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4342 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4343 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4344 DWORD reg = ins->dst[0].reg.idx[0].offset;
4345 struct glsl_src_param src0_param;
4347 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4348 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
4349 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
4352 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
4354 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4355 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4356 struct glsl_sample_function sample_function;
4357 DWORD reg = ins->dst[0].reg.idx[0].offset;
4358 struct glsl_src_param src0_param;
4360 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4361 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4363 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4365 /* Sample the texture using the calculated coordinates */
4366 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
4369 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
4370 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
4371 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
4373 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4374 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4375 struct glsl_sample_function sample_function;
4376 DWORD reg = ins->dst[0].reg.idx[0].offset;
4377 struct glsl_src_param src0_param;
4379 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4380 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4382 /* Dependent read, not valid with conditional NP2 */
4383 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4385 /* Sample the texture using the calculated coordinates */
4386 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
4388 tex_mx->current_row = 0;
4391 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4392 * Perform the 3rd row of a 3x3 matrix multiply */
4393 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
4395 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4396 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4397 DWORD reg = ins->dst[0].reg.idx[0].offset;
4398 struct glsl_src_param src0_param;
4399 char dst_mask[6];
4401 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4403 shader_glsl_append_dst(ins->ctx->buffer, ins);
4404 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4405 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
4407 tex_mx->current_row = 0;
4410 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4411 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4412 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
4414 struct glsl_src_param src0_param;
4415 struct glsl_src_param src1_param;
4416 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4417 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4418 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4419 struct glsl_sample_function sample_function;
4420 DWORD reg = ins->dst[0].reg.idx[0].offset;
4421 char coord_mask[6];
4423 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4424 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4426 /* Perform the last matrix multiply operation */
4427 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4428 /* Reflection calculation */
4429 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
4431 /* Dependent read, not valid with conditional NP2 */
4432 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4433 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4435 /* Sample the texture */
4436 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4437 NULL, NULL, NULL, "tmp0%s", coord_mask);
4439 tex_mx->current_row = 0;
4442 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4443 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4444 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
4446 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4447 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4448 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4449 struct glsl_sample_function sample_function;
4450 DWORD reg = ins->dst[0].reg.idx[0].offset;
4451 struct glsl_src_param src0_param;
4452 char coord_mask[6];
4454 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4456 /* Perform the last matrix multiply operation */
4457 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
4459 /* Construct the eye-ray vector from w coordinates */
4460 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
4461 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
4462 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4464 /* Dependent read, not valid with conditional NP2 */
4465 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4466 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4468 /* Sample the texture using the calculated coordinates */
4469 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4470 NULL, NULL, NULL, "tmp0%s", coord_mask);
4472 tex_mx->current_row = 0;
4475 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4476 * Apply a fake bump map transform.
4477 * texbem is pshader <= 1.3 only, this saves a few version checks
4479 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
4481 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4482 struct glsl_sample_function sample_function;
4483 struct glsl_src_param coord_param;
4484 DWORD sampler_idx;
4485 DWORD mask;
4486 DWORD flags;
4487 char coord_mask[6];
4489 sampler_idx = ins->dst[0].reg.idx[0].offset;
4490 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4491 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4493 /* Dependent read, not valid with conditional NP2 */
4494 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4495 mask = sample_function.coord_mask;
4497 shader_glsl_write_mask_to_str(mask, coord_mask);
4499 /* With projected textures, texbem only divides the static texture coord,
4500 * not the displacement, so we can't let GL handle this. */
4501 if (flags & WINED3D_PSARGS_PROJECTED)
4503 DWORD div_mask=0;
4504 char coord_div_mask[3];
4505 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4507 case WINED3D_TTFF_COUNT1:
4508 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4509 break;
4510 case WINED3D_TTFF_COUNT2:
4511 div_mask = WINED3DSP_WRITEMASK_1;
4512 break;
4513 case WINED3D_TTFF_COUNT3:
4514 div_mask = WINED3DSP_WRITEMASK_2;
4515 break;
4516 case WINED3D_TTFF_COUNT4:
4517 case WINED3D_TTFF_DISABLE:
4518 div_mask = WINED3DSP_WRITEMASK_3;
4519 break;
4521 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4522 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4525 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4527 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4528 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4529 coord_param.param_str, coord_mask);
4531 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4533 struct glsl_src_param luminance_param;
4534 struct glsl_dst_param dst_param;
4536 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4537 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4539 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4540 dst_param.reg_name, dst_param.mask_str,
4541 luminance_param.param_str, sampler_idx, sampler_idx);
4545 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4547 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4548 struct glsl_src_param src0_param, src1_param;
4550 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4551 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4553 shader_glsl_append_dst(ins->ctx->buffer, ins);
4554 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4555 src0_param.param_str, sampler_idx, src1_param.param_str);
4558 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4559 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4560 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4562 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4563 struct glsl_sample_function sample_function;
4564 struct glsl_src_param src0_param;
4566 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4568 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4569 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4570 "%s.wx", src0_param.reg_name);
4573 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4574 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4575 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4577 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4578 struct glsl_sample_function sample_function;
4579 struct glsl_src_param src0_param;
4581 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4583 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4584 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4585 "%s.yz", src0_param.reg_name);
4588 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4589 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4590 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4592 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4593 struct glsl_sample_function sample_function;
4594 struct glsl_src_param src0_param;
4596 /* Dependent read, not valid with conditional NP2 */
4597 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4598 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4600 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4601 "%s", src0_param.param_str);
4604 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4605 * If any of the first 3 components are < 0, discard this pixel */
4606 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4608 struct glsl_dst_param dst_param;
4610 /* The argument is a destination parameter, and no writemasks are allowed */
4611 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4612 if (ins->ctx->reg_maps->shader_version.major >= 2)
4614 if (ins->ctx->reg_maps->shader_version.major >= 4)
4615 FIXME("SM4 discard not implemented.\n");
4616 /* 2.0 shaders compare all 4 components in texkill */
4617 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4618 } else {
4619 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
4620 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
4621 * 4 components are defined, only the first 3 are used
4623 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4627 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4628 * dst = dot2(src0, src1) + src2 */
4629 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4631 struct glsl_src_param src0_param;
4632 struct glsl_src_param src1_param;
4633 struct glsl_src_param src2_param;
4634 DWORD write_mask;
4635 unsigned int mask_size;
4637 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4638 mask_size = shader_glsl_get_write_mask_size(write_mask);
4640 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4641 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4642 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4644 if (mask_size > 1) {
4645 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4646 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4647 } else {
4648 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4649 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4653 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
4654 const struct wined3d_shader_signature *input_signature,
4655 const struct wined3d_shader_reg_maps *reg_maps,
4656 enum vertexprocessing_mode vertexprocessing)
4658 unsigned int i;
4660 for (i = 0; i < input_signature->element_count; ++i)
4662 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4663 const char *semantic_name;
4664 UINT semantic_idx;
4665 char reg_mask[6];
4667 /* Unused */
4668 if (!(reg_maps->input_registers & (1 << input->register_idx)))
4669 continue;
4671 semantic_name = input->semantic_name;
4672 semantic_idx = input->semantic_idx;
4673 shader_glsl_write_mask_to_str(input->mask, reg_mask);
4675 if (vertexprocessing == vertexshader)
4677 if (input->sysval_semantic == WINED3D_SV_POSITION)
4678 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
4679 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4680 else
4681 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
4682 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
4683 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
4685 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4687 if (semantic_idx < 8 && vertexprocessing == pretransformed)
4688 shader_addline(buffer, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4689 shader->u.ps.input_reg_map[input->register_idx], reg_mask, semantic_idx, reg_mask);
4690 else
4691 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4692 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4694 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4696 if (!semantic_idx)
4697 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4698 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4699 else if (semantic_idx == 1)
4700 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4701 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4702 else
4703 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4704 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4706 else
4708 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4709 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4714 /*********************************************
4715 * Vertex Shader Specific Code begins here
4716 ********************************************/
4718 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4720 struct glsl_program_key key;
4722 key.vs_id = entry->vs.id;
4723 key.gs_id = entry->gs.id;
4724 key.ps_id = entry->ps.id;
4726 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4728 ERR("Failed to insert program entry.\n");
4732 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4733 GLuint vs_id, GLuint gs_id, GLuint ps_id)
4735 struct wine_rb_entry *entry;
4736 struct glsl_program_key key;
4738 key.vs_id = vs_id;
4739 key.gs_id = gs_id;
4740 key.ps_id = ps_id;
4742 entry = wine_rb_get(&priv->program_lookup, &key);
4743 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4746 /* Context activation is done by the caller. */
4747 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4748 struct glsl_shader_prog_link *entry)
4750 struct glsl_program_key key;
4752 key.vs_id = entry->vs.id;
4753 key.gs_id = entry->gs.id;
4754 key.ps_id = entry->ps.id;
4755 wine_rb_remove(&priv->program_lookup, &key);
4757 GL_EXTCALL(glDeleteProgram(entry->id));
4758 if (entry->vs.id)
4759 list_remove(&entry->vs.shader_entry);
4760 if (entry->gs.id)
4761 list_remove(&entry->gs.shader_entry);
4762 if (entry->ps.id)
4763 list_remove(&entry->ps.shader_entry);
4764 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4765 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4766 HeapFree(GetProcessHeap(), 0, entry);
4769 static void handle_ps3_input(struct wined3d_string_buffer *buffer,
4770 const struct wined3d_gl_info *gl_info, const DWORD *map,
4771 const struct wined3d_shader_signature *input_signature,
4772 const struct wined3d_shader_reg_maps *reg_maps_in,
4773 const struct wined3d_shader_signature *output_signature,
4774 const struct wined3d_shader_reg_maps *reg_maps_out)
4776 unsigned int i, j;
4777 DWORD *set;
4778 DWORD in_idx;
4779 unsigned int in_count = vec4_varyings(3, gl_info);
4780 char reg_mask[6];
4781 char destination[50];
4783 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
4785 for (i = 0; i < input_signature->element_count; ++i)
4787 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4789 if (!(reg_maps_in->input_registers & (1 << input->register_idx)))
4790 continue;
4792 in_idx = map[input->register_idx];
4793 /* Declared, but not read register */
4794 if (in_idx == ~0u)
4795 continue;
4796 if (in_idx >= (in_count + 2))
4798 FIXME("More input varyings declared than supported, expect issues.\n");
4799 continue;
4802 if (in_idx == in_count)
4803 sprintf(destination, "gl_FrontColor");
4804 else if (in_idx == in_count + 1)
4805 sprintf(destination, "gl_FrontSecondaryColor");
4806 else
4807 sprintf(destination, "ps_link[%u]", in_idx);
4809 if (!set[in_idx])
4810 set[in_idx] = ~0u;
4812 for (j = 0; j < output_signature->element_count; ++j)
4814 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
4815 DWORD mask;
4817 if (!(reg_maps_out->output_registers & (1 << output->register_idx))
4818 || input->semantic_idx != output->semantic_idx
4819 || strcmp(input->semantic_name, output->semantic_name)
4820 || !(mask = input->mask & output->mask))
4821 continue;
4823 if (set[in_idx] == ~0u)
4824 set[in_idx] = mask;
4825 else
4826 set[in_idx] |= mask;
4827 shader_glsl_write_mask_to_str(mask, reg_mask);
4829 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4830 destination, reg_mask, output->register_idx, reg_mask);
4834 for (i = 0; i < in_count + 2; ++i)
4836 unsigned int size;
4838 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4839 continue;
4841 if (set[i] == ~0U) set[i] = 0;
4843 size = 0;
4844 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4845 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4846 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4847 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4848 reg_mask[size] = '\0';
4850 if (i == in_count)
4851 sprintf(destination, "gl_FrontColor");
4852 else if (i == in_count + 1)
4853 sprintf(destination, "gl_FrontSecondaryColor");
4854 else
4855 sprintf(destination, "ps_link[%u]", i);
4857 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
4858 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
4861 HeapFree(GetProcessHeap(), 0, set);
4864 /* Context activation is done by the caller. */
4865 static GLuint generate_param_reorder_function(struct wined3d_string_buffer *buffer,
4866 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4867 const struct wined3d_gl_info *gl_info)
4869 GLuint ret = 0;
4870 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4871 unsigned int i;
4872 const char *semantic_name;
4873 UINT semantic_idx;
4874 char reg_mask[6];
4876 string_buffer_clear(buffer);
4878 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &vs->reg_maps.shader_version));
4880 if (ps_major < 3)
4882 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4884 for (i = 0; i < vs->output_signature.element_count; ++i)
4886 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
4887 DWORD write_mask;
4889 if (!(vs->reg_maps.output_registers & (1 << output->register_idx)))
4890 continue;
4892 semantic_name = output->semantic_name;
4893 semantic_idx = output->semantic_idx;
4894 write_mask = output->mask;
4895 shader_glsl_write_mask_to_str(write_mask, reg_mask);
4897 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4899 if (!semantic_idx)
4900 shader_addline(buffer, "gl_FrontColor%s = vs_out[%u]%s;\n",
4901 reg_mask, output->register_idx, reg_mask);
4902 else if (semantic_idx == 1)
4903 shader_addline(buffer, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4904 reg_mask, output->register_idx, reg_mask);
4906 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
4908 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4909 reg_mask, output->register_idx, reg_mask);
4911 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4913 if (semantic_idx < 8)
4915 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
4916 write_mask |= WINED3DSP_WRITEMASK_3;
4918 shader_addline(buffer, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4919 semantic_idx, reg_mask, output->register_idx, reg_mask);
4920 if (!(write_mask & WINED3DSP_WRITEMASK_3))
4921 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
4924 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4926 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", output->register_idx, reg_mask[1]);
4928 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
4930 shader_addline(buffer, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n",
4931 output->register_idx, reg_mask[1]);
4934 shader_addline(buffer, "}\n");
4936 else
4938 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
4939 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4940 shader_addline(buffer, "varying vec4 ps_link[%u];\n", in_count);
4941 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4943 /* First, sort out position and point size. Those are not passed to the pixel shader */
4944 for (i = 0; i < vs->output_signature.element_count; ++i)
4946 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
4948 if (!(vs->reg_maps.output_registers & (1 << output->register_idx)))
4949 continue;
4951 semantic_name = output->semantic_name;
4952 semantic_idx = output->semantic_idx;
4953 shader_glsl_write_mask_to_str(output->mask, reg_mask);
4955 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
4957 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4958 reg_mask, output->register_idx, reg_mask);
4960 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4962 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", output->register_idx, reg_mask[1]);
4966 /* Then, fix the pixel shader input */
4967 handle_ps3_input(buffer, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
4968 &ps->reg_maps, &vs->output_signature, &vs->reg_maps);
4970 shader_addline(buffer, "}\n");
4973 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
4974 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
4975 shader_glsl_compile(gl_info, ret, buffer->buffer);
4977 return ret;
4980 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer)
4982 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4983 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4984 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4985 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4986 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4987 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4990 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
4992 switch (mode)
4994 case WINED3D_FFP_PS_FOG_OFF:
4995 return;
4997 case WINED3D_FFP_PS_FOG_LINEAR:
4998 shader_addline(buffer, "float Fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;\n");
4999 break;
5001 case WINED3D_FFP_PS_FOG_EXP:
5002 /* Fog = e^-(gl_Fog.density * gl_FogFragCoord) */
5003 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
5004 break;
5006 case WINED3D_FFP_PS_FOG_EXP2:
5007 /* Fog = e^-((gl_Fog.density * gl_FogFragCoord)^2) */
5008 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
5009 break;
5011 default:
5012 ERR("Invalid fog mode %#x.\n", mode);
5013 return;
5016 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, clamp(Fog, 0.0, 1.0));\n");
5019 /* Context activation is done by the caller. */
5020 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
5021 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
5022 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
5024 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5025 const struct wined3d_gl_info *gl_info = context->gl_info;
5026 const DWORD *function = shader->function;
5027 struct shader_glsl_ctx_priv priv_ctx;
5029 /* Create the hw GLSL shader object and assign it as the shader->prgId */
5030 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5032 memset(&priv_ctx, 0, sizeof(priv_ctx));
5033 priv_ctx.cur_ps_args = args;
5034 priv_ctx.cur_np2fixup_info = np2fixup_info;
5036 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5038 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5039 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5040 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5041 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
5042 /* The spec says that it doesn't have to be explicitly enabled, but the
5043 * nvidia drivers write a warning if we don't do so. */
5044 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5045 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5046 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5047 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5048 if (gl_info->supported[EXT_GPU_SHADER4])
5049 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5051 /* Base Declarations */
5052 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5054 /* Pack 3.0 inputs */
5055 if (reg_maps->shader_version.major >= 3)
5056 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args->vp_mode);
5058 /* Base Shader Body */
5059 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5061 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
5062 if (reg_maps->shader_version.major < 2)
5064 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
5065 shader_addline(buffer, "gl_FragData[0] = R0;\n");
5068 if (args->srgb_correction)
5069 shader_glsl_generate_srgb_write_correction(buffer);
5071 /* SM < 3 does not replace the fog stage. */
5072 if (reg_maps->shader_version.major < 3)
5073 shader_glsl_generate_fog_code(buffer, args->fog);
5075 shader_addline(buffer, "}\n");
5077 TRACE("Compiling shader object %u.\n", shader_id);
5078 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5080 return shader_id;
5083 /* Context activation is done by the caller. */
5084 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
5085 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
5086 const struct vs_compile_args *args)
5088 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5089 const struct wined3d_gl_info *gl_info = context->gl_info;
5090 const DWORD *function = shader->function;
5091 struct shader_glsl_ctx_priv priv_ctx;
5093 /* Create the hw GLSL shader program and assign it as the shader->prgId */
5094 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5096 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5098 if (gl_info->supported[ARB_DRAW_INSTANCED])
5099 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
5100 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5101 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5102 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5103 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5104 if (gl_info->supported[EXT_GPU_SHADER4])
5105 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5107 memset(&priv_ctx, 0, sizeof(priv_ctx));
5108 priv_ctx.cur_vs_args = args;
5110 /* Base Declarations */
5111 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5113 /* Base Shader Body */
5114 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5116 /* Unpack outputs */
5117 shader_addline(buffer, "order_ps_input(vs_out);\n");
5119 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
5120 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
5121 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
5122 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
5124 if (args->fog_src == VS_FOG_Z)
5125 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
5126 else if (!reg_maps->fog)
5127 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
5129 /* We always store the clipplanes without y inversion */
5130 if (args->clip_enabled)
5131 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
5133 /* Write the final position.
5135 * OpenGL coordinates specify the center of the pixel while d3d coords specify
5136 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
5137 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
5138 * contains 1.0 to allow a mad.
5140 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
5141 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
5143 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
5145 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
5146 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
5147 * which is the same as z = z * 2 - w.
5149 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
5151 shader_addline(buffer, "}\n");
5153 TRACE("Compiling shader object %u.\n", shader_id);
5154 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5156 return shader_id;
5159 /* Context activation is done by the caller. */
5160 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
5161 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader)
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 GLuint shader_id;
5169 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
5171 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5173 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
5174 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
5175 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5176 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5177 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5178 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5179 if (gl_info->supported[EXT_GPU_SHADER4])
5180 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5182 memset(&priv_ctx, 0, sizeof(priv_ctx));
5183 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5184 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5185 shader_addline(buffer, "}\n");
5187 TRACE("Compiling shader object %u.\n", shader_id);
5188 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5190 return shader_id;
5193 static GLuint find_glsl_pshader(const struct wined3d_context *context,
5194 struct wined3d_string_buffer *buffer, struct wined3d_shader *shader,
5195 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
5197 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
5198 struct glsl_shader_private *shader_data;
5199 struct ps_np2fixup_info *np2fixup;
5200 UINT i;
5201 DWORD new_size;
5202 GLuint ret;
5204 if (!shader->backend_data)
5206 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5207 if (!shader->backend_data)
5209 ERR("Failed to allocate backend data.\n");
5210 return 0;
5213 shader_data = shader->backend_data;
5214 gl_shaders = shader_data->gl_shaders.ps;
5216 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5217 * so a linear search is more performant than a hashmap or a binary search
5218 * (cache coherency etc)
5220 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5222 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
5224 if (args->np2_fixup)
5225 *np2fixup_info = &gl_shaders[i].np2fixup;
5226 return gl_shaders[i].id;
5230 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5231 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5232 if (shader_data->num_gl_shaders)
5234 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5235 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
5236 new_size * sizeof(*gl_shaders));
5238 else
5240 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5241 new_size = 1;
5244 if(!new_array) {
5245 ERR("Out of memory\n");
5246 return 0;
5248 shader_data->gl_shaders.ps = new_array;
5249 shader_data->shader_array_size = new_size;
5250 gl_shaders = new_array;
5253 gl_shaders[shader_data->num_gl_shaders].args = *args;
5255 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
5256 memset(np2fixup, 0, sizeof(*np2fixup));
5257 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
5259 pixelshader_update_resource_types(shader, args->tex_types);
5261 string_buffer_clear(buffer);
5262 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
5263 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5265 return ret;
5268 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
5269 const DWORD use_map) {
5270 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
5271 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
5272 return stored->fog_src == new->fog_src;
5275 static GLuint find_glsl_vshader(const struct wined3d_context *context,
5276 struct wined3d_string_buffer *buffer, struct wined3d_shader *shader,
5277 const struct vs_compile_args *args)
5279 UINT i;
5280 DWORD new_size;
5281 DWORD use_map = context->stream_info.use_map;
5282 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
5283 struct glsl_shader_private *shader_data;
5284 GLuint ret;
5286 if (!shader->backend_data)
5288 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5289 if (!shader->backend_data)
5291 ERR("Failed to allocate backend data.\n");
5292 return 0;
5295 shader_data = shader->backend_data;
5296 gl_shaders = shader_data->gl_shaders.vs;
5298 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5299 * so a linear search is more performant than a hashmap or a binary search
5300 * (cache coherency etc)
5302 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5304 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
5305 return gl_shaders[i].id;
5308 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5310 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5311 if (shader_data->num_gl_shaders)
5313 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5314 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
5315 new_size * sizeof(*gl_shaders));
5317 else
5319 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5320 new_size = 1;
5323 if(!new_array) {
5324 ERR("Out of memory\n");
5325 return 0;
5327 shader_data->gl_shaders.vs = new_array;
5328 shader_data->shader_array_size = new_size;
5329 gl_shaders = new_array;
5332 gl_shaders[shader_data->num_gl_shaders].args = *args;
5334 string_buffer_clear(buffer);
5335 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
5336 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5338 return ret;
5341 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
5342 struct wined3d_string_buffer *buffer, struct wined3d_shader *shader)
5344 struct glsl_gs_compiled_shader *gl_shaders;
5345 struct glsl_shader_private *shader_data;
5346 GLuint ret;
5348 if (!shader->backend_data)
5350 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
5352 ERR("Failed to allocate backend data.\n");
5353 return 0;
5356 shader_data = shader->backend_data;
5357 gl_shaders = shader_data->gl_shaders.gs;
5359 if (shader_data->num_gl_shaders)
5360 return gl_shaders[0].id;
5362 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5364 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
5366 ERR("Failed to allocate GL shader array.\n");
5367 return 0;
5369 shader_data->shader_array_size = 1;
5370 gl_shaders = shader_data->gl_shaders.gs;
5372 string_buffer_clear(buffer);
5373 ret = shader_glsl_generate_geometry_shader(context, buffer, shader);
5374 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5376 return ret;
5379 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
5381 switch (mcs)
5383 case WINED3D_MCS_MATERIAL:
5384 return material;
5385 case WINED3D_MCS_COLOR1:
5386 return "gl_Color";
5387 case WINED3D_MCS_COLOR2:
5388 return "gl_SecondaryColor";
5389 default:
5390 ERR("Invalid material color source %#x.\n", mcs);
5391 return "<invalid>";
5395 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
5396 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
5398 const char *diffuse, *specular, *emission, *ambient;
5399 enum wined3d_light_type light_type;
5400 unsigned int i;
5402 if (!settings->lighting)
5404 shader_addline(buffer, "gl_FrontColor = gl_Color;\n");
5405 shader_addline(buffer, "gl_FrontSecondaryColor = gl_SecondaryColor;\n");
5406 return;
5409 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
5410 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
5411 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
5412 shader_addline(buffer, "vec3 dir, dst;\n");
5413 shader_addline(buffer, "float att, t;\n");
5415 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
5416 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
5417 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
5418 emission = shader_glsl_ffp_mcs(settings->emission_source, "ffp_material.emission");
5420 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
5422 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
5423 switch (light_type)
5425 case WINED3D_LIGHT_POINT:
5426 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5427 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5428 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5429 shader_addline(buffer, "dst.x = 1.0;\n");
5430 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5431 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5432 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", i, i, i);
5433 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz / att;\n", i);
5434 if (!settings->normal)
5436 shader_addline(buffer, "}\n");
5437 break;
5439 shader_addline(buffer, "dir = normalize(dir);\n");
5440 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5441 " * ffp_light[%u].diffuse.xyz) / att;\n", i);
5442 if (settings->localviewer)
5443 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5444 else
5445 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5446 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5447 " * ffp_light[%u].specular) / att;\n", i);
5448 shader_addline(buffer, "}\n");
5449 break;
5451 case WINED3D_LIGHT_SPOT:
5452 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5453 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5454 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5455 shader_addline(buffer, "dst.x = 1.0;\n");
5456 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5457 shader_addline(buffer, "dir = normalize(dir);\n");
5458 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", i);
5459 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", i);
5460 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", i);
5461 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
5462 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff)"
5463 " / dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5464 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5465 i, i, i, i, i, i, i);
5466 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5467 if (!settings->normal)
5469 shader_addline(buffer, "}\n");
5470 break;
5472 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5473 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
5474 if (settings->localviewer)
5475 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5476 else
5477 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5478 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5479 " * ffp_light[%u].specular) * att;\n", i);
5480 shader_addline(buffer, "}\n");
5481 break;
5483 case WINED3D_LIGHT_DIRECTIONAL:
5484 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
5485 if (!settings->normal)
5486 break;
5487 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", i);
5488 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5489 " * ffp_light[%u].diffuse.xyz;\n", i);
5490 /* TODO: In the non-local viewer case the halfvector is constant
5491 * and could be precomputed and stored in a uniform. */
5492 if (settings->localviewer)
5493 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5494 else
5495 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5496 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
5497 " * ffp_light[%u].specular;\n", i);
5498 break;
5500 default:
5501 if (light_type)
5502 FIXME("Unhandled light type %#x.\n", light_type);
5503 continue;
5507 shader_addline(buffer, "gl_FrontColor.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
5508 ambient, diffuse, emission);
5509 shader_addline(buffer, "gl_FrontColor.w = %s.w;\n", diffuse);
5510 shader_addline(buffer, "gl_FrontSecondaryColor = %s * specular;\n", specular);
5513 /* Context activation is done by the caller. */
5514 static GLuint shader_glsl_generate_ffp_vertex_shader(struct wined3d_string_buffer *buffer,
5515 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
5517 GLuint shader_obj;
5518 unsigned int i;
5520 string_buffer_clear(buffer);
5522 shader_addline(buffer, "#version 120\n");
5523 shader_addline(buffer, "\n");
5525 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix;\n");
5526 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
5527 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
5528 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
5530 shader_addline(buffer, "uniform struct\n{\n");
5531 shader_addline(buffer, " vec4 emission;\n");
5532 shader_addline(buffer, " vec4 ambient;\n");
5533 shader_addline(buffer, " vec4 diffuse;\n");
5534 shader_addline(buffer, " vec4 specular;\n");
5535 shader_addline(buffer, " float shininess;\n");
5536 shader_addline(buffer, "} ffp_material;\n");
5538 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
5539 shader_addline(buffer, "uniform struct\n{\n");
5540 shader_addline(buffer, " vec4 diffuse;\n");
5541 shader_addline(buffer, " vec4 specular;\n");
5542 shader_addline(buffer, " vec4 ambient;\n");
5543 shader_addline(buffer, " vec4 position;\n");
5544 shader_addline(buffer, " vec3 direction;\n");
5545 shader_addline(buffer, " float range;\n");
5546 shader_addline(buffer, " float falloff;\n");
5547 shader_addline(buffer, " float c_att;\n");
5548 shader_addline(buffer, " float l_att;\n");
5549 shader_addline(buffer, " float q_att;\n");
5550 shader_addline(buffer, " float cos_htheta;\n");
5551 shader_addline(buffer, " float cos_hphi;\n");
5552 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
5554 shader_addline(buffer, "\nvoid main()\n{\n");
5555 shader_addline(buffer, "float m;\n");
5556 shader_addline(buffer, "vec3 r;\n");
5558 if (settings->transformed)
5560 shader_addline(buffer, "vec4 ec_pos = vec4(gl_Vertex.xyz, 1.0);\n");
5561 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5562 shader_addline(buffer, "if (gl_Vertex.w != 0.0) gl_Position /= gl_Vertex.w;\n");
5564 else
5566 shader_addline(buffer, "vec4 ec_pos = ffp_modelview_matrix * gl_Vertex;\n");
5567 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5568 if (settings->clipping)
5569 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
5570 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
5573 if (!settings->normal)
5574 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
5575 else if (settings->normalize)
5576 shader_addline(buffer, "vec3 normal = normalize(ffp_normal_matrix * gl_Normal);\n");
5577 else
5578 shader_addline(buffer, "vec3 normal = ffp_normal_matrix * gl_Normal;\n");
5580 shader_glsl_ffp_vertex_lighting(buffer, settings, gl_info);
5582 for (i = 0; i < MAX_TEXTURES; ++i)
5584 switch (settings->texgen[i] << WINED3D_FFP_TCI_SHIFT)
5586 case WINED3DTSS_TCI_PASSTHRU:
5587 if (settings->texcoords & (1 << i))
5588 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u] * gl_MultiTexCoord%d;\n",
5589 i, i, i);
5590 break;
5592 case WINED3DTSS_TCI_CAMERASPACENORMAL:
5593 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
5594 break;
5596 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
5597 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
5598 break;
5600 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
5601 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u]"
5602 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
5603 break;
5605 case WINED3DTSS_TCI_SPHEREMAP:
5606 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
5607 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
5608 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u]"
5609 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
5610 break;
5612 default:
5613 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
5614 break;
5618 switch (settings->fog_mode)
5620 case WINED3D_FFP_VS_FOG_OFF:
5621 break;
5623 case WINED3D_FFP_VS_FOG_FOGCOORD:
5624 shader_addline(buffer, "gl_FogFragCoord = gl_SecondaryColor.w * 255.0;\n");
5625 break;
5627 case WINED3D_FFP_VS_FOG_RANGE:
5628 shader_addline(buffer, "gl_FogFragCoord = length(ec_pos.xyz);\n");
5629 break;
5631 case WINED3D_FFP_VS_FOG_DEPTH:
5632 if (settings->ortho_fog)
5633 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
5634 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z * 0.5 + 0.5;\n");
5635 else if (settings->transformed)
5636 shader_addline(buffer, "gl_FogFragCoord = ec_pos.z;\n");
5637 else
5638 shader_addline(buffer, "gl_FogFragCoord = abs(ec_pos.z);\n");
5639 break;
5641 default:
5642 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
5643 break;
5646 if (settings->point_size)
5648 shader_addline(buffer, "gl_PointSize = gl_Point.size / sqrt(gl_Point.distanceConstantAttenuation"
5649 " + gl_Point.distanceLinearAttenuation * length(ec_pos.xyz)"
5650 " + gl_Point.distanceQuadraticAttenuation * dot(ec_pos.xyz, ec_pos.xyz));\n");
5651 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, gl_Point.sizeMin, gl_Point.sizeMax);\n");
5654 shader_addline(buffer, "}\n");
5656 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5657 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5659 return shader_obj;
5662 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
5663 DWORD argnum, unsigned int stage, DWORD arg)
5665 const char *ret;
5667 if (arg == ARG_UNUSED)
5668 return "<unused arg>";
5670 switch (arg & WINED3DTA_SELECTMASK)
5672 case WINED3DTA_DIFFUSE:
5673 ret = "gl_Color";
5674 break;
5676 case WINED3DTA_CURRENT:
5677 if (!stage)
5678 ret = "gl_Color";
5679 else
5680 ret = "ret";
5681 break;
5683 case WINED3DTA_TEXTURE:
5684 switch (stage)
5686 case 0: ret = "tex0"; break;
5687 case 1: ret = "tex1"; break;
5688 case 2: ret = "tex2"; break;
5689 case 3: ret = "tex3"; break;
5690 case 4: ret = "tex4"; break;
5691 case 5: ret = "tex5"; break;
5692 case 6: ret = "tex6"; break;
5693 case 7: ret = "tex7"; break;
5694 default:
5695 ret = "<invalid texture>";
5696 break;
5698 break;
5700 case WINED3DTA_TFACTOR:
5701 ret = "tex_factor";
5702 break;
5704 case WINED3DTA_SPECULAR:
5705 ret = "gl_SecondaryColor";
5706 break;
5708 case WINED3DTA_TEMP:
5709 ret = "temp_reg";
5710 break;
5712 case WINED3DTA_CONSTANT:
5713 switch (stage)
5715 case 0: ret = "tss_const0"; break;
5716 case 1: ret = "tss_const1"; break;
5717 case 2: ret = "tss_const2"; break;
5718 case 3: ret = "tss_const3"; break;
5719 case 4: ret = "tss_const4"; break;
5720 case 5: ret = "tss_const5"; break;
5721 case 6: ret = "tss_const6"; break;
5722 case 7: ret = "tss_const7"; break;
5723 default:
5724 ret = "<invalid constant>";
5725 break;
5727 break;
5729 default:
5730 return "<unhandled arg>";
5733 if (arg & WINED3DTA_COMPLEMENT)
5735 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
5736 if (argnum == 0)
5737 ret = "arg0";
5738 else if (argnum == 1)
5739 ret = "arg1";
5740 else if (argnum == 2)
5741 ret = "arg2";
5744 if (arg & WINED3DTA_ALPHAREPLICATE)
5746 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
5747 if (argnum == 0)
5748 ret = "arg0";
5749 else if (argnum == 1)
5750 ret = "arg1";
5751 else if (argnum == 2)
5752 ret = "arg2";
5755 return ret;
5758 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
5759 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
5761 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5763 if (color && alpha)
5764 dstmask = "";
5765 else if (color)
5766 dstmask = ".xyz";
5767 else
5768 dstmask = ".w";
5770 if (dst == tempreg)
5771 dstreg = "temp_reg";
5772 else
5773 dstreg = "ret";
5775 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
5776 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
5777 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
5779 switch (op)
5781 case WINED3D_TOP_DISABLE:
5782 if (!stage)
5783 shader_addline(buffer, "%s%s = gl_Color%s;\n", dstreg, dstmask, dstmask);
5784 break;
5786 case WINED3D_TOP_SELECT_ARG1:
5787 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
5788 break;
5790 case WINED3D_TOP_SELECT_ARG2:
5791 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
5792 break;
5794 case WINED3D_TOP_MODULATE:
5795 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5796 break;
5798 case WINED3D_TOP_MODULATE_4X:
5799 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
5800 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5801 break;
5803 case WINED3D_TOP_MODULATE_2X:
5804 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
5805 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5806 break;
5808 case WINED3D_TOP_ADD:
5809 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
5810 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5811 break;
5813 case WINED3D_TOP_ADD_SIGNED:
5814 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
5815 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5816 break;
5818 case WINED3D_TOP_ADD_SIGNED_2X:
5819 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
5820 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5821 break;
5823 case WINED3D_TOP_SUBTRACT:
5824 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
5825 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5826 break;
5828 case WINED3D_TOP_ADD_SMOOTH:
5829 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
5830 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
5831 break;
5833 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
5834 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5835 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5836 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5837 break;
5839 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5840 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5841 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5842 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5843 break;
5845 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5846 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
5847 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5848 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5849 break;
5851 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5852 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5853 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5854 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
5855 break;
5857 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
5858 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
5859 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5860 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5861 break;
5863 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
5864 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
5865 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5866 break;
5868 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
5869 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
5870 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5871 break;
5873 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
5874 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5875 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5876 break;
5877 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
5878 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
5879 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5880 break;
5882 case WINED3D_TOP_BUMPENVMAP:
5883 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5884 /* These are handled in the first pass, nothing to do. */
5885 break;
5887 case WINED3D_TOP_DOTPRODUCT3:
5888 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
5889 dstreg, dstmask, arg1, arg2, dstmask);
5890 break;
5892 case WINED3D_TOP_MULTIPLY_ADD:
5893 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
5894 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
5895 break;
5897 case WINED3D_TOP_LERP:
5898 /* MSDN isn't quite right here. */
5899 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
5900 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
5901 break;
5903 default:
5904 FIXME("Unhandled operation %#x.\n", op);
5905 break;
5909 /* Context activation is done by the caller. */
5910 static GLuint shader_glsl_generate_ffp_fragment_shader(struct wined3d_string_buffer *buffer,
5911 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
5913 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
5914 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
5915 const char *final_combiner_src = "ret";
5916 UINT lowest_disabled_stage;
5917 GLuint shader_id;
5918 DWORD arg0, arg1, arg2;
5919 unsigned int stage;
5921 string_buffer_clear(buffer);
5923 /* Find out which textures are read */
5924 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5926 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5927 break;
5929 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
5930 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
5931 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
5933 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
5934 || (stage == 0 && settings->color_key_enabled))
5935 tex_map |= 1 << stage;
5936 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5937 tfactor_used = TRUE;
5938 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5939 tempreg_used = TRUE;
5940 if (settings->op[stage].dst == tempreg)
5941 tempreg_used = TRUE;
5942 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5943 tss_const_map |= 1 << stage;
5945 switch (settings->op[stage].cop)
5947 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5948 lum_map |= 1 << stage;
5949 /* fall through */
5950 case WINED3D_TOP_BUMPENVMAP:
5951 bump_map |= 1 << stage;
5952 /* fall through */
5953 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5954 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5955 tex_map |= 1 << stage;
5956 break;
5958 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5959 tfactor_used = TRUE;
5960 break;
5962 default:
5963 break;
5966 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5967 continue;
5969 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
5970 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
5971 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
5973 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5974 tex_map |= 1 << stage;
5975 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5976 tfactor_used = TRUE;
5977 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5978 tempreg_used = TRUE;
5979 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5980 tss_const_map |= 1 << stage;
5982 lowest_disabled_stage = stage;
5984 shader_addline(buffer, "#version 120\n");
5986 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5987 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5989 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
5990 shader_addline(buffer, "vec4 ret;\n");
5991 if (tempreg_used || settings->sRGB_write)
5992 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
5993 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
5995 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5997 if (tss_const_map & (1 << stage))
5998 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
6000 if (!(tex_map & (1 << stage)))
6001 continue;
6003 switch (settings->op[stage].tex_type)
6005 case WINED3D_GL_RES_TYPE_TEX_1D:
6006 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
6007 break;
6008 case WINED3D_GL_RES_TYPE_TEX_2D:
6009 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
6010 break;
6011 case WINED3D_GL_RES_TYPE_TEX_3D:
6012 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
6013 break;
6014 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6015 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
6016 break;
6017 case WINED3D_GL_RES_TYPE_TEX_RECT:
6018 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
6019 break;
6020 default:
6021 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
6022 break;
6025 shader_addline(buffer, "vec4 tex%u;\n", stage);
6027 if (!(bump_map & (1 << stage)))
6028 continue;
6029 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
6031 if (!(lum_map & (1 << stage)))
6032 continue;
6033 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
6034 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
6036 if (tfactor_used)
6037 shader_addline(buffer, "uniform vec4 tex_factor;\n");
6038 if (settings->color_key_enabled)
6039 shader_addline(buffer, "uniform vec4 color_key;\n");
6040 shader_addline(buffer, "uniform vec4 specular_enable;\n");
6042 if (settings->sRGB_write)
6044 shader_addline(buffer, "const vec4 srgb_const0 = ");
6045 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
6046 shader_addline(buffer, ";\n");
6047 shader_addline(buffer, "const vec4 srgb_const1 = ");
6048 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
6049 shader_addline(buffer, ";\n");
6052 shader_addline(buffer, "void main()\n{\n");
6054 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
6055 shader_addline(buffer, "if (any(lessThan(gl_TexCoord[7], vec4(0.0)))) discard;\n");
6057 /* Generate texture sampling instructions) */
6058 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
6060 const char *texture_function, *coord_mask;
6061 char tex_reg_name[8];
6062 BOOL proj;
6064 if (!(tex_map & (1 << stage)))
6065 continue;
6067 if (settings->op[stage].projected == proj_none)
6069 proj = FALSE;
6071 else if (settings->op[stage].projected == proj_count4
6072 || settings->op[stage].projected == proj_count3)
6074 proj = TRUE;
6076 else
6078 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
6079 proj = TRUE;
6082 switch (settings->op[stage].tex_type)
6084 case WINED3D_GL_RES_TYPE_TEX_1D:
6085 if (proj)
6087 texture_function = "texture1DProj";
6088 coord_mask = "xw";
6090 else
6092 texture_function = "texture1D";
6093 coord_mask = "x";
6095 break;
6096 case WINED3D_GL_RES_TYPE_TEX_2D:
6097 if (proj)
6099 texture_function = "texture2DProj";
6100 coord_mask = "xyw";
6102 else
6104 texture_function = "texture2D";
6105 coord_mask = "xy";
6107 break;
6108 case WINED3D_GL_RES_TYPE_TEX_3D:
6109 if (proj)
6111 texture_function = "texture3DProj";
6112 coord_mask = "xyzw";
6114 else
6116 texture_function = "texture3D";
6117 coord_mask = "xyz";
6119 break;
6120 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6121 texture_function = "textureCube";
6122 coord_mask = "xyz";
6123 break;
6124 case WINED3D_GL_RES_TYPE_TEX_RECT:
6125 if (proj)
6127 texture_function = "texture2DRectProj";
6128 coord_mask = "xyw";
6130 else
6132 texture_function = "texture2DRect";
6133 coord_mask = "xy";
6135 break;
6136 default:
6137 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
6138 texture_function = "";
6139 coord_mask = "xyzw";
6140 break;
6143 if (stage > 0
6144 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
6145 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
6147 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
6149 /* With projective textures, texbem only divides the static
6150 * texture coord, not the displacement, so multiply the
6151 * displacement with the dividing parameter before passing it to
6152 * TXP. */
6153 if (settings->op[stage].projected != proj_none)
6155 if (settings->op[stage].projected == proj_count4)
6157 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].w) + gl_TexCoord[%u].xy;\n",
6158 stage, stage);
6159 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].ww;\n", stage);
6161 else
6163 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].z) + gl_TexCoord[%u].xy;\n",
6164 stage, stage);
6165 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].zz;\n", stage);
6168 else
6170 shader_addline(buffer, "ret = gl_TexCoord[%u] + ret.xyxy;\n", stage);
6173 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
6174 stage, texture_function, stage, coord_mask);
6176 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6177 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
6178 stage, stage - 1, stage - 1, stage - 1);
6180 else if (settings->op[stage].projected == proj_count3)
6182 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].xyz);\n",
6183 stage, texture_function, stage, stage);
6185 else
6187 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].%s);\n",
6188 stage, texture_function, stage, stage, coord_mask);
6191 sprintf(tex_reg_name, "tex%u", stage);
6192 shader_glsl_color_correction_ext(buffer, tex_reg_name, WINED3DSP_WRITEMASK_ALL,
6193 settings->op[stage].color_fixup);
6196 if (settings->color_key_enabled)
6197 shader_addline(buffer, "if (all(equal(tex0, color_key))) discard;\n");
6199 /* Generate the main shader */
6200 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6202 BOOL op_equal;
6204 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6206 if (!stage)
6207 final_combiner_src = "gl_Color";
6208 break;
6211 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6212 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6213 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
6214 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6215 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6216 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
6217 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6218 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6219 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
6220 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6221 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6222 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
6223 else
6224 op_equal = settings->op[stage].aop == settings->op[stage].cop
6225 && settings->op[stage].carg0 == settings->op[stage].aarg0
6226 && settings->op[stage].carg1 == settings->op[stage].aarg1
6227 && settings->op[stage].carg2 == settings->op[stage].aarg2;
6229 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6231 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6232 settings->op[stage].cop, settings->op[stage].carg0,
6233 settings->op[stage].carg1, settings->op[stage].carg2);
6234 if (!stage)
6235 shader_addline(buffer, "ret.w = gl_Color.w;\n");
6237 else if (op_equal)
6239 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
6240 settings->op[stage].cop, settings->op[stage].carg0,
6241 settings->op[stage].carg1, settings->op[stage].carg2);
6243 else
6245 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6246 settings->op[stage].cop, settings->op[stage].carg0,
6247 settings->op[stage].carg1, settings->op[stage].carg2);
6248 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
6249 settings->op[stage].aop, settings->op[stage].aarg0,
6250 settings->op[stage].aarg1, settings->op[stage].aarg2);
6254 shader_addline(buffer, "gl_FragData[0] = gl_SecondaryColor * specular_enable + %s;\n", final_combiner_src);
6256 if (settings->sRGB_write)
6257 shader_glsl_generate_srgb_write_correction(buffer);
6259 shader_glsl_generate_fog_code(buffer, settings->fog);
6261 shader_addline(buffer, "}\n");
6263 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6264 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6265 return shader_id;
6268 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
6269 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
6271 struct glsl_ffp_vertex_shader *shader;
6272 const struct wine_rb_entry *entry;
6274 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
6275 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
6277 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
6278 return NULL;
6280 shader->desc.settings = *settings;
6281 shader->id = shader_glsl_generate_ffp_vertex_shader(&priv->shader_buffer, settings, gl_info);
6282 list_init(&shader->linked_programs);
6283 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
6284 ERR("Failed to insert ffp vertex shader.\n");
6286 return shader;
6289 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
6290 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
6292 struct glsl_ffp_fragment_shader *glsl_desc;
6293 const struct ffp_frag_desc *desc;
6295 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
6296 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
6298 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
6299 return NULL;
6301 glsl_desc->entry.settings = *args;
6302 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(&priv->shader_buffer, args, gl_info);
6303 list_init(&glsl_desc->linked_programs);
6304 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
6306 return glsl_desc;
6310 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
6311 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
6313 unsigned int i;
6314 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6316 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
6317 sizeof(GLuint) * gl_info->limits.glsl_vs_float_constants);
6318 for (i = 0; i < vs_c_count; ++i)
6320 string_buffer_sprintf(name, "vs_c[%u]", i);
6321 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6323 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
6324 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLuint));
6326 for (i = 0; i < MAX_CONST_I; ++i)
6328 string_buffer_sprintf(name, "vs_i[%u]", i);
6329 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6332 for (i = 0; i < MAX_CONST_B; ++i)
6334 string_buffer_sprintf(name, "vs_b[%u]", i);
6335 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6338 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "posFixup"));
6340 vs->modelview_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_modelview_matrix"));
6341 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
6342 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
6343 for (i = 0; i < MAX_TEXTURES; ++i)
6345 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
6346 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6348 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
6349 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
6350 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
6351 vs->material_emission_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emission"));
6352 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
6353 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
6354 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
6356 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
6357 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6358 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
6359 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6360 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
6361 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6362 string_buffer_sprintf(name, "ffp_light[%u].position", i);
6363 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6364 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
6365 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6366 string_buffer_sprintf(name, "ffp_light[%u].range", i);
6367 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6368 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
6369 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6370 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
6371 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6372 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
6373 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6374 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
6375 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6376 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
6377 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6378 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
6379 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6382 string_buffer_release(&priv->string_buffers, name);
6385 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
6386 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
6388 unsigned int i;
6389 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6391 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
6392 sizeof(GLuint) * gl_info->limits.glsl_ps_float_constants);
6393 for (i = 0; i < ps_c_count; ++i)
6395 string_buffer_sprintf(name, "ps_c[%u]", i);
6396 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6398 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
6399 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLuint));
6401 for (i = 0; i < MAX_CONST_I; ++i)
6403 string_buffer_sprintf(name, "ps_i[%u]", i);
6404 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6407 for (i = 0; i < MAX_CONST_B; ++i)
6409 string_buffer_sprintf(name, "ps_b[%u]", i);
6410 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6413 for (i = 0; i < MAX_TEXTURES; ++i)
6415 string_buffer_sprintf(name, "bumpenv_mat%u", i);
6416 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6417 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
6418 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6419 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
6420 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6421 string_buffer_sprintf(name, "tss_const%u", i);
6422 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6425 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
6426 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
6427 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
6428 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
6429 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
6431 string_buffer_release(&priv->string_buffers, name);
6434 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
6435 struct shader_glsl_priv *priv, GLuint program_id,
6436 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
6438 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6439 GLuint block_idx;
6440 unsigned int i;
6441 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6443 for (i = 0; i < count; ++i)
6445 if (!reg_maps->cb_sizes[i])
6446 continue;
6448 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
6449 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
6450 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
6452 checkGLcall("glUniformBlockBinding");
6453 string_buffer_release(&priv->string_buffers, name);
6456 /* Context activation is done by the caller. */
6457 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
6458 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
6460 const struct wined3d_gl_info *gl_info = context->gl_info;
6461 const struct ps_np2fixup_info *np2fixup_info = NULL;
6462 struct glsl_shader_prog_link *entry = NULL;
6463 struct wined3d_shader *vshader = NULL;
6464 struct wined3d_shader *gshader = NULL;
6465 struct wined3d_shader *pshader = NULL;
6466 GLuint program_id = 0;
6467 GLuint reorder_shader_id = 0;
6468 unsigned int i;
6469 GLuint vs_id = 0;
6470 GLuint gs_id = 0;
6471 GLuint ps_id = 0;
6472 struct list *ps_list, *vs_list;
6474 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_VERTEX)))
6476 vs_id = ctx_data->glsl_program->vs.id;
6477 vs_list = &ctx_data->glsl_program->vs.shader_entry;
6479 if (use_vs(state))
6481 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
6482 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
6484 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_GEOMETRY))
6485 && ctx_data->glsl_program->gs.id)
6486 gs_id = ctx_data->glsl_program->gs.id;
6487 else if (gshader)
6488 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
6491 else if (use_vs(state))
6493 struct vs_compile_args vs_compile_args;
6494 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
6496 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args);
6497 vs_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
6498 vs_list = &vshader->linked_programs;
6500 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
6501 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
6503 else if (priv->vertex_pipe == &glsl_vertex_pipe)
6505 struct glsl_ffp_vertex_shader *ffp_shader;
6506 struct wined3d_ffp_vs_settings settings;
6508 wined3d_ffp_get_vs_settings(state, &context->stream_info, &settings);
6509 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
6510 vs_id = ffp_shader->id;
6511 vs_list = &ffp_shader->linked_programs;
6514 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_PIXEL)))
6516 ps_id = ctx_data->glsl_program->ps.id;
6517 ps_list = &ctx_data->glsl_program->ps.shader_entry;
6519 if (use_ps(state))
6520 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
6522 else if (use_ps(state))
6524 struct ps_compile_args ps_compile_args;
6525 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
6526 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, gl_info);
6527 ps_id = find_glsl_pshader(context, &priv->shader_buffer,
6528 pshader, &ps_compile_args, &np2fixup_info);
6529 ps_list = &pshader->linked_programs;
6531 else if (priv->fragment_pipe == &glsl_fragment_pipe)
6533 struct glsl_ffp_fragment_shader *ffp_shader;
6534 struct ffp_frag_settings settings;
6536 gen_ffp_frag_op(context, state, &settings, FALSE);
6537 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
6538 ps_id = ffp_shader->id;
6539 ps_list = &ffp_shader->linked_programs;
6542 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
6544 ctx_data->glsl_program = entry;
6545 return;
6548 /* If we get to this point, then no matching program exists, so we create one */
6549 program_id = GL_EXTCALL(glCreateProgram());
6550 TRACE("Created new GLSL shader program %u.\n", program_id);
6552 /* Create the entry */
6553 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
6554 entry->id = program_id;
6555 entry->vs.id = vs_id;
6556 entry->gs.id = gs_id;
6557 entry->ps.id = ps_id;
6558 entry->constant_version = 0;
6559 entry->ps.np2_fixup_info = np2fixup_info;
6560 /* Add the hash table entry */
6561 add_glsl_program_entry(priv, entry);
6563 /* Set the current program */
6564 ctx_data->glsl_program = entry;
6566 /* Attach GLSL vshader */
6567 if (vs_id)
6569 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
6570 GL_EXTCALL(glAttachShader(program_id, vs_id));
6571 checkGLcall("glAttachShader");
6573 list_add_head(vs_list, &entry->vs.shader_entry);
6576 if (vshader)
6578 WORD map = vshader->reg_maps.input_registers;
6579 struct wined3d_string_buffer *tmp_name = string_buffer_get(&priv->string_buffers);
6581 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
6582 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
6583 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
6584 checkGLcall("glAttachShader");
6585 /* Flag the reorder function for deletion, then it will be freed automatically when the program
6586 * is destroyed
6588 GL_EXTCALL(glDeleteShader(reorder_shader_id));
6590 /* Bind vertex attributes to a corresponding index number to match
6591 * the same index numbers as ARB_vertex_programs (makes loading
6592 * vertex attributes simpler). With this method, we can use the
6593 * exact same code to load the attributes later for both ARB and
6594 * GLSL shaders.
6596 * We have to do this here because we need to know the Program ID
6597 * in order to make the bindings work, and it has to be done prior
6598 * to linking the GLSL program. */
6599 for (i = 0; map; map >>= 1, ++i)
6601 if (!(map & 1)) continue;
6603 string_buffer_sprintf(tmp_name, "vs_in%u", i);
6604 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
6606 checkGLcall("glBindAttribLocation");
6607 string_buffer_release(&priv->string_buffers, tmp_name);
6610 if (gshader)
6612 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
6613 GL_EXTCALL(glAttachShader(program_id, gs_id));
6614 checkGLcall("glAttachShader");
6616 TRACE("input type %s, output type %s, vertices out %u.\n",
6617 debug_d3dprimitivetype(gshader->u.gs.input_type),
6618 debug_d3dprimitivetype(gshader->u.gs.output_type),
6619 gshader->u.gs.vertices_out);
6620 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
6621 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
6622 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
6623 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
6624 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
6625 gshader->u.gs.vertices_out));
6626 checkGLcall("glProgramParameteriARB");
6628 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
6631 /* Attach GLSL pshader */
6632 if (ps_id)
6634 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
6635 GL_EXTCALL(glAttachShader(program_id, ps_id));
6636 checkGLcall("glAttachShader");
6638 list_add_head(ps_list, &entry->ps.shader_entry);
6641 /* Link the program */
6642 TRACE("Linking GLSL shader program %u.\n", program_id);
6643 GL_EXTCALL(glLinkProgram(program_id));
6644 shader_glsl_validate_link(gl_info, program_id);
6646 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
6647 vshader ? min(vshader->limits->constant_float, gl_info->limits.glsl_vs_float_constants) : 0);
6648 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
6649 pshader ? min(pshader->limits->constant_float, gl_info->limits.glsl_ps_float_constants) : 0);
6650 checkGLcall("Find glsl program uniform locations");
6652 if (pshader && pshader->reg_maps.shader_version.major >= 3
6653 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
6655 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
6656 entry->vs.vertex_color_clamp = GL_FALSE;
6658 else
6660 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
6663 /* Set the shader to allow uniform loading on it */
6664 GL_EXTCALL(glUseProgram(program_id));
6665 checkGLcall("glUseProgram");
6667 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
6668 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
6669 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
6670 * vertex shader with fixed function pixel processing is used we make sure that the card
6671 * supports enough samplers to allow the max number of vertex samplers with all possible
6672 * fixed function fragment processing setups. So once the program is linked these samplers
6673 * won't change. */
6674 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
6676 entry->constant_update_mask = 0;
6677 if (vshader)
6679 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
6680 if (vshader->reg_maps.integer_constants)
6681 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
6682 if (vshader->reg_maps.boolean_constants)
6683 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
6684 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
6686 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps,
6687 0, gl_info->limits.vertex_uniform_blocks);
6689 else
6691 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
6692 | WINED3D_SHADER_CONST_FFP_PROJ;
6694 for (i = 0; i < MAX_TEXTURES; ++i)
6696 if (entry->vs.texture_matrix_location[i] != -1)
6698 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
6699 break;
6702 if (entry->vs.material_ambient_location != -1)
6703 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
6704 if (entry->vs.light_ambient_location != -1)
6705 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
6708 if (gshader)
6709 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps,
6710 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
6712 if (ps_id)
6714 if (pshader)
6716 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
6717 if (pshader->reg_maps.integer_constants)
6718 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
6719 if (pshader->reg_maps.boolean_constants)
6720 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
6721 if (entry->ps.ycorrection_location != -1)
6722 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
6724 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps,
6725 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
6726 gl_info->limits.fragment_uniform_blocks);
6728 else
6730 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
6733 for (i = 0; i < MAX_TEXTURES; ++i)
6735 if (entry->ps.bumpenv_mat_location[i] != -1)
6737 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
6738 break;
6742 if (entry->ps.np2_fixup_location != -1)
6743 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
6744 if (entry->ps.color_key_location != -1)
6745 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
6749 /* Context activation is done by the caller. */
6750 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum wined3d_gl_resource_type tex_type,
6751 BOOL masked)
6753 GLuint program_id;
6754 GLuint vshader_id, pshader_id;
6755 const char *blt_pshader;
6757 static const char blt_vshader[] =
6758 "#version 120\n"
6759 "void main(void)\n"
6760 "{\n"
6761 " gl_Position = gl_Vertex;\n"
6762 " gl_FrontColor = vec4(1.0);\n"
6763 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
6764 "}\n";
6766 static const char * const blt_pshaders_full[WINED3D_GL_RES_TYPE_COUNT] =
6768 /* WINED3D_GL_RES_TYPE_TEX_1D */
6769 NULL,
6770 /* WINED3D_GL_RES_TYPE_TEX_2D */
6771 "#version 120\n"
6772 "uniform sampler2D sampler;\n"
6773 "void main(void)\n"
6774 "{\n"
6775 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6776 "}\n",
6777 /* WINED3D_GL_RES_TYPE_TEX_3D */
6778 NULL,
6779 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
6780 "#version 120\n"
6781 "uniform samplerCube sampler;\n"
6782 "void main(void)\n"
6783 "{\n"
6784 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6785 "}\n",
6786 /* WINED3D_GL_RES_TYPE_TEX_RECT */
6787 "#version 120\n"
6788 "#extension GL_ARB_texture_rectangle : enable\n"
6789 "uniform sampler2DRect sampler;\n"
6790 "void main(void)\n"
6791 "{\n"
6792 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6793 "}\n",
6796 static const char * const blt_pshaders_masked[WINED3D_GL_RES_TYPE_COUNT] =
6798 /* WINED3D_GL_RES_TYPE_TEX_1D */
6799 NULL,
6800 /* WINED3D_GL_RES_TYPE_TEX_2D */
6801 "#version 120\n"
6802 "uniform sampler2D sampler;\n"
6803 "uniform vec4 mask;\n"
6804 "void main(void)\n"
6805 "{\n"
6806 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6807 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6808 "}\n",
6809 /* WINED3D_GL_RES_TYPE_TEX_3D */
6810 NULL,
6811 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
6812 "#version 120\n"
6813 "uniform samplerCube sampler;\n"
6814 "uniform vec4 mask;\n"
6815 "void main(void)\n"
6816 "{\n"
6817 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6818 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6819 "}\n",
6820 /* WINED3D_GL_RES_TYPE_TEX_RECT */
6821 "#version 120\n"
6822 "#extension GL_ARB_texture_rectangle : enable\n"
6823 "uniform sampler2DRect sampler;\n"
6824 "uniform vec4 mask;\n"
6825 "void main(void)\n"
6826 "{\n"
6827 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6828 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6829 "}\n",
6832 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
6833 if (!blt_pshader)
6835 FIXME("tex_type %#x not supported\n", tex_type);
6836 return 0;
6839 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6840 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
6842 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6843 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
6845 program_id = GL_EXTCALL(glCreateProgram());
6846 GL_EXTCALL(glAttachShader(program_id, vshader_id));
6847 GL_EXTCALL(glAttachShader(program_id, pshader_id));
6848 GL_EXTCALL(glLinkProgram(program_id));
6850 shader_glsl_validate_link(gl_info, program_id);
6852 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
6853 * is destroyed
6855 GL_EXTCALL(glDeleteShader(vshader_id));
6856 GL_EXTCALL(glDeleteShader(pshader_id));
6857 return program_id;
6860 /* Context activation is done by the caller. */
6861 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
6862 const struct wined3d_state *state)
6864 struct glsl_context_data *ctx_data = context->shader_backend_data;
6865 const struct wined3d_gl_info *gl_info = context->gl_info;
6866 struct shader_glsl_priv *priv = shader_priv;
6867 GLuint program_id = 0, prev_id = 0;
6868 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
6870 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
6871 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
6873 if (ctx_data->glsl_program)
6875 prev_id = ctx_data->glsl_program->id;
6876 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6878 else
6880 prev_id = 0;
6881 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6884 set_glsl_shader_program(context, state, priv, ctx_data);
6886 if (ctx_data->glsl_program)
6888 program_id = ctx_data->glsl_program->id;
6889 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6891 else
6893 program_id = 0;
6894 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6897 if (old_vertex_color_clamp != current_vertex_color_clamp)
6899 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6901 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
6902 checkGLcall("glClampColorARB");
6904 else
6906 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
6910 TRACE("Using GLSL program %u.\n", program_id);
6912 if (prev_id != program_id)
6914 GL_EXTCALL(glUseProgram(program_id));
6915 checkGLcall("glUseProgram");
6917 if (program_id)
6918 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
6922 /* "context" is not necessarily the currently active context. */
6923 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
6925 struct glsl_context_data *ctx_data = context->shader_backend_data;
6927 ctx_data->glsl_program = NULL;
6928 context->shader_update_mask = (1 << WINED3D_SHADER_TYPE_PIXEL)
6929 | (1 << WINED3D_SHADER_TYPE_VERTEX)
6930 | (1 << WINED3D_SHADER_TYPE_GEOMETRY);
6933 /* Context activation is done by the caller. */
6934 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
6936 const struct wined3d_gl_info *gl_info = context->gl_info;
6937 struct shader_glsl_priv *priv = shader_priv;
6939 shader_glsl_invalidate_current_program(context);
6940 GL_EXTCALL(glUseProgram(0));
6941 checkGLcall("glUseProgram");
6943 priv->vertex_pipe->vp_enable(gl_info, FALSE);
6944 priv->fragment_pipe->enable_extension(gl_info, FALSE);
6946 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6948 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
6949 checkGLcall("glClampColorARB");
6953 /* Context activation is done by the caller. */
6954 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
6955 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size)
6957 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
6958 struct shader_glsl_priv *priv = shader_priv;
6959 GLuint *blt_program;
6960 GLint loc;
6962 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
6963 if (!*blt_program)
6965 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
6966 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
6967 GL_EXTCALL(glUseProgram(*blt_program));
6968 GL_EXTCALL(glUniform1i(loc, 0));
6970 else
6972 GL_EXTCALL(glUseProgram(*blt_program));
6975 if (masked)
6977 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
6978 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
6982 /* Context activation is done by the caller. */
6983 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
6985 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
6986 GLuint program_id;
6988 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
6989 if (program_id) TRACE("Using GLSL program %u\n", program_id);
6991 GL_EXTCALL(glUseProgram(program_id));
6992 checkGLcall("glUseProgram");
6995 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
6996 const struct glsl_shader_prog_link *program)
6998 const struct glsl_context_data *ctx_data;
6999 struct wined3d_context *context;
7000 unsigned int i;
7002 for (i = 0; i < device->context_count; ++i)
7004 context = device->contexts[i];
7005 ctx_data = context->shader_backend_data;
7007 if (ctx_data->glsl_program == program)
7008 shader_glsl_invalidate_current_program(context);
7012 static void shader_glsl_destroy(struct wined3d_shader *shader)
7014 struct glsl_shader_private *shader_data = shader->backend_data;
7015 struct wined3d_device *device = shader->device;
7016 struct shader_glsl_priv *priv = device->shader_priv;
7017 const struct wined3d_gl_info *gl_info;
7018 const struct list *linked_programs;
7019 struct wined3d_context *context;
7021 if (!shader_data || !shader_data->num_gl_shaders)
7023 HeapFree(GetProcessHeap(), 0, shader_data);
7024 shader->backend_data = NULL;
7025 return;
7028 context = context_acquire(device, NULL);
7029 gl_info = context->gl_info;
7031 TRACE("Deleting linked programs.\n");
7032 linked_programs = &shader->linked_programs;
7033 if (linked_programs->next)
7035 struct glsl_shader_prog_link *entry, *entry2;
7036 UINT i;
7038 switch (shader->reg_maps.shader_version.type)
7040 case WINED3D_SHADER_TYPE_PIXEL:
7042 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
7044 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7046 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
7047 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7048 checkGLcall("glDeleteShader");
7050 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
7052 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7053 struct glsl_shader_prog_link, ps.shader_entry)
7055 shader_glsl_invalidate_contexts_program(device, entry);
7056 delete_glsl_program_entry(priv, gl_info, entry);
7059 break;
7062 case WINED3D_SHADER_TYPE_VERTEX:
7064 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
7066 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7068 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
7069 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7070 checkGLcall("glDeleteShader");
7072 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
7074 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7075 struct glsl_shader_prog_link, vs.shader_entry)
7077 shader_glsl_invalidate_contexts_program(device, entry);
7078 delete_glsl_program_entry(priv, gl_info, entry);
7081 break;
7084 case WINED3D_SHADER_TYPE_GEOMETRY:
7086 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
7088 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7090 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
7091 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7092 checkGLcall("glDeleteShader");
7094 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
7096 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7097 struct glsl_shader_prog_link, gs.shader_entry)
7099 shader_glsl_invalidate_contexts_program(device, entry);
7100 delete_glsl_program_entry(priv, gl_info, entry);
7103 break;
7106 default:
7107 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
7108 break;
7112 HeapFree(GetProcessHeap(), 0, shader->backend_data);
7113 shader->backend_data = NULL;
7115 context_release(context);
7118 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
7120 const struct glsl_program_key *k = key;
7121 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
7122 const struct glsl_shader_prog_link, program_lookup_entry);
7124 if (k->vs_id > prog->vs.id) return 1;
7125 else if (k->vs_id < prog->vs.id) return -1;
7127 if (k->gs_id > prog->gs.id) return 1;
7128 else if (k->gs_id < prog->gs.id) return -1;
7130 if (k->ps_id > prog->ps.id) return 1;
7131 else if (k->ps_id < prog->ps.id) return -1;
7133 return 0;
7136 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
7138 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
7139 + constant_count * sizeof(*heap->contained)
7140 + constant_count * sizeof(*heap->positions);
7141 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
7143 if (!mem)
7145 ERR("Failed to allocate memory\n");
7146 return FALSE;
7149 heap->entries = mem;
7150 heap->entries[1].version = 0;
7151 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
7152 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
7153 heap->positions = (unsigned int *)(heap->contained + constant_count);
7154 heap->size = 1;
7156 return TRUE;
7159 static void constant_heap_free(struct constant_heap *heap)
7161 HeapFree(GetProcessHeap(), 0, heap->entries);
7164 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
7166 wined3d_rb_alloc,
7167 wined3d_rb_realloc,
7168 wined3d_rb_free,
7169 glsl_program_key_compare,
7172 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
7173 const struct fragment_pipeline *fragment_pipe)
7175 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7176 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
7177 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
7178 gl_info->limits.glsl_ps_float_constants)) + 1;
7179 struct fragment_caps fragment_caps;
7180 void *vertex_priv, *fragment_priv;
7182 string_buffer_list_init(&priv->string_buffers);
7184 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
7186 ERR("Failed to initialize vertex pipe.\n");
7187 HeapFree(GetProcessHeap(), 0, priv);
7188 return E_FAIL;
7191 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
7193 ERR("Failed to initialize fragment pipe.\n");
7194 vertex_pipe->vp_free(device);
7195 HeapFree(GetProcessHeap(), 0, priv);
7196 return E_FAIL;
7199 if (!string_buffer_init(&priv->shader_buffer))
7201 ERR("Failed to initialize shader buffer.\n");
7202 goto fail;
7205 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
7206 if (!priv->stack)
7208 ERR("Failed to allocate memory.\n");
7209 goto fail;
7212 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
7214 ERR("Failed to initialize vertex shader constant heap\n");
7215 goto fail;
7218 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
7220 ERR("Failed to initialize pixel shader constant heap\n");
7221 goto fail;
7224 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
7226 ERR("Failed to initialize rbtree.\n");
7227 goto fail;
7230 priv->next_constant_version = 1;
7231 priv->vertex_pipe = vertex_pipe;
7232 priv->fragment_pipe = fragment_pipe;
7233 fragment_pipe->get_caps(gl_info, &fragment_caps);
7234 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
7236 device->vertex_priv = vertex_priv;
7237 device->fragment_priv = fragment_priv;
7238 device->shader_priv = priv;
7240 return WINED3D_OK;
7242 fail:
7243 constant_heap_free(&priv->pconst_heap);
7244 constant_heap_free(&priv->vconst_heap);
7245 HeapFree(GetProcessHeap(), 0, priv->stack);
7246 string_buffer_free(&priv->shader_buffer);
7247 fragment_pipe->free_private(device);
7248 vertex_pipe->vp_free(device);
7249 HeapFree(GetProcessHeap(), 0, priv);
7250 return E_OUTOFMEMORY;
7253 /* Context activation is done by the caller. */
7254 static void shader_glsl_free(struct wined3d_device *device)
7256 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7257 struct shader_glsl_priv *priv = device->shader_priv;
7258 int i;
7260 for (i = 0; i < WINED3D_GL_RES_TYPE_COUNT; ++i)
7262 if (priv->depth_blt_program_full[i])
7264 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
7266 if (priv->depth_blt_program_masked[i])
7268 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
7272 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
7273 constant_heap_free(&priv->pconst_heap);
7274 constant_heap_free(&priv->vconst_heap);
7275 HeapFree(GetProcessHeap(), 0, priv->stack);
7276 string_buffer_list_cleanup(&priv->string_buffers);
7277 string_buffer_free(&priv->shader_buffer);
7278 priv->fragment_pipe->free_private(device);
7279 priv->vertex_pipe->vp_free(device);
7281 HeapFree(GetProcessHeap(), 0, device->shader_priv);
7282 device->shader_priv = NULL;
7285 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
7287 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
7288 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
7291 static void shader_glsl_free_context_data(struct wined3d_context *context)
7293 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
7296 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
7298 UINT shader_model;
7300 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
7301 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
7302 && gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] && gl_info->supported[ARB_DRAW_INSTANCED]
7303 && gl_info->supported[ARB_TEXTURE_RG] && gl_info->supported[ARB_SAMPLER_OBJECTS])
7304 shader_model = 4;
7305 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
7306 * texldd and texldl instructions. */
7307 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
7308 shader_model = 3;
7309 else
7310 shader_model = 2;
7311 TRACE("Shader model %u.\n", shader_model);
7313 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
7314 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
7315 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
7317 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
7318 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
7320 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
7321 * Direct3D minimum requirement.
7323 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
7324 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
7326 * The problem is that the refrast clamps temporary results in the shader to
7327 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
7328 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
7329 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
7330 * offer a way to query this.
7332 if (shader_model >= 4)
7333 caps->ps_1x_max_value = FLT_MAX;
7334 else
7335 caps->ps_1x_max_value = 1024.0f;
7337 /* Ideally we'd only set caps like sRGB writes here if supported by both
7338 * the shader backend and the fragment pipe, but we can get called before
7339 * shader_glsl_alloc(). */
7340 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
7341 | WINED3D_SHADER_CAP_SRGB_WRITE;
7344 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
7346 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
7348 TRACE("Checking support for fixup:\n");
7349 dump_color_fixup_desc(fixup);
7352 /* We support everything except YUV conversions. */
7353 if (!is_complex_fixup(fixup))
7355 TRACE("[OK]\n");
7356 return TRUE;
7359 TRACE("[FAILED]\n");
7360 return FALSE;
7363 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
7365 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
7366 /* WINED3DSIH_ADD */ shader_glsl_binop,
7367 /* WINED3DSIH_AND */ shader_glsl_binop,
7368 /* WINED3DSIH_BEM */ shader_glsl_bem,
7369 /* WINED3DSIH_BREAK */ shader_glsl_break,
7370 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
7371 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
7372 /* WINED3DSIH_CALL */ shader_glsl_call,
7373 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
7374 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
7375 /* WINED3DSIH_CND */ shader_glsl_cnd,
7376 /* WINED3DSIH_CRS */ shader_glsl_cross,
7377 /* WINED3DSIH_CUT */ shader_glsl_cut,
7378 /* WINED3DSIH_DCL */ shader_glsl_nop,
7379 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
7380 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
7381 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
7382 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
7383 /* WINED3DSIH_DEF */ shader_glsl_nop,
7384 /* WINED3DSIH_DEFB */ shader_glsl_nop,
7385 /* WINED3DSIH_DEFI */ shader_glsl_nop,
7386 /* WINED3DSIH_DIV */ shader_glsl_binop,
7387 /* WINED3DSIH_DP2 */ shader_glsl_dot,
7388 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
7389 /* WINED3DSIH_DP3 */ shader_glsl_dot,
7390 /* WINED3DSIH_DP4 */ shader_glsl_dot,
7391 /* WINED3DSIH_DST */ shader_glsl_dst,
7392 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
7393 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
7394 /* WINED3DSIH_ELSE */ shader_glsl_else,
7395 /* WINED3DSIH_EMIT */ shader_glsl_emit,
7396 /* WINED3DSIH_ENDIF */ shader_glsl_end,
7397 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
7398 /* WINED3DSIH_ENDREP */ shader_glsl_end,
7399 /* WINED3DSIH_EQ */ shader_glsl_relop,
7400 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
7401 /* WINED3DSIH_EXPP */ shader_glsl_expp,
7402 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
7403 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
7404 /* WINED3DSIH_GE */ shader_glsl_relop,
7405 /* WINED3DSIH_IADD */ shader_glsl_binop,
7406 /* WINED3DSIH_IEQ */ NULL,
7407 /* WINED3DSIH_IF */ shader_glsl_if,
7408 /* WINED3DSIH_IFC */ shader_glsl_ifc,
7409 /* WINED3DSIH_IGE */ shader_glsl_relop,
7410 /* WINED3DSIH_IMUL */ shader_glsl_imul,
7411 /* WINED3DSIH_ISHL */ shader_glsl_binop,
7412 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
7413 /* WINED3DSIH_LABEL */ shader_glsl_label,
7414 /* WINED3DSIH_LD */ NULL,
7415 /* WINED3DSIH_LIT */ shader_glsl_lit,
7416 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
7417 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
7418 /* WINED3DSIH_LOOP */ shader_glsl_loop,
7419 /* WINED3DSIH_LRP */ shader_glsl_lrp,
7420 /* WINED3DSIH_LT */ shader_glsl_relop,
7421 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
7422 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
7423 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
7424 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
7425 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
7426 /* WINED3DSIH_MAD */ shader_glsl_mad,
7427 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
7428 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
7429 /* WINED3DSIH_MOV */ shader_glsl_mov,
7430 /* WINED3DSIH_MOVA */ shader_glsl_mov,
7431 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
7432 /* WINED3DSIH_MUL */ shader_glsl_binop,
7433 /* WINED3DSIH_NE */ shader_glsl_relop,
7434 /* WINED3DSIH_NOP */ shader_glsl_nop,
7435 /* WINED3DSIH_NRM */ shader_glsl_nrm,
7436 /* WINED3DSIH_OR */ shader_glsl_binop,
7437 /* WINED3DSIH_PHASE */ shader_glsl_nop,
7438 /* WINED3DSIH_POW */ shader_glsl_pow,
7439 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
7440 /* WINED3DSIH_REP */ shader_glsl_rep,
7441 /* WINED3DSIH_RET */ shader_glsl_ret,
7442 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
7443 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
7444 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
7445 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
7446 /* WINED3DSIH_SAMPLE_LOD */ NULL,
7447 /* WINED3DSIH_SETP */ NULL,
7448 /* WINED3DSIH_SGE */ shader_glsl_compare,
7449 /* WINED3DSIH_SGN */ shader_glsl_sgn,
7450 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
7451 /* WINED3DSIH_SLT */ shader_glsl_compare,
7452 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
7453 /* WINED3DSIH_SUB */ shader_glsl_binop,
7454 /* WINED3DSIH_TEX */ shader_glsl_tex,
7455 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
7456 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
7457 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
7458 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
7459 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
7460 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
7461 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
7462 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
7463 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
7464 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
7465 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
7466 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
7467 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
7468 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
7469 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
7470 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
7471 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
7472 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
7473 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
7474 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
7475 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
7476 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
7477 /* WINED3DSIH_UGE */ shader_glsl_relop,
7478 /* WINED3DSIH_USHR */ shader_glsl_binop,
7479 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
7480 /* WINED3DSIH_XOR */ shader_glsl_binop,
7483 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
7484 SHADER_HANDLER hw_fct;
7486 /* Select handler */
7487 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
7489 /* Unhandled opcode */
7490 if (!hw_fct)
7492 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
7493 return;
7495 hw_fct(ins);
7497 shader_glsl_add_instruction_modifiers(ins);
7500 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
7502 struct shader_glsl_priv *priv = shader_priv;
7504 return priv->ffp_proj_control;
7507 const struct wined3d_shader_backend_ops glsl_shader_backend =
7509 shader_glsl_handle_instruction,
7510 shader_glsl_select,
7511 shader_glsl_disable,
7512 shader_glsl_select_depth_blt,
7513 shader_glsl_deselect_depth_blt,
7514 shader_glsl_update_float_vertex_constants,
7515 shader_glsl_update_float_pixel_constants,
7516 shader_glsl_load_constants,
7517 shader_glsl_destroy,
7518 shader_glsl_alloc,
7519 shader_glsl_free,
7520 shader_glsl_allocate_context_data,
7521 shader_glsl_free_context_data,
7522 shader_glsl_get_caps,
7523 shader_glsl_color_fixup_supported,
7524 shader_glsl_has_ffp_proj_control,
7527 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
7529 if (enable)
7530 gl_info->gl_ops.gl.p_glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
7531 else
7532 gl_info->gl_ops.gl.p_glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
7533 checkGLcall("GL_VERTEX_PROGRAM_POINT_SIZE_ARB");
7536 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
7538 caps->xyzrhw = TRUE;
7539 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
7540 caps->max_vertex_blend_matrices = 1;
7541 caps->max_vertex_blend_matrix_index = 0;
7542 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
7543 | WINED3DVTXPCAPS_MATERIALSOURCE7
7544 | WINED3DVTXPCAPS_VERTEXFOG
7545 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
7546 | WINED3DVTXPCAPS_POSITIONALLIGHTS
7547 | WINED3DVTXPCAPS_LOCALVIEWER
7548 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
7549 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
7550 caps->max_user_clip_planes = gl_info->limits.clipplanes;
7551 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
7554 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7556 struct shader_glsl_priv *priv;
7558 if (shader_backend == &glsl_shader_backend)
7560 priv = shader_priv;
7562 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
7564 ERR("Failed to initialize rbtree.\n");
7565 return NULL;
7568 return priv;
7571 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
7573 return NULL;
7576 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
7578 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7579 struct glsl_ffp_vertex_shader, desc.entry);
7580 struct glsl_shader_prog_link *program, *program2;
7581 struct glsl_ffp_destroy_ctx *ctx = context;
7583 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7584 struct glsl_shader_prog_link, vs.shader_entry)
7586 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7588 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
7589 HeapFree(GetProcessHeap(), 0, shader);
7592 /* Context activation is done by the caller. */
7593 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
7595 struct shader_glsl_priv *priv = device->vertex_priv;
7596 struct glsl_ffp_destroy_ctx ctx;
7598 ctx.priv = priv;
7599 ctx.gl_info = &device->adapter->gl_info;
7600 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
7603 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
7604 const struct wined3d_state *state, DWORD state_id)
7606 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7609 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
7610 const struct wined3d_state *state, DWORD state_id)
7612 const struct wined3d_gl_info *gl_info = context->gl_info;
7613 BOOL transformed = context->stream_info.position_transformed;
7614 BOOL wasrhw = context->last_was_rhw;
7615 unsigned int i;
7617 context->last_was_rhw = transformed;
7619 if (!use_vs(state))
7621 if (context->last_was_vshader)
7623 for (i = 0; i < gl_info->limits.clipplanes; ++i)
7624 clipplane(context, state, STATE_CLIPPLANE(i));
7627 if (transformed != wasrhw)
7628 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
7630 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7632 /* Because of settings->texcoords, we have to always regenerate the
7633 * vertex shader on a vdecl change.
7634 * TODO: Just always output all the texcoords when there are enough
7635 * varyings available to drop the dependency. */
7636 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7638 if (use_ps(state)
7639 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
7640 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
7641 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7643 else
7645 if (!context->last_was_vshader)
7647 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
7648 for (i = 0; i < gl_info->limits.clipplanes; ++i)
7649 clipplane(context, state, STATE_CLIPPLANE(i));
7653 context->last_was_vshader = use_vs(state);
7656 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
7657 const struct wined3d_state *state, DWORD state_id)
7659 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7660 /* Different vertex shaders potentially require a different vertex attributes setup. */
7661 if (!isStateDirty(context, STATE_VDECL))
7662 context_apply_state(context, state, STATE_VDECL);
7665 static void glsl_vertex_pipe_world(struct wined3d_context *context,
7666 const struct wined3d_state *state, DWORD state_id)
7668 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
7671 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
7673 const struct wined3d_gl_info *gl_info = context->gl_info;
7674 unsigned int k;
7676 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
7677 | WINED3D_SHADER_CONST_FFP_LIGHTS;
7679 for (k = 0; k < gl_info->limits.clipplanes; ++k)
7681 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
7682 clipplane(context, state, STATE_CLIPPLANE(k));
7685 if (context->swapchain->device->vertexBlendUsed)
7687 static int warned;
7689 if (!warned++)
7690 FIXME("Vertex blending emulation.\n");
7694 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
7695 const struct wined3d_state *state, DWORD state_id)
7697 /* Table fog behavior depends on the projection matrix. */
7698 if (state->render_states[WINED3D_RS_FOGENABLE]
7699 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
7700 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7701 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
7704 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
7705 const struct wined3d_state *state, DWORD state_id)
7707 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
7708 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
7709 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
7710 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
7711 state_pscale(context, state, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE));
7712 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
7715 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
7716 const struct wined3d_state *state, DWORD state_id)
7718 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7721 static void glsl_vertex_pipe_material(struct wined3d_context *context,
7722 const struct wined3d_state *state, DWORD state_id)
7724 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
7727 static void glsl_vertex_pipe_light(struct wined3d_context *context,
7728 const struct wined3d_state *state, DWORD state_id)
7730 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
7733 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
7735 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
7736 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
7737 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
7738 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
7739 /* Clip planes */
7740 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
7741 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
7742 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
7743 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
7744 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
7745 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
7746 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
7747 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
7748 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
7749 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
7750 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
7751 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
7752 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
7753 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
7754 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
7755 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
7756 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
7757 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
7758 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
7759 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
7760 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
7761 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
7762 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
7763 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
7764 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
7765 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
7766 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
7767 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
7768 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
7769 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
7770 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
7771 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
7772 /* Lights */
7773 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7774 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7775 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7776 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7777 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7778 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7779 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7780 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7781 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7782 /* Viewport */
7783 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
7784 /* Transform states */
7785 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
7786 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
7787 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7788 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7789 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7790 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7791 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7792 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7793 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7794 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7795 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
7796 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7797 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7798 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7799 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7800 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7801 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7802 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7803 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7804 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7805 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7806 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7807 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7808 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7809 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7810 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7811 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7812 /* Fog */
7813 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7814 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7815 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7816 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7817 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
7818 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
7819 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7820 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7821 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7822 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7823 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7824 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7825 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7826 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7827 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7828 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7829 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7830 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_arb }, ARB_POINT_PARAMETERS },
7831 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_ext }, EXT_POINT_PARAMETERS },
7832 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_w }, WINED3D_GL_EXT_NONE },
7833 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
7834 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
7835 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), state_pscale }, WINED3D_GL_EXT_NONE },
7836 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7837 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7838 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7839 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, ARB_POINT_PARAMETERS },
7840 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, EXT_POINT_PARAMETERS },
7841 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
7842 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7843 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7844 /* NP2 texture matrix fixups. They are not needed if
7845 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
7846 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
7847 * matrix. */
7848 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7849 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7850 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7851 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7852 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7853 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7854 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7855 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7856 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7857 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7858 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7859 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7860 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7861 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7862 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7863 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7864 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7865 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7866 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7867 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7868 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7869 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7870 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7871 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7872 {STATE_POINT_SIZE_ENABLE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7873 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
7876 /* TODO:
7877 * - This currently depends on GL fixed function functions to set things
7878 * like light parameters. Ideally we'd use regular uniforms for that.
7879 * - In part because of the previous point, much of this is modelled after
7880 * GL fixed function, and has much of the same limitations. For example,
7881 * D3D spot lights are slightly different from GL spot lights.
7882 * - We can now implement drawing transformed vertices using the GLSL pipe,
7883 * instead of using the immediate mode fallback.
7884 * - Similarly, we don't need the fallback for certain combinations of
7885 * material sources anymore.
7886 * - Implement vertex blending and vertex tweening.
7887 * - Handle WINED3D_TSS_TEXCOORD_INDEX in the shader, instead of duplicating
7888 * attribute arrays in load_tex_coords().
7889 * - Per-vertex point sizes. */
7890 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
7892 glsl_vertex_pipe_vp_enable,
7893 glsl_vertex_pipe_vp_get_caps,
7894 glsl_vertex_pipe_vp_alloc,
7895 glsl_vertex_pipe_vp_free,
7896 glsl_vertex_pipe_vp_states,
7899 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
7901 /* Nothing to do. */
7904 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
7906 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
7907 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
7908 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
7909 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
7910 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
7911 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
7912 | WINED3DTEXOPCAPS_SELECTARG1
7913 | WINED3DTEXOPCAPS_SELECTARG2
7914 | WINED3DTEXOPCAPS_MODULATE4X
7915 | WINED3DTEXOPCAPS_MODULATE2X
7916 | WINED3DTEXOPCAPS_MODULATE
7917 | WINED3DTEXOPCAPS_ADDSIGNED2X
7918 | WINED3DTEXOPCAPS_ADDSIGNED
7919 | WINED3DTEXOPCAPS_ADD
7920 | WINED3DTEXOPCAPS_SUBTRACT
7921 | WINED3DTEXOPCAPS_ADDSMOOTH
7922 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
7923 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
7924 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
7925 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
7926 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
7927 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
7928 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
7929 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
7930 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
7931 | WINED3DTEXOPCAPS_DOTPRODUCT3
7932 | WINED3DTEXOPCAPS_MULTIPLYADD
7933 | WINED3DTEXOPCAPS_LERP
7934 | WINED3DTEXOPCAPS_BUMPENVMAP
7935 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
7936 caps->MaxTextureBlendStages = 8;
7937 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
7940 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7942 struct shader_glsl_priv *priv;
7944 if (shader_backend == &glsl_shader_backend)
7946 priv = shader_priv;
7948 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
7950 ERR("Failed to initialize rbtree.\n");
7951 return NULL;
7954 return priv;
7957 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
7959 return NULL;
7962 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
7964 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7965 struct glsl_ffp_fragment_shader, entry.entry);
7966 struct glsl_shader_prog_link *program, *program2;
7967 struct glsl_ffp_destroy_ctx *ctx = context;
7969 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7970 struct glsl_shader_prog_link, ps.shader_entry)
7972 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7974 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
7975 HeapFree(GetProcessHeap(), 0, shader);
7978 /* Context activation is done by the caller. */
7979 static void glsl_fragment_pipe_free(struct wined3d_device *device)
7981 struct shader_glsl_priv *priv = device->fragment_priv;
7982 struct glsl_ffp_destroy_ctx ctx;
7984 ctx.priv = priv;
7985 ctx.gl_info = &device->adapter->gl_info;
7986 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
7989 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
7990 const struct wined3d_state *state, DWORD state_id)
7992 context->last_was_pshader = use_ps(state);
7994 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7997 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
7998 const struct wined3d_state *state, DWORD state_id)
8000 BOOL use_vshader = use_vs(state);
8001 enum fogsource new_source;
8002 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
8003 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
8005 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
8007 if (!state->render_states[WINED3D_RS_FOGENABLE])
8008 return;
8010 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
8012 if (use_vshader)
8013 new_source = FOGSOURCE_VS;
8014 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
8015 new_source = FOGSOURCE_COORD;
8016 else
8017 new_source = FOGSOURCE_FFP;
8019 else
8021 new_source = FOGSOURCE_FFP;
8024 if (new_source != context->fog_source || fogstart == fogend)
8026 context->fog_source = new_source;
8027 state_fogstartend(context, state, STATE_RENDER(WINED3D_RS_FOGSTART));
8031 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
8032 const struct wined3d_state *state, DWORD state_id)
8034 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
8035 glsl_fragment_pipe_fog(context, state, state_id);
8038 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
8039 const struct wined3d_state *state, DWORD state_id)
8041 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
8044 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
8045 const struct wined3d_state *state, DWORD state_id)
8047 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
8050 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
8051 const struct wined3d_state *state, DWORD state_id)
8053 const struct wined3d_gl_info *gl_info = context->gl_info;
8054 int glParm;
8055 float ref;
8057 TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
8059 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
8061 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
8062 checkGLcall("glEnable GL_ALPHA_TEST");
8064 else
8066 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
8067 checkGLcall("glDisable GL_ALPHA_TEST");
8068 return;
8071 ref = ((float)state->render_states[WINED3D_RS_ALPHAREF]) / 255.0f;
8072 glParm = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
8074 if (glParm)
8076 gl_info->gl_ops.gl.p_glAlphaFunc(glParm, ref);
8077 checkGLcall("glAlphaFunc");
8081 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
8082 const struct wined3d_state *state, DWORD state_id)
8084 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
8087 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
8089 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8090 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8091 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8092 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8093 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8094 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8095 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8096 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8097 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8098 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8099 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8100 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8101 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8102 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8103 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8104 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8105 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8106 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8107 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8108 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8109 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8110 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8111 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8112 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8113 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8114 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8115 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8116 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8117 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8118 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8119 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8120 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8121 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8122 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8123 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8124 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8125 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8126 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8127 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8128 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8129 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8130 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8131 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8132 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8133 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8134 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8135 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8136 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8137 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8138 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8139 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8140 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8141 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8142 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8143 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8144 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8145 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8146 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8147 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8148 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8149 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8150 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8151 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8152 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8153 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8154 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8155 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8156 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8157 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8158 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8159 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8160 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8161 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8162 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8163 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
8164 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
8165 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
8166 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_EXT_NONE },
8167 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8168 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
8169 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
8170 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8171 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8172 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
8173 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
8174 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
8175 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8176 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
8177 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
8178 {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 },
8179 {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 },
8180 {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 },
8181 {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 },
8182 {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 },
8183 {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 },
8184 {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 },
8185 {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 },
8186 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8187 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8188 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8189 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8190 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8191 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8192 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8193 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8194 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8195 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
8198 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
8200 return TRUE;
8203 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
8207 const struct fragment_pipeline glsl_fragment_pipe =
8209 glsl_fragment_pipe_enable,
8210 glsl_fragment_pipe_get_caps,
8211 glsl_fragment_pipe_alloc,
8212 glsl_fragment_pipe_free,
8213 glsl_fragment_pipe_alloc_context_data,
8214 glsl_fragment_pipe_free_context_data,
8215 shader_glsl_color_fixup_supported,
8216 glsl_fragment_pipe_state_template,