wined3d: Implement d3d10 style instance data step rates.
[wine/multimedia.git] / dlls / wined3d / glsl_shader.c
blob0b307d38f4a01a8c297f31dcceb6fd207fc45e68
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[tex_type_count];
100 GLuint depth_blt_program_masked[tex_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;
123 struct glsl_gs_program
125 struct list shader_entry;
126 GLuint id;
129 struct glsl_ps_program
131 struct list shader_entry;
132 GLuint id;
133 GLint *uniform_f_locations;
134 GLint uniform_i_locations[MAX_CONST_I];
135 GLint uniform_b_locations[MAX_CONST_B];
136 GLint bumpenv_mat_location[MAX_TEXTURES];
137 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
138 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
139 GLint tss_constant_location[MAX_TEXTURES];
140 GLint tex_factor_location;
141 GLint specular_enable_location;
142 GLint ycorrection_location;
143 GLint np2_fixup_location;
144 const struct ps_np2fixup_info *np2_fixup_info;
147 /* Struct to maintain data about a linked GLSL program */
148 struct glsl_shader_prog_link
150 struct wine_rb_entry program_lookup_entry;
151 struct glsl_vs_program vs;
152 struct glsl_gs_program gs;
153 struct glsl_ps_program ps;
154 GLuint id;
155 DWORD constant_update_mask;
156 UINT constant_version;
159 struct glsl_program_key
161 GLuint vs_id;
162 GLuint gs_id;
163 GLuint ps_id;
166 struct shader_glsl_ctx_priv {
167 const struct vs_compile_args *cur_vs_args;
168 const struct ps_compile_args *cur_ps_args;
169 struct ps_np2fixup_info *cur_np2fixup_info;
172 struct glsl_context_data
174 struct glsl_shader_prog_link *glsl_program;
177 struct glsl_ps_compiled_shader
179 struct ps_compile_args args;
180 struct ps_np2fixup_info np2fixup;
181 GLuint id;
184 struct glsl_vs_compiled_shader
186 struct vs_compile_args args;
187 GLuint id;
190 struct glsl_gs_compiled_shader
192 GLuint id;
195 struct glsl_shader_private
197 union
199 struct glsl_vs_compiled_shader *vs;
200 struct glsl_gs_compiled_shader *gs;
201 struct glsl_ps_compiled_shader *ps;
202 } gl_shaders;
203 UINT num_gl_shaders, shader_array_size;
206 struct glsl_ffp_vertex_shader
208 struct wined3d_ffp_vs_desc desc;
209 GLuint id;
210 struct list linked_programs;
213 struct glsl_ffp_fragment_shader
215 struct ffp_frag_desc entry;
216 GLuint id;
217 struct list linked_programs;
220 struct glsl_ffp_destroy_ctx
222 struct shader_glsl_priv *priv;
223 const struct wined3d_gl_info *gl_info;
226 static const char *debug_gl_shader_type(GLenum type)
228 switch (type)
230 #define WINED3D_TO_STR(u) case u: return #u
231 WINED3D_TO_STR(GL_VERTEX_SHADER);
232 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
233 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
234 #undef WINED3D_TO_STR
235 default:
236 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
240 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
242 switch (type)
244 case WINED3D_SHADER_TYPE_VERTEX:
245 return "vs";
247 case WINED3D_SHADER_TYPE_GEOMETRY:
248 return "gs";
250 case WINED3D_SHADER_TYPE_PIXEL:
251 return "ps";
253 default:
254 FIXME("Unhandled shader type %#x.\n", type);
255 return "unknown";
259 static void shader_glsl_append_imm_vec4(struct wined3d_shader_buffer *buffer, const float *values)
261 char str[4][17];
263 wined3d_ftoa(values[0], str[0]);
264 wined3d_ftoa(values[1], str[1]);
265 wined3d_ftoa(values[2], str[2]);
266 wined3d_ftoa(values[3], str[3]);
267 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
270 /* Extract a line from the info log.
271 * Note that this modifies the source string. */
272 static char *get_info_log_line(char **ptr)
274 char *p, *q;
276 p = *ptr;
277 if (!(q = strstr(p, "\n")))
279 if (!*p) return NULL;
280 *ptr += strlen(p);
281 return p;
283 *q = '\0';
284 *ptr = q + 1;
286 return p;
289 /* Context activation is done by the caller. */
290 static void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
292 int length = 0;
293 char *log;
295 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
296 return;
298 if (program)
299 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
300 else
301 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
303 /* A size of 1 is just a null-terminated string, so the log should be bigger than
304 * that if there are errors. */
305 if (length > 1)
307 char *ptr, *line;
309 log = HeapAlloc(GetProcessHeap(), 0, length);
310 /* The info log is supposed to be zero-terminated, but at least some
311 * versions of fglrx don't terminate the string properly. The reported
312 * length does include the terminator, so explicitly set it to zero
313 * here. */
314 log[length - 1] = 0;
315 if (program)
316 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
317 else
318 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
320 ptr = log;
321 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
323 WARN("Info log received from GLSL shader #%u:\n", id);
324 while ((line = get_info_log_line(&ptr))) WARN(" %s\n", line);
326 else
328 FIXME("Info log received from GLSL shader #%u:\n", id);
329 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
331 HeapFree(GetProcessHeap(), 0, log);
335 /* Context activation is done by the caller. */
336 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
338 TRACE("Compiling shader object %u.\n", shader);
339 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
340 checkGLcall("glShaderSource");
341 GL_EXTCALL(glCompileShader(shader));
342 checkGLcall("glCompileShader");
343 print_glsl_info_log(gl_info, shader, FALSE);
346 /* Context activation is done by the caller. */
347 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
349 GLint i, shader_count, source_size = -1;
350 GLuint *shaders;
351 char *source = NULL;
353 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
354 shaders = HeapAlloc(GetProcessHeap(), 0, shader_count * sizeof(*shaders));
355 if (!shaders)
357 ERR("Failed to allocate shader array memory.\n");
358 return;
361 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
362 for (i = 0; i < shader_count; ++i)
364 char *ptr, *line;
365 GLint tmp;
367 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
369 if (source_size < tmp)
371 HeapFree(GetProcessHeap(), 0, source);
373 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
374 if (!source)
376 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
377 HeapFree(GetProcessHeap(), 0, shaders);
378 return;
380 source_size = tmp;
383 FIXME("Shader %u:\n", shaders[i]);
384 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
385 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
386 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
387 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
388 FIXME("\n");
390 ptr = source;
391 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
392 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
393 FIXME("\n");
396 HeapFree(GetProcessHeap(), 0, source);
397 HeapFree(GetProcessHeap(), 0, shaders);
400 /* Context activation is done by the caller. */
401 static void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
403 GLint tmp;
405 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
406 return;
408 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
409 if (!tmp)
411 FIXME("Program %u link status invalid.\n", program);
412 shader_glsl_dump_program_source(gl_info, program);
415 print_glsl_info_log(gl_info, program, TRUE);
418 /* Context activation is done by the caller. */
419 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
420 const DWORD *tex_unit_map, GLuint program_id)
422 unsigned int mapped_unit;
423 char sampler_name[20];
424 const char *prefix;
425 unsigned int i, j;
426 GLint name_loc;
428 static const struct
430 enum wined3d_shader_type type;
431 unsigned int base_idx;
432 unsigned int count;
434 sampler_info[] =
436 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
437 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
440 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
442 prefix = shader_glsl_get_prefix(sampler_info[i].type);
444 for (j = 0; j < sampler_info[i].count; ++j)
446 snprintf(sampler_name, sizeof(sampler_name), "%s_sampler%u", prefix, j);
447 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name));
448 if (name_loc == -1)
449 continue;
451 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
452 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
454 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name, mapped_unit);
455 continue;
458 TRACE("Loading sampler %s on unit %u.\n", sampler_name, mapped_unit);
459 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
462 checkGLcall("glUniform1i");
465 /* Context activation is done by the caller. */
466 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
467 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
469 unsigned int start = ~0U, end = 0;
470 int stack_idx = 0;
471 unsigned int heap_idx = 1;
472 unsigned int idx;
474 if (heap->entries[heap_idx].version <= version) return;
476 idx = heap->entries[heap_idx].idx;
477 if (constant_locations[idx] != -1)
478 start = end = idx;
479 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
481 while (stack_idx >= 0)
483 /* Note that we fall through to the next case statement. */
484 switch(stack[stack_idx])
486 case HEAP_NODE_TRAVERSE_LEFT:
488 unsigned int left_idx = heap_idx << 1;
489 if (left_idx < heap->size && heap->entries[left_idx].version > version)
491 heap_idx = left_idx;
492 idx = heap->entries[heap_idx].idx;
493 if (constant_locations[idx] != -1)
495 if (start > idx)
496 start = idx;
497 if (end < idx)
498 end = idx;
501 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
502 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
503 break;
507 case HEAP_NODE_TRAVERSE_RIGHT:
509 unsigned int right_idx = (heap_idx << 1) + 1;
510 if (right_idx < heap->size && heap->entries[right_idx].version > version)
512 heap_idx = right_idx;
513 idx = heap->entries[heap_idx].idx;
514 if (constant_locations[idx] != -1)
516 if (start > idx)
517 start = idx;
518 if (end < idx)
519 end = idx;
522 stack[stack_idx++] = HEAP_NODE_POP;
523 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
524 break;
528 case HEAP_NODE_POP:
529 heap_idx >>= 1;
530 --stack_idx;
531 break;
534 if (start <= end)
535 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start * 4]));
536 checkGLcall("walk_constant_heap()");
539 /* Context activation is done by the caller. */
540 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
542 GLfloat clamped_constant[4];
544 if (location == -1) return;
546 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
547 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
548 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
549 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
551 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
554 /* Context activation is done by the caller. */
555 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
556 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
558 int stack_idx = 0;
559 unsigned int heap_idx = 1;
560 unsigned int idx;
562 if (heap->entries[heap_idx].version <= version) return;
564 idx = heap->entries[heap_idx].idx;
565 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
566 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
568 while (stack_idx >= 0)
570 /* Note that we fall through to the next case statement. */
571 switch(stack[stack_idx])
573 case HEAP_NODE_TRAVERSE_LEFT:
575 unsigned int left_idx = heap_idx << 1;
576 if (left_idx < heap->size && heap->entries[left_idx].version > version)
578 heap_idx = left_idx;
579 idx = heap->entries[heap_idx].idx;
580 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
582 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
583 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
584 break;
588 case HEAP_NODE_TRAVERSE_RIGHT:
590 unsigned int right_idx = (heap_idx << 1) + 1;
591 if (right_idx < heap->size && heap->entries[right_idx].version > version)
593 heap_idx = right_idx;
594 idx = heap->entries[heap_idx].idx;
595 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
597 stack[stack_idx++] = HEAP_NODE_POP;
598 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
599 break;
603 case HEAP_NODE_POP:
604 heap_idx >>= 1;
605 --stack_idx;
606 break;
609 checkGLcall("walk_constant_heap_clamped()");
612 /* Context activation is done by the caller. */
613 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
614 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
615 unsigned char *stack, UINT version)
617 const struct wined3d_shader_lconst *lconst;
619 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
620 if (shader->reg_maps.shader_version.major == 1
621 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
622 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
623 else
624 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
626 if (!shader->load_local_constsF)
628 TRACE("No need to load local float constants for this shader\n");
629 return;
632 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
633 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
635 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
637 checkGLcall("glUniform4fv()");
640 /* Context activation is done by the caller. */
641 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
642 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
644 unsigned int i;
645 struct list* ptr;
647 for (i = 0; constants_set; constants_set >>= 1, ++i)
649 if (!(constants_set & 1)) continue;
651 /* We found this uniform name in the program - go ahead and send the data */
652 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i * 4]));
655 /* Load immediate constants */
656 ptr = list_head(&shader->constantsI);
657 while (ptr)
659 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
660 unsigned int idx = lconst->idx;
661 const GLint *values = (const GLint *)lconst->value;
663 /* We found this uniform name in the program - go ahead and send the data */
664 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
665 ptr = list_next(&shader->constantsI, ptr);
667 checkGLcall("glUniform4iv()");
670 /* Context activation is done by the caller. */
671 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
672 const GLint locations[MAX_CONST_B], const BOOL *constants, WORD constants_set)
674 unsigned int i;
675 struct list* ptr;
677 for (i = 0; constants_set; constants_set >>= 1, ++i)
679 if (!(constants_set & 1)) continue;
681 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
684 /* Load immediate constants */
685 ptr = list_head(&shader->constantsB);
686 while (ptr)
688 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
689 unsigned int idx = lconst->idx;
690 const GLint *values = (const GLint *)lconst->value;
692 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
693 ptr = list_next(&shader->constantsB, ptr);
695 checkGLcall("glUniform1iv()");
698 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
700 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
703 /* Context activation is done by the caller (state handler). */
704 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
705 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
707 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
708 UINT fixup = ps->np2_fixup_info->active;
709 UINT i;
711 for (i = 0; fixup; fixup >>= 1, ++i)
713 const struct wined3d_texture *tex = state->textures[i];
714 unsigned char idx = ps->np2_fixup_info->idx[i];
715 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
717 if (!tex)
719 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
720 continue;
723 if (idx % 2)
725 tex_dim[2] = tex->pow2_matrix[0];
726 tex_dim[3] = tex->pow2_matrix[5];
728 else
730 tex_dim[0] = tex->pow2_matrix[0];
731 tex_dim[1] = tex->pow2_matrix[5];
735 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
738 /* Context activation is done by the caller (state handler). */
739 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
740 const struct wined3d_state *state)
742 const struct glsl_context_data *ctx_data = context->shader_backend_data;
743 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
744 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
745 const struct wined3d_gl_info *gl_info = context->gl_info;
746 struct shader_glsl_priv *priv = shader_priv;
747 float position_fixup[4];
748 DWORD update_mask = 0;
750 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
751 UINT constant_version;
752 int i;
754 if (!prog) {
755 /* No GLSL program set - nothing to do. */
756 return;
758 constant_version = prog->constant_version;
759 update_mask = context->constant_update_mask & prog->constant_update_mask;
761 if (update_mask & WINED3D_SHADER_CONST_VS_F)
762 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
763 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
765 if (update_mask & WINED3D_SHADER_CONST_VS_I)
766 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
767 vshader->reg_maps.integer_constants);
769 if (update_mask & WINED3D_SHADER_CONST_VS_B)
770 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
771 vshader->reg_maps.boolean_constants);
773 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
775 shader_get_position_fixup(context, state, position_fixup);
776 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
777 checkGLcall("glUniform4fv");
780 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
782 struct wined3d_matrix mat;
784 get_modelview_matrix(context, state, &mat);
785 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location, 1, FALSE, &mat._11));
786 checkGLcall("glUniformMatrix4fv");
789 if (update_mask & WINED3D_SHADER_CONST_PS_F)
790 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
791 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
793 if (update_mask & WINED3D_SHADER_CONST_PS_I)
794 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
795 pshader->reg_maps.integer_constants);
797 if (update_mask & WINED3D_SHADER_CONST_PS_B)
798 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
799 pshader->reg_maps.boolean_constants);
801 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
803 for (i = 0; i < MAX_TEXTURES; ++i)
805 if (prog->ps.bumpenv_mat_location[i] == -1)
806 continue;
808 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
809 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
811 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
813 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
814 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
815 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
816 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
820 checkGLcall("bump env uniforms");
823 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
825 float correction_params[4];
827 if (context->render_offscreen)
829 correction_params[0] = 0.0f;
830 correction_params[1] = 1.0f;
831 } else {
832 /* position is window relative, not viewport relative */
833 correction_params[0] = (float) context->current_rt->resource.height;
834 correction_params[1] = -1.0f;
836 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, correction_params));
839 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
840 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
842 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
844 float col[4];
846 if (prog->ps.tex_factor_location != -1)
848 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_TEXTUREFACTOR], col);
849 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, col));
852 if (state->render_states[WINED3D_RS_SPECULARENABLE])
853 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
854 else
855 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
857 for (i = 0; i < MAX_TEXTURES; ++i)
859 if (prog->ps.tss_constant_location[i] == -1)
860 continue;
862 D3DCOLORTOGLFLOAT4(state->texture_states[i][WINED3D_TSS_CONSTANT], col);
863 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, col));
866 checkGLcall("fixed function uniforms");
869 if (priv->next_constant_version == UINT_MAX)
871 TRACE("Max constant version reached, resetting to 0.\n");
872 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
873 priv->next_constant_version = 1;
875 else
877 prog->constant_version = priv->next_constant_version++;
881 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
883 struct constant_entry *entries = heap->entries;
884 unsigned int *positions = heap->positions;
885 unsigned int heap_idx, parent_idx;
887 if (!heap->contained[idx])
889 heap_idx = heap->size++;
890 heap->contained[idx] = TRUE;
892 else
894 heap_idx = positions[idx];
897 while (heap_idx > 1)
899 parent_idx = heap_idx >> 1;
901 if (new_version <= entries[parent_idx].version) break;
903 entries[heap_idx] = entries[parent_idx];
904 positions[entries[parent_idx].idx] = heap_idx;
905 heap_idx = parent_idx;
908 entries[heap_idx].version = new_version;
909 entries[heap_idx].idx = idx;
910 positions[idx] = heap_idx;
913 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
915 struct shader_glsl_priv *priv = device->shader_priv;
916 struct constant_heap *heap = &priv->vconst_heap;
917 UINT i;
919 for (i = start; i < count + start; ++i)
921 update_heap_entry(heap, i, priv->next_constant_version);
924 for (i = 0; i < device->context_count; ++i)
926 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
930 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
932 struct shader_glsl_priv *priv = device->shader_priv;
933 struct constant_heap *heap = &priv->pconst_heap;
934 UINT i;
936 for (i = start; i < count + start; ++i)
938 update_heap_entry(heap, i, priv->next_constant_version);
941 for (i = 0; i < device->context_count; ++i)
943 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
947 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
949 unsigned int ret = gl_info->limits.glsl_varyings / 4;
950 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
951 if(shader_major > 3) return ret;
953 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
954 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
955 return ret;
958 /** Generate the variable & register declarations for the GLSL output target */
959 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
960 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
961 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
963 const struct wined3d_shader_version *version = &reg_maps->shader_version;
964 const struct wined3d_state *state = &shader->device->state;
965 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
966 const struct wined3d_gl_info *gl_info = context->gl_info;
967 const struct wined3d_fb_state *fb = &shader->device->fb;
968 unsigned int i, extra_constants_needed = 0;
969 const struct wined3d_shader_lconst *lconst;
970 const char *prefix;
971 DWORD map;
973 prefix = shader_glsl_get_prefix(version->type);
975 /* Prototype the subroutines */
976 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
978 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
981 /* Declare the constants (aka uniforms) */
982 if (shader->limits->constant_float > 0)
984 unsigned max_constantsF;
986 /* Unless the shader uses indirect addressing, always declare the
987 * maximum array size and ignore that we need some uniforms privately.
988 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
989 * and immediate values, still declare VC[256]. If the shader needs
990 * more uniforms than we have it won't work in any case. If it uses
991 * less, the compiler will figure out which uniforms are really used
992 * and strip them out. This allows a shader to use c255 on a dx9 card,
993 * as long as it doesn't also use all the other constants.
995 * If the shader uses indirect addressing the compiler must assume
996 * that all declared uniforms are used. In this case, declare only the
997 * amount that we're assured to have.
999 * Thus we run into problems in these two cases:
1000 * 1) The shader really uses more uniforms than supported.
1001 * 2) The shader uses indirect addressing, less constants than
1002 * supported, but uses a constant index > #supported consts. */
1003 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1005 /* No indirect addressing here. */
1006 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1008 else
1010 if (reg_maps->usesrelconstF)
1012 /* Subtract the other potential uniforms from the max
1013 * available (bools, ints, and 1 row of projection matrix).
1014 * Subtract another uniform for immediate values, which have
1015 * to be loaded via uniform by the driver as well. The shader
1016 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1017 * shader code, so one vec4 should be enough. (Unfortunately
1018 * the Nvidia driver doesn't store 128 and -128 in one float).
1020 * Writing gl_ClipVertex requires one uniform for each
1021 * clipplane as well. */
1022 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1023 if(ctx_priv->cur_vs_args->clip_enabled)
1025 max_constantsF -= gl_info->limits.clipplanes;
1027 max_constantsF -= count_bits(reg_maps->integer_constants);
1028 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1029 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1030 * for now take this into account when calculating the number of available constants
1032 max_constantsF -= count_bits(reg_maps->boolean_constants);
1033 /* Set by driver quirks in directx.c */
1034 max_constantsF -= gl_info->reserved_glsl_constants;
1036 if (max_constantsF < shader->limits->constant_float)
1038 static unsigned int once;
1040 if (!once++)
1041 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1042 " it may not render correctly.\n");
1043 else
1044 WARN("The hardware does not support enough uniform components to run this shader.\n");
1047 else
1049 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1052 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1053 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1056 /* Always declare the full set of constants, the compiler can remove the
1057 * unused ones because d3d doesn't (yet) support indirect int and bool
1058 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1059 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1060 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1062 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1063 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1065 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1067 if (reg_maps->cb_sizes[i])
1068 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1069 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1072 /* Declare texture samplers */
1073 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1075 struct wined3d_shader_sampler_map_entry *entry;
1076 BOOL shadow_sampler, tex_rect;
1077 const char *sampler_type;
1079 entry = &reg_maps->sampler_map.entries[i];
1081 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1083 ERR("Invalid resource index %u.\n", entry->resource_idx);
1084 continue;
1087 shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1 << entry->sampler_idx));
1088 switch (reg_maps->resource_info[entry->resource_idx].type)
1090 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1091 if (shadow_sampler)
1092 sampler_type = "sampler1DShadow";
1093 else
1094 sampler_type = "sampler1D";
1095 break;
1097 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1098 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1099 && (ps_args->np2_fixup & (1 << entry->resource_idx))
1100 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1101 if (shadow_sampler)
1103 if (tex_rect)
1104 sampler_type = "sampler2DRectShadow";
1105 else
1106 sampler_type = "sampler2DShadow";
1108 else
1110 if (tex_rect)
1111 sampler_type = "sampler2DRect";
1112 else
1113 sampler_type = "sampler2D";
1115 break;
1117 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1118 if (shadow_sampler)
1119 FIXME("Unsupported 3D shadow sampler.\n");
1120 sampler_type = "sampler3D";
1121 break;
1123 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1124 if (shadow_sampler)
1125 FIXME("Unsupported Cube shadow sampler.\n");
1126 sampler_type = "samplerCube";
1127 break;
1129 default:
1130 sampler_type = "unsupported_sampler";
1131 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[i].type);
1132 break;
1134 shader_addline(buffer, "uniform %s %s_sampler%u;\n", sampler_type, prefix, entry->bind_idx);
1137 /* Declare uniforms for NP2 texcoord fixup:
1138 * This is NOT done inside the loop that declares the texture samplers
1139 * since the NP2 fixup code is currently only used for the GeforceFX
1140 * series and when forcing the ARB_npot extension off. Modern cards just
1141 * skip the code anyway, so put it inside a separate loop. */
1142 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1144 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1145 UINT cur = 0;
1147 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1148 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1149 * samplerNP2Fixup stores texture dimensions and is updated through
1150 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1152 for (i = 0; i < shader->limits->sampler; ++i)
1154 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1 << i)))
1155 continue;
1157 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1159 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1160 continue;
1163 fixup->idx[i] = cur++;
1166 fixup->num_consts = (cur + 1) >> 1;
1167 fixup->active = ps_args->np2_fixup;
1168 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1171 /* Declare address variables */
1172 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1174 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1177 /* Declare texture coordinate temporaries and initialize them */
1178 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1180 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1183 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1185 /* Declare attributes. */
1186 for (i = 0, map = reg_maps->input_registers; map; map >>= 1, ++i)
1188 if (map & 1)
1189 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, i);
1192 shader_addline(buffer, "uniform vec4 posFixup;\n");
1193 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits->packed_output);
1195 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1197 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
1199 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1201 if (version->major >= 3)
1203 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
1205 if (use_vs(state))
1206 shader_addline(buffer, "varying vec4 %s_link[%u];\n", prefix, in_count);
1207 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1210 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1212 if (!(map & 1))
1213 continue;
1215 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1217 if (reg_maps->luminanceparams & (1 << i))
1219 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1220 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1221 extra_constants_needed++;
1224 extra_constants_needed++;
1227 if (ps_args->srgb_correction)
1229 shader_addline(buffer, "const vec4 srgb_const0 = ");
1230 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
1231 shader_addline(buffer, ";\n");
1232 shader_addline(buffer, "const vec4 srgb_const1 = ");
1233 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
1234 shader_addline(buffer, ";\n");
1236 if (reg_maps->vpos || reg_maps->usesdsy)
1238 if (shader->limits->constant_float + extra_constants_needed
1239 + 1 < gl_info->limits.glsl_ps_float_constants)
1241 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1242 extra_constants_needed++;
1244 else
1246 float ycorrection[] =
1248 context->render_offscreen ? 0.0f : fb->render_targets[0]->height,
1249 context->render_offscreen ? 1.0f : -1.0f,
1250 0.0f,
1251 0.0f,
1254 /* This happens because we do not have proper tracking of the
1255 * constant registers that are actually used, only the max
1256 * limit of the shader version. */
1257 FIXME("Cannot find a free uniform for vpos correction params\n");
1258 shader_addline(buffer, "const vec4 ycorrection = ");
1259 shader_glsl_append_imm_vec4(buffer, ycorrection);
1260 shader_addline(buffer, ";\n");
1262 shader_addline(buffer, "vec4 vpos;\n");
1266 /* Declare output register temporaries */
1267 if (shader->limits->packed_output)
1268 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
1270 /* Declare temporary variables */
1271 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1273 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1276 /* Declare loop registers aLx */
1277 if (version->major < 4)
1279 for (i = 0; i < reg_maps->loop_depth; ++i)
1281 shader_addline(buffer, "int aL%u;\n", i);
1282 shader_addline(buffer, "int tmpInt%u;\n", i);
1286 /* Temporary variables for matrix operations */
1287 shader_addline(buffer, "vec4 tmp0;\n");
1288 shader_addline(buffer, "vec4 tmp1;\n");
1290 if (!shader->load_local_constsF)
1292 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1294 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
1295 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
1296 shader_addline(buffer, ";\n");
1300 /* Start the main program. */
1301 shader_addline(buffer, "void main()\n{\n");
1303 /* Direct3D applications expect integer vPos values, while OpenGL drivers
1304 * add approximately 0.5. This causes off-by-one problems as spotted by
1305 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
1306 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
1307 * causes precision troubles when we just subtract 0.5.
1309 * To deal with that, just floor() the position. This will eliminate the
1310 * fraction on all cards.
1312 * TODO: Test how this behaves with multisampling.
1314 * An advantage of floor is that it works even if the driver doesn't add
1315 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
1316 * to return in gl_FragCoord, even though coordinates specify the pixel
1317 * centers instead of the pixel corners. This code will behave correctly
1318 * on drivers that returns integer values. */
1319 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
1320 shader_addline(buffer,
1321 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1324 /*****************************************************************************
1325 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1327 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1328 ****************************************************************************/
1330 /* Prototypes */
1331 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1332 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1334 /** Used for opcode modifiers - They multiply the result by the specified amount */
1335 static const char * const shift_glsl_tab[] = {
1336 "", /* 0 (none) */
1337 "2.0 * ", /* 1 (x2) */
1338 "4.0 * ", /* 2 (x4) */
1339 "8.0 * ", /* 3 (x8) */
1340 "16.0 * ", /* 4 (x16) */
1341 "32.0 * ", /* 5 (x32) */
1342 "", /* 6 (x64) */
1343 "", /* 7 (x128) */
1344 "", /* 8 (d256) */
1345 "", /* 9 (d128) */
1346 "", /* 10 (d64) */
1347 "", /* 11 (d32) */
1348 "0.0625 * ", /* 12 (d16) */
1349 "0.125 * ", /* 13 (d8) */
1350 "0.25 * ", /* 14 (d4) */
1351 "0.5 * " /* 15 (d2) */
1354 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1355 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1356 const char *in_reg, const char *in_regswizzle, char *out_str)
1358 out_str[0] = 0;
1360 switch (src_modifier)
1362 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1363 case WINED3DSPSM_DW:
1364 case WINED3DSPSM_NONE:
1365 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1366 break;
1367 case WINED3DSPSM_NEG:
1368 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1369 break;
1370 case WINED3DSPSM_NOT:
1371 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1372 break;
1373 case WINED3DSPSM_BIAS:
1374 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1375 break;
1376 case WINED3DSPSM_BIASNEG:
1377 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1378 break;
1379 case WINED3DSPSM_SIGN:
1380 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1381 break;
1382 case WINED3DSPSM_SIGNNEG:
1383 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1384 break;
1385 case WINED3DSPSM_COMP:
1386 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1387 break;
1388 case WINED3DSPSM_X2:
1389 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1390 break;
1391 case WINED3DSPSM_X2NEG:
1392 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1393 break;
1394 case WINED3DSPSM_ABS:
1395 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1396 break;
1397 case WINED3DSPSM_ABSNEG:
1398 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1399 break;
1400 default:
1401 FIXME("Unhandled modifier %u\n", src_modifier);
1402 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1406 /** Writes the GLSL variable name that corresponds to the register that the
1407 * DX opcode parameter is trying to access */
1408 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1409 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1411 /* oPos, oFog and oPts in D3D */
1412 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
1414 const struct wined3d_shader *shader = ins->ctx->shader;
1415 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1416 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1417 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1418 const char *prefix = shader_glsl_get_prefix(version->type);
1419 struct glsl_src_param rel_param0, rel_param1;
1420 char imm_str[4][17];
1422 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
1423 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
1424 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
1425 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
1426 *is_color = FALSE;
1428 switch (reg->type)
1430 case WINED3DSPR_TEMP:
1431 sprintf(register_name, "R%u", reg->idx[0].offset);
1432 break;
1434 case WINED3DSPR_INPUT:
1435 /* vertex shaders */
1436 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1438 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1439 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx[0].offset))
1440 *is_color = TRUE;
1441 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
1442 break;
1445 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1447 if (reg->idx[0].rel_addr)
1449 if (reg->idx[1].rel_addr)
1450 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
1451 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1452 else
1453 sprintf(register_name, "gs_in[%s + %u][%u]",
1454 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
1456 else if (reg->idx[1].rel_addr)
1457 sprintf(register_name, "gs_in[%u][%s + %u]",
1458 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1459 else
1460 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
1461 break;
1464 /* pixel shaders >= 3.0 */
1465 if (version->major >= 3)
1467 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
1468 unsigned int in_count = vec4_varyings(version->major, gl_info);
1470 if (reg->idx[0].rel_addr)
1472 /* Removing a + 0 would be an obvious optimization, but
1473 * OS X doesn't see the NOP operation there. */
1474 if (idx)
1476 if (shader->u.ps.declared_in_count > in_count)
1478 sprintf(register_name,
1479 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
1480 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
1481 prefix, rel_param0.param_str, idx);
1483 else
1485 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
1488 else
1490 if (shader->u.ps.declared_in_count > in_count)
1492 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
1493 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
1494 prefix, rel_param0.param_str);
1496 else
1498 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
1502 else
1504 if (idx == in_count) sprintf(register_name, "gl_Color");
1505 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
1506 else sprintf(register_name, "%s_in[%u]", prefix, idx);
1509 else
1511 if (!reg->idx[0].offset)
1512 strcpy(register_name, "gl_Color");
1513 else
1514 strcpy(register_name, "gl_SecondaryColor");
1515 break;
1517 break;
1519 case WINED3DSPR_CONST:
1521 /* Relative addressing */
1522 if (reg->idx[0].rel_addr)
1524 if (reg->idx[0].offset)
1525 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
1526 else
1527 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
1529 else
1531 if (shader_constant_is_local(shader, reg->idx[0].offset))
1532 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
1533 else
1534 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
1537 break;
1539 case WINED3DSPR_CONSTINT:
1540 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
1541 break;
1543 case WINED3DSPR_CONSTBOOL:
1544 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
1545 break;
1547 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1548 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1549 sprintf(register_name, "T%u", reg->idx[0].offset);
1550 else
1551 sprintf(register_name, "A%u", reg->idx[0].offset);
1552 break;
1554 case WINED3DSPR_LOOP:
1555 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
1556 break;
1558 case WINED3DSPR_SAMPLER:
1559 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
1560 break;
1562 case WINED3DSPR_COLOROUT:
1563 if (reg->idx[0].offset >= gl_info->limits.buffers)
1564 WARN("Write to render target %u, only %d supported.\n",
1565 reg->idx[0].offset, gl_info->limits.buffers);
1567 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
1568 break;
1570 case WINED3DSPR_RASTOUT:
1571 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
1572 break;
1574 case WINED3DSPR_DEPTHOUT:
1575 sprintf(register_name, "gl_FragDepth");
1576 break;
1578 case WINED3DSPR_ATTROUT:
1579 if (!reg->idx[0].offset)
1580 sprintf(register_name, "%s_out[8]", prefix);
1581 else
1582 sprintf(register_name, "%s_out[9]", prefix);
1583 break;
1585 case WINED3DSPR_TEXCRDOUT:
1586 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1587 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
1588 break;
1590 case WINED3DSPR_MISCTYPE:
1591 if (!reg->idx[0].offset)
1593 /* vPos */
1594 sprintf(register_name, "vpos");
1596 else if (reg->idx[0].offset == 1)
1598 /* Note that gl_FrontFacing is a bool, while vFace is
1599 * a float for which the sign determines front/back */
1600 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1602 else
1604 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
1605 sprintf(register_name, "unrecognized_register");
1607 break;
1609 case WINED3DSPR_IMMCONST:
1610 switch (reg->immconst_type)
1612 case WINED3D_IMMCONST_SCALAR:
1613 switch (reg->data_type)
1615 case WINED3D_DATA_FLOAT:
1616 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
1617 break;
1618 case WINED3D_DATA_INT:
1619 sprintf(register_name, "%#x", reg->immconst_data[0]);
1620 break;
1621 case WINED3D_DATA_RESOURCE:
1622 case WINED3D_DATA_SAMPLER:
1623 case WINED3D_DATA_UINT:
1624 sprintf(register_name, "%#xu", reg->immconst_data[0]);
1625 break;
1626 default:
1627 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1628 break;
1630 break;
1632 case WINED3D_IMMCONST_VEC4:
1633 switch (reg->data_type)
1635 case WINED3D_DATA_FLOAT:
1636 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
1637 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
1638 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
1639 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
1640 sprintf(register_name, "vec4(%s, %s, %s, %s)",
1641 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
1642 break;
1643 case WINED3D_DATA_INT:
1644 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
1645 reg->immconst_data[0], reg->immconst_data[1],
1646 reg->immconst_data[2], reg->immconst_data[3]);
1647 break;
1648 case WINED3D_DATA_RESOURCE:
1649 case WINED3D_DATA_SAMPLER:
1650 case WINED3D_DATA_UINT:
1651 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
1652 reg->immconst_data[0], reg->immconst_data[1],
1653 reg->immconst_data[2], reg->immconst_data[3]);
1654 break;
1655 default:
1656 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1657 break;
1659 break;
1661 default:
1662 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
1663 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
1665 break;
1667 case WINED3DSPR_CONSTBUFFER:
1668 if (reg->idx[1].rel_addr)
1669 sprintf(register_name, "%s_cb%u[%s + %u]",
1670 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1671 else
1672 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
1673 break;
1675 case WINED3DSPR_PRIMID:
1676 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
1677 break;
1679 default:
1680 FIXME("Unhandled register type %#x.\n", reg->type);
1681 sprintf(register_name, "unrecognized_register");
1682 break;
1686 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1688 *str++ = '.';
1689 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1690 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1691 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1692 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1693 *str = '\0';
1696 /* Get the GLSL write mask for the destination register */
1697 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1699 DWORD mask = param->write_mask;
1701 if (shader_is_scalar(&param->reg))
1703 mask = WINED3DSP_WRITEMASK_0;
1704 *write_mask = '\0';
1706 else
1708 shader_glsl_write_mask_to_str(mask, write_mask);
1711 return mask;
1714 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1715 unsigned int size = 0;
1717 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1718 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1719 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1720 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1722 return size;
1725 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
1727 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1728 * but addressed as "rgba". To fix this we need to swap the register's x
1729 * and z components. */
1730 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1732 *str++ = '.';
1733 /* swizzle bits fields: wwzzyyxx */
1734 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
1735 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
1736 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
1737 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
1738 *str = '\0';
1741 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
1742 BOOL fixup, DWORD mask, char *swizzle_str)
1744 if (shader_is_scalar(&param->reg))
1745 *swizzle_str = '\0';
1746 else
1747 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
1750 /* From a given parameter token, generate the corresponding GLSL string.
1751 * Also, return the actual register name and swizzle in case the
1752 * caller needs this information as well. */
1753 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1754 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
1756 BOOL is_color = FALSE;
1757 char swizzle_str[6];
1759 glsl_src->reg_name[0] = '\0';
1760 glsl_src->param_str[0] = '\0';
1761 swizzle_str[0] = '\0';
1763 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
1764 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
1766 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
1768 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
1770 else
1772 char reg_name[200];
1774 switch (wined3d_src->reg.data_type)
1776 case WINED3D_DATA_FLOAT:
1777 sprintf(reg_name, "%s", glsl_src->reg_name);
1778 break;
1779 case WINED3D_DATA_INT:
1780 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
1781 break;
1782 case WINED3D_DATA_RESOURCE:
1783 case WINED3D_DATA_SAMPLER:
1784 case WINED3D_DATA_UINT:
1785 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
1786 break;
1787 default:
1788 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
1789 sprintf(reg_name, "%s", glsl_src->reg_name);
1790 break;
1793 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
1797 /* From a given parameter token, generate the corresponding GLSL string.
1798 * Also, return the actual register name and swizzle in case the
1799 * caller needs this information as well. */
1800 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1801 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
1803 BOOL is_color = FALSE;
1805 glsl_dst->mask_str[0] = '\0';
1806 glsl_dst->reg_name[0] = '\0';
1808 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
1809 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1812 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1813 static DWORD shader_glsl_append_dst_ext(struct wined3d_shader_buffer *buffer,
1814 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
1815 enum wined3d_data_type data_type)
1817 struct glsl_dst_param glsl_dst;
1818 DWORD mask;
1820 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
1822 switch (data_type)
1824 case WINED3D_DATA_FLOAT:
1825 shader_addline(buffer, "%s%s = %s(",
1826 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1827 break;
1828 case WINED3D_DATA_INT:
1829 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
1830 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1831 break;
1832 case WINED3D_DATA_RESOURCE:
1833 case WINED3D_DATA_SAMPLER:
1834 case WINED3D_DATA_UINT:
1835 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
1836 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1837 break;
1838 default:
1839 FIXME("Unhandled data type %#x.\n", data_type);
1840 shader_addline(buffer, "%s%s = %s(",
1841 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1842 break;
1846 return mask;
1849 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1850 static DWORD shader_glsl_append_dst(struct wined3d_shader_buffer *buffer, const struct wined3d_shader_instruction *ins)
1852 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
1855 /** Process GLSL instruction modifiers */
1856 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1858 struct glsl_dst_param dst_param;
1859 DWORD modifiers;
1861 if (!ins->dst_count) return;
1863 modifiers = ins->dst[0].modifiers;
1864 if (!modifiers) return;
1866 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
1868 if (modifiers & WINED3DSPDM_SATURATE)
1870 /* _SAT means to clamp the value of the register to between 0 and 1 */
1871 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1872 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1875 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
1877 FIXME("_centroid modifier not handled\n");
1880 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
1882 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1886 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
1888 switch (op)
1890 case WINED3D_SHADER_REL_OP_GT: return ">";
1891 case WINED3D_SHADER_REL_OP_EQ: return "==";
1892 case WINED3D_SHADER_REL_OP_GE: return ">=";
1893 case WINED3D_SHADER_REL_OP_LT: return "<";
1894 case WINED3D_SHADER_REL_OP_NE: return "!=";
1895 case WINED3D_SHADER_REL_OP_LE: return "<=";
1896 default:
1897 FIXME("Unrecognized operator %#x.\n", op);
1898 return "(\?\?)";
1902 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
1903 DWORD resource_idx, DWORD flags, struct glsl_sample_function *sample_function)
1905 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
1906 const struct wined3d_gl_info *gl_info = ctx->gl_info;
1907 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
1908 && (((const struct shader_glsl_ctx_priv *)ctx->backend_data)->cur_ps_args->shadow & (1 << resource_idx));
1909 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
1910 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1911 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
1912 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
1914 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
1916 /* Note that there's no such thing as a projected cube texture. */
1917 switch (resource_type)
1919 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1920 if (shadow)
1922 if (lod)
1924 sample_function->name = projected ? "shadow1DProjLod" : "shadow1DLod";
1926 else if (grad)
1928 if (gl_info->supported[EXT_GPU_SHADER4])
1929 sample_function->name = projected ? "shadow1DProjGrad" : "shadow1DGrad";
1930 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1931 sample_function->name = projected ? "shadow1DProjGradARB" : "shadow1DGradARB";
1932 else
1934 FIXME("Unsupported 1D shadow grad function.\n");
1935 sample_function->name = "unsupported1DGrad";
1938 else
1940 sample_function->name = projected ? "shadow1DProj" : "shadow1D";
1942 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1944 else
1946 if (lod)
1948 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
1950 else if (grad)
1952 if (gl_info->supported[EXT_GPU_SHADER4])
1953 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
1954 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1955 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
1956 else
1958 FIXME("Unsupported 1D grad function.\n");
1959 sample_function->name = "unsupported1DGrad";
1962 else
1964 sample_function->name = projected ? "texture1DProj" : "texture1D";
1966 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1968 break;
1970 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1971 if (shadow)
1973 if (texrect)
1975 if (lod)
1977 sample_function->name = projected ? "shadow2DRectProjLod" : "shadow2DRectLod";
1979 else if (grad)
1981 if (gl_info->supported[EXT_GPU_SHADER4])
1982 sample_function->name = projected ? "shadow2DRectProjGrad" : "shadow2DRectGrad";
1983 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1984 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
1985 else
1987 FIXME("Unsupported RECT shadow grad function.\n");
1988 sample_function->name = "unsupported2DRectGrad";
1991 else
1993 sample_function->name = projected ? "shadow2DRectProj" : "shadow2DRect";
1996 else
1998 if (lod)
2000 sample_function->name = projected ? "shadow2DProjLod" : "shadow2DLod";
2002 else if (grad)
2004 if (gl_info->supported[EXT_GPU_SHADER4])
2005 sample_function->name = projected ? "shadow2DProjGrad" : "shadow2DGrad";
2006 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2007 sample_function->name = projected ? "shadow2DProjGradARB" : "shadow2DGradARB";
2008 else
2010 FIXME("Unsupported 2D shadow grad function.\n");
2011 sample_function->name = "unsupported2DGrad";
2014 else
2016 sample_function->name = projected ? "shadow2DProj" : "shadow2D";
2019 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2021 else
2023 if (texrect)
2025 if (lod)
2027 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
2029 else if (grad)
2031 if (gl_info->supported[EXT_GPU_SHADER4])
2032 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
2033 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2034 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
2035 else
2037 FIXME("Unsupported RECT grad function.\n");
2038 sample_function->name = "unsupported2DRectGrad";
2041 else
2043 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
2046 else
2048 if (lod)
2050 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
2052 else if (grad)
2054 if (gl_info->supported[EXT_GPU_SHADER4])
2055 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
2056 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2057 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
2058 else
2060 FIXME("Unsupported 2D grad function.\n");
2061 sample_function->name = "unsupported2DGrad";
2064 else
2066 sample_function->name = projected ? "texture2DProj" : "texture2D";
2069 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2071 break;
2073 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2074 if (shadow)
2076 FIXME("Unsupported 3D shadow function.\n");
2077 sample_function->name = "unsupported3DShadow";
2078 sample_function->coord_mask = 0;
2080 else
2082 if (lod)
2084 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
2086 else if (grad)
2088 if (gl_info->supported[EXT_GPU_SHADER4])
2089 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
2090 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2091 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
2092 else
2094 FIXME("Unsupported 3D grad function.\n");
2095 sample_function->name = "unsupported3DGrad";
2098 else
2100 sample_function->name = projected ? "texture3DProj" : "texture3D";
2102 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2104 break;
2106 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2107 if (shadow)
2109 FIXME("Unsupported Cube shadow function.\n");
2110 sample_function->name = "unsupportedCubeShadow";
2111 sample_function->coord_mask = 0;
2113 else
2115 if (lod)
2117 sample_function->name = "textureCubeLod";
2119 else if (grad)
2121 if (gl_info->supported[EXT_GPU_SHADER4])
2122 sample_function->name = "textureCubeGrad";
2123 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2124 sample_function->name = "textureCubeGradARB";
2125 else
2127 FIXME("Unsupported Cube grad function.\n");
2128 sample_function->name = "unsupportedCubeGrad";
2131 else
2133 sample_function->name = "textureCube";
2135 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2137 break;
2139 default:
2140 sample_function->name = "";
2141 sample_function->coord_mask = 0;
2142 FIXME("Unhandled resource type %#x.\n", resource_type);
2143 break;
2147 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2148 BOOL sign_fixup, enum fixup_channel_source channel_source)
2150 switch(channel_source)
2152 case CHANNEL_SOURCE_ZERO:
2153 strcat(arguments, "0.0");
2154 break;
2156 case CHANNEL_SOURCE_ONE:
2157 strcat(arguments, "1.0");
2158 break;
2160 case CHANNEL_SOURCE_X:
2161 strcat(arguments, reg_name);
2162 strcat(arguments, ".x");
2163 break;
2165 case CHANNEL_SOURCE_Y:
2166 strcat(arguments, reg_name);
2167 strcat(arguments, ".y");
2168 break;
2170 case CHANNEL_SOURCE_Z:
2171 strcat(arguments, reg_name);
2172 strcat(arguments, ".z");
2173 break;
2175 case CHANNEL_SOURCE_W:
2176 strcat(arguments, reg_name);
2177 strcat(arguments, ".w");
2178 break;
2180 default:
2181 FIXME("Unhandled channel source %#x\n", channel_source);
2182 strcat(arguments, "undefined");
2183 break;
2186 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2189 static void shader_glsl_color_correction_ext(struct wined3d_shader_buffer *buffer,
2190 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2192 unsigned int mask_size, remaining;
2193 DWORD fixup_mask = 0;
2194 char arguments[256];
2195 char mask_str[6];
2197 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2198 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2199 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2200 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2201 if (!(mask &= fixup_mask))
2202 return;
2204 if (is_complex_fixup(fixup))
2206 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2207 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2208 return;
2211 shader_glsl_write_mask_to_str(mask, mask_str);
2212 mask_size = shader_glsl_get_write_mask_size(mask);
2214 arguments[0] = '\0';
2215 remaining = mask_size;
2216 if (mask & WINED3DSP_WRITEMASK_0)
2218 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2219 if (--remaining) strcat(arguments, ", ");
2221 if (mask & WINED3DSP_WRITEMASK_1)
2223 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2224 if (--remaining) strcat(arguments, ", ");
2226 if (mask & WINED3DSP_WRITEMASK_2)
2228 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2229 if (--remaining) strcat(arguments, ", ");
2231 if (mask & WINED3DSP_WRITEMASK_3)
2233 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2234 if (--remaining) strcat(arguments, ", ");
2237 if (mask_size > 1)
2238 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2239 else
2240 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2243 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2245 char reg_name[256];
2246 BOOL is_color;
2248 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2249 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2252 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2253 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2254 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2256 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2257 char dst_swizzle[6];
2258 struct color_fixup_desc fixup;
2259 BOOL np2_fixup = FALSE;
2260 va_list args;
2262 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2264 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2266 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2267 fixup = priv->cur_ps_args->color_fixup[sampler];
2269 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2270 if(bias) {
2271 FIXME("Biased sampling from NP2 textures is unsupported\n");
2272 } else {
2273 np2_fixup = TRUE;
2277 else
2279 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
2282 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2284 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2285 sample_function->name, shader_glsl_get_prefix(version->type), sampler);
2287 va_start(args, coord_reg_fmt);
2288 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2289 va_end(args);
2291 if(bias) {
2292 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2293 } else {
2294 if (np2_fixup) {
2295 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2296 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2298 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2299 (idx % 2) ? "zw" : "xy", dst_swizzle);
2300 } else if(dx && dy) {
2301 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2302 } else {
2303 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2307 if(!is_identity_fixup(fixup)) {
2308 shader_glsl_color_correction(ins, fixup);
2312 /*****************************************************************************
2313 * Begin processing individual instruction opcodes
2314 ****************************************************************************/
2316 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2318 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2319 struct glsl_src_param src0_param;
2320 struct glsl_src_param src1_param;
2321 DWORD write_mask;
2322 const char *op;
2324 /* Determine the GLSL operator to use based on the opcode */
2325 switch (ins->handler_idx)
2327 case WINED3DSIH_ADD: op = "+"; break;
2328 case WINED3DSIH_AND: op = "&"; break;
2329 case WINED3DSIH_DIV: op = "/"; break;
2330 case WINED3DSIH_IADD: op = "+"; break;
2331 case WINED3DSIH_ISHL: op = "<<"; break;
2332 case WINED3DSIH_MUL: op = "*"; break;
2333 case WINED3DSIH_OR: op = "|"; break;
2334 case WINED3DSIH_SUB: op = "-"; break;
2335 case WINED3DSIH_USHR: op = ">>"; break;
2336 case WINED3DSIH_XOR: op = "^"; break;
2337 default:
2338 op = "<unhandled operator>";
2339 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2340 break;
2343 write_mask = shader_glsl_append_dst(buffer, ins);
2344 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2345 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2346 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2349 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2351 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2352 struct glsl_src_param src0_param;
2353 struct glsl_src_param src1_param;
2354 unsigned int mask_size;
2355 DWORD write_mask;
2356 const char *op;
2358 write_mask = shader_glsl_append_dst(buffer, ins);
2359 mask_size = shader_glsl_get_write_mask_size(write_mask);
2360 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2361 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2363 if (mask_size > 1)
2365 switch (ins->handler_idx)
2367 case WINED3DSIH_EQ: op = "equal"; break;
2368 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2369 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2370 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
2371 case WINED3DSIH_LT: op = "lessThan"; break;
2372 case WINED3DSIH_NE: op = "notEqual"; break;
2373 default:
2374 op = "<unhandled operator>";
2375 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2376 break;
2379 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2380 mask_size, op, src0_param.param_str, src1_param.param_str);
2382 else
2384 switch (ins->handler_idx)
2386 case WINED3DSIH_EQ: op = "=="; break;
2387 case WINED3DSIH_GE: op = ">="; break;
2388 case WINED3DSIH_IGE: op = ">="; break;
2389 case WINED3DSIH_UGE: op = ">="; break;
2390 case WINED3DSIH_LT: op = "<"; break;
2391 case WINED3DSIH_NE: op = "!="; break;
2392 default:
2393 op = "<unhandled operator>";
2394 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2395 break;
2398 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2399 src0_param.param_str, op, src1_param.param_str);
2403 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2405 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2406 struct glsl_src_param src0_param;
2407 struct glsl_src_param src1_param;
2408 DWORD write_mask;
2410 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2411 * not, we can emulate it. */
2412 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2413 FIXME("64-bit integer multiplies not implemented.\n");
2415 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2417 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2418 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2419 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2421 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2422 src0_param.param_str, src1_param.param_str);
2426 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2428 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2429 struct glsl_src_param src0_param, src1_param;
2430 DWORD write_mask;
2432 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2435 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2437 char dst_mask[6];
2439 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2440 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2441 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2442 shader_addline(buffer, "tmp0%s = %s / %s;\n",
2443 dst_mask, src0_param.param_str, src1_param.param_str);
2445 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2446 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2447 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2448 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
2450 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2451 shader_addline(buffer, "tmp0%s);\n", dst_mask);
2453 else
2455 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2456 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2457 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2458 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
2461 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2463 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2464 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2465 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2466 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
2470 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2471 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2473 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2474 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2475 struct glsl_src_param src0_param;
2476 DWORD write_mask;
2478 write_mask = shader_glsl_append_dst(buffer, ins);
2479 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2481 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2482 * shader versions WINED3DSIO_MOVA is used for this. */
2483 if (ins->ctx->reg_maps->shader_version.major == 1
2484 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
2485 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2487 /* This is a simple floor() */
2488 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2489 if (mask_size > 1) {
2490 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2491 } else {
2492 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2495 else if(ins->handler_idx == WINED3DSIH_MOVA)
2497 /* We need to *round* to the nearest int here. */
2498 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2500 if (gl_info->supported[EXT_GPU_SHADER4])
2502 if (mask_size > 1)
2503 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2504 else
2505 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2507 else
2509 if (mask_size > 1)
2510 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2511 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2512 else
2513 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2514 src0_param.param_str, src0_param.param_str);
2517 else
2519 shader_addline(buffer, "%s);\n", src0_param.param_str);
2523 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2524 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2526 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2527 struct glsl_src_param src0_param;
2528 struct glsl_src_param src1_param;
2529 DWORD dst_write_mask, src_write_mask;
2530 unsigned int dst_size = 0;
2532 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2533 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2535 /* dp3 works on vec3, dp4 on vec4 */
2536 if (ins->handler_idx == WINED3DSIH_DP4)
2538 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2539 } else {
2540 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2543 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2544 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2546 if (dst_size > 1) {
2547 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2548 } else {
2549 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2553 /* Note that this instruction has some restrictions. The destination write mask
2554 * can't contain the w component, and the source swizzles have to be .xyzw */
2555 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2557 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2558 struct glsl_src_param src0_param;
2559 struct glsl_src_param src1_param;
2560 char dst_mask[6];
2562 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2563 shader_glsl_append_dst(ins->ctx->buffer, ins);
2564 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2565 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2566 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2569 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
2571 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
2574 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2575 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2576 * GLSL uses the value as-is. */
2577 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2579 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2580 struct glsl_src_param src0_param;
2581 struct glsl_src_param src1_param;
2582 DWORD dst_write_mask;
2583 unsigned int dst_size;
2585 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2586 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2588 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2589 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2591 if (dst_size > 1)
2593 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2594 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
2596 else
2598 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
2599 src1_param.param_str, src0_param.param_str, src1_param.param_str);
2603 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2604 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
2606 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2607 struct glsl_src_param src_param;
2608 const char *instruction;
2609 DWORD write_mask;
2610 unsigned i;
2612 /* Determine the GLSL function to use based on the opcode */
2613 /* TODO: Possibly make this a table for faster lookups */
2614 switch (ins->handler_idx)
2616 case WINED3DSIH_MIN: instruction = "min"; break;
2617 case WINED3DSIH_MAX: instruction = "max"; break;
2618 case WINED3DSIH_ABS: instruction = "abs"; break;
2619 case WINED3DSIH_FRC: instruction = "fract"; break;
2620 case WINED3DSIH_DSX: instruction = "dFdx"; break;
2621 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
2622 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
2623 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
2624 default: instruction = "";
2625 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2626 break;
2629 write_mask = shader_glsl_append_dst(buffer, ins);
2631 shader_addline(buffer, "%s(", instruction);
2633 if (ins->src_count)
2635 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2636 shader_addline(buffer, "%s", src_param.param_str);
2637 for (i = 1; i < ins->src_count; ++i)
2639 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
2640 shader_addline(buffer, ", %s", src_param.param_str);
2644 shader_addline(buffer, "));\n");
2647 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
2649 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
2651 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2652 struct glsl_src_param src_param;
2653 unsigned int mask_size;
2654 DWORD write_mask;
2655 char dst_mask[6];
2657 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
2658 mask_size = shader_glsl_get_write_mask_size(write_mask);
2659 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2661 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
2662 src_param.param_str, src_param.param_str);
2663 shader_glsl_append_dst(buffer, ins);
2665 if (mask_size > 1)
2667 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
2668 mask_size, src_param.param_str);
2670 else
2672 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
2673 src_param.param_str);
2677 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
2679 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2680 struct glsl_src_param src0_param;
2681 const char *prefix, *suffix;
2682 unsigned int dst_size;
2683 DWORD dst_write_mask;
2685 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2686 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2688 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src0_param);
2690 switch (ins->handler_idx)
2692 case WINED3DSIH_EXP:
2693 case WINED3DSIH_EXPP:
2694 prefix = "exp2(";
2695 suffix = ")";
2696 break;
2698 case WINED3DSIH_LOG:
2699 case WINED3DSIH_LOGP:
2700 prefix = "log2(abs(";
2701 suffix = "))";
2702 break;
2704 case WINED3DSIH_RCP:
2705 prefix = "1.0 / ";
2706 suffix = "";
2707 break;
2709 case WINED3DSIH_RSQ:
2710 prefix = "inversesqrt(abs(";
2711 suffix = "))";
2712 break;
2714 default:
2715 prefix = "";
2716 suffix = "";
2717 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2718 break;
2721 if (dst_size > 1)
2722 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
2723 else
2724 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
2727 /** Process the WINED3DSIO_EXPP instruction in GLSL:
2728 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2729 * dst.x = 2^(floor(src))
2730 * dst.y = src - floor(src)
2731 * dst.z = 2^src (partial precision is allowed, but optional)
2732 * dst.w = 1.0;
2733 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2734 * dst = 2^src; (partial precision is allowed, but optional)
2736 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
2738 if (ins->ctx->reg_maps->shader_version.major < 2)
2740 struct glsl_src_param src_param;
2741 char dst_mask[6];
2743 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2745 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
2746 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
2747 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
2748 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
2750 shader_glsl_append_dst(ins->ctx->buffer, ins);
2751 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2752 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
2753 return;
2756 shader_glsl_scalar_op(ins);
2759 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
2761 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2762 struct glsl_src_param src_param;
2763 unsigned int mask_size;
2764 DWORD write_mask;
2766 write_mask = shader_glsl_append_dst(buffer, ins);
2767 mask_size = shader_glsl_get_write_mask_size(write_mask);
2768 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2770 if (mask_size > 1)
2771 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
2772 else
2773 shader_addline(buffer, "int(%s));\n", src_param.param_str);
2776 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
2778 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2779 struct glsl_src_param src_param;
2780 unsigned int mask_size;
2781 DWORD write_mask;
2783 write_mask = shader_glsl_append_dst(buffer, ins);
2784 mask_size = shader_glsl_get_write_mask_size(write_mask);
2785 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2787 if (mask_size > 1)
2788 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
2789 else
2790 shader_addline(buffer, "float(%s));\n", src_param.param_str);
2793 /** Process signed comparison opcodes in GLSL. */
2794 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
2796 struct glsl_src_param src0_param;
2797 struct glsl_src_param src1_param;
2798 DWORD write_mask;
2799 unsigned int mask_size;
2801 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2802 mask_size = shader_glsl_get_write_mask_size(write_mask);
2803 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2804 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2806 if (mask_size > 1) {
2807 const char *compare;
2809 switch(ins->handler_idx)
2811 case WINED3DSIH_SLT: compare = "lessThan"; break;
2812 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
2813 default: compare = "";
2814 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2817 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
2818 src0_param.param_str, src1_param.param_str);
2819 } else {
2820 switch(ins->handler_idx)
2822 case WINED3DSIH_SLT:
2823 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2824 * to return 0.0 but step returns 1.0 because step is not < x
2825 * An alternative is a bvec compare padded with an unused second component.
2826 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2827 * issue. Playing with not() is not possible either because not() does not accept
2828 * a scalar.
2830 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
2831 src0_param.param_str, src1_param.param_str);
2832 break;
2833 case WINED3DSIH_SGE:
2834 /* Here we can use the step() function and safe a conditional */
2835 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
2836 break;
2837 default:
2838 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2844 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
2846 const char *condition_prefix, *condition_suffix;
2847 struct wined3d_shader_dst_param dst;
2848 struct glsl_src_param src0_param;
2849 struct glsl_src_param src1_param;
2850 struct glsl_src_param src2_param;
2851 BOOL temp_destination = FALSE;
2852 DWORD cmp_channel = 0;
2853 unsigned int i, j;
2854 char mask_char[6];
2855 DWORD write_mask;
2857 switch (ins->handler_idx)
2859 case WINED3DSIH_CMP:
2860 condition_prefix = "";
2861 condition_suffix = " >= 0.0";
2862 break;
2864 case WINED3DSIH_CND:
2865 condition_prefix = "";
2866 condition_suffix = " > 0.5";
2867 break;
2869 case WINED3DSIH_MOVC:
2870 condition_prefix = "bool(";
2871 condition_suffix = ")";
2872 break;
2874 default:
2875 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2876 condition_prefix = "<unhandled prefix>";
2877 condition_suffix = "<unhandled suffix>";
2878 break;
2881 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
2883 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2884 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2885 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2886 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2888 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2889 condition_prefix, src0_param.param_str, condition_suffix,
2890 src1_param.param_str, src2_param.param_str);
2891 return;
2894 dst = ins->dst[0];
2896 /* Splitting the instruction up in multiple lines imposes a problem:
2897 * The first lines may overwrite source parameters of the following lines.
2898 * Deal with that by using a temporary destination register if needed. */
2899 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
2900 && ins->src[0].reg.type == dst.reg.type)
2901 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
2902 && ins->src[1].reg.type == dst.reg.type)
2903 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
2904 && ins->src[2].reg.type == dst.reg.type))
2905 temp_destination = TRUE;
2907 /* Cycle through all source0 channels. */
2908 for (i = 0; i < 4; ++i)
2910 write_mask = 0;
2911 /* Find the destination channels which use the current source0 channel. */
2912 for (j = 0; j < 4; ++j)
2914 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2916 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2917 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2920 dst.write_mask = ins->dst[0].write_mask & write_mask;
2922 if (temp_destination)
2924 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
2925 continue;
2926 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
2928 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
2929 continue;
2931 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2932 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2933 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2935 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2936 condition_prefix, src0_param.param_str, condition_suffix,
2937 src1_param.param_str, src2_param.param_str);
2940 if (temp_destination)
2942 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
2943 shader_glsl_append_dst(ins->ctx->buffer, ins);
2944 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
2948 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2949 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2950 * the compare is done per component of src0. */
2951 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
2953 struct glsl_src_param src0_param;
2954 struct glsl_src_param src1_param;
2955 struct glsl_src_param src2_param;
2956 DWORD write_mask;
2957 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2958 ins->ctx->reg_maps->shader_version.minor);
2960 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
2962 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2963 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2964 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2965 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2967 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
2968 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
2969 else
2970 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2971 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2972 return;
2975 shader_glsl_conditional_move(ins);
2978 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2979 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
2981 struct glsl_src_param src0_param;
2982 struct glsl_src_param src1_param;
2983 struct glsl_src_param src2_param;
2984 DWORD write_mask;
2986 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2987 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2988 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2989 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2990 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
2991 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2994 /* Handles transforming all WINED3DSIO_M?x? opcodes for
2995 Vertex shaders to GLSL codes */
2996 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
2998 int i;
2999 int nComponents = 0;
3000 struct wined3d_shader_dst_param tmp_dst = {{0}};
3001 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3002 struct wined3d_shader_instruction tmp_ins;
3004 memset(&tmp_ins, 0, sizeof(tmp_ins));
3006 /* Set constants for the temporary argument */
3007 tmp_ins.ctx = ins->ctx;
3008 tmp_ins.dst_count = 1;
3009 tmp_ins.dst = &tmp_dst;
3010 tmp_ins.src_count = 2;
3011 tmp_ins.src = tmp_src;
3013 switch(ins->handler_idx)
3015 case WINED3DSIH_M4x4:
3016 nComponents = 4;
3017 tmp_ins.handler_idx = WINED3DSIH_DP4;
3018 break;
3019 case WINED3DSIH_M4x3:
3020 nComponents = 3;
3021 tmp_ins.handler_idx = WINED3DSIH_DP4;
3022 break;
3023 case WINED3DSIH_M3x4:
3024 nComponents = 4;
3025 tmp_ins.handler_idx = WINED3DSIH_DP3;
3026 break;
3027 case WINED3DSIH_M3x3:
3028 nComponents = 3;
3029 tmp_ins.handler_idx = WINED3DSIH_DP3;
3030 break;
3031 case WINED3DSIH_M3x2:
3032 nComponents = 2;
3033 tmp_ins.handler_idx = WINED3DSIH_DP3;
3034 break;
3035 default:
3036 break;
3039 tmp_dst = ins->dst[0];
3040 tmp_src[0] = ins->src[0];
3041 tmp_src[1] = ins->src[1];
3042 for (i = 0; i < nComponents; ++i)
3044 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3045 shader_glsl_dot(&tmp_ins);
3046 ++tmp_src[1].reg.idx[0].offset;
3051 The LRP instruction performs a component-wise linear interpolation
3052 between the second and third operands using the first operand as the
3053 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3054 This is equivalent to mix(src2, src1, src0);
3056 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3058 struct glsl_src_param src0_param;
3059 struct glsl_src_param src1_param;
3060 struct glsl_src_param src2_param;
3061 DWORD write_mask;
3063 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3065 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3066 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3067 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3069 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3070 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3073 /** Process the WINED3DSIO_LIT instruction in GLSL:
3074 * dst.x = dst.w = 1.0
3075 * dst.y = (src0.x > 0) ? src0.x
3076 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3077 * where src.w is clamped at +- 128
3079 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3081 struct glsl_src_param src0_param;
3082 struct glsl_src_param src1_param;
3083 struct glsl_src_param src3_param;
3084 char dst_mask[6];
3086 shader_glsl_append_dst(ins->ctx->buffer, ins);
3087 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3089 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3090 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3091 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3093 /* The sdk specifies the instruction like this
3094 * dst.x = 1.0;
3095 * if(src.x > 0.0) dst.y = src.x
3096 * else dst.y = 0.0.
3097 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3098 * else dst.z = 0.0;
3099 * dst.w = 1.0;
3100 * (where power = src.w clamped between -128 and 128)
3102 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3103 * dst.x = 1.0 ... No further explanation needed
3104 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3105 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3106 * dst.w = 1.0. ... Nothing fancy.
3108 * So we still have one conditional in there. So do this:
3109 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3111 * 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),
3112 * 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.
3113 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3115 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3116 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3117 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3119 shader_addline(ins->ctx->buffer,
3120 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3121 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3122 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3123 src0_param.param_str, src3_param.param_str, dst_mask);
3126 /** Process the WINED3DSIO_DST instruction in GLSL:
3127 * dst.x = 1.0
3128 * dst.y = src0.x * src0.y
3129 * dst.z = src0.z
3130 * dst.w = src1.w
3132 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3134 struct glsl_src_param src0y_param;
3135 struct glsl_src_param src0z_param;
3136 struct glsl_src_param src1y_param;
3137 struct glsl_src_param src1w_param;
3138 char dst_mask[6];
3140 shader_glsl_append_dst(ins->ctx->buffer, ins);
3141 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3143 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3144 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3145 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3146 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3148 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3149 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3152 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3153 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3154 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3156 * dst.x = cos(src0.?)
3157 * dst.y = sin(src0.?)
3158 * dst.z = dst.z
3159 * dst.w = dst.w
3161 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3163 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3164 struct glsl_src_param src0_param;
3165 DWORD write_mask;
3167 if (ins->ctx->reg_maps->shader_version.major < 4)
3169 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3171 write_mask = shader_glsl_append_dst(buffer, ins);
3172 switch (write_mask)
3174 case WINED3DSP_WRITEMASK_0:
3175 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3176 break;
3178 case WINED3DSP_WRITEMASK_1:
3179 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3180 break;
3182 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3183 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3184 src0_param.param_str, src0_param.param_str);
3185 break;
3187 default:
3188 ERR("Write mask should be .x, .y or .xy\n");
3189 break;
3192 return;
3195 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3198 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3200 char dst_mask[6];
3202 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3203 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3204 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3206 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3207 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3208 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3210 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3211 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3213 else
3215 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3216 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3217 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3220 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3222 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3223 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3224 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3228 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3229 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3230 * generate invalid code
3232 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3234 struct glsl_src_param src0_param;
3235 DWORD write_mask;
3237 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3238 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3240 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3243 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3244 * Start a for() loop where src1.y is the initial value of aL,
3245 * increment aL by src1.z for a total of src1.x iterations.
3246 * Need to use a temporary variable for this operation.
3248 /* FIXME: I don't think nested loops will work correctly this way. */
3249 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3251 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3252 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3253 const struct wined3d_shader *shader = ins->ctx->shader;
3254 const struct wined3d_shader_lconst *constant;
3255 struct glsl_src_param src1_param;
3256 const DWORD *control_values = NULL;
3258 if (ins->ctx->reg_maps->shader_version.major < 4)
3260 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3262 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3263 * class hardware doesn't support real varying indexing, but Microsoft
3264 * designed this feature for Shader model 2.x+. If the loop control is
3265 * known at compile time, the GLSL compiler can unroll the loop, and
3266 * replace indirect addressing with direct addressing. */
3267 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3269 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3271 if (constant->idx == ins->src[1].reg.idx[0].offset)
3273 control_values = constant->value;
3274 break;
3279 if (control_values)
3281 struct wined3d_shader_loop_control loop_control;
3282 loop_control.count = control_values[0];
3283 loop_control.start = control_values[1];
3284 loop_control.step = (int)control_values[2];
3286 if (loop_control.step > 0)
3288 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3289 loop_state->current_depth, loop_control.start,
3290 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3291 loop_state->current_depth, loop_control.step);
3293 else if (loop_control.step < 0)
3295 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3296 loop_state->current_depth, loop_control.start,
3297 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3298 loop_state->current_depth, loop_control.step);
3300 else
3302 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3303 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3304 loop_state->current_depth, loop_control.count,
3305 loop_state->current_depth);
3308 else
3310 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3311 loop_state->current_depth, loop_state->current_reg,
3312 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3313 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3316 ++loop_state->current_reg;
3318 else
3320 shader_addline(buffer, "for (;;)\n{\n");
3323 ++loop_state->current_depth;
3326 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3328 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3330 shader_addline(ins->ctx->buffer, "}\n");
3332 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3334 --loop_state->current_depth;
3335 --loop_state->current_reg;
3338 if (ins->handler_idx == WINED3DSIH_ENDREP)
3340 --loop_state->current_depth;
3344 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3346 const struct wined3d_shader *shader = ins->ctx->shader;
3347 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3348 const struct wined3d_shader_lconst *constant;
3349 struct glsl_src_param src0_param;
3350 const DWORD *control_values = NULL;
3352 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3353 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3355 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3357 if (constant->idx == ins->src[0].reg.idx[0].offset)
3359 control_values = constant->value;
3360 break;
3365 if (control_values)
3367 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3368 loop_state->current_depth, loop_state->current_depth,
3369 control_values[0], loop_state->current_depth);
3371 else
3373 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3374 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3375 loop_state->current_depth, loop_state->current_depth,
3376 src0_param.param_str, loop_state->current_depth);
3379 ++loop_state->current_depth;
3382 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3384 struct glsl_src_param src0_param;
3386 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3387 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
3390 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3392 struct glsl_src_param src0_param;
3393 struct glsl_src_param src1_param;
3395 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3396 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3398 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3399 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3402 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3404 shader_addline(ins->ctx->buffer, "} else {\n");
3407 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3409 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3412 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3414 shader_addline(ins->ctx->buffer, "break;\n");
3417 /* FIXME: According to MSDN the compare is done per component. */
3418 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3420 struct glsl_src_param src0_param;
3421 struct glsl_src_param src1_param;
3423 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3424 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3426 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3427 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3430 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
3432 struct glsl_src_param src_param;
3434 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
3435 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
3438 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
3440 shader_addline(ins->ctx->buffer, "}\n");
3441 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
3444 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
3446 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
3449 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3451 struct glsl_src_param src1_param;
3453 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3454 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
3455 src1_param.param_str, ins->src[0].reg.idx[0].offset);
3458 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3460 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3461 * function only suppresses the unhandled instruction warning
3465 /*********************************************
3466 * Pixel Shader Specific Code begins here
3467 ********************************************/
3468 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3470 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3471 ins->ctx->reg_maps->shader_version.minor);
3472 struct glsl_sample_function sample_function;
3473 DWORD sample_flags = 0;
3474 DWORD resource_idx;
3475 DWORD mask = 0, swizzle;
3476 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3478 /* 1.0-1.4: Use destination register as sampler source.
3479 * 2.0+: Use provided sampler source. */
3480 if (shader_version < WINED3D_SHADER_VERSION(2,0))
3481 resource_idx = ins->dst[0].reg.idx[0].offset;
3482 else
3483 resource_idx = ins->src[1].reg.idx[0].offset;
3485 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3487 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3488 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3489 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
3491 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3492 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3494 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3495 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3497 case WINED3D_TTFF_COUNT1:
3498 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3499 break;
3500 case WINED3D_TTFF_COUNT2:
3501 mask = WINED3DSP_WRITEMASK_1;
3502 break;
3503 case WINED3D_TTFF_COUNT3:
3504 mask = WINED3DSP_WRITEMASK_2;
3505 break;
3506 case WINED3D_TTFF_COUNT4:
3507 case WINED3D_TTFF_DISABLE:
3508 mask = WINED3DSP_WRITEMASK_3;
3509 break;
3513 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3515 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3517 if (src_mod == WINED3DSPSM_DZ) {
3518 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3519 mask = WINED3DSP_WRITEMASK_2;
3520 } else if (src_mod == WINED3DSPSM_DW) {
3521 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3522 mask = WINED3DSP_WRITEMASK_3;
3525 else
3527 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
3528 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3530 /* ps 2.0 texldp instruction always divides by the fourth component. */
3531 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3532 mask = WINED3DSP_WRITEMASK_3;
3536 if (priv->cur_ps_args->np2_fixup & (1 << resource_idx))
3537 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3539 shader_glsl_get_sample_function(ins->ctx, resource_idx, sample_flags, &sample_function);
3540 mask |= sample_function.coord_mask;
3542 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3543 else swizzle = ins->src[1].swizzle;
3545 /* 1.0-1.3: Use destination register as coordinate source.
3546 1.4+: Use provided coordinate source register. */
3547 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3549 char coord_mask[6];
3550 shader_glsl_write_mask_to_str(mask, coord_mask);
3551 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
3552 "T%u%s", resource_idx, coord_mask);
3554 else
3556 struct glsl_src_param coord_param;
3557 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3558 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3560 struct glsl_src_param bias;
3561 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3562 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3563 "%s", coord_param.param_str);
3564 } else {
3565 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
3566 "%s", coord_param.param_str);
3571 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3573 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3574 struct glsl_src_param coord_param, dx_param, dy_param;
3575 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3576 struct glsl_sample_function sample_function;
3577 DWORD sampler_idx;
3578 DWORD swizzle = ins->src[1].swizzle;
3579 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3581 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3583 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3584 shader_glsl_tex(ins);
3585 return;
3588 sampler_idx = ins->src[1].reg.idx[0].offset;
3589 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3590 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3592 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3593 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3594 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3595 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3597 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3598 "%s", coord_param.param_str);
3601 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3603 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3604 struct glsl_src_param coord_param, lod_param;
3605 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3606 struct glsl_sample_function sample_function;
3607 DWORD sampler_idx;
3608 DWORD swizzle = ins->src[1].swizzle;
3609 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3611 sampler_idx = ins->src[1].reg.idx[0].offset;
3612 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3613 && priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3614 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3616 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3617 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3619 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3621 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3622 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
3624 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
3625 * However, the NVIDIA drivers allow them in fragment shaders as well,
3626 * even without the appropriate extension. */
3627 WARN("Using %s in fragment shader.\n", sample_function.name);
3629 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3630 "%s", coord_param.param_str);
3633 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
3634 unsigned int resource_idx, unsigned int sampler_idx)
3636 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
3637 unsigned int i;
3639 for (i = 0; i < sampler_map->count; ++i)
3641 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
3642 return entries[i].bind_idx;
3645 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
3647 return ~0u;
3650 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
3652 struct glsl_sample_function sample_function;
3653 struct glsl_src_param coord_param;
3654 unsigned int sampler_idx;
3656 shader_glsl_get_sample_function(ins->ctx, ins->src[1].reg.idx[0].offset, 0, &sample_function);
3657 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3658 sampler_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
3659 ins->src[1].reg.idx[0].offset, ins->src[2].reg.idx[0].offset);
3660 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE,
3661 NULL, NULL, NULL, "%s", coord_param.param_str);
3664 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3666 /* FIXME: Make this work for more than just 2D textures */
3667 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3668 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3670 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3672 char dst_mask[6];
3674 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3675 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3676 ins->dst[0].reg.idx[0].offset, dst_mask);
3678 else
3680 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3681 DWORD reg = ins->src[0].reg.idx[0].offset;
3682 char dst_swizzle[6];
3684 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3686 if (src_mod == WINED3DSPSM_DZ)
3688 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3689 struct glsl_src_param div_param;
3691 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3693 if (mask_size > 1) {
3694 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3695 } else {
3696 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3699 else if (src_mod == WINED3DSPSM_DW)
3701 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3702 struct glsl_src_param div_param;
3704 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3706 if (mask_size > 1) {
3707 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3708 } else {
3709 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3711 } else {
3712 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3717 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3718 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3719 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3720 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3722 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3723 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3724 struct glsl_sample_function sample_function;
3725 struct glsl_src_param src0_param;
3726 UINT mask_size;
3728 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3730 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3731 * scalar, and projected sampling would require 4.
3733 * It is a dependent read - not valid with conditional NP2 textures
3735 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3736 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3738 switch(mask_size)
3740 case 1:
3741 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3742 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3743 break;
3745 case 2:
3746 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3747 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3748 break;
3750 case 3:
3751 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3752 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3753 break;
3755 default:
3756 FIXME("Unexpected mask size %u\n", mask_size);
3757 break;
3761 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3762 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3763 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3765 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3766 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3767 struct glsl_src_param src0_param;
3768 DWORD dst_mask;
3769 unsigned int mask_size;
3771 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3772 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3773 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3775 if (mask_size > 1) {
3776 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3777 } else {
3778 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3782 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3783 * Calculate the depth as dst.x / dst.y */
3784 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3786 struct glsl_dst_param dst_param;
3788 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3790 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3791 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3792 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3793 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3794 * >= 1.0 or < 0.0
3796 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3797 dst_param.reg_name, dst_param.reg_name);
3800 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3801 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3802 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3803 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3805 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3807 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3808 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3809 struct glsl_src_param src0_param;
3811 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3813 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3814 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3817 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3818 * Calculate the 1st of a 2-row matrix multiplication. */
3819 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3821 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3822 DWORD reg = ins->dst[0].reg.idx[0].offset;
3823 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3824 struct glsl_src_param src0_param;
3826 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3827 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3830 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3831 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3832 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3834 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3835 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3836 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3837 DWORD reg = ins->dst[0].reg.idx[0].offset;
3838 struct glsl_src_param src0_param;
3840 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3841 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
3842 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
3845 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3847 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3848 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3849 struct glsl_sample_function sample_function;
3850 DWORD reg = ins->dst[0].reg.idx[0].offset;
3851 struct glsl_src_param src0_param;
3853 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3854 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3856 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3858 /* Sample the texture using the calculated coordinates */
3859 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
3862 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3863 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3864 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3866 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3867 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3868 struct glsl_sample_function sample_function;
3869 DWORD reg = ins->dst[0].reg.idx[0].offset;
3870 struct glsl_src_param src0_param;
3872 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3873 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3875 /* Dependent read, not valid with conditional NP2 */
3876 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3878 /* Sample the texture using the calculated coordinates */
3879 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3881 tex_mx->current_row = 0;
3884 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3885 * Perform the 3rd row of a 3x3 matrix multiply */
3886 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
3888 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3889 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3890 DWORD reg = ins->dst[0].reg.idx[0].offset;
3891 struct glsl_src_param src0_param;
3892 char dst_mask[6];
3894 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3896 shader_glsl_append_dst(ins->ctx->buffer, ins);
3897 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3898 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
3900 tex_mx->current_row = 0;
3903 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3904 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3905 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
3907 struct glsl_src_param src0_param;
3908 struct glsl_src_param src1_param;
3909 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3910 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3911 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3912 struct glsl_sample_function sample_function;
3913 DWORD reg = ins->dst[0].reg.idx[0].offset;
3914 char coord_mask[6];
3916 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3917 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3919 /* Perform the last matrix multiply operation */
3920 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3921 /* Reflection calculation */
3922 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
3924 /* Dependent read, not valid with conditional NP2 */
3925 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3926 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3928 /* Sample the texture */
3929 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3930 NULL, NULL, NULL, "tmp0%s", coord_mask);
3932 tex_mx->current_row = 0;
3935 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3936 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3937 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
3939 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3940 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3941 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3942 struct glsl_sample_function sample_function;
3943 DWORD reg = ins->dst[0].reg.idx[0].offset;
3944 struct glsl_src_param src0_param;
3945 char coord_mask[6];
3947 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3949 /* Perform the last matrix multiply operation */
3950 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
3952 /* Construct the eye-ray vector from w coordinates */
3953 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3954 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
3955 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3957 /* Dependent read, not valid with conditional NP2 */
3958 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3959 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3961 /* Sample the texture using the calculated coordinates */
3962 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3963 NULL, NULL, NULL, "tmp0%s", coord_mask);
3965 tex_mx->current_row = 0;
3968 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3969 * Apply a fake bump map transform.
3970 * texbem is pshader <= 1.3 only, this saves a few version checks
3972 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
3974 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3975 struct glsl_sample_function sample_function;
3976 struct glsl_src_param coord_param;
3977 DWORD sampler_idx;
3978 DWORD mask;
3979 DWORD flags;
3980 char coord_mask[6];
3982 sampler_idx = ins->dst[0].reg.idx[0].offset;
3983 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3984 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3986 /* Dependent read, not valid with conditional NP2 */
3987 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3988 mask = sample_function.coord_mask;
3990 shader_glsl_write_mask_to_str(mask, coord_mask);
3992 /* With projected textures, texbem only divides the static texture coord,
3993 * not the displacement, so we can't let GL handle this. */
3994 if (flags & WINED3D_PSARGS_PROJECTED)
3996 DWORD div_mask=0;
3997 char coord_div_mask[3];
3998 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4000 case WINED3D_TTFF_COUNT1:
4001 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4002 break;
4003 case WINED3D_TTFF_COUNT2:
4004 div_mask = WINED3DSP_WRITEMASK_1;
4005 break;
4006 case WINED3D_TTFF_COUNT3:
4007 div_mask = WINED3DSP_WRITEMASK_2;
4008 break;
4009 case WINED3D_TTFF_COUNT4:
4010 case WINED3D_TTFF_DISABLE:
4011 div_mask = WINED3DSP_WRITEMASK_3;
4012 break;
4014 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4015 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4018 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4020 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4021 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4022 coord_param.param_str, coord_mask);
4024 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4026 struct glsl_src_param luminance_param;
4027 struct glsl_dst_param dst_param;
4029 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4030 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4032 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4033 dst_param.reg_name, dst_param.mask_str,
4034 luminance_param.param_str, sampler_idx, sampler_idx);
4038 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4040 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4041 struct glsl_src_param src0_param, src1_param;
4043 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4044 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4046 shader_glsl_append_dst(ins->ctx->buffer, ins);
4047 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4048 src0_param.param_str, sampler_idx, src1_param.param_str);
4051 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4052 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4053 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4055 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4056 struct glsl_sample_function sample_function;
4057 struct glsl_src_param src0_param;
4059 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4061 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4062 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4063 "%s.wx", src0_param.reg_name);
4066 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4067 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4068 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4070 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4071 struct glsl_sample_function sample_function;
4072 struct glsl_src_param src0_param;
4074 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4076 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4077 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4078 "%s.yz", src0_param.reg_name);
4081 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4082 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4083 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4085 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4086 struct glsl_sample_function sample_function;
4087 struct glsl_src_param src0_param;
4089 /* Dependent read, not valid with conditional NP2 */
4090 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4091 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4093 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4094 "%s", src0_param.param_str);
4097 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4098 * If any of the first 3 components are < 0, discard this pixel */
4099 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4101 struct glsl_dst_param dst_param;
4103 /* The argument is a destination parameter, and no writemasks are allowed */
4104 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4105 if (ins->ctx->reg_maps->shader_version.major >= 2)
4107 if (ins->ctx->reg_maps->shader_version.major >= 4)
4108 FIXME("SM4 discard not implemented.\n");
4109 /* 2.0 shaders compare all 4 components in texkill */
4110 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4111 } else {
4112 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
4113 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
4114 * 4 components are defined, only the first 3 are used
4116 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4120 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4121 * dst = dot2(src0, src1) + src2 */
4122 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4124 struct glsl_src_param src0_param;
4125 struct glsl_src_param src1_param;
4126 struct glsl_src_param src2_param;
4127 DWORD write_mask;
4128 unsigned int mask_size;
4130 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4131 mask_size = shader_glsl_get_write_mask_size(write_mask);
4133 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4134 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4135 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4137 if (mask_size > 1) {
4138 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4139 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4140 } else {
4141 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4142 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4146 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
4147 const struct wined3d_shader_signature *input_signature,
4148 const struct wined3d_shader_reg_maps *reg_maps,
4149 enum vertexprocessing_mode vertexprocessing)
4151 unsigned int i;
4153 for (i = 0; i < input_signature->element_count; ++i)
4155 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4156 const char *semantic_name;
4157 UINT semantic_idx;
4158 char reg_mask[6];
4160 /* Unused */
4161 if (!(reg_maps->input_registers & (1 << input->register_idx)))
4162 continue;
4164 semantic_name = input->semantic_name;
4165 semantic_idx = input->semantic_idx;
4166 shader_glsl_write_mask_to_str(input->mask, reg_mask);
4168 if (vertexprocessing == vertexshader)
4170 if (!strcmp(semantic_name, "SV_POSITION") && !semantic_idx)
4171 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
4172 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4173 else
4174 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
4175 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
4176 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
4178 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4180 if (semantic_idx < 8 && vertexprocessing == pretransformed)
4181 shader_addline(buffer, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4182 shader->u.ps.input_reg_map[input->register_idx], reg_mask, semantic_idx, reg_mask);
4183 else
4184 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4185 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4187 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4189 if (!semantic_idx)
4190 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4191 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4192 else if (semantic_idx == 1)
4193 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4194 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4195 else
4196 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4197 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4199 else
4201 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4202 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4207 /*********************************************
4208 * Vertex Shader Specific Code begins here
4209 ********************************************/
4211 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4213 struct glsl_program_key key;
4215 key.vs_id = entry->vs.id;
4216 key.gs_id = entry->gs.id;
4217 key.ps_id = entry->ps.id;
4219 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4221 ERR("Failed to insert program entry.\n");
4225 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4226 GLuint vs_id, GLuint gs_id, GLuint ps_id)
4228 struct wine_rb_entry *entry;
4229 struct glsl_program_key key;
4231 key.vs_id = vs_id;
4232 key.gs_id = gs_id;
4233 key.ps_id = ps_id;
4235 entry = wine_rb_get(&priv->program_lookup, &key);
4236 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4239 /* Context activation is done by the caller. */
4240 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4241 struct glsl_shader_prog_link *entry)
4243 struct glsl_program_key key;
4245 key.vs_id = entry->vs.id;
4246 key.gs_id = entry->gs.id;
4247 key.ps_id = entry->ps.id;
4248 wine_rb_remove(&priv->program_lookup, &key);
4250 GL_EXTCALL(glDeleteProgram(entry->id));
4251 if (entry->vs.id)
4252 list_remove(&entry->vs.shader_entry);
4253 if (entry->gs.id)
4254 list_remove(&entry->gs.shader_entry);
4255 if (entry->ps.id)
4256 list_remove(&entry->ps.shader_entry);
4257 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4258 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4259 HeapFree(GetProcessHeap(), 0, entry);
4262 static void handle_ps3_input(struct wined3d_shader_buffer *buffer,
4263 const struct wined3d_gl_info *gl_info, const DWORD *map,
4264 const struct wined3d_shader_signature *input_signature,
4265 const struct wined3d_shader_reg_maps *reg_maps_in,
4266 const struct wined3d_shader_signature *output_signature,
4267 const struct wined3d_shader_reg_maps *reg_maps_out)
4269 unsigned int i, j;
4270 DWORD *set;
4271 DWORD in_idx;
4272 unsigned int in_count = vec4_varyings(3, gl_info);
4273 char reg_mask[6];
4274 char destination[50];
4276 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
4278 for (i = 0; i < input_signature->element_count; ++i)
4280 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4282 if (!(reg_maps_in->input_registers & (1 << input->register_idx)))
4283 continue;
4285 in_idx = map[input->register_idx];
4286 /* Declared, but not read register */
4287 if (in_idx == ~0u)
4288 continue;
4289 if (in_idx >= (in_count + 2))
4291 FIXME("More input varyings declared than supported, expect issues.\n");
4292 continue;
4295 if (in_idx == in_count)
4296 sprintf(destination, "gl_FrontColor");
4297 else if (in_idx == in_count + 1)
4298 sprintf(destination, "gl_FrontSecondaryColor");
4299 else
4300 sprintf(destination, "ps_link[%u]", in_idx);
4302 if (!set[in_idx])
4303 set[in_idx] = ~0u;
4305 for (j = 0; j < output_signature->element_count; ++j)
4307 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
4308 DWORD mask;
4310 if (!(reg_maps_out->output_registers & (1 << output->register_idx))
4311 || input->semantic_idx != output->semantic_idx
4312 || strcmp(input->semantic_name, output->semantic_name)
4313 || !(mask = input->mask & output->mask))
4314 continue;
4316 if (set[in_idx] == ~0u)
4317 set[in_idx] = mask;
4318 else
4319 set[in_idx] |= mask;
4320 shader_glsl_write_mask_to_str(mask, reg_mask);
4322 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4323 destination, reg_mask, output->register_idx, reg_mask);
4327 for (i = 0; i < in_count + 2; ++i)
4329 unsigned int size;
4331 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4332 continue;
4334 if (set[i] == ~0U) set[i] = 0;
4336 size = 0;
4337 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4338 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4339 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4340 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4341 reg_mask[size] = '\0';
4343 if (i == in_count)
4344 sprintf(destination, "gl_FrontColor");
4345 else if (i == in_count + 1)
4346 sprintf(destination, "gl_FrontSecondaryColor");
4347 else
4348 sprintf(destination, "ps_link[%u]", i);
4350 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
4351 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
4354 HeapFree(GetProcessHeap(), 0, set);
4357 /* Context activation is done by the caller. */
4358 static GLuint generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
4359 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4360 const struct wined3d_gl_info *gl_info)
4362 GLuint ret = 0;
4363 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4364 unsigned int i;
4365 const char *semantic_name;
4366 UINT semantic_idx;
4367 char reg_mask[6];
4369 shader_buffer_clear(buffer);
4371 shader_addline(buffer, "#version 120\n");
4373 if (ps_major < 3)
4375 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4377 for (i = 0; i < vs->output_signature.element_count; ++i)
4379 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
4380 DWORD write_mask;
4382 if (!(vs->reg_maps.output_registers & (1 << output->register_idx)))
4383 continue;
4385 semantic_name = output->semantic_name;
4386 semantic_idx = output->semantic_idx;
4387 write_mask = output->mask;
4388 shader_glsl_write_mask_to_str(write_mask, reg_mask);
4390 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4392 if (!semantic_idx)
4393 shader_addline(buffer, "gl_FrontColor%s = vs_out[%u]%s;\n",
4394 reg_mask, output->register_idx, reg_mask);
4395 else if (semantic_idx == 1)
4396 shader_addline(buffer, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4397 reg_mask, output->register_idx, reg_mask);
4399 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
4401 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4402 reg_mask, output->register_idx, reg_mask);
4404 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4406 if (semantic_idx < 8)
4408 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
4409 write_mask |= WINED3DSP_WRITEMASK_3;
4411 shader_addline(buffer, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4412 semantic_idx, reg_mask, output->register_idx, reg_mask);
4413 if (!(write_mask & WINED3DSP_WRITEMASK_3))
4414 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
4417 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4419 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", output->register_idx, reg_mask[1]);
4421 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
4423 shader_addline(buffer, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n",
4424 output->register_idx, reg_mask[1]);
4427 shader_addline(buffer, "}\n");
4429 else
4431 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
4432 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4433 shader_addline(buffer, "varying vec4 ps_link[%u];\n", in_count);
4434 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4436 /* First, sort out position and point size. Those are not passed to the pixel shader */
4437 for (i = 0; i < vs->output_signature.element_count; ++i)
4439 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
4441 if (!(vs->reg_maps.output_registers & (1 << output->register_idx)))
4442 continue;
4444 semantic_name = output->semantic_name;
4445 semantic_idx = output->semantic_idx;
4446 shader_glsl_write_mask_to_str(output->mask, reg_mask);
4448 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
4450 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4451 reg_mask, output->register_idx, reg_mask);
4453 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4455 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", output->register_idx, reg_mask[1]);
4459 /* Then, fix the pixel shader input */
4460 handle_ps3_input(buffer, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
4461 &ps->reg_maps, &vs->output_signature, &vs->reg_maps);
4463 shader_addline(buffer, "}\n");
4466 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
4467 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
4468 shader_glsl_compile(gl_info, ret, buffer->buffer);
4470 return ret;
4473 static void shader_glsl_generate_srgb_write_correction(struct wined3d_shader_buffer *buffer)
4475 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4476 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4477 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4478 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4479 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4480 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4483 static void shader_glsl_generate_fog_code(struct wined3d_shader_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
4485 switch (mode)
4487 case WINED3D_FFP_PS_FOG_OFF:
4488 return;
4490 case WINED3D_FFP_PS_FOG_LINEAR:
4491 shader_addline(buffer, "float Fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;\n");
4492 break;
4494 case WINED3D_FFP_PS_FOG_EXP:
4495 /* Fog = e^-(gl_Fog.density * gl_FogFragCoord) */
4496 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4497 break;
4499 case WINED3D_FFP_PS_FOG_EXP2:
4500 /* Fog = e^-((gl_Fog.density * gl_FogFragCoord)^2) */
4501 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4502 break;
4504 default:
4505 ERR("Invalid fog mode %#x.\n", mode);
4506 return;
4509 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, clamp(Fog, 0.0, 1.0));\n");
4512 /* Context activation is done by the caller. */
4513 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
4514 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4515 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
4517 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4518 const struct wined3d_gl_info *gl_info = context->gl_info;
4519 const DWORD *function = shader->function;
4520 struct shader_glsl_ctx_priv priv_ctx;
4522 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4523 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
4525 memset(&priv_ctx, 0, sizeof(priv_ctx));
4526 priv_ctx.cur_ps_args = args;
4527 priv_ctx.cur_np2fixup_info = np2fixup_info;
4529 shader_addline(buffer, "#version 120\n");
4531 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4532 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4533 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4534 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
4535 /* The spec says that it doesn't have to be explicitly enabled, but the
4536 * nvidia drivers write a warning if we don't do so. */
4537 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4538 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4539 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4540 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4541 if (gl_info->supported[EXT_GPU_SHADER4])
4542 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4544 /* Base Declarations */
4545 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4547 /* Pack 3.0 inputs */
4548 if (reg_maps->shader_version.major >= 3)
4549 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args->vp_mode);
4551 /* Base Shader Body */
4552 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4554 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4555 if (reg_maps->shader_version.major < 2)
4557 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4558 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4561 if (args->srgb_correction)
4562 shader_glsl_generate_srgb_write_correction(buffer);
4564 /* SM < 3 does not replace the fog stage. */
4565 if (reg_maps->shader_version.major < 3)
4566 shader_glsl_generate_fog_code(buffer, args->fog);
4568 shader_addline(buffer, "}\n");
4570 TRACE("Compiling shader object %u.\n", shader_id);
4571 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4573 return shader_id;
4576 /* Context activation is done by the caller. */
4577 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4578 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4579 const struct vs_compile_args *args)
4581 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4582 const struct wined3d_gl_info *gl_info = context->gl_info;
4583 const DWORD *function = shader->function;
4584 struct shader_glsl_ctx_priv priv_ctx;
4586 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4587 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
4589 shader_addline(buffer, "#version 120\n");
4591 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4592 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4593 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4594 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4595 if (gl_info->supported[EXT_GPU_SHADER4])
4596 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4598 memset(&priv_ctx, 0, sizeof(priv_ctx));
4599 priv_ctx.cur_vs_args = args;
4601 /* Base Declarations */
4602 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4604 /* Base Shader Body */
4605 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4607 /* Unpack outputs */
4608 shader_addline(buffer, "order_ps_input(vs_out);\n");
4610 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4611 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4612 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4613 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4615 if (args->fog_src == VS_FOG_Z)
4616 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4617 else if (!reg_maps->fog)
4618 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4620 /* We always store the clipplanes without y inversion */
4621 if (args->clip_enabled)
4622 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4624 /* Write the final position.
4626 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4627 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4628 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4629 * contains 1.0 to allow a mad.
4631 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4632 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4634 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4636 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4637 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4638 * which is the same as z = z * 2 - w.
4640 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4642 shader_addline(buffer, "}\n");
4644 TRACE("Compiling shader object %u.\n", shader_id);
4645 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4647 return shader_id;
4650 /* Context activation is done by the caller. */
4651 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
4652 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader)
4654 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4655 const struct wined3d_gl_info *gl_info = context->gl_info;
4656 const DWORD *function = shader->function;
4657 struct shader_glsl_ctx_priv priv_ctx;
4658 GLuint shader_id;
4660 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
4662 shader_addline(buffer, "#version 120\n");
4664 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
4665 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
4666 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4667 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4668 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4669 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4670 if (gl_info->supported[EXT_GPU_SHADER4])
4671 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4673 memset(&priv_ctx, 0, sizeof(priv_ctx));
4674 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4675 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4676 shader_addline(buffer, "}\n");
4678 TRACE("Compiling shader object %u.\n", shader_id);
4679 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4681 return shader_id;
4684 static GLuint find_glsl_pshader(const struct wined3d_context *context,
4685 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4686 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
4688 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
4689 struct glsl_shader_private *shader_data;
4690 struct ps_np2fixup_info *np2fixup;
4691 UINT i;
4692 DWORD new_size;
4693 GLuint ret;
4695 if (!shader->backend_data)
4697 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4698 if (!shader->backend_data)
4700 ERR("Failed to allocate backend data.\n");
4701 return 0;
4704 shader_data = shader->backend_data;
4705 gl_shaders = shader_data->gl_shaders.ps;
4707 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4708 * so a linear search is more performant than a hashmap or a binary search
4709 * (cache coherency etc)
4711 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4713 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
4715 if (args->np2_fixup)
4716 *np2fixup_info = &gl_shaders[i].np2fixup;
4717 return gl_shaders[i].id;
4721 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4722 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4723 if (shader_data->num_gl_shaders)
4725 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4726 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
4727 new_size * sizeof(*gl_shaders));
4729 else
4731 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4732 new_size = 1;
4735 if(!new_array) {
4736 ERR("Out of memory\n");
4737 return 0;
4739 shader_data->gl_shaders.ps = new_array;
4740 shader_data->shader_array_size = new_size;
4741 gl_shaders = new_array;
4744 gl_shaders[shader_data->num_gl_shaders].args = *args;
4746 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
4747 memset(np2fixup, 0, sizeof(*np2fixup));
4748 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
4750 pixelshader_update_resource_types(shader, args->tex_types);
4752 shader_buffer_clear(buffer);
4753 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4754 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4756 return ret;
4759 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4760 const DWORD use_map) {
4761 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4762 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4763 return stored->fog_src == new->fog_src;
4766 static GLuint find_glsl_vshader(const struct wined3d_context *context,
4767 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4768 const struct vs_compile_args *args)
4770 UINT i;
4771 DWORD new_size;
4772 DWORD use_map = context->stream_info.use_map;
4773 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
4774 struct glsl_shader_private *shader_data;
4775 GLuint ret;
4777 if (!shader->backend_data)
4779 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4780 if (!shader->backend_data)
4782 ERR("Failed to allocate backend data.\n");
4783 return 0;
4786 shader_data = shader->backend_data;
4787 gl_shaders = shader_data->gl_shaders.vs;
4789 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4790 * so a linear search is more performant than a hashmap or a binary search
4791 * (cache coherency etc)
4793 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4795 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
4796 return gl_shaders[i].id;
4799 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4801 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4802 if (shader_data->num_gl_shaders)
4804 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4805 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
4806 new_size * sizeof(*gl_shaders));
4808 else
4810 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4811 new_size = 1;
4814 if(!new_array) {
4815 ERR("Out of memory\n");
4816 return 0;
4818 shader_data->gl_shaders.vs = new_array;
4819 shader_data->shader_array_size = new_size;
4820 gl_shaders = new_array;
4823 gl_shaders[shader_data->num_gl_shaders].args = *args;
4825 shader_buffer_clear(buffer);
4826 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4827 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4829 return ret;
4832 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
4833 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader)
4835 struct glsl_gs_compiled_shader *gl_shaders;
4836 struct glsl_shader_private *shader_data;
4837 GLuint ret;
4839 if (!shader->backend_data)
4841 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
4843 ERR("Failed to allocate backend data.\n");
4844 return 0;
4847 shader_data = shader->backend_data;
4848 gl_shaders = shader_data->gl_shaders.gs;
4850 if (shader_data->num_gl_shaders)
4851 return gl_shaders[0].id;
4853 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4855 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
4857 ERR("Failed to allocate GL shader array.\n");
4858 return 0;
4860 shader_data->shader_array_size = 1;
4861 gl_shaders = shader_data->gl_shaders.gs;
4863 shader_buffer_clear(buffer);
4864 ret = shader_glsl_generate_geometry_shader(context, buffer, shader);
4865 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4867 return ret;
4870 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
4872 switch (mcs)
4874 case WINED3D_MCS_MATERIAL:
4875 return material;
4876 case WINED3D_MCS_COLOR1:
4877 return "gl_Color";
4878 case WINED3D_MCS_COLOR2:
4879 return "gl_SecondaryColor";
4880 default:
4881 ERR("Invalid material color source %#x.\n", mcs);
4882 return "<invalid>";
4886 static void shader_glsl_ffp_vertex_lighting(struct wined3d_shader_buffer *buffer,
4887 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4889 const char *diffuse, *specular, *emission, *ambient;
4890 enum wined3d_light_type light_type;
4891 unsigned int i;
4893 if (!settings->lighting)
4895 shader_addline(buffer, "gl_FrontColor = gl_Color;\n");
4896 shader_addline(buffer, "gl_FrontSecondaryColor = gl_SecondaryColor;\n");
4897 return;
4900 shader_addline(buffer, "vec3 ambient = gl_LightModel.ambient.xyz;\n");
4901 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
4902 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
4903 shader_addline(buffer, "vec3 dir, dst;\n");
4904 shader_addline(buffer, "float att, t;\n");
4906 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "gl_FrontMaterial.ambient");
4907 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "gl_FrontMaterial.diffuse");
4908 specular = shader_glsl_ffp_mcs(settings->specular_source, "gl_FrontMaterial.specular");
4909 emission = shader_glsl_ffp_mcs(settings->emission_source, "gl_FrontMaterial.emission");
4911 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
4913 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
4914 switch (light_type)
4916 case WINED3D_LIGHT_POINT:
4917 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4918 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4919 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4920 shader_addline(buffer, "dst.x = 1.0;\n");
4921 shader_addline(buffer, "att = dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4922 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n", i, i, i);
4923 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz / att;\n", i);
4924 if (!settings->normal)
4925 break;
4926 shader_addline(buffer, "dir = normalize(dir);\n");
4927 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
4928 " * gl_LightSource[%u].diffuse.xyz) / att;\n", i);
4929 if (settings->localviewer)
4930 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4931 else
4932 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4933 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4934 " * gl_LightSource[%u].specular) / att;\n", i);
4935 break;
4937 case WINED3D_LIGHT_SPOT:
4938 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4939 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4940 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4941 shader_addline(buffer, "dst.x = 1.0;\n");
4942 shader_addline(buffer, "dir = normalize(dir);\n");
4943 shader_addline(buffer, "t = dot(-dir, normalize(gl_LightSource[%u].spotDirection));\n", i);
4944 shader_addline(buffer, "if (t < gl_LightSource[%u].spotCosCutoff) att = 0.0;\n", i);
4945 shader_addline(buffer, "else att = pow(t, gl_LightSource[%u].spotExponent)"
4946 " / dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4947 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n",
4948 i, i, i, i);
4949 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz * att;\n", i);
4950 if (!settings->normal)
4951 break;
4952 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
4953 " * gl_LightSource[%u].diffuse.xyz) * att;\n", i);
4954 if (settings->localviewer)
4955 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4956 else
4957 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4958 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4959 " * gl_LightSource[%u].specular) * att;\n", i);
4960 break;
4962 case WINED3D_LIGHT_DIRECTIONAL:
4963 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz;\n", i);
4964 if (!settings->normal)
4965 break;
4966 shader_addline(buffer, "dir = normalize(gl_LightSource[%u].position.xyz);\n", i);
4967 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
4968 " * gl_LightSource[%u].diffuse.xyz;\n", i);
4969 shader_addline(buffer, "t = dot(normal, gl_LightSource[%u].halfVector.xyz);\n", i);
4970 shader_addline(buffer, "if (t > 0.0) specular += pow(t, gl_FrontMaterial.shininess)"
4971 " * gl_LightSource[%u].specular;\n", i);
4972 break;
4974 default:
4975 if (light_type)
4976 FIXME("Unhandled light type %#x.\n", light_type);
4977 continue;
4981 shader_addline(buffer, "gl_FrontColor.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
4982 ambient, diffuse, emission);
4983 shader_addline(buffer, "gl_FrontColor.w = %s.w;\n", diffuse);
4984 shader_addline(buffer, "gl_FrontSecondaryColor = %s * specular;\n", specular);
4987 /* Context activation is done by the caller. */
4988 static GLuint shader_glsl_generate_ffp_vertex_shader(struct wined3d_shader_buffer *buffer,
4989 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4991 GLuint shader_obj;
4992 unsigned int i;
4994 shader_buffer_clear(buffer);
4996 shader_addline(buffer, "#version 120\n");
4997 shader_addline(buffer, "\n");
4999 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix;\n");
5001 shader_addline(buffer, "\nvoid main()\n{\n");
5002 shader_addline(buffer, "float m;\n");
5003 shader_addline(buffer, "vec3 r;\n");
5005 if (settings->transformed)
5007 shader_addline(buffer, "vec4 ec_pos = vec4(gl_Vertex.xyz, 1.0);\n");
5008 shader_addline(buffer, "gl_Position = gl_ProjectionMatrix * ec_pos;\n");
5009 shader_addline(buffer, "if (gl_Vertex.w != 0.0) gl_Position /= gl_Vertex.w;\n");
5011 else
5013 shader_addline(buffer, "vec4 ec_pos = ffp_modelview_matrix * gl_Vertex;\n");
5014 shader_addline(buffer, "gl_Position = gl_ProjectionMatrix * ec_pos;\n");
5015 if (settings->clipping)
5016 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
5017 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
5020 if (!settings->normal)
5021 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
5022 else if (settings->normalize)
5023 shader_addline(buffer, "vec3 normal = normalize(gl_NormalMatrix * gl_Normal);\n");
5024 else
5025 shader_addline(buffer, "vec3 normal = gl_NormalMatrix * gl_Normal;\n");
5027 shader_glsl_ffp_vertex_lighting(buffer, settings, gl_info);
5029 for (i = 0; i < MAX_TEXTURES; ++i)
5031 switch (settings->texgen[i] << WINED3D_FFP_TCI_SHIFT)
5033 case WINED3DTSS_TCI_PASSTHRU:
5034 if (settings->texcoords & (1 << i))
5035 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * gl_MultiTexCoord%d;\n",
5036 i, i, i);
5037 break;
5039 case WINED3DTSS_TCI_CAMERASPACENORMAL:
5040 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * vec4(normal, 1.0);\n", i, i);
5041 break;
5043 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
5044 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * ec_pos;\n", i, i);
5045 break;
5047 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
5048 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
5049 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
5050 break;
5052 case WINED3DTSS_TCI_SPHEREMAP:
5053 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
5054 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
5055 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
5056 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
5057 break;
5059 default:
5060 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
5061 break;
5065 switch (settings->fog_mode)
5067 case WINED3D_FFP_VS_FOG_OFF:
5068 break;
5070 case WINED3D_FFP_VS_FOG_FOGCOORD:
5071 shader_addline(buffer, "gl_FogFragCoord = gl_SecondaryColor.w * 255.0;\n");
5072 break;
5074 case WINED3D_FFP_VS_FOG_RANGE:
5075 shader_addline(buffer, "gl_FogFragCoord = length(ec_pos.xyz);\n");
5076 break;
5078 case WINED3D_FFP_VS_FOG_DEPTH:
5079 if (settings->ortho_fog)
5080 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
5081 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z * 0.5 + 0.5;\n");
5082 else if (settings->transformed)
5083 shader_addline(buffer, "gl_FogFragCoord = ec_pos.z;\n");
5084 else
5085 shader_addline(buffer, "gl_FogFragCoord = abs(ec_pos.z);\n");
5086 break;
5088 default:
5089 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
5090 break;
5093 if (settings->point_size)
5095 shader_addline(buffer, "gl_PointSize = gl_Point.size / sqrt(gl_Point.distanceConstantAttenuation"
5096 " + gl_Point.distanceLinearAttenuation * length(ec_pos.xyz)"
5097 " + gl_Point.distanceQuadraticAttenuation * dot(ec_pos.xyz, ec_pos.xyz));\n");
5098 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, gl_Point.sizeMin, gl_Point.sizeMax);\n");
5101 shader_addline(buffer, "}\n");
5103 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5104 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5106 return shader_obj;
5109 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_shader_buffer *buffer,
5110 DWORD argnum, unsigned int stage, DWORD arg)
5112 const char *ret;
5114 if (arg == ARG_UNUSED)
5115 return "<unused arg>";
5117 switch (arg & WINED3DTA_SELECTMASK)
5119 case WINED3DTA_DIFFUSE:
5120 ret = "gl_Color";
5121 break;
5123 case WINED3DTA_CURRENT:
5124 if (!stage)
5125 ret = "gl_Color";
5126 else
5127 ret = "ret";
5128 break;
5130 case WINED3DTA_TEXTURE:
5131 switch (stage)
5133 case 0: ret = "tex0"; break;
5134 case 1: ret = "tex1"; break;
5135 case 2: ret = "tex2"; break;
5136 case 3: ret = "tex3"; break;
5137 case 4: ret = "tex4"; break;
5138 case 5: ret = "tex5"; break;
5139 case 6: ret = "tex6"; break;
5140 case 7: ret = "tex7"; break;
5141 default:
5142 ret = "<invalid texture>";
5143 break;
5145 break;
5147 case WINED3DTA_TFACTOR:
5148 ret = "tex_factor";
5149 break;
5151 case WINED3DTA_SPECULAR:
5152 ret = "gl_SecondaryColor";
5153 break;
5155 case WINED3DTA_TEMP:
5156 ret = "temp_reg";
5157 break;
5159 case WINED3DTA_CONSTANT:
5160 switch (stage)
5162 case 0: ret = "tss_const0"; break;
5163 case 1: ret = "tss_const1"; break;
5164 case 2: ret = "tss_const2"; break;
5165 case 3: ret = "tss_const3"; break;
5166 case 4: ret = "tss_const4"; break;
5167 case 5: ret = "tss_const5"; break;
5168 case 6: ret = "tss_const6"; break;
5169 case 7: ret = "tss_const7"; break;
5170 default:
5171 ret = "<invalid constant>";
5172 break;
5174 break;
5176 default:
5177 return "<unhandled arg>";
5180 if (arg & WINED3DTA_COMPLEMENT)
5182 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
5183 if (argnum == 0)
5184 ret = "arg0";
5185 else if (argnum == 1)
5186 ret = "arg1";
5187 else if (argnum == 2)
5188 ret = "arg2";
5191 if (arg & WINED3DTA_ALPHAREPLICATE)
5193 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
5194 if (argnum == 0)
5195 ret = "arg0";
5196 else if (argnum == 1)
5197 ret = "arg1";
5198 else if (argnum == 2)
5199 ret = "arg2";
5202 return ret;
5205 static void shader_glsl_ffp_fragment_op(struct wined3d_shader_buffer *buffer, unsigned int stage, BOOL color,
5206 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
5208 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5210 if (color && alpha)
5211 dstmask = "";
5212 else if (color)
5213 dstmask = ".xyz";
5214 else
5215 dstmask = ".w";
5217 if (dst == tempreg)
5218 dstreg = "temp_reg";
5219 else
5220 dstreg = "ret";
5222 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
5223 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
5224 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
5226 switch (op)
5228 case WINED3D_TOP_DISABLE:
5229 if (!stage)
5230 shader_addline(buffer, "%s%s = gl_Color%s;\n", dstreg, dstmask, dstmask);
5231 break;
5233 case WINED3D_TOP_SELECT_ARG1:
5234 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
5235 break;
5237 case WINED3D_TOP_SELECT_ARG2:
5238 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
5239 break;
5241 case WINED3D_TOP_MODULATE:
5242 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5243 break;
5245 case WINED3D_TOP_MODULATE_4X:
5246 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
5247 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5248 break;
5250 case WINED3D_TOP_MODULATE_2X:
5251 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
5252 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5253 break;
5255 case WINED3D_TOP_ADD:
5256 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
5257 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5258 break;
5260 case WINED3D_TOP_ADD_SIGNED:
5261 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
5262 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5263 break;
5265 case WINED3D_TOP_ADD_SIGNED_2X:
5266 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
5267 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5268 break;
5270 case WINED3D_TOP_SUBTRACT:
5271 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
5272 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5273 break;
5275 case WINED3D_TOP_ADD_SMOOTH:
5276 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
5277 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
5278 break;
5280 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
5281 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5282 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5283 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5284 break;
5286 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5287 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5288 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5289 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5290 break;
5292 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5293 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
5294 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5295 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5296 break;
5298 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5299 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5300 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5301 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
5302 break;
5304 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
5305 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
5306 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5307 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5308 break;
5310 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
5311 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
5312 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5313 break;
5315 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
5316 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
5317 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5318 break;
5320 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
5321 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5322 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5323 break;
5324 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
5325 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
5326 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5327 break;
5329 case WINED3D_TOP_BUMPENVMAP:
5330 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5331 /* These are handled in the first pass, nothing to do. */
5332 break;
5334 case WINED3D_TOP_DOTPRODUCT3:
5335 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
5336 dstreg, dstmask, arg1, arg2, dstmask);
5337 break;
5339 case WINED3D_TOP_MULTIPLY_ADD:
5340 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
5341 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
5342 break;
5344 case WINED3D_TOP_LERP:
5345 /* MSDN isn't quite right here. */
5346 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
5347 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
5348 break;
5350 default:
5351 FIXME("Unhandled operation %#x.\n", op);
5352 break;
5356 /* Context activation is done by the caller. */
5357 static GLuint shader_glsl_generate_ffp_fragment_shader(struct wined3d_shader_buffer *buffer,
5358 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
5360 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
5361 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
5362 const char *final_combiner_src = "ret";
5363 UINT lowest_disabled_stage;
5364 GLuint shader_id;
5365 DWORD arg0, arg1, arg2;
5366 unsigned int stage;
5368 shader_buffer_clear(buffer);
5370 /* Find out which textures are read */
5371 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5373 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5374 break;
5376 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
5377 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
5378 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
5380 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5381 tex_map |= 1 << stage;
5382 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5383 tfactor_used = TRUE;
5384 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5385 tempreg_used = TRUE;
5386 if (settings->op[stage].dst == tempreg)
5387 tempreg_used = TRUE;
5388 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5389 tss_const_map |= 1 << stage;
5391 switch (settings->op[stage].cop)
5393 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5394 lum_map |= 1 << stage;
5395 /* fall through */
5396 case WINED3D_TOP_BUMPENVMAP:
5397 bump_map |= 1 << stage;
5398 /* fall through */
5399 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5400 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5401 tex_map |= 1 << stage;
5402 break;
5404 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5405 tfactor_used = TRUE;
5406 break;
5408 default:
5409 break;
5412 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5413 continue;
5415 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
5416 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
5417 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
5419 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5420 tex_map |= 1 << stage;
5421 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5422 tfactor_used = TRUE;
5423 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5424 tempreg_used = TRUE;
5425 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5426 tss_const_map |= 1 << stage;
5428 lowest_disabled_stage = stage;
5430 shader_addline(buffer, "#version 120\n");
5432 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5433 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5435 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
5436 shader_addline(buffer, "vec4 ret;\n");
5437 if (tempreg_used || settings->sRGB_write)
5438 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
5439 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
5441 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5443 if (tss_const_map & (1 << stage))
5444 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
5446 if (!(tex_map & (1 << stage)))
5447 continue;
5449 switch (settings->op[stage].tex_type)
5451 case tex_1d:
5452 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
5453 break;
5454 case tex_2d:
5455 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
5456 break;
5457 case tex_3d:
5458 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
5459 break;
5460 case tex_cube:
5461 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
5462 break;
5463 case tex_rect:
5464 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
5465 break;
5466 default:
5467 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
5468 break;
5471 shader_addline(buffer, "vec4 tex%u;\n", stage);
5473 if (!(bump_map & (1 << stage)))
5474 continue;
5475 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
5477 if (!(lum_map & (1 << stage)))
5478 continue;
5479 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
5480 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
5482 if (tfactor_used)
5483 shader_addline(buffer, "uniform vec4 tex_factor;\n");
5484 shader_addline(buffer, "uniform vec4 specular_enable;\n");
5486 if (settings->sRGB_write)
5488 shader_addline(buffer, "const vec4 srgb_const0 = ");
5489 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
5490 shader_addline(buffer, ";\n");
5491 shader_addline(buffer, "const vec4 srgb_const1 = ");
5492 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
5493 shader_addline(buffer, ";\n");
5496 shader_addline(buffer, "void main()\n{\n");
5498 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
5499 shader_addline(buffer, "if (any(lessThan(gl_TexCoord[7], vec4(0.0)))) discard;\n");
5501 /* Generate texture sampling instructions) */
5502 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
5504 const char *texture_function, *coord_mask;
5505 char tex_reg_name[8];
5506 BOOL proj;
5508 if (!(tex_map & (1 << stage)))
5509 continue;
5511 if (settings->op[stage].projected == proj_none)
5513 proj = FALSE;
5515 else if (settings->op[stage].projected == proj_count4
5516 || settings->op[stage].projected == proj_count3)
5518 proj = TRUE;
5520 else
5522 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
5523 proj = TRUE;
5526 switch (settings->op[stage].tex_type)
5528 case tex_1d:
5529 if (proj)
5531 texture_function = "texture1DProj";
5532 coord_mask = "xw";
5534 else
5536 texture_function = "texture1D";
5537 coord_mask = "x";
5539 break;
5540 case tex_2d:
5541 if (proj)
5543 texture_function = "texture2DProj";
5544 coord_mask = "xyw";
5546 else
5548 texture_function = "texture2D";
5549 coord_mask = "xy";
5551 break;
5552 case tex_3d:
5553 if (proj)
5555 texture_function = "texture3DProj";
5556 coord_mask = "xyzw";
5558 else
5560 texture_function = "texture3D";
5561 coord_mask = "xyz";
5563 break;
5564 case tex_cube:
5565 texture_function = "textureCube";
5566 coord_mask = "xyz";
5567 break;
5568 case tex_rect:
5569 if (proj)
5571 texture_function = "texture2DRectProj";
5572 coord_mask = "xyw";
5574 else
5576 texture_function = "texture2DRect";
5577 coord_mask = "xy";
5579 break;
5580 default:
5581 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
5582 texture_function = "";
5583 coord_mask = "xyzw";
5584 break;
5587 if (stage > 0
5588 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
5589 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
5591 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
5593 /* With projective textures, texbem only divides the static
5594 * texture coord, not the displacement, so multiply the
5595 * displacement with the dividing parameter before passing it to
5596 * TXP. */
5597 if (settings->op[stage].projected != proj_none)
5599 if (settings->op[stage].projected == proj_count4)
5601 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].w) + gl_TexCoord[%u].xy;\n",
5602 stage, stage);
5603 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].ww;\n", stage);
5605 else
5607 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].z) + gl_TexCoord[%u].xy;\n",
5608 stage, stage);
5609 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].zz;\n", stage);
5612 else
5614 shader_addline(buffer, "ret = gl_TexCoord[%u] + ret.xyxy;\n", stage);
5617 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
5618 stage, texture_function, stage, coord_mask);
5620 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
5621 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
5622 stage, stage - 1, stage - 1, stage - 1);
5624 else if (settings->op[stage].projected == proj_count3)
5626 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].xyz);\n",
5627 stage, texture_function, stage, stage);
5629 else
5631 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].%s);\n",
5632 stage, texture_function, stage, stage, coord_mask);
5635 sprintf(tex_reg_name, "tex%u", stage);
5636 shader_glsl_color_correction_ext(buffer, tex_reg_name, WINED3DSP_WRITEMASK_ALL,
5637 settings->op[stage].color_fixup);
5640 /* Generate the main shader */
5641 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5643 BOOL op_equal;
5645 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5647 if (!stage)
5648 final_combiner_src = "gl_Color";
5649 break;
5652 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5653 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5654 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
5655 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5656 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5657 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
5658 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5659 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5660 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
5661 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5662 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5663 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
5664 else
5665 op_equal = settings->op[stage].aop == settings->op[stage].cop
5666 && settings->op[stage].carg0 == settings->op[stage].aarg0
5667 && settings->op[stage].carg1 == settings->op[stage].aarg1
5668 && settings->op[stage].carg2 == settings->op[stage].aarg2;
5670 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5672 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5673 settings->op[stage].cop, settings->op[stage].carg0,
5674 settings->op[stage].carg1, settings->op[stage].carg2);
5675 if (!stage)
5676 shader_addline(buffer, "ret.w = gl_Color.w;\n");
5678 else if (op_equal)
5680 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
5681 settings->op[stage].cop, settings->op[stage].carg0,
5682 settings->op[stage].carg1, settings->op[stage].carg2);
5684 else
5686 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5687 settings->op[stage].cop, settings->op[stage].carg0,
5688 settings->op[stage].carg1, settings->op[stage].carg2);
5689 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
5690 settings->op[stage].aop, settings->op[stage].aarg0,
5691 settings->op[stage].aarg1, settings->op[stage].aarg2);
5695 shader_addline(buffer, "gl_FragData[0] = gl_SecondaryColor * specular_enable + %s;\n", final_combiner_src);
5697 if (settings->sRGB_write)
5698 shader_glsl_generate_srgb_write_correction(buffer);
5700 shader_glsl_generate_fog_code(buffer, settings->fog);
5702 shader_addline(buffer, "}\n");
5704 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5705 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5706 return shader_id;
5709 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
5710 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
5712 struct glsl_ffp_vertex_shader *shader;
5713 const struct wine_rb_entry *entry;
5715 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
5716 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
5718 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
5719 return NULL;
5721 shader->desc.settings = *settings;
5722 shader->id = shader_glsl_generate_ffp_vertex_shader(&priv->shader_buffer, settings, gl_info);
5723 list_init(&shader->linked_programs);
5724 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
5725 ERR("Failed to insert ffp vertex shader.\n");
5727 return shader;
5730 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
5731 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
5733 struct glsl_ffp_fragment_shader *glsl_desc;
5734 const struct ffp_frag_desc *desc;
5736 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
5737 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
5739 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
5740 return NULL;
5742 glsl_desc->entry.settings = *args;
5743 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(&priv->shader_buffer, args, gl_info);
5744 list_init(&glsl_desc->linked_programs);
5745 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
5747 return glsl_desc;
5751 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
5752 GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
5754 unsigned int i;
5755 char name[32];
5757 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5758 sizeof(GLuint) * gl_info->limits.glsl_vs_float_constants);
5759 for (i = 0; i < vs_c_count; ++i)
5761 snprintf(name, sizeof(name), "vs_c[%u]", i);
5762 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5764 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
5765 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLuint));
5767 for (i = 0; i < MAX_CONST_I; ++i)
5769 snprintf(name, sizeof(name), "vs_i[%u]", i);
5770 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5773 for (i = 0; i < MAX_CONST_B; ++i)
5775 snprintf(name, sizeof(name), "vs_b[%u]", i);
5776 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5779 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "posFixup"));
5781 vs->modelview_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_modelview_matrix"));
5784 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
5785 GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
5787 unsigned int i;
5788 char name[32];
5790 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5791 sizeof(GLuint) * gl_info->limits.glsl_ps_float_constants);
5792 for (i = 0; i < ps_c_count; ++i)
5794 snprintf(name, sizeof(name), "ps_c[%u]", i);
5795 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5797 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
5798 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLuint));
5800 for (i = 0; i < MAX_CONST_I; ++i)
5802 snprintf(name, sizeof(name), "ps_i[%u]", i);
5803 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5806 for (i = 0; i < MAX_CONST_B; ++i)
5808 snprintf(name, sizeof(name), "ps_b[%u]", i);
5809 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5812 for (i = 0; i < MAX_TEXTURES; ++i)
5814 snprintf(name, sizeof(name), "bumpenv_mat%u", i);
5815 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5816 snprintf(name, sizeof(name), "bumpenv_lum_scale%u", i);
5817 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5818 snprintf(name, sizeof(name), "bumpenv_lum_offset%u", i);
5819 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5820 snprintf(name, sizeof(name), "tss_const%u", i);
5821 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5824 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
5825 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
5826 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
5827 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
5830 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info, GLuint program_id,
5831 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
5833 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
5834 GLuint block_idx;
5835 unsigned int i;
5836 char name[16];
5838 for (i = 0; i < count; ++i)
5840 if (!reg_maps->cb_sizes[i])
5841 continue;
5843 snprintf(name, sizeof(name), "block_%s_cb%u", prefix, i);
5844 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name));
5845 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
5847 checkGLcall("glUniformBlockBinding");
5850 /* Context activation is done by the caller. */
5851 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
5852 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
5854 const struct wined3d_gl_info *gl_info = context->gl_info;
5855 const struct ps_np2fixup_info *np2fixup_info = NULL;
5856 struct glsl_shader_prog_link *entry = NULL;
5857 struct wined3d_shader *vshader = NULL;
5858 struct wined3d_shader *gshader = NULL;
5859 struct wined3d_shader *pshader = NULL;
5860 GLuint program_id = 0;
5861 GLuint reorder_shader_id = 0;
5862 unsigned int i;
5863 GLuint vs_id = 0;
5864 GLuint gs_id = 0;
5865 GLuint ps_id = 0;
5866 struct list *ps_list, *vs_list;
5868 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_VERTEX)))
5870 vs_id = ctx_data->glsl_program->vs.id;
5871 vs_list = &ctx_data->glsl_program->vs.shader_entry;
5873 if (use_vs(state))
5875 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5876 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
5878 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_GEOMETRY))
5879 && ctx_data->glsl_program->gs.id)
5880 gs_id = ctx_data->glsl_program->gs.id;
5881 else if (gshader)
5882 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5885 else if (use_vs(state))
5887 struct vs_compile_args vs_compile_args;
5888 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5890 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args);
5891 vs_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
5892 vs_list = &vshader->linked_programs;
5894 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
5895 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5897 else if (priv->vertex_pipe == &glsl_vertex_pipe)
5899 struct glsl_ffp_vertex_shader *ffp_shader;
5900 struct wined3d_ffp_vs_settings settings;
5902 wined3d_ffp_get_vs_settings(state, &context->stream_info, &settings);
5903 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
5904 vs_id = ffp_shader->id;
5905 vs_list = &ffp_shader->linked_programs;
5908 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_PIXEL)))
5910 ps_id = ctx_data->glsl_program->ps.id;
5911 ps_list = &ctx_data->glsl_program->ps.shader_entry;
5913 if (use_ps(state))
5914 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
5916 else if (use_ps(state))
5918 struct ps_compile_args ps_compile_args;
5919 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
5920 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, gl_info);
5921 ps_id = find_glsl_pshader(context, &priv->shader_buffer,
5922 pshader, &ps_compile_args, &np2fixup_info);
5923 ps_list = &pshader->linked_programs;
5925 else if (priv->fragment_pipe == &glsl_fragment_pipe)
5927 struct glsl_ffp_fragment_shader *ffp_shader;
5928 struct ffp_frag_settings settings;
5930 gen_ffp_frag_op(context, state, &settings, FALSE);
5931 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
5932 ps_id = ffp_shader->id;
5933 ps_list = &ffp_shader->linked_programs;
5936 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
5938 ctx_data->glsl_program = entry;
5939 return;
5942 /* If we get to this point, then no matching program exists, so we create one */
5943 program_id = GL_EXTCALL(glCreateProgram());
5944 TRACE("Created new GLSL shader program %u.\n", program_id);
5946 /* Create the entry */
5947 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
5948 entry->id = program_id;
5949 entry->vs.id = vs_id;
5950 entry->gs.id = gs_id;
5951 entry->ps.id = ps_id;
5952 entry->constant_version = 0;
5953 entry->ps.np2_fixup_info = np2fixup_info;
5954 /* Add the hash table entry */
5955 add_glsl_program_entry(priv, entry);
5957 /* Set the current program */
5958 ctx_data->glsl_program = entry;
5960 /* Attach GLSL vshader */
5961 if (vs_id)
5963 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
5964 GL_EXTCALL(glAttachShader(program_id, vs_id));
5965 checkGLcall("glAttachShader");
5967 list_add_head(vs_list, &entry->vs.shader_entry);
5970 if (vshader)
5972 WORD map = vshader->reg_maps.input_registers;
5973 char tmp_name[10];
5975 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
5976 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
5977 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
5978 checkGLcall("glAttachShader");
5979 /* Flag the reorder function for deletion, then it will be freed automatically when the program
5980 * is destroyed
5982 GL_EXTCALL(glDeleteShader(reorder_shader_id));
5984 /* Bind vertex attributes to a corresponding index number to match
5985 * the same index numbers as ARB_vertex_programs (makes loading
5986 * vertex attributes simpler). With this method, we can use the
5987 * exact same code to load the attributes later for both ARB and
5988 * GLSL shaders.
5990 * We have to do this here because we need to know the Program ID
5991 * in order to make the bindings work, and it has to be done prior
5992 * to linking the GLSL program. */
5993 for (i = 0; map; map >>= 1, ++i)
5995 if (!(map & 1)) continue;
5997 snprintf(tmp_name, sizeof(tmp_name), "vs_in%u", i);
5998 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name));
6000 checkGLcall("glBindAttribLocation");
6003 if (gshader)
6005 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
6006 GL_EXTCALL(glAttachShader(program_id, gs_id));
6007 checkGLcall("glAttachShader");
6009 TRACE("input type %s, output type %s, vertices out %u.\n",
6010 debug_d3dprimitivetype(gshader->u.gs.input_type),
6011 debug_d3dprimitivetype(gshader->u.gs.output_type),
6012 gshader->u.gs.vertices_out);
6013 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
6014 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
6015 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
6016 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
6017 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
6018 gshader->u.gs.vertices_out));
6019 checkGLcall("glProgramParameteriARB");
6021 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
6024 /* Attach GLSL pshader */
6025 if (ps_id)
6027 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
6028 GL_EXTCALL(glAttachShader(program_id, ps_id));
6029 checkGLcall("glAttachShader");
6031 list_add_head(ps_list, &entry->ps.shader_entry);
6034 /* Link the program */
6035 TRACE("Linking GLSL shader program %u.\n", program_id);
6036 GL_EXTCALL(glLinkProgram(program_id));
6037 shader_glsl_validate_link(gl_info, program_id);
6039 shader_glsl_init_vs_uniform_locations(gl_info, program_id, &entry->vs,
6040 vshader ? min(vshader->limits->constant_float, gl_info->limits.glsl_vs_float_constants) : 0);
6041 shader_glsl_init_ps_uniform_locations(gl_info, program_id, &entry->ps,
6042 pshader ? min(pshader->limits->constant_float, gl_info->limits.glsl_ps_float_constants) : 0);
6043 checkGLcall("Find glsl program uniform locations");
6045 if (pshader && pshader->reg_maps.shader_version.major >= 3
6046 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
6048 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
6049 entry->vs.vertex_color_clamp = GL_FALSE;
6051 else
6053 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
6056 /* Set the shader to allow uniform loading on it */
6057 GL_EXTCALL(glUseProgram(program_id));
6058 checkGLcall("glUseProgram");
6060 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
6061 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
6062 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
6063 * vertex shader with fixed function pixel processing is used we make sure that the card
6064 * supports enough samplers to allow the max number of vertex samplers with all possible
6065 * fixed function fragment processing setups. So once the program is linked these samplers
6066 * won't change. */
6067 shader_glsl_load_samplers(gl_info, context->tex_unit_map, program_id);
6069 entry->constant_update_mask = 0;
6070 if (vshader)
6072 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
6073 if (vshader->reg_maps.integer_constants)
6074 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
6075 if (vshader->reg_maps.boolean_constants)
6076 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
6077 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
6079 shader_glsl_init_uniform_block_bindings(gl_info, program_id, &vshader->reg_maps,
6080 0, gl_info->limits.vertex_uniform_blocks);
6082 else
6084 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
6087 if (gshader)
6088 shader_glsl_init_uniform_block_bindings(gl_info, program_id, &gshader->reg_maps,
6089 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
6091 if (ps_id)
6093 if (pshader)
6095 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
6096 if (pshader->reg_maps.integer_constants)
6097 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
6098 if (pshader->reg_maps.boolean_constants)
6099 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
6100 if (entry->ps.ycorrection_location != -1)
6101 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
6103 shader_glsl_init_uniform_block_bindings(gl_info, program_id, &pshader->reg_maps,
6104 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
6105 gl_info->limits.fragment_uniform_blocks);
6107 else
6109 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
6112 for (i = 0; i < MAX_TEXTURES; ++i)
6114 if (entry->ps.bumpenv_mat_location[i] != -1)
6116 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
6117 break;
6121 if (entry->ps.np2_fixup_location != -1)
6122 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
6126 /* Context activation is done by the caller. */
6127 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum tex_types tex_type, BOOL masked)
6129 GLuint program_id;
6130 GLuint vshader_id, pshader_id;
6131 const char *blt_pshader;
6133 static const char blt_vshader[] =
6134 "#version 120\n"
6135 "void main(void)\n"
6136 "{\n"
6137 " gl_Position = gl_Vertex;\n"
6138 " gl_FrontColor = vec4(1.0);\n"
6139 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
6140 "}\n";
6142 static const char * const blt_pshaders_full[tex_type_count] =
6144 /* tex_1d */
6145 NULL,
6146 /* tex_2d */
6147 "#version 120\n"
6148 "uniform sampler2D sampler;\n"
6149 "void main(void)\n"
6150 "{\n"
6151 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6152 "}\n",
6153 /* tex_3d */
6154 NULL,
6155 /* tex_cube */
6156 "#version 120\n"
6157 "uniform samplerCube sampler;\n"
6158 "void main(void)\n"
6159 "{\n"
6160 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6161 "}\n",
6162 /* tex_rect */
6163 "#version 120\n"
6164 "#extension GL_ARB_texture_rectangle : enable\n"
6165 "uniform sampler2DRect sampler;\n"
6166 "void main(void)\n"
6167 "{\n"
6168 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6169 "}\n",
6172 static const char * const blt_pshaders_masked[tex_type_count] =
6174 /* tex_1d */
6175 NULL,
6176 /* tex_2d */
6177 "#version 120\n"
6178 "uniform sampler2D sampler;\n"
6179 "uniform vec4 mask;\n"
6180 "void main(void)\n"
6181 "{\n"
6182 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6183 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6184 "}\n",
6185 /* tex_3d */
6186 NULL,
6187 /* tex_cube */
6188 "#version 120\n"
6189 "uniform samplerCube sampler;\n"
6190 "uniform vec4 mask;\n"
6191 "void main(void)\n"
6192 "{\n"
6193 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6194 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6195 "}\n",
6196 /* tex_rect */
6197 "#version 120\n"
6198 "#extension GL_ARB_texture_rectangle : enable\n"
6199 "uniform sampler2DRect sampler;\n"
6200 "uniform vec4 mask;\n"
6201 "void main(void)\n"
6202 "{\n"
6203 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6204 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6205 "}\n",
6208 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
6209 if (!blt_pshader)
6211 FIXME("tex_type %#x not supported\n", tex_type);
6212 return 0;
6215 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6216 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
6218 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6219 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
6221 program_id = GL_EXTCALL(glCreateProgram());
6222 GL_EXTCALL(glAttachShader(program_id, vshader_id));
6223 GL_EXTCALL(glAttachShader(program_id, pshader_id));
6224 GL_EXTCALL(glLinkProgram(program_id));
6226 shader_glsl_validate_link(gl_info, program_id);
6228 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
6229 * is destroyed
6231 GL_EXTCALL(glDeleteShader(vshader_id));
6232 GL_EXTCALL(glDeleteShader(pshader_id));
6233 return program_id;
6236 /* Context activation is done by the caller. */
6237 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
6238 const struct wined3d_state *state)
6240 struct glsl_context_data *ctx_data = context->shader_backend_data;
6241 const struct wined3d_gl_info *gl_info = context->gl_info;
6242 struct shader_glsl_priv *priv = shader_priv;
6243 GLuint program_id = 0, prev_id = 0;
6244 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
6246 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
6247 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
6249 if (ctx_data->glsl_program)
6251 prev_id = ctx_data->glsl_program->id;
6252 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6254 else
6256 prev_id = 0;
6257 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6260 set_glsl_shader_program(context, state, priv, ctx_data);
6262 if (ctx_data->glsl_program)
6264 program_id = ctx_data->glsl_program->id;
6265 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6267 else
6269 program_id = 0;
6270 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6273 if (old_vertex_color_clamp != current_vertex_color_clamp)
6275 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6277 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
6278 checkGLcall("glClampColorARB");
6280 else
6282 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
6286 TRACE("Using GLSL program %u.\n", program_id);
6288 if (prev_id != program_id)
6290 GL_EXTCALL(glUseProgram(program_id));
6291 checkGLcall("glUseProgram");
6293 if (program_id)
6294 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
6298 /* "context" is not necessarily the currently active context. */
6299 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
6301 struct glsl_context_data *ctx_data = context->shader_backend_data;
6303 ctx_data->glsl_program = NULL;
6304 context->shader_update_mask = (1 << WINED3D_SHADER_TYPE_PIXEL)
6305 | (1 << WINED3D_SHADER_TYPE_VERTEX)
6306 | (1 << WINED3D_SHADER_TYPE_GEOMETRY);
6309 /* Context activation is done by the caller. */
6310 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
6312 const struct wined3d_gl_info *gl_info = context->gl_info;
6313 struct shader_glsl_priv *priv = shader_priv;
6315 shader_glsl_invalidate_current_program(context);
6316 GL_EXTCALL(glUseProgram(0));
6317 checkGLcall("glUseProgram");
6319 priv->vertex_pipe->vp_enable(gl_info, FALSE);
6320 priv->fragment_pipe->enable_extension(gl_info, FALSE);
6322 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6324 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
6325 checkGLcall("glClampColorARB");
6329 /* Context activation is done by the caller. */
6330 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
6331 enum tex_types tex_type, const SIZE *ds_mask_size)
6333 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
6334 struct shader_glsl_priv *priv = shader_priv;
6335 GLuint *blt_program;
6336 GLint loc;
6338 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
6339 if (!*blt_program)
6341 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
6342 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
6343 GL_EXTCALL(glUseProgram(*blt_program));
6344 GL_EXTCALL(glUniform1i(loc, 0));
6346 else
6348 GL_EXTCALL(glUseProgram(*blt_program));
6351 if (masked)
6353 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
6354 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
6358 /* Context activation is done by the caller. */
6359 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
6361 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
6362 GLuint program_id;
6364 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
6365 if (program_id) TRACE("Using GLSL program %u\n", program_id);
6367 GL_EXTCALL(glUseProgram(program_id));
6368 checkGLcall("glUseProgram");
6371 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
6372 const struct glsl_shader_prog_link *program)
6374 const struct glsl_context_data *ctx_data;
6375 struct wined3d_context *context;
6376 unsigned int i;
6378 for (i = 0; i < device->context_count; ++i)
6380 context = device->contexts[i];
6381 ctx_data = context->shader_backend_data;
6383 if (ctx_data->glsl_program == program)
6384 shader_glsl_invalidate_current_program(context);
6388 static void shader_glsl_destroy(struct wined3d_shader *shader)
6390 struct glsl_shader_private *shader_data = shader->backend_data;
6391 struct wined3d_device *device = shader->device;
6392 struct shader_glsl_priv *priv = device->shader_priv;
6393 const struct wined3d_gl_info *gl_info;
6394 const struct list *linked_programs;
6395 struct wined3d_context *context;
6397 if (!shader_data || !shader_data->num_gl_shaders)
6399 HeapFree(GetProcessHeap(), 0, shader_data);
6400 shader->backend_data = NULL;
6401 return;
6404 context = context_acquire(device, NULL);
6405 gl_info = context->gl_info;
6407 TRACE("Deleting linked programs.\n");
6408 linked_programs = &shader->linked_programs;
6409 if (linked_programs->next)
6411 struct glsl_shader_prog_link *entry, *entry2;
6412 UINT i;
6414 switch (shader->reg_maps.shader_version.type)
6416 case WINED3D_SHADER_TYPE_PIXEL:
6418 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
6420 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6422 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
6423 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
6424 checkGLcall("glDeleteShader");
6426 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
6428 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6429 struct glsl_shader_prog_link, ps.shader_entry)
6431 shader_glsl_invalidate_contexts_program(device, entry);
6432 delete_glsl_program_entry(priv, gl_info, entry);
6435 break;
6438 case WINED3D_SHADER_TYPE_VERTEX:
6440 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
6442 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6444 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
6445 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
6446 checkGLcall("glDeleteShader");
6448 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
6450 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6451 struct glsl_shader_prog_link, vs.shader_entry)
6453 shader_glsl_invalidate_contexts_program(device, entry);
6454 delete_glsl_program_entry(priv, gl_info, entry);
6457 break;
6460 case WINED3D_SHADER_TYPE_GEOMETRY:
6462 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
6464 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6466 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
6467 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
6468 checkGLcall("glDeleteShader");
6470 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
6472 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6473 struct glsl_shader_prog_link, gs.shader_entry)
6475 shader_glsl_invalidate_contexts_program(device, entry);
6476 delete_glsl_program_entry(priv, gl_info, entry);
6479 break;
6482 default:
6483 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
6484 break;
6488 HeapFree(GetProcessHeap(), 0, shader->backend_data);
6489 shader->backend_data = NULL;
6491 context_release(context);
6494 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
6496 const struct glsl_program_key *k = key;
6497 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
6498 const struct glsl_shader_prog_link, program_lookup_entry);
6500 if (k->vs_id > prog->vs.id) return 1;
6501 else if (k->vs_id < prog->vs.id) return -1;
6503 if (k->gs_id > prog->gs.id) return 1;
6504 else if (k->gs_id < prog->gs.id) return -1;
6506 if (k->ps_id > prog->ps.id) return 1;
6507 else if (k->ps_id < prog->ps.id) return -1;
6509 return 0;
6512 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
6514 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
6515 + constant_count * sizeof(*heap->contained)
6516 + constant_count * sizeof(*heap->positions);
6517 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
6519 if (!mem)
6521 ERR("Failed to allocate memory\n");
6522 return FALSE;
6525 heap->entries = mem;
6526 heap->entries[1].version = 0;
6527 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
6528 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
6529 heap->positions = (unsigned int *)(heap->contained + constant_count);
6530 heap->size = 1;
6532 return TRUE;
6535 static void constant_heap_free(struct constant_heap *heap)
6537 HeapFree(GetProcessHeap(), 0, heap->entries);
6540 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
6542 wined3d_rb_alloc,
6543 wined3d_rb_realloc,
6544 wined3d_rb_free,
6545 glsl_program_key_compare,
6548 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
6549 const struct fragment_pipeline *fragment_pipe)
6551 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6552 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
6553 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
6554 gl_info->limits.glsl_ps_float_constants)) + 1;
6555 struct fragment_caps fragment_caps;
6556 void *vertex_priv, *fragment_priv;
6558 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
6560 ERR("Failed to initialize vertex pipe.\n");
6561 HeapFree(GetProcessHeap(), 0, priv);
6562 return E_FAIL;
6565 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
6567 ERR("Failed to initialize fragment pipe.\n");
6568 vertex_pipe->vp_free(device);
6569 HeapFree(GetProcessHeap(), 0, priv);
6570 return E_FAIL;
6573 if (!shader_buffer_init(&priv->shader_buffer))
6575 ERR("Failed to initialize shader buffer.\n");
6576 goto fail;
6579 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
6580 if (!priv->stack)
6582 ERR("Failed to allocate memory.\n");
6583 goto fail;
6586 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
6588 ERR("Failed to initialize vertex shader constant heap\n");
6589 goto fail;
6592 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
6594 ERR("Failed to initialize pixel shader constant heap\n");
6595 goto fail;
6598 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
6600 ERR("Failed to initialize rbtree.\n");
6601 goto fail;
6604 priv->next_constant_version = 1;
6605 priv->vertex_pipe = vertex_pipe;
6606 priv->fragment_pipe = fragment_pipe;
6607 fragment_pipe->get_caps(gl_info, &fragment_caps);
6608 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
6610 device->vertex_priv = vertex_priv;
6611 device->fragment_priv = fragment_priv;
6612 device->shader_priv = priv;
6614 return WINED3D_OK;
6616 fail:
6617 constant_heap_free(&priv->pconst_heap);
6618 constant_heap_free(&priv->vconst_heap);
6619 HeapFree(GetProcessHeap(), 0, priv->stack);
6620 shader_buffer_free(&priv->shader_buffer);
6621 fragment_pipe->free_private(device);
6622 vertex_pipe->vp_free(device);
6623 HeapFree(GetProcessHeap(), 0, priv);
6624 return E_OUTOFMEMORY;
6627 /* Context activation is done by the caller. */
6628 static void shader_glsl_free(struct wined3d_device *device)
6630 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6631 struct shader_glsl_priv *priv = device->shader_priv;
6632 int i;
6634 for (i = 0; i < tex_type_count; ++i)
6636 if (priv->depth_blt_program_full[i])
6638 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
6640 if (priv->depth_blt_program_masked[i])
6642 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
6646 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
6647 constant_heap_free(&priv->pconst_heap);
6648 constant_heap_free(&priv->vconst_heap);
6649 HeapFree(GetProcessHeap(), 0, priv->stack);
6650 shader_buffer_free(&priv->shader_buffer);
6651 priv->fragment_pipe->free_private(device);
6652 priv->vertex_pipe->vp_free(device);
6654 HeapFree(GetProcessHeap(), 0, device->shader_priv);
6655 device->shader_priv = NULL;
6658 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
6660 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
6661 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
6664 static void shader_glsl_free_context_data(struct wined3d_context *context)
6666 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
6669 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
6671 UINT shader_model;
6673 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
6674 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
6675 && gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] && gl_info->supported[ARB_DRAW_INSTANCED]
6676 && gl_info->supported[ARB_TEXTURE_RG] && gl_info->supported[ARB_SAMPLER_OBJECTS])
6677 shader_model = 4;
6678 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
6679 * texldd and texldl instructions. */
6680 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
6681 shader_model = 3;
6682 else
6683 shader_model = 2;
6684 TRACE("Shader model %u.\n", shader_model);
6686 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
6687 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
6688 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
6690 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
6691 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
6693 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
6694 * Direct3D minimum requirement.
6696 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
6697 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
6699 * The problem is that the refrast clamps temporary results in the shader to
6700 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
6701 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
6702 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
6703 * offer a way to query this.
6705 if (shader_model >= 4)
6706 caps->ps_1x_max_value = FLT_MAX;
6707 else
6708 caps->ps_1x_max_value = 1024.0f;
6710 /* Ideally we'd only set caps like sRGB writes here if supported by both
6711 * the shader backend and the fragment pipe, but we can get called before
6712 * shader_glsl_alloc(). */
6713 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
6714 | WINED3D_SHADER_CAP_SRGB_WRITE;
6717 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
6719 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
6721 TRACE("Checking support for fixup:\n");
6722 dump_color_fixup_desc(fixup);
6725 /* We support everything except YUV conversions. */
6726 if (!is_complex_fixup(fixup))
6728 TRACE("[OK]\n");
6729 return TRUE;
6732 TRACE("[FAILED]\n");
6733 return FALSE;
6736 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
6738 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
6739 /* WINED3DSIH_ADD */ shader_glsl_binop,
6740 /* WINED3DSIH_AND */ shader_glsl_binop,
6741 /* WINED3DSIH_BEM */ shader_glsl_bem,
6742 /* WINED3DSIH_BREAK */ shader_glsl_break,
6743 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
6744 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
6745 /* WINED3DSIH_CALL */ shader_glsl_call,
6746 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
6747 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
6748 /* WINED3DSIH_CND */ shader_glsl_cnd,
6749 /* WINED3DSIH_CRS */ shader_glsl_cross,
6750 /* WINED3DSIH_CUT */ shader_glsl_cut,
6751 /* WINED3DSIH_DCL */ shader_glsl_nop,
6752 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
6753 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
6754 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
6755 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
6756 /* WINED3DSIH_DEF */ shader_glsl_nop,
6757 /* WINED3DSIH_DEFB */ shader_glsl_nop,
6758 /* WINED3DSIH_DEFI */ shader_glsl_nop,
6759 /* WINED3DSIH_DIV */ shader_glsl_binop,
6760 /* WINED3DSIH_DP2 */ shader_glsl_dot,
6761 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
6762 /* WINED3DSIH_DP3 */ shader_glsl_dot,
6763 /* WINED3DSIH_DP4 */ shader_glsl_dot,
6764 /* WINED3DSIH_DST */ shader_glsl_dst,
6765 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
6766 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
6767 /* WINED3DSIH_ELSE */ shader_glsl_else,
6768 /* WINED3DSIH_EMIT */ shader_glsl_emit,
6769 /* WINED3DSIH_ENDIF */ shader_glsl_end,
6770 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
6771 /* WINED3DSIH_ENDREP */ shader_glsl_end,
6772 /* WINED3DSIH_EQ */ shader_glsl_relop,
6773 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
6774 /* WINED3DSIH_EXPP */ shader_glsl_expp,
6775 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
6776 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
6777 /* WINED3DSIH_GE */ shader_glsl_relop,
6778 /* WINED3DSIH_IADD */ shader_glsl_binop,
6779 /* WINED3DSIH_IEQ */ NULL,
6780 /* WINED3DSIH_IF */ shader_glsl_if,
6781 /* WINED3DSIH_IFC */ shader_glsl_ifc,
6782 /* WINED3DSIH_IGE */ shader_glsl_relop,
6783 /* WINED3DSIH_IMUL */ shader_glsl_imul,
6784 /* WINED3DSIH_ISHL */ shader_glsl_binop,
6785 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
6786 /* WINED3DSIH_LABEL */ shader_glsl_label,
6787 /* WINED3DSIH_LD */ NULL,
6788 /* WINED3DSIH_LIT */ shader_glsl_lit,
6789 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
6790 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
6791 /* WINED3DSIH_LOOP */ shader_glsl_loop,
6792 /* WINED3DSIH_LRP */ shader_glsl_lrp,
6793 /* WINED3DSIH_LT */ shader_glsl_relop,
6794 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
6795 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
6796 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
6797 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
6798 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
6799 /* WINED3DSIH_MAD */ shader_glsl_mad,
6800 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
6801 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
6802 /* WINED3DSIH_MOV */ shader_glsl_mov,
6803 /* WINED3DSIH_MOVA */ shader_glsl_mov,
6804 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
6805 /* WINED3DSIH_MUL */ shader_glsl_binop,
6806 /* WINED3DSIH_NE */ shader_glsl_relop,
6807 /* WINED3DSIH_NOP */ shader_glsl_nop,
6808 /* WINED3DSIH_NRM */ shader_glsl_nrm,
6809 /* WINED3DSIH_OR */ shader_glsl_binop,
6810 /* WINED3DSIH_PHASE */ shader_glsl_nop,
6811 /* WINED3DSIH_POW */ shader_glsl_pow,
6812 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
6813 /* WINED3DSIH_REP */ shader_glsl_rep,
6814 /* WINED3DSIH_RET */ shader_glsl_ret,
6815 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
6816 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
6817 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
6818 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
6819 /* WINED3DSIH_SAMPLE_LOD */ NULL,
6820 /* WINED3DSIH_SETP */ NULL,
6821 /* WINED3DSIH_SGE */ shader_glsl_compare,
6822 /* WINED3DSIH_SGN */ shader_glsl_sgn,
6823 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
6824 /* WINED3DSIH_SLT */ shader_glsl_compare,
6825 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
6826 /* WINED3DSIH_SUB */ shader_glsl_binop,
6827 /* WINED3DSIH_TEX */ shader_glsl_tex,
6828 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
6829 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
6830 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
6831 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
6832 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
6833 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
6834 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
6835 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
6836 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
6837 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
6838 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
6839 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
6840 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
6841 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
6842 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
6843 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
6844 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
6845 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
6846 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
6847 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
6848 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
6849 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
6850 /* WINED3DSIH_UGE */ shader_glsl_relop,
6851 /* WINED3DSIH_USHR */ shader_glsl_binop,
6852 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
6853 /* WINED3DSIH_XOR */ shader_glsl_binop,
6856 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
6857 SHADER_HANDLER hw_fct;
6859 /* Select handler */
6860 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
6862 /* Unhandled opcode */
6863 if (!hw_fct)
6865 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
6866 return;
6868 hw_fct(ins);
6870 shader_glsl_add_instruction_modifiers(ins);
6873 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
6875 struct shader_glsl_priv *priv = shader_priv;
6877 return priv->ffp_proj_control;
6880 const struct wined3d_shader_backend_ops glsl_shader_backend =
6882 shader_glsl_handle_instruction,
6883 shader_glsl_select,
6884 shader_glsl_disable,
6885 shader_glsl_select_depth_blt,
6886 shader_glsl_deselect_depth_blt,
6887 shader_glsl_update_float_vertex_constants,
6888 shader_glsl_update_float_pixel_constants,
6889 shader_glsl_load_constants,
6890 shader_glsl_destroy,
6891 shader_glsl_alloc,
6892 shader_glsl_free,
6893 shader_glsl_allocate_context_data,
6894 shader_glsl_free_context_data,
6895 shader_glsl_get_caps,
6896 shader_glsl_color_fixup_supported,
6897 shader_glsl_has_ffp_proj_control,
6900 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
6902 if (enable)
6903 gl_info->gl_ops.gl.p_glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6904 else
6905 gl_info->gl_ops.gl.p_glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6906 checkGLcall("GL_VERTEX_PROGRAM_POINT_SIZE_ARB");
6909 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
6911 caps->xyzrhw = TRUE;
6912 caps->max_active_lights = gl_info->limits.lights;
6913 caps->max_vertex_blend_matrices = 1;
6914 caps->max_vertex_blend_matrix_index = 0;
6915 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
6916 | WINED3DVTXPCAPS_MATERIALSOURCE7
6917 | WINED3DVTXPCAPS_VERTEXFOG
6918 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
6919 | WINED3DVTXPCAPS_POSITIONALLIGHTS
6920 | WINED3DVTXPCAPS_LOCALVIEWER
6921 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
6922 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
6923 caps->max_user_clip_planes = gl_info->limits.clipplanes;
6924 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
6927 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
6929 struct shader_glsl_priv *priv;
6931 if (shader_backend == &glsl_shader_backend)
6933 priv = shader_priv;
6935 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
6937 ERR("Failed to initialize rbtree.\n");
6938 return NULL;
6941 return priv;
6944 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
6946 return NULL;
6949 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
6951 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
6952 struct glsl_ffp_vertex_shader, desc.entry);
6953 struct glsl_shader_prog_link *program, *program2;
6954 struct glsl_ffp_destroy_ctx *ctx = context;
6956 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
6957 struct glsl_shader_prog_link, vs.shader_entry)
6959 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
6961 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
6962 HeapFree(GetProcessHeap(), 0, shader);
6965 /* Context activation is done by the caller. */
6966 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
6968 struct shader_glsl_priv *priv = device->vertex_priv;
6969 struct glsl_ffp_destroy_ctx ctx;
6971 ctx.priv = priv;
6972 ctx.gl_info = &device->adapter->gl_info;
6973 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
6976 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
6977 const struct wined3d_state *state, DWORD state_id)
6979 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
6982 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
6983 const struct wined3d_state *state, DWORD state_id)
6985 const struct wined3d_gl_info *gl_info = context->gl_info;
6986 BOOL transformed = context->stream_info.position_transformed;
6987 BOOL wasrhw = context->last_was_rhw;
6988 unsigned int i;
6990 context->last_was_rhw = transformed;
6992 if (!use_vs(state))
6994 if (context->last_was_vshader)
6996 for (i = 0; i < gl_info->limits.clipplanes; ++i)
6997 clipplane(context, state, STATE_CLIPPLANE(i));
7000 if (transformed != wasrhw)
7002 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION))
7003 && !isStateDirty(context, STATE_VIEWPORT))
7004 transform_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
7007 for (i = 0; i < MAX_TEXTURES; ++i)
7009 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_TEXTURE0 + i)))
7010 transform_texture(context, state, STATE_TEXTURESTAGE(i, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS));
7013 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_LIGHTING)))
7014 state_lighting(context, state, STATE_RENDER(WINED3D_RS_LIGHTING));
7015 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_NORMALIZENORMALS)))
7016 state_normalize(context, state, STATE_RENDER(WINED3D_RS_NORMALIZENORMALS));
7018 /* Because of settings->texcoords, we have to always regenerate the
7019 * vertex shader on a vdecl change.
7020 * TODO: Just always output all the texcoords when there are enough
7021 * varyings available to drop the dependency. */
7022 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7024 if (use_ps(state)
7025 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
7026 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
7027 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7029 else
7031 if (!context->last_was_vshader)
7033 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
7034 for (i = 0; i < gl_info->limits.clipplanes; ++i)
7035 clipplane(context, state, STATE_CLIPPLANE(i));
7039 if (transformed != wasrhw && !isStateDirty(context, STATE_RENDER(WINED3D_RS_ZENABLE)))
7040 context_apply_state(context, state, STATE_RENDER(WINED3D_RS_ZENABLE));
7042 context->last_was_vshader = use_vs(state);
7045 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
7046 const struct wined3d_state *state, DWORD state_id)
7048 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7049 /* Different vertex shaders potentially require a different vertex attributes setup. */
7050 if (!isStateDirty(context, STATE_VDECL))
7051 context_apply_state(context, state, STATE_VDECL);
7054 static void glsl_vertex_pipe_world(struct wined3d_context *context,
7055 const struct wined3d_state *state, DWORD state_id)
7057 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
7060 void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
7062 const struct wined3d_gl_info *gl_info = context->gl_info;
7063 const struct wined3d_light_info *light = NULL;
7064 unsigned int k;
7066 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
7068 for (k = 0; k < gl_info->limits.lights; ++k)
7070 if (!(light = state->lights[k]))
7071 continue;
7072 gl_info->gl_ops.gl.p_glLightfv(GL_LIGHT0 + light->glIndex, GL_POSITION, light->lightPosn);
7073 checkGLcall("glLightfv posn");
7074 gl_info->gl_ops.gl.p_glLightfv(GL_LIGHT0 + light->glIndex, GL_SPOT_DIRECTION, light->lightDirn);
7075 checkGLcall("glLightfv dirn");
7078 for (k = 0; k < gl_info->limits.clipplanes; ++k)
7080 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
7081 clipplane(context, state, STATE_CLIPPLANE(k));
7084 if (context->swapchain->device->vertexBlendUsed)
7086 static int warned;
7088 if (!warned++)
7089 FIXME("Vertex blending emulation.\n");
7093 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
7094 const struct wined3d_state *state, DWORD state_id)
7096 /* Table fog behavior depends on the projection matrix. */
7097 if (state->render_states[WINED3D_RS_FOGENABLE]
7098 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
7099 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7100 transform_projection(context, state, state_id);
7103 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
7104 const struct wined3d_state *state, DWORD state_id)
7106 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
7107 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
7108 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
7109 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
7110 state_pscale(context, state, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE));
7111 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
7114 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
7116 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
7117 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
7118 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
7119 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), state_specularenable }, WINED3D_GL_EXT_NONE },
7120 /* Clip planes */
7121 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
7122 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
7123 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
7124 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
7125 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
7126 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
7127 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
7128 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
7129 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
7130 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
7131 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
7132 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
7133 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
7134 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
7135 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
7136 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
7137 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
7138 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
7139 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
7140 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
7141 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
7142 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
7143 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
7144 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
7145 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
7146 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
7147 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
7148 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
7149 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
7150 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
7151 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
7152 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
7153 /* Lights */
7154 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7155 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), light }, WINED3D_GL_EXT_NONE },
7156 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), light }, WINED3D_GL_EXT_NONE },
7157 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), light }, WINED3D_GL_EXT_NONE },
7158 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), light }, WINED3D_GL_EXT_NONE },
7159 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), light }, WINED3D_GL_EXT_NONE },
7160 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), light }, WINED3D_GL_EXT_NONE },
7161 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), light }, WINED3D_GL_EXT_NONE },
7162 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), light }, WINED3D_GL_EXT_NONE },
7163 /* Viewport */
7164 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
7165 /* Transform states */
7166 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
7167 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
7168 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7169 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7170 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7171 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7172 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7173 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7174 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7175 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7176 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
7177 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7178 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7179 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7180 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7181 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7182 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7183 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7184 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7185 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7186 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7187 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7188 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7189 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7190 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7191 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7192 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7193 /* Fog */
7194 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7195 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7196 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7197 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7198 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
7199 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
7200 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7201 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), state_ambient }, WINED3D_GL_EXT_NONE },
7202 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7203 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7204 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7205 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7206 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7207 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7208 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7209 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7210 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7211 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_arb }, ARB_POINT_PARAMETERS },
7212 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_ext }, EXT_POINT_PARAMETERS },
7213 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_w }, WINED3D_GL_EXT_NONE },
7214 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
7215 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
7216 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), state_pscale }, WINED3D_GL_EXT_NONE },
7217 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7218 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7219 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7220 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, ARB_POINT_PARAMETERS },
7221 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, EXT_POINT_PARAMETERS },
7222 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
7223 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7224 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7225 /* Samplers for NP2 texture matrix adjustions. They are not needed if
7226 * GL_ARB_texture_non_power_of_two is supported, so register a NULL state
7227 * handler in that case to get the vertex part of sampler() skipped (VTF
7228 * is handled in the misc states). Otherwise, register
7229 * sampler_texmatrix(), which takes care of updating the texture matrix. */
7230 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7231 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7232 {STATE_SAMPLER(0), {STATE_SAMPLER(0), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7233 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7234 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7235 {STATE_SAMPLER(1), {STATE_SAMPLER(1), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7236 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7237 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7238 {STATE_SAMPLER(2), {STATE_SAMPLER(2), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7239 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7240 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7241 {STATE_SAMPLER(3), {STATE_SAMPLER(3), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7242 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7243 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7244 {STATE_SAMPLER(4), {STATE_SAMPLER(4), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7245 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7246 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7247 {STATE_SAMPLER(5), {STATE_SAMPLER(5), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7248 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7249 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7250 {STATE_SAMPLER(6), {STATE_SAMPLER(6), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7251 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7252 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7253 {STATE_SAMPLER(7), {STATE_SAMPLER(7), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7254 {STATE_POINT_SIZE_ENABLE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7255 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
7258 /* TODO:
7259 * - This currently depends on GL fixed function functions to set things
7260 * like light parameters. Ideally we'd use regular uniforms for that.
7261 * - In part because of the previous point, much of this is modelled after
7262 * GL fixed function, and has much of the same limitations. For example,
7263 * D3D spot lights are slightly different from GL spot lights.
7264 * - We can now implement drawing transformed vertices using the GLSL pipe,
7265 * instead of using the immediate mode fallback.
7266 * - Similarly, we don't need the fallback for certain combinations of
7267 * material sources anymore.
7268 * - Implement vertex blending and vertex tweening.
7269 * - Handle WINED3D_TSS_TEXCOORD_INDEX in the shader, instead of duplicating
7270 * attribute arrays in load_tex_coords().
7271 * - Per-vertex point sizes. */
7272 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
7274 glsl_vertex_pipe_vp_enable,
7275 glsl_vertex_pipe_vp_get_caps,
7276 glsl_vertex_pipe_vp_alloc,
7277 glsl_vertex_pipe_vp_free,
7278 glsl_vertex_pipe_vp_states,
7281 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
7283 /* Nothing to do. */
7286 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
7288 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
7289 | WINED3D_FRAGMENT_CAP_SRGB_WRITE;
7290 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
7291 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
7292 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
7293 | WINED3DTEXOPCAPS_SELECTARG1
7294 | WINED3DTEXOPCAPS_SELECTARG2
7295 | WINED3DTEXOPCAPS_MODULATE4X
7296 | WINED3DTEXOPCAPS_MODULATE2X
7297 | WINED3DTEXOPCAPS_MODULATE
7298 | WINED3DTEXOPCAPS_ADDSIGNED2X
7299 | WINED3DTEXOPCAPS_ADDSIGNED
7300 | WINED3DTEXOPCAPS_ADD
7301 | WINED3DTEXOPCAPS_SUBTRACT
7302 | WINED3DTEXOPCAPS_ADDSMOOTH
7303 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
7304 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
7305 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
7306 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
7307 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
7308 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
7309 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
7310 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
7311 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
7312 | WINED3DTEXOPCAPS_DOTPRODUCT3
7313 | WINED3DTEXOPCAPS_MULTIPLYADD
7314 | WINED3DTEXOPCAPS_LERP
7315 | WINED3DTEXOPCAPS_BUMPENVMAP
7316 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
7317 caps->MaxTextureBlendStages = 8;
7318 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
7321 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7323 struct shader_glsl_priv *priv;
7325 if (shader_backend == &glsl_shader_backend)
7327 priv = shader_priv;
7329 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
7331 ERR("Failed to initialize rbtree.\n");
7332 return NULL;
7335 return priv;
7338 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
7340 return NULL;
7343 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
7345 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7346 struct glsl_ffp_fragment_shader, entry.entry);
7347 struct glsl_shader_prog_link *program, *program2;
7348 struct glsl_ffp_destroy_ctx *ctx = context;
7350 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7351 struct glsl_shader_prog_link, ps.shader_entry)
7353 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7355 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
7356 HeapFree(GetProcessHeap(), 0, shader);
7359 /* Context activation is done by the caller. */
7360 static void glsl_fragment_pipe_free(struct wined3d_device *device)
7362 struct shader_glsl_priv *priv = device->fragment_priv;
7363 struct glsl_ffp_destroy_ctx ctx;
7365 ctx.priv = priv;
7366 ctx.gl_info = &device->adapter->gl_info;
7367 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
7370 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
7371 const struct wined3d_state *state, DWORD state_id)
7373 context->last_was_pshader = use_ps(state);
7375 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7378 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
7379 const struct wined3d_state *state, DWORD state_id)
7381 BOOL use_vshader = use_vs(state);
7382 enum fogsource new_source;
7383 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
7384 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
7386 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7388 if (!state->render_states[WINED3D_RS_FOGENABLE])
7389 return;
7391 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
7393 if (use_vshader)
7394 new_source = FOGSOURCE_VS;
7395 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
7396 new_source = FOGSOURCE_COORD;
7397 else
7398 new_source = FOGSOURCE_FFP;
7400 else
7402 new_source = FOGSOURCE_FFP;
7405 if (new_source != context->fog_source || fogstart == fogend)
7407 context->fog_source = new_source;
7408 state_fogstartend(context, state, STATE_RENDER(WINED3D_RS_FOGSTART));
7412 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
7413 const struct wined3d_state *state, DWORD state_id)
7415 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
7416 glsl_fragment_pipe_fog(context, state, state_id);
7419 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
7420 const struct wined3d_state *state, DWORD state_id)
7422 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7425 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
7426 const struct wined3d_state *state, DWORD state_id)
7428 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7431 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
7433 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
7434 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7435 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7436 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7437 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7438 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7439 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7440 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7441 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7442 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7443 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7444 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7445 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7446 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7447 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7448 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7449 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7450 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7451 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7452 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7453 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7454 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7455 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7456 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7457 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7458 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7459 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7460 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7461 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7462 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7463 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7464 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7465 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7466 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7467 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7468 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7469 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7470 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7471 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7472 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7473 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7474 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7475 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7476 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7477 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7478 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7479 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7480 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7481 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7482 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7483 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7484 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7485 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7486 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7487 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7488 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7489 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7490 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7491 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7492 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7493 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7494 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7495 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7496 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7497 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7498 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7499 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7500 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7501 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7502 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7503 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7504 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7505 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7506 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7507 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
7508 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
7509 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7510 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7511 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
7512 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
7513 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
7514 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7515 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
7516 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
7517 {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 },
7518 {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 },
7519 {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 },
7520 {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 },
7521 {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 },
7522 {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 },
7523 {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 },
7524 {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 },
7525 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7526 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7527 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7528 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7529 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7530 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7531 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7532 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7533 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7534 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
7537 const struct fragment_pipeline glsl_fragment_pipe =
7539 glsl_fragment_pipe_enable,
7540 glsl_fragment_pipe_get_caps,
7541 glsl_fragment_pipe_alloc,
7542 glsl_fragment_pipe_free,
7543 shader_glsl_color_fixup_supported,
7544 glsl_fragment_pipe_state_template,