wined3d: Use the core version of the shader object functions.
[wine/multimedia.git] / dlls / wined3d / glsl_shader.c
blobf3e921c207a26e37a8b0d82581dbaad115e32a48
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;
121 struct glsl_gs_program
123 struct list shader_entry;
124 GLuint id;
127 struct glsl_ps_program
129 struct list shader_entry;
130 GLuint id;
131 GLint *uniform_f_locations;
132 GLint uniform_i_locations[MAX_CONST_I];
133 GLint uniform_b_locations[MAX_CONST_B];
134 GLint bumpenv_mat_location[MAX_TEXTURES];
135 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
136 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
137 GLint tss_constant_location[MAX_TEXTURES];
138 GLint tex_factor_location;
139 GLint specular_enable_location;
140 GLint ycorrection_location;
141 GLint np2_fixup_location;
142 const struct ps_np2fixup_info *np2_fixup_info;
145 /* Struct to maintain data about a linked GLSL program */
146 struct glsl_shader_prog_link
148 struct wine_rb_entry program_lookup_entry;
149 struct glsl_vs_program vs;
150 struct glsl_gs_program gs;
151 struct glsl_ps_program ps;
152 GLuint id;
153 DWORD constant_update_mask;
154 UINT constant_version;
157 struct glsl_program_key
159 GLuint vs_id;
160 GLuint gs_id;
161 GLuint ps_id;
164 struct shader_glsl_ctx_priv {
165 const struct vs_compile_args *cur_vs_args;
166 const struct ps_compile_args *cur_ps_args;
167 struct ps_np2fixup_info *cur_np2fixup_info;
170 struct glsl_context_data
172 struct glsl_shader_prog_link *glsl_program;
175 struct glsl_ps_compiled_shader
177 struct ps_compile_args args;
178 struct ps_np2fixup_info np2fixup;
179 GLuint id;
182 struct glsl_vs_compiled_shader
184 struct vs_compile_args args;
185 GLuint id;
188 struct glsl_gs_compiled_shader
190 GLuint id;
193 struct glsl_shader_private
195 union
197 struct glsl_vs_compiled_shader *vs;
198 struct glsl_gs_compiled_shader *gs;
199 struct glsl_ps_compiled_shader *ps;
200 } gl_shaders;
201 UINT num_gl_shaders, shader_array_size;
204 struct glsl_ffp_vertex_shader
206 struct wined3d_ffp_vs_desc desc;
207 GLuint id;
208 struct list linked_programs;
211 struct glsl_ffp_fragment_shader
213 struct ffp_frag_desc entry;
214 GLuint id;
215 struct list linked_programs;
218 struct glsl_ffp_destroy_ctx
220 struct shader_glsl_priv *priv;
221 const struct wined3d_gl_info *gl_info;
224 static const char *debug_gl_shader_type(GLenum type)
226 switch (type)
228 #define WINED3D_TO_STR(u) case u: return #u
229 WINED3D_TO_STR(GL_VERTEX_SHADER);
230 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
231 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
232 #undef WINED3D_TO_STR
233 default:
234 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
238 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
240 switch (type)
242 case WINED3D_SHADER_TYPE_VERTEX:
243 return "vs";
245 case WINED3D_SHADER_TYPE_GEOMETRY:
246 return "gs";
248 case WINED3D_SHADER_TYPE_PIXEL:
249 return "ps";
251 default:
252 FIXME("Unhandled shader type %#x.\n", type);
253 return "unknown";
257 static void shader_glsl_append_imm_vec4(struct wined3d_shader_buffer *buffer, const float *values)
259 char str[4][17];
261 wined3d_ftoa(values[0], str[0]);
262 wined3d_ftoa(values[1], str[1]);
263 wined3d_ftoa(values[2], str[2]);
264 wined3d_ftoa(values[3], str[3]);
265 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
268 /* Extract a line from the info log.
269 * Note that this modifies the source string. */
270 static char *get_info_log_line(char **ptr)
272 char *p, *q;
274 p = *ptr;
275 if (!(q = strstr(p, "\n")))
277 if (!*p) return NULL;
278 *ptr += strlen(p);
279 return p;
281 *q = '\0';
282 *ptr = q + 1;
284 return p;
287 /* Context activation is done by the caller. */
288 static void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
290 int length = 0;
291 char *log;
293 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
294 return;
296 if (program)
297 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
298 else
299 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
301 /* A size of 1 is just a null-terminated string, so the log should be bigger than
302 * that if there are errors. */
303 if (length > 1)
305 char *ptr, *line;
307 log = HeapAlloc(GetProcessHeap(), 0, length);
308 /* The info log is supposed to be zero-terminated, but at least some
309 * versions of fglrx don't terminate the string properly. The reported
310 * length does include the terminator, so explicitly set it to zero
311 * here. */
312 log[length - 1] = 0;
313 if (program)
314 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
315 else
316 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
318 ptr = log;
319 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
321 WARN("Info log received from GLSL shader #%u:\n", id);
322 while ((line = get_info_log_line(&ptr))) WARN(" %s\n", line);
324 else
326 FIXME("Info log received from GLSL shader #%u:\n", id);
327 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
329 HeapFree(GetProcessHeap(), 0, log);
333 /* Context activation is done by the caller. */
334 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
336 TRACE("Compiling shader object %u.\n", shader);
337 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
338 checkGLcall("glShaderSource");
339 GL_EXTCALL(glCompileShader(shader));
340 checkGLcall("glCompileShader");
341 print_glsl_info_log(gl_info, shader, FALSE);
344 /* Context activation is done by the caller. */
345 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
347 GLint i, shader_count, source_size = -1;
348 GLuint *shaders;
349 char *source = NULL;
351 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
352 shaders = HeapAlloc(GetProcessHeap(), 0, shader_count * sizeof(*shaders));
353 if (!shaders)
355 ERR("Failed to allocate shader array memory.\n");
356 return;
359 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
360 for (i = 0; i < shader_count; ++i)
362 char *ptr, *line;
363 GLint tmp;
365 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
367 if (source_size < tmp)
369 HeapFree(GetProcessHeap(), 0, source);
371 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
372 if (!source)
374 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
375 HeapFree(GetProcessHeap(), 0, shaders);
376 return;
378 source_size = tmp;
381 FIXME("Shader %u:\n", shaders[i]);
382 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
383 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
384 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
385 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
386 FIXME("\n");
388 ptr = source;
389 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
390 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
391 FIXME("\n");
394 HeapFree(GetProcessHeap(), 0, source);
395 HeapFree(GetProcessHeap(), 0, shaders);
398 /* Context activation is done by the caller. */
399 static void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
401 GLint tmp;
403 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
404 return;
406 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
407 if (!tmp)
409 FIXME("Program %u link status invalid.\n", program);
410 shader_glsl_dump_program_source(gl_info, program);
413 print_glsl_info_log(gl_info, program, TRUE);
416 /* Context activation is done by the caller. */
417 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
418 const DWORD *tex_unit_map, GLuint program_id)
420 unsigned int mapped_unit;
421 char sampler_name[20];
422 const char *prefix;
423 unsigned int i, j;
424 GLint name_loc;
426 static const struct
428 enum wined3d_shader_type type;
429 unsigned int base_idx;
430 unsigned int count;
432 sampler_info[] =
434 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
435 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
438 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
440 prefix = shader_glsl_get_prefix(sampler_info[i].type);
442 for (j = 0; j < sampler_info[i].count; ++j)
444 snprintf(sampler_name, sizeof(sampler_name), "%s_sampler%u", prefix, j);
445 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name));
446 if (name_loc == -1)
447 continue;
449 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
450 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
452 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name, mapped_unit);
453 continue;
456 TRACE("Loading sampler %s on unit %u.\n", sampler_name, mapped_unit);
457 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
460 checkGLcall("glUniform1i");
463 /* Context activation is done by the caller. */
464 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
465 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
467 unsigned int start = ~0U, end = 0;
468 int stack_idx = 0;
469 unsigned int heap_idx = 1;
470 unsigned int idx;
472 if (heap->entries[heap_idx].version <= version) return;
474 idx = heap->entries[heap_idx].idx;
475 if (constant_locations[idx] != -1)
476 start = end = idx;
477 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
479 while (stack_idx >= 0)
481 /* Note that we fall through to the next case statement. */
482 switch(stack[stack_idx])
484 case HEAP_NODE_TRAVERSE_LEFT:
486 unsigned int left_idx = heap_idx << 1;
487 if (left_idx < heap->size && heap->entries[left_idx].version > version)
489 heap_idx = left_idx;
490 idx = heap->entries[heap_idx].idx;
491 if (constant_locations[idx] != -1)
493 if (start > idx)
494 start = idx;
495 if (end < idx)
496 end = idx;
499 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
500 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
501 break;
505 case HEAP_NODE_TRAVERSE_RIGHT:
507 unsigned int right_idx = (heap_idx << 1) + 1;
508 if (right_idx < heap->size && heap->entries[right_idx].version > version)
510 heap_idx = right_idx;
511 idx = heap->entries[heap_idx].idx;
512 if (constant_locations[idx] != -1)
514 if (start > idx)
515 start = idx;
516 if (end < idx)
517 end = idx;
520 stack[stack_idx++] = HEAP_NODE_POP;
521 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
522 break;
526 case HEAP_NODE_POP:
527 heap_idx >>= 1;
528 --stack_idx;
529 break;
532 if (start <= end)
533 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start * 4]));
534 checkGLcall("walk_constant_heap()");
537 /* Context activation is done by the caller. */
538 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
540 GLfloat clamped_constant[4];
542 if (location == -1) return;
544 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
545 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
546 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
547 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
549 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
552 /* Context activation is done by the caller. */
553 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
554 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
556 int stack_idx = 0;
557 unsigned int heap_idx = 1;
558 unsigned int idx;
560 if (heap->entries[heap_idx].version <= version) return;
562 idx = heap->entries[heap_idx].idx;
563 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
564 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
566 while (stack_idx >= 0)
568 /* Note that we fall through to the next case statement. */
569 switch(stack[stack_idx])
571 case HEAP_NODE_TRAVERSE_LEFT:
573 unsigned int left_idx = heap_idx << 1;
574 if (left_idx < heap->size && heap->entries[left_idx].version > version)
576 heap_idx = left_idx;
577 idx = heap->entries[heap_idx].idx;
578 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
580 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
581 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
582 break;
586 case HEAP_NODE_TRAVERSE_RIGHT:
588 unsigned int right_idx = (heap_idx << 1) + 1;
589 if (right_idx < heap->size && heap->entries[right_idx].version > version)
591 heap_idx = right_idx;
592 idx = heap->entries[heap_idx].idx;
593 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
595 stack[stack_idx++] = HEAP_NODE_POP;
596 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
597 break;
601 case HEAP_NODE_POP:
602 heap_idx >>= 1;
603 --stack_idx;
604 break;
607 checkGLcall("walk_constant_heap_clamped()");
610 /* Context activation is done by the caller. */
611 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
612 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
613 unsigned char *stack, UINT version)
615 const struct wined3d_shader_lconst *lconst;
617 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
618 if (shader->reg_maps.shader_version.major == 1
619 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
620 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
621 else
622 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
624 if (!shader->load_local_constsF)
626 TRACE("No need to load local float constants for this shader\n");
627 return;
630 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
631 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
633 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
635 checkGLcall("glUniform4fv()");
638 /* Context activation is done by the caller. */
639 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
640 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
642 unsigned int i;
643 struct list* ptr;
645 for (i = 0; constants_set; constants_set >>= 1, ++i)
647 if (!(constants_set & 1)) continue;
649 /* We found this uniform name in the program - go ahead and send the data */
650 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i * 4]));
653 /* Load immediate constants */
654 ptr = list_head(&shader->constantsI);
655 while (ptr)
657 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
658 unsigned int idx = lconst->idx;
659 const GLint *values = (const GLint *)lconst->value;
661 /* We found this uniform name in the program - go ahead and send the data */
662 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
663 ptr = list_next(&shader->constantsI, ptr);
665 checkGLcall("glUniform4iv()");
668 /* Context activation is done by the caller. */
669 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
670 const GLint locations[MAX_CONST_B], const BOOL *constants, WORD constants_set)
672 unsigned int i;
673 struct list* ptr;
675 for (i = 0; constants_set; constants_set >>= 1, ++i)
677 if (!(constants_set & 1)) continue;
679 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
682 /* Load immediate constants */
683 ptr = list_head(&shader->constantsB);
684 while (ptr)
686 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
687 unsigned int idx = lconst->idx;
688 const GLint *values = (const GLint *)lconst->value;
690 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
691 ptr = list_next(&shader->constantsB, ptr);
693 checkGLcall("glUniform1iv()");
696 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
698 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
701 /* Context activation is done by the caller (state handler). */
702 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
703 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
705 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
706 UINT fixup = ps->np2_fixup_info->active;
707 UINT i;
709 for (i = 0; fixup; fixup >>= 1, ++i)
711 const struct wined3d_texture *tex = state->textures[i];
712 unsigned char idx = ps->np2_fixup_info->idx[i];
713 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
715 if (!tex)
717 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
718 continue;
721 if (idx % 2)
723 tex_dim[2] = tex->pow2_matrix[0];
724 tex_dim[3] = tex->pow2_matrix[5];
726 else
728 tex_dim[0] = tex->pow2_matrix[0];
729 tex_dim[1] = tex->pow2_matrix[5];
733 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
736 /* Context activation is done by the caller (state handler). */
737 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
738 const struct wined3d_state *state)
740 const struct glsl_context_data *ctx_data = context->shader_backend_data;
741 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
742 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
743 const struct wined3d_gl_info *gl_info = context->gl_info;
744 struct shader_glsl_priv *priv = shader_priv;
745 float position_fixup[4];
746 DWORD update_mask = 0;
748 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
749 UINT constant_version;
750 int i;
752 if (!prog) {
753 /* No GLSL program set - nothing to do. */
754 return;
756 constant_version = prog->constant_version;
757 update_mask = context->constant_update_mask & prog->constant_update_mask;
759 if (update_mask & WINED3D_SHADER_CONST_VS_F)
760 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
761 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
763 if (update_mask & WINED3D_SHADER_CONST_VS_I)
764 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
765 vshader->reg_maps.integer_constants);
767 if (update_mask & WINED3D_SHADER_CONST_VS_B)
768 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
769 vshader->reg_maps.boolean_constants);
771 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
773 shader_get_position_fixup(context, state, position_fixup);
774 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
775 checkGLcall("glUniform4fv");
778 if (update_mask & WINED3D_SHADER_CONST_PS_F)
779 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
780 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
782 if (update_mask & WINED3D_SHADER_CONST_PS_I)
783 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
784 pshader->reg_maps.integer_constants);
786 if (update_mask & WINED3D_SHADER_CONST_PS_B)
787 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
788 pshader->reg_maps.boolean_constants);
790 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
792 for (i = 0; i < MAX_TEXTURES; ++i)
794 if (prog->ps.bumpenv_mat_location[i] == -1)
795 continue;
797 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
798 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
800 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
802 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
803 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
804 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
805 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
809 checkGLcall("bump env uniforms");
812 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
814 float correction_params[4];
816 if (context->render_offscreen)
818 correction_params[0] = 0.0f;
819 correction_params[1] = 1.0f;
820 } else {
821 /* position is window relative, not viewport relative */
822 correction_params[0] = (float) context->current_rt->resource.height;
823 correction_params[1] = -1.0f;
825 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, correction_params));
828 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
829 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
831 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
833 float col[4];
835 if (prog->ps.tex_factor_location != -1)
837 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_TEXTUREFACTOR], col);
838 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, col));
841 if (state->render_states[WINED3D_RS_SPECULARENABLE])
842 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
843 else
844 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
846 for (i = 0; i < MAX_TEXTURES; ++i)
848 if (prog->ps.tss_constant_location[i] == -1)
849 continue;
851 D3DCOLORTOGLFLOAT4(state->texture_states[i][WINED3D_TSS_CONSTANT], col);
852 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, col));
855 checkGLcall("fixed function uniforms");
858 if (priv->next_constant_version == UINT_MAX)
860 TRACE("Max constant version reached, resetting to 0.\n");
861 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
862 priv->next_constant_version = 1;
864 else
866 prog->constant_version = priv->next_constant_version++;
870 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
872 struct constant_entry *entries = heap->entries;
873 unsigned int *positions = heap->positions;
874 unsigned int heap_idx, parent_idx;
876 if (!heap->contained[idx])
878 heap_idx = heap->size++;
879 heap->contained[idx] = TRUE;
881 else
883 heap_idx = positions[idx];
886 while (heap_idx > 1)
888 parent_idx = heap_idx >> 1;
890 if (new_version <= entries[parent_idx].version) break;
892 entries[heap_idx] = entries[parent_idx];
893 positions[entries[parent_idx].idx] = heap_idx;
894 heap_idx = parent_idx;
897 entries[heap_idx].version = new_version;
898 entries[heap_idx].idx = idx;
899 positions[idx] = heap_idx;
902 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
904 struct shader_glsl_priv *priv = device->shader_priv;
905 struct constant_heap *heap = &priv->vconst_heap;
906 UINT i;
908 for (i = start; i < count + start; ++i)
910 update_heap_entry(heap, i, priv->next_constant_version);
913 for (i = 0; i < device->context_count; ++i)
915 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
919 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
921 struct shader_glsl_priv *priv = device->shader_priv;
922 struct constant_heap *heap = &priv->pconst_heap;
923 UINT i;
925 for (i = start; i < count + start; ++i)
927 update_heap_entry(heap, i, priv->next_constant_version);
930 for (i = 0; i < device->context_count; ++i)
932 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
936 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
938 unsigned int ret = gl_info->limits.glsl_varyings / 4;
939 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
940 if(shader_major > 3) return ret;
942 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
943 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
944 return ret;
947 /** Generate the variable & register declarations for the GLSL output target */
948 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
949 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
950 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
952 const struct wined3d_shader_version *version = &reg_maps->shader_version;
953 const struct wined3d_state *state = &shader->device->state;
954 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
955 const struct wined3d_gl_info *gl_info = context->gl_info;
956 const struct wined3d_fb_state *fb = &shader->device->fb;
957 unsigned int i, extra_constants_needed = 0;
958 const struct wined3d_shader_lconst *lconst;
959 const char *prefix;
960 DWORD map;
962 prefix = shader_glsl_get_prefix(version->type);
964 /* Prototype the subroutines */
965 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
967 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
970 /* Declare the constants (aka uniforms) */
971 if (shader->limits->constant_float > 0)
973 unsigned max_constantsF;
975 /* Unless the shader uses indirect addressing, always declare the
976 * maximum array size and ignore that we need some uniforms privately.
977 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
978 * and immediate values, still declare VC[256]. If the shader needs
979 * more uniforms than we have it won't work in any case. If it uses
980 * less, the compiler will figure out which uniforms are really used
981 * and strip them out. This allows a shader to use c255 on a dx9 card,
982 * as long as it doesn't also use all the other constants.
984 * If the shader uses indirect addressing the compiler must assume
985 * that all declared uniforms are used. In this case, declare only the
986 * amount that we're assured to have.
988 * Thus we run into problems in these two cases:
989 * 1) The shader really uses more uniforms than supported.
990 * 2) The shader uses indirect addressing, less constants than
991 * supported, but uses a constant index > #supported consts. */
992 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
994 /* No indirect addressing here. */
995 max_constantsF = gl_info->limits.glsl_ps_float_constants;
997 else
999 if (reg_maps->usesrelconstF)
1001 /* Subtract the other potential uniforms from the max
1002 * available (bools, ints, and 1 row of projection matrix).
1003 * Subtract another uniform for immediate values, which have
1004 * to be loaded via uniform by the driver as well. The shader
1005 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1006 * shader code, so one vec4 should be enough. (Unfortunately
1007 * the Nvidia driver doesn't store 128 and -128 in one float).
1009 * Writing gl_ClipVertex requires one uniform for each
1010 * clipplane as well. */
1011 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1012 if(ctx_priv->cur_vs_args->clip_enabled)
1014 max_constantsF -= gl_info->limits.clipplanes;
1016 max_constantsF -= count_bits(reg_maps->integer_constants);
1017 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1018 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1019 * for now take this into account when calculating the number of available constants
1021 max_constantsF -= count_bits(reg_maps->boolean_constants);
1022 /* Set by driver quirks in directx.c */
1023 max_constantsF -= gl_info->reserved_glsl_constants;
1025 if (max_constantsF < shader->limits->constant_float)
1027 static unsigned int once;
1029 if (!once++)
1030 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1031 " it may not render correctly.\n");
1032 else
1033 WARN("The hardware does not support enough uniform components to run this shader.\n");
1036 else
1038 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1041 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1042 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1045 /* Always declare the full set of constants, the compiler can remove the
1046 * unused ones because d3d doesn't (yet) support indirect int and bool
1047 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1048 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1049 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1051 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1052 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1054 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1056 if (reg_maps->cb_sizes[i])
1057 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1058 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1061 /* Declare texture samplers */
1062 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1064 struct wined3d_shader_sampler_map_entry *entry;
1065 BOOL shadow_sampler, tex_rect;
1066 const char *sampler_type;
1068 entry = &reg_maps->sampler_map.entries[i];
1070 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1072 ERR("Invalid resource index %u.\n", entry->resource_idx);
1073 continue;
1076 shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1 << entry->sampler_idx));
1077 switch (reg_maps->resource_info[entry->resource_idx].type)
1079 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1080 if (shadow_sampler)
1081 sampler_type = "sampler1DShadow";
1082 else
1083 sampler_type = "sampler1D";
1084 break;
1086 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1087 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1088 && (ps_args->np2_fixup & (1 << entry->resource_idx))
1089 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1090 if (shadow_sampler)
1092 if (tex_rect)
1093 sampler_type = "sampler2DRectShadow";
1094 else
1095 sampler_type = "sampler2DShadow";
1097 else
1099 if (tex_rect)
1100 sampler_type = "sampler2DRect";
1101 else
1102 sampler_type = "sampler2D";
1104 break;
1106 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1107 if (shadow_sampler)
1108 FIXME("Unsupported 3D shadow sampler.\n");
1109 sampler_type = "sampler3D";
1110 break;
1112 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1113 if (shadow_sampler)
1114 FIXME("Unsupported Cube shadow sampler.\n");
1115 sampler_type = "samplerCube";
1116 break;
1118 default:
1119 sampler_type = "unsupported_sampler";
1120 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[i].type);
1121 break;
1123 shader_addline(buffer, "uniform %s %s_sampler%u;\n", sampler_type, prefix, entry->bind_idx);
1126 /* Declare uniforms for NP2 texcoord fixup:
1127 * This is NOT done inside the loop that declares the texture samplers
1128 * since the NP2 fixup code is currently only used for the GeforceFX
1129 * series and when forcing the ARB_npot extension off. Modern cards just
1130 * skip the code anyway, so put it inside a separate loop. */
1131 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1133 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1134 UINT cur = 0;
1136 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1137 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1138 * samplerNP2Fixup stores texture dimensions and is updated through
1139 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1141 for (i = 0; i < shader->limits->sampler; ++i)
1143 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1 << i)))
1144 continue;
1146 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1148 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1149 continue;
1152 fixup->idx[i] = cur++;
1155 fixup->num_consts = (cur + 1) >> 1;
1156 fixup->active = ps_args->np2_fixup;
1157 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1160 /* Declare address variables */
1161 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1163 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1166 /* Declare texture coordinate temporaries and initialize them */
1167 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1169 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1172 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1174 /* Declare attributes. */
1175 for (i = 0, map = reg_maps->input_registers; map; map >>= 1, ++i)
1177 if (map & 1)
1178 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, i);
1181 shader_addline(buffer, "uniform vec4 posFixup;\n");
1182 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits->packed_output);
1184 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1186 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
1188 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1190 if (version->major >= 3)
1192 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
1194 if (use_vs(state))
1195 shader_addline(buffer, "varying vec4 %s_link[%u];\n", prefix, in_count);
1196 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1199 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1201 if (!(map & 1))
1202 continue;
1204 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1206 if (reg_maps->luminanceparams & (1 << i))
1208 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1209 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1210 extra_constants_needed++;
1213 extra_constants_needed++;
1216 if (ps_args->srgb_correction)
1218 shader_addline(buffer, "const vec4 srgb_const0 = ");
1219 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
1220 shader_addline(buffer, ";\n");
1221 shader_addline(buffer, "const vec4 srgb_const1 = ");
1222 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
1223 shader_addline(buffer, ";\n");
1225 if (reg_maps->vpos || reg_maps->usesdsy)
1227 if (shader->limits->constant_float + extra_constants_needed
1228 + 1 < gl_info->limits.glsl_ps_float_constants)
1230 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1231 extra_constants_needed++;
1233 else
1235 float ycorrection[] =
1237 context->render_offscreen ? 0.0f : fb->render_targets[0]->height,
1238 context->render_offscreen ? 1.0f : -1.0f,
1239 0.0f,
1240 0.0f,
1243 /* This happens because we do not have proper tracking of the
1244 * constant registers that are actually used, only the max
1245 * limit of the shader version. */
1246 FIXME("Cannot find a free uniform for vpos correction params\n");
1247 shader_addline(buffer, "const vec4 ycorrection = ");
1248 shader_glsl_append_imm_vec4(buffer, ycorrection);
1249 shader_addline(buffer, ";\n");
1251 shader_addline(buffer, "vec4 vpos;\n");
1255 /* Declare output register temporaries */
1256 if (shader->limits->packed_output)
1257 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
1259 /* Declare temporary variables */
1260 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1262 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1265 /* Declare loop registers aLx */
1266 if (version->major < 4)
1268 for (i = 0; i < reg_maps->loop_depth; ++i)
1270 shader_addline(buffer, "int aL%u;\n", i);
1271 shader_addline(buffer, "int tmpInt%u;\n", i);
1275 /* Temporary variables for matrix operations */
1276 shader_addline(buffer, "vec4 tmp0;\n");
1277 shader_addline(buffer, "vec4 tmp1;\n");
1279 if (!shader->load_local_constsF)
1281 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1283 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
1284 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
1285 shader_addline(buffer, ";\n");
1289 /* Start the main program. */
1290 shader_addline(buffer, "void main()\n{\n");
1292 /* Direct3D applications expect integer vPos values, while OpenGL drivers
1293 * add approximately 0.5. This causes off-by-one problems as spotted by
1294 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
1295 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
1296 * causes precision troubles when we just subtract 0.5.
1298 * To deal with that, just floor() the position. This will eliminate the
1299 * fraction on all cards.
1301 * TODO: Test how this behaves with multisampling.
1303 * An advantage of floor is that it works even if the driver doesn't add
1304 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
1305 * to return in gl_FragCoord, even though coordinates specify the pixel
1306 * centers instead of the pixel corners. This code will behave correctly
1307 * on drivers that returns integer values. */
1308 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
1309 shader_addline(buffer,
1310 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1313 /*****************************************************************************
1314 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1316 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1317 ****************************************************************************/
1319 /* Prototypes */
1320 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1321 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1323 /** Used for opcode modifiers - They multiply the result by the specified amount */
1324 static const char * const shift_glsl_tab[] = {
1325 "", /* 0 (none) */
1326 "2.0 * ", /* 1 (x2) */
1327 "4.0 * ", /* 2 (x4) */
1328 "8.0 * ", /* 3 (x8) */
1329 "16.0 * ", /* 4 (x16) */
1330 "32.0 * ", /* 5 (x32) */
1331 "", /* 6 (x64) */
1332 "", /* 7 (x128) */
1333 "", /* 8 (d256) */
1334 "", /* 9 (d128) */
1335 "", /* 10 (d64) */
1336 "", /* 11 (d32) */
1337 "0.0625 * ", /* 12 (d16) */
1338 "0.125 * ", /* 13 (d8) */
1339 "0.25 * ", /* 14 (d4) */
1340 "0.5 * " /* 15 (d2) */
1343 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1344 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1345 const char *in_reg, const char *in_regswizzle, char *out_str)
1347 out_str[0] = 0;
1349 switch (src_modifier)
1351 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1352 case WINED3DSPSM_DW:
1353 case WINED3DSPSM_NONE:
1354 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1355 break;
1356 case WINED3DSPSM_NEG:
1357 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1358 break;
1359 case WINED3DSPSM_NOT:
1360 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1361 break;
1362 case WINED3DSPSM_BIAS:
1363 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1364 break;
1365 case WINED3DSPSM_BIASNEG:
1366 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1367 break;
1368 case WINED3DSPSM_SIGN:
1369 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1370 break;
1371 case WINED3DSPSM_SIGNNEG:
1372 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1373 break;
1374 case WINED3DSPSM_COMP:
1375 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1376 break;
1377 case WINED3DSPSM_X2:
1378 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1379 break;
1380 case WINED3DSPSM_X2NEG:
1381 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1382 break;
1383 case WINED3DSPSM_ABS:
1384 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1385 break;
1386 case WINED3DSPSM_ABSNEG:
1387 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1388 break;
1389 default:
1390 FIXME("Unhandled modifier %u\n", src_modifier);
1391 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1395 /** Writes the GLSL variable name that corresponds to the register that the
1396 * DX opcode parameter is trying to access */
1397 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1398 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1400 /* oPos, oFog and oPts in D3D */
1401 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
1403 const struct wined3d_shader *shader = ins->ctx->shader;
1404 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1405 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1406 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1407 const char *prefix = shader_glsl_get_prefix(version->type);
1408 struct glsl_src_param rel_param0, rel_param1;
1409 char imm_str[4][17];
1411 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
1412 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
1413 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
1414 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
1415 *is_color = FALSE;
1417 switch (reg->type)
1419 case WINED3DSPR_TEMP:
1420 sprintf(register_name, "R%u", reg->idx[0].offset);
1421 break;
1423 case WINED3DSPR_INPUT:
1424 /* vertex shaders */
1425 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1427 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1428 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx[0].offset))
1429 *is_color = TRUE;
1430 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
1431 break;
1434 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1436 if (reg->idx[0].rel_addr)
1438 if (reg->idx[1].rel_addr)
1439 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
1440 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1441 else
1442 sprintf(register_name, "gs_in[%s + %u][%u]",
1443 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
1445 else if (reg->idx[1].rel_addr)
1446 sprintf(register_name, "gs_in[%u][%s + %u]",
1447 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1448 else
1449 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
1450 break;
1453 /* pixel shaders >= 3.0 */
1454 if (version->major >= 3)
1456 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
1457 unsigned int in_count = vec4_varyings(version->major, gl_info);
1459 if (reg->idx[0].rel_addr)
1461 /* Removing a + 0 would be an obvious optimization, but
1462 * OS X doesn't see the NOP operation there. */
1463 if (idx)
1465 if (shader->u.ps.declared_in_count > in_count)
1467 sprintf(register_name,
1468 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
1469 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
1470 prefix, rel_param0.param_str, idx);
1472 else
1474 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
1477 else
1479 if (shader->u.ps.declared_in_count > in_count)
1481 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
1482 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
1483 prefix, rel_param0.param_str);
1485 else
1487 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
1491 else
1493 if (idx == in_count) sprintf(register_name, "gl_Color");
1494 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
1495 else sprintf(register_name, "%s_in[%u]", prefix, idx);
1498 else
1500 if (!reg->idx[0].offset)
1501 strcpy(register_name, "gl_Color");
1502 else
1503 strcpy(register_name, "gl_SecondaryColor");
1504 break;
1506 break;
1508 case WINED3DSPR_CONST:
1510 /* Relative addressing */
1511 if (reg->idx[0].rel_addr)
1513 if (reg->idx[0].offset)
1514 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
1515 else
1516 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
1518 else
1520 if (shader_constant_is_local(shader, reg->idx[0].offset))
1521 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
1522 else
1523 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
1526 break;
1528 case WINED3DSPR_CONSTINT:
1529 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
1530 break;
1532 case WINED3DSPR_CONSTBOOL:
1533 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
1534 break;
1536 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1537 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1538 sprintf(register_name, "T%u", reg->idx[0].offset);
1539 else
1540 sprintf(register_name, "A%u", reg->idx[0].offset);
1541 break;
1543 case WINED3DSPR_LOOP:
1544 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
1545 break;
1547 case WINED3DSPR_SAMPLER:
1548 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
1549 break;
1551 case WINED3DSPR_COLOROUT:
1552 if (reg->idx[0].offset >= gl_info->limits.buffers)
1553 WARN("Write to render target %u, only %d supported.\n",
1554 reg->idx[0].offset, gl_info->limits.buffers);
1556 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
1557 break;
1559 case WINED3DSPR_RASTOUT:
1560 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
1561 break;
1563 case WINED3DSPR_DEPTHOUT:
1564 sprintf(register_name, "gl_FragDepth");
1565 break;
1567 case WINED3DSPR_ATTROUT:
1568 if (!reg->idx[0].offset)
1569 sprintf(register_name, "%s_out[8]", prefix);
1570 else
1571 sprintf(register_name, "%s_out[9]", prefix);
1572 break;
1574 case WINED3DSPR_TEXCRDOUT:
1575 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1576 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
1577 break;
1579 case WINED3DSPR_MISCTYPE:
1580 if (!reg->idx[0].offset)
1582 /* vPos */
1583 sprintf(register_name, "vpos");
1585 else if (reg->idx[0].offset == 1)
1587 /* Note that gl_FrontFacing is a bool, while vFace is
1588 * a float for which the sign determines front/back */
1589 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1591 else
1593 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
1594 sprintf(register_name, "unrecognized_register");
1596 break;
1598 case WINED3DSPR_IMMCONST:
1599 switch (reg->immconst_type)
1601 case WINED3D_IMMCONST_SCALAR:
1602 switch (reg->data_type)
1604 case WINED3D_DATA_FLOAT:
1605 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
1606 break;
1607 case WINED3D_DATA_INT:
1608 sprintf(register_name, "%#x", reg->immconst_data[0]);
1609 break;
1610 case WINED3D_DATA_RESOURCE:
1611 case WINED3D_DATA_SAMPLER:
1612 case WINED3D_DATA_UINT:
1613 sprintf(register_name, "%#xu", reg->immconst_data[0]);
1614 break;
1615 default:
1616 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1617 break;
1619 break;
1621 case WINED3D_IMMCONST_VEC4:
1622 switch (reg->data_type)
1624 case WINED3D_DATA_FLOAT:
1625 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
1626 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
1627 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
1628 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
1629 sprintf(register_name, "vec4(%s, %s, %s, %s)",
1630 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
1631 break;
1632 case WINED3D_DATA_INT:
1633 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
1634 reg->immconst_data[0], reg->immconst_data[1],
1635 reg->immconst_data[2], reg->immconst_data[3]);
1636 break;
1637 case WINED3D_DATA_RESOURCE:
1638 case WINED3D_DATA_SAMPLER:
1639 case WINED3D_DATA_UINT:
1640 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
1641 reg->immconst_data[0], reg->immconst_data[1],
1642 reg->immconst_data[2], reg->immconst_data[3]);
1643 break;
1644 default:
1645 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1646 break;
1648 break;
1650 default:
1651 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
1652 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
1654 break;
1656 case WINED3DSPR_CONSTBUFFER:
1657 if (reg->idx[1].rel_addr)
1658 sprintf(register_name, "%s_cb%u[%s + %u]",
1659 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1660 else
1661 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
1662 break;
1664 case WINED3DSPR_PRIMID:
1665 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
1666 break;
1668 default:
1669 FIXME("Unhandled register type %#x.\n", reg->type);
1670 sprintf(register_name, "unrecognized_register");
1671 break;
1675 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1677 *str++ = '.';
1678 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1679 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1680 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1681 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1682 *str = '\0';
1685 /* Get the GLSL write mask for the destination register */
1686 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1688 DWORD mask = param->write_mask;
1690 if (shader_is_scalar(&param->reg))
1692 mask = WINED3DSP_WRITEMASK_0;
1693 *write_mask = '\0';
1695 else
1697 shader_glsl_write_mask_to_str(mask, write_mask);
1700 return mask;
1703 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1704 unsigned int size = 0;
1706 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1707 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1708 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1709 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1711 return size;
1714 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
1716 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1717 * but addressed as "rgba". To fix this we need to swap the register's x
1718 * and z components. */
1719 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1721 *str++ = '.';
1722 /* swizzle bits fields: wwzzyyxx */
1723 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
1724 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
1725 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
1726 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
1727 *str = '\0';
1730 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
1731 BOOL fixup, DWORD mask, char *swizzle_str)
1733 if (shader_is_scalar(&param->reg))
1734 *swizzle_str = '\0';
1735 else
1736 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
1739 /* From a given parameter token, generate the corresponding GLSL string.
1740 * Also, return the actual register name and swizzle in case the
1741 * caller needs this information as well. */
1742 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1743 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
1745 BOOL is_color = FALSE;
1746 char swizzle_str[6];
1748 glsl_src->reg_name[0] = '\0';
1749 glsl_src->param_str[0] = '\0';
1750 swizzle_str[0] = '\0';
1752 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
1753 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
1755 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
1757 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
1759 else
1761 char param_str[200];
1763 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, param_str);
1765 switch (wined3d_src->reg.data_type)
1767 case WINED3D_DATA_FLOAT:
1768 sprintf(glsl_src->param_str, "%s", param_str);
1769 break;
1770 case WINED3D_DATA_INT:
1771 sprintf(glsl_src->param_str, "floatBitsToInt(%s)", param_str);
1772 break;
1773 case WINED3D_DATA_RESOURCE:
1774 case WINED3D_DATA_SAMPLER:
1775 case WINED3D_DATA_UINT:
1776 sprintf(glsl_src->param_str, "floatBitsToUint(%s)", param_str);
1777 break;
1778 default:
1779 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
1780 sprintf(glsl_src->param_str, "%s", param_str);
1781 break;
1786 /* From a given parameter token, generate the corresponding GLSL string.
1787 * Also, return the actual register name and swizzle in case the
1788 * caller needs this information as well. */
1789 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1790 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
1792 BOOL is_color = FALSE;
1794 glsl_dst->mask_str[0] = '\0';
1795 glsl_dst->reg_name[0] = '\0';
1797 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
1798 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1801 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1802 static DWORD shader_glsl_append_dst_ext(struct wined3d_shader_buffer *buffer,
1803 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
1804 enum wined3d_data_type data_type)
1806 struct glsl_dst_param glsl_dst;
1807 DWORD mask;
1809 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
1811 switch (data_type)
1813 case WINED3D_DATA_FLOAT:
1814 shader_addline(buffer, "%s%s = %s(",
1815 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1816 break;
1817 case WINED3D_DATA_INT:
1818 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
1819 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1820 break;
1821 case WINED3D_DATA_RESOURCE:
1822 case WINED3D_DATA_SAMPLER:
1823 case WINED3D_DATA_UINT:
1824 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
1825 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1826 break;
1827 default:
1828 FIXME("Unhandled data type %#x.\n", data_type);
1829 shader_addline(buffer, "%s%s = %s(",
1830 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1831 break;
1835 return mask;
1838 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1839 static DWORD shader_glsl_append_dst(struct wined3d_shader_buffer *buffer, const struct wined3d_shader_instruction *ins)
1841 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
1844 /** Process GLSL instruction modifiers */
1845 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1847 struct glsl_dst_param dst_param;
1848 DWORD modifiers;
1850 if (!ins->dst_count) return;
1852 modifiers = ins->dst[0].modifiers;
1853 if (!modifiers) return;
1855 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
1857 if (modifiers & WINED3DSPDM_SATURATE)
1859 /* _SAT means to clamp the value of the register to between 0 and 1 */
1860 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1861 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1864 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
1866 FIXME("_centroid modifier not handled\n");
1869 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
1871 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1875 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
1877 switch (op)
1879 case WINED3D_SHADER_REL_OP_GT: return ">";
1880 case WINED3D_SHADER_REL_OP_EQ: return "==";
1881 case WINED3D_SHADER_REL_OP_GE: return ">=";
1882 case WINED3D_SHADER_REL_OP_LT: return "<";
1883 case WINED3D_SHADER_REL_OP_NE: return "!=";
1884 case WINED3D_SHADER_REL_OP_LE: return "<=";
1885 default:
1886 FIXME("Unrecognized operator %#x.\n", op);
1887 return "(\?\?)";
1891 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
1892 DWORD resource_idx, DWORD flags, struct glsl_sample_function *sample_function)
1894 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
1895 const struct wined3d_gl_info *gl_info = ctx->gl_info;
1896 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
1897 && (((const struct shader_glsl_ctx_priv *)ctx->backend_data)->cur_ps_args->shadow & (1 << resource_idx));
1898 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
1899 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1900 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
1901 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
1903 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
1905 /* Note that there's no such thing as a projected cube texture. */
1906 switch (resource_type)
1908 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1909 if (shadow)
1911 if (lod)
1913 sample_function->name = projected ? "shadow1DProjLod" : "shadow1DLod";
1915 else if (grad)
1917 if (gl_info->supported[EXT_GPU_SHADER4])
1918 sample_function->name = projected ? "shadow1DProjGrad" : "shadow1DGrad";
1919 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1920 sample_function->name = projected ? "shadow1DProjGradARB" : "shadow1DGradARB";
1921 else
1923 FIXME("Unsupported 1D shadow grad function.\n");
1924 sample_function->name = "unsupported1DGrad";
1927 else
1929 sample_function->name = projected ? "shadow1DProj" : "shadow1D";
1931 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1933 else
1935 if (lod)
1937 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
1939 else if (grad)
1941 if (gl_info->supported[EXT_GPU_SHADER4])
1942 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
1943 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1944 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
1945 else
1947 FIXME("Unsupported 1D grad function.\n");
1948 sample_function->name = "unsupported1DGrad";
1951 else
1953 sample_function->name = projected ? "texture1DProj" : "texture1D";
1955 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1957 break;
1959 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1960 if (shadow)
1962 if (texrect)
1964 if (lod)
1966 sample_function->name = projected ? "shadow2DRectProjLod" : "shadow2DRectLod";
1968 else if (grad)
1970 if (gl_info->supported[EXT_GPU_SHADER4])
1971 sample_function->name = projected ? "shadow2DRectProjGrad" : "shadow2DRectGrad";
1972 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1973 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
1974 else
1976 FIXME("Unsupported RECT shadow grad function.\n");
1977 sample_function->name = "unsupported2DRectGrad";
1980 else
1982 sample_function->name = projected ? "shadow2DRectProj" : "shadow2DRect";
1985 else
1987 if (lod)
1989 sample_function->name = projected ? "shadow2DProjLod" : "shadow2DLod";
1991 else if (grad)
1993 if (gl_info->supported[EXT_GPU_SHADER4])
1994 sample_function->name = projected ? "shadow2DProjGrad" : "shadow2DGrad";
1995 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1996 sample_function->name = projected ? "shadow2DProjGradARB" : "shadow2DGradARB";
1997 else
1999 FIXME("Unsupported 2D shadow grad function.\n");
2000 sample_function->name = "unsupported2DGrad";
2003 else
2005 sample_function->name = projected ? "shadow2DProj" : "shadow2D";
2008 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2010 else
2012 if (texrect)
2014 if (lod)
2016 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
2018 else if (grad)
2020 if (gl_info->supported[EXT_GPU_SHADER4])
2021 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
2022 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2023 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
2024 else
2026 FIXME("Unsupported RECT grad function.\n");
2027 sample_function->name = "unsupported2DRectGrad";
2030 else
2032 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
2035 else
2037 if (lod)
2039 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
2041 else if (grad)
2043 if (gl_info->supported[EXT_GPU_SHADER4])
2044 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
2045 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2046 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
2047 else
2049 FIXME("Unsupported 2D grad function.\n");
2050 sample_function->name = "unsupported2DGrad";
2053 else
2055 sample_function->name = projected ? "texture2DProj" : "texture2D";
2058 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2060 break;
2062 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2063 if (shadow)
2065 FIXME("Unsupported 3D shadow function.\n");
2066 sample_function->name = "unsupported3DShadow";
2067 sample_function->coord_mask = 0;
2069 else
2071 if (lod)
2073 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
2075 else if (grad)
2077 if (gl_info->supported[EXT_GPU_SHADER4])
2078 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
2079 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2080 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
2081 else
2083 FIXME("Unsupported 3D grad function.\n");
2084 sample_function->name = "unsupported3DGrad";
2087 else
2089 sample_function->name = projected ? "texture3DProj" : "texture3D";
2091 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2093 break;
2095 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2096 if (shadow)
2098 FIXME("Unsupported Cube shadow function.\n");
2099 sample_function->name = "unsupportedCubeShadow";
2100 sample_function->coord_mask = 0;
2102 else
2104 if (lod)
2106 sample_function->name = "textureCubeLod";
2108 else if (grad)
2110 if (gl_info->supported[EXT_GPU_SHADER4])
2111 sample_function->name = "textureCubeGrad";
2112 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2113 sample_function->name = "textureCubeGradARB";
2114 else
2116 FIXME("Unsupported Cube grad function.\n");
2117 sample_function->name = "unsupportedCubeGrad";
2120 else
2122 sample_function->name = "textureCube";
2124 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2126 break;
2128 default:
2129 sample_function->name = "";
2130 sample_function->coord_mask = 0;
2131 FIXME("Unhandled resource type %#x.\n", resource_type);
2132 break;
2136 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2137 BOOL sign_fixup, enum fixup_channel_source channel_source)
2139 switch(channel_source)
2141 case CHANNEL_SOURCE_ZERO:
2142 strcat(arguments, "0.0");
2143 break;
2145 case CHANNEL_SOURCE_ONE:
2146 strcat(arguments, "1.0");
2147 break;
2149 case CHANNEL_SOURCE_X:
2150 strcat(arguments, reg_name);
2151 strcat(arguments, ".x");
2152 break;
2154 case CHANNEL_SOURCE_Y:
2155 strcat(arguments, reg_name);
2156 strcat(arguments, ".y");
2157 break;
2159 case CHANNEL_SOURCE_Z:
2160 strcat(arguments, reg_name);
2161 strcat(arguments, ".z");
2162 break;
2164 case CHANNEL_SOURCE_W:
2165 strcat(arguments, reg_name);
2166 strcat(arguments, ".w");
2167 break;
2169 default:
2170 FIXME("Unhandled channel source %#x\n", channel_source);
2171 strcat(arguments, "undefined");
2172 break;
2175 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2178 static void shader_glsl_color_correction_ext(struct wined3d_shader_buffer *buffer,
2179 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2181 unsigned int mask_size, remaining;
2182 DWORD fixup_mask = 0;
2183 char arguments[256];
2184 char mask_str[6];
2186 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2187 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2188 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2189 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2190 if (!(mask &= fixup_mask))
2191 return;
2193 if (is_complex_fixup(fixup))
2195 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2196 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2197 return;
2200 shader_glsl_write_mask_to_str(mask, mask_str);
2201 mask_size = shader_glsl_get_write_mask_size(mask);
2203 arguments[0] = '\0';
2204 remaining = mask_size;
2205 if (mask & WINED3DSP_WRITEMASK_0)
2207 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2208 if (--remaining) strcat(arguments, ", ");
2210 if (mask & WINED3DSP_WRITEMASK_1)
2212 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2213 if (--remaining) strcat(arguments, ", ");
2215 if (mask & WINED3DSP_WRITEMASK_2)
2217 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2218 if (--remaining) strcat(arguments, ", ");
2220 if (mask & WINED3DSP_WRITEMASK_3)
2222 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2223 if (--remaining) strcat(arguments, ", ");
2226 if (mask_size > 1)
2227 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2228 else
2229 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2232 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2234 char reg_name[256];
2235 BOOL is_color;
2237 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2238 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2241 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2242 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2243 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2245 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2246 char dst_swizzle[6];
2247 struct color_fixup_desc fixup;
2248 BOOL np2_fixup = FALSE;
2249 va_list args;
2251 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2253 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2255 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2256 fixup = priv->cur_ps_args->color_fixup[sampler];
2258 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2259 if(bias) {
2260 FIXME("Biased sampling from NP2 textures is unsupported\n");
2261 } else {
2262 np2_fixup = TRUE;
2266 else
2268 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
2271 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2273 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2274 sample_function->name, shader_glsl_get_prefix(version->type), sampler);
2276 va_start(args, coord_reg_fmt);
2277 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2278 va_end(args);
2280 if(bias) {
2281 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2282 } else {
2283 if (np2_fixup) {
2284 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2285 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2287 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2288 (idx % 2) ? "zw" : "xy", dst_swizzle);
2289 } else if(dx && dy) {
2290 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2291 } else {
2292 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2296 if(!is_identity_fixup(fixup)) {
2297 shader_glsl_color_correction(ins, fixup);
2301 /*****************************************************************************
2302 * Begin processing individual instruction opcodes
2303 ****************************************************************************/
2305 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2307 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2308 struct glsl_src_param src0_param;
2309 struct glsl_src_param src1_param;
2310 DWORD write_mask;
2311 const char *op;
2313 /* Determine the GLSL operator to use based on the opcode */
2314 switch (ins->handler_idx)
2316 case WINED3DSIH_ADD: op = "+"; break;
2317 case WINED3DSIH_AND: op = "&"; break;
2318 case WINED3DSIH_DIV: op = "/"; break;
2319 case WINED3DSIH_IADD: op = "+"; break;
2320 case WINED3DSIH_ISHL: op = "<<"; break;
2321 case WINED3DSIH_MUL: op = "*"; break;
2322 case WINED3DSIH_SUB: op = "-"; break;
2323 case WINED3DSIH_USHR: op = ">>"; break;
2324 case WINED3DSIH_XOR: op = "^"; break;
2325 default:
2326 op = "<unhandled operator>";
2327 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2328 break;
2331 write_mask = shader_glsl_append_dst(buffer, ins);
2332 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2333 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2334 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2337 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2339 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2340 struct glsl_src_param src0_param;
2341 struct glsl_src_param src1_param;
2342 unsigned int mask_size;
2343 DWORD write_mask;
2344 const char *op;
2346 write_mask = shader_glsl_append_dst(buffer, ins);
2347 mask_size = shader_glsl_get_write_mask_size(write_mask);
2348 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2349 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2351 if (mask_size > 1)
2353 switch (ins->handler_idx)
2355 case WINED3DSIH_EQ: op = "equal"; break;
2356 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2357 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2358 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
2359 case WINED3DSIH_LT: op = "lessThan"; break;
2360 default:
2361 op = "<unhandled operator>";
2362 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2363 break;
2366 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2367 mask_size, op, src0_param.param_str, src1_param.param_str);
2369 else
2371 switch (ins->handler_idx)
2373 case WINED3DSIH_EQ: op = "=="; break;
2374 case WINED3DSIH_GE: op = ">="; break;
2375 case WINED3DSIH_IGE: op = ">="; break;
2376 case WINED3DSIH_UGE: op = ">="; break;
2377 case WINED3DSIH_LT: op = "<"; break;
2378 default:
2379 op = "<unhandled operator>";
2380 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2381 break;
2384 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2385 src0_param.param_str, op, src1_param.param_str);
2389 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2391 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2392 struct glsl_src_param src0_param;
2393 struct glsl_src_param src1_param;
2394 DWORD write_mask;
2396 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2397 * not, we can emulate it. */
2398 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2399 FIXME("64-bit integer multiplies not implemented.\n");
2401 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2403 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2404 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2405 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2407 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2408 src0_param.param_str, src1_param.param_str);
2412 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2414 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2415 struct glsl_src_param src0_param, src1_param;
2416 DWORD write_mask;
2418 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2421 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2423 char dst_mask[6];
2425 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2426 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2427 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2428 shader_addline(buffer, "tmp0%s = %s / %s;\n",
2429 dst_mask, src0_param.param_str, src1_param.param_str);
2431 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2432 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2433 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2434 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
2436 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2437 shader_addline(buffer, "tmp0%s);\n", dst_mask);
2439 else
2441 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2442 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2443 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2444 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
2447 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2449 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2450 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2451 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2452 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
2456 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2457 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2459 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2460 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2461 struct glsl_src_param src0_param;
2462 DWORD write_mask;
2464 write_mask = shader_glsl_append_dst(buffer, ins);
2465 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2467 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2468 * shader versions WINED3DSIO_MOVA is used for this. */
2469 if (ins->ctx->reg_maps->shader_version.major == 1
2470 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
2471 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2473 /* This is a simple floor() */
2474 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2475 if (mask_size > 1) {
2476 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2477 } else {
2478 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2481 else if(ins->handler_idx == WINED3DSIH_MOVA)
2483 /* We need to *round* to the nearest int here. */
2484 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2486 if (gl_info->supported[EXT_GPU_SHADER4])
2488 if (mask_size > 1)
2489 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2490 else
2491 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2493 else
2495 if (mask_size > 1)
2496 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2497 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2498 else
2499 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2500 src0_param.param_str, src0_param.param_str);
2503 else
2505 shader_addline(buffer, "%s);\n", src0_param.param_str);
2509 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2510 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2512 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2513 struct glsl_src_param src0_param;
2514 struct glsl_src_param src1_param;
2515 DWORD dst_write_mask, src_write_mask;
2516 unsigned int dst_size = 0;
2518 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2519 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2521 /* dp3 works on vec3, dp4 on vec4 */
2522 if (ins->handler_idx == WINED3DSIH_DP4)
2524 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2525 } else {
2526 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2529 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2530 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2532 if (dst_size > 1) {
2533 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2534 } else {
2535 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2539 /* Note that this instruction has some restrictions. The destination write mask
2540 * can't contain the w component, and the source swizzles have to be .xyzw */
2541 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2543 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2544 struct glsl_src_param src0_param;
2545 struct glsl_src_param src1_param;
2546 char dst_mask[6];
2548 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2549 shader_glsl_append_dst(ins->ctx->buffer, ins);
2550 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2551 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2552 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2555 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
2557 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
2560 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2561 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2562 * GLSL uses the value as-is. */
2563 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2565 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2566 struct glsl_src_param src0_param;
2567 struct glsl_src_param src1_param;
2568 DWORD dst_write_mask;
2569 unsigned int dst_size;
2571 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2572 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2574 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2575 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2577 if (dst_size > 1)
2579 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2580 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
2582 else
2584 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
2585 src1_param.param_str, src0_param.param_str, src1_param.param_str);
2589 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2590 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
2592 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2593 struct glsl_src_param src_param;
2594 const char *instruction;
2595 DWORD write_mask;
2596 unsigned i;
2598 /* Determine the GLSL function to use based on the opcode */
2599 /* TODO: Possibly make this a table for faster lookups */
2600 switch (ins->handler_idx)
2602 case WINED3DSIH_MIN: instruction = "min"; break;
2603 case WINED3DSIH_MAX: instruction = "max"; break;
2604 case WINED3DSIH_ABS: instruction = "abs"; break;
2605 case WINED3DSIH_FRC: instruction = "fract"; break;
2606 case WINED3DSIH_DSX: instruction = "dFdx"; break;
2607 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
2608 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
2609 default: instruction = "";
2610 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2611 break;
2614 write_mask = shader_glsl_append_dst(buffer, ins);
2616 shader_addline(buffer, "%s(", instruction);
2618 if (ins->src_count)
2620 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2621 shader_addline(buffer, "%s", src_param.param_str);
2622 for (i = 1; i < ins->src_count; ++i)
2624 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
2625 shader_addline(buffer, ", %s", src_param.param_str);
2629 shader_addline(buffer, "));\n");
2632 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
2634 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
2636 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2637 struct glsl_src_param src_param;
2638 unsigned int mask_size;
2639 DWORD write_mask;
2640 char dst_mask[6];
2642 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
2643 mask_size = shader_glsl_get_write_mask_size(write_mask);
2644 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2646 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
2647 src_param.param_str, src_param.param_str);
2648 shader_glsl_append_dst(buffer, ins);
2650 if (mask_size > 1)
2652 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
2653 mask_size, src_param.param_str);
2655 else
2657 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
2658 src_param.param_str);
2662 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
2664 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2665 struct glsl_src_param src0_param;
2666 const char *prefix, *suffix;
2667 unsigned int dst_size;
2668 DWORD dst_write_mask;
2670 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2671 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2673 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src0_param);
2675 switch (ins->handler_idx)
2677 case WINED3DSIH_EXP:
2678 case WINED3DSIH_EXPP:
2679 prefix = "exp2(";
2680 suffix = ")";
2681 break;
2683 case WINED3DSIH_LOG:
2684 case WINED3DSIH_LOGP:
2685 prefix = "log2(abs(";
2686 suffix = "))";
2687 break;
2689 case WINED3DSIH_RCP:
2690 prefix = "1.0 / ";
2691 suffix = "";
2692 break;
2694 case WINED3DSIH_RSQ:
2695 prefix = "inversesqrt(abs(";
2696 suffix = "))";
2697 break;
2699 default:
2700 prefix = "";
2701 suffix = "";
2702 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2703 break;
2706 if (dst_size > 1)
2707 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
2708 else
2709 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
2712 /** Process the WINED3DSIO_EXPP instruction in GLSL:
2713 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2714 * dst.x = 2^(floor(src))
2715 * dst.y = src - floor(src)
2716 * dst.z = 2^src (partial precision is allowed, but optional)
2717 * dst.w = 1.0;
2718 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2719 * dst = 2^src; (partial precision is allowed, but optional)
2721 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
2723 if (ins->ctx->reg_maps->shader_version.major < 2)
2725 struct glsl_src_param src_param;
2726 char dst_mask[6];
2728 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2730 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
2731 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
2732 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
2733 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
2735 shader_glsl_append_dst(ins->ctx->buffer, ins);
2736 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2737 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
2738 return;
2741 shader_glsl_scalar_op(ins);
2744 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
2746 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2747 struct glsl_src_param src_param;
2748 unsigned int mask_size;
2749 DWORD write_mask;
2751 write_mask = shader_glsl_append_dst(buffer, ins);
2752 mask_size = shader_glsl_get_write_mask_size(write_mask);
2753 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2755 if (mask_size > 1)
2756 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
2757 else
2758 shader_addline(buffer, "int(%s));\n", src_param.param_str);
2761 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
2763 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2764 struct glsl_src_param src_param;
2765 unsigned int mask_size;
2766 DWORD write_mask;
2768 write_mask = shader_glsl_append_dst(buffer, ins);
2769 mask_size = shader_glsl_get_write_mask_size(write_mask);
2770 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2772 if (mask_size > 1)
2773 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
2774 else
2775 shader_addline(buffer, "float(%s));\n", src_param.param_str);
2778 /** Process signed comparison opcodes in GLSL. */
2779 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
2781 struct glsl_src_param src0_param;
2782 struct glsl_src_param src1_param;
2783 DWORD write_mask;
2784 unsigned int mask_size;
2786 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2787 mask_size = shader_glsl_get_write_mask_size(write_mask);
2788 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2789 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2791 if (mask_size > 1) {
2792 const char *compare;
2794 switch(ins->handler_idx)
2796 case WINED3DSIH_SLT: compare = "lessThan"; break;
2797 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
2798 default: compare = "";
2799 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2802 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
2803 src0_param.param_str, src1_param.param_str);
2804 } else {
2805 switch(ins->handler_idx)
2807 case WINED3DSIH_SLT:
2808 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2809 * to return 0.0 but step returns 1.0 because step is not < x
2810 * An alternative is a bvec compare padded with an unused second component.
2811 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2812 * issue. Playing with not() is not possible either because not() does not accept
2813 * a scalar.
2815 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
2816 src0_param.param_str, src1_param.param_str);
2817 break;
2818 case WINED3DSIH_SGE:
2819 /* Here we can use the step() function and safe a conditional */
2820 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
2821 break;
2822 default:
2823 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2829 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
2831 const char *condition_prefix, *condition_suffix;
2832 struct wined3d_shader_dst_param dst;
2833 struct glsl_src_param src0_param;
2834 struct glsl_src_param src1_param;
2835 struct glsl_src_param src2_param;
2836 BOOL temp_destination = FALSE;
2837 DWORD cmp_channel = 0;
2838 unsigned int i, j;
2839 char mask_char[6];
2840 DWORD write_mask;
2842 switch (ins->handler_idx)
2844 case WINED3DSIH_CMP:
2845 condition_prefix = "";
2846 condition_suffix = " >= 0.0";
2847 break;
2849 case WINED3DSIH_CND:
2850 condition_prefix = "";
2851 condition_suffix = " > 0.5";
2852 break;
2854 case WINED3DSIH_MOVC:
2855 condition_prefix = "bool(";
2856 condition_suffix = ")";
2857 break;
2859 default:
2860 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2861 condition_prefix = "<unhandled prefix>";
2862 condition_suffix = "<unhandled suffix>";
2863 break;
2866 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
2868 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2869 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2870 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2871 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2873 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2874 condition_prefix, src0_param.param_str, condition_suffix,
2875 src1_param.param_str, src2_param.param_str);
2876 return;
2879 dst = ins->dst[0];
2881 /* Splitting the instruction up in multiple lines imposes a problem:
2882 * The first lines may overwrite source parameters of the following lines.
2883 * Deal with that by using a temporary destination register if needed. */
2884 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
2885 && ins->src[0].reg.type == dst.reg.type)
2886 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
2887 && ins->src[1].reg.type == dst.reg.type)
2888 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
2889 && ins->src[2].reg.type == dst.reg.type))
2890 temp_destination = TRUE;
2892 /* Cycle through all source0 channels. */
2893 for (i = 0; i < 4; ++i)
2895 write_mask = 0;
2896 /* Find the destination channels which use the current source0 channel. */
2897 for (j = 0; j < 4; ++j)
2899 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2901 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2902 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2905 dst.write_mask = ins->dst[0].write_mask & write_mask;
2907 if (temp_destination)
2909 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
2910 continue;
2911 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
2913 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
2914 continue;
2916 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2917 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2918 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2920 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2921 condition_prefix, src0_param.param_str, condition_suffix,
2922 src1_param.param_str, src2_param.param_str);
2925 if (temp_destination)
2927 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
2928 shader_glsl_append_dst(ins->ctx->buffer, ins);
2929 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
2933 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2934 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2935 * the compare is done per component of src0. */
2936 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
2938 struct glsl_src_param src0_param;
2939 struct glsl_src_param src1_param;
2940 struct glsl_src_param src2_param;
2941 DWORD write_mask;
2942 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2943 ins->ctx->reg_maps->shader_version.minor);
2945 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
2947 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2948 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2949 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2950 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2952 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
2953 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
2954 else
2955 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2956 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2957 return;
2960 shader_glsl_conditional_move(ins);
2963 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2964 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
2966 struct glsl_src_param src0_param;
2967 struct glsl_src_param src1_param;
2968 struct glsl_src_param src2_param;
2969 DWORD write_mask;
2971 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2972 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2973 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2974 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2975 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
2976 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2979 /* Handles transforming all WINED3DSIO_M?x? opcodes for
2980 Vertex shaders to GLSL codes */
2981 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
2983 int i;
2984 int nComponents = 0;
2985 struct wined3d_shader_dst_param tmp_dst = {{0}};
2986 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
2987 struct wined3d_shader_instruction tmp_ins;
2989 memset(&tmp_ins, 0, sizeof(tmp_ins));
2991 /* Set constants for the temporary argument */
2992 tmp_ins.ctx = ins->ctx;
2993 tmp_ins.dst_count = 1;
2994 tmp_ins.dst = &tmp_dst;
2995 tmp_ins.src_count = 2;
2996 tmp_ins.src = tmp_src;
2998 switch(ins->handler_idx)
3000 case WINED3DSIH_M4x4:
3001 nComponents = 4;
3002 tmp_ins.handler_idx = WINED3DSIH_DP4;
3003 break;
3004 case WINED3DSIH_M4x3:
3005 nComponents = 3;
3006 tmp_ins.handler_idx = WINED3DSIH_DP4;
3007 break;
3008 case WINED3DSIH_M3x4:
3009 nComponents = 4;
3010 tmp_ins.handler_idx = WINED3DSIH_DP3;
3011 break;
3012 case WINED3DSIH_M3x3:
3013 nComponents = 3;
3014 tmp_ins.handler_idx = WINED3DSIH_DP3;
3015 break;
3016 case WINED3DSIH_M3x2:
3017 nComponents = 2;
3018 tmp_ins.handler_idx = WINED3DSIH_DP3;
3019 break;
3020 default:
3021 break;
3024 tmp_dst = ins->dst[0];
3025 tmp_src[0] = ins->src[0];
3026 tmp_src[1] = ins->src[1];
3027 for (i = 0; i < nComponents; ++i)
3029 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3030 shader_glsl_dot(&tmp_ins);
3031 ++tmp_src[1].reg.idx[0].offset;
3036 The LRP instruction performs a component-wise linear interpolation
3037 between the second and third operands using the first operand as the
3038 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3039 This is equivalent to mix(src2, src1, src0);
3041 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3043 struct glsl_src_param src0_param;
3044 struct glsl_src_param src1_param;
3045 struct glsl_src_param src2_param;
3046 DWORD write_mask;
3048 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3050 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3051 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3052 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3054 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3055 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3058 /** Process the WINED3DSIO_LIT instruction in GLSL:
3059 * dst.x = dst.w = 1.0
3060 * dst.y = (src0.x > 0) ? src0.x
3061 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3062 * where src.w is clamped at +- 128
3064 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3066 struct glsl_src_param src0_param;
3067 struct glsl_src_param src1_param;
3068 struct glsl_src_param src3_param;
3069 char dst_mask[6];
3071 shader_glsl_append_dst(ins->ctx->buffer, ins);
3072 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3074 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3075 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3076 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3078 /* The sdk specifies the instruction like this
3079 * dst.x = 1.0;
3080 * if(src.x > 0.0) dst.y = src.x
3081 * else dst.y = 0.0.
3082 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3083 * else dst.z = 0.0;
3084 * dst.w = 1.0;
3085 * (where power = src.w clamped between -128 and 128)
3087 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3088 * dst.x = 1.0 ... No further explanation needed
3089 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3090 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3091 * dst.w = 1.0. ... Nothing fancy.
3093 * So we still have one conditional in there. So do this:
3094 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3096 * 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),
3097 * 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.
3098 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3100 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3101 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3102 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3104 shader_addline(ins->ctx->buffer,
3105 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3106 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3107 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3108 src0_param.param_str, src3_param.param_str, dst_mask);
3111 /** Process the WINED3DSIO_DST instruction in GLSL:
3112 * dst.x = 1.0
3113 * dst.y = src0.x * src0.y
3114 * dst.z = src0.z
3115 * dst.w = src1.w
3117 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3119 struct glsl_src_param src0y_param;
3120 struct glsl_src_param src0z_param;
3121 struct glsl_src_param src1y_param;
3122 struct glsl_src_param src1w_param;
3123 char dst_mask[6];
3125 shader_glsl_append_dst(ins->ctx->buffer, ins);
3126 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3128 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3129 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3130 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3131 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3133 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3134 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3137 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3138 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3139 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3141 * dst.x = cos(src0.?)
3142 * dst.y = sin(src0.?)
3143 * dst.z = dst.z
3144 * dst.w = dst.w
3146 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3148 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3149 struct glsl_src_param src0_param;
3150 DWORD write_mask;
3152 if (ins->ctx->reg_maps->shader_version.major < 4)
3154 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3156 write_mask = shader_glsl_append_dst(buffer, ins);
3157 switch (write_mask)
3159 case WINED3DSP_WRITEMASK_0:
3160 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3161 break;
3163 case WINED3DSP_WRITEMASK_1:
3164 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3165 break;
3167 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3168 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3169 src0_param.param_str, src0_param.param_str);
3170 break;
3172 default:
3173 ERR("Write mask should be .x, .y or .xy\n");
3174 break;
3177 return;
3180 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3183 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3185 char dst_mask[6];
3187 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3188 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3189 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3191 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3192 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3193 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3195 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3196 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3198 else
3200 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3201 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3202 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3205 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3207 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3208 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3209 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3213 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3214 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3215 * generate invalid code
3217 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3219 struct glsl_src_param src0_param;
3220 DWORD write_mask;
3222 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3223 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3225 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3228 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3229 * Start a for() loop where src1.y is the initial value of aL,
3230 * increment aL by src1.z for a total of src1.x iterations.
3231 * Need to use a temporary variable for this operation.
3233 /* FIXME: I don't think nested loops will work correctly this way. */
3234 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3236 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3237 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3238 const struct wined3d_shader *shader = ins->ctx->shader;
3239 const struct wined3d_shader_lconst *constant;
3240 struct glsl_src_param src1_param;
3241 const DWORD *control_values = NULL;
3243 if (ins->ctx->reg_maps->shader_version.major < 4)
3245 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3247 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3248 * class hardware doesn't support real varying indexing, but Microsoft
3249 * designed this feature for Shader model 2.x+. If the loop control is
3250 * known at compile time, the GLSL compiler can unroll the loop, and
3251 * replace indirect addressing with direct addressing. */
3252 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3254 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3256 if (constant->idx == ins->src[1].reg.idx[0].offset)
3258 control_values = constant->value;
3259 break;
3264 if (control_values)
3266 struct wined3d_shader_loop_control loop_control;
3267 loop_control.count = control_values[0];
3268 loop_control.start = control_values[1];
3269 loop_control.step = (int)control_values[2];
3271 if (loop_control.step > 0)
3273 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3274 loop_state->current_depth, loop_control.start,
3275 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3276 loop_state->current_depth, loop_control.step);
3278 else if (loop_control.step < 0)
3280 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3281 loop_state->current_depth, loop_control.start,
3282 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3283 loop_state->current_depth, loop_control.step);
3285 else
3287 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3288 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3289 loop_state->current_depth, loop_control.count,
3290 loop_state->current_depth);
3293 else
3295 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3296 loop_state->current_depth, loop_state->current_reg,
3297 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3298 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3301 ++loop_state->current_reg;
3303 else
3305 shader_addline(buffer, "for (;;)\n{\n");
3308 ++loop_state->current_depth;
3311 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3313 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3315 shader_addline(ins->ctx->buffer, "}\n");
3317 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3319 --loop_state->current_depth;
3320 --loop_state->current_reg;
3323 if (ins->handler_idx == WINED3DSIH_ENDREP)
3325 --loop_state->current_depth;
3329 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3331 const struct wined3d_shader *shader = ins->ctx->shader;
3332 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3333 const struct wined3d_shader_lconst *constant;
3334 struct glsl_src_param src0_param;
3335 const DWORD *control_values = NULL;
3337 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3338 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3340 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3342 if (constant->idx == ins->src[0].reg.idx[0].offset)
3344 control_values = constant->value;
3345 break;
3350 if (control_values)
3352 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3353 loop_state->current_depth, loop_state->current_depth,
3354 control_values[0], loop_state->current_depth);
3356 else
3358 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3359 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3360 loop_state->current_depth, loop_state->current_depth,
3361 src0_param.param_str, loop_state->current_depth);
3364 ++loop_state->current_depth;
3367 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3369 struct glsl_src_param src0_param;
3371 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3372 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
3375 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3377 struct glsl_src_param src0_param;
3378 struct glsl_src_param src1_param;
3380 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3381 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3383 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3384 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3387 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3389 shader_addline(ins->ctx->buffer, "} else {\n");
3392 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3394 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3397 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3399 shader_addline(ins->ctx->buffer, "break;\n");
3402 /* FIXME: According to MSDN the compare is done per component. */
3403 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3405 struct glsl_src_param src0_param;
3406 struct glsl_src_param src1_param;
3408 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3409 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3411 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3412 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3415 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
3417 struct glsl_src_param src_param;
3419 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
3420 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
3423 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
3425 shader_addline(ins->ctx->buffer, "}\n");
3426 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
3429 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
3431 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
3434 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3436 struct glsl_src_param src1_param;
3438 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3439 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
3440 src1_param.param_str, ins->src[0].reg.idx[0].offset);
3443 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3445 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3446 * function only suppresses the unhandled instruction warning
3450 /*********************************************
3451 * Pixel Shader Specific Code begins here
3452 ********************************************/
3453 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3455 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3456 ins->ctx->reg_maps->shader_version.minor);
3457 struct glsl_sample_function sample_function;
3458 DWORD sample_flags = 0;
3459 DWORD resource_idx;
3460 DWORD mask = 0, swizzle;
3461 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3463 /* 1.0-1.4: Use destination register as sampler source.
3464 * 2.0+: Use provided sampler source. */
3465 if (shader_version < WINED3D_SHADER_VERSION(2,0))
3466 resource_idx = ins->dst[0].reg.idx[0].offset;
3467 else
3468 resource_idx = ins->src[1].reg.idx[0].offset;
3470 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3472 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3473 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3474 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
3476 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3477 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3479 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3480 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3482 case WINED3D_TTFF_COUNT1:
3483 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3484 break;
3485 case WINED3D_TTFF_COUNT2:
3486 mask = WINED3DSP_WRITEMASK_1;
3487 break;
3488 case WINED3D_TTFF_COUNT3:
3489 mask = WINED3DSP_WRITEMASK_2;
3490 break;
3491 case WINED3D_TTFF_COUNT4:
3492 case WINED3D_TTFF_DISABLE:
3493 mask = WINED3DSP_WRITEMASK_3;
3494 break;
3498 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3500 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3502 if (src_mod == WINED3DSPSM_DZ) {
3503 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3504 mask = WINED3DSP_WRITEMASK_2;
3505 } else if (src_mod == WINED3DSPSM_DW) {
3506 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3507 mask = WINED3DSP_WRITEMASK_3;
3510 else
3512 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
3513 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3515 /* ps 2.0 texldp instruction always divides by the fourth component. */
3516 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3517 mask = WINED3DSP_WRITEMASK_3;
3521 if (priv->cur_ps_args->np2_fixup & (1 << resource_idx))
3522 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3524 shader_glsl_get_sample_function(ins->ctx, resource_idx, sample_flags, &sample_function);
3525 mask |= sample_function.coord_mask;
3527 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3528 else swizzle = ins->src[1].swizzle;
3530 /* 1.0-1.3: Use destination register as coordinate source.
3531 1.4+: Use provided coordinate source register. */
3532 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3534 char coord_mask[6];
3535 shader_glsl_write_mask_to_str(mask, coord_mask);
3536 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
3537 "T%u%s", resource_idx, coord_mask);
3539 else
3541 struct glsl_src_param coord_param;
3542 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3543 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3545 struct glsl_src_param bias;
3546 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3547 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3548 "%s", coord_param.param_str);
3549 } else {
3550 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
3551 "%s", coord_param.param_str);
3556 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3558 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3559 struct glsl_src_param coord_param, dx_param, dy_param;
3560 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3561 struct glsl_sample_function sample_function;
3562 DWORD sampler_idx;
3563 DWORD swizzle = ins->src[1].swizzle;
3564 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3566 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3568 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3569 shader_glsl_tex(ins);
3570 return;
3573 sampler_idx = ins->src[1].reg.idx[0].offset;
3574 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3575 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3577 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3578 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3579 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3580 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3582 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3583 "%s", coord_param.param_str);
3586 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3588 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3589 struct glsl_src_param coord_param, lod_param;
3590 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3591 struct glsl_sample_function sample_function;
3592 DWORD sampler_idx;
3593 DWORD swizzle = ins->src[1].swizzle;
3594 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3596 sampler_idx = ins->src[1].reg.idx[0].offset;
3597 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3598 && priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3599 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3601 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3602 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3604 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3606 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3607 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
3609 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
3610 * However, the NVIDIA drivers allow them in fragment shaders as well,
3611 * even without the appropriate extension. */
3612 WARN("Using %s in fragment shader.\n", sample_function.name);
3614 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3615 "%s", coord_param.param_str);
3618 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
3619 unsigned int resource_idx, unsigned int sampler_idx)
3621 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
3622 unsigned int i;
3624 for (i = 0; i < sampler_map->count; ++i)
3626 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
3627 return entries[i].bind_idx;
3630 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
3632 return ~0u;
3635 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
3637 struct glsl_sample_function sample_function;
3638 struct glsl_src_param coord_param;
3639 unsigned int sampler_idx;
3641 shader_glsl_get_sample_function(ins->ctx, ins->src[1].reg.idx[0].offset, 0, &sample_function);
3642 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3643 sampler_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
3644 ins->src[1].reg.idx[0].offset, ins->src[2].reg.idx[0].offset);
3645 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE,
3646 NULL, NULL, NULL, "%s", coord_param.param_str);
3649 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3651 /* FIXME: Make this work for more than just 2D textures */
3652 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3653 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3655 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3657 char dst_mask[6];
3659 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3660 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3661 ins->dst[0].reg.idx[0].offset, dst_mask);
3663 else
3665 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3666 DWORD reg = ins->src[0].reg.idx[0].offset;
3667 char dst_swizzle[6];
3669 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3671 if (src_mod == WINED3DSPSM_DZ)
3673 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3674 struct glsl_src_param div_param;
3676 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3678 if (mask_size > 1) {
3679 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3680 } else {
3681 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3684 else if (src_mod == WINED3DSPSM_DW)
3686 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3687 struct glsl_src_param div_param;
3689 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3691 if (mask_size > 1) {
3692 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3693 } else {
3694 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3696 } else {
3697 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3702 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3703 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3704 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3705 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3707 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3708 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3709 struct glsl_sample_function sample_function;
3710 struct glsl_src_param src0_param;
3711 UINT mask_size;
3713 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3715 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3716 * scalar, and projected sampling would require 4.
3718 * It is a dependent read - not valid with conditional NP2 textures
3720 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3721 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3723 switch(mask_size)
3725 case 1:
3726 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3727 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3728 break;
3730 case 2:
3731 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3732 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3733 break;
3735 case 3:
3736 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3737 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3738 break;
3740 default:
3741 FIXME("Unexpected mask size %u\n", mask_size);
3742 break;
3746 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3747 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3748 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3750 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3751 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3752 struct glsl_src_param src0_param;
3753 DWORD dst_mask;
3754 unsigned int mask_size;
3756 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3757 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3758 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3760 if (mask_size > 1) {
3761 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3762 } else {
3763 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3767 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3768 * Calculate the depth as dst.x / dst.y */
3769 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3771 struct glsl_dst_param dst_param;
3773 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3775 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3776 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3777 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3778 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3779 * >= 1.0 or < 0.0
3781 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3782 dst_param.reg_name, dst_param.reg_name);
3785 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3786 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3787 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3788 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3790 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3792 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3793 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3794 struct glsl_src_param src0_param;
3796 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3798 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3799 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3802 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3803 * Calculate the 1st of a 2-row matrix multiplication. */
3804 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3806 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3807 DWORD reg = ins->dst[0].reg.idx[0].offset;
3808 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3809 struct glsl_src_param src0_param;
3811 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3812 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3815 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3816 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3817 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3819 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3820 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3821 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3822 DWORD reg = ins->dst[0].reg.idx[0].offset;
3823 struct glsl_src_param src0_param;
3825 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3826 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
3827 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
3830 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3832 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3833 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3834 struct glsl_sample_function sample_function;
3835 DWORD reg = ins->dst[0].reg.idx[0].offset;
3836 struct glsl_src_param src0_param;
3838 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3839 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3841 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3843 /* Sample the texture using the calculated coordinates */
3844 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
3847 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3848 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3849 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3851 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3852 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3853 struct glsl_sample_function sample_function;
3854 DWORD reg = ins->dst[0].reg.idx[0].offset;
3855 struct glsl_src_param src0_param;
3857 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3858 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3860 /* Dependent read, not valid with conditional NP2 */
3861 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3863 /* Sample the texture using the calculated coordinates */
3864 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3866 tex_mx->current_row = 0;
3869 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3870 * Perform the 3rd row of a 3x3 matrix multiply */
3871 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
3873 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3874 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3875 DWORD reg = ins->dst[0].reg.idx[0].offset;
3876 struct glsl_src_param src0_param;
3877 char dst_mask[6];
3879 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3881 shader_glsl_append_dst(ins->ctx->buffer, ins);
3882 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3883 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
3885 tex_mx->current_row = 0;
3888 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3889 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3890 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
3892 struct glsl_src_param src0_param;
3893 struct glsl_src_param src1_param;
3894 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3895 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3896 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3897 struct glsl_sample_function sample_function;
3898 DWORD reg = ins->dst[0].reg.idx[0].offset;
3899 char coord_mask[6];
3901 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3902 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3904 /* Perform the last matrix multiply operation */
3905 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3906 /* Reflection calculation */
3907 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
3909 /* Dependent read, not valid with conditional NP2 */
3910 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3911 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3913 /* Sample the texture */
3914 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3915 NULL, NULL, NULL, "tmp0%s", coord_mask);
3917 tex_mx->current_row = 0;
3920 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3921 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3922 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
3924 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3925 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3926 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3927 struct glsl_sample_function sample_function;
3928 DWORD reg = ins->dst[0].reg.idx[0].offset;
3929 struct glsl_src_param src0_param;
3930 char coord_mask[6];
3932 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3934 /* Perform the last matrix multiply operation */
3935 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
3937 /* Construct the eye-ray vector from w coordinates */
3938 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3939 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
3940 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3942 /* Dependent read, not valid with conditional NP2 */
3943 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3944 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3946 /* Sample the texture using the calculated coordinates */
3947 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3948 NULL, NULL, NULL, "tmp0%s", coord_mask);
3950 tex_mx->current_row = 0;
3953 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3954 * Apply a fake bump map transform.
3955 * texbem is pshader <= 1.3 only, this saves a few version checks
3957 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
3959 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3960 struct glsl_sample_function sample_function;
3961 struct glsl_src_param coord_param;
3962 DWORD sampler_idx;
3963 DWORD mask;
3964 DWORD flags;
3965 char coord_mask[6];
3967 sampler_idx = ins->dst[0].reg.idx[0].offset;
3968 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3969 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3971 /* Dependent read, not valid with conditional NP2 */
3972 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3973 mask = sample_function.coord_mask;
3975 shader_glsl_write_mask_to_str(mask, coord_mask);
3977 /* With projected textures, texbem only divides the static texture coord,
3978 * not the displacement, so we can't let GL handle this. */
3979 if (flags & WINED3D_PSARGS_PROJECTED)
3981 DWORD div_mask=0;
3982 char coord_div_mask[3];
3983 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3985 case WINED3D_TTFF_COUNT1:
3986 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3987 break;
3988 case WINED3D_TTFF_COUNT2:
3989 div_mask = WINED3DSP_WRITEMASK_1;
3990 break;
3991 case WINED3D_TTFF_COUNT3:
3992 div_mask = WINED3DSP_WRITEMASK_2;
3993 break;
3994 case WINED3D_TTFF_COUNT4:
3995 case WINED3D_TTFF_DISABLE:
3996 div_mask = WINED3DSP_WRITEMASK_3;
3997 break;
3999 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4000 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4003 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4005 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4006 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4007 coord_param.param_str, coord_mask);
4009 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4011 struct glsl_src_param luminance_param;
4012 struct glsl_dst_param dst_param;
4014 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4015 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4017 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4018 dst_param.reg_name, dst_param.mask_str,
4019 luminance_param.param_str, sampler_idx, sampler_idx);
4023 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4025 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4026 struct glsl_src_param src0_param, src1_param;
4028 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4029 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4031 shader_glsl_append_dst(ins->ctx->buffer, ins);
4032 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4033 src0_param.param_str, sampler_idx, src1_param.param_str);
4036 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4037 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4038 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4040 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4041 struct glsl_sample_function sample_function;
4042 struct glsl_src_param src0_param;
4044 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4046 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4047 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4048 "%s.wx", src0_param.reg_name);
4051 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4052 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4053 static void shader_glsl_texreg2gb(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.yz", src0_param.reg_name);
4066 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4067 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4068 static void shader_glsl_texreg2rgb(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 /* Dependent read, not valid with conditional NP2 */
4075 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4076 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4078 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4079 "%s", src0_param.param_str);
4082 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4083 * If any of the first 3 components are < 0, discard this pixel */
4084 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4086 struct glsl_dst_param dst_param;
4088 /* The argument is a destination parameter, and no writemasks are allowed */
4089 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4090 if (ins->ctx->reg_maps->shader_version.major >= 2)
4092 if (ins->ctx->reg_maps->shader_version.major >= 4)
4093 FIXME("SM4 discard not implemented.\n");
4094 /* 2.0 shaders compare all 4 components in texkill */
4095 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4096 } else {
4097 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
4098 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
4099 * 4 components are defined, only the first 3 are used
4101 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4105 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4106 * dst = dot2(src0, src1) + src2 */
4107 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4109 struct glsl_src_param src0_param;
4110 struct glsl_src_param src1_param;
4111 struct glsl_src_param src2_param;
4112 DWORD write_mask;
4113 unsigned int mask_size;
4115 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4116 mask_size = shader_glsl_get_write_mask_size(write_mask);
4118 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4119 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4120 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4122 if (mask_size > 1) {
4123 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4124 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4125 } else {
4126 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4127 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4131 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
4132 const struct wined3d_shader_signature_element *input_signature,
4133 const struct wined3d_shader_reg_maps *reg_maps,
4134 enum vertexprocessing_mode vertexprocessing)
4136 WORD map = reg_maps->input_registers;
4137 unsigned int i;
4139 for (i = 0; map; map >>= 1, ++i)
4141 const char *semantic_name;
4142 UINT semantic_idx;
4143 char reg_mask[6];
4145 /* Unused */
4146 if (!(map & 1)) continue;
4148 semantic_name = input_signature[i].semantic_name;
4149 semantic_idx = input_signature[i].semantic_idx;
4150 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
4152 if (vertexprocessing == vertexshader)
4154 if (!strcmp(semantic_name, "SV_POSITION"))
4155 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
4156 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4157 else
4158 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
4159 shader->u.ps.input_reg_map[i], reg_mask, shader->u.ps.input_reg_map[i], reg_mask);
4161 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4163 if (semantic_idx < 8 && vertexprocessing == pretransformed)
4164 shader_addline(buffer, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4165 shader->u.ps.input_reg_map[i], reg_mask, semantic_idx, reg_mask);
4166 else
4167 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4168 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4170 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4172 if (!semantic_idx)
4173 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4174 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4175 else if (semantic_idx == 1)
4176 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4177 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4178 else
4179 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4180 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4182 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[i], reg_mask, reg_mask);
4190 /*********************************************
4191 * Vertex Shader Specific Code begins here
4192 ********************************************/
4194 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4196 struct glsl_program_key key;
4198 key.vs_id = entry->vs.id;
4199 key.gs_id = entry->gs.id;
4200 key.ps_id = entry->ps.id;
4202 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4204 ERR("Failed to insert program entry.\n");
4208 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4209 GLuint vs_id, GLuint gs_id, GLuint ps_id)
4211 struct wine_rb_entry *entry;
4212 struct glsl_program_key key;
4214 key.vs_id = vs_id;
4215 key.gs_id = gs_id;
4216 key.ps_id = ps_id;
4218 entry = wine_rb_get(&priv->program_lookup, &key);
4219 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4222 /* Context activation is done by the caller. */
4223 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4224 struct glsl_shader_prog_link *entry)
4226 struct glsl_program_key key;
4228 key.vs_id = entry->vs.id;
4229 key.gs_id = entry->gs.id;
4230 key.ps_id = entry->ps.id;
4231 wine_rb_remove(&priv->program_lookup, &key);
4233 GL_EXTCALL(glDeleteProgram(entry->id));
4234 if (entry->vs.id)
4235 list_remove(&entry->vs.shader_entry);
4236 if (entry->gs.id)
4237 list_remove(&entry->gs.shader_entry);
4238 if (entry->ps.id)
4239 list_remove(&entry->ps.shader_entry);
4240 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4241 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4242 HeapFree(GetProcessHeap(), 0, entry);
4245 static void handle_ps3_input(struct wined3d_shader_buffer *buffer,
4246 const struct wined3d_gl_info *gl_info, const DWORD *map,
4247 const struct wined3d_shader_signature_element *input_signature,
4248 const struct wined3d_shader_reg_maps *reg_maps_in,
4249 const struct wined3d_shader_signature_element *output_signature,
4250 const struct wined3d_shader_reg_maps *reg_maps_out)
4252 unsigned int i, j;
4253 const char *semantic_name_in;
4254 UINT semantic_idx_in;
4255 DWORD *set;
4256 DWORD in_idx;
4257 unsigned int in_count = vec4_varyings(3, gl_info);
4258 char reg_mask[6];
4259 char destination[50];
4260 WORD input_map, output_map;
4262 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
4264 input_map = reg_maps_in->input_registers;
4265 for (i = 0; input_map; input_map >>= 1, ++i)
4267 if (!(input_map & 1)) continue;
4269 in_idx = map[i];
4270 /* Declared, but not read register */
4271 if (in_idx == ~0U) continue;
4272 if (in_idx >= (in_count + 2))
4274 FIXME("More input varyings declared than supported, expect issues.\n");
4275 continue;
4278 if (in_idx == in_count)
4279 sprintf(destination, "gl_FrontColor");
4280 else if (in_idx == in_count + 1)
4281 sprintf(destination, "gl_FrontSecondaryColor");
4282 else
4283 sprintf(destination, "ps_link[%u]", in_idx);
4285 semantic_name_in = input_signature[i].semantic_name;
4286 semantic_idx_in = input_signature[i].semantic_idx;
4287 set[in_idx] = ~0U;
4289 output_map = reg_maps_out->output_registers;
4290 for (j = 0; output_map; output_map >>= 1, ++j)
4292 DWORD mask;
4294 if (!(output_map & 1)
4295 || semantic_idx_in != output_signature[j].semantic_idx
4296 || strcmp(semantic_name_in, output_signature[j].semantic_name)
4297 || !(mask = input_signature[i].mask & output_signature[j].mask))
4298 continue;
4300 set[in_idx] = mask;
4301 shader_glsl_write_mask_to_str(mask, reg_mask);
4303 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4304 destination, reg_mask, j, reg_mask);
4308 for (i = 0; i < in_count + 2; ++i)
4310 unsigned int size;
4312 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4313 continue;
4315 if (set[i] == ~0U) set[i] = 0;
4317 size = 0;
4318 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4319 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4320 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4321 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4322 reg_mask[size] = '\0';
4324 if (i == in_count)
4325 sprintf(destination, "gl_FrontColor");
4326 else if (i == in_count + 1)
4327 sprintf(destination, "gl_FrontSecondaryColor");
4328 else
4329 sprintf(destination, "ps_link[%u]", i);
4331 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
4332 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
4335 HeapFree(GetProcessHeap(), 0, set);
4338 /* Context activation is done by the caller. */
4339 static GLuint generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
4340 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4341 const struct wined3d_gl_info *gl_info)
4343 GLuint ret = 0;
4344 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4345 unsigned int i;
4346 const char *semantic_name;
4347 UINT semantic_idx;
4348 char reg_mask[6];
4349 const struct wined3d_shader_signature_element *output_signature = vs->output_signature;
4350 WORD map = vs->reg_maps.output_registers;
4352 shader_buffer_clear(buffer);
4354 shader_addline(buffer, "#version 120\n");
4356 if (ps_major < 3)
4358 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4360 for (i = 0; map; map >>= 1, ++i)
4362 DWORD write_mask;
4364 if (!(map & 1)) continue;
4366 semantic_name = output_signature[i].semantic_name;
4367 semantic_idx = output_signature[i].semantic_idx;
4368 write_mask = output_signature[i].mask;
4369 shader_glsl_write_mask_to_str(write_mask, reg_mask);
4371 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4373 if (!semantic_idx)
4374 shader_addline(buffer, "gl_FrontColor%s = vs_out[%u]%s;\n",
4375 reg_mask, i, reg_mask);
4376 else if (semantic_idx == 1)
4377 shader_addline(buffer, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4378 reg_mask, i, reg_mask);
4380 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4382 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4383 reg_mask, i, reg_mask);
4385 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4387 if (semantic_idx < 8)
4389 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
4390 write_mask |= WINED3DSP_WRITEMASK_3;
4392 shader_addline(buffer, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4393 semantic_idx, reg_mask, i, reg_mask);
4394 if (!(write_mask & WINED3DSP_WRITEMASK_3))
4395 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
4398 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4400 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4402 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
4404 shader_addline(buffer, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n", i, reg_mask[1]);
4407 shader_addline(buffer, "}\n");
4409 else
4411 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
4412 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4413 shader_addline(buffer, "varying vec4 ps_link[%u];\n", in_count);
4414 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4416 /* First, sort out position and point size. Those are not passed to the pixel shader */
4417 for (i = 0; map; map >>= 1, ++i)
4419 if (!(map & 1)) continue;
4421 semantic_name = output_signature[i].semantic_name;
4422 shader_glsl_write_mask_to_str(output_signature[i].mask, reg_mask);
4424 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4426 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4427 reg_mask, i, reg_mask);
4429 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4431 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4435 /* Then, fix the pixel shader input */
4436 handle_ps3_input(buffer, gl_info, ps->u.ps.input_reg_map, ps->input_signature,
4437 &ps->reg_maps, output_signature, &vs->reg_maps);
4439 shader_addline(buffer, "}\n");
4442 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
4443 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
4444 shader_glsl_compile(gl_info, ret, buffer->buffer);
4446 return ret;
4449 static void shader_glsl_generate_srgb_write_correction(struct wined3d_shader_buffer *buffer)
4451 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4452 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4453 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4454 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4455 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4456 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4459 static void shader_glsl_generate_fog_code(struct wined3d_shader_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
4461 switch (mode)
4463 case WINED3D_FFP_PS_FOG_OFF:
4464 return;
4466 case WINED3D_FFP_PS_FOG_LINEAR:
4467 shader_addline(buffer, "float Fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;\n");
4468 break;
4470 case WINED3D_FFP_PS_FOG_EXP:
4471 /* Fog = e^-(gl_Fog.density * gl_FogFragCoord) */
4472 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4473 break;
4475 case WINED3D_FFP_PS_FOG_EXP2:
4476 /* Fog = e^-((gl_Fog.density * gl_FogFragCoord)^2) */
4477 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4478 break;
4480 default:
4481 ERR("Invalid fog mode %#x.\n", mode);
4482 return;
4485 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, clamp(Fog, 0.0, 1.0));\n");
4488 /* Context activation is done by the caller. */
4489 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
4490 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4491 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
4493 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4494 const struct wined3d_gl_info *gl_info = context->gl_info;
4495 const DWORD *function = shader->function;
4496 struct shader_glsl_ctx_priv priv_ctx;
4498 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4499 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
4501 memset(&priv_ctx, 0, sizeof(priv_ctx));
4502 priv_ctx.cur_ps_args = args;
4503 priv_ctx.cur_np2fixup_info = np2fixup_info;
4505 shader_addline(buffer, "#version 120\n");
4507 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4508 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4509 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4510 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
4511 /* The spec says that it doesn't have to be explicitly enabled, but the
4512 * nvidia drivers write a warning if we don't do so. */
4513 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4514 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4515 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4516 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4517 if (gl_info->supported[EXT_GPU_SHADER4])
4518 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4520 /* Base Declarations */
4521 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4523 /* Pack 3.0 inputs */
4524 if (reg_maps->shader_version.major >= 3)
4525 shader_glsl_input_pack(shader, buffer, shader->input_signature, reg_maps, args->vp_mode);
4527 /* Base Shader Body */
4528 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4530 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4531 if (reg_maps->shader_version.major < 2)
4533 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4534 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4537 if (args->srgb_correction)
4538 shader_glsl_generate_srgb_write_correction(buffer);
4540 /* SM < 3 does not replace the fog stage. */
4541 if (reg_maps->shader_version.major < 3)
4542 shader_glsl_generate_fog_code(buffer, args->fog);
4544 shader_addline(buffer, "}\n");
4546 TRACE("Compiling shader object %u.\n", shader_id);
4547 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4549 return shader_id;
4552 /* Context activation is done by the caller. */
4553 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4554 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4555 const struct vs_compile_args *args)
4557 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4558 const struct wined3d_gl_info *gl_info = context->gl_info;
4559 const DWORD *function = shader->function;
4560 struct shader_glsl_ctx_priv priv_ctx;
4562 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4563 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
4565 shader_addline(buffer, "#version 120\n");
4567 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4568 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4569 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4570 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4571 if (gl_info->supported[EXT_GPU_SHADER4])
4572 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4574 memset(&priv_ctx, 0, sizeof(priv_ctx));
4575 priv_ctx.cur_vs_args = args;
4577 /* Base Declarations */
4578 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4580 /* Base Shader Body */
4581 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4583 /* Unpack outputs */
4584 shader_addline(buffer, "order_ps_input(vs_out);\n");
4586 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4587 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4588 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4589 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4591 if (args->fog_src == VS_FOG_Z)
4592 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4593 else if (!reg_maps->fog)
4594 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4596 /* We always store the clipplanes without y inversion */
4597 if (args->clip_enabled)
4598 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4600 /* Write the final position.
4602 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4603 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4604 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4605 * contains 1.0 to allow a mad.
4607 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4608 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4610 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4612 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4613 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4614 * which is the same as z = z * 2 - w.
4616 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4618 shader_addline(buffer, "}\n");
4620 TRACE("Compiling shader object %u.\n", shader_id);
4621 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4623 return shader_id;
4626 /* Context activation is done by the caller. */
4627 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
4628 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader)
4630 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4631 const struct wined3d_gl_info *gl_info = context->gl_info;
4632 const DWORD *function = shader->function;
4633 struct shader_glsl_ctx_priv priv_ctx;
4634 GLuint shader_id;
4636 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
4638 shader_addline(buffer, "#version 120\n");
4640 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
4641 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
4642 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4643 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4644 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4645 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4646 if (gl_info->supported[EXT_GPU_SHADER4])
4647 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4649 memset(&priv_ctx, 0, sizeof(priv_ctx));
4650 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4651 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4652 shader_addline(buffer, "}\n");
4654 TRACE("Compiling shader object %u.\n", shader_id);
4655 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4657 return shader_id;
4660 static GLuint find_glsl_pshader(const struct wined3d_context *context,
4661 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4662 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
4664 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
4665 struct glsl_shader_private *shader_data;
4666 struct ps_np2fixup_info *np2fixup;
4667 UINT i;
4668 DWORD new_size;
4669 GLuint ret;
4671 if (!shader->backend_data)
4673 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4674 if (!shader->backend_data)
4676 ERR("Failed to allocate backend data.\n");
4677 return 0;
4680 shader_data = shader->backend_data;
4681 gl_shaders = shader_data->gl_shaders.ps;
4683 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4684 * so a linear search is more performant than a hashmap or a binary search
4685 * (cache coherency etc)
4687 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4689 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
4691 if (args->np2_fixup)
4692 *np2fixup_info = &gl_shaders[i].np2fixup;
4693 return gl_shaders[i].id;
4697 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4698 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4699 if (shader_data->num_gl_shaders)
4701 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4702 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
4703 new_size * sizeof(*gl_shaders));
4705 else
4707 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4708 new_size = 1;
4711 if(!new_array) {
4712 ERR("Out of memory\n");
4713 return 0;
4715 shader_data->gl_shaders.ps = new_array;
4716 shader_data->shader_array_size = new_size;
4717 gl_shaders = new_array;
4720 gl_shaders[shader_data->num_gl_shaders].args = *args;
4722 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
4723 memset(np2fixup, 0, sizeof(*np2fixup));
4724 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
4726 pixelshader_update_resource_types(shader, args->tex_types);
4728 shader_buffer_clear(buffer);
4729 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4730 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4732 return ret;
4735 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4736 const DWORD use_map) {
4737 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4738 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4739 return stored->fog_src == new->fog_src;
4742 static GLuint find_glsl_vshader(const struct wined3d_context *context,
4743 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4744 const struct vs_compile_args *args)
4746 UINT i;
4747 DWORD new_size;
4748 DWORD use_map = context->stream_info.use_map;
4749 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
4750 struct glsl_shader_private *shader_data;
4751 GLuint ret;
4753 if (!shader->backend_data)
4755 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4756 if (!shader->backend_data)
4758 ERR("Failed to allocate backend data.\n");
4759 return 0;
4762 shader_data = shader->backend_data;
4763 gl_shaders = shader_data->gl_shaders.vs;
4765 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4766 * so a linear search is more performant than a hashmap or a binary search
4767 * (cache coherency etc)
4769 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4771 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
4772 return gl_shaders[i].id;
4775 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4777 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4778 if (shader_data->num_gl_shaders)
4780 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4781 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
4782 new_size * sizeof(*gl_shaders));
4784 else
4786 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4787 new_size = 1;
4790 if(!new_array) {
4791 ERR("Out of memory\n");
4792 return 0;
4794 shader_data->gl_shaders.vs = new_array;
4795 shader_data->shader_array_size = new_size;
4796 gl_shaders = new_array;
4799 gl_shaders[shader_data->num_gl_shaders].args = *args;
4801 shader_buffer_clear(buffer);
4802 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4803 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4805 return ret;
4808 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
4809 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader)
4811 struct glsl_gs_compiled_shader *gl_shaders;
4812 struct glsl_shader_private *shader_data;
4813 GLuint ret;
4815 if (!shader->backend_data)
4817 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
4819 ERR("Failed to allocate backend data.\n");
4820 return 0;
4823 shader_data = shader->backend_data;
4824 gl_shaders = shader_data->gl_shaders.gs;
4826 if (shader_data->num_gl_shaders)
4827 return gl_shaders[0].id;
4829 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4831 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
4833 ERR("Failed to allocate GL shader array.\n");
4834 return 0;
4836 shader_data->shader_array_size = 1;
4837 gl_shaders = shader_data->gl_shaders.gs;
4839 shader_buffer_clear(buffer);
4840 ret = shader_glsl_generate_geometry_shader(context, buffer, shader);
4841 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4843 return ret;
4846 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
4848 switch (mcs)
4850 case WINED3D_MCS_MATERIAL:
4851 return material;
4852 case WINED3D_MCS_COLOR1:
4853 return "gl_Color";
4854 case WINED3D_MCS_COLOR2:
4855 return "gl_SecondaryColor";
4856 default:
4857 ERR("Invalid material color source %#x.\n", mcs);
4858 return "<invalid>";
4862 static void shader_glsl_ffp_vertex_lighting(struct wined3d_shader_buffer *buffer,
4863 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4865 const char *diffuse, *specular, *emission, *ambient;
4866 enum wined3d_light_type light_type;
4867 unsigned int i;
4869 if (!settings->lighting)
4871 shader_addline(buffer, "gl_FrontColor = gl_Color;\n");
4872 shader_addline(buffer, "gl_FrontSecondaryColor = gl_SecondaryColor;\n");
4873 return;
4876 shader_addline(buffer, "vec3 ambient = gl_LightModel.ambient.xyz;\n");
4877 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
4878 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
4879 shader_addline(buffer, "vec3 dir, dst;\n");
4880 shader_addline(buffer, "float att, t;\n");
4882 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "gl_FrontMaterial.ambient");
4883 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "gl_FrontMaterial.diffuse");
4884 specular = shader_glsl_ffp_mcs(settings->specular_source, "gl_FrontMaterial.specular");
4885 emission = shader_glsl_ffp_mcs(settings->emission_source, "gl_FrontMaterial.emission");
4887 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
4889 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
4890 switch (light_type)
4892 case WINED3D_LIGHT_POINT:
4893 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4894 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4895 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4896 shader_addline(buffer, "dst.x = 1.0;\n");
4897 shader_addline(buffer, "att = dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4898 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n", i, i, i);
4899 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz / att;\n", i);
4900 if (!settings->normal)
4901 break;
4902 shader_addline(buffer, "dir = normalize(dir);\n");
4903 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
4904 " * gl_LightSource[%u].diffuse.xyz) / att;\n", i);
4905 if (settings->localviewer)
4906 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4907 else
4908 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4909 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4910 " * gl_LightSource[%u].specular) / att;\n", i);
4911 break;
4913 case WINED3D_LIGHT_SPOT:
4914 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4915 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4916 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4917 shader_addline(buffer, "dst.x = 1.0;\n");
4918 shader_addline(buffer, "dir = normalize(dir);\n");
4919 shader_addline(buffer, "t = dot(-dir, normalize(gl_LightSource[%u].spotDirection));\n", i);
4920 shader_addline(buffer, "if (t < gl_LightSource[%u].spotCosCutoff) att = 0.0;\n", i);
4921 shader_addline(buffer, "else att = pow(t, gl_LightSource[%u].spotExponent)"
4922 " / dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4923 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n",
4924 i, i, i, i);
4925 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz * att;\n", i);
4926 if (!settings->normal)
4927 break;
4928 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
4929 " * gl_LightSource[%u].diffuse.xyz) * att;\n", i);
4930 if (settings->localviewer)
4931 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4932 else
4933 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4934 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4935 " * gl_LightSource[%u].specular) * att;\n", i);
4936 break;
4938 case WINED3D_LIGHT_DIRECTIONAL:
4939 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz;\n", i);
4940 if (!settings->normal)
4941 break;
4942 shader_addline(buffer, "dir = normalize(gl_LightSource[%u].position.xyz);\n", i);
4943 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
4944 " * gl_LightSource[%u].diffuse.xyz;\n", i);
4945 shader_addline(buffer, "t = dot(normal, gl_LightSource[%u].halfVector.xyz);\n", i);
4946 shader_addline(buffer, "if (t > 0.0) specular += pow(t, gl_FrontMaterial.shininess)"
4947 " * gl_LightSource[%u].specular;\n", i);
4948 break;
4950 default:
4951 if (light_type)
4952 FIXME("Unhandled light type %#x.\n", light_type);
4953 continue;
4957 shader_addline(buffer, "gl_FrontColor.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
4958 ambient, diffuse, emission);
4959 shader_addline(buffer, "gl_FrontColor.w = %s.w;\n", diffuse);
4960 shader_addline(buffer, "gl_FrontSecondaryColor = %s * specular;\n", specular);
4963 /* Context activation is done by the caller. */
4964 static GLuint shader_glsl_generate_ffp_vertex_shader(struct wined3d_shader_buffer *buffer,
4965 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4967 GLuint shader_obj;
4968 unsigned int i;
4970 shader_buffer_clear(buffer);
4972 shader_addline(buffer, "#version 120\n");
4973 shader_addline(buffer, "\n");
4974 shader_addline(buffer, "void main()\n{\n");
4975 shader_addline(buffer, "float m;\n");
4976 shader_addline(buffer, "vec3 r;\n");
4978 if (settings->transformed)
4980 shader_addline(buffer, "vec4 ec_pos = vec4(gl_Vertex.xyz, 1.0);\n");
4981 shader_addline(buffer, "gl_Position = gl_ProjectionMatrix * ec_pos;\n");
4982 shader_addline(buffer, "if (gl_Vertex.w != 0.0) gl_Position /= gl_Vertex.w;\n");
4984 else
4986 shader_addline(buffer, "vec4 ec_pos = gl_ModelViewMatrix * gl_Vertex;\n");
4987 shader_addline(buffer, "gl_Position = gl_ProjectionMatrix * ec_pos;\n");
4988 if (settings->clipping)
4989 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
4990 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
4993 if (!settings->normal)
4994 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
4995 else if (settings->normalize)
4996 shader_addline(buffer, "vec3 normal = normalize(gl_NormalMatrix * gl_Normal);\n");
4997 else
4998 shader_addline(buffer, "vec3 normal = gl_NormalMatrix * gl_Normal;\n");
5000 shader_glsl_ffp_vertex_lighting(buffer, settings, gl_info);
5002 for (i = 0; i < MAX_TEXTURES; ++i)
5004 switch (settings->texgen[i] << WINED3D_FFP_TCI_SHIFT)
5006 case WINED3DTSS_TCI_PASSTHRU:
5007 if (settings->texcoords & (1 << i))
5008 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * gl_MultiTexCoord%d;\n",
5009 i, i, i);
5010 break;
5012 case WINED3DTSS_TCI_CAMERASPACENORMAL:
5013 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * vec4(normal, 1.0);\n", i, i);
5014 break;
5016 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
5017 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * ec_pos;\n", i, i);
5018 break;
5020 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
5021 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
5022 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
5023 break;
5025 case WINED3DTSS_TCI_SPHEREMAP:
5026 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
5027 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
5028 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
5029 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
5030 break;
5032 default:
5033 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
5034 break;
5038 switch (settings->fog_mode)
5040 case WINED3D_FFP_VS_FOG_OFF:
5041 break;
5043 case WINED3D_FFP_VS_FOG_FOGCOORD:
5044 shader_addline(buffer, "gl_FogFragCoord = gl_SecondaryColor.w * 255.0;\n");
5045 break;
5047 case WINED3D_FFP_VS_FOG_RANGE:
5048 shader_addline(buffer, "gl_FogFragCoord = length(ec_pos.xyz);\n");
5049 break;
5051 case WINED3D_FFP_VS_FOG_DEPTH:
5052 if (settings->ortho_fog)
5053 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
5054 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z * 0.5 + 0.5;\n");
5055 else if (settings->transformed)
5056 shader_addline(buffer, "gl_FogFragCoord = ec_pos.z;\n");
5057 else
5058 shader_addline(buffer, "gl_FogFragCoord = abs(ec_pos.z);\n");
5059 break;
5061 default:
5062 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
5063 break;
5066 if (settings->point_size)
5068 shader_addline(buffer, "gl_PointSize = gl_Point.size / sqrt(gl_Point.distanceConstantAttenuation"
5069 " + gl_Point.distanceLinearAttenuation * length(ec_pos.xyz)"
5070 " + gl_Point.distanceQuadraticAttenuation * dot(ec_pos.xyz, ec_pos.xyz));\n");
5071 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, gl_Point.sizeMin, gl_Point.sizeMax);\n");
5074 shader_addline(buffer, "}\n");
5076 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5077 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5079 return shader_obj;
5082 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_shader_buffer *buffer,
5083 DWORD argnum, unsigned int stage, DWORD arg)
5085 const char *ret;
5087 if (arg == ARG_UNUSED)
5088 return "<unused arg>";
5090 switch (arg & WINED3DTA_SELECTMASK)
5092 case WINED3DTA_DIFFUSE:
5093 ret = "gl_Color";
5094 break;
5096 case WINED3DTA_CURRENT:
5097 if (!stage)
5098 ret = "gl_Color";
5099 else
5100 ret = "ret";
5101 break;
5103 case WINED3DTA_TEXTURE:
5104 switch (stage)
5106 case 0: ret = "tex0"; break;
5107 case 1: ret = "tex1"; break;
5108 case 2: ret = "tex2"; break;
5109 case 3: ret = "tex3"; break;
5110 case 4: ret = "tex4"; break;
5111 case 5: ret = "tex5"; break;
5112 case 6: ret = "tex6"; break;
5113 case 7: ret = "tex7"; break;
5114 default:
5115 ret = "<invalid texture>";
5116 break;
5118 break;
5120 case WINED3DTA_TFACTOR:
5121 ret = "tex_factor";
5122 break;
5124 case WINED3DTA_SPECULAR:
5125 ret = "gl_SecondaryColor";
5126 break;
5128 case WINED3DTA_TEMP:
5129 ret = "temp_reg";
5130 break;
5132 case WINED3DTA_CONSTANT:
5133 switch (stage)
5135 case 0: ret = "tss_const0"; break;
5136 case 1: ret = "tss_const1"; break;
5137 case 2: ret = "tss_const2"; break;
5138 case 3: ret = "tss_const3"; break;
5139 case 4: ret = "tss_const4"; break;
5140 case 5: ret = "tss_const5"; break;
5141 case 6: ret = "tss_const6"; break;
5142 case 7: ret = "tss_const7"; break;
5143 default:
5144 ret = "<invalid constant>";
5145 break;
5147 break;
5149 default:
5150 return "<unhandled arg>";
5153 if (arg & WINED3DTA_COMPLEMENT)
5155 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
5156 if (argnum == 0)
5157 ret = "arg0";
5158 else if (argnum == 1)
5159 ret = "arg1";
5160 else if (argnum == 2)
5161 ret = "arg2";
5164 if (arg & WINED3DTA_ALPHAREPLICATE)
5166 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
5167 if (argnum == 0)
5168 ret = "arg0";
5169 else if (argnum == 1)
5170 ret = "arg1";
5171 else if (argnum == 2)
5172 ret = "arg2";
5175 return ret;
5178 static void shader_glsl_ffp_fragment_op(struct wined3d_shader_buffer *buffer, unsigned int stage, BOOL color,
5179 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
5181 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5183 if (color && alpha)
5184 dstmask = "";
5185 else if (color)
5186 dstmask = ".xyz";
5187 else
5188 dstmask = ".w";
5190 if (dst == tempreg)
5191 dstreg = "temp_reg";
5192 else
5193 dstreg = "ret";
5195 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
5196 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
5197 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
5199 switch (op)
5201 case WINED3D_TOP_DISABLE:
5202 if (!stage)
5203 shader_addline(buffer, "%s%s = gl_Color%s;\n", dstreg, dstmask, dstmask);
5204 break;
5206 case WINED3D_TOP_SELECT_ARG1:
5207 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
5208 break;
5210 case WINED3D_TOP_SELECT_ARG2:
5211 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
5212 break;
5214 case WINED3D_TOP_MODULATE:
5215 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5216 break;
5218 case WINED3D_TOP_MODULATE_4X:
5219 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
5220 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5221 break;
5223 case WINED3D_TOP_MODULATE_2X:
5224 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
5225 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5226 break;
5228 case WINED3D_TOP_ADD:
5229 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
5230 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5231 break;
5233 case WINED3D_TOP_ADD_SIGNED:
5234 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
5235 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5236 break;
5238 case WINED3D_TOP_ADD_SIGNED_2X:
5239 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
5240 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5241 break;
5243 case WINED3D_TOP_SUBTRACT:
5244 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
5245 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5246 break;
5248 case WINED3D_TOP_ADD_SMOOTH:
5249 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
5250 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
5251 break;
5253 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
5254 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5255 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5256 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5257 break;
5259 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5260 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5261 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5262 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5263 break;
5265 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5266 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
5267 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5268 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5269 break;
5271 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5272 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5273 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5274 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
5275 break;
5277 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
5278 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
5279 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5280 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5281 break;
5283 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
5284 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
5285 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5286 break;
5288 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
5289 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
5290 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5291 break;
5293 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
5294 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5295 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5296 break;
5297 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
5298 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
5299 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5300 break;
5302 case WINED3D_TOP_BUMPENVMAP:
5303 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5304 /* These are handled in the first pass, nothing to do. */
5305 break;
5307 case WINED3D_TOP_DOTPRODUCT3:
5308 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
5309 dstreg, dstmask, arg1, arg2, dstmask);
5310 break;
5312 case WINED3D_TOP_MULTIPLY_ADD:
5313 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
5314 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
5315 break;
5317 case WINED3D_TOP_LERP:
5318 /* MSDN isn't quite right here. */
5319 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
5320 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
5321 break;
5323 default:
5324 FIXME("Unhandled operation %#x.\n", op);
5325 break;
5329 /* Context activation is done by the caller. */
5330 static GLuint shader_glsl_generate_ffp_fragment_shader(struct wined3d_shader_buffer *buffer,
5331 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
5333 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
5334 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
5335 const char *final_combiner_src = "ret";
5336 UINT lowest_disabled_stage;
5337 GLuint shader_id;
5338 DWORD arg0, arg1, arg2;
5339 unsigned int stage;
5341 shader_buffer_clear(buffer);
5343 /* Find out which textures are read */
5344 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5346 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5347 break;
5349 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
5350 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
5351 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
5353 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5354 tex_map |= 1 << stage;
5355 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5356 tfactor_used = TRUE;
5357 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5358 tempreg_used = TRUE;
5359 if (settings->op[stage].dst == tempreg)
5360 tempreg_used = TRUE;
5361 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5362 tss_const_map |= 1 << stage;
5364 switch (settings->op[stage].cop)
5366 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5367 lum_map |= 1 << stage;
5368 /* fall through */
5369 case WINED3D_TOP_BUMPENVMAP:
5370 bump_map |= 1 << stage;
5371 /* fall through */
5372 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5373 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5374 tex_map |= 1 << stage;
5375 break;
5377 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5378 tfactor_used = TRUE;
5379 break;
5381 default:
5382 break;
5385 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5386 continue;
5388 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
5389 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
5390 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
5392 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5393 tex_map |= 1 << stage;
5394 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5395 tfactor_used = TRUE;
5396 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5397 tempreg_used = TRUE;
5398 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5399 tss_const_map |= 1 << stage;
5401 lowest_disabled_stage = stage;
5403 shader_addline(buffer, "#version 120\n");
5405 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5406 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5408 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
5409 shader_addline(buffer, "vec4 ret;\n");
5410 if (tempreg_used || settings->sRGB_write)
5411 shader_addline(buffer, "vec4 temp_reg;\n");
5412 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
5414 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5416 if (tss_const_map & (1 << stage))
5417 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
5419 if (!(tex_map & (1 << stage)))
5420 continue;
5422 switch (settings->op[stage].tex_type)
5424 case tex_1d:
5425 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
5426 break;
5427 case tex_2d:
5428 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
5429 break;
5430 case tex_3d:
5431 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
5432 break;
5433 case tex_cube:
5434 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
5435 break;
5436 case tex_rect:
5437 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
5438 break;
5439 default:
5440 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
5441 break;
5444 shader_addline(buffer, "vec4 tex%u;\n", stage);
5446 if (!(bump_map & (1 << stage)))
5447 continue;
5448 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
5450 if (!(lum_map & (1 << stage)))
5451 continue;
5452 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
5453 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
5455 if (tfactor_used)
5456 shader_addline(buffer, "uniform vec4 tex_factor;\n");
5457 shader_addline(buffer, "uniform vec4 specular_enable;\n");
5459 if (settings->sRGB_write)
5461 shader_addline(buffer, "const vec4 srgb_const0 = ");
5462 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
5463 shader_addline(buffer, ";\n");
5464 shader_addline(buffer, "const vec4 srgb_const1 = ");
5465 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
5466 shader_addline(buffer, ";\n");
5469 shader_addline(buffer, "void main()\n{\n");
5471 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
5472 shader_addline(buffer, "if (any(lessThan(gl_TexCoord[7], vec4(0.0)))) discard;\n");
5474 /* Generate texture sampling instructions) */
5475 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
5477 const char *texture_function, *coord_mask;
5478 char tex_reg_name[8];
5479 BOOL proj;
5481 if (!(tex_map & (1 << stage)))
5482 continue;
5484 if (settings->op[stage].projected == proj_none)
5486 proj = FALSE;
5488 else if (settings->op[stage].projected == proj_count4
5489 || settings->op[stage].projected == proj_count3)
5491 proj = TRUE;
5493 else
5495 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
5496 proj = TRUE;
5499 switch (settings->op[stage].tex_type)
5501 case tex_1d:
5502 if (proj)
5504 texture_function = "texture1DProj";
5505 coord_mask = "xw";
5507 else
5509 texture_function = "texture1D";
5510 coord_mask = "x";
5512 break;
5513 case tex_2d:
5514 if (proj)
5516 texture_function = "texture2DProj";
5517 coord_mask = "xyw";
5519 else
5521 texture_function = "texture2D";
5522 coord_mask = "xy";
5524 break;
5525 case tex_3d:
5526 if (proj)
5528 texture_function = "texture3DProj";
5529 coord_mask = "xyzw";
5531 else
5533 texture_function = "texture3D";
5534 coord_mask = "xyz";
5536 break;
5537 case tex_cube:
5538 texture_function = "textureCube";
5539 coord_mask = "xyz";
5540 break;
5541 case tex_rect:
5542 if (proj)
5544 texture_function = "texture2DRectProj";
5545 coord_mask = "xyw";
5547 else
5549 texture_function = "texture2DRect";
5550 coord_mask = "xy";
5552 break;
5553 default:
5554 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
5555 texture_function = "";
5556 coord_mask = "xyzw";
5557 break;
5560 if (stage > 0
5561 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
5562 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
5564 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
5566 /* With projective textures, texbem only divides the static
5567 * texture coord, not the displacement, so multiply the
5568 * displacement with the dividing parameter before passing it to
5569 * TXP. */
5570 if (settings->op[stage].projected != proj_none)
5572 if (settings->op[stage].projected == proj_count4)
5574 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].w) + gl_TexCoord[%u].xy;\n",
5575 stage, stage);
5576 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].ww;\n", stage);
5578 else
5580 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].z) + gl_TexCoord[%u].xy;\n",
5581 stage, stage);
5582 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].zz;\n", stage);
5585 else
5587 shader_addline(buffer, "ret = gl_TexCoord[%u] + ret.xyxy;\n", stage);
5590 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
5591 stage, texture_function, stage, coord_mask);
5593 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
5594 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
5595 stage, stage - 1, stage - 1, stage - 1);
5597 else if (settings->op[stage].projected == proj_count3)
5599 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].xyz);\n",
5600 stage, texture_function, stage, stage);
5602 else
5604 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].%s);\n",
5605 stage, texture_function, stage, stage, coord_mask);
5608 sprintf(tex_reg_name, "tex%u", stage);
5609 shader_glsl_color_correction_ext(buffer, tex_reg_name, WINED3DSP_WRITEMASK_ALL,
5610 settings->op[stage].color_fixup);
5613 /* Generate the main shader */
5614 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5616 BOOL op_equal;
5618 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5620 if (!stage)
5621 final_combiner_src = "gl_Color";
5622 break;
5625 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5626 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5627 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
5628 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5629 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5630 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
5631 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5632 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5633 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
5634 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5635 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5636 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
5637 else
5638 op_equal = settings->op[stage].aop == settings->op[stage].cop
5639 && settings->op[stage].carg0 == settings->op[stage].aarg0
5640 && settings->op[stage].carg1 == settings->op[stage].aarg1
5641 && settings->op[stage].carg2 == settings->op[stage].aarg2;
5643 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5645 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5646 settings->op[stage].cop, settings->op[stage].carg0,
5647 settings->op[stage].carg1, settings->op[stage].carg2);
5648 if (!stage)
5649 shader_addline(buffer, "ret.w = gl_Color.w;\n");
5651 else if (op_equal)
5653 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
5654 settings->op[stage].cop, settings->op[stage].carg0,
5655 settings->op[stage].carg1, settings->op[stage].carg2);
5657 else
5659 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5660 settings->op[stage].cop, settings->op[stage].carg0,
5661 settings->op[stage].carg1, settings->op[stage].carg2);
5662 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
5663 settings->op[stage].aop, settings->op[stage].aarg0,
5664 settings->op[stage].aarg1, settings->op[stage].aarg2);
5668 shader_addline(buffer, "gl_FragData[0] = gl_SecondaryColor * specular_enable + %s;\n", final_combiner_src);
5670 if (settings->sRGB_write)
5671 shader_glsl_generate_srgb_write_correction(buffer);
5673 shader_glsl_generate_fog_code(buffer, settings->fog);
5675 shader_addline(buffer, "}\n");
5677 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5678 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5679 return shader_id;
5682 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
5683 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
5685 struct glsl_ffp_vertex_shader *shader;
5686 const struct wine_rb_entry *entry;
5688 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
5689 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
5691 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
5692 return NULL;
5694 shader->desc.settings = *settings;
5695 shader->id = shader_glsl_generate_ffp_vertex_shader(&priv->shader_buffer, settings, gl_info);
5696 list_init(&shader->linked_programs);
5697 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
5698 ERR("Failed to insert ffp vertex shader.\n");
5700 return shader;
5703 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
5704 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
5706 struct glsl_ffp_fragment_shader *glsl_desc;
5707 const struct ffp_frag_desc *desc;
5709 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
5710 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
5712 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
5713 return NULL;
5715 glsl_desc->entry.settings = *args;
5716 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(&priv->shader_buffer, args, gl_info);
5717 list_init(&glsl_desc->linked_programs);
5718 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
5720 return glsl_desc;
5724 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
5725 GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
5727 unsigned int i;
5728 char name[32];
5730 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5731 sizeof(GLuint) * gl_info->limits.glsl_vs_float_constants);
5732 for (i = 0; i < vs_c_count; ++i)
5734 snprintf(name, sizeof(name), "vs_c[%u]", i);
5735 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5737 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
5738 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLuint));
5740 for (i = 0; i < MAX_CONST_I; ++i)
5742 snprintf(name, sizeof(name), "vs_i[%u]", i);
5743 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5746 for (i = 0; i < MAX_CONST_B; ++i)
5748 snprintf(name, sizeof(name), "vs_b[%u]", i);
5749 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5752 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "posFixup"));
5755 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
5756 GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
5758 unsigned int i;
5759 char name[32];
5761 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5762 sizeof(GLuint) * gl_info->limits.glsl_ps_float_constants);
5763 for (i = 0; i < ps_c_count; ++i)
5765 snprintf(name, sizeof(name), "ps_c[%u]", i);
5766 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5768 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
5769 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLuint));
5771 for (i = 0; i < MAX_CONST_I; ++i)
5773 snprintf(name, sizeof(name), "ps_i[%u]", i);
5774 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5777 for (i = 0; i < MAX_CONST_B; ++i)
5779 snprintf(name, sizeof(name), "ps_b[%u]", i);
5780 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5783 for (i = 0; i < MAX_TEXTURES; ++i)
5785 snprintf(name, sizeof(name), "bumpenv_mat%u", i);
5786 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5787 snprintf(name, sizeof(name), "bumpenv_lum_scale%u", i);
5788 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5789 snprintf(name, sizeof(name), "bumpenv_lum_offset%u", i);
5790 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5791 snprintf(name, sizeof(name), "tss_const%u", i);
5792 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5795 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
5796 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
5797 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
5798 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
5801 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info, GLuint program_id,
5802 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
5804 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
5805 GLuint block_idx;
5806 unsigned int i;
5807 char name[16];
5809 for (i = 0; i < count; ++i)
5811 if (!reg_maps->cb_sizes[i])
5812 continue;
5814 snprintf(name, sizeof(name), "block_%s_cb%u", prefix, i);
5815 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name));
5816 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
5818 checkGLcall("glUniformBlockBinding");
5821 /* Context activation is done by the caller. */
5822 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
5823 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
5825 const struct wined3d_gl_info *gl_info = context->gl_info;
5826 const struct ps_np2fixup_info *np2fixup_info = NULL;
5827 struct glsl_shader_prog_link *entry = NULL;
5828 struct wined3d_shader *vshader = NULL;
5829 struct wined3d_shader *gshader = NULL;
5830 struct wined3d_shader *pshader = NULL;
5831 GLuint program_id = 0;
5832 GLuint reorder_shader_id = 0;
5833 unsigned int i;
5834 GLuint vs_id = 0;
5835 GLuint gs_id = 0;
5836 GLuint ps_id = 0;
5837 struct list *ps_list, *vs_list;
5839 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_VERTEX)))
5841 vs_id = ctx_data->glsl_program->vs.id;
5842 vs_list = &ctx_data->glsl_program->vs.shader_entry;
5844 if (use_vs(state))
5846 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5847 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
5849 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_GEOMETRY))
5850 && ctx_data->glsl_program->gs.id)
5851 gs_id = ctx_data->glsl_program->gs.id;
5852 else if (gshader)
5853 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5856 else if (use_vs(state))
5858 struct vs_compile_args vs_compile_args;
5859 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5861 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args);
5862 vs_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
5863 vs_list = &vshader->linked_programs;
5865 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
5866 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5868 else if (priv->vertex_pipe == &glsl_vertex_pipe)
5870 struct glsl_ffp_vertex_shader *ffp_shader;
5871 struct wined3d_ffp_vs_settings settings;
5873 wined3d_ffp_get_vs_settings(state, &context->stream_info, &settings);
5874 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
5875 vs_id = ffp_shader->id;
5876 vs_list = &ffp_shader->linked_programs;
5879 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_PIXEL)))
5881 ps_id = ctx_data->glsl_program->ps.id;
5882 ps_list = &ctx_data->glsl_program->ps.shader_entry;
5884 if (use_ps(state))
5885 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
5887 else if (use_ps(state))
5889 struct ps_compile_args ps_compile_args;
5890 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
5891 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, gl_info);
5892 ps_id = find_glsl_pshader(context, &priv->shader_buffer,
5893 pshader, &ps_compile_args, &np2fixup_info);
5894 ps_list = &pshader->linked_programs;
5896 else if (priv->fragment_pipe == &glsl_fragment_pipe)
5898 struct glsl_ffp_fragment_shader *ffp_shader;
5899 struct ffp_frag_settings settings;
5901 gen_ffp_frag_op(context, state, &settings, FALSE);
5902 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
5903 ps_id = ffp_shader->id;
5904 ps_list = &ffp_shader->linked_programs;
5907 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
5909 ctx_data->glsl_program = entry;
5910 return;
5913 /* If we get to this point, then no matching program exists, so we create one */
5914 program_id = GL_EXTCALL(glCreateProgram());
5915 TRACE("Created new GLSL shader program %u.\n", program_id);
5917 /* Create the entry */
5918 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
5919 entry->id = program_id;
5920 entry->vs.id = vs_id;
5921 entry->gs.id = gs_id;
5922 entry->ps.id = ps_id;
5923 entry->constant_version = 0;
5924 entry->ps.np2_fixup_info = np2fixup_info;
5925 /* Add the hash table entry */
5926 add_glsl_program_entry(priv, entry);
5928 /* Set the current program */
5929 ctx_data->glsl_program = entry;
5931 /* Attach GLSL vshader */
5932 if (vs_id)
5934 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
5935 GL_EXTCALL(glAttachShader(program_id, vs_id));
5936 checkGLcall("glAttachShader");
5938 list_add_head(vs_list, &entry->vs.shader_entry);
5941 if (vshader)
5943 WORD map = vshader->reg_maps.input_registers;
5944 char tmp_name[10];
5946 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
5947 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
5948 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
5949 checkGLcall("glAttachShader");
5950 /* Flag the reorder function for deletion, then it will be freed automatically when the program
5951 * is destroyed
5953 GL_EXTCALL(glDeleteShader(reorder_shader_id));
5955 /* Bind vertex attributes to a corresponding index number to match
5956 * the same index numbers as ARB_vertex_programs (makes loading
5957 * vertex attributes simpler). With this method, we can use the
5958 * exact same code to load the attributes later for both ARB and
5959 * GLSL shaders.
5961 * We have to do this here because we need to know the Program ID
5962 * in order to make the bindings work, and it has to be done prior
5963 * to linking the GLSL program. */
5964 for (i = 0; map; map >>= 1, ++i)
5966 if (!(map & 1)) continue;
5968 snprintf(tmp_name, sizeof(tmp_name), "vs_in%u", i);
5969 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name));
5971 checkGLcall("glBindAttribLocation");
5974 if (gshader)
5976 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
5977 GL_EXTCALL(glAttachShader(program_id, gs_id));
5978 checkGLcall("glAttachShader");
5980 TRACE("input type %s, output type %s, vertices out %u.\n",
5981 debug_d3dprimitivetype(gshader->u.gs.input_type),
5982 debug_d3dprimitivetype(gshader->u.gs.output_type),
5983 gshader->u.gs.vertices_out);
5984 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
5985 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
5986 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
5987 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
5988 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
5989 gshader->u.gs.vertices_out));
5990 checkGLcall("glProgramParameteriARB");
5992 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
5995 /* Attach GLSL pshader */
5996 if (ps_id)
5998 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
5999 GL_EXTCALL(glAttachShader(program_id, ps_id));
6000 checkGLcall("glAttachShader");
6002 list_add_head(ps_list, &entry->ps.shader_entry);
6005 /* Link the program */
6006 TRACE("Linking GLSL shader program %u.\n", program_id);
6007 GL_EXTCALL(glLinkProgram(program_id));
6008 shader_glsl_validate_link(gl_info, program_id);
6010 shader_glsl_init_vs_uniform_locations(gl_info, program_id, &entry->vs,
6011 vshader ? min(vshader->limits->constant_float, gl_info->limits.glsl_vs_float_constants) : 0);
6012 shader_glsl_init_ps_uniform_locations(gl_info, program_id, &entry->ps,
6013 pshader ? min(pshader->limits->constant_float, gl_info->limits.glsl_ps_float_constants) : 0);
6014 checkGLcall("Find glsl program uniform locations");
6016 if (pshader && pshader->reg_maps.shader_version.major >= 3
6017 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
6019 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
6020 entry->vs.vertex_color_clamp = GL_FALSE;
6022 else
6024 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
6027 /* Set the shader to allow uniform loading on it */
6028 GL_EXTCALL(glUseProgram(program_id));
6029 checkGLcall("glUseProgram");
6031 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
6032 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
6033 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
6034 * vertex shader with fixed function pixel processing is used we make sure that the card
6035 * supports enough samplers to allow the max number of vertex samplers with all possible
6036 * fixed function fragment processing setups. So once the program is linked these samplers
6037 * won't change. */
6038 shader_glsl_load_samplers(gl_info, context->tex_unit_map, program_id);
6040 entry->constant_update_mask = 0;
6041 if (vshader)
6043 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
6044 if (vshader->reg_maps.integer_constants)
6045 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
6046 if (vshader->reg_maps.boolean_constants)
6047 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
6048 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
6050 shader_glsl_init_uniform_block_bindings(gl_info, program_id, &vshader->reg_maps,
6051 0, gl_info->limits.vertex_uniform_blocks);
6054 if (gshader)
6055 shader_glsl_init_uniform_block_bindings(gl_info, program_id, &gshader->reg_maps,
6056 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
6058 if (ps_id)
6060 if (pshader)
6062 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
6063 if (pshader->reg_maps.integer_constants)
6064 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
6065 if (pshader->reg_maps.boolean_constants)
6066 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
6067 if (entry->ps.ycorrection_location != -1)
6068 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
6070 shader_glsl_init_uniform_block_bindings(gl_info, program_id, &pshader->reg_maps,
6071 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
6072 gl_info->limits.fragment_uniform_blocks);
6074 else
6076 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
6079 for (i = 0; i < MAX_TEXTURES; ++i)
6081 if (entry->ps.bumpenv_mat_location[i] != -1)
6083 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
6084 break;
6088 if (entry->ps.np2_fixup_location != -1)
6089 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
6093 /* Context activation is done by the caller. */
6094 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum tex_types tex_type, BOOL masked)
6096 GLuint program_id;
6097 GLuint vshader_id, pshader_id;
6098 const char *blt_pshader;
6100 static const char blt_vshader[] =
6101 "#version 120\n"
6102 "void main(void)\n"
6103 "{\n"
6104 " gl_Position = gl_Vertex;\n"
6105 " gl_FrontColor = vec4(1.0);\n"
6106 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
6107 "}\n";
6109 static const char * const blt_pshaders_full[tex_type_count] =
6111 /* tex_1d */
6112 NULL,
6113 /* tex_2d */
6114 "#version 120\n"
6115 "uniform sampler2D sampler;\n"
6116 "void main(void)\n"
6117 "{\n"
6118 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6119 "}\n",
6120 /* tex_3d */
6121 NULL,
6122 /* tex_cube */
6123 "#version 120\n"
6124 "uniform samplerCube sampler;\n"
6125 "void main(void)\n"
6126 "{\n"
6127 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6128 "}\n",
6129 /* tex_rect */
6130 "#version 120\n"
6131 "#extension GL_ARB_texture_rectangle : enable\n"
6132 "uniform sampler2DRect sampler;\n"
6133 "void main(void)\n"
6134 "{\n"
6135 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6136 "}\n",
6139 static const char * const blt_pshaders_masked[tex_type_count] =
6141 /* tex_1d */
6142 NULL,
6143 /* tex_2d */
6144 "#version 120\n"
6145 "uniform sampler2D sampler;\n"
6146 "uniform vec4 mask;\n"
6147 "void main(void)\n"
6148 "{\n"
6149 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6150 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6151 "}\n",
6152 /* tex_3d */
6153 NULL,
6154 /* tex_cube */
6155 "#version 120\n"
6156 "uniform samplerCube sampler;\n"
6157 "uniform vec4 mask;\n"
6158 "void main(void)\n"
6159 "{\n"
6160 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6161 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6162 "}\n",
6163 /* tex_rect */
6164 "#version 120\n"
6165 "#extension GL_ARB_texture_rectangle : enable\n"
6166 "uniform sampler2DRect sampler;\n"
6167 "uniform vec4 mask;\n"
6168 "void main(void)\n"
6169 "{\n"
6170 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6171 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6172 "}\n",
6175 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
6176 if (!blt_pshader)
6178 FIXME("tex_type %#x not supported\n", tex_type);
6179 return 0;
6182 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6183 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
6185 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6186 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
6188 program_id = GL_EXTCALL(glCreateProgram());
6189 GL_EXTCALL(glAttachShader(program_id, vshader_id));
6190 GL_EXTCALL(glAttachShader(program_id, pshader_id));
6191 GL_EXTCALL(glLinkProgram(program_id));
6193 shader_glsl_validate_link(gl_info, program_id);
6195 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
6196 * is destroyed
6198 GL_EXTCALL(glDeleteShader(vshader_id));
6199 GL_EXTCALL(glDeleteShader(pshader_id));
6200 return program_id;
6203 /* Context activation is done by the caller. */
6204 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
6205 const struct wined3d_state *state)
6207 struct glsl_context_data *ctx_data = context->shader_backend_data;
6208 const struct wined3d_gl_info *gl_info = context->gl_info;
6209 struct shader_glsl_priv *priv = shader_priv;
6210 GLuint program_id = 0, prev_id = 0;
6211 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
6213 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
6214 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
6216 if (ctx_data->glsl_program)
6218 prev_id = ctx_data->glsl_program->id;
6219 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6221 else
6223 prev_id = 0;
6224 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6227 set_glsl_shader_program(context, state, priv, ctx_data);
6229 if (ctx_data->glsl_program)
6231 program_id = ctx_data->glsl_program->id;
6232 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6234 else
6236 program_id = 0;
6237 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6240 if (old_vertex_color_clamp != current_vertex_color_clamp)
6242 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6244 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
6245 checkGLcall("glClampColorARB");
6247 else
6249 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
6253 TRACE("Using GLSL program %u.\n", program_id);
6255 if (prev_id != program_id)
6257 GL_EXTCALL(glUseProgram(program_id));
6258 checkGLcall("glUseProgram");
6260 if (program_id)
6261 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
6265 /* "context" is not necessarily the currently active context. */
6266 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
6268 struct glsl_context_data *ctx_data = context->shader_backend_data;
6270 ctx_data->glsl_program = NULL;
6271 context->shader_update_mask = (1 << WINED3D_SHADER_TYPE_PIXEL)
6272 | (1 << WINED3D_SHADER_TYPE_VERTEX)
6273 | (1 << WINED3D_SHADER_TYPE_GEOMETRY);
6276 /* Context activation is done by the caller. */
6277 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
6279 const struct wined3d_gl_info *gl_info = context->gl_info;
6280 struct shader_glsl_priv *priv = shader_priv;
6282 shader_glsl_invalidate_current_program(context);
6283 GL_EXTCALL(glUseProgram(0));
6284 checkGLcall("glUseProgram");
6286 priv->vertex_pipe->vp_enable(gl_info, FALSE);
6287 priv->fragment_pipe->enable_extension(gl_info, FALSE);
6289 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6291 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
6292 checkGLcall("glClampColorARB");
6296 /* Context activation is done by the caller. */
6297 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
6298 enum tex_types tex_type, const SIZE *ds_mask_size)
6300 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
6301 struct shader_glsl_priv *priv = shader_priv;
6302 GLuint *blt_program;
6303 GLint loc;
6305 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
6306 if (!*blt_program)
6308 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
6309 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
6310 GL_EXTCALL(glUseProgram(*blt_program));
6311 GL_EXTCALL(glUniform1i(loc, 0));
6313 else
6315 GL_EXTCALL(glUseProgram(*blt_program));
6318 if (masked)
6320 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
6321 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
6325 /* Context activation is done by the caller. */
6326 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
6328 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
6329 GLuint program_id;
6331 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
6332 if (program_id) TRACE("Using GLSL program %u\n", program_id);
6334 GL_EXTCALL(glUseProgram(program_id));
6335 checkGLcall("glUseProgram");
6338 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
6339 const struct glsl_shader_prog_link *program)
6341 const struct glsl_context_data *ctx_data;
6342 struct wined3d_context *context;
6343 unsigned int i;
6345 for (i = 0; i < device->context_count; ++i)
6347 context = device->contexts[i];
6348 ctx_data = context->shader_backend_data;
6350 if (ctx_data->glsl_program == program)
6351 shader_glsl_invalidate_current_program(context);
6355 static void shader_glsl_destroy(struct wined3d_shader *shader)
6357 struct glsl_shader_private *shader_data = shader->backend_data;
6358 struct wined3d_device *device = shader->device;
6359 struct shader_glsl_priv *priv = device->shader_priv;
6360 const struct wined3d_gl_info *gl_info;
6361 const struct list *linked_programs;
6362 struct wined3d_context *context;
6364 if (!shader_data || !shader_data->num_gl_shaders)
6366 HeapFree(GetProcessHeap(), 0, shader_data);
6367 shader->backend_data = NULL;
6368 return;
6371 context = context_acquire(device, NULL);
6372 gl_info = context->gl_info;
6374 TRACE("Deleting linked programs.\n");
6375 linked_programs = &shader->linked_programs;
6376 if (linked_programs->next)
6378 struct glsl_shader_prog_link *entry, *entry2;
6379 UINT i;
6381 switch (shader->reg_maps.shader_version.type)
6383 case WINED3D_SHADER_TYPE_PIXEL:
6385 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
6387 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6389 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
6390 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
6391 checkGLcall("glDeleteShader");
6393 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
6395 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6396 struct glsl_shader_prog_link, ps.shader_entry)
6398 shader_glsl_invalidate_contexts_program(device, entry);
6399 delete_glsl_program_entry(priv, gl_info, entry);
6402 break;
6405 case WINED3D_SHADER_TYPE_VERTEX:
6407 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
6409 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6411 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
6412 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
6413 checkGLcall("glDeleteShader");
6415 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
6417 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6418 struct glsl_shader_prog_link, vs.shader_entry)
6420 shader_glsl_invalidate_contexts_program(device, entry);
6421 delete_glsl_program_entry(priv, gl_info, entry);
6424 break;
6427 case WINED3D_SHADER_TYPE_GEOMETRY:
6429 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
6431 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6433 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
6434 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
6435 checkGLcall("glDeleteShader");
6437 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
6439 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6440 struct glsl_shader_prog_link, gs.shader_entry)
6442 shader_glsl_invalidate_contexts_program(device, entry);
6443 delete_glsl_program_entry(priv, gl_info, entry);
6446 break;
6449 default:
6450 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
6451 break;
6455 HeapFree(GetProcessHeap(), 0, shader->backend_data);
6456 shader->backend_data = NULL;
6458 context_release(context);
6461 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
6463 const struct glsl_program_key *k = key;
6464 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
6465 const struct glsl_shader_prog_link, program_lookup_entry);
6467 if (k->vs_id > prog->vs.id) return 1;
6468 else if (k->vs_id < prog->vs.id) return -1;
6470 if (k->gs_id > prog->gs.id) return 1;
6471 else if (k->gs_id < prog->gs.id) return -1;
6473 if (k->ps_id > prog->ps.id) return 1;
6474 else if (k->ps_id < prog->ps.id) return -1;
6476 return 0;
6479 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
6481 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
6482 + constant_count * sizeof(*heap->contained)
6483 + constant_count * sizeof(*heap->positions);
6484 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
6486 if (!mem)
6488 ERR("Failed to allocate memory\n");
6489 return FALSE;
6492 heap->entries = mem;
6493 heap->entries[1].version = 0;
6494 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
6495 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
6496 heap->positions = (unsigned int *)(heap->contained + constant_count);
6497 heap->size = 1;
6499 return TRUE;
6502 static void constant_heap_free(struct constant_heap *heap)
6504 HeapFree(GetProcessHeap(), 0, heap->entries);
6507 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
6509 wined3d_rb_alloc,
6510 wined3d_rb_realloc,
6511 wined3d_rb_free,
6512 glsl_program_key_compare,
6515 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
6516 const struct fragment_pipeline *fragment_pipe)
6518 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6519 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
6520 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
6521 gl_info->limits.glsl_ps_float_constants)) + 1;
6522 struct fragment_caps fragment_caps;
6523 void *vertex_priv, *fragment_priv;
6525 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
6527 ERR("Failed to initialize vertex pipe.\n");
6528 HeapFree(GetProcessHeap(), 0, priv);
6529 return E_FAIL;
6532 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
6534 ERR("Failed to initialize fragment pipe.\n");
6535 vertex_pipe->vp_free(device);
6536 HeapFree(GetProcessHeap(), 0, priv);
6537 return E_FAIL;
6540 if (!shader_buffer_init(&priv->shader_buffer))
6542 ERR("Failed to initialize shader buffer.\n");
6543 goto fail;
6546 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
6547 if (!priv->stack)
6549 ERR("Failed to allocate memory.\n");
6550 goto fail;
6553 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
6555 ERR("Failed to initialize vertex shader constant heap\n");
6556 goto fail;
6559 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
6561 ERR("Failed to initialize pixel shader constant heap\n");
6562 goto fail;
6565 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
6567 ERR("Failed to initialize rbtree.\n");
6568 goto fail;
6571 priv->next_constant_version = 1;
6572 priv->vertex_pipe = vertex_pipe;
6573 priv->fragment_pipe = fragment_pipe;
6574 fragment_pipe->get_caps(gl_info, &fragment_caps);
6575 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
6577 device->vertex_priv = vertex_priv;
6578 device->fragment_priv = fragment_priv;
6579 device->shader_priv = priv;
6581 return WINED3D_OK;
6583 fail:
6584 constant_heap_free(&priv->pconst_heap);
6585 constant_heap_free(&priv->vconst_heap);
6586 HeapFree(GetProcessHeap(), 0, priv->stack);
6587 shader_buffer_free(&priv->shader_buffer);
6588 fragment_pipe->free_private(device);
6589 vertex_pipe->vp_free(device);
6590 HeapFree(GetProcessHeap(), 0, priv);
6591 return E_OUTOFMEMORY;
6594 /* Context activation is done by the caller. */
6595 static void shader_glsl_free(struct wined3d_device *device)
6597 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6598 struct shader_glsl_priv *priv = device->shader_priv;
6599 int i;
6601 for (i = 0; i < tex_type_count; ++i)
6603 if (priv->depth_blt_program_full[i])
6605 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
6607 if (priv->depth_blt_program_masked[i])
6609 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
6613 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
6614 constant_heap_free(&priv->pconst_heap);
6615 constant_heap_free(&priv->vconst_heap);
6616 HeapFree(GetProcessHeap(), 0, priv->stack);
6617 shader_buffer_free(&priv->shader_buffer);
6618 priv->fragment_pipe->free_private(device);
6619 priv->vertex_pipe->vp_free(device);
6621 HeapFree(GetProcessHeap(), 0, device->shader_priv);
6622 device->shader_priv = NULL;
6625 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
6627 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
6628 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
6631 static void shader_glsl_free_context_data(struct wined3d_context *context)
6633 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
6636 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
6638 UINT shader_model;
6640 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
6641 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
6642 && gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] && gl_info->supported[ARB_DRAW_INSTANCED]
6643 && gl_info->supported[ARB_TEXTURE_RG] && gl_info->supported[ARB_SAMPLER_OBJECTS])
6644 shader_model = 4;
6645 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
6646 * texldd and texldl instructions. */
6647 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
6648 shader_model = 3;
6649 else
6650 shader_model = 2;
6651 TRACE("Shader model %u.\n", shader_model);
6653 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
6654 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
6655 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
6657 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
6658 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
6660 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
6661 * Direct3D minimum requirement.
6663 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
6664 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
6666 * The problem is that the refrast clamps temporary results in the shader to
6667 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
6668 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
6669 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
6670 * offer a way to query this.
6672 if (shader_model >= 4)
6673 caps->ps_1x_max_value = FLT_MAX;
6674 else
6675 caps->ps_1x_max_value = 1024.0f;
6677 /* Ideally we'd only set caps like sRGB writes here if supported by both
6678 * the shader backend and the fragment pipe, but we can get called before
6679 * shader_glsl_alloc(). */
6680 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
6681 | WINED3D_SHADER_CAP_SRGB_WRITE;
6684 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
6686 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
6688 TRACE("Checking support for fixup:\n");
6689 dump_color_fixup_desc(fixup);
6692 /* We support everything except YUV conversions. */
6693 if (!is_complex_fixup(fixup))
6695 TRACE("[OK]\n");
6696 return TRUE;
6699 TRACE("[FAILED]\n");
6700 return FALSE;
6703 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
6705 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
6706 /* WINED3DSIH_ADD */ shader_glsl_binop,
6707 /* WINED3DSIH_AND */ shader_glsl_binop,
6708 /* WINED3DSIH_BEM */ shader_glsl_bem,
6709 /* WINED3DSIH_BREAK */ shader_glsl_break,
6710 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
6711 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
6712 /* WINED3DSIH_CALL */ shader_glsl_call,
6713 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
6714 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
6715 /* WINED3DSIH_CND */ shader_glsl_cnd,
6716 /* WINED3DSIH_CRS */ shader_glsl_cross,
6717 /* WINED3DSIH_CUT */ shader_glsl_cut,
6718 /* WINED3DSIH_DCL */ shader_glsl_nop,
6719 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
6720 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
6721 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
6722 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
6723 /* WINED3DSIH_DEF */ shader_glsl_nop,
6724 /* WINED3DSIH_DEFB */ shader_glsl_nop,
6725 /* WINED3DSIH_DEFI */ shader_glsl_nop,
6726 /* WINED3DSIH_DIV */ shader_glsl_binop,
6727 /* WINED3DSIH_DP2 */ shader_glsl_dot,
6728 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
6729 /* WINED3DSIH_DP3 */ shader_glsl_dot,
6730 /* WINED3DSIH_DP4 */ shader_glsl_dot,
6731 /* WINED3DSIH_DST */ shader_glsl_dst,
6732 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
6733 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
6734 /* WINED3DSIH_ELSE */ shader_glsl_else,
6735 /* WINED3DSIH_EMIT */ shader_glsl_emit,
6736 /* WINED3DSIH_ENDIF */ shader_glsl_end,
6737 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
6738 /* WINED3DSIH_ENDREP */ shader_glsl_end,
6739 /* WINED3DSIH_EQ */ shader_glsl_relop,
6740 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
6741 /* WINED3DSIH_EXPP */ shader_glsl_expp,
6742 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
6743 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
6744 /* WINED3DSIH_GE */ shader_glsl_relop,
6745 /* WINED3DSIH_IADD */ shader_glsl_binop,
6746 /* WINED3DSIH_IEQ */ NULL,
6747 /* WINED3DSIH_IF */ shader_glsl_if,
6748 /* WINED3DSIH_IFC */ shader_glsl_ifc,
6749 /* WINED3DSIH_IGE */ shader_glsl_relop,
6750 /* WINED3DSIH_IMUL */ shader_glsl_imul,
6751 /* WINED3DSIH_ISHL */ shader_glsl_binop,
6752 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
6753 /* WINED3DSIH_LABEL */ shader_glsl_label,
6754 /* WINED3DSIH_LD */ NULL,
6755 /* WINED3DSIH_LIT */ shader_glsl_lit,
6756 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
6757 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
6758 /* WINED3DSIH_LOOP */ shader_glsl_loop,
6759 /* WINED3DSIH_LRP */ shader_glsl_lrp,
6760 /* WINED3DSIH_LT */ shader_glsl_relop,
6761 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
6762 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
6763 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
6764 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
6765 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
6766 /* WINED3DSIH_MAD */ shader_glsl_mad,
6767 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
6768 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
6769 /* WINED3DSIH_MOV */ shader_glsl_mov,
6770 /* WINED3DSIH_MOVA */ shader_glsl_mov,
6771 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
6772 /* WINED3DSIH_MUL */ shader_glsl_binop,
6773 /* WINED3DSIH_NOP */ shader_glsl_nop,
6774 /* WINED3DSIH_NRM */ shader_glsl_nrm,
6775 /* WINED3DSIH_PHASE */ shader_glsl_nop,
6776 /* WINED3DSIH_POW */ shader_glsl_pow,
6777 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
6778 /* WINED3DSIH_REP */ shader_glsl_rep,
6779 /* WINED3DSIH_RET */ shader_glsl_ret,
6780 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
6781 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
6782 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
6783 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
6784 /* WINED3DSIH_SAMPLE_LOD */ NULL,
6785 /* WINED3DSIH_SETP */ NULL,
6786 /* WINED3DSIH_SGE */ shader_glsl_compare,
6787 /* WINED3DSIH_SGN */ shader_glsl_sgn,
6788 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
6789 /* WINED3DSIH_SLT */ shader_glsl_compare,
6790 /* WINED3DSIH_SQRT */ NULL,
6791 /* WINED3DSIH_SUB */ shader_glsl_binop,
6792 /* WINED3DSIH_TEX */ shader_glsl_tex,
6793 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
6794 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
6795 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
6796 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
6797 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
6798 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
6799 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
6800 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
6801 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
6802 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
6803 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
6804 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
6805 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
6806 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
6807 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
6808 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
6809 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
6810 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
6811 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
6812 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
6813 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
6814 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
6815 /* WINED3DSIH_UGE */ shader_glsl_relop,
6816 /* WINED3DSIH_USHR */ shader_glsl_binop,
6817 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
6818 /* WINED3DSIH_XOR */ shader_glsl_binop,
6821 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
6822 SHADER_HANDLER hw_fct;
6824 /* Select handler */
6825 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
6827 /* Unhandled opcode */
6828 if (!hw_fct)
6830 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
6831 return;
6833 hw_fct(ins);
6835 shader_glsl_add_instruction_modifiers(ins);
6838 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
6840 struct shader_glsl_priv *priv = shader_priv;
6842 return priv->ffp_proj_control;
6845 const struct wined3d_shader_backend_ops glsl_shader_backend =
6847 shader_glsl_handle_instruction,
6848 shader_glsl_select,
6849 shader_glsl_disable,
6850 shader_glsl_select_depth_blt,
6851 shader_glsl_deselect_depth_blt,
6852 shader_glsl_update_float_vertex_constants,
6853 shader_glsl_update_float_pixel_constants,
6854 shader_glsl_load_constants,
6855 shader_glsl_destroy,
6856 shader_glsl_alloc,
6857 shader_glsl_free,
6858 shader_glsl_allocate_context_data,
6859 shader_glsl_free_context_data,
6860 shader_glsl_get_caps,
6861 shader_glsl_color_fixup_supported,
6862 shader_glsl_has_ffp_proj_control,
6865 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
6867 if (enable)
6868 gl_info->gl_ops.gl.p_glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6869 else
6870 gl_info->gl_ops.gl.p_glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6871 checkGLcall("GL_VERTEX_PROGRAM_POINT_SIZE_ARB");
6874 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
6876 caps->xyzrhw = TRUE;
6877 caps->max_active_lights = gl_info->limits.lights;
6878 caps->max_vertex_blend_matrices = 1;
6879 caps->max_vertex_blend_matrix_index = 0;
6880 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
6881 | WINED3DVTXPCAPS_MATERIALSOURCE7
6882 | WINED3DVTXPCAPS_VERTEXFOG
6883 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
6884 | WINED3DVTXPCAPS_POSITIONALLIGHTS
6885 | WINED3DVTXPCAPS_LOCALVIEWER
6886 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
6887 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
6888 caps->max_user_clip_planes = gl_info->limits.clipplanes;
6889 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
6892 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
6894 struct shader_glsl_priv *priv;
6896 if (shader_backend == &glsl_shader_backend)
6898 priv = shader_priv;
6900 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
6902 ERR("Failed to initialize rbtree.\n");
6903 return NULL;
6906 return priv;
6909 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
6911 return NULL;
6914 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
6916 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
6917 struct glsl_ffp_vertex_shader, desc.entry);
6918 struct glsl_shader_prog_link *program, *program2;
6919 struct glsl_ffp_destroy_ctx *ctx = context;
6921 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
6922 struct glsl_shader_prog_link, vs.shader_entry)
6924 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
6926 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
6927 HeapFree(GetProcessHeap(), 0, shader);
6930 /* Context activation is done by the caller. */
6931 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
6933 struct shader_glsl_priv *priv = device->vertex_priv;
6934 struct glsl_ffp_destroy_ctx ctx;
6936 ctx.priv = priv;
6937 ctx.gl_info = &device->adapter->gl_info;
6938 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
6941 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
6942 const struct wined3d_state *state, DWORD state_id)
6944 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
6947 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
6948 const struct wined3d_state *state, DWORD state_id)
6950 /* Table fog behavior depends on the projection matrix. */
6951 if (state->render_states[WINED3D_RS_FOGENABLE]
6952 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
6953 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
6954 transform_projection(context, state, state_id);
6957 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
6959 {STATE_VDECL, {STATE_VDECL, vertexdeclaration }, WINED3D_GL_EXT_NONE },
6960 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6961 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
6962 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), state_specularenable }, WINED3D_GL_EXT_NONE },
6963 /* Clip planes */
6964 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
6965 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
6966 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
6967 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
6968 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
6969 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
6970 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
6971 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
6972 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
6973 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
6974 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
6975 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
6976 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
6977 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
6978 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
6979 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
6980 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
6981 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
6982 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
6983 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
6984 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
6985 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
6986 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
6987 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
6988 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
6989 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
6990 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
6991 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
6992 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
6993 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
6994 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
6995 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
6996 /* Lights */
6997 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6998 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), light }, WINED3D_GL_EXT_NONE },
6999 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), light }, WINED3D_GL_EXT_NONE },
7000 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), light }, WINED3D_GL_EXT_NONE },
7001 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), light }, WINED3D_GL_EXT_NONE },
7002 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), light }, WINED3D_GL_EXT_NONE },
7003 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), light }, WINED3D_GL_EXT_NONE },
7004 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), light }, WINED3D_GL_EXT_NONE },
7005 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), light }, WINED3D_GL_EXT_NONE },
7006 /* Viewport */
7007 {STATE_VIEWPORT, {STATE_VIEWPORT, viewport_vertexpart }, WINED3D_GL_EXT_NONE },
7008 /* Transform states */
7009 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), transform_view }, WINED3D_GL_EXT_NONE },
7010 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
7011 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7012 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7013 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7014 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7015 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7016 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7017 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7018 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7019 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), transform_world }, WINED3D_GL_EXT_NONE },
7020 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7021 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7022 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7023 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7024 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7025 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7026 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7027 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7028 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7029 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7030 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7031 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7032 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7033 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7034 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7035 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7036 /* Fog */
7037 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7038 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7039 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7040 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7041 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
7042 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
7043 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7044 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), state_ambient }, WINED3D_GL_EXT_NONE },
7045 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7046 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7047 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7048 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7049 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7050 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7051 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7052 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7053 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7054 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_arb }, ARB_POINT_PARAMETERS },
7055 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_ext }, EXT_POINT_PARAMETERS },
7056 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_w }, WINED3D_GL_EXT_NONE },
7057 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
7058 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
7059 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), state_pscale }, WINED3D_GL_EXT_NONE },
7060 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7061 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7062 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7063 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, ARB_POINT_PARAMETERS },
7064 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, EXT_POINT_PARAMETERS },
7065 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
7066 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7067 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7068 /* Samplers for NP2 texture matrix adjustions. They are not needed if
7069 * GL_ARB_texture_non_power_of_two is supported, so register a NULL state
7070 * handler in that case to get the vertex part of sampler() skipped (VTF
7071 * is handled in the misc states). Otherwise, register
7072 * sampler_texmatrix(), which takes care of updating the texture matrix. */
7073 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7074 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7075 {STATE_SAMPLER(0), {STATE_SAMPLER(0), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7076 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7077 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7078 {STATE_SAMPLER(1), {STATE_SAMPLER(1), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7079 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7080 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7081 {STATE_SAMPLER(2), {STATE_SAMPLER(2), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7082 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7083 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7084 {STATE_SAMPLER(3), {STATE_SAMPLER(3), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7085 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7086 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7087 {STATE_SAMPLER(4), {STATE_SAMPLER(4), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7088 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7089 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7090 {STATE_SAMPLER(5), {STATE_SAMPLER(5), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7091 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7092 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7093 {STATE_SAMPLER(6), {STATE_SAMPLER(6), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7094 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7095 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7096 {STATE_SAMPLER(7), {STATE_SAMPLER(7), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7097 {STATE_POINT_SIZE_ENABLE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7098 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
7101 /* TODO:
7102 * - This currently depends on GL fixed function functions to set things
7103 * like light parameters. Ideally we'd use regular uniforms for that.
7104 * - In part because of the previous point, much of this is modelled after
7105 * GL fixed function, and has much of the same limitations. For example,
7106 * D3D spot lights are slightly different from GL spot lights.
7107 * - We can now implement drawing transformed vertices using the GLSL pipe,
7108 * instead of using the immediate mode fallback.
7109 * - Similarly, we don't need the fallback for certain combinations of
7110 * material sources anymore.
7111 * - Implement vertex blending and vertex tweening.
7112 * - Handle WINED3D_TSS_TEXCOORD_INDEX in the shader, instead of duplicating
7113 * attribute arrays in load_tex_coords().
7114 * - Per-vertex point sizes. */
7115 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
7117 glsl_vertex_pipe_vp_enable,
7118 glsl_vertex_pipe_vp_get_caps,
7119 glsl_vertex_pipe_vp_alloc,
7120 glsl_vertex_pipe_vp_free,
7121 glsl_vertex_pipe_vp_states,
7124 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
7126 /* Nothing to do. */
7129 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
7131 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
7132 | WINED3D_FRAGMENT_CAP_SRGB_WRITE;
7133 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
7134 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
7135 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
7136 | WINED3DTEXOPCAPS_SELECTARG1
7137 | WINED3DTEXOPCAPS_SELECTARG2
7138 | WINED3DTEXOPCAPS_MODULATE4X
7139 | WINED3DTEXOPCAPS_MODULATE2X
7140 | WINED3DTEXOPCAPS_MODULATE
7141 | WINED3DTEXOPCAPS_ADDSIGNED2X
7142 | WINED3DTEXOPCAPS_ADDSIGNED
7143 | WINED3DTEXOPCAPS_ADD
7144 | WINED3DTEXOPCAPS_SUBTRACT
7145 | WINED3DTEXOPCAPS_ADDSMOOTH
7146 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
7147 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
7148 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
7149 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
7150 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
7151 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
7152 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
7153 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
7154 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
7155 | WINED3DTEXOPCAPS_DOTPRODUCT3
7156 | WINED3DTEXOPCAPS_MULTIPLYADD
7157 | WINED3DTEXOPCAPS_LERP
7158 | WINED3DTEXOPCAPS_BUMPENVMAP
7159 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
7160 caps->MaxTextureBlendStages = 8;
7161 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
7164 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7166 struct shader_glsl_priv *priv;
7168 if (shader_backend == &glsl_shader_backend)
7170 priv = shader_priv;
7172 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
7174 ERR("Failed to initialize rbtree.\n");
7175 return NULL;
7178 return priv;
7181 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
7183 return NULL;
7186 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
7188 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7189 struct glsl_ffp_fragment_shader, entry.entry);
7190 struct glsl_shader_prog_link *program, *program2;
7191 struct glsl_ffp_destroy_ctx *ctx = context;
7193 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7194 struct glsl_shader_prog_link, ps.shader_entry)
7196 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7198 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
7199 HeapFree(GetProcessHeap(), 0, shader);
7202 /* Context activation is done by the caller. */
7203 static void glsl_fragment_pipe_free(struct wined3d_device *device)
7205 struct shader_glsl_priv *priv = device->fragment_priv;
7206 struct glsl_ffp_destroy_ctx ctx;
7208 ctx.priv = priv;
7209 ctx.gl_info = &device->adapter->gl_info;
7210 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
7213 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
7214 const struct wined3d_state *state, DWORD state_id)
7216 context->last_was_pshader = use_ps(state);
7218 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7221 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
7222 const struct wined3d_state *state, DWORD state_id)
7224 BOOL use_vshader = use_vs(state);
7225 enum fogsource new_source;
7226 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
7227 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
7229 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7231 if (!state->render_states[WINED3D_RS_FOGENABLE])
7232 return;
7234 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
7236 if (use_vshader)
7237 new_source = FOGSOURCE_VS;
7238 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->last_was_rhw)
7239 new_source = FOGSOURCE_COORD;
7240 else
7241 new_source = FOGSOURCE_FFP;
7243 else
7245 new_source = FOGSOURCE_FFP;
7248 if (new_source != context->fog_source || fogstart == fogend)
7250 context->fog_source = new_source;
7251 state_fogstartend(context, state, STATE_RENDER(WINED3D_RS_FOGSTART));
7255 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
7256 const struct wined3d_state *state, DWORD state_id)
7258 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7261 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
7262 const struct wined3d_state *state, DWORD state_id)
7264 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7267 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
7269 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7270 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7271 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7272 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7273 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7274 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7275 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7276 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7277 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7278 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7279 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7280 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7281 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7282 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7283 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7284 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7285 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7286 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7287 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7288 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7289 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7290 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7291 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7292 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7293 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7294 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7295 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7296 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7297 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7298 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7299 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7300 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7301 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7302 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7303 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7304 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7305 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7306 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7307 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7308 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7309 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7310 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7311 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7312 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7313 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7314 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7315 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7316 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7317 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7318 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7319 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7320 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7321 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7322 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7323 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7324 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7325 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7326 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7327 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7328 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7329 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7330 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7331 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7332 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7333 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7334 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7335 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7336 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7337 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7338 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7339 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7340 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7341 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7342 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
7343 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
7344 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7345 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7346 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
7347 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
7348 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
7349 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7350 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
7351 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
7352 {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 },
7353 {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 },
7354 {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 },
7355 {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 },
7356 {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 },
7357 {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 },
7358 {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 },
7359 {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 },
7360 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7361 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7362 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7363 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7364 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7365 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7366 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7367 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7368 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7369 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
7372 const struct fragment_pipeline glsl_fragment_pipe =
7374 glsl_fragment_pipe_enable,
7375 glsl_fragment_pipe_get_caps,
7376 glsl_fragment_pipe_alloc,
7377 glsl_fragment_pipe_free,
7378 shader_glsl_color_fixup_supported,
7379 glsl_fragment_pipe_state_template,