wined3d: Implement color keying in the glsl fragment pipeline.
[wine.git] / dlls / wined3d / glsl_shader.c
blob39c58cc5dc50fa01a0bf735276ff1db14de57841
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_shader_buffer shader_buffer;
95 struct wine_rb_tree program_lookup;
96 struct constant_heap vconst_heap;
97 struct constant_heap pconst_heap;
98 unsigned char *stack;
99 GLuint depth_blt_program_full[WINED3D_GL_RES_TYPE_COUNT];
100 GLuint depth_blt_program_masked[WINED3D_GL_RES_TYPE_COUNT];
101 UINT next_constant_version;
103 const struct wined3d_vertex_pipe_ops *vertex_pipe;
104 const struct fragment_pipeline *fragment_pipe;
105 struct wine_rb_tree ffp_vertex_shaders;
106 struct wine_rb_tree ffp_fragment_shaders;
107 BOOL ffp_proj_control;
110 struct glsl_vs_program
112 struct list shader_entry;
113 GLuint id;
114 GLenum vertex_color_clamp;
115 GLint *uniform_f_locations;
116 GLint uniform_i_locations[MAX_CONST_I];
117 GLint uniform_b_locations[MAX_CONST_B];
118 GLint pos_fixup_location;
120 GLint modelview_matrix_location;
121 GLint projection_matrix_location;
122 GLint normal_matrix_location;
125 struct glsl_gs_program
127 struct list shader_entry;
128 GLuint id;
131 struct glsl_ps_program
133 struct list shader_entry;
134 GLuint id;
135 GLint *uniform_f_locations;
136 GLint uniform_i_locations[MAX_CONST_I];
137 GLint uniform_b_locations[MAX_CONST_B];
138 GLint bumpenv_mat_location[MAX_TEXTURES];
139 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
140 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
141 GLint tss_constant_location[MAX_TEXTURES];
142 GLint tex_factor_location;
143 GLint specular_enable_location;
144 GLint ycorrection_location;
145 GLint np2_fixup_location;
146 GLint color_key_location;
147 const struct ps_np2fixup_info *np2_fixup_info;
150 /* Struct to maintain data about a linked GLSL program */
151 struct glsl_shader_prog_link
153 struct wine_rb_entry program_lookup_entry;
154 struct glsl_vs_program vs;
155 struct glsl_gs_program gs;
156 struct glsl_ps_program ps;
157 GLuint id;
158 DWORD constant_update_mask;
159 UINT constant_version;
162 struct glsl_program_key
164 GLuint vs_id;
165 GLuint gs_id;
166 GLuint ps_id;
169 struct shader_glsl_ctx_priv {
170 const struct vs_compile_args *cur_vs_args;
171 const struct ps_compile_args *cur_ps_args;
172 struct ps_np2fixup_info *cur_np2fixup_info;
175 struct glsl_context_data
177 struct glsl_shader_prog_link *glsl_program;
180 struct glsl_ps_compiled_shader
182 struct ps_compile_args args;
183 struct ps_np2fixup_info np2fixup;
184 GLuint id;
187 struct glsl_vs_compiled_shader
189 struct vs_compile_args args;
190 GLuint id;
193 struct glsl_gs_compiled_shader
195 GLuint id;
198 struct glsl_shader_private
200 union
202 struct glsl_vs_compiled_shader *vs;
203 struct glsl_gs_compiled_shader *gs;
204 struct glsl_ps_compiled_shader *ps;
205 } gl_shaders;
206 UINT num_gl_shaders, shader_array_size;
209 struct glsl_ffp_vertex_shader
211 struct wined3d_ffp_vs_desc desc;
212 GLuint id;
213 struct list linked_programs;
216 struct glsl_ffp_fragment_shader
218 struct ffp_frag_desc entry;
219 GLuint id;
220 struct list linked_programs;
223 struct glsl_ffp_destroy_ctx
225 struct shader_glsl_priv *priv;
226 const struct wined3d_gl_info *gl_info;
229 static const char *debug_gl_shader_type(GLenum type)
231 switch (type)
233 #define WINED3D_TO_STR(u) case u: return #u
234 WINED3D_TO_STR(GL_VERTEX_SHADER);
235 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
236 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
237 #undef WINED3D_TO_STR
238 default:
239 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
243 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
245 switch (type)
247 case WINED3D_SHADER_TYPE_VERTEX:
248 return "vs";
250 case WINED3D_SHADER_TYPE_GEOMETRY:
251 return "gs";
253 case WINED3D_SHADER_TYPE_PIXEL:
254 return "ps";
256 default:
257 FIXME("Unhandled shader type %#x.\n", type);
258 return "unknown";
262 static const char *shader_glsl_get_version(const struct wined3d_gl_info *gl_info,
263 const struct wined3d_shader_version *version)
265 if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30) && version->major >= 4)
266 return "#version 130";
267 else
268 return "#version 120";
271 static void shader_glsl_append_imm_vec4(struct wined3d_shader_buffer *buffer, const float *values)
273 char str[4][17];
275 wined3d_ftoa(values[0], str[0]);
276 wined3d_ftoa(values[1], str[1]);
277 wined3d_ftoa(values[2], str[2]);
278 wined3d_ftoa(values[3], str[3]);
279 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
282 /* Extract a line from the info log.
283 * Note that this modifies the source string. */
284 static char *get_info_log_line(char **ptr)
286 char *p, *q;
288 p = *ptr;
289 if (!(q = strstr(p, "\n")))
291 if (!*p) return NULL;
292 *ptr += strlen(p);
293 return p;
295 *q = '\0';
296 *ptr = q + 1;
298 return p;
301 /* Context activation is done by the caller. */
302 static void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
304 int length = 0;
305 char *log;
307 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
308 return;
310 if (program)
311 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
312 else
313 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
315 /* A size of 1 is just a null-terminated string, so the log should be bigger than
316 * that if there are errors. */
317 if (length > 1)
319 char *ptr, *line;
321 log = HeapAlloc(GetProcessHeap(), 0, length);
322 /* The info log is supposed to be zero-terminated, but at least some
323 * versions of fglrx don't terminate the string properly. The reported
324 * length does include the terminator, so explicitly set it to zero
325 * here. */
326 log[length - 1] = 0;
327 if (program)
328 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
329 else
330 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
332 ptr = log;
333 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
335 WARN("Info log received from GLSL shader #%u:\n", id);
336 while ((line = get_info_log_line(&ptr))) WARN(" %s\n", line);
338 else
340 FIXME("Info log received from GLSL shader #%u:\n", id);
341 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
343 HeapFree(GetProcessHeap(), 0, log);
347 /* Context activation is done by the caller. */
348 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
350 TRACE("Compiling shader object %u.\n", shader);
351 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
352 checkGLcall("glShaderSource");
353 GL_EXTCALL(glCompileShader(shader));
354 checkGLcall("glCompileShader");
355 print_glsl_info_log(gl_info, shader, FALSE);
358 /* Context activation is done by the caller. */
359 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
361 GLint i, shader_count, source_size = -1;
362 GLuint *shaders;
363 char *source = NULL;
365 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
366 shaders = HeapAlloc(GetProcessHeap(), 0, shader_count * sizeof(*shaders));
367 if (!shaders)
369 ERR("Failed to allocate shader array memory.\n");
370 return;
373 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
374 for (i = 0; i < shader_count; ++i)
376 char *ptr, *line;
377 GLint tmp;
379 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
381 if (source_size < tmp)
383 HeapFree(GetProcessHeap(), 0, source);
385 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
386 if (!source)
388 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
389 HeapFree(GetProcessHeap(), 0, shaders);
390 return;
392 source_size = tmp;
395 FIXME("Shader %u:\n", shaders[i]);
396 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
397 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
398 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
399 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
400 FIXME("\n");
402 ptr = source;
403 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
404 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
405 FIXME("\n");
408 HeapFree(GetProcessHeap(), 0, source);
409 HeapFree(GetProcessHeap(), 0, shaders);
412 /* Context activation is done by the caller. */
413 static void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
415 GLint tmp;
417 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
418 return;
420 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
421 if (!tmp)
423 FIXME("Program %u link status invalid.\n", program);
424 shader_glsl_dump_program_source(gl_info, program);
427 print_glsl_info_log(gl_info, program, TRUE);
430 /* Context activation is done by the caller. */
431 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
432 const DWORD *tex_unit_map, GLuint program_id)
434 unsigned int mapped_unit;
435 char sampler_name[20];
436 const char *prefix;
437 unsigned int i, j;
438 GLint name_loc;
440 static const struct
442 enum wined3d_shader_type type;
443 unsigned int base_idx;
444 unsigned int count;
446 sampler_info[] =
448 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
449 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
452 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
454 prefix = shader_glsl_get_prefix(sampler_info[i].type);
456 for (j = 0; j < sampler_info[i].count; ++j)
458 snprintf(sampler_name, sizeof(sampler_name), "%s_sampler%u", prefix, j);
459 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name));
460 if (name_loc == -1)
461 continue;
463 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
464 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
466 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name, mapped_unit);
467 continue;
470 TRACE("Loading sampler %s on unit %u.\n", sampler_name, mapped_unit);
471 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
474 checkGLcall("glUniform1i");
477 /* Context activation is done by the caller. */
478 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
479 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
481 unsigned int start = ~0U, end = 0;
482 int stack_idx = 0;
483 unsigned int heap_idx = 1;
484 unsigned int idx;
486 if (heap->entries[heap_idx].version <= version) return;
488 idx = heap->entries[heap_idx].idx;
489 if (constant_locations[idx] != -1)
490 start = end = idx;
491 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
493 while (stack_idx >= 0)
495 /* Note that we fall through to the next case statement. */
496 switch(stack[stack_idx])
498 case HEAP_NODE_TRAVERSE_LEFT:
500 unsigned int left_idx = heap_idx << 1;
501 if (left_idx < heap->size && heap->entries[left_idx].version > version)
503 heap_idx = left_idx;
504 idx = heap->entries[heap_idx].idx;
505 if (constant_locations[idx] != -1)
507 if (start > idx)
508 start = idx;
509 if (end < idx)
510 end = idx;
513 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
514 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
515 break;
519 case HEAP_NODE_TRAVERSE_RIGHT:
521 unsigned int right_idx = (heap_idx << 1) + 1;
522 if (right_idx < heap->size && heap->entries[right_idx].version > version)
524 heap_idx = right_idx;
525 idx = heap->entries[heap_idx].idx;
526 if (constant_locations[idx] != -1)
528 if (start > idx)
529 start = idx;
530 if (end < idx)
531 end = idx;
534 stack[stack_idx++] = HEAP_NODE_POP;
535 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
536 break;
540 case HEAP_NODE_POP:
541 heap_idx >>= 1;
542 --stack_idx;
543 break;
546 if (start <= end)
547 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start * 4]));
548 checkGLcall("walk_constant_heap()");
551 /* Context activation is done by the caller. */
552 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
554 GLfloat clamped_constant[4];
556 if (location == -1) return;
558 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
559 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
560 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
561 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
563 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
566 /* Context activation is done by the caller. */
567 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
568 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
570 int stack_idx = 0;
571 unsigned int heap_idx = 1;
572 unsigned int idx;
574 if (heap->entries[heap_idx].version <= version) return;
576 idx = heap->entries[heap_idx].idx;
577 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
578 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
580 while (stack_idx >= 0)
582 /* Note that we fall through to the next case statement. */
583 switch(stack[stack_idx])
585 case HEAP_NODE_TRAVERSE_LEFT:
587 unsigned int left_idx = heap_idx << 1;
588 if (left_idx < heap->size && heap->entries[left_idx].version > version)
590 heap_idx = left_idx;
591 idx = heap->entries[heap_idx].idx;
592 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
594 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
595 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
596 break;
600 case HEAP_NODE_TRAVERSE_RIGHT:
602 unsigned int right_idx = (heap_idx << 1) + 1;
603 if (right_idx < heap->size && heap->entries[right_idx].version > version)
605 heap_idx = right_idx;
606 idx = heap->entries[heap_idx].idx;
607 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
609 stack[stack_idx++] = HEAP_NODE_POP;
610 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
611 break;
615 case HEAP_NODE_POP:
616 heap_idx >>= 1;
617 --stack_idx;
618 break;
621 checkGLcall("walk_constant_heap_clamped()");
624 /* Context activation is done by the caller. */
625 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
626 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
627 unsigned char *stack, UINT version)
629 const struct wined3d_shader_lconst *lconst;
631 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
632 if (shader->reg_maps.shader_version.major == 1
633 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
634 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
635 else
636 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
638 if (!shader->load_local_constsF)
640 TRACE("No need to load local float constants for this shader\n");
641 return;
644 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
645 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
647 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
649 checkGLcall("glUniform4fv()");
652 /* Context activation is done by the caller. */
653 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
654 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
656 unsigned int i;
657 struct list* ptr;
659 for (i = 0; constants_set; constants_set >>= 1, ++i)
661 if (!(constants_set & 1)) continue;
663 /* We found this uniform name in the program - go ahead and send the data */
664 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i * 4]));
667 /* Load immediate constants */
668 ptr = list_head(&shader->constantsI);
669 while (ptr)
671 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
672 unsigned int idx = lconst->idx;
673 const GLint *values = (const GLint *)lconst->value;
675 /* We found this uniform name in the program - go ahead and send the data */
676 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
677 ptr = list_next(&shader->constantsI, ptr);
679 checkGLcall("glUniform4iv()");
682 /* Context activation is done by the caller. */
683 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
684 const GLint locations[MAX_CONST_B], const BOOL *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 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
696 /* Load immediate constants */
697 ptr = list_head(&shader->constantsB);
698 while (ptr)
700 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
701 unsigned int idx = lconst->idx;
702 const GLint *values = (const GLint *)lconst->value;
704 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
705 ptr = list_next(&shader->constantsB, ptr);
707 checkGLcall("glUniform1iv()");
710 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
712 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
715 /* Context activation is done by the caller (state handler). */
716 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
717 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
719 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
720 UINT fixup = ps->np2_fixup_info->active;
721 UINT i;
723 for (i = 0; fixup; fixup >>= 1, ++i)
725 const struct wined3d_texture *tex = state->textures[i];
726 unsigned char idx = ps->np2_fixup_info->idx[i];
727 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
729 if (!tex)
731 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
732 continue;
735 if (idx % 2)
737 tex_dim[2] = tex->pow2_matrix[0];
738 tex_dim[3] = tex->pow2_matrix[5];
740 else
742 tex_dim[0] = tex->pow2_matrix[0];
743 tex_dim[1] = tex->pow2_matrix[5];
747 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
750 /* Taken and adapted from Mesa. */
751 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
753 float pos, neg, t, det;
754 struct wined3d_matrix temp;
756 /* Calculate the determinant of upper left 3x3 submatrix and
757 * determine if the matrix is singular. */
758 pos = neg = 0.0f;
759 t = in->_11 * in->_22 * in->_33;
760 if (t >= 0.0f)
761 pos += t;
762 else
763 neg += t;
765 t = in->_21 * in->_32 * in->_13;
766 if (t >= 0.0f)
767 pos += t;
768 else
769 neg += t;
770 t = in->_31 * in->_12 * in->_23;
771 if (t >= 0.0f)
772 pos += t;
773 else
774 neg += t;
776 t = -in->_31 * in->_22 * in->_13;
777 if (t >= 0.0f)
778 pos += t;
779 else
780 neg += t;
781 t = -in->_21 * in->_12 * in->_33;
782 if (t >= 0.0f)
783 pos += t;
784 else
785 neg += t;
787 t = -in->_11 * in->_32 * in->_23;
788 if (t >= 0.0f)
789 pos += t;
790 else
791 neg += t;
793 det = pos + neg;
795 if (fabsf(det) < 1e-25f)
796 return FALSE;
798 det = 1.0f / det;
799 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
800 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
801 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
802 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
803 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
804 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
805 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
806 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
807 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
809 *out = temp;
810 return TRUE;
813 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
814 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
816 const struct wined3d_gl_info *gl_info = context->gl_info;
817 float mat[3 * 3];
818 struct wined3d_matrix mv;
819 unsigned int i, j;
821 /* gl_NormalMatrix is defined in the OpenGL spec as "transpose of the
822 * inverse of the upper leftmost 3x3 of gl_ModelViewMatrix" and that
823 * seems to be correct for D3D too. */
824 get_modelview_matrix(context, state, &mv);
825 invert_matrix_3d(&mv, &mv);
826 /* Tests show that singular modelview matrices are used unchanged as normal
827 * matrices on D3D3 and older. There seems to be no clearly consistent
828 * behavior on newer D3D versions so always follow older ddraw behavior. */
829 for (i = 0; i < 3; ++i)
830 for (j = 0; j < 3; ++j)
831 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
833 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
834 checkGLcall("glUniformMatrix3fv");
837 /* Context activation is done by the caller (state handler). */
838 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
839 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
841 struct wined3d_color float_key;
842 const struct wined3d_texture *texture = state->textures[0];
844 wined3d_format_convert_color_to_float(texture->resource.format, NULL,
845 texture->async.src_blt_color_key.color_space_high_value, &float_key);
846 GL_EXTCALL(glUniform4fv(ps->color_key_location, 1, &float_key.r));
849 /* Context activation is done by the caller (state handler). */
850 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
851 const struct wined3d_state *state)
853 const struct glsl_context_data *ctx_data = context->shader_backend_data;
854 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
855 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
856 const struct wined3d_gl_info *gl_info = context->gl_info;
857 struct shader_glsl_priv *priv = shader_priv;
858 float position_fixup[4];
859 DWORD update_mask = 0;
861 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
862 UINT constant_version;
863 int i;
865 if (!prog) {
866 /* No GLSL program set - nothing to do. */
867 return;
869 constant_version = prog->constant_version;
870 update_mask = context->constant_update_mask & prog->constant_update_mask;
872 if (update_mask & WINED3D_SHADER_CONST_VS_F)
873 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
874 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
876 if (update_mask & WINED3D_SHADER_CONST_VS_I)
877 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
878 vshader->reg_maps.integer_constants);
880 if (update_mask & WINED3D_SHADER_CONST_VS_B)
881 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
882 vshader->reg_maps.boolean_constants);
884 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
886 shader_get_position_fixup(context, state, position_fixup);
887 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
888 checkGLcall("glUniform4fv");
891 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
893 struct wined3d_matrix mat;
895 get_modelview_matrix(context, state, &mat);
896 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location, 1, FALSE, &mat._11));
897 checkGLcall("glUniformMatrix4fv");
899 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
902 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
904 struct wined3d_matrix projection;
906 get_projection_matrix(context, state, &projection);
907 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
908 checkGLcall("glUniformMatrix4fv");
911 if (update_mask & WINED3D_SHADER_CONST_PS_F)
912 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
913 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
915 if (update_mask & WINED3D_SHADER_CONST_PS_I)
916 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
917 pshader->reg_maps.integer_constants);
919 if (update_mask & WINED3D_SHADER_CONST_PS_B)
920 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
921 pshader->reg_maps.boolean_constants);
923 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
925 for (i = 0; i < MAX_TEXTURES; ++i)
927 if (prog->ps.bumpenv_mat_location[i] == -1)
928 continue;
930 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
931 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
933 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
935 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
936 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
937 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
938 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
942 checkGLcall("bump env uniforms");
945 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
947 float correction_params[4];
949 if (context->render_offscreen)
951 correction_params[0] = 0.0f;
952 correction_params[1] = 1.0f;
953 } else {
954 /* position is window relative, not viewport relative */
955 correction_params[0] = (float) context->current_rt->resource.height;
956 correction_params[1] = -1.0f;
958 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, correction_params));
961 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
962 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
963 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
964 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
966 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
968 float col[4];
970 if (prog->ps.tex_factor_location != -1)
972 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_TEXTUREFACTOR], col);
973 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, col));
976 if (state->render_states[WINED3D_RS_SPECULARENABLE])
977 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
978 else
979 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
981 for (i = 0; i < MAX_TEXTURES; ++i)
983 if (prog->ps.tss_constant_location[i] == -1)
984 continue;
986 D3DCOLORTOGLFLOAT4(state->texture_states[i][WINED3D_TSS_CONSTANT], col);
987 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, col));
990 checkGLcall("fixed function uniforms");
993 if (priv->next_constant_version == UINT_MAX)
995 TRACE("Max constant version reached, resetting to 0.\n");
996 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
997 priv->next_constant_version = 1;
999 else
1001 prog->constant_version = priv->next_constant_version++;
1005 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1007 struct constant_entry *entries = heap->entries;
1008 unsigned int *positions = heap->positions;
1009 unsigned int heap_idx, parent_idx;
1011 if (!heap->contained[idx])
1013 heap_idx = heap->size++;
1014 heap->contained[idx] = TRUE;
1016 else
1018 heap_idx = positions[idx];
1021 while (heap_idx > 1)
1023 parent_idx = heap_idx >> 1;
1025 if (new_version <= entries[parent_idx].version) break;
1027 entries[heap_idx] = entries[parent_idx];
1028 positions[entries[parent_idx].idx] = heap_idx;
1029 heap_idx = parent_idx;
1032 entries[heap_idx].version = new_version;
1033 entries[heap_idx].idx = idx;
1034 positions[idx] = heap_idx;
1037 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1039 struct shader_glsl_priv *priv = device->shader_priv;
1040 struct constant_heap *heap = &priv->vconst_heap;
1041 UINT i;
1043 for (i = start; i < count + start; ++i)
1045 update_heap_entry(heap, i, priv->next_constant_version);
1048 for (i = 0; i < device->context_count; ++i)
1050 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
1054 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1056 struct shader_glsl_priv *priv = device->shader_priv;
1057 struct constant_heap *heap = &priv->pconst_heap;
1058 UINT i;
1060 for (i = start; i < count + start; ++i)
1062 update_heap_entry(heap, i, priv->next_constant_version);
1065 for (i = 0; i < device->context_count; ++i)
1067 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
1071 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1073 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1074 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1075 if(shader_major > 3) return ret;
1077 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1078 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1079 return ret;
1082 /** Generate the variable & register declarations for the GLSL output target */
1083 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1084 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
1085 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1087 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1088 const struct wined3d_state *state = &shader->device->state;
1089 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1090 const struct wined3d_gl_info *gl_info = context->gl_info;
1091 const struct wined3d_fb_state *fb = &shader->device->fb;
1092 unsigned int i, extra_constants_needed = 0;
1093 const struct wined3d_shader_lconst *lconst;
1094 const char *prefix;
1095 DWORD map;
1097 prefix = shader_glsl_get_prefix(version->type);
1099 /* Prototype the subroutines */
1100 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1102 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1105 /* Declare the constants (aka uniforms) */
1106 if (shader->limits->constant_float > 0)
1108 unsigned max_constantsF;
1110 /* Unless the shader uses indirect addressing, always declare the
1111 * maximum array size and ignore that we need some uniforms privately.
1112 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1113 * and immediate values, still declare VC[256]. If the shader needs
1114 * more uniforms than we have it won't work in any case. If it uses
1115 * less, the compiler will figure out which uniforms are really used
1116 * and strip them out. This allows a shader to use c255 on a dx9 card,
1117 * as long as it doesn't also use all the other constants.
1119 * If the shader uses indirect addressing the compiler must assume
1120 * that all declared uniforms are used. In this case, declare only the
1121 * amount that we're assured to have.
1123 * Thus we run into problems in these two cases:
1124 * 1) The shader really uses more uniforms than supported.
1125 * 2) The shader uses indirect addressing, less constants than
1126 * supported, but uses a constant index > #supported consts. */
1127 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1129 /* No indirect addressing here. */
1130 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1132 else
1134 if (reg_maps->usesrelconstF)
1136 /* Subtract the other potential uniforms from the max
1137 * available (bools, ints, and 1 row of projection matrix).
1138 * Subtract another uniform for immediate values, which have
1139 * to be loaded via uniform by the driver as well. The shader
1140 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1141 * shader code, so one vec4 should be enough. (Unfortunately
1142 * the Nvidia driver doesn't store 128 and -128 in one float).
1144 * Writing gl_ClipVertex requires one uniform for each
1145 * clipplane as well. */
1146 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1147 if(ctx_priv->cur_vs_args->clip_enabled)
1149 max_constantsF -= gl_info->limits.clipplanes;
1151 max_constantsF -= count_bits(reg_maps->integer_constants);
1152 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1153 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1154 * for now take this into account when calculating the number of available constants
1156 max_constantsF -= count_bits(reg_maps->boolean_constants);
1157 /* Set by driver quirks in directx.c */
1158 max_constantsF -= gl_info->reserved_glsl_constants;
1160 if (max_constantsF < shader->limits->constant_float)
1162 static unsigned int once;
1164 if (!once++)
1165 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1166 " it may not render correctly.\n");
1167 else
1168 WARN("The hardware does not support enough uniform components to run this shader.\n");
1171 else
1173 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1176 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1177 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1180 /* Always declare the full set of constants, the compiler can remove the
1181 * unused ones because d3d doesn't (yet) support indirect int and bool
1182 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1183 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1184 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1186 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1187 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1189 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1191 if (reg_maps->cb_sizes[i])
1192 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1193 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1196 /* Declare texture samplers */
1197 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1199 struct wined3d_shader_sampler_map_entry *entry;
1200 BOOL shadow_sampler, tex_rect;
1201 const char *sampler_type;
1203 entry = &reg_maps->sampler_map.entries[i];
1205 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1207 ERR("Invalid resource index %u.\n", entry->resource_idx);
1208 continue;
1211 shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1 << entry->sampler_idx));
1212 switch (reg_maps->resource_info[entry->resource_idx].type)
1214 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1215 if (shadow_sampler)
1216 sampler_type = "sampler1DShadow";
1217 else
1218 sampler_type = "sampler1D";
1219 break;
1221 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1222 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1223 && (ps_args->np2_fixup & (1 << entry->resource_idx))
1224 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1225 if (shadow_sampler)
1227 if (tex_rect)
1228 sampler_type = "sampler2DRectShadow";
1229 else
1230 sampler_type = "sampler2DShadow";
1232 else
1234 if (tex_rect)
1235 sampler_type = "sampler2DRect";
1236 else
1237 sampler_type = "sampler2D";
1239 break;
1241 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1242 if (shadow_sampler)
1243 FIXME("Unsupported 3D shadow sampler.\n");
1244 sampler_type = "sampler3D";
1245 break;
1247 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1248 if (shadow_sampler)
1249 FIXME("Unsupported Cube shadow sampler.\n");
1250 sampler_type = "samplerCube";
1251 break;
1253 default:
1254 sampler_type = "unsupported_sampler";
1255 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[i].type);
1256 break;
1258 shader_addline(buffer, "uniform %s %s_sampler%u;\n", sampler_type, prefix, entry->bind_idx);
1261 /* Declare uniforms for NP2 texcoord fixup:
1262 * This is NOT done inside the loop that declares the texture samplers
1263 * since the NP2 fixup code is currently only used for the GeforceFX
1264 * series and when forcing the ARB_npot extension off. Modern cards just
1265 * skip the code anyway, so put it inside a separate loop. */
1266 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1268 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1269 UINT cur = 0;
1271 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1272 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1273 * samplerNP2Fixup stores texture dimensions and is updated through
1274 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1276 for (i = 0; i < shader->limits->sampler; ++i)
1278 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1 << i)))
1279 continue;
1281 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1283 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1284 continue;
1287 fixup->idx[i] = cur++;
1290 fixup->num_consts = (cur + 1) >> 1;
1291 fixup->active = ps_args->np2_fixup;
1292 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1295 /* Declare address variables */
1296 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1298 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1301 /* Declare texture coordinate temporaries and initialize them */
1302 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1304 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1307 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1309 for (i = 0; i < shader->input_signature.element_count; ++i)
1311 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
1312 if (e->sysval_semantic == WINED3D_SV_INSTANCEID)
1313 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1314 prefix, e->register_idx);
1315 else
1316 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, e->register_idx);
1319 shader_addline(buffer, "uniform vec4 posFixup;\n");
1320 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits->packed_output);
1322 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1324 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
1326 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1328 if (version->major >= 3)
1330 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
1332 if (use_vs(state))
1333 shader_addline(buffer, "varying vec4 %s_link[%u];\n", prefix, in_count);
1334 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1337 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1339 if (!(map & 1))
1340 continue;
1342 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1344 if (reg_maps->luminanceparams & (1 << i))
1346 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1347 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1348 extra_constants_needed++;
1351 extra_constants_needed++;
1354 if (ps_args->srgb_correction)
1356 shader_addline(buffer, "const vec4 srgb_const0 = ");
1357 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
1358 shader_addline(buffer, ";\n");
1359 shader_addline(buffer, "const vec4 srgb_const1 = ");
1360 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
1361 shader_addline(buffer, ";\n");
1363 if (reg_maps->vpos || reg_maps->usesdsy)
1365 if (shader->limits->constant_float + extra_constants_needed
1366 + 1 < gl_info->limits.glsl_ps_float_constants)
1368 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1369 extra_constants_needed++;
1371 else
1373 float ycorrection[] =
1375 context->render_offscreen ? 0.0f : fb->render_targets[0]->height,
1376 context->render_offscreen ? 1.0f : -1.0f,
1377 0.0f,
1378 0.0f,
1381 /* This happens because we do not have proper tracking of the
1382 * constant registers that are actually used, only the max
1383 * limit of the shader version. */
1384 FIXME("Cannot find a free uniform for vpos correction params\n");
1385 shader_addline(buffer, "const vec4 ycorrection = ");
1386 shader_glsl_append_imm_vec4(buffer, ycorrection);
1387 shader_addline(buffer, ";\n");
1389 shader_addline(buffer, "vec4 vpos;\n");
1393 /* Declare output register temporaries */
1394 if (shader->limits->packed_output)
1395 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
1397 /* Declare temporary variables */
1398 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1400 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1403 /* Declare loop registers aLx */
1404 if (version->major < 4)
1406 for (i = 0; i < reg_maps->loop_depth; ++i)
1408 shader_addline(buffer, "int aL%u;\n", i);
1409 shader_addline(buffer, "int tmpInt%u;\n", i);
1413 /* Temporary variables for matrix operations */
1414 shader_addline(buffer, "vec4 tmp0;\n");
1415 shader_addline(buffer, "vec4 tmp1;\n");
1417 if (!shader->load_local_constsF)
1419 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1421 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
1422 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
1423 shader_addline(buffer, ";\n");
1427 /* Start the main program. */
1428 shader_addline(buffer, "void main()\n{\n");
1430 /* Direct3D applications expect integer vPos values, while OpenGL drivers
1431 * add approximately 0.5. This causes off-by-one problems as spotted by
1432 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
1433 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
1434 * causes precision troubles when we just subtract 0.5.
1436 * To deal with that, just floor() the position. This will eliminate the
1437 * fraction on all cards.
1439 * TODO: Test how this behaves with multisampling.
1441 * An advantage of floor is that it works even if the driver doesn't add
1442 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
1443 * to return in gl_FragCoord, even though coordinates specify the pixel
1444 * centers instead of the pixel corners. This code will behave correctly
1445 * on drivers that returns integer values. */
1446 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
1448 if (shader->device->wined3d->flags & WINED3D_PIXEL_CENTER_INTEGER)
1449 shader_addline(buffer,
1450 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1451 else
1452 shader_addline(buffer,
1453 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
1457 /*****************************************************************************
1458 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1460 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1461 ****************************************************************************/
1463 /* Prototypes */
1464 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1465 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1467 /** Used for opcode modifiers - They multiply the result by the specified amount */
1468 static const char * const shift_glsl_tab[] = {
1469 "", /* 0 (none) */
1470 "2.0 * ", /* 1 (x2) */
1471 "4.0 * ", /* 2 (x4) */
1472 "8.0 * ", /* 3 (x8) */
1473 "16.0 * ", /* 4 (x16) */
1474 "32.0 * ", /* 5 (x32) */
1475 "", /* 6 (x64) */
1476 "", /* 7 (x128) */
1477 "", /* 8 (d256) */
1478 "", /* 9 (d128) */
1479 "", /* 10 (d64) */
1480 "", /* 11 (d32) */
1481 "0.0625 * ", /* 12 (d16) */
1482 "0.125 * ", /* 13 (d8) */
1483 "0.25 * ", /* 14 (d4) */
1484 "0.5 * " /* 15 (d2) */
1487 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1488 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1489 const char *in_reg, const char *in_regswizzle, char *out_str)
1491 out_str[0] = 0;
1493 switch (src_modifier)
1495 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1496 case WINED3DSPSM_DW:
1497 case WINED3DSPSM_NONE:
1498 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1499 break;
1500 case WINED3DSPSM_NEG:
1501 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1502 break;
1503 case WINED3DSPSM_NOT:
1504 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1505 break;
1506 case WINED3DSPSM_BIAS:
1507 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1508 break;
1509 case WINED3DSPSM_BIASNEG:
1510 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1511 break;
1512 case WINED3DSPSM_SIGN:
1513 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1514 break;
1515 case WINED3DSPSM_SIGNNEG:
1516 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1517 break;
1518 case WINED3DSPSM_COMP:
1519 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1520 break;
1521 case WINED3DSPSM_X2:
1522 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1523 break;
1524 case WINED3DSPSM_X2NEG:
1525 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1526 break;
1527 case WINED3DSPSM_ABS:
1528 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1529 break;
1530 case WINED3DSPSM_ABSNEG:
1531 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1532 break;
1533 default:
1534 FIXME("Unhandled modifier %u\n", src_modifier);
1535 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1539 /** Writes the GLSL variable name that corresponds to the register that the
1540 * DX opcode parameter is trying to access */
1541 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1542 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1544 /* oPos, oFog and oPts in D3D */
1545 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
1547 const struct wined3d_shader *shader = ins->ctx->shader;
1548 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1549 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1550 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1551 const char *prefix = shader_glsl_get_prefix(version->type);
1552 struct glsl_src_param rel_param0, rel_param1;
1553 char imm_str[4][17];
1555 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
1556 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
1557 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
1558 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
1559 *is_color = FALSE;
1561 switch (reg->type)
1563 case WINED3DSPR_TEMP:
1564 sprintf(register_name, "R%u", reg->idx[0].offset);
1565 break;
1567 case WINED3DSPR_INPUT:
1568 /* vertex shaders */
1569 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1571 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1572 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx[0].offset))
1573 *is_color = TRUE;
1574 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
1575 break;
1578 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1580 if (reg->idx[0].rel_addr)
1582 if (reg->idx[1].rel_addr)
1583 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
1584 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1585 else
1586 sprintf(register_name, "gs_in[%s + %u][%u]",
1587 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
1589 else if (reg->idx[1].rel_addr)
1590 sprintf(register_name, "gs_in[%u][%s + %u]",
1591 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1592 else
1593 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
1594 break;
1597 /* pixel shaders >= 3.0 */
1598 if (version->major >= 3)
1600 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
1601 unsigned int in_count = vec4_varyings(version->major, gl_info);
1603 if (reg->idx[0].rel_addr)
1605 /* Removing a + 0 would be an obvious optimization, but
1606 * OS X doesn't see the NOP operation there. */
1607 if (idx)
1609 if (shader->u.ps.declared_in_count > in_count)
1611 sprintf(register_name,
1612 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
1613 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
1614 prefix, rel_param0.param_str, idx);
1616 else
1618 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
1621 else
1623 if (shader->u.ps.declared_in_count > in_count)
1625 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
1626 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
1627 prefix, rel_param0.param_str);
1629 else
1631 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
1635 else
1637 if (idx == in_count) sprintf(register_name, "gl_Color");
1638 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
1639 else sprintf(register_name, "%s_in[%u]", prefix, idx);
1642 else
1644 if (!reg->idx[0].offset)
1645 strcpy(register_name, "gl_Color");
1646 else
1647 strcpy(register_name, "gl_SecondaryColor");
1648 break;
1650 break;
1652 case WINED3DSPR_CONST:
1654 /* Relative addressing */
1655 if (reg->idx[0].rel_addr)
1657 if (reg->idx[0].offset)
1658 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
1659 else
1660 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
1662 else
1664 if (shader_constant_is_local(shader, reg->idx[0].offset))
1665 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
1666 else
1667 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
1670 break;
1672 case WINED3DSPR_CONSTINT:
1673 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
1674 break;
1676 case WINED3DSPR_CONSTBOOL:
1677 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
1678 break;
1680 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1681 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1682 sprintf(register_name, "T%u", reg->idx[0].offset);
1683 else
1684 sprintf(register_name, "A%u", reg->idx[0].offset);
1685 break;
1687 case WINED3DSPR_LOOP:
1688 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
1689 break;
1691 case WINED3DSPR_SAMPLER:
1692 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
1693 break;
1695 case WINED3DSPR_COLOROUT:
1696 if (reg->idx[0].offset >= gl_info->limits.buffers)
1697 WARN("Write to render target %u, only %d supported.\n",
1698 reg->idx[0].offset, gl_info->limits.buffers);
1700 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
1701 break;
1703 case WINED3DSPR_RASTOUT:
1704 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
1705 break;
1707 case WINED3DSPR_DEPTHOUT:
1708 sprintf(register_name, "gl_FragDepth");
1709 break;
1711 case WINED3DSPR_ATTROUT:
1712 if (!reg->idx[0].offset)
1713 sprintf(register_name, "%s_out[8]", prefix);
1714 else
1715 sprintf(register_name, "%s_out[9]", prefix);
1716 break;
1718 case WINED3DSPR_TEXCRDOUT:
1719 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1720 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
1721 break;
1723 case WINED3DSPR_MISCTYPE:
1724 if (!reg->idx[0].offset)
1726 /* vPos */
1727 sprintf(register_name, "vpos");
1729 else if (reg->idx[0].offset == 1)
1731 /* Note that gl_FrontFacing is a bool, while vFace is
1732 * a float for which the sign determines front/back */
1733 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1735 else
1737 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
1738 sprintf(register_name, "unrecognized_register");
1740 break;
1742 case WINED3DSPR_IMMCONST:
1743 switch (reg->immconst_type)
1745 case WINED3D_IMMCONST_SCALAR:
1746 switch (reg->data_type)
1748 case WINED3D_DATA_FLOAT:
1749 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
1750 break;
1751 case WINED3D_DATA_INT:
1752 sprintf(register_name, "%#x", reg->immconst_data[0]);
1753 break;
1754 case WINED3D_DATA_RESOURCE:
1755 case WINED3D_DATA_SAMPLER:
1756 case WINED3D_DATA_UINT:
1757 sprintf(register_name, "%#xu", reg->immconst_data[0]);
1758 break;
1759 default:
1760 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1761 break;
1763 break;
1765 case WINED3D_IMMCONST_VEC4:
1766 switch (reg->data_type)
1768 case WINED3D_DATA_FLOAT:
1769 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
1770 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
1771 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
1772 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
1773 sprintf(register_name, "vec4(%s, %s, %s, %s)",
1774 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
1775 break;
1776 case WINED3D_DATA_INT:
1777 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
1778 reg->immconst_data[0], reg->immconst_data[1],
1779 reg->immconst_data[2], reg->immconst_data[3]);
1780 break;
1781 case WINED3D_DATA_RESOURCE:
1782 case WINED3D_DATA_SAMPLER:
1783 case WINED3D_DATA_UINT:
1784 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
1785 reg->immconst_data[0], reg->immconst_data[1],
1786 reg->immconst_data[2], reg->immconst_data[3]);
1787 break;
1788 default:
1789 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1790 break;
1792 break;
1794 default:
1795 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
1796 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
1798 break;
1800 case WINED3DSPR_CONSTBUFFER:
1801 if (reg->idx[1].rel_addr)
1802 sprintf(register_name, "%s_cb%u[%s + %u]",
1803 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1804 else
1805 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
1806 break;
1808 case WINED3DSPR_PRIMID:
1809 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
1810 break;
1812 default:
1813 FIXME("Unhandled register type %#x.\n", reg->type);
1814 sprintf(register_name, "unrecognized_register");
1815 break;
1819 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1821 *str++ = '.';
1822 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1823 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1824 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1825 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1826 *str = '\0';
1829 /* Get the GLSL write mask for the destination register */
1830 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1832 DWORD mask = param->write_mask;
1834 if (shader_is_scalar(&param->reg))
1836 mask = WINED3DSP_WRITEMASK_0;
1837 *write_mask = '\0';
1839 else
1841 shader_glsl_write_mask_to_str(mask, write_mask);
1844 return mask;
1847 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1848 unsigned int size = 0;
1850 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1851 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1852 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1853 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1855 return size;
1858 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
1860 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1861 * but addressed as "rgba". To fix this we need to swap the register's x
1862 * and z components. */
1863 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1865 *str++ = '.';
1866 /* swizzle bits fields: wwzzyyxx */
1867 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
1868 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
1869 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
1870 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
1871 *str = '\0';
1874 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
1875 BOOL fixup, DWORD mask, char *swizzle_str)
1877 if (shader_is_scalar(&param->reg))
1878 *swizzle_str = '\0';
1879 else
1880 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
1883 /* From a given parameter token, generate the corresponding GLSL string.
1884 * Also, return the actual register name and swizzle in case the
1885 * caller needs this information as well. */
1886 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1887 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
1889 BOOL is_color = FALSE;
1890 char swizzle_str[6];
1892 glsl_src->reg_name[0] = '\0';
1893 glsl_src->param_str[0] = '\0';
1894 swizzle_str[0] = '\0';
1896 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
1897 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
1899 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
1901 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
1903 else
1905 char reg_name[200];
1907 switch (wined3d_src->reg.data_type)
1909 case WINED3D_DATA_FLOAT:
1910 sprintf(reg_name, "%s", glsl_src->reg_name);
1911 break;
1912 case WINED3D_DATA_INT:
1913 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
1914 break;
1915 case WINED3D_DATA_RESOURCE:
1916 case WINED3D_DATA_SAMPLER:
1917 case WINED3D_DATA_UINT:
1918 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
1919 break;
1920 default:
1921 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
1922 sprintf(reg_name, "%s", glsl_src->reg_name);
1923 break;
1926 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
1930 /* From a given parameter token, generate the corresponding GLSL string.
1931 * Also, return the actual register name and swizzle in case the
1932 * caller needs this information as well. */
1933 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1934 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
1936 BOOL is_color = FALSE;
1938 glsl_dst->mask_str[0] = '\0';
1939 glsl_dst->reg_name[0] = '\0';
1941 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
1942 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1945 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1946 static DWORD shader_glsl_append_dst_ext(struct wined3d_shader_buffer *buffer,
1947 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
1948 enum wined3d_data_type data_type)
1950 struct glsl_dst_param glsl_dst;
1951 DWORD mask;
1953 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
1955 switch (data_type)
1957 case WINED3D_DATA_FLOAT:
1958 shader_addline(buffer, "%s%s = %s(",
1959 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1960 break;
1961 case WINED3D_DATA_INT:
1962 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
1963 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1964 break;
1965 case WINED3D_DATA_RESOURCE:
1966 case WINED3D_DATA_SAMPLER:
1967 case WINED3D_DATA_UINT:
1968 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
1969 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1970 break;
1971 default:
1972 FIXME("Unhandled data type %#x.\n", data_type);
1973 shader_addline(buffer, "%s%s = %s(",
1974 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1975 break;
1979 return mask;
1982 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1983 static DWORD shader_glsl_append_dst(struct wined3d_shader_buffer *buffer, const struct wined3d_shader_instruction *ins)
1985 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
1988 /** Process GLSL instruction modifiers */
1989 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1991 struct glsl_dst_param dst_param;
1992 DWORD modifiers;
1994 if (!ins->dst_count) return;
1996 modifiers = ins->dst[0].modifiers;
1997 if (!modifiers) return;
1999 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2001 if (modifiers & WINED3DSPDM_SATURATE)
2003 /* _SAT means to clamp the value of the register to between 0 and 1 */
2004 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
2005 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
2008 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
2010 FIXME("_centroid modifier not handled\n");
2013 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
2015 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2019 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
2021 switch (op)
2023 case WINED3D_SHADER_REL_OP_GT: return ">";
2024 case WINED3D_SHADER_REL_OP_EQ: return "==";
2025 case WINED3D_SHADER_REL_OP_GE: return ">=";
2026 case WINED3D_SHADER_REL_OP_LT: return "<";
2027 case WINED3D_SHADER_REL_OP_NE: return "!=";
2028 case WINED3D_SHADER_REL_OP_LE: return "<=";
2029 default:
2030 FIXME("Unrecognized operator %#x.\n", op);
2031 return "(\?\?)";
2035 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
2036 DWORD resource_idx, DWORD flags, struct glsl_sample_function *sample_function)
2038 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
2039 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2040 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
2041 && (((const struct shader_glsl_ctx_priv *)ctx->backend_data)->cur_ps_args->shadow & (1 << resource_idx));
2042 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
2043 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2044 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
2045 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
2047 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
2049 /* Note that there's no such thing as a projected cube texture. */
2050 switch (resource_type)
2052 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2053 if (shadow)
2055 if (lod)
2057 sample_function->name = projected ? "shadow1DProjLod" : "shadow1DLod";
2059 else if (grad)
2061 if (gl_info->supported[EXT_GPU_SHADER4])
2062 sample_function->name = projected ? "shadow1DProjGrad" : "shadow1DGrad";
2063 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2064 sample_function->name = projected ? "shadow1DProjGradARB" : "shadow1DGradARB";
2065 else
2067 FIXME("Unsupported 1D shadow grad function.\n");
2068 sample_function->name = "unsupported1DGrad";
2071 else
2073 sample_function->name = projected ? "shadow1DProj" : "shadow1D";
2075 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2077 else
2079 if (lod)
2081 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
2083 else if (grad)
2085 if (gl_info->supported[EXT_GPU_SHADER4])
2086 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
2087 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2088 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
2089 else
2091 FIXME("Unsupported 1D grad function.\n");
2092 sample_function->name = "unsupported1DGrad";
2095 else
2097 sample_function->name = projected ? "texture1DProj" : "texture1D";
2099 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
2101 break;
2103 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2104 if (shadow)
2106 if (texrect)
2108 if (lod)
2110 sample_function->name = projected ? "shadow2DRectProjLod" : "shadow2DRectLod";
2112 else if (grad)
2114 if (gl_info->supported[EXT_GPU_SHADER4])
2115 sample_function->name = projected ? "shadow2DRectProjGrad" : "shadow2DRectGrad";
2116 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2117 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
2118 else
2120 FIXME("Unsupported RECT shadow grad function.\n");
2121 sample_function->name = "unsupported2DRectGrad";
2124 else
2126 sample_function->name = projected ? "shadow2DRectProj" : "shadow2DRect";
2129 else
2131 if (lod)
2133 sample_function->name = projected ? "shadow2DProjLod" : "shadow2DLod";
2135 else if (grad)
2137 if (gl_info->supported[EXT_GPU_SHADER4])
2138 sample_function->name = projected ? "shadow2DProjGrad" : "shadow2DGrad";
2139 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2140 sample_function->name = projected ? "shadow2DProjGradARB" : "shadow2DGradARB";
2141 else
2143 FIXME("Unsupported 2D shadow grad function.\n");
2144 sample_function->name = "unsupported2DGrad";
2147 else
2149 sample_function->name = projected ? "shadow2DProj" : "shadow2D";
2152 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2154 else
2156 if (texrect)
2158 if (lod)
2160 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
2162 else if (grad)
2164 if (gl_info->supported[EXT_GPU_SHADER4])
2165 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
2166 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2167 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
2168 else
2170 FIXME("Unsupported RECT grad function.\n");
2171 sample_function->name = "unsupported2DRectGrad";
2174 else
2176 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
2179 else
2181 if (lod)
2183 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
2185 else if (grad)
2187 if (gl_info->supported[EXT_GPU_SHADER4])
2188 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
2189 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2190 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
2191 else
2193 FIXME("Unsupported 2D grad function.\n");
2194 sample_function->name = "unsupported2DGrad";
2197 else
2199 sample_function->name = projected ? "texture2DProj" : "texture2D";
2202 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2204 break;
2206 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2207 if (shadow)
2209 FIXME("Unsupported 3D shadow function.\n");
2210 sample_function->name = "unsupported3DShadow";
2211 sample_function->coord_mask = 0;
2213 else
2215 if (lod)
2217 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
2219 else if (grad)
2221 if (gl_info->supported[EXT_GPU_SHADER4])
2222 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
2223 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2224 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
2225 else
2227 FIXME("Unsupported 3D grad function.\n");
2228 sample_function->name = "unsupported3DGrad";
2231 else
2233 sample_function->name = projected ? "texture3DProj" : "texture3D";
2235 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2237 break;
2239 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2240 if (shadow)
2242 FIXME("Unsupported Cube shadow function.\n");
2243 sample_function->name = "unsupportedCubeShadow";
2244 sample_function->coord_mask = 0;
2246 else
2248 if (lod)
2250 sample_function->name = "textureCubeLod";
2252 else if (grad)
2254 if (gl_info->supported[EXT_GPU_SHADER4])
2255 sample_function->name = "textureCubeGrad";
2256 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2257 sample_function->name = "textureCubeGradARB";
2258 else
2260 FIXME("Unsupported Cube grad function.\n");
2261 sample_function->name = "unsupportedCubeGrad";
2264 else
2266 sample_function->name = "textureCube";
2268 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2270 break;
2272 default:
2273 sample_function->name = "";
2274 sample_function->coord_mask = 0;
2275 FIXME("Unhandled resource type %#x.\n", resource_type);
2276 break;
2280 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2281 BOOL sign_fixup, enum fixup_channel_source channel_source)
2283 switch(channel_source)
2285 case CHANNEL_SOURCE_ZERO:
2286 strcat(arguments, "0.0");
2287 break;
2289 case CHANNEL_SOURCE_ONE:
2290 strcat(arguments, "1.0");
2291 break;
2293 case CHANNEL_SOURCE_X:
2294 strcat(arguments, reg_name);
2295 strcat(arguments, ".x");
2296 break;
2298 case CHANNEL_SOURCE_Y:
2299 strcat(arguments, reg_name);
2300 strcat(arguments, ".y");
2301 break;
2303 case CHANNEL_SOURCE_Z:
2304 strcat(arguments, reg_name);
2305 strcat(arguments, ".z");
2306 break;
2308 case CHANNEL_SOURCE_W:
2309 strcat(arguments, reg_name);
2310 strcat(arguments, ".w");
2311 break;
2313 default:
2314 FIXME("Unhandled channel source %#x\n", channel_source);
2315 strcat(arguments, "undefined");
2316 break;
2319 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2322 static void shader_glsl_color_correction_ext(struct wined3d_shader_buffer *buffer,
2323 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2325 unsigned int mask_size, remaining;
2326 DWORD fixup_mask = 0;
2327 char arguments[256];
2328 char mask_str[6];
2330 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2331 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2332 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2333 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2334 if (!(mask &= fixup_mask))
2335 return;
2337 if (is_complex_fixup(fixup))
2339 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2340 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2341 return;
2344 shader_glsl_write_mask_to_str(mask, mask_str);
2345 mask_size = shader_glsl_get_write_mask_size(mask);
2347 arguments[0] = '\0';
2348 remaining = mask_size;
2349 if (mask & WINED3DSP_WRITEMASK_0)
2351 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2352 if (--remaining) strcat(arguments, ", ");
2354 if (mask & WINED3DSP_WRITEMASK_1)
2356 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2357 if (--remaining) strcat(arguments, ", ");
2359 if (mask & WINED3DSP_WRITEMASK_2)
2361 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2362 if (--remaining) strcat(arguments, ", ");
2364 if (mask & WINED3DSP_WRITEMASK_3)
2366 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2367 if (--remaining) strcat(arguments, ", ");
2370 if (mask_size > 1)
2371 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2372 else
2373 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2376 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2378 char reg_name[256];
2379 BOOL is_color;
2381 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2382 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2385 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2386 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2387 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2389 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2390 char dst_swizzle[6];
2391 struct color_fixup_desc fixup;
2392 BOOL np2_fixup = FALSE;
2393 va_list args;
2395 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2397 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2399 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2400 fixup = priv->cur_ps_args->color_fixup[sampler];
2402 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2403 if(bias) {
2404 FIXME("Biased sampling from NP2 textures is unsupported\n");
2405 } else {
2406 np2_fixup = TRUE;
2410 else
2412 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
2415 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2417 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2418 sample_function->name, shader_glsl_get_prefix(version->type), sampler);
2420 va_start(args, coord_reg_fmt);
2421 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2422 va_end(args);
2424 if(bias) {
2425 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2426 } else {
2427 if (np2_fixup) {
2428 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2429 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2431 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2432 (idx % 2) ? "zw" : "xy", dst_swizzle);
2433 } else if(dx && dy) {
2434 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2435 } else {
2436 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2440 if(!is_identity_fixup(fixup)) {
2441 shader_glsl_color_correction(ins, fixup);
2445 /*****************************************************************************
2446 * Begin processing individual instruction opcodes
2447 ****************************************************************************/
2449 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2451 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2452 struct glsl_src_param src0_param;
2453 struct glsl_src_param src1_param;
2454 DWORD write_mask;
2455 const char *op;
2457 /* Determine the GLSL operator to use based on the opcode */
2458 switch (ins->handler_idx)
2460 case WINED3DSIH_ADD: op = "+"; break;
2461 case WINED3DSIH_AND: op = "&"; break;
2462 case WINED3DSIH_DIV: op = "/"; break;
2463 case WINED3DSIH_IADD: op = "+"; break;
2464 case WINED3DSIH_ISHL: op = "<<"; break;
2465 case WINED3DSIH_MUL: op = "*"; break;
2466 case WINED3DSIH_OR: op = "|"; break;
2467 case WINED3DSIH_SUB: op = "-"; break;
2468 case WINED3DSIH_USHR: op = ">>"; break;
2469 case WINED3DSIH_XOR: op = "^"; break;
2470 default:
2471 op = "<unhandled operator>";
2472 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2473 break;
2476 write_mask = shader_glsl_append_dst(buffer, ins);
2477 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2478 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2479 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2482 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2484 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2485 struct glsl_src_param src0_param;
2486 struct glsl_src_param src1_param;
2487 unsigned int mask_size;
2488 DWORD write_mask;
2489 const char *op;
2491 write_mask = shader_glsl_append_dst(buffer, ins);
2492 mask_size = shader_glsl_get_write_mask_size(write_mask);
2493 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2494 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2496 if (mask_size > 1)
2498 switch (ins->handler_idx)
2500 case WINED3DSIH_EQ: op = "equal"; break;
2501 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2502 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2503 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
2504 case WINED3DSIH_LT: op = "lessThan"; break;
2505 case WINED3DSIH_NE: op = "notEqual"; break;
2506 default:
2507 op = "<unhandled operator>";
2508 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2509 break;
2512 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2513 mask_size, op, src0_param.param_str, src1_param.param_str);
2515 else
2517 switch (ins->handler_idx)
2519 case WINED3DSIH_EQ: op = "=="; break;
2520 case WINED3DSIH_GE: op = ">="; break;
2521 case WINED3DSIH_IGE: op = ">="; break;
2522 case WINED3DSIH_UGE: op = ">="; break;
2523 case WINED3DSIH_LT: op = "<"; break;
2524 case WINED3DSIH_NE: op = "!="; break;
2525 default:
2526 op = "<unhandled operator>";
2527 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2528 break;
2531 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2532 src0_param.param_str, op, src1_param.param_str);
2536 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2538 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2539 struct glsl_src_param src0_param;
2540 struct glsl_src_param src1_param;
2541 DWORD write_mask;
2543 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2544 * not, we can emulate it. */
2545 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2546 FIXME("64-bit integer multiplies not implemented.\n");
2548 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2550 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2551 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2552 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2554 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2555 src0_param.param_str, src1_param.param_str);
2559 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2561 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2562 struct glsl_src_param src0_param, src1_param;
2563 DWORD write_mask;
2565 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2568 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2570 char dst_mask[6];
2572 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2573 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2574 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2575 shader_addline(buffer, "tmp0%s = %s / %s;\n",
2576 dst_mask, src0_param.param_str, src1_param.param_str);
2578 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2579 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2580 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2581 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
2583 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2584 shader_addline(buffer, "tmp0%s);\n", dst_mask);
2586 else
2588 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2589 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2590 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2591 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
2594 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2596 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2597 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2598 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2599 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
2603 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2604 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2606 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2607 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2608 struct glsl_src_param src0_param;
2609 DWORD write_mask;
2611 write_mask = shader_glsl_append_dst(buffer, ins);
2612 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2614 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2615 * shader versions WINED3DSIO_MOVA is used for this. */
2616 if (ins->ctx->reg_maps->shader_version.major == 1
2617 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
2618 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2620 /* This is a simple floor() */
2621 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2622 if (mask_size > 1) {
2623 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2624 } else {
2625 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2628 else if(ins->handler_idx == WINED3DSIH_MOVA)
2630 /* We need to *round* to the nearest int here. */
2631 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2633 if (gl_info->supported[EXT_GPU_SHADER4])
2635 if (mask_size > 1)
2636 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2637 else
2638 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2640 else
2642 if (mask_size > 1)
2643 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2644 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2645 else
2646 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2647 src0_param.param_str, src0_param.param_str);
2650 else
2652 shader_addline(buffer, "%s);\n", src0_param.param_str);
2656 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2657 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2659 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2660 struct glsl_src_param src0_param;
2661 struct glsl_src_param src1_param;
2662 DWORD dst_write_mask, src_write_mask;
2663 unsigned int dst_size = 0;
2665 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2666 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2668 /* dp3 works on vec3, dp4 on vec4 */
2669 if (ins->handler_idx == WINED3DSIH_DP4)
2671 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2672 } else {
2673 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2676 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2677 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2679 if (dst_size > 1) {
2680 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2681 } else {
2682 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2686 /* Note that this instruction has some restrictions. The destination write mask
2687 * can't contain the w component, and the source swizzles have to be .xyzw */
2688 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2690 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2691 struct glsl_src_param src0_param;
2692 struct glsl_src_param src1_param;
2693 char dst_mask[6];
2695 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2696 shader_glsl_append_dst(ins->ctx->buffer, ins);
2697 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2698 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2699 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2702 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
2704 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
2707 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2708 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2709 * GLSL uses the value as-is. */
2710 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2712 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2713 struct glsl_src_param src0_param;
2714 struct glsl_src_param src1_param;
2715 DWORD dst_write_mask;
2716 unsigned int dst_size;
2718 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2719 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2721 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2722 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2724 if (dst_size > 1)
2726 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2727 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
2729 else
2731 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
2732 src1_param.param_str, src0_param.param_str, src1_param.param_str);
2736 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2737 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
2739 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2740 struct glsl_src_param src_param;
2741 const char *instruction;
2742 DWORD write_mask;
2743 unsigned i;
2745 /* Determine the GLSL function to use based on the opcode */
2746 /* TODO: Possibly make this a table for faster lookups */
2747 switch (ins->handler_idx)
2749 case WINED3DSIH_MIN: instruction = "min"; break;
2750 case WINED3DSIH_MAX: instruction = "max"; break;
2751 case WINED3DSIH_ABS: instruction = "abs"; break;
2752 case WINED3DSIH_FRC: instruction = "fract"; break;
2753 case WINED3DSIH_DSX: instruction = "dFdx"; break;
2754 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
2755 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
2756 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
2757 default: instruction = "";
2758 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2759 break;
2762 write_mask = shader_glsl_append_dst(buffer, ins);
2764 shader_addline(buffer, "%s(", instruction);
2766 if (ins->src_count)
2768 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2769 shader_addline(buffer, "%s", src_param.param_str);
2770 for (i = 1; i < ins->src_count; ++i)
2772 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
2773 shader_addline(buffer, ", %s", src_param.param_str);
2777 shader_addline(buffer, "));\n");
2780 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
2782 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
2784 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2785 struct glsl_src_param src_param;
2786 unsigned int mask_size;
2787 DWORD write_mask;
2788 char dst_mask[6];
2790 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
2791 mask_size = shader_glsl_get_write_mask_size(write_mask);
2792 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2794 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
2795 src_param.param_str, src_param.param_str);
2796 shader_glsl_append_dst(buffer, ins);
2798 if (mask_size > 1)
2800 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
2801 mask_size, src_param.param_str);
2803 else
2805 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
2806 src_param.param_str);
2810 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
2812 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2813 struct glsl_src_param src0_param;
2814 const char *prefix, *suffix;
2815 unsigned int dst_size;
2816 DWORD dst_write_mask;
2818 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2819 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2821 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src0_param);
2823 switch (ins->handler_idx)
2825 case WINED3DSIH_EXP:
2826 case WINED3DSIH_EXPP:
2827 prefix = "exp2(";
2828 suffix = ")";
2829 break;
2831 case WINED3DSIH_LOG:
2832 case WINED3DSIH_LOGP:
2833 prefix = "log2(abs(";
2834 suffix = "))";
2835 break;
2837 case WINED3DSIH_RCP:
2838 prefix = "1.0 / ";
2839 suffix = "";
2840 break;
2842 case WINED3DSIH_RSQ:
2843 prefix = "inversesqrt(abs(";
2844 suffix = "))";
2845 break;
2847 default:
2848 prefix = "";
2849 suffix = "";
2850 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2851 break;
2854 if (dst_size > 1)
2855 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
2856 else
2857 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
2860 /** Process the WINED3DSIO_EXPP instruction in GLSL:
2861 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2862 * dst.x = 2^(floor(src))
2863 * dst.y = src - floor(src)
2864 * dst.z = 2^src (partial precision is allowed, but optional)
2865 * dst.w = 1.0;
2866 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2867 * dst = 2^src; (partial precision is allowed, but optional)
2869 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
2871 if (ins->ctx->reg_maps->shader_version.major < 2)
2873 struct glsl_src_param src_param;
2874 char dst_mask[6];
2876 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2878 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
2879 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
2880 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
2881 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
2883 shader_glsl_append_dst(ins->ctx->buffer, ins);
2884 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2885 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
2886 return;
2889 shader_glsl_scalar_op(ins);
2892 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
2894 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2895 struct glsl_src_param src_param;
2896 unsigned int mask_size;
2897 DWORD write_mask;
2899 write_mask = shader_glsl_append_dst(buffer, ins);
2900 mask_size = shader_glsl_get_write_mask_size(write_mask);
2901 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2903 if (mask_size > 1)
2904 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
2905 else
2906 shader_addline(buffer, "int(%s));\n", src_param.param_str);
2909 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
2911 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2912 struct glsl_src_param src_param;
2913 unsigned int mask_size;
2914 DWORD write_mask;
2916 write_mask = shader_glsl_append_dst(buffer, ins);
2917 mask_size = shader_glsl_get_write_mask_size(write_mask);
2918 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2920 if (mask_size > 1)
2921 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
2922 else
2923 shader_addline(buffer, "float(%s));\n", src_param.param_str);
2926 /** Process signed comparison opcodes in GLSL. */
2927 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
2929 struct glsl_src_param src0_param;
2930 struct glsl_src_param src1_param;
2931 DWORD write_mask;
2932 unsigned int mask_size;
2934 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2935 mask_size = shader_glsl_get_write_mask_size(write_mask);
2936 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2937 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2939 if (mask_size > 1) {
2940 const char *compare;
2942 switch(ins->handler_idx)
2944 case WINED3DSIH_SLT: compare = "lessThan"; break;
2945 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
2946 default: compare = "";
2947 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2950 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
2951 src0_param.param_str, src1_param.param_str);
2952 } else {
2953 switch(ins->handler_idx)
2955 case WINED3DSIH_SLT:
2956 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2957 * to return 0.0 but step returns 1.0 because step is not < x
2958 * An alternative is a bvec compare padded with an unused second component.
2959 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2960 * issue. Playing with not() is not possible either because not() does not accept
2961 * a scalar.
2963 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
2964 src0_param.param_str, src1_param.param_str);
2965 break;
2966 case WINED3DSIH_SGE:
2967 /* Here we can use the step() function and safe a conditional */
2968 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
2969 break;
2970 default:
2971 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2977 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
2979 const char *condition_prefix, *condition_suffix;
2980 struct wined3d_shader_dst_param dst;
2981 struct glsl_src_param src0_param;
2982 struct glsl_src_param src1_param;
2983 struct glsl_src_param src2_param;
2984 BOOL temp_destination = FALSE;
2985 DWORD cmp_channel = 0;
2986 unsigned int i, j;
2987 char mask_char[6];
2988 DWORD write_mask;
2990 switch (ins->handler_idx)
2992 case WINED3DSIH_CMP:
2993 condition_prefix = "";
2994 condition_suffix = " >= 0.0";
2995 break;
2997 case WINED3DSIH_CND:
2998 condition_prefix = "";
2999 condition_suffix = " > 0.5";
3000 break;
3002 case WINED3DSIH_MOVC:
3003 condition_prefix = "bool(";
3004 condition_suffix = ")";
3005 break;
3007 default:
3008 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3009 condition_prefix = "<unhandled prefix>";
3010 condition_suffix = "<unhandled suffix>";
3011 break;
3014 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
3016 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3017 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3018 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3019 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3021 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3022 condition_prefix, src0_param.param_str, condition_suffix,
3023 src1_param.param_str, src2_param.param_str);
3024 return;
3027 dst = ins->dst[0];
3029 /* Splitting the instruction up in multiple lines imposes a problem:
3030 * The first lines may overwrite source parameters of the following lines.
3031 * Deal with that by using a temporary destination register if needed. */
3032 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
3033 && ins->src[0].reg.type == dst.reg.type)
3034 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
3035 && ins->src[1].reg.type == dst.reg.type)
3036 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
3037 && ins->src[2].reg.type == dst.reg.type))
3038 temp_destination = TRUE;
3040 /* Cycle through all source0 channels. */
3041 for (i = 0; i < 4; ++i)
3043 write_mask = 0;
3044 /* Find the destination channels which use the current source0 channel. */
3045 for (j = 0; j < 4; ++j)
3047 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
3049 write_mask |= WINED3DSP_WRITEMASK_0 << j;
3050 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
3053 dst.write_mask = ins->dst[0].write_mask & write_mask;
3055 if (temp_destination)
3057 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
3058 continue;
3059 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
3061 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
3062 continue;
3064 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
3065 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3066 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3068 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3069 condition_prefix, src0_param.param_str, condition_suffix,
3070 src1_param.param_str, src2_param.param_str);
3073 if (temp_destination)
3075 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3076 shader_glsl_append_dst(ins->ctx->buffer, ins);
3077 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3081 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3082 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3083 * the compare is done per component of src0. */
3084 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3086 struct glsl_src_param src0_param;
3087 struct glsl_src_param src1_param;
3088 struct glsl_src_param src2_param;
3089 DWORD write_mask;
3090 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3091 ins->ctx->reg_maps->shader_version.minor);
3093 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3095 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3096 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3097 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3098 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3100 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
3101 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3102 else
3103 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3104 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3105 return;
3108 shader_glsl_conditional_move(ins);
3111 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3112 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3114 struct glsl_src_param src0_param;
3115 struct glsl_src_param src1_param;
3116 struct glsl_src_param src2_param;
3117 DWORD write_mask;
3119 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3120 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3121 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3122 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3123 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3124 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3127 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3128 Vertex shaders to GLSL codes */
3129 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3131 int i;
3132 int nComponents = 0;
3133 struct wined3d_shader_dst_param tmp_dst = {{0}};
3134 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3135 struct wined3d_shader_instruction tmp_ins;
3137 memset(&tmp_ins, 0, sizeof(tmp_ins));
3139 /* Set constants for the temporary argument */
3140 tmp_ins.ctx = ins->ctx;
3141 tmp_ins.dst_count = 1;
3142 tmp_ins.dst = &tmp_dst;
3143 tmp_ins.src_count = 2;
3144 tmp_ins.src = tmp_src;
3146 switch(ins->handler_idx)
3148 case WINED3DSIH_M4x4:
3149 nComponents = 4;
3150 tmp_ins.handler_idx = WINED3DSIH_DP4;
3151 break;
3152 case WINED3DSIH_M4x3:
3153 nComponents = 3;
3154 tmp_ins.handler_idx = WINED3DSIH_DP4;
3155 break;
3156 case WINED3DSIH_M3x4:
3157 nComponents = 4;
3158 tmp_ins.handler_idx = WINED3DSIH_DP3;
3159 break;
3160 case WINED3DSIH_M3x3:
3161 nComponents = 3;
3162 tmp_ins.handler_idx = WINED3DSIH_DP3;
3163 break;
3164 case WINED3DSIH_M3x2:
3165 nComponents = 2;
3166 tmp_ins.handler_idx = WINED3DSIH_DP3;
3167 break;
3168 default:
3169 break;
3172 tmp_dst = ins->dst[0];
3173 tmp_src[0] = ins->src[0];
3174 tmp_src[1] = ins->src[1];
3175 for (i = 0; i < nComponents; ++i)
3177 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3178 shader_glsl_dot(&tmp_ins);
3179 ++tmp_src[1].reg.idx[0].offset;
3184 The LRP instruction performs a component-wise linear interpolation
3185 between the second and third operands using the first operand as the
3186 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3187 This is equivalent to mix(src2, src1, src0);
3189 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3191 struct glsl_src_param src0_param;
3192 struct glsl_src_param src1_param;
3193 struct glsl_src_param src2_param;
3194 DWORD write_mask;
3196 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3198 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3199 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3200 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3202 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3203 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3206 /** Process the WINED3DSIO_LIT instruction in GLSL:
3207 * dst.x = dst.w = 1.0
3208 * dst.y = (src0.x > 0) ? src0.x
3209 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3210 * where src.w is clamped at +- 128
3212 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3214 struct glsl_src_param src0_param;
3215 struct glsl_src_param src1_param;
3216 struct glsl_src_param src3_param;
3217 char dst_mask[6];
3219 shader_glsl_append_dst(ins->ctx->buffer, ins);
3220 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3222 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3223 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3224 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3226 /* The sdk specifies the instruction like this
3227 * dst.x = 1.0;
3228 * if(src.x > 0.0) dst.y = src.x
3229 * else dst.y = 0.0.
3230 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3231 * else dst.z = 0.0;
3232 * dst.w = 1.0;
3233 * (where power = src.w clamped between -128 and 128)
3235 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3236 * dst.x = 1.0 ... No further explanation needed
3237 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3238 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3239 * dst.w = 1.0. ... Nothing fancy.
3241 * So we still have one conditional in there. So do this:
3242 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3244 * 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),
3245 * 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.
3246 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3248 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3249 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3250 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3252 shader_addline(ins->ctx->buffer,
3253 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3254 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3255 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3256 src0_param.param_str, src3_param.param_str, dst_mask);
3259 /** Process the WINED3DSIO_DST instruction in GLSL:
3260 * dst.x = 1.0
3261 * dst.y = src0.x * src0.y
3262 * dst.z = src0.z
3263 * dst.w = src1.w
3265 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3267 struct glsl_src_param src0y_param;
3268 struct glsl_src_param src0z_param;
3269 struct glsl_src_param src1y_param;
3270 struct glsl_src_param src1w_param;
3271 char dst_mask[6];
3273 shader_glsl_append_dst(ins->ctx->buffer, ins);
3274 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3276 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3277 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3278 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3279 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3281 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3282 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3285 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3286 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3287 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3289 * dst.x = cos(src0.?)
3290 * dst.y = sin(src0.?)
3291 * dst.z = dst.z
3292 * dst.w = dst.w
3294 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3296 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3297 struct glsl_src_param src0_param;
3298 DWORD write_mask;
3300 if (ins->ctx->reg_maps->shader_version.major < 4)
3302 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3304 write_mask = shader_glsl_append_dst(buffer, ins);
3305 switch (write_mask)
3307 case WINED3DSP_WRITEMASK_0:
3308 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3309 break;
3311 case WINED3DSP_WRITEMASK_1:
3312 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3313 break;
3315 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3316 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3317 src0_param.param_str, src0_param.param_str);
3318 break;
3320 default:
3321 ERR("Write mask should be .x, .y or .xy\n");
3322 break;
3325 return;
3328 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3331 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3333 char dst_mask[6];
3335 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3336 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3337 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3339 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3340 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3341 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3343 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3344 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3346 else
3348 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3349 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3350 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3353 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3355 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3356 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3357 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3361 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3362 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3363 * generate invalid code
3365 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3367 struct glsl_src_param src0_param;
3368 DWORD write_mask;
3370 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3371 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3373 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3376 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3377 * Start a for() loop where src1.y is the initial value of aL,
3378 * increment aL by src1.z for a total of src1.x iterations.
3379 * Need to use a temporary variable for this operation.
3381 /* FIXME: I don't think nested loops will work correctly this way. */
3382 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3384 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3385 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3386 const struct wined3d_shader *shader = ins->ctx->shader;
3387 const struct wined3d_shader_lconst *constant;
3388 struct glsl_src_param src1_param;
3389 const DWORD *control_values = NULL;
3391 if (ins->ctx->reg_maps->shader_version.major < 4)
3393 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3395 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3396 * class hardware doesn't support real varying indexing, but Microsoft
3397 * designed this feature for Shader model 2.x+. If the loop control is
3398 * known at compile time, the GLSL compiler can unroll the loop, and
3399 * replace indirect addressing with direct addressing. */
3400 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3402 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3404 if (constant->idx == ins->src[1].reg.idx[0].offset)
3406 control_values = constant->value;
3407 break;
3412 if (control_values)
3414 struct wined3d_shader_loop_control loop_control;
3415 loop_control.count = control_values[0];
3416 loop_control.start = control_values[1];
3417 loop_control.step = (int)control_values[2];
3419 if (loop_control.step > 0)
3421 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3422 loop_state->current_depth, loop_control.start,
3423 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3424 loop_state->current_depth, loop_control.step);
3426 else if (loop_control.step < 0)
3428 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3429 loop_state->current_depth, loop_control.start,
3430 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3431 loop_state->current_depth, loop_control.step);
3433 else
3435 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3436 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3437 loop_state->current_depth, loop_control.count,
3438 loop_state->current_depth);
3441 else
3443 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3444 loop_state->current_depth, loop_state->current_reg,
3445 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3446 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3449 ++loop_state->current_reg;
3451 else
3453 shader_addline(buffer, "for (;;)\n{\n");
3456 ++loop_state->current_depth;
3459 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3461 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3463 shader_addline(ins->ctx->buffer, "}\n");
3465 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3467 --loop_state->current_depth;
3468 --loop_state->current_reg;
3471 if (ins->handler_idx == WINED3DSIH_ENDREP)
3473 --loop_state->current_depth;
3477 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3479 const struct wined3d_shader *shader = ins->ctx->shader;
3480 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3481 const struct wined3d_shader_lconst *constant;
3482 struct glsl_src_param src0_param;
3483 const DWORD *control_values = NULL;
3485 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3486 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3488 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3490 if (constant->idx == ins->src[0].reg.idx[0].offset)
3492 control_values = constant->value;
3493 break;
3498 if (control_values)
3500 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3501 loop_state->current_depth, loop_state->current_depth,
3502 control_values[0], loop_state->current_depth);
3504 else
3506 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3507 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3508 loop_state->current_depth, loop_state->current_depth,
3509 src0_param.param_str, loop_state->current_depth);
3512 ++loop_state->current_depth;
3515 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3517 struct glsl_src_param src0_param;
3519 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3520 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
3523 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3525 struct glsl_src_param src0_param;
3526 struct glsl_src_param src1_param;
3528 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3529 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3531 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3532 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3535 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3537 shader_addline(ins->ctx->buffer, "} else {\n");
3540 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3542 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3545 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3547 shader_addline(ins->ctx->buffer, "break;\n");
3550 /* FIXME: According to MSDN the compare is done per component. */
3551 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3553 struct glsl_src_param src0_param;
3554 struct glsl_src_param src1_param;
3556 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3557 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3559 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3560 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3563 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
3565 struct glsl_src_param src_param;
3567 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
3568 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
3571 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
3573 shader_addline(ins->ctx->buffer, "}\n");
3574 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
3577 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
3579 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
3582 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3584 struct glsl_src_param src1_param;
3586 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3587 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
3588 src1_param.param_str, ins->src[0].reg.idx[0].offset);
3591 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3593 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3594 * function only suppresses the unhandled instruction warning
3598 /*********************************************
3599 * Pixel Shader Specific Code begins here
3600 ********************************************/
3601 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3603 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3604 ins->ctx->reg_maps->shader_version.minor);
3605 struct glsl_sample_function sample_function;
3606 DWORD sample_flags = 0;
3607 DWORD resource_idx;
3608 DWORD mask = 0, swizzle;
3609 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3611 /* 1.0-1.4: Use destination register as sampler source.
3612 * 2.0+: Use provided sampler source. */
3613 if (shader_version < WINED3D_SHADER_VERSION(2,0))
3614 resource_idx = ins->dst[0].reg.idx[0].offset;
3615 else
3616 resource_idx = ins->src[1].reg.idx[0].offset;
3618 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3620 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3621 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3622 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
3624 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3625 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3627 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3628 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3630 case WINED3D_TTFF_COUNT1:
3631 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3632 break;
3633 case WINED3D_TTFF_COUNT2:
3634 mask = WINED3DSP_WRITEMASK_1;
3635 break;
3636 case WINED3D_TTFF_COUNT3:
3637 mask = WINED3DSP_WRITEMASK_2;
3638 break;
3639 case WINED3D_TTFF_COUNT4:
3640 case WINED3D_TTFF_DISABLE:
3641 mask = WINED3DSP_WRITEMASK_3;
3642 break;
3646 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3648 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3650 if (src_mod == WINED3DSPSM_DZ) {
3651 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3652 mask = WINED3DSP_WRITEMASK_2;
3653 } else if (src_mod == WINED3DSPSM_DW) {
3654 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3655 mask = WINED3DSP_WRITEMASK_3;
3658 else
3660 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
3661 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3663 /* ps 2.0 texldp instruction always divides by the fourth component. */
3664 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3665 mask = WINED3DSP_WRITEMASK_3;
3669 if (priv->cur_ps_args->np2_fixup & (1 << resource_idx))
3670 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3672 shader_glsl_get_sample_function(ins->ctx, resource_idx, sample_flags, &sample_function);
3673 mask |= sample_function.coord_mask;
3675 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3676 else swizzle = ins->src[1].swizzle;
3678 /* 1.0-1.3: Use destination register as coordinate source.
3679 1.4+: Use provided coordinate source register. */
3680 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3682 char coord_mask[6];
3683 shader_glsl_write_mask_to_str(mask, coord_mask);
3684 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
3685 "T%u%s", resource_idx, coord_mask);
3687 else
3689 struct glsl_src_param coord_param;
3690 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3691 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3693 struct glsl_src_param bias;
3694 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3695 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3696 "%s", coord_param.param_str);
3697 } else {
3698 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
3699 "%s", coord_param.param_str);
3704 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3706 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3707 struct glsl_src_param coord_param, dx_param, dy_param;
3708 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3709 struct glsl_sample_function sample_function;
3710 DWORD sampler_idx;
3711 DWORD swizzle = ins->src[1].swizzle;
3712 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3714 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3716 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3717 shader_glsl_tex(ins);
3718 return;
3721 sampler_idx = ins->src[1].reg.idx[0].offset;
3722 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3723 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3725 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3726 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3727 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3728 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3730 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3731 "%s", coord_param.param_str);
3734 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3736 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3737 struct glsl_src_param coord_param, lod_param;
3738 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3739 struct glsl_sample_function sample_function;
3740 DWORD sampler_idx;
3741 DWORD swizzle = ins->src[1].swizzle;
3742 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3744 sampler_idx = ins->src[1].reg.idx[0].offset;
3745 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3746 && priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3747 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3749 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3750 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3752 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3754 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3755 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
3757 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
3758 * However, the NVIDIA drivers allow them in fragment shaders as well,
3759 * even without the appropriate extension. */
3760 WARN("Using %s in fragment shader.\n", sample_function.name);
3762 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3763 "%s", coord_param.param_str);
3766 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
3767 unsigned int resource_idx, unsigned int sampler_idx)
3769 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
3770 unsigned int i;
3772 for (i = 0; i < sampler_map->count; ++i)
3774 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
3775 return entries[i].bind_idx;
3778 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
3780 return ~0u;
3783 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
3785 struct glsl_sample_function sample_function;
3786 struct glsl_src_param coord_param;
3787 unsigned int sampler_idx;
3789 shader_glsl_get_sample_function(ins->ctx, ins->src[1].reg.idx[0].offset, 0, &sample_function);
3790 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3791 sampler_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
3792 ins->src[1].reg.idx[0].offset, ins->src[2].reg.idx[0].offset);
3793 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE,
3794 NULL, NULL, NULL, "%s", coord_param.param_str);
3797 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3799 /* FIXME: Make this work for more than just 2D textures */
3800 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3801 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3803 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3805 char dst_mask[6];
3807 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3808 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3809 ins->dst[0].reg.idx[0].offset, dst_mask);
3811 else
3813 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3814 DWORD reg = ins->src[0].reg.idx[0].offset;
3815 char dst_swizzle[6];
3817 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3819 if (src_mod == WINED3DSPSM_DZ)
3821 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3822 struct glsl_src_param div_param;
3824 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3826 if (mask_size > 1) {
3827 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3828 } else {
3829 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3832 else if (src_mod == WINED3DSPSM_DW)
3834 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3835 struct glsl_src_param div_param;
3837 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3839 if (mask_size > 1) {
3840 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3841 } else {
3842 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3844 } else {
3845 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3850 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3851 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3852 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3853 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3855 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3856 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3857 struct glsl_sample_function sample_function;
3858 struct glsl_src_param src0_param;
3859 UINT mask_size;
3861 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3863 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3864 * scalar, and projected sampling would require 4.
3866 * It is a dependent read - not valid with conditional NP2 textures
3868 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3869 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3871 switch(mask_size)
3873 case 1:
3874 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3875 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3876 break;
3878 case 2:
3879 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3880 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3881 break;
3883 case 3:
3884 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3885 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3886 break;
3888 default:
3889 FIXME("Unexpected mask size %u\n", mask_size);
3890 break;
3894 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3895 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3896 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3898 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3899 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3900 struct glsl_src_param src0_param;
3901 DWORD dst_mask;
3902 unsigned int mask_size;
3904 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3905 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3906 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3908 if (mask_size > 1) {
3909 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3910 } else {
3911 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3915 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3916 * Calculate the depth as dst.x / dst.y */
3917 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3919 struct glsl_dst_param dst_param;
3921 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3923 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3924 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3925 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3926 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3927 * >= 1.0 or < 0.0
3929 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3930 dst_param.reg_name, dst_param.reg_name);
3933 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3934 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3935 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3936 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3938 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3940 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3941 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3942 struct glsl_src_param src0_param;
3944 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3946 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3947 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3950 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3951 * Calculate the 1st of a 2-row matrix multiplication. */
3952 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3954 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3955 DWORD reg = ins->dst[0].reg.idx[0].offset;
3956 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3957 struct glsl_src_param src0_param;
3959 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3960 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3963 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3964 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3965 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3967 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3968 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3969 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3970 DWORD reg = ins->dst[0].reg.idx[0].offset;
3971 struct glsl_src_param src0_param;
3973 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3974 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
3975 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
3978 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3980 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3981 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3982 struct glsl_sample_function sample_function;
3983 DWORD reg = ins->dst[0].reg.idx[0].offset;
3984 struct glsl_src_param src0_param;
3986 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3987 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3989 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3991 /* Sample the texture using the calculated coordinates */
3992 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
3995 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3996 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3997 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3999 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4000 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4001 struct glsl_sample_function sample_function;
4002 DWORD reg = ins->dst[0].reg.idx[0].offset;
4003 struct glsl_src_param src0_param;
4005 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4006 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4008 /* Dependent read, not valid with conditional NP2 */
4009 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4011 /* Sample the texture using the calculated coordinates */
4012 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
4014 tex_mx->current_row = 0;
4017 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4018 * Perform the 3rd row of a 3x3 matrix multiply */
4019 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
4021 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4022 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4023 DWORD reg = ins->dst[0].reg.idx[0].offset;
4024 struct glsl_src_param src0_param;
4025 char dst_mask[6];
4027 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4029 shader_glsl_append_dst(ins->ctx->buffer, ins);
4030 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4031 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
4033 tex_mx->current_row = 0;
4036 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4037 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4038 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
4040 struct glsl_src_param src0_param;
4041 struct glsl_src_param src1_param;
4042 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
4043 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4044 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4045 struct glsl_sample_function sample_function;
4046 DWORD reg = ins->dst[0].reg.idx[0].offset;
4047 char coord_mask[6];
4049 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4050 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4052 /* Perform the last matrix multiply operation */
4053 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4054 /* Reflection calculation */
4055 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
4057 /* Dependent read, not valid with conditional NP2 */
4058 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4059 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4061 /* Sample the texture */
4062 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4063 NULL, NULL, NULL, "tmp0%s", coord_mask);
4065 tex_mx->current_row = 0;
4068 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4069 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4070 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
4072 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
4073 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4074 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4075 struct glsl_sample_function sample_function;
4076 DWORD reg = ins->dst[0].reg.idx[0].offset;
4077 struct glsl_src_param src0_param;
4078 char coord_mask[6];
4080 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4082 /* Perform the last matrix multiply operation */
4083 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
4085 /* Construct the eye-ray vector from w coordinates */
4086 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
4087 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
4088 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4090 /* Dependent read, not valid with conditional NP2 */
4091 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4092 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4094 /* Sample the texture using the calculated coordinates */
4095 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4096 NULL, NULL, NULL, "tmp0%s", coord_mask);
4098 tex_mx->current_row = 0;
4101 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4102 * Apply a fake bump map transform.
4103 * texbem is pshader <= 1.3 only, this saves a few version checks
4105 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
4107 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4108 struct glsl_sample_function sample_function;
4109 struct glsl_src_param coord_param;
4110 DWORD sampler_idx;
4111 DWORD mask;
4112 DWORD flags;
4113 char coord_mask[6];
4115 sampler_idx = ins->dst[0].reg.idx[0].offset;
4116 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4117 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4119 /* Dependent read, not valid with conditional NP2 */
4120 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4121 mask = sample_function.coord_mask;
4123 shader_glsl_write_mask_to_str(mask, coord_mask);
4125 /* With projected textures, texbem only divides the static texture coord,
4126 * not the displacement, so we can't let GL handle this. */
4127 if (flags & WINED3D_PSARGS_PROJECTED)
4129 DWORD div_mask=0;
4130 char coord_div_mask[3];
4131 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4133 case WINED3D_TTFF_COUNT1:
4134 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4135 break;
4136 case WINED3D_TTFF_COUNT2:
4137 div_mask = WINED3DSP_WRITEMASK_1;
4138 break;
4139 case WINED3D_TTFF_COUNT3:
4140 div_mask = WINED3DSP_WRITEMASK_2;
4141 break;
4142 case WINED3D_TTFF_COUNT4:
4143 case WINED3D_TTFF_DISABLE:
4144 div_mask = WINED3DSP_WRITEMASK_3;
4145 break;
4147 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4148 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4151 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4153 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4154 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4155 coord_param.param_str, coord_mask);
4157 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4159 struct glsl_src_param luminance_param;
4160 struct glsl_dst_param dst_param;
4162 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4163 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4165 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4166 dst_param.reg_name, dst_param.mask_str,
4167 luminance_param.param_str, sampler_idx, sampler_idx);
4171 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4173 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4174 struct glsl_src_param src0_param, src1_param;
4176 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4177 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4179 shader_glsl_append_dst(ins->ctx->buffer, ins);
4180 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4181 src0_param.param_str, sampler_idx, src1_param.param_str);
4184 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4185 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4186 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4188 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4189 struct glsl_sample_function sample_function;
4190 struct glsl_src_param src0_param;
4192 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4194 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4195 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4196 "%s.wx", src0_param.reg_name);
4199 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4200 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4201 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4203 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4204 struct glsl_sample_function sample_function;
4205 struct glsl_src_param src0_param;
4207 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4209 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4210 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4211 "%s.yz", src0_param.reg_name);
4214 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4215 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4216 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4218 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4219 struct glsl_sample_function sample_function;
4220 struct glsl_src_param src0_param;
4222 /* Dependent read, not valid with conditional NP2 */
4223 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4224 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4226 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4227 "%s", src0_param.param_str);
4230 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4231 * If any of the first 3 components are < 0, discard this pixel */
4232 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4234 struct glsl_dst_param dst_param;
4236 /* The argument is a destination parameter, and no writemasks are allowed */
4237 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4238 if (ins->ctx->reg_maps->shader_version.major >= 2)
4240 if (ins->ctx->reg_maps->shader_version.major >= 4)
4241 FIXME("SM4 discard not implemented.\n");
4242 /* 2.0 shaders compare all 4 components in texkill */
4243 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4244 } else {
4245 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
4246 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
4247 * 4 components are defined, only the first 3 are used
4249 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4253 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4254 * dst = dot2(src0, src1) + src2 */
4255 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4257 struct glsl_src_param src0_param;
4258 struct glsl_src_param src1_param;
4259 struct glsl_src_param src2_param;
4260 DWORD write_mask;
4261 unsigned int mask_size;
4263 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4264 mask_size = shader_glsl_get_write_mask_size(write_mask);
4266 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4267 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4268 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4270 if (mask_size > 1) {
4271 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4272 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4273 } else {
4274 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4275 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4279 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
4280 const struct wined3d_shader_signature *input_signature,
4281 const struct wined3d_shader_reg_maps *reg_maps,
4282 enum vertexprocessing_mode vertexprocessing)
4284 unsigned int i;
4286 for (i = 0; i < input_signature->element_count; ++i)
4288 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4289 const char *semantic_name;
4290 UINT semantic_idx;
4291 char reg_mask[6];
4293 /* Unused */
4294 if (!(reg_maps->input_registers & (1 << input->register_idx)))
4295 continue;
4297 semantic_name = input->semantic_name;
4298 semantic_idx = input->semantic_idx;
4299 shader_glsl_write_mask_to_str(input->mask, reg_mask);
4301 if (vertexprocessing == vertexshader)
4303 if (input->sysval_semantic == WINED3D_SV_POSITION)
4304 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
4305 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4306 else
4307 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
4308 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
4309 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
4311 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4313 if (semantic_idx < 8 && vertexprocessing == pretransformed)
4314 shader_addline(buffer, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4315 shader->u.ps.input_reg_map[input->register_idx], reg_mask, semantic_idx, reg_mask);
4316 else
4317 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4318 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4320 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4322 if (!semantic_idx)
4323 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4324 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4325 else if (semantic_idx == 1)
4326 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4327 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4328 else
4329 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4330 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4332 else
4334 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4335 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4340 /*********************************************
4341 * Vertex Shader Specific Code begins here
4342 ********************************************/
4344 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4346 struct glsl_program_key key;
4348 key.vs_id = entry->vs.id;
4349 key.gs_id = entry->gs.id;
4350 key.ps_id = entry->ps.id;
4352 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4354 ERR("Failed to insert program entry.\n");
4358 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4359 GLuint vs_id, GLuint gs_id, GLuint ps_id)
4361 struct wine_rb_entry *entry;
4362 struct glsl_program_key key;
4364 key.vs_id = vs_id;
4365 key.gs_id = gs_id;
4366 key.ps_id = ps_id;
4368 entry = wine_rb_get(&priv->program_lookup, &key);
4369 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4372 /* Context activation is done by the caller. */
4373 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4374 struct glsl_shader_prog_link *entry)
4376 struct glsl_program_key key;
4378 key.vs_id = entry->vs.id;
4379 key.gs_id = entry->gs.id;
4380 key.ps_id = entry->ps.id;
4381 wine_rb_remove(&priv->program_lookup, &key);
4383 GL_EXTCALL(glDeleteProgram(entry->id));
4384 if (entry->vs.id)
4385 list_remove(&entry->vs.shader_entry);
4386 if (entry->gs.id)
4387 list_remove(&entry->gs.shader_entry);
4388 if (entry->ps.id)
4389 list_remove(&entry->ps.shader_entry);
4390 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4391 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4392 HeapFree(GetProcessHeap(), 0, entry);
4395 static void handle_ps3_input(struct wined3d_shader_buffer *buffer,
4396 const struct wined3d_gl_info *gl_info, const DWORD *map,
4397 const struct wined3d_shader_signature *input_signature,
4398 const struct wined3d_shader_reg_maps *reg_maps_in,
4399 const struct wined3d_shader_signature *output_signature,
4400 const struct wined3d_shader_reg_maps *reg_maps_out)
4402 unsigned int i, j;
4403 DWORD *set;
4404 DWORD in_idx;
4405 unsigned int in_count = vec4_varyings(3, gl_info);
4406 char reg_mask[6];
4407 char destination[50];
4409 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
4411 for (i = 0; i < input_signature->element_count; ++i)
4413 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4415 if (!(reg_maps_in->input_registers & (1 << input->register_idx)))
4416 continue;
4418 in_idx = map[input->register_idx];
4419 /* Declared, but not read register */
4420 if (in_idx == ~0u)
4421 continue;
4422 if (in_idx >= (in_count + 2))
4424 FIXME("More input varyings declared than supported, expect issues.\n");
4425 continue;
4428 if (in_idx == in_count)
4429 sprintf(destination, "gl_FrontColor");
4430 else if (in_idx == in_count + 1)
4431 sprintf(destination, "gl_FrontSecondaryColor");
4432 else
4433 sprintf(destination, "ps_link[%u]", in_idx);
4435 if (!set[in_idx])
4436 set[in_idx] = ~0u;
4438 for (j = 0; j < output_signature->element_count; ++j)
4440 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
4441 DWORD mask;
4443 if (!(reg_maps_out->output_registers & (1 << output->register_idx))
4444 || input->semantic_idx != output->semantic_idx
4445 || strcmp(input->semantic_name, output->semantic_name)
4446 || !(mask = input->mask & output->mask))
4447 continue;
4449 if (set[in_idx] == ~0u)
4450 set[in_idx] = mask;
4451 else
4452 set[in_idx] |= mask;
4453 shader_glsl_write_mask_to_str(mask, reg_mask);
4455 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4456 destination, reg_mask, output->register_idx, reg_mask);
4460 for (i = 0; i < in_count + 2; ++i)
4462 unsigned int size;
4464 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4465 continue;
4467 if (set[i] == ~0U) set[i] = 0;
4469 size = 0;
4470 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4471 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4472 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4473 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4474 reg_mask[size] = '\0';
4476 if (i == in_count)
4477 sprintf(destination, "gl_FrontColor");
4478 else if (i == in_count + 1)
4479 sprintf(destination, "gl_FrontSecondaryColor");
4480 else
4481 sprintf(destination, "ps_link[%u]", i);
4483 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
4484 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
4487 HeapFree(GetProcessHeap(), 0, set);
4490 /* Context activation is done by the caller. */
4491 static GLuint generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
4492 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4493 const struct wined3d_gl_info *gl_info)
4495 GLuint ret = 0;
4496 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4497 unsigned int i;
4498 const char *semantic_name;
4499 UINT semantic_idx;
4500 char reg_mask[6];
4502 shader_buffer_clear(buffer);
4504 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &vs->reg_maps.shader_version));
4506 if (ps_major < 3)
4508 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4510 for (i = 0; i < vs->output_signature.element_count; ++i)
4512 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
4513 DWORD write_mask;
4515 if (!(vs->reg_maps.output_registers & (1 << output->register_idx)))
4516 continue;
4518 semantic_name = output->semantic_name;
4519 semantic_idx = output->semantic_idx;
4520 write_mask = output->mask;
4521 shader_glsl_write_mask_to_str(write_mask, reg_mask);
4523 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4525 if (!semantic_idx)
4526 shader_addline(buffer, "gl_FrontColor%s = vs_out[%u]%s;\n",
4527 reg_mask, output->register_idx, reg_mask);
4528 else if (semantic_idx == 1)
4529 shader_addline(buffer, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4530 reg_mask, output->register_idx, reg_mask);
4532 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
4534 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4535 reg_mask, output->register_idx, reg_mask);
4537 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4539 if (semantic_idx < 8)
4541 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
4542 write_mask |= WINED3DSP_WRITEMASK_3;
4544 shader_addline(buffer, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4545 semantic_idx, reg_mask, output->register_idx, reg_mask);
4546 if (!(write_mask & WINED3DSP_WRITEMASK_3))
4547 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
4550 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4552 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", output->register_idx, reg_mask[1]);
4554 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
4556 shader_addline(buffer, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n",
4557 output->register_idx, reg_mask[1]);
4560 shader_addline(buffer, "}\n");
4562 else
4564 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
4565 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4566 shader_addline(buffer, "varying vec4 ps_link[%u];\n", in_count);
4567 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4569 /* First, sort out position and point size. Those are not passed to the pixel shader */
4570 for (i = 0; i < vs->output_signature.element_count; ++i)
4572 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
4574 if (!(vs->reg_maps.output_registers & (1 << output->register_idx)))
4575 continue;
4577 semantic_name = output->semantic_name;
4578 semantic_idx = output->semantic_idx;
4579 shader_glsl_write_mask_to_str(output->mask, reg_mask);
4581 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
4583 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4584 reg_mask, output->register_idx, reg_mask);
4586 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4588 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", output->register_idx, reg_mask[1]);
4592 /* Then, fix the pixel shader input */
4593 handle_ps3_input(buffer, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
4594 &ps->reg_maps, &vs->output_signature, &vs->reg_maps);
4596 shader_addline(buffer, "}\n");
4599 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
4600 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
4601 shader_glsl_compile(gl_info, ret, buffer->buffer);
4603 return ret;
4606 static void shader_glsl_generate_srgb_write_correction(struct wined3d_shader_buffer *buffer)
4608 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4609 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4610 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4611 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4612 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4613 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4616 static void shader_glsl_generate_fog_code(struct wined3d_shader_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
4618 switch (mode)
4620 case WINED3D_FFP_PS_FOG_OFF:
4621 return;
4623 case WINED3D_FFP_PS_FOG_LINEAR:
4624 shader_addline(buffer, "float Fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;\n");
4625 break;
4627 case WINED3D_FFP_PS_FOG_EXP:
4628 /* Fog = e^-(gl_Fog.density * gl_FogFragCoord) */
4629 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4630 break;
4632 case WINED3D_FFP_PS_FOG_EXP2:
4633 /* Fog = e^-((gl_Fog.density * gl_FogFragCoord)^2) */
4634 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4635 break;
4637 default:
4638 ERR("Invalid fog mode %#x.\n", mode);
4639 return;
4642 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, clamp(Fog, 0.0, 1.0));\n");
4645 /* Context activation is done by the caller. */
4646 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
4647 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4648 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
4650 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4651 const struct wined3d_gl_info *gl_info = context->gl_info;
4652 const DWORD *function = shader->function;
4653 struct shader_glsl_ctx_priv priv_ctx;
4655 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4656 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
4658 memset(&priv_ctx, 0, sizeof(priv_ctx));
4659 priv_ctx.cur_ps_args = args;
4660 priv_ctx.cur_np2fixup_info = np2fixup_info;
4662 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
4664 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4665 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4666 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4667 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
4668 /* The spec says that it doesn't have to be explicitly enabled, but the
4669 * nvidia drivers write a warning if we don't do so. */
4670 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4671 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4672 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4673 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4674 if (gl_info->supported[EXT_GPU_SHADER4])
4675 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4677 /* Base Declarations */
4678 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4680 /* Pack 3.0 inputs */
4681 if (reg_maps->shader_version.major >= 3)
4682 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args->vp_mode);
4684 /* Base Shader Body */
4685 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4687 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4688 if (reg_maps->shader_version.major < 2)
4690 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4691 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4694 if (args->srgb_correction)
4695 shader_glsl_generate_srgb_write_correction(buffer);
4697 /* SM < 3 does not replace the fog stage. */
4698 if (reg_maps->shader_version.major < 3)
4699 shader_glsl_generate_fog_code(buffer, args->fog);
4701 shader_addline(buffer, "}\n");
4703 TRACE("Compiling shader object %u.\n", shader_id);
4704 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4706 return shader_id;
4709 /* Context activation is done by the caller. */
4710 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4711 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4712 const struct vs_compile_args *args)
4714 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4715 const struct wined3d_gl_info *gl_info = context->gl_info;
4716 const DWORD *function = shader->function;
4717 struct shader_glsl_ctx_priv priv_ctx;
4719 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4720 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
4722 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
4724 if (gl_info->supported[ARB_DRAW_INSTANCED])
4725 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
4726 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4727 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4728 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4729 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4730 if (gl_info->supported[EXT_GPU_SHADER4])
4731 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4733 memset(&priv_ctx, 0, sizeof(priv_ctx));
4734 priv_ctx.cur_vs_args = args;
4736 /* Base Declarations */
4737 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4739 /* Base Shader Body */
4740 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4742 /* Unpack outputs */
4743 shader_addline(buffer, "order_ps_input(vs_out);\n");
4745 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4746 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4747 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4748 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4750 if (args->fog_src == VS_FOG_Z)
4751 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4752 else if (!reg_maps->fog)
4753 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4755 /* We always store the clipplanes without y inversion */
4756 if (args->clip_enabled)
4757 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4759 /* Write the final position.
4761 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4762 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4763 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4764 * contains 1.0 to allow a mad.
4766 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4767 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4769 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4771 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4772 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4773 * which is the same as z = z * 2 - w.
4775 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4777 shader_addline(buffer, "}\n");
4779 TRACE("Compiling shader object %u.\n", shader_id);
4780 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4782 return shader_id;
4785 /* Context activation is done by the caller. */
4786 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
4787 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader)
4789 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4790 const struct wined3d_gl_info *gl_info = context->gl_info;
4791 const DWORD *function = shader->function;
4792 struct shader_glsl_ctx_priv priv_ctx;
4793 GLuint shader_id;
4795 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
4797 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
4799 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
4800 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
4801 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4802 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4803 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4804 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4805 if (gl_info->supported[EXT_GPU_SHADER4])
4806 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4808 memset(&priv_ctx, 0, sizeof(priv_ctx));
4809 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4810 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4811 shader_addline(buffer, "}\n");
4813 TRACE("Compiling shader object %u.\n", shader_id);
4814 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4816 return shader_id;
4819 static GLuint find_glsl_pshader(const struct wined3d_context *context,
4820 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4821 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
4823 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
4824 struct glsl_shader_private *shader_data;
4825 struct ps_np2fixup_info *np2fixup;
4826 UINT i;
4827 DWORD new_size;
4828 GLuint ret;
4830 if (!shader->backend_data)
4832 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4833 if (!shader->backend_data)
4835 ERR("Failed to allocate backend data.\n");
4836 return 0;
4839 shader_data = shader->backend_data;
4840 gl_shaders = shader_data->gl_shaders.ps;
4842 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4843 * so a linear search is more performant than a hashmap or a binary search
4844 * (cache coherency etc)
4846 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4848 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
4850 if (args->np2_fixup)
4851 *np2fixup_info = &gl_shaders[i].np2fixup;
4852 return gl_shaders[i].id;
4856 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4857 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4858 if (shader_data->num_gl_shaders)
4860 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4861 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
4862 new_size * sizeof(*gl_shaders));
4864 else
4866 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4867 new_size = 1;
4870 if(!new_array) {
4871 ERR("Out of memory\n");
4872 return 0;
4874 shader_data->gl_shaders.ps = new_array;
4875 shader_data->shader_array_size = new_size;
4876 gl_shaders = new_array;
4879 gl_shaders[shader_data->num_gl_shaders].args = *args;
4881 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
4882 memset(np2fixup, 0, sizeof(*np2fixup));
4883 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
4885 pixelshader_update_resource_types(shader, args->tex_types);
4887 shader_buffer_clear(buffer);
4888 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4889 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4891 return ret;
4894 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4895 const DWORD use_map) {
4896 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4897 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4898 return stored->fog_src == new->fog_src;
4901 static GLuint find_glsl_vshader(const struct wined3d_context *context,
4902 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4903 const struct vs_compile_args *args)
4905 UINT i;
4906 DWORD new_size;
4907 DWORD use_map = context->stream_info.use_map;
4908 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
4909 struct glsl_shader_private *shader_data;
4910 GLuint ret;
4912 if (!shader->backend_data)
4914 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4915 if (!shader->backend_data)
4917 ERR("Failed to allocate backend data.\n");
4918 return 0;
4921 shader_data = shader->backend_data;
4922 gl_shaders = shader_data->gl_shaders.vs;
4924 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4925 * so a linear search is more performant than a hashmap or a binary search
4926 * (cache coherency etc)
4928 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4930 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
4931 return gl_shaders[i].id;
4934 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4936 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4937 if (shader_data->num_gl_shaders)
4939 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4940 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
4941 new_size * sizeof(*gl_shaders));
4943 else
4945 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4946 new_size = 1;
4949 if(!new_array) {
4950 ERR("Out of memory\n");
4951 return 0;
4953 shader_data->gl_shaders.vs = new_array;
4954 shader_data->shader_array_size = new_size;
4955 gl_shaders = new_array;
4958 gl_shaders[shader_data->num_gl_shaders].args = *args;
4960 shader_buffer_clear(buffer);
4961 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4962 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4964 return ret;
4967 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
4968 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader)
4970 struct glsl_gs_compiled_shader *gl_shaders;
4971 struct glsl_shader_private *shader_data;
4972 GLuint ret;
4974 if (!shader->backend_data)
4976 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
4978 ERR("Failed to allocate backend data.\n");
4979 return 0;
4982 shader_data = shader->backend_data;
4983 gl_shaders = shader_data->gl_shaders.gs;
4985 if (shader_data->num_gl_shaders)
4986 return gl_shaders[0].id;
4988 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4990 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
4992 ERR("Failed to allocate GL shader array.\n");
4993 return 0;
4995 shader_data->shader_array_size = 1;
4996 gl_shaders = shader_data->gl_shaders.gs;
4998 shader_buffer_clear(buffer);
4999 ret = shader_glsl_generate_geometry_shader(context, buffer, shader);
5000 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5002 return ret;
5005 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
5007 switch (mcs)
5009 case WINED3D_MCS_MATERIAL:
5010 return material;
5011 case WINED3D_MCS_COLOR1:
5012 return "gl_Color";
5013 case WINED3D_MCS_COLOR2:
5014 return "gl_SecondaryColor";
5015 default:
5016 ERR("Invalid material color source %#x.\n", mcs);
5017 return "<invalid>";
5021 static void shader_glsl_ffp_vertex_lighting(struct wined3d_shader_buffer *buffer,
5022 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
5024 const char *diffuse, *specular, *emission, *ambient;
5025 enum wined3d_light_type light_type;
5026 unsigned int i;
5028 if (!settings->lighting)
5030 shader_addline(buffer, "gl_FrontColor = gl_Color;\n");
5031 shader_addline(buffer, "gl_FrontSecondaryColor = gl_SecondaryColor;\n");
5032 return;
5035 shader_addline(buffer, "vec3 ambient = gl_LightModel.ambient.xyz;\n");
5036 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
5037 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
5038 shader_addline(buffer, "vec3 dir, dst;\n");
5039 shader_addline(buffer, "float att, t;\n");
5041 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "gl_FrontMaterial.ambient");
5042 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "gl_FrontMaterial.diffuse");
5043 specular = shader_glsl_ffp_mcs(settings->specular_source, "gl_FrontMaterial.specular");
5044 emission = shader_glsl_ffp_mcs(settings->emission_source, "gl_FrontMaterial.emission");
5046 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
5048 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
5049 switch (light_type)
5051 case WINED3D_LIGHT_POINT:
5052 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
5053 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5054 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5055 shader_addline(buffer, "dst.x = 1.0;\n");
5056 shader_addline(buffer, "att = dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
5057 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n", i, i, i);
5058 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz / att;\n", i);
5059 if (!settings->normal)
5060 break;
5061 shader_addline(buffer, "dir = normalize(dir);\n");
5062 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5063 " * gl_LightSource[%u].diffuse.xyz) / att;\n", i);
5064 if (settings->localviewer)
5065 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5066 else
5067 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
5068 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
5069 " * gl_LightSource[%u].specular) / att;\n", i);
5070 break;
5072 case WINED3D_LIGHT_SPOT:
5073 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
5074 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5075 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5076 shader_addline(buffer, "dst.x = 1.0;\n");
5077 shader_addline(buffer, "dir = normalize(dir);\n");
5078 shader_addline(buffer, "t = dot(-dir, normalize(gl_LightSource[%u].spotDirection));\n", i);
5079 shader_addline(buffer, "if (t < gl_LightSource[%u].spotCosCutoff) att = 0.0;\n", i);
5080 shader_addline(buffer, "else att = pow(t, gl_LightSource[%u].spotExponent)"
5081 " / dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
5082 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n",
5083 i, i, i, i);
5084 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz * att;\n", i);
5085 if (!settings->normal)
5086 break;
5087 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5088 " * gl_LightSource[%u].diffuse.xyz) * att;\n", i);
5089 if (settings->localviewer)
5090 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5091 else
5092 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
5093 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
5094 " * gl_LightSource[%u].specular) * att;\n", i);
5095 break;
5097 case WINED3D_LIGHT_DIRECTIONAL:
5098 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz;\n", i);
5099 if (!settings->normal)
5100 break;
5101 shader_addline(buffer, "dir = normalize(gl_LightSource[%u].position.xyz);\n", i);
5102 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5103 " * gl_LightSource[%u].diffuse.xyz;\n", i);
5104 shader_addline(buffer, "t = dot(normal, gl_LightSource[%u].halfVector.xyz);\n", i);
5105 shader_addline(buffer, "if (t > 0.0) specular += pow(t, gl_FrontMaterial.shininess)"
5106 " * gl_LightSource[%u].specular;\n", i);
5107 break;
5109 default:
5110 if (light_type)
5111 FIXME("Unhandled light type %#x.\n", light_type);
5112 continue;
5116 shader_addline(buffer, "gl_FrontColor.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
5117 ambient, diffuse, emission);
5118 shader_addline(buffer, "gl_FrontColor.w = %s.w;\n", diffuse);
5119 shader_addline(buffer, "gl_FrontSecondaryColor = %s * specular;\n", specular);
5122 /* Context activation is done by the caller. */
5123 static GLuint shader_glsl_generate_ffp_vertex_shader(struct wined3d_shader_buffer *buffer,
5124 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
5126 GLuint shader_obj;
5127 unsigned int i;
5129 shader_buffer_clear(buffer);
5131 shader_addline(buffer, "#version 120\n");
5132 shader_addline(buffer, "\n");
5134 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix;\n");
5135 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
5136 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
5138 shader_addline(buffer, "\nvoid main()\n{\n");
5139 shader_addline(buffer, "float m;\n");
5140 shader_addline(buffer, "vec3 r;\n");
5142 if (settings->transformed)
5144 shader_addline(buffer, "vec4 ec_pos = vec4(gl_Vertex.xyz, 1.0);\n");
5145 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5146 shader_addline(buffer, "if (gl_Vertex.w != 0.0) gl_Position /= gl_Vertex.w;\n");
5148 else
5150 shader_addline(buffer, "vec4 ec_pos = ffp_modelview_matrix * gl_Vertex;\n");
5151 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5152 if (settings->clipping)
5153 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
5154 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
5157 if (!settings->normal)
5158 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
5159 else if (settings->normalize)
5160 shader_addline(buffer, "vec3 normal = normalize(ffp_normal_matrix * gl_Normal);\n");
5161 else
5162 shader_addline(buffer, "vec3 normal = ffp_normal_matrix * gl_Normal;\n");
5164 shader_glsl_ffp_vertex_lighting(buffer, settings, gl_info);
5166 for (i = 0; i < MAX_TEXTURES; ++i)
5168 switch (settings->texgen[i] << WINED3D_FFP_TCI_SHIFT)
5170 case WINED3DTSS_TCI_PASSTHRU:
5171 if (settings->texcoords & (1 << i))
5172 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * gl_MultiTexCoord%d;\n",
5173 i, i, i);
5174 break;
5176 case WINED3DTSS_TCI_CAMERASPACENORMAL:
5177 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * vec4(normal, 1.0);\n", i, i);
5178 break;
5180 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
5181 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * ec_pos;\n", i, i);
5182 break;
5184 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
5185 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
5186 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
5187 break;
5189 case WINED3DTSS_TCI_SPHEREMAP:
5190 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
5191 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
5192 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
5193 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
5194 break;
5196 default:
5197 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
5198 break;
5202 switch (settings->fog_mode)
5204 case WINED3D_FFP_VS_FOG_OFF:
5205 break;
5207 case WINED3D_FFP_VS_FOG_FOGCOORD:
5208 shader_addline(buffer, "gl_FogFragCoord = gl_SecondaryColor.w * 255.0;\n");
5209 break;
5211 case WINED3D_FFP_VS_FOG_RANGE:
5212 shader_addline(buffer, "gl_FogFragCoord = length(ec_pos.xyz);\n");
5213 break;
5215 case WINED3D_FFP_VS_FOG_DEPTH:
5216 if (settings->ortho_fog)
5217 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
5218 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z * 0.5 + 0.5;\n");
5219 else if (settings->transformed)
5220 shader_addline(buffer, "gl_FogFragCoord = ec_pos.z;\n");
5221 else
5222 shader_addline(buffer, "gl_FogFragCoord = abs(ec_pos.z);\n");
5223 break;
5225 default:
5226 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
5227 break;
5230 if (settings->point_size)
5232 shader_addline(buffer, "gl_PointSize = gl_Point.size / sqrt(gl_Point.distanceConstantAttenuation"
5233 " + gl_Point.distanceLinearAttenuation * length(ec_pos.xyz)"
5234 " + gl_Point.distanceQuadraticAttenuation * dot(ec_pos.xyz, ec_pos.xyz));\n");
5235 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, gl_Point.sizeMin, gl_Point.sizeMax);\n");
5238 shader_addline(buffer, "}\n");
5240 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5241 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5243 return shader_obj;
5246 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_shader_buffer *buffer,
5247 DWORD argnum, unsigned int stage, DWORD arg)
5249 const char *ret;
5251 if (arg == ARG_UNUSED)
5252 return "<unused arg>";
5254 switch (arg & WINED3DTA_SELECTMASK)
5256 case WINED3DTA_DIFFUSE:
5257 ret = "gl_Color";
5258 break;
5260 case WINED3DTA_CURRENT:
5261 if (!stage)
5262 ret = "gl_Color";
5263 else
5264 ret = "ret";
5265 break;
5267 case WINED3DTA_TEXTURE:
5268 switch (stage)
5270 case 0: ret = "tex0"; break;
5271 case 1: ret = "tex1"; break;
5272 case 2: ret = "tex2"; break;
5273 case 3: ret = "tex3"; break;
5274 case 4: ret = "tex4"; break;
5275 case 5: ret = "tex5"; break;
5276 case 6: ret = "tex6"; break;
5277 case 7: ret = "tex7"; break;
5278 default:
5279 ret = "<invalid texture>";
5280 break;
5282 break;
5284 case WINED3DTA_TFACTOR:
5285 ret = "tex_factor";
5286 break;
5288 case WINED3DTA_SPECULAR:
5289 ret = "gl_SecondaryColor";
5290 break;
5292 case WINED3DTA_TEMP:
5293 ret = "temp_reg";
5294 break;
5296 case WINED3DTA_CONSTANT:
5297 switch (stage)
5299 case 0: ret = "tss_const0"; break;
5300 case 1: ret = "tss_const1"; break;
5301 case 2: ret = "tss_const2"; break;
5302 case 3: ret = "tss_const3"; break;
5303 case 4: ret = "tss_const4"; break;
5304 case 5: ret = "tss_const5"; break;
5305 case 6: ret = "tss_const6"; break;
5306 case 7: ret = "tss_const7"; break;
5307 default:
5308 ret = "<invalid constant>";
5309 break;
5311 break;
5313 default:
5314 return "<unhandled arg>";
5317 if (arg & WINED3DTA_COMPLEMENT)
5319 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
5320 if (argnum == 0)
5321 ret = "arg0";
5322 else if (argnum == 1)
5323 ret = "arg1";
5324 else if (argnum == 2)
5325 ret = "arg2";
5328 if (arg & WINED3DTA_ALPHAREPLICATE)
5330 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
5331 if (argnum == 0)
5332 ret = "arg0";
5333 else if (argnum == 1)
5334 ret = "arg1";
5335 else if (argnum == 2)
5336 ret = "arg2";
5339 return ret;
5342 static void shader_glsl_ffp_fragment_op(struct wined3d_shader_buffer *buffer, unsigned int stage, BOOL color,
5343 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
5345 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5347 if (color && alpha)
5348 dstmask = "";
5349 else if (color)
5350 dstmask = ".xyz";
5351 else
5352 dstmask = ".w";
5354 if (dst == tempreg)
5355 dstreg = "temp_reg";
5356 else
5357 dstreg = "ret";
5359 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
5360 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
5361 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
5363 switch (op)
5365 case WINED3D_TOP_DISABLE:
5366 if (!stage)
5367 shader_addline(buffer, "%s%s = gl_Color%s;\n", dstreg, dstmask, dstmask);
5368 break;
5370 case WINED3D_TOP_SELECT_ARG1:
5371 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
5372 break;
5374 case WINED3D_TOP_SELECT_ARG2:
5375 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
5376 break;
5378 case WINED3D_TOP_MODULATE:
5379 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5380 break;
5382 case WINED3D_TOP_MODULATE_4X:
5383 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
5384 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5385 break;
5387 case WINED3D_TOP_MODULATE_2X:
5388 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
5389 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5390 break;
5392 case WINED3D_TOP_ADD:
5393 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
5394 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5395 break;
5397 case WINED3D_TOP_ADD_SIGNED:
5398 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
5399 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5400 break;
5402 case WINED3D_TOP_ADD_SIGNED_2X:
5403 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
5404 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5405 break;
5407 case WINED3D_TOP_SUBTRACT:
5408 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
5409 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5410 break;
5412 case WINED3D_TOP_ADD_SMOOTH:
5413 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
5414 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
5415 break;
5417 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
5418 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5419 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5420 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5421 break;
5423 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5424 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5425 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5426 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5427 break;
5429 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5430 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
5431 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5432 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5433 break;
5435 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5436 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5437 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5438 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
5439 break;
5441 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
5442 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
5443 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5444 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5445 break;
5447 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
5448 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
5449 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5450 break;
5452 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
5453 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
5454 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5455 break;
5457 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
5458 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5459 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5460 break;
5461 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
5462 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
5463 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5464 break;
5466 case WINED3D_TOP_BUMPENVMAP:
5467 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5468 /* These are handled in the first pass, nothing to do. */
5469 break;
5471 case WINED3D_TOP_DOTPRODUCT3:
5472 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
5473 dstreg, dstmask, arg1, arg2, dstmask);
5474 break;
5476 case WINED3D_TOP_MULTIPLY_ADD:
5477 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
5478 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
5479 break;
5481 case WINED3D_TOP_LERP:
5482 /* MSDN isn't quite right here. */
5483 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
5484 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
5485 break;
5487 default:
5488 FIXME("Unhandled operation %#x.\n", op);
5489 break;
5493 /* Context activation is done by the caller. */
5494 static GLuint shader_glsl_generate_ffp_fragment_shader(struct wined3d_shader_buffer *buffer,
5495 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
5497 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
5498 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
5499 const char *final_combiner_src = "ret";
5500 UINT lowest_disabled_stage;
5501 GLuint shader_id;
5502 DWORD arg0, arg1, arg2;
5503 unsigned int stage;
5505 shader_buffer_clear(buffer);
5507 /* Find out which textures are read */
5508 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5510 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5511 break;
5513 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
5514 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
5515 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
5517 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
5518 || (stage == 0 && settings->color_key_enabled))
5519 tex_map |= 1 << stage;
5520 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5521 tfactor_used = TRUE;
5522 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5523 tempreg_used = TRUE;
5524 if (settings->op[stage].dst == tempreg)
5525 tempreg_used = TRUE;
5526 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5527 tss_const_map |= 1 << stage;
5529 switch (settings->op[stage].cop)
5531 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5532 lum_map |= 1 << stage;
5533 /* fall through */
5534 case WINED3D_TOP_BUMPENVMAP:
5535 bump_map |= 1 << stage;
5536 /* fall through */
5537 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5538 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5539 tex_map |= 1 << stage;
5540 break;
5542 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5543 tfactor_used = TRUE;
5544 break;
5546 default:
5547 break;
5550 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5551 continue;
5553 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
5554 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
5555 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
5557 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5558 tex_map |= 1 << stage;
5559 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5560 tfactor_used = TRUE;
5561 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5562 tempreg_used = TRUE;
5563 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5564 tss_const_map |= 1 << stage;
5566 lowest_disabled_stage = stage;
5568 shader_addline(buffer, "#version 120\n");
5570 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5571 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5573 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
5574 shader_addline(buffer, "vec4 ret;\n");
5575 if (tempreg_used || settings->sRGB_write)
5576 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
5577 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
5579 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5581 if (tss_const_map & (1 << stage))
5582 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
5584 if (!(tex_map & (1 << stage)))
5585 continue;
5587 switch (settings->op[stage].tex_type)
5589 case WINED3D_GL_RES_TYPE_TEX_1D:
5590 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
5591 break;
5592 case WINED3D_GL_RES_TYPE_TEX_2D:
5593 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
5594 break;
5595 case WINED3D_GL_RES_TYPE_TEX_3D:
5596 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
5597 break;
5598 case WINED3D_GL_RES_TYPE_TEX_CUBE:
5599 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
5600 break;
5601 case WINED3D_GL_RES_TYPE_TEX_RECT:
5602 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
5603 break;
5604 default:
5605 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
5606 break;
5609 shader_addline(buffer, "vec4 tex%u;\n", stage);
5611 if (!(bump_map & (1 << stage)))
5612 continue;
5613 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
5615 if (!(lum_map & (1 << stage)))
5616 continue;
5617 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
5618 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
5620 if (tfactor_used)
5621 shader_addline(buffer, "uniform vec4 tex_factor;\n");
5622 if (settings->color_key_enabled)
5623 shader_addline(buffer, "uniform vec4 color_key;\n");
5624 shader_addline(buffer, "uniform vec4 specular_enable;\n");
5626 if (settings->sRGB_write)
5628 shader_addline(buffer, "const vec4 srgb_const0 = ");
5629 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
5630 shader_addline(buffer, ";\n");
5631 shader_addline(buffer, "const vec4 srgb_const1 = ");
5632 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
5633 shader_addline(buffer, ";\n");
5636 shader_addline(buffer, "void main()\n{\n");
5638 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
5639 shader_addline(buffer, "if (any(lessThan(gl_TexCoord[7], vec4(0.0)))) discard;\n");
5641 /* Generate texture sampling instructions) */
5642 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
5644 const char *texture_function, *coord_mask;
5645 char tex_reg_name[8];
5646 BOOL proj;
5648 if (!(tex_map & (1 << stage)))
5649 continue;
5651 if (settings->op[stage].projected == proj_none)
5653 proj = FALSE;
5655 else if (settings->op[stage].projected == proj_count4
5656 || settings->op[stage].projected == proj_count3)
5658 proj = TRUE;
5660 else
5662 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
5663 proj = TRUE;
5666 switch (settings->op[stage].tex_type)
5668 case WINED3D_GL_RES_TYPE_TEX_1D:
5669 if (proj)
5671 texture_function = "texture1DProj";
5672 coord_mask = "xw";
5674 else
5676 texture_function = "texture1D";
5677 coord_mask = "x";
5679 break;
5680 case WINED3D_GL_RES_TYPE_TEX_2D:
5681 if (proj)
5683 texture_function = "texture2DProj";
5684 coord_mask = "xyw";
5686 else
5688 texture_function = "texture2D";
5689 coord_mask = "xy";
5691 break;
5692 case WINED3D_GL_RES_TYPE_TEX_3D:
5693 if (proj)
5695 texture_function = "texture3DProj";
5696 coord_mask = "xyzw";
5698 else
5700 texture_function = "texture3D";
5701 coord_mask = "xyz";
5703 break;
5704 case WINED3D_GL_RES_TYPE_TEX_CUBE:
5705 texture_function = "textureCube";
5706 coord_mask = "xyz";
5707 break;
5708 case WINED3D_GL_RES_TYPE_TEX_RECT:
5709 if (proj)
5711 texture_function = "texture2DRectProj";
5712 coord_mask = "xyw";
5714 else
5716 texture_function = "texture2DRect";
5717 coord_mask = "xy";
5719 break;
5720 default:
5721 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
5722 texture_function = "";
5723 coord_mask = "xyzw";
5724 break;
5727 if (stage > 0
5728 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
5729 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
5731 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
5733 /* With projective textures, texbem only divides the static
5734 * texture coord, not the displacement, so multiply the
5735 * displacement with the dividing parameter before passing it to
5736 * TXP. */
5737 if (settings->op[stage].projected != proj_none)
5739 if (settings->op[stage].projected == proj_count4)
5741 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].w) + gl_TexCoord[%u].xy;\n",
5742 stage, stage);
5743 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].ww;\n", stage);
5745 else
5747 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].z) + gl_TexCoord[%u].xy;\n",
5748 stage, stage);
5749 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].zz;\n", stage);
5752 else
5754 shader_addline(buffer, "ret = gl_TexCoord[%u] + ret.xyxy;\n", stage);
5757 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
5758 stage, texture_function, stage, coord_mask);
5760 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
5761 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
5762 stage, stage - 1, stage - 1, stage - 1);
5764 else if (settings->op[stage].projected == proj_count3)
5766 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].xyz);\n",
5767 stage, texture_function, stage, stage);
5769 else
5771 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].%s);\n",
5772 stage, texture_function, stage, stage, coord_mask);
5775 sprintf(tex_reg_name, "tex%u", stage);
5776 shader_glsl_color_correction_ext(buffer, tex_reg_name, WINED3DSP_WRITEMASK_ALL,
5777 settings->op[stage].color_fixup);
5780 if (settings->color_key_enabled)
5781 shader_addline(buffer, "if (all(equal(tex0, color_key))) discard;\n");
5783 /* Generate the main shader */
5784 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5786 BOOL op_equal;
5788 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5790 if (!stage)
5791 final_combiner_src = "gl_Color";
5792 break;
5795 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5796 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5797 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
5798 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5799 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5800 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
5801 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5802 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5803 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
5804 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5805 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5806 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
5807 else
5808 op_equal = settings->op[stage].aop == settings->op[stage].cop
5809 && settings->op[stage].carg0 == settings->op[stage].aarg0
5810 && settings->op[stage].carg1 == settings->op[stage].aarg1
5811 && settings->op[stage].carg2 == settings->op[stage].aarg2;
5813 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5815 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5816 settings->op[stage].cop, settings->op[stage].carg0,
5817 settings->op[stage].carg1, settings->op[stage].carg2);
5818 if (!stage)
5819 shader_addline(buffer, "ret.w = gl_Color.w;\n");
5821 else if (op_equal)
5823 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
5824 settings->op[stage].cop, settings->op[stage].carg0,
5825 settings->op[stage].carg1, settings->op[stage].carg2);
5827 else
5829 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5830 settings->op[stage].cop, settings->op[stage].carg0,
5831 settings->op[stage].carg1, settings->op[stage].carg2);
5832 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
5833 settings->op[stage].aop, settings->op[stage].aarg0,
5834 settings->op[stage].aarg1, settings->op[stage].aarg2);
5838 shader_addline(buffer, "gl_FragData[0] = gl_SecondaryColor * specular_enable + %s;\n", final_combiner_src);
5840 if (settings->sRGB_write)
5841 shader_glsl_generate_srgb_write_correction(buffer);
5843 shader_glsl_generate_fog_code(buffer, settings->fog);
5845 shader_addline(buffer, "}\n");
5847 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5848 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5849 return shader_id;
5852 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
5853 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
5855 struct glsl_ffp_vertex_shader *shader;
5856 const struct wine_rb_entry *entry;
5858 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
5859 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
5861 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
5862 return NULL;
5864 shader->desc.settings = *settings;
5865 shader->id = shader_glsl_generate_ffp_vertex_shader(&priv->shader_buffer, settings, gl_info);
5866 list_init(&shader->linked_programs);
5867 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
5868 ERR("Failed to insert ffp vertex shader.\n");
5870 return shader;
5873 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
5874 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
5876 struct glsl_ffp_fragment_shader *glsl_desc;
5877 const struct ffp_frag_desc *desc;
5879 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
5880 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
5882 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
5883 return NULL;
5885 glsl_desc->entry.settings = *args;
5886 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(&priv->shader_buffer, args, gl_info);
5887 list_init(&glsl_desc->linked_programs);
5888 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
5890 return glsl_desc;
5894 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
5895 GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
5897 unsigned int i;
5898 char name[32];
5900 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5901 sizeof(GLuint) * gl_info->limits.glsl_vs_float_constants);
5902 for (i = 0; i < vs_c_count; ++i)
5904 snprintf(name, sizeof(name), "vs_c[%u]", i);
5905 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5907 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
5908 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLuint));
5910 for (i = 0; i < MAX_CONST_I; ++i)
5912 snprintf(name, sizeof(name), "vs_i[%u]", i);
5913 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5916 for (i = 0; i < MAX_CONST_B; ++i)
5918 snprintf(name, sizeof(name), "vs_b[%u]", i);
5919 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5922 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "posFixup"));
5924 vs->modelview_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_modelview_matrix"));
5925 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
5926 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
5929 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
5930 GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
5932 unsigned int i;
5933 char name[32];
5935 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5936 sizeof(GLuint) * gl_info->limits.glsl_ps_float_constants);
5937 for (i = 0; i < ps_c_count; ++i)
5939 snprintf(name, sizeof(name), "ps_c[%u]", i);
5940 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5942 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
5943 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLuint));
5945 for (i = 0; i < MAX_CONST_I; ++i)
5947 snprintf(name, sizeof(name), "ps_i[%u]", i);
5948 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5951 for (i = 0; i < MAX_CONST_B; ++i)
5953 snprintf(name, sizeof(name), "ps_b[%u]", i);
5954 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5957 for (i = 0; i < MAX_TEXTURES; ++i)
5959 snprintf(name, sizeof(name), "bumpenv_mat%u", i);
5960 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5961 snprintf(name, sizeof(name), "bumpenv_lum_scale%u", i);
5962 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5963 snprintf(name, sizeof(name), "bumpenv_lum_offset%u", i);
5964 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5965 snprintf(name, sizeof(name), "tss_const%u", i);
5966 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5969 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
5970 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
5971 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
5972 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
5973 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
5976 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info, GLuint program_id,
5977 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
5979 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
5980 GLuint block_idx;
5981 unsigned int i;
5982 char name[16];
5984 for (i = 0; i < count; ++i)
5986 if (!reg_maps->cb_sizes[i])
5987 continue;
5989 snprintf(name, sizeof(name), "block_%s_cb%u", prefix, i);
5990 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name));
5991 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
5993 checkGLcall("glUniformBlockBinding");
5996 /* Context activation is done by the caller. */
5997 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
5998 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
6000 const struct wined3d_gl_info *gl_info = context->gl_info;
6001 const struct ps_np2fixup_info *np2fixup_info = NULL;
6002 struct glsl_shader_prog_link *entry = NULL;
6003 struct wined3d_shader *vshader = NULL;
6004 struct wined3d_shader *gshader = NULL;
6005 struct wined3d_shader *pshader = NULL;
6006 GLuint program_id = 0;
6007 GLuint reorder_shader_id = 0;
6008 unsigned int i;
6009 GLuint vs_id = 0;
6010 GLuint gs_id = 0;
6011 GLuint ps_id = 0;
6012 struct list *ps_list, *vs_list;
6014 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_VERTEX)))
6016 vs_id = ctx_data->glsl_program->vs.id;
6017 vs_list = &ctx_data->glsl_program->vs.shader_entry;
6019 if (use_vs(state))
6021 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
6022 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
6024 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_GEOMETRY))
6025 && ctx_data->glsl_program->gs.id)
6026 gs_id = ctx_data->glsl_program->gs.id;
6027 else if (gshader)
6028 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
6031 else if (use_vs(state))
6033 struct vs_compile_args vs_compile_args;
6034 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
6036 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args);
6037 vs_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
6038 vs_list = &vshader->linked_programs;
6040 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
6041 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
6043 else if (priv->vertex_pipe == &glsl_vertex_pipe)
6045 struct glsl_ffp_vertex_shader *ffp_shader;
6046 struct wined3d_ffp_vs_settings settings;
6048 wined3d_ffp_get_vs_settings(state, &context->stream_info, &settings);
6049 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
6050 vs_id = ffp_shader->id;
6051 vs_list = &ffp_shader->linked_programs;
6054 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_PIXEL)))
6056 ps_id = ctx_data->glsl_program->ps.id;
6057 ps_list = &ctx_data->glsl_program->ps.shader_entry;
6059 if (use_ps(state))
6060 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
6062 else if (use_ps(state))
6064 struct ps_compile_args ps_compile_args;
6065 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
6066 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, gl_info);
6067 ps_id = find_glsl_pshader(context, &priv->shader_buffer,
6068 pshader, &ps_compile_args, &np2fixup_info);
6069 ps_list = &pshader->linked_programs;
6071 else if (priv->fragment_pipe == &glsl_fragment_pipe)
6073 struct glsl_ffp_fragment_shader *ffp_shader;
6074 struct ffp_frag_settings settings;
6076 gen_ffp_frag_op(context, state, &settings, FALSE);
6077 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
6078 ps_id = ffp_shader->id;
6079 ps_list = &ffp_shader->linked_programs;
6082 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
6084 ctx_data->glsl_program = entry;
6085 return;
6088 /* If we get to this point, then no matching program exists, so we create one */
6089 program_id = GL_EXTCALL(glCreateProgram());
6090 TRACE("Created new GLSL shader program %u.\n", program_id);
6092 /* Create the entry */
6093 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
6094 entry->id = program_id;
6095 entry->vs.id = vs_id;
6096 entry->gs.id = gs_id;
6097 entry->ps.id = ps_id;
6098 entry->constant_version = 0;
6099 entry->ps.np2_fixup_info = np2fixup_info;
6100 /* Add the hash table entry */
6101 add_glsl_program_entry(priv, entry);
6103 /* Set the current program */
6104 ctx_data->glsl_program = entry;
6106 /* Attach GLSL vshader */
6107 if (vs_id)
6109 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
6110 GL_EXTCALL(glAttachShader(program_id, vs_id));
6111 checkGLcall("glAttachShader");
6113 list_add_head(vs_list, &entry->vs.shader_entry);
6116 if (vshader)
6118 WORD map = vshader->reg_maps.input_registers;
6119 char tmp_name[10];
6121 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
6122 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
6123 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
6124 checkGLcall("glAttachShader");
6125 /* Flag the reorder function for deletion, then it will be freed automatically when the program
6126 * is destroyed
6128 GL_EXTCALL(glDeleteShader(reorder_shader_id));
6130 /* Bind vertex attributes to a corresponding index number to match
6131 * the same index numbers as ARB_vertex_programs (makes loading
6132 * vertex attributes simpler). With this method, we can use the
6133 * exact same code to load the attributes later for both ARB and
6134 * GLSL shaders.
6136 * We have to do this here because we need to know the Program ID
6137 * in order to make the bindings work, and it has to be done prior
6138 * to linking the GLSL program. */
6139 for (i = 0; map; map >>= 1, ++i)
6141 if (!(map & 1)) continue;
6143 snprintf(tmp_name, sizeof(tmp_name), "vs_in%u", i);
6144 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name));
6146 checkGLcall("glBindAttribLocation");
6149 if (gshader)
6151 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
6152 GL_EXTCALL(glAttachShader(program_id, gs_id));
6153 checkGLcall("glAttachShader");
6155 TRACE("input type %s, output type %s, vertices out %u.\n",
6156 debug_d3dprimitivetype(gshader->u.gs.input_type),
6157 debug_d3dprimitivetype(gshader->u.gs.output_type),
6158 gshader->u.gs.vertices_out);
6159 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
6160 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
6161 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
6162 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
6163 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
6164 gshader->u.gs.vertices_out));
6165 checkGLcall("glProgramParameteriARB");
6167 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
6170 /* Attach GLSL pshader */
6171 if (ps_id)
6173 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
6174 GL_EXTCALL(glAttachShader(program_id, ps_id));
6175 checkGLcall("glAttachShader");
6177 list_add_head(ps_list, &entry->ps.shader_entry);
6180 /* Link the program */
6181 TRACE("Linking GLSL shader program %u.\n", program_id);
6182 GL_EXTCALL(glLinkProgram(program_id));
6183 shader_glsl_validate_link(gl_info, program_id);
6185 shader_glsl_init_vs_uniform_locations(gl_info, program_id, &entry->vs,
6186 vshader ? min(vshader->limits->constant_float, gl_info->limits.glsl_vs_float_constants) : 0);
6187 shader_glsl_init_ps_uniform_locations(gl_info, program_id, &entry->ps,
6188 pshader ? min(pshader->limits->constant_float, gl_info->limits.glsl_ps_float_constants) : 0);
6189 checkGLcall("Find glsl program uniform locations");
6191 if (pshader && pshader->reg_maps.shader_version.major >= 3
6192 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
6194 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
6195 entry->vs.vertex_color_clamp = GL_FALSE;
6197 else
6199 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
6202 /* Set the shader to allow uniform loading on it */
6203 GL_EXTCALL(glUseProgram(program_id));
6204 checkGLcall("glUseProgram");
6206 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
6207 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
6208 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
6209 * vertex shader with fixed function pixel processing is used we make sure that the card
6210 * supports enough samplers to allow the max number of vertex samplers with all possible
6211 * fixed function fragment processing setups. So once the program is linked these samplers
6212 * won't change. */
6213 shader_glsl_load_samplers(gl_info, context->tex_unit_map, program_id);
6215 entry->constant_update_mask = 0;
6216 if (vshader)
6218 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
6219 if (vshader->reg_maps.integer_constants)
6220 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
6221 if (vshader->reg_maps.boolean_constants)
6222 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
6223 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
6225 shader_glsl_init_uniform_block_bindings(gl_info, program_id, &vshader->reg_maps,
6226 0, gl_info->limits.vertex_uniform_blocks);
6228 else
6230 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
6231 | WINED3D_SHADER_CONST_FFP_PROJ;
6234 if (gshader)
6235 shader_glsl_init_uniform_block_bindings(gl_info, program_id, &gshader->reg_maps,
6236 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
6238 if (ps_id)
6240 if (pshader)
6242 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
6243 if (pshader->reg_maps.integer_constants)
6244 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
6245 if (pshader->reg_maps.boolean_constants)
6246 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
6247 if (entry->ps.ycorrection_location != -1)
6248 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
6250 shader_glsl_init_uniform_block_bindings(gl_info, program_id, &pshader->reg_maps,
6251 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
6252 gl_info->limits.fragment_uniform_blocks);
6254 else
6256 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
6259 for (i = 0; i < MAX_TEXTURES; ++i)
6261 if (entry->ps.bumpenv_mat_location[i] != -1)
6263 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
6264 break;
6268 if (entry->ps.np2_fixup_location != -1)
6269 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
6270 if (entry->ps.color_key_location != -1)
6271 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
6275 /* Context activation is done by the caller. */
6276 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum wined3d_gl_resource_type tex_type,
6277 BOOL masked)
6279 GLuint program_id;
6280 GLuint vshader_id, pshader_id;
6281 const char *blt_pshader;
6283 static const char blt_vshader[] =
6284 "#version 120\n"
6285 "void main(void)\n"
6286 "{\n"
6287 " gl_Position = gl_Vertex;\n"
6288 " gl_FrontColor = vec4(1.0);\n"
6289 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
6290 "}\n";
6292 static const char * const blt_pshaders_full[WINED3D_GL_RES_TYPE_COUNT] =
6294 /* WINED3D_GL_RES_TYPE_TEX_1D */
6295 NULL,
6296 /* WINED3D_GL_RES_TYPE_TEX_2D */
6297 "#version 120\n"
6298 "uniform sampler2D sampler;\n"
6299 "void main(void)\n"
6300 "{\n"
6301 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6302 "}\n",
6303 /* WINED3D_GL_RES_TYPE_TEX_3D */
6304 NULL,
6305 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
6306 "#version 120\n"
6307 "uniform samplerCube sampler;\n"
6308 "void main(void)\n"
6309 "{\n"
6310 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6311 "}\n",
6312 /* WINED3D_GL_RES_TYPE_TEX_RECT */
6313 "#version 120\n"
6314 "#extension GL_ARB_texture_rectangle : enable\n"
6315 "uniform sampler2DRect sampler;\n"
6316 "void main(void)\n"
6317 "{\n"
6318 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6319 "}\n",
6322 static const char * const blt_pshaders_masked[WINED3D_GL_RES_TYPE_COUNT] =
6324 /* WINED3D_GL_RES_TYPE_TEX_1D */
6325 NULL,
6326 /* WINED3D_GL_RES_TYPE_TEX_2D */
6327 "#version 120\n"
6328 "uniform sampler2D sampler;\n"
6329 "uniform vec4 mask;\n"
6330 "void main(void)\n"
6331 "{\n"
6332 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6333 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6334 "}\n",
6335 /* WINED3D_GL_RES_TYPE_TEX_3D */
6336 NULL,
6337 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
6338 "#version 120\n"
6339 "uniform samplerCube sampler;\n"
6340 "uniform vec4 mask;\n"
6341 "void main(void)\n"
6342 "{\n"
6343 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6344 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6345 "}\n",
6346 /* WINED3D_GL_RES_TYPE_TEX_RECT */
6347 "#version 120\n"
6348 "#extension GL_ARB_texture_rectangle : enable\n"
6349 "uniform sampler2DRect sampler;\n"
6350 "uniform vec4 mask;\n"
6351 "void main(void)\n"
6352 "{\n"
6353 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6354 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6355 "}\n",
6358 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
6359 if (!blt_pshader)
6361 FIXME("tex_type %#x not supported\n", tex_type);
6362 return 0;
6365 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6366 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
6368 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6369 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
6371 program_id = GL_EXTCALL(glCreateProgram());
6372 GL_EXTCALL(glAttachShader(program_id, vshader_id));
6373 GL_EXTCALL(glAttachShader(program_id, pshader_id));
6374 GL_EXTCALL(glLinkProgram(program_id));
6376 shader_glsl_validate_link(gl_info, program_id);
6378 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
6379 * is destroyed
6381 GL_EXTCALL(glDeleteShader(vshader_id));
6382 GL_EXTCALL(glDeleteShader(pshader_id));
6383 return program_id;
6386 /* Context activation is done by the caller. */
6387 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
6388 const struct wined3d_state *state)
6390 struct glsl_context_data *ctx_data = context->shader_backend_data;
6391 const struct wined3d_gl_info *gl_info = context->gl_info;
6392 struct shader_glsl_priv *priv = shader_priv;
6393 GLuint program_id = 0, prev_id = 0;
6394 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
6396 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
6397 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
6399 if (ctx_data->glsl_program)
6401 prev_id = ctx_data->glsl_program->id;
6402 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6404 else
6406 prev_id = 0;
6407 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6410 set_glsl_shader_program(context, state, priv, ctx_data);
6412 if (ctx_data->glsl_program)
6414 program_id = ctx_data->glsl_program->id;
6415 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6417 else
6419 program_id = 0;
6420 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6423 if (old_vertex_color_clamp != current_vertex_color_clamp)
6425 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6427 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
6428 checkGLcall("glClampColorARB");
6430 else
6432 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
6436 TRACE("Using GLSL program %u.\n", program_id);
6438 if (prev_id != program_id)
6440 GL_EXTCALL(glUseProgram(program_id));
6441 checkGLcall("glUseProgram");
6443 if (program_id)
6444 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
6448 /* "context" is not necessarily the currently active context. */
6449 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
6451 struct glsl_context_data *ctx_data = context->shader_backend_data;
6453 ctx_data->glsl_program = NULL;
6454 context->shader_update_mask = (1 << WINED3D_SHADER_TYPE_PIXEL)
6455 | (1 << WINED3D_SHADER_TYPE_VERTEX)
6456 | (1 << WINED3D_SHADER_TYPE_GEOMETRY);
6459 /* Context activation is done by the caller. */
6460 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
6462 const struct wined3d_gl_info *gl_info = context->gl_info;
6463 struct shader_glsl_priv *priv = shader_priv;
6465 shader_glsl_invalidate_current_program(context);
6466 GL_EXTCALL(glUseProgram(0));
6467 checkGLcall("glUseProgram");
6469 priv->vertex_pipe->vp_enable(gl_info, FALSE);
6470 priv->fragment_pipe->enable_extension(gl_info, FALSE);
6472 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6474 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
6475 checkGLcall("glClampColorARB");
6479 /* Context activation is done by the caller. */
6480 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
6481 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size)
6483 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
6484 struct shader_glsl_priv *priv = shader_priv;
6485 GLuint *blt_program;
6486 GLint loc;
6488 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
6489 if (!*blt_program)
6491 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
6492 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
6493 GL_EXTCALL(glUseProgram(*blt_program));
6494 GL_EXTCALL(glUniform1i(loc, 0));
6496 else
6498 GL_EXTCALL(glUseProgram(*blt_program));
6501 if (masked)
6503 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
6504 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
6508 /* Context activation is done by the caller. */
6509 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
6511 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
6512 GLuint program_id;
6514 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
6515 if (program_id) TRACE("Using GLSL program %u\n", program_id);
6517 GL_EXTCALL(glUseProgram(program_id));
6518 checkGLcall("glUseProgram");
6521 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
6522 const struct glsl_shader_prog_link *program)
6524 const struct glsl_context_data *ctx_data;
6525 struct wined3d_context *context;
6526 unsigned int i;
6528 for (i = 0; i < device->context_count; ++i)
6530 context = device->contexts[i];
6531 ctx_data = context->shader_backend_data;
6533 if (ctx_data->glsl_program == program)
6534 shader_glsl_invalidate_current_program(context);
6538 static void shader_glsl_destroy(struct wined3d_shader *shader)
6540 struct glsl_shader_private *shader_data = shader->backend_data;
6541 struct wined3d_device *device = shader->device;
6542 struct shader_glsl_priv *priv = device->shader_priv;
6543 const struct wined3d_gl_info *gl_info;
6544 const struct list *linked_programs;
6545 struct wined3d_context *context;
6547 if (!shader_data || !shader_data->num_gl_shaders)
6549 HeapFree(GetProcessHeap(), 0, shader_data);
6550 shader->backend_data = NULL;
6551 return;
6554 context = context_acquire(device, NULL);
6555 gl_info = context->gl_info;
6557 TRACE("Deleting linked programs.\n");
6558 linked_programs = &shader->linked_programs;
6559 if (linked_programs->next)
6561 struct glsl_shader_prog_link *entry, *entry2;
6562 UINT i;
6564 switch (shader->reg_maps.shader_version.type)
6566 case WINED3D_SHADER_TYPE_PIXEL:
6568 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
6570 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6572 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
6573 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
6574 checkGLcall("glDeleteShader");
6576 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
6578 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6579 struct glsl_shader_prog_link, ps.shader_entry)
6581 shader_glsl_invalidate_contexts_program(device, entry);
6582 delete_glsl_program_entry(priv, gl_info, entry);
6585 break;
6588 case WINED3D_SHADER_TYPE_VERTEX:
6590 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
6592 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6594 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
6595 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
6596 checkGLcall("glDeleteShader");
6598 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
6600 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6601 struct glsl_shader_prog_link, vs.shader_entry)
6603 shader_glsl_invalidate_contexts_program(device, entry);
6604 delete_glsl_program_entry(priv, gl_info, entry);
6607 break;
6610 case WINED3D_SHADER_TYPE_GEOMETRY:
6612 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
6614 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6616 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
6617 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
6618 checkGLcall("glDeleteShader");
6620 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
6622 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6623 struct glsl_shader_prog_link, gs.shader_entry)
6625 shader_glsl_invalidate_contexts_program(device, entry);
6626 delete_glsl_program_entry(priv, gl_info, entry);
6629 break;
6632 default:
6633 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
6634 break;
6638 HeapFree(GetProcessHeap(), 0, shader->backend_data);
6639 shader->backend_data = NULL;
6641 context_release(context);
6644 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
6646 const struct glsl_program_key *k = key;
6647 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
6648 const struct glsl_shader_prog_link, program_lookup_entry);
6650 if (k->vs_id > prog->vs.id) return 1;
6651 else if (k->vs_id < prog->vs.id) return -1;
6653 if (k->gs_id > prog->gs.id) return 1;
6654 else if (k->gs_id < prog->gs.id) return -1;
6656 if (k->ps_id > prog->ps.id) return 1;
6657 else if (k->ps_id < prog->ps.id) return -1;
6659 return 0;
6662 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
6664 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
6665 + constant_count * sizeof(*heap->contained)
6666 + constant_count * sizeof(*heap->positions);
6667 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
6669 if (!mem)
6671 ERR("Failed to allocate memory\n");
6672 return FALSE;
6675 heap->entries = mem;
6676 heap->entries[1].version = 0;
6677 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
6678 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
6679 heap->positions = (unsigned int *)(heap->contained + constant_count);
6680 heap->size = 1;
6682 return TRUE;
6685 static void constant_heap_free(struct constant_heap *heap)
6687 HeapFree(GetProcessHeap(), 0, heap->entries);
6690 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
6692 wined3d_rb_alloc,
6693 wined3d_rb_realloc,
6694 wined3d_rb_free,
6695 glsl_program_key_compare,
6698 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
6699 const struct fragment_pipeline *fragment_pipe)
6701 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6702 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
6703 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
6704 gl_info->limits.glsl_ps_float_constants)) + 1;
6705 struct fragment_caps fragment_caps;
6706 void *vertex_priv, *fragment_priv;
6708 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
6710 ERR("Failed to initialize vertex pipe.\n");
6711 HeapFree(GetProcessHeap(), 0, priv);
6712 return E_FAIL;
6715 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
6717 ERR("Failed to initialize fragment pipe.\n");
6718 vertex_pipe->vp_free(device);
6719 HeapFree(GetProcessHeap(), 0, priv);
6720 return E_FAIL;
6723 if (!shader_buffer_init(&priv->shader_buffer))
6725 ERR("Failed to initialize shader buffer.\n");
6726 goto fail;
6729 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
6730 if (!priv->stack)
6732 ERR("Failed to allocate memory.\n");
6733 goto fail;
6736 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
6738 ERR("Failed to initialize vertex shader constant heap\n");
6739 goto fail;
6742 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
6744 ERR("Failed to initialize pixel shader constant heap\n");
6745 goto fail;
6748 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
6750 ERR("Failed to initialize rbtree.\n");
6751 goto fail;
6754 priv->next_constant_version = 1;
6755 priv->vertex_pipe = vertex_pipe;
6756 priv->fragment_pipe = fragment_pipe;
6757 fragment_pipe->get_caps(gl_info, &fragment_caps);
6758 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
6760 device->vertex_priv = vertex_priv;
6761 device->fragment_priv = fragment_priv;
6762 device->shader_priv = priv;
6764 return WINED3D_OK;
6766 fail:
6767 constant_heap_free(&priv->pconst_heap);
6768 constant_heap_free(&priv->vconst_heap);
6769 HeapFree(GetProcessHeap(), 0, priv->stack);
6770 shader_buffer_free(&priv->shader_buffer);
6771 fragment_pipe->free_private(device);
6772 vertex_pipe->vp_free(device);
6773 HeapFree(GetProcessHeap(), 0, priv);
6774 return E_OUTOFMEMORY;
6777 /* Context activation is done by the caller. */
6778 static void shader_glsl_free(struct wined3d_device *device)
6780 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6781 struct shader_glsl_priv *priv = device->shader_priv;
6782 int i;
6784 for (i = 0; i < WINED3D_GL_RES_TYPE_COUNT; ++i)
6786 if (priv->depth_blt_program_full[i])
6788 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
6790 if (priv->depth_blt_program_masked[i])
6792 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
6796 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
6797 constant_heap_free(&priv->pconst_heap);
6798 constant_heap_free(&priv->vconst_heap);
6799 HeapFree(GetProcessHeap(), 0, priv->stack);
6800 shader_buffer_free(&priv->shader_buffer);
6801 priv->fragment_pipe->free_private(device);
6802 priv->vertex_pipe->vp_free(device);
6804 HeapFree(GetProcessHeap(), 0, device->shader_priv);
6805 device->shader_priv = NULL;
6808 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
6810 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
6811 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
6814 static void shader_glsl_free_context_data(struct wined3d_context *context)
6816 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
6819 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
6821 UINT shader_model;
6823 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
6824 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
6825 && gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] && gl_info->supported[ARB_DRAW_INSTANCED]
6826 && gl_info->supported[ARB_TEXTURE_RG] && gl_info->supported[ARB_SAMPLER_OBJECTS])
6827 shader_model = 4;
6828 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
6829 * texldd and texldl instructions. */
6830 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
6831 shader_model = 3;
6832 else
6833 shader_model = 2;
6834 TRACE("Shader model %u.\n", shader_model);
6836 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
6837 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
6838 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
6840 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
6841 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
6843 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
6844 * Direct3D minimum requirement.
6846 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
6847 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
6849 * The problem is that the refrast clamps temporary results in the shader to
6850 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
6851 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
6852 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
6853 * offer a way to query this.
6855 if (shader_model >= 4)
6856 caps->ps_1x_max_value = FLT_MAX;
6857 else
6858 caps->ps_1x_max_value = 1024.0f;
6860 /* Ideally we'd only set caps like sRGB writes here if supported by both
6861 * the shader backend and the fragment pipe, but we can get called before
6862 * shader_glsl_alloc(). */
6863 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
6864 | WINED3D_SHADER_CAP_SRGB_WRITE;
6867 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
6869 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
6871 TRACE("Checking support for fixup:\n");
6872 dump_color_fixup_desc(fixup);
6875 /* We support everything except YUV conversions. */
6876 if (!is_complex_fixup(fixup))
6878 TRACE("[OK]\n");
6879 return TRUE;
6882 TRACE("[FAILED]\n");
6883 return FALSE;
6886 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
6888 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
6889 /* WINED3DSIH_ADD */ shader_glsl_binop,
6890 /* WINED3DSIH_AND */ shader_glsl_binop,
6891 /* WINED3DSIH_BEM */ shader_glsl_bem,
6892 /* WINED3DSIH_BREAK */ shader_glsl_break,
6893 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
6894 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
6895 /* WINED3DSIH_CALL */ shader_glsl_call,
6896 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
6897 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
6898 /* WINED3DSIH_CND */ shader_glsl_cnd,
6899 /* WINED3DSIH_CRS */ shader_glsl_cross,
6900 /* WINED3DSIH_CUT */ shader_glsl_cut,
6901 /* WINED3DSIH_DCL */ shader_glsl_nop,
6902 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
6903 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
6904 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
6905 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
6906 /* WINED3DSIH_DEF */ shader_glsl_nop,
6907 /* WINED3DSIH_DEFB */ shader_glsl_nop,
6908 /* WINED3DSIH_DEFI */ shader_glsl_nop,
6909 /* WINED3DSIH_DIV */ shader_glsl_binop,
6910 /* WINED3DSIH_DP2 */ shader_glsl_dot,
6911 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
6912 /* WINED3DSIH_DP3 */ shader_glsl_dot,
6913 /* WINED3DSIH_DP4 */ shader_glsl_dot,
6914 /* WINED3DSIH_DST */ shader_glsl_dst,
6915 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
6916 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
6917 /* WINED3DSIH_ELSE */ shader_glsl_else,
6918 /* WINED3DSIH_EMIT */ shader_glsl_emit,
6919 /* WINED3DSIH_ENDIF */ shader_glsl_end,
6920 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
6921 /* WINED3DSIH_ENDREP */ shader_glsl_end,
6922 /* WINED3DSIH_EQ */ shader_glsl_relop,
6923 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
6924 /* WINED3DSIH_EXPP */ shader_glsl_expp,
6925 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
6926 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
6927 /* WINED3DSIH_GE */ shader_glsl_relop,
6928 /* WINED3DSIH_IADD */ shader_glsl_binop,
6929 /* WINED3DSIH_IEQ */ NULL,
6930 /* WINED3DSIH_IF */ shader_glsl_if,
6931 /* WINED3DSIH_IFC */ shader_glsl_ifc,
6932 /* WINED3DSIH_IGE */ shader_glsl_relop,
6933 /* WINED3DSIH_IMUL */ shader_glsl_imul,
6934 /* WINED3DSIH_ISHL */ shader_glsl_binop,
6935 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
6936 /* WINED3DSIH_LABEL */ shader_glsl_label,
6937 /* WINED3DSIH_LD */ NULL,
6938 /* WINED3DSIH_LIT */ shader_glsl_lit,
6939 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
6940 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
6941 /* WINED3DSIH_LOOP */ shader_glsl_loop,
6942 /* WINED3DSIH_LRP */ shader_glsl_lrp,
6943 /* WINED3DSIH_LT */ shader_glsl_relop,
6944 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
6945 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
6946 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
6947 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
6948 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
6949 /* WINED3DSIH_MAD */ shader_glsl_mad,
6950 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
6951 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
6952 /* WINED3DSIH_MOV */ shader_glsl_mov,
6953 /* WINED3DSIH_MOVA */ shader_glsl_mov,
6954 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
6955 /* WINED3DSIH_MUL */ shader_glsl_binop,
6956 /* WINED3DSIH_NE */ shader_glsl_relop,
6957 /* WINED3DSIH_NOP */ shader_glsl_nop,
6958 /* WINED3DSIH_NRM */ shader_glsl_nrm,
6959 /* WINED3DSIH_OR */ shader_glsl_binop,
6960 /* WINED3DSIH_PHASE */ shader_glsl_nop,
6961 /* WINED3DSIH_POW */ shader_glsl_pow,
6962 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
6963 /* WINED3DSIH_REP */ shader_glsl_rep,
6964 /* WINED3DSIH_RET */ shader_glsl_ret,
6965 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
6966 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
6967 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
6968 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
6969 /* WINED3DSIH_SAMPLE_LOD */ NULL,
6970 /* WINED3DSIH_SETP */ NULL,
6971 /* WINED3DSIH_SGE */ shader_glsl_compare,
6972 /* WINED3DSIH_SGN */ shader_glsl_sgn,
6973 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
6974 /* WINED3DSIH_SLT */ shader_glsl_compare,
6975 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
6976 /* WINED3DSIH_SUB */ shader_glsl_binop,
6977 /* WINED3DSIH_TEX */ shader_glsl_tex,
6978 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
6979 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
6980 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
6981 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
6982 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
6983 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
6984 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
6985 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
6986 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
6987 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
6988 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
6989 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
6990 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
6991 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
6992 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
6993 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
6994 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
6995 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
6996 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
6997 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
6998 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
6999 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
7000 /* WINED3DSIH_UGE */ shader_glsl_relop,
7001 /* WINED3DSIH_USHR */ shader_glsl_binop,
7002 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
7003 /* WINED3DSIH_XOR */ shader_glsl_binop,
7006 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
7007 SHADER_HANDLER hw_fct;
7009 /* Select handler */
7010 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
7012 /* Unhandled opcode */
7013 if (!hw_fct)
7015 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
7016 return;
7018 hw_fct(ins);
7020 shader_glsl_add_instruction_modifiers(ins);
7023 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
7025 struct shader_glsl_priv *priv = shader_priv;
7027 return priv->ffp_proj_control;
7030 const struct wined3d_shader_backend_ops glsl_shader_backend =
7032 shader_glsl_handle_instruction,
7033 shader_glsl_select,
7034 shader_glsl_disable,
7035 shader_glsl_select_depth_blt,
7036 shader_glsl_deselect_depth_blt,
7037 shader_glsl_update_float_vertex_constants,
7038 shader_glsl_update_float_pixel_constants,
7039 shader_glsl_load_constants,
7040 shader_glsl_destroy,
7041 shader_glsl_alloc,
7042 shader_glsl_free,
7043 shader_glsl_allocate_context_data,
7044 shader_glsl_free_context_data,
7045 shader_glsl_get_caps,
7046 shader_glsl_color_fixup_supported,
7047 shader_glsl_has_ffp_proj_control,
7050 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
7052 if (enable)
7053 gl_info->gl_ops.gl.p_glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
7054 else
7055 gl_info->gl_ops.gl.p_glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
7056 checkGLcall("GL_VERTEX_PROGRAM_POINT_SIZE_ARB");
7059 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
7061 caps->xyzrhw = TRUE;
7062 caps->max_active_lights = gl_info->limits.lights;
7063 caps->max_vertex_blend_matrices = 1;
7064 caps->max_vertex_blend_matrix_index = 0;
7065 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
7066 | WINED3DVTXPCAPS_MATERIALSOURCE7
7067 | WINED3DVTXPCAPS_VERTEXFOG
7068 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
7069 | WINED3DVTXPCAPS_POSITIONALLIGHTS
7070 | WINED3DVTXPCAPS_LOCALVIEWER
7071 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
7072 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
7073 caps->max_user_clip_planes = gl_info->limits.clipplanes;
7074 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
7077 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7079 struct shader_glsl_priv *priv;
7081 if (shader_backend == &glsl_shader_backend)
7083 priv = shader_priv;
7085 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
7087 ERR("Failed to initialize rbtree.\n");
7088 return NULL;
7091 return priv;
7094 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
7096 return NULL;
7099 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
7101 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7102 struct glsl_ffp_vertex_shader, desc.entry);
7103 struct glsl_shader_prog_link *program, *program2;
7104 struct glsl_ffp_destroy_ctx *ctx = context;
7106 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7107 struct glsl_shader_prog_link, vs.shader_entry)
7109 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7111 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
7112 HeapFree(GetProcessHeap(), 0, shader);
7115 /* Context activation is done by the caller. */
7116 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
7118 struct shader_glsl_priv *priv = device->vertex_priv;
7119 struct glsl_ffp_destroy_ctx ctx;
7121 ctx.priv = priv;
7122 ctx.gl_info = &device->adapter->gl_info;
7123 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
7126 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
7127 const struct wined3d_state *state, DWORD state_id)
7129 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7132 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
7133 const struct wined3d_state *state, DWORD state_id)
7135 const struct wined3d_gl_info *gl_info = context->gl_info;
7136 BOOL transformed = context->stream_info.position_transformed;
7137 BOOL wasrhw = context->last_was_rhw;
7138 unsigned int i;
7140 context->last_was_rhw = transformed;
7142 if (!use_vs(state))
7144 if (context->last_was_vshader)
7146 for (i = 0; i < gl_info->limits.clipplanes; ++i)
7147 clipplane(context, state, STATE_CLIPPLANE(i));
7150 if (transformed != wasrhw)
7151 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
7153 for (i = 0; i < MAX_TEXTURES; ++i)
7155 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_TEXTURE0 + i)))
7156 transform_texture(context, state, STATE_TEXTURESTAGE(i, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS));
7159 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_LIGHTING)))
7160 state_lighting(context, state, STATE_RENDER(WINED3D_RS_LIGHTING));
7161 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_NORMALIZENORMALS)))
7162 state_normalize(context, state, STATE_RENDER(WINED3D_RS_NORMALIZENORMALS));
7164 /* Because of settings->texcoords, we have to always regenerate the
7165 * vertex shader on a vdecl change.
7166 * TODO: Just always output all the texcoords when there are enough
7167 * varyings available to drop the dependency. */
7168 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7170 if (use_ps(state)
7171 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
7172 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
7173 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7175 else
7177 if (!context->last_was_vshader)
7179 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
7180 for (i = 0; i < gl_info->limits.clipplanes; ++i)
7181 clipplane(context, state, STATE_CLIPPLANE(i));
7185 context->last_was_vshader = use_vs(state);
7188 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
7189 const struct wined3d_state *state, DWORD state_id)
7191 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7192 /* Different vertex shaders potentially require a different vertex attributes setup. */
7193 if (!isStateDirty(context, STATE_VDECL))
7194 context_apply_state(context, state, STATE_VDECL);
7197 static void glsl_vertex_pipe_world(struct wined3d_context *context,
7198 const struct wined3d_state *state, DWORD state_id)
7200 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
7203 void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
7205 const struct wined3d_gl_info *gl_info = context->gl_info;
7206 const struct wined3d_light_info *light = NULL;
7207 unsigned int k;
7209 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
7211 for (k = 0; k < gl_info->limits.lights; ++k)
7213 if (!(light = state->lights[k]))
7214 continue;
7215 gl_info->gl_ops.gl.p_glLightfv(GL_LIGHT0 + light->glIndex, GL_POSITION, light->lightPosn);
7216 checkGLcall("glLightfv posn");
7217 gl_info->gl_ops.gl.p_glLightfv(GL_LIGHT0 + light->glIndex, GL_SPOT_DIRECTION, light->lightDirn);
7218 checkGLcall("glLightfv dirn");
7221 for (k = 0; k < gl_info->limits.clipplanes; ++k)
7223 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
7224 clipplane(context, state, STATE_CLIPPLANE(k));
7227 if (context->swapchain->device->vertexBlendUsed)
7229 static int warned;
7231 if (!warned++)
7232 FIXME("Vertex blending emulation.\n");
7236 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
7237 const struct wined3d_state *state, DWORD state_id)
7239 /* Table fog behavior depends on the projection matrix. */
7240 if (state->render_states[WINED3D_RS_FOGENABLE]
7241 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
7242 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7243 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
7246 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
7247 const struct wined3d_state *state, DWORD state_id)
7249 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
7250 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
7251 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
7252 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
7253 state_pscale(context, state, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE));
7254 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
7257 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
7259 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
7260 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
7261 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
7262 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), state_specularenable }, WINED3D_GL_EXT_NONE },
7263 /* Clip planes */
7264 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
7265 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
7266 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
7267 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
7268 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
7269 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
7270 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
7271 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
7272 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
7273 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
7274 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
7275 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
7276 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
7277 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
7278 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
7279 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
7280 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
7281 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
7282 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
7283 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
7284 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
7285 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
7286 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
7287 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
7288 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
7289 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
7290 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
7291 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
7292 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
7293 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
7294 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
7295 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
7296 /* Lights */
7297 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7298 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), light }, WINED3D_GL_EXT_NONE },
7299 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), light }, WINED3D_GL_EXT_NONE },
7300 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), light }, WINED3D_GL_EXT_NONE },
7301 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), light }, WINED3D_GL_EXT_NONE },
7302 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), light }, WINED3D_GL_EXT_NONE },
7303 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), light }, WINED3D_GL_EXT_NONE },
7304 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), light }, WINED3D_GL_EXT_NONE },
7305 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), light }, WINED3D_GL_EXT_NONE },
7306 /* Viewport */
7307 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
7308 /* Transform states */
7309 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
7310 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
7311 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7312 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7313 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7314 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7315 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7316 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7317 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7318 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7319 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
7320 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7321 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7322 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7323 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7324 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7325 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7326 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7327 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7328 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7329 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7330 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7331 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7332 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7333 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7334 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7335 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7336 /* Fog */
7337 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7338 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7339 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7340 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7341 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
7342 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
7343 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7344 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), state_ambient }, WINED3D_GL_EXT_NONE },
7345 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7346 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7347 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7348 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7349 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7350 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7351 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7352 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7353 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7354 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_arb }, ARB_POINT_PARAMETERS },
7355 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_ext }, EXT_POINT_PARAMETERS },
7356 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_w }, WINED3D_GL_EXT_NONE },
7357 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
7358 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
7359 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), state_pscale }, WINED3D_GL_EXT_NONE },
7360 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7361 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7362 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7363 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, ARB_POINT_PARAMETERS },
7364 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, EXT_POINT_PARAMETERS },
7365 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
7366 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7367 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7368 /* Samplers for NP2 texture matrix adjustions. They are not needed if
7369 * GL_ARB_texture_non_power_of_two is supported, so register a NULL state
7370 * handler in that case to get the vertex part of sampler() skipped (VTF
7371 * is handled in the misc states). Otherwise, register
7372 * sampler_texmatrix(), which takes care of updating the texture matrix. */
7373 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7374 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7375 {STATE_SAMPLER(0), {STATE_SAMPLER(0), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7376 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7377 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7378 {STATE_SAMPLER(1), {STATE_SAMPLER(1), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7379 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7380 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7381 {STATE_SAMPLER(2), {STATE_SAMPLER(2), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7382 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7383 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7384 {STATE_SAMPLER(3), {STATE_SAMPLER(3), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7385 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7386 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7387 {STATE_SAMPLER(4), {STATE_SAMPLER(4), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7388 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7389 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7390 {STATE_SAMPLER(5), {STATE_SAMPLER(5), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7391 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7392 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7393 {STATE_SAMPLER(6), {STATE_SAMPLER(6), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7394 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7395 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7396 {STATE_SAMPLER(7), {STATE_SAMPLER(7), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7397 {STATE_POINT_SIZE_ENABLE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7398 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
7401 /* TODO:
7402 * - This currently depends on GL fixed function functions to set things
7403 * like light parameters. Ideally we'd use regular uniforms for that.
7404 * - In part because of the previous point, much of this is modelled after
7405 * GL fixed function, and has much of the same limitations. For example,
7406 * D3D spot lights are slightly different from GL spot lights.
7407 * - We can now implement drawing transformed vertices using the GLSL pipe,
7408 * instead of using the immediate mode fallback.
7409 * - Similarly, we don't need the fallback for certain combinations of
7410 * material sources anymore.
7411 * - Implement vertex blending and vertex tweening.
7412 * - Handle WINED3D_TSS_TEXCOORD_INDEX in the shader, instead of duplicating
7413 * attribute arrays in load_tex_coords().
7414 * - Per-vertex point sizes. */
7415 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
7417 glsl_vertex_pipe_vp_enable,
7418 glsl_vertex_pipe_vp_get_caps,
7419 glsl_vertex_pipe_vp_alloc,
7420 glsl_vertex_pipe_vp_free,
7421 glsl_vertex_pipe_vp_states,
7424 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
7426 /* Nothing to do. */
7429 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
7431 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
7432 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
7433 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
7434 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
7435 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
7436 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
7437 | WINED3DTEXOPCAPS_SELECTARG1
7438 | WINED3DTEXOPCAPS_SELECTARG2
7439 | WINED3DTEXOPCAPS_MODULATE4X
7440 | WINED3DTEXOPCAPS_MODULATE2X
7441 | WINED3DTEXOPCAPS_MODULATE
7442 | WINED3DTEXOPCAPS_ADDSIGNED2X
7443 | WINED3DTEXOPCAPS_ADDSIGNED
7444 | WINED3DTEXOPCAPS_ADD
7445 | WINED3DTEXOPCAPS_SUBTRACT
7446 | WINED3DTEXOPCAPS_ADDSMOOTH
7447 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
7448 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
7449 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
7450 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
7451 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
7452 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
7453 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
7454 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
7455 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
7456 | WINED3DTEXOPCAPS_DOTPRODUCT3
7457 | WINED3DTEXOPCAPS_MULTIPLYADD
7458 | WINED3DTEXOPCAPS_LERP
7459 | WINED3DTEXOPCAPS_BUMPENVMAP
7460 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
7461 caps->MaxTextureBlendStages = 8;
7462 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
7465 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7467 struct shader_glsl_priv *priv;
7469 if (shader_backend == &glsl_shader_backend)
7471 priv = shader_priv;
7473 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
7475 ERR("Failed to initialize rbtree.\n");
7476 return NULL;
7479 return priv;
7482 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
7484 return NULL;
7487 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
7489 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7490 struct glsl_ffp_fragment_shader, entry.entry);
7491 struct glsl_shader_prog_link *program, *program2;
7492 struct glsl_ffp_destroy_ctx *ctx = context;
7494 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7495 struct glsl_shader_prog_link, ps.shader_entry)
7497 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7499 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
7500 HeapFree(GetProcessHeap(), 0, shader);
7503 /* Context activation is done by the caller. */
7504 static void glsl_fragment_pipe_free(struct wined3d_device *device)
7506 struct shader_glsl_priv *priv = device->fragment_priv;
7507 struct glsl_ffp_destroy_ctx ctx;
7509 ctx.priv = priv;
7510 ctx.gl_info = &device->adapter->gl_info;
7511 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
7514 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
7515 const struct wined3d_state *state, DWORD state_id)
7517 context->last_was_pshader = use_ps(state);
7519 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7522 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
7523 const struct wined3d_state *state, DWORD state_id)
7525 BOOL use_vshader = use_vs(state);
7526 enum fogsource new_source;
7527 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
7528 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
7530 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7532 if (!state->render_states[WINED3D_RS_FOGENABLE])
7533 return;
7535 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
7537 if (use_vshader)
7538 new_source = FOGSOURCE_VS;
7539 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
7540 new_source = FOGSOURCE_COORD;
7541 else
7542 new_source = FOGSOURCE_FFP;
7544 else
7546 new_source = FOGSOURCE_FFP;
7549 if (new_source != context->fog_source || fogstart == fogend)
7551 context->fog_source = new_source;
7552 state_fogstartend(context, state, STATE_RENDER(WINED3D_RS_FOGSTART));
7556 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
7557 const struct wined3d_state *state, DWORD state_id)
7559 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
7560 glsl_fragment_pipe_fog(context, state, state_id);
7563 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
7564 const struct wined3d_state *state, DWORD state_id)
7566 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7569 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
7570 const struct wined3d_state *state, DWORD state_id)
7572 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7575 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
7577 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
7578 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7579 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7580 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7581 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7582 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7583 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7584 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7585 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7586 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7587 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7588 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7589 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7590 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7591 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7592 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7593 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7594 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7595 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7596 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7597 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7598 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7599 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7600 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7601 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7602 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7603 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7604 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7605 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7606 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7607 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7608 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7609 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7610 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7611 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7612 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7613 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7614 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7615 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7616 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7617 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7618 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7619 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7620 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7621 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7622 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7623 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7624 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7625 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7626 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7627 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7628 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7629 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7630 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7631 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7632 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7633 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7634 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7635 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7636 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7637 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7638 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7639 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7640 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7641 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7642 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7643 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7644 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7645 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7646 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7647 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7648 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7649 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7650 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7651 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
7652 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
7653 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7654 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7655 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
7656 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
7657 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
7658 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7659 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
7660 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
7661 {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 },
7662 {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 },
7663 {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 },
7664 {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 },
7665 {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 },
7666 {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 },
7667 {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 },
7668 {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 },
7669 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7670 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7671 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7672 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7673 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7674 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7675 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7676 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7677 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7678 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
7681 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
7683 return TRUE;
7686 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
7690 const struct fragment_pipeline glsl_fragment_pipe =
7692 glsl_fragment_pipe_enable,
7693 glsl_fragment_pipe_get_caps,
7694 glsl_fragment_pipe_alloc,
7695 glsl_fragment_pipe_free,
7696 glsl_fragment_pipe_alloc_context_data,
7697 glsl_fragment_pipe_free_context_data,
7698 shader_glsl_color_fixup_supported,
7699 glsl_fragment_pipe_state_template,